Prompt specification
Build or customize the Three.js 3D scene "Blades respond to passing waves" inside the active workspace.
Engineering Constraints:
1. Runtime & Orbit: Use Three.js r160 with OrbitControls (damping enabled) and ACESFilmicToneMapping.
2. Frame Timing: Drive all motion strictly with clock.getDelta() so animation speed is FPS-independent.
3. Viewport Hygiene: Handle window resize cleanly, cap DPR at Math.min(window.devicePixelRatio, 2), and dispose geometries/materials/renderer on unmount.
Scene Metadata:
Scene: Blades respond to passing waves
Category: Biology & Nature · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/blades-respond-to-passing-waves-x0774
Scene Description Prompt:
Low-poly seagrass bends with prescribed waves while roots stay anchored; illustrate particles slowing within the patch. Three.js from cdnjs, one HTML file.
Working Three.js Starter (blades-respond-to-passing-waves-x0774):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Root-Pinned Seagrass Wave Bed</title>
<style>
:root {
color-scheme: dark;
--panel: rgba(5, 30, 36, 0.78);
--line: rgba(165, 240, 226, 0.22);
--fast: #ffae62;
--slow: #55ead2;
--text: #e8fff8;
--muted: #9dc5bd;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #062e36;
font-family: Inter, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}
canvas {
display: block;
touch-action: none;
}
.panel {
position: fixed;
top: 20px;
left: 20px;
width: min(330px, calc(100vw - 40px));
padding: 17px 18px 15px;
color: var(--text);
background: var(--panel);
border: 1px solid var(--line);
border-radius: 15px;
box-shadow: 0 16px 50px rgba(0, 0, 0, 0.28);
backdrop-filter: blur(12px);
user-select: none;
}
.eyebrow {
margin-bottom: 5px;
color: var(--slow);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.18em;
text-transform: uppercase;
}
h1 {
margin: 0 0 5px;
font-size: 21px;
line-height: 1.15;
letter-spacing: -0.02em;
}
.subtitle {
margin: 0 0 15px;
color: var(--muted);
font-size: 12px;
line-height: 1.45;
}
.metric {
display: flex;
align-items: baseline;
justify-content: space-between;
margin-top: 13px;
color: var(--muted);
font-size: 11px;
}
.metric strong {
color: var(--text);
font-size: 13px;
font-variant-numeric: tabular-nums;
}
.bar {
height: 6px;
margin-top: 7px;
overflow: hidden;
background: rgba(255, 255, 255, 0.08);
border-radius: 99px;
}
.bar > span {
display: block;
width: 50%;
height: 100%;
background: linear-gradient(90deg, var(--slow), var(--fast));
border-radius: inherit;
transition: width 100ms linear;
}
.legend {
display: flex;
gap: 16px;
margin-top: 14px;
color: var(--muted);
font-size: 11px;
}
.legend span {
display: inline-flex;
align-items: center;
gap: 6px;
}
.dot {
width: 8px;
height: 8px;
border-radius: 50%;
box-shadow: 0 0 10px currentColor;
}
.dot.fast {
color: var(--fast);
background: var(--fast);
}
.dot.slow {
color: var(--slow);
background: var(--slow);
}
.wave-box {
margin-top: 15px;
padding-top: 12px;
border-top: 1px solid var(--line);
}
.wave-title {
display: flex;
justify-content: space-between;
margin-bottom: 6px;
color: var(--muted);
font-size: 10px;
letter-spacing: 0.08em;
text-transform: uppercase;
}
#wavePlot {
width: 100%;
height: 58px;
border-radius: 8px;
background: rgba(0, 13, 18, 0.38);
}
.formula {
margin-top: 7px;
color: #b8ddd4;
font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
font-size: 10px;
line-height: 1.45;
}
.controls {
position: fixed;
right: 20px;
bottom: 20px;
display: flex;
align-items: center;
gap: 12px;
color: rgba(222, 255, 247, 0.72);
font-size: 11px;
text-shadow: 0 1px 8px #00191e;
pointer-events: none;
}
button {
padding: 9px 13px;
color: var(--text);
background: rgba(5, 30, 36, 0.8);
border: 1px solid var(--line);
border-radius: 9px;
font: inherit;
font-size: 11px;
cursor: pointer;
pointer-events: auto;
backdrop-filter: blur(8px);
}
button:hover {
border-color: rgba(85, 234, 210, 0.65);
background: rgba(13, 58, 62, 0.9);
}
@media (max-width: 600px) {
.panel {
top: 10px;
left: 10px;
width: min(300px, calc(100vw - 20px));
padding: 13px 14px;
}
h1 {
font-size: 18px;
}
.subtitle {
margin-bottom: 10px;
}
.controls {
right: 12px;
bottom: 12px;
}
.hint {
display: none;
}
}
</style>
</head>
<body>
<section class="panel">
<div class="eyebrow">Root-pinned wave bed</div>
<h1>Seagrass slows the current</h1>
<p class="subtitle">
A prescribed traveling wave bends every blade while its base remains fixed.
Tracers turn cyan and shorten as they enter the canopy.
</p>
<div class="metric">
<span>Mean tracer speed</span>
<strong id="speedValue">1.00 m/s</strong>
</div>
<div class="bar"><span id="speedBar"></span></div>
<div class="metric">
<span>Tracers inside patch</span>
<strong id="insideValue">0</strong>
</div>
<div class="legend">
<span><i class="dot fast"></i>Fast flow</span>
<span><i class="dot slow"></i>Slowed in grass</span>
</div>
<div class="wave-box">
<div class="wave-title">
<span>Prescribed wave</span>
<span id="waveState">Traveling</span>
</div>
<canvas id="wavePlot"></canvas>
<div class="formula">
θ(u,t) = u¹·⁶⁵ · [A sin(kz − ωt + φ),<br />
B sin(k′x + ω′t + φ′)]
</div>
</div>
</section>
<div class="controls">
<span class="hint">Drag to orbit · Scroll to zoom · R to reset</span>
<button id="pauseButton" type="button">Pause</button>
</div>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// ------------------------------------------------------------
// Renderer and scene
// ------------------------------------------------------------
const renderer = new THREE.WebGLRenderer({
antialias: true,
alpha: false,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.08;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
document.body.prepend(renderer.domElement);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x062f38);
scene.fog = new THREE.Fog(0x062f38, 13, 36);
const camera = new THREE.PerspectiveCamera(
43,
window.innerWidth / window.innerHeight,
0.1,
80
);
const cameraState = {
theta: 0.53,
phi: 1.02,
radius: 17.2,
target: new THREE.Vector3(0, 1.05, 0)
};
function updateCamera() {
const s = Math.sin(cameraState.phi);
const c = Math.cos(cameraState.phi);
camera.position.set(
cameraState.target.x + cameraState.radius * s * Math.sin(cameraState.theta),
cameraState.target.y + cameraState.radius * c,
cameraState.target.z + cameraState.radius * s * Math.cos(cameraState.theta)
);
camera.lookAt(cameraState.target);
}
updateCamera();
// ------------------------------------------------------------
// Lighting
// ------------------------------------------------------------
const hemisphere = new THREE.HemisphereLight(0xb9fff1, 0x4b3c25, 1.8);
scene.add(hemisphere);
const sun = new THREE.DirectionalLight(0xffedc3, 2.6);
sun.position.set(6, 12, 7);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -10;
sun.shadow.camera.right = 10;
sun.shadow.camera.top = 9;
sun.shadow.camera.bottom = -9;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 30;
sun.shadow.bias = -0.00035;
scene.add(sun);
const rim = new THREE.DirectionalLight(0x3bc7d0, 1.1);
rim.position.set(-7, 5, -8);
scene.add(rim);
// ------------------------------------------------------------
// Utilities
// ------------------------------------------------------------
function mulberry32(seed) {
return function () {
let t = seed += 0x6D2B79F5;
t = Math.imul(t ^ (t >>> 15), t | 1);
t ^= t + Math.imul(t ^ (t >>> 7), t | 61);
return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
};
}
const random = mulberry32(20250308);
const rand = (min, max) => min + (max - min) * random();
const clamp = (value, min, max) => Math.max(min, Math.min(max, value));
function smoothstep(edge0, edge1, value) {
const t = clamp((value - edge0) / (edge1 - edge0), 0, 1);
return t * t * (3 - 2 * t);
}
const patchRadiusX = 5.2;
const patchRadiusZ = 2.8;
function patchRadius(x, z) {
return Math.hypot(x / patchRadiusX, z / patchRadiusZ);
}
function terrainHeight(x, z) {
const edge = smoothstep(0.88, 1.6, patchRadius(x, z));
const ripple =
0.095 * Math.sin(x * 0.62 + z * 0.31) +
0.055 * Math.sin(z * 1.18 - x * 0.18);
return -0.45 + ripple * edge;
}
// ------------------------------------------------------------
// Low-poly sand bed
// ------------------------------------------------------------
const terrainGeometry = new THREE.PlaneGeometry(34, 24, 18, 13);
terrainGeometry.rotateX(-Math.PI / 2);
const terrainPosition = terrainGeometry.attributes.position;
const terrainColors = new Float32Array(terrainPosition.count * 3);
const terrainColor = new THREE.Color();
for (let i = 0; i < terrainPosition.count; i++) {
const x = terrainPosition.getX(i);
const z = terrainPosition.getZ(i);
terrainPosition.setY(i, terrainHeight(x, z));
const variation = 0.5 + 0.5 * Math.sin(x * 0.43 + z * 0.77);
terrainColor.setHSL(
0.105 + variation * 0.018,
0.28,
0.36 + variation * 0.055
);
terrainColors[i * 3] = terrainColor.r;
terrainColors[i * 3 + 1] = terrainColor.g;
terrainColors[i * 3 + 2] = terrainColor.b;
}
terrainGeometry.setAttribute(
"color",
new THREE.BufferAttribute(terrainColors, 3)
);
terrainGeometry.computeVertexNormals();
const terrain = new THREE.Mesh(
terrainGeometry,
new THREE.MeshStandardMaterial({
vertexColors: true,
roughness: 0.98,
metalness: 0,
flatShading: true
})
);
terrain.receiveShadow = true;
scene.add(terrain);
// Subtle patch footprint.
const patchGeometry = new THREE.CircleGeometry(1, 64);
patchGeometry.rotateX(-Math.PI / 2);
const patch = new THREE.Mesh(
patchGeometry,
new THREE.MeshBasicMaterial({
color: 0x2e9b69,
transparent: true,
opacity: 0.13,
depthWrite: false,
side: THREE.DoubleSide
})
);
patch.scale.set(patchRadiusX, 1, patchRadiusZ);
patch.position.y = -0.438;
scene.add(patch);
// ------------------------------------------------------------
// Low-poly stones and rhizome anchors
// ------------------------------------------------------------
const stoneCount = 65;
const stoneGeometry = new THREE.DodecahedronGeometry(0.12, 0);
const stoneMaterial = new THREE.MeshStandardMaterial({
color: 0x77705a,
roughness: 1,
flatShading: true
});
const stones = new THREE.InstancedMesh(stoneGeometry, stoneMaterial, stoneCount);
const dummy = new THREE.Object3D();
for (let i = 0; i < stoneCount; i++) {
const angle = rand(0, Math.PI * 2);
const radius = Math.sqrt(random()) * 5.7;
const x = Math.cos(angle) * radius;
const z = Math.sin(angle) * radius * 0.55;
dummy.position.set(x, terrainHeight(x, z) + 0.035, z);
dummy.rotation.set(rand(0, 3), rand(0, 3), rand(0, 3));
dummy.scale.set(
rand(0.55, 1.35),
rand(0.35, 0.8),
rand(0.55, 1.3)
);
dummy.updateMatrix();
stones.setMatrixAt(i, dummy.matrix);
}
stones.castShadow = true;
stones.receiveShadow = true;
scene.add(stones);
// ------------------------------------------------------------
// Seagrass: one dynamic, non-indexed low-poly mesh
// ------------------------------------------------------------
const bladeCount = 155;
const levels = 6;
const verticesPerBlade = levels * 6;
const grassPosition = new Float32Array(bladeCount * verticesPerBlade * 3);
const grassColor = new Float32Array(grassPosition.length);
const blades = [];
const grassGeometry = new THREE.BufferGeometry();
const grassPositionAttribute = new THREE.BufferAttribute(grassPosition, 3);
grassPositionAttribute.setUsage(THREE.DynamicDrawUsage);
grassGeometry.setAttribute(
"position",
grassPositionAttribute
);
grassGeometry.setAttribute(
"color",
new THREE.BufferAttribute(grassColor, 3)
);
function writeColor(vertexIndex, color, u) {
const offset = vertexIndex * 3;
grassColor[offset] = color.r;
grassColor[offset + 1] = color.g;
grassColor[offset + 2] = color.b;
}
for (let i = 0; i < bladeCount; i++) {
const angle = rand(0, Math.PI * 2);
const radius = Math.sqrt(random()) * 0.98;
const x = Math.cos(angle) * radius * 5.1;
const z = Math.sin(angle) * radius * 2.65;
const height = 1.05 + rand(0, 1.7) + (1 - radius) * 0.52;
const blade = {
x,
z,
y: terrainHeight(x, z) + 0.01,
height,
width: rand(0.055, 0.13),
azimuth: rand(0, Math.PI * 2),
phase: rand(0, Math.PI * 2),
phase2: rand(0, Math.PI * 2),
flex: rand(0.65, 1.35),
levelData: new Float32Array((levels + 1) * 6)
};
blades.push(blade);
const baseColor = new THREE.Color().setHSL(
rand(0.31, 0.38),
rand(0.52, 0.72),
rand(0.15, 0.23)
);
const tipColor = new THREE.Color().setHSL(
rand(0.25, 0.34),
rand(0.48, 0.68),
rand(0.34, 0.49)
);
const color = new THREE.Color();
let vertexIndex = i * verticesPerBlade;
for (let segment = 0; segment < levels; segment++) {
const u0 = segment / levels;
const u1 = (segment + 1) / levels;
// Triangle 1: lower-left, lower-right, upper-left.
color.copy(baseColor).lerp(tipColor, u0 * 0.5);
writeColor(vertexIndex++, color, u0);
color.copy(baseColor).lerp(tipColor, u0 * 0.5);
writeColor(vertexIndex++, color, u0);
color.copy(baseColor).lerp(tipColor, u1 * 0.5);
writeColor(vertexIndex++, color, u1);
// Triangle 2: lower-right, upper-right, upper-left.
color.copy(baseColor).lerp(tipColor, u0 * 0.5);
writeColor(vertexIndex++, color, u0);
color.copy(baseColor).lerp(tipColor, u1 * 0.5);
writeColor(vertexIndex++, color, u1);
color.copy(baseColor).lerp(tipColor, u1 * 0.5);
writeColor(vertexIndex++, color, u1);
}
}
const grassMaterial = new THREE.MeshStandardMaterial({
vertexColors: true,
side: THREE.DoubleSide,
roughness: 0.88,
metalness: 0,
flatShading: true
});
const grass = new THREE.Mesh(grassGeometry, grassMaterial);
grass.castShadow = true;
grass.receiveShadow = true;
grass.frustumCulled = false;
scene.add(grass);
function updateGrass(time) {
let positionIndex = 0;
for (const blade of blades) {
const data = blade.levelData;
const segmentLength = blade.height / levels;
// Level zero is explicitly pinned to the root.
data[0] = blade.x;
data[1] = blade.y;
data[2] = blade.z;
data[3] = Math.cos(blade.azimuth);
data[4] = 0;
data[5] = Math.sin(blade.azimuth);
let centerX = blade.x;
let centerY = blade.y;
let centerZ = blade.z;
for (let level = 1; level <= levels; level++) {
const u = (level - 0.5) / levels;
// Prescribed traveling-wave field.
const waveX =
Math.sin(0.78 * blade.z - 1.55 * time + blade.phase) *
(0.30 + blade.flex * 0.16);
const waveZ =
Math.sin(0.62 * blade.x + 0.92 * time + blade.phase2) * 0.13;
const bendX =
Math.sin(u * 1.65) * waveX +
0.045 * Math.sin(u * 2.2 + blade.phase2 + time * 1.8);
const bendZ =
Math.sin(u * 1.55) * waveZ +
0.025 * Math.sin(u * 2.4 - blade.phase + time * 1.2);
const horizontalX = Math.sin(bendX);
const horizontalZ = Math.sin(bendZ);
const horizontalMagnitude = Math.hypot(horizontalX, horizontalZ);
const vertical = Math.sqrt(
Math.max(0.08, 1 - horizontalMagnitude * horizontalMagnitude)
);
// Integrate a constant-length centerline, so the root never moves.
centerX += horizontalX * segmentLength;
centerY += vertical * segmentLength;
centerZ += horizontalZ * segmentLength;
let sideX = Math.cos(blade.azimuth);
let sideZ = Math.sin(blade.azimuth);
if (horizontalMagnitude > 0.001) {
const perpendicularX = -horizontalZ / horizontalMagnitude;
const perpendicularZ = horizontalX / horizontalMagnitude;
const blend = smoothstep(0.02, 0.32, horizontalMagnitude);
sideX = THREE.MathUtils.lerp(sideX, perpendicularX, blend);
sideZ = THREE.MathUtils.lerp(sideZ, perpendicularZ, blend);
}
const sideLength = Math.hypot(sideX, sideZ) || 1;
sideX /= sideLength;
sideZ /= sideLength;
const offset = level * 6;
data[offset] = centerX;
data[offset + 1] = centerY;
data[offset + 2] = centerZ;
data[offset + 3] = sideX;
data[offset + 4] = 0;
data[offset + 5] = sideZ;
}
for (let level = 0; level < levels; level++) {
const lower = level * 6;
const upper = (level + 1) * 6;
const u0 = level / levels;
const u1 = (level + 1) / levels;
const width0 = blade.width * Math.pow(1 - u0, 0.72);
const width1 = blade.width * Math.pow(1 - u1, 0.72);
const p0L = [
data[lower] + data[lower + 3] * width0 * 0.5,
data[lower + 1],
data[lower + 2] + data[lower + 5] * width0 * 0.5
];
const p0R = [
data[lower] - data[lower + 3] * width0 * 0.5,
data[lower + 1],
data[lower + 2] - data[lower + 5] * width0 * 0.5
];
const p1L = [
data[upper] + data[upper + 3] * width1 * 0.5,
data[upper + 1],
data[upper + 2] + data[upper + 5] * width1 * 0.5
];
const p1R = [
data[upper] - data[upper + 3] * width1 * 0.5,
data[upper + 1],
data[upper + 2] - data[upper + 5] * width1 * 0.5
];
const triangle = (a, b, c) => {
grassPosition[positionIndex++] = a[0];
grassPosition[positionIndex++] = a[1];
grassPosition[positionIndex++] = a[2];
grassPosition[positionIndex++] = b[0];
grassPosition[positionIndex++] = b[1];
grassPosition[positionIndex++] = b[2];
grassPosition[positionIndex++] = c[0];
grassPosition[positionIndex++] = c[1];
grassPosition[positionIndex++] = c[2];
};
triangle(p0L, p0R, p1L);
triangle(p0R, p1R, p1L);
}
}
grassPositionAttribute.needsUpdate = true;
}
// Initial deformation.
updateGrass(0);
// ------------------------------------------------------------
// Tracer particles and speed-dependent trails
// ------------------------------------------------------------
const particleCount = 180;
const particleGeometry = new THREE.IcosahedronGeometry(0.06, 0);
const particleMaterial = new THREE.MeshBasicMaterial({
color: 0xffffff,
transparent: true,
opacity: 0.92,
depthWrite: false
});
const particleMesh = new THREE.InstancedMesh(
particleGeometry,
particleMaterial,
particleCount
);
particleMesh.instanceMatrix.setUsage(THREE.DynamicDrawUsage);
particleMesh.frustumCulled = false;
scene.add(particleMesh);
const particles = [];
const fastColor = new THREE.Color(0xffa85d);
const slowColor = new THREE.Color(0x4df0d4);
const tracerColor = new THREE.Color();
for (let i = 0; i < particleCount; i++) {
const insideLane = i < particleCount * 0.78;
particles.push({
x: rand(-13.2, 13.2),
lane: insideLane ? rand(-2.65, 2.65) : rand(-4.5, 4.5),
y: rand(0.18, 2.65),
phase: rand(0, Math.PI * 2),
size: rand(0.65, 1.45),
jitter: rand(0.87, 1.13),
speed: 1.3
});
}
const trailPositions = new Float32Array(particleCount * 6);
const trailColors = new Float32Array(particleCount * 6);
const trailGeometry = new THREE.BufferGeometry();
trailGeometry.setAttribute(
"position",
new THREE.BufferAttribute(trailPositions, 3).setUsage(THREE.DynamicDrawUsage)
);
trailGeometry.setAttribute(
"color",
new THREE.BufferAttribute(trailColors, 3).setUsage(THREE.DynamicDrawUsage)
);
const trails = new THREE.LineSegments(
trailGeometry,
new THREE.LineBasicMaterial({
vertexColors: true,
transparent: true,
opacity: 0.48,
depthWrite: false,
blending: THREE.AdditiveBlending
})
);
trails.frustumCulled = false;
scene.add(trails);
let meanSpeed = 1;
let insideCount = 0;
function updateParticles(delta, time) {
let speedTotal = 0;
insideCount = 0;
for (let i = 0; i < particles.length; i++) {
const particle = particles[i];
const z =
particle.lane +
0.13 * Math.sin(time * 0.55 + particle.phase) +
0.045 * Math.sin(time * 1.3 + particle.phase * 2);
const radius = patchRadius(particle.x, z);
const patchInfluence = 1 - smoothstep(0.78, 1.14, radius);
const targetSpeed =
THREE.MathUtils.lerp(1.62, 0.39, patchInfluence) *
particle.jitter;
particle.speed +=
(targetSpeed - particle.speed) * Math.min(1, delta * 4.5);
particle.x += particle.speed * delta;
if (particle.x > 13.5) {
particle.x = -13.5;
}
const y =
particle.y +
0.075 * Math.sin(time * 0.8 + particle.phase);
tracerColor.copy(fastColor).lerp(slowColor, patchInfluence);
dummy.position.set(particle.x, y, z);
dummy.rotation.set(0, 0, 0);
dummy.scale.setScalar(particle.size);
dummy.updateMatrix();
particleMesh.setMatrixAt(i, dummy.matrix);
particleMesh.setColorAt(i, tracerColor);
// Faster tracers receive longer streaks.
const tailLength = 0.09 + particle.speed * 0.27;
const trailOffset = i * 6;
trailPositions[trailOffset] = particle.x;
trailPositions[trailOffset + 1] = y;
trailPositions[trailOffset + 2] = z;
trailPositions[trailOffset + 3] = particle.x - tailLength;
trailPositions[trailOffset + 4] = y;
trailPositions[trailOffset + 5] = z;
trailColors[trailOffset] = tracerColor.r;
trailColors[trailOffset + 1] = tracerColor.g;
trailColors[trailOffset + 2] = tracerColor.b;
trailColors[trailOffset + 3] = tracerColor.r * 0.18;
trailColors[trailOffset + 4] = tracerColor.g * 0.18;
trailColors[trailOffset + 5] = tracerColor.b * 0.18;
speedTotal += particle.speed;
if (radius < 1) {
insideCount++;
}
}
meanSpeed = speedTotal / particles.length;
particleMesh.instanceMatrix.needsUpdate = true;
particleMesh.instanceColor.needsUpdate = true;
trailGeometry.attributes.position.needsUpdate = true;
trailGeometry.attributes.color.needsUpdate = true;
}
// ------------------------------------------------------------
// Direct camera controls — no addons
// ------------------------------------------------------------
let dragging = false;
let previousPointerX = 0;
let previousPointerY = 0;
renderer.domElement.addEventListener("pointerdown", (event) => {
dragging = true;
previousPointerX = event.clientX;
previousPointerY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!dragging) return;
const deltaX = event.clientX - previousPointerX;
const deltaY = event.clientY - previousPointerY;
previousPointerX = event.clientX;
previousPointerY = event.clientY;
cameraState.theta -= deltaX * 0.006;
cameraState.phi = clamp(cameraState.phi + deltaY * 0.005, 0.35, 1.42);
});
function endDrag(event) {
dragging = false;
if (event.pointerId !== undefined &&
renderer.domElement.hasPointerCapture(event.pointerId)) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
}
renderer.domElement.addEventListener("pointerup", endDrag);
renderer.domElement.addEventListener("pointercancel", endDrag);
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraState.radius = clamp(
cameraState.radius + event.deltaY * 0.012,
10.5,
27
);
},
{ passive: false }
);
function resetCamera() {
cameraState.theta = 0.53;
cameraState.phi = 1.02;
cameraState.radius = 17.2;
cameraState.target.set(0, 1.05, 0);
}
renderer.domElement.addEventListener("dblclick", resetCamera);
// ------------------------------------------------------------
// UI, pause control, and wave plot
// ------------------------------------------------------------
const pauseButton = document.getElementById("pauseButton");
const speedValue = document.getElementById("speedValue");
const speedBar = document.getElementById("speedBar");
const insideValue = document.getElementById("insideValue");
const waveState = document.getElementById("waveState");
const waveCanvas = document.getElementById("wavePlot");
const waveContext = waveCanvas.getContext("2d");
let paused = false;
function togglePause() {
paused = !paused;
pauseButton.textContent = paused ? "Resume" : "Pause";
waveState.textContent = paused ? "Paused" : "Traveling";
}
pauseButton.addEventListener("click", togglePause);
window.addEventListener("keydown", (event) => {
if (event.code === "Space") {
event.preventDefault();
togglePause();
}
if (event.key.toLowerCase() === "r") {
resetCamera();
}
});
let waveWidth = 0;
let waveHeight = 0;
let waveDpr = 1;
function resizeWavePlot() {
const rect = waveCanvas.getBoundingClientRect();
waveDpr = Math.min(window.devicePixelRatio, 2);
waveWidth = rect.width;
waveHeight = rect.height;
waveCanvas.width = Math.round(waveWidth * waveDpr);
waveCanvas.height = Math.round(waveHeight * waveDpr);
waveContext.setTransform(waveDpr, 0, 0, waveDpr, 0, 0);
}
resizeWavePlot();
window.addEventListener("resize", resizeWavePlot);
function drawWavePlot(time) {
const ctx = waveContext;
const width = waveWidth;
const height = waveHeight;
ctx.clearRect(0, 0, width, height);
ctx.strokeStyle = "rgba(150, 220, 210, 0.10)";
ctx.lineWidth = 1;
for (let y = height * 0.25; y < height; y += height * 0.25) {
ctx.beginPath();
ctx.moveTo(0, y);
ctx.lineTo(width, y);
ctx.stroke();
}
const gradient = ctx.createLinearGradient(0, 0, width, 0);
gradient.addColorStop(0, "#ffae62");
gradient.addColorStop(0.5, "#55ead2");
gradient.addColorStop(1, "#ffae62");
ctx.strokeStyle = gradient;
ctx.lineWidth = 2;
ctx.beginPath();
for (let x = 0; x <= width; x += 2) {
const u = x / width;
const y =
height * 0.53 -
Math.sin(u * Math.PI * 4 - time * 1.55 + 0.4) *
height * 0.27;
if (x === 0) {
ctx.moveTo(x, y);
} else {
ctx.lineTo(x, y);
}
}
ctx.stroke();
}
// ------------------------------------------------------------
// Animation
// ------------------------------------------------------------
const clock = new THREE.Clock();
let elapsed = 0;
let telemetryTimer = 0;
function animate() {
const delta = Math.min(clock.getDelta(), 0.04);
if (!paused) {
elapsed += delta;
updateGrass(elapsed);
updateParticles(delta, elapsed);
}
updateCamera();
drawWavePlot(elapsed);
telemetryTimer += delta;
if (telemetryTimer > 0.1) {
telemetryTimer = 0;
speedValue.textContent = `${meanSpeed.toFixed(2)} m/s`;
speedBar.style.width = `${clamp(meanSpeed / 1.65, 0, 1) * 100}%`;
insideValue.textContent = `${insideCount} / ${particleCount}`;
}
renderer.render(scene, camera);
}
renderer.setAnimationLoop(animate);
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
resizeWavePlot();
});
</script>
</body>
</html>
```