Prompt specification
Build or customize the Three.js 3D scene "Braided-river gravel bar and shallow channel tile" 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: Braided-river gravel bar and shallow channel tile
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/braided-river-gravel-bar-and-shallow-channel-tile-x3097
Scene Description Prompt:
Please use Three.js from cdnjs for a voxel scene based on this idea: braided-river gravel bar and shallow channel tile. Build a referenced New Zealand alpine-river fragment with gravel bar and two shallow channels. Show water dividing around the bar and rejoining downstream. Return one self-contained HTML file.
Working Three.js Starter (braided-river-gravel-bar-and-shallow-channel-tile-x3097):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Braided Alpine River — Voxel Field Tile</title>
<style>
:root {
--ink: #20312f;
--muted: #61716d;
--panel: rgba(245, 249, 246, 0.82);
--line: rgba(38, 65, 60, 0.17);
--water: #65b9c1;
--gravel: #b5a78e;
--alpine: #73806a;
--snow: #edf1eb;
}
* { box-sizing: border-box; }
html, body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #d4e0dc;
color: var(--ink);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
#scene {
position: fixed;
inset: 0;
}
canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
outline: none;
}
#vignette {
position: fixed;
inset: 0;
pointer-events: none;
background:
radial-gradient(circle at 50% 45%, transparent 48%, rgba(38, 62, 57, 0.11) 100%),
linear-gradient(to bottom, rgba(255,255,255,0.08), transparent 30%);
}
.hud {
position: fixed;
inset: 0;
pointer-events: none;
}
.title-card {
position: absolute;
top: 24px;
left: 24px;
width: min(390px, calc(100vw - 48px));
padding: 18px 20px 17px;
border: 1px solid var(--line);
border-radius: 4px;
background: var(--panel);
box-shadow: 0 14px 45px rgba(35, 61, 55, 0.10);
backdrop-filter: blur(12px);
}
.eyebrow {
margin-bottom: 8px;
color: #688079;
font-size: 10px;
font-weight: 750;
letter-spacing: 0.18em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-family: Georgia, "Times New Roman", serif;
font-size: clamp(25px, 3vw, 38px);
font-weight: 500;
line-height: 1.03;
letter-spacing: -0.035em;
}
.subtitle {
margin: 10px 0 0;
max-width: 330px;
color: var(--muted);
font-size: 12px;
line-height: 1.55;
}
.legend {
position: absolute;
top: 24px;
right: 24px;
min-width: 184px;
padding: 14px 15px;
border: 1px solid var(--line);
border-radius: 4px;
background: var(--panel);
box-shadow: 0 14px 45px rgba(35, 61, 55, 0.08);
backdrop-filter: blur(12px);
}
.legend-title {
margin-bottom: 10px;
color: #73817d;
font-size: 9px;
font-weight: 750;
letter-spacing: 0.16em;
text-transform: uppercase;
}
.legend-row {
display: flex;
align-items: center;
gap: 9px;
margin: 8px 0;
color: #4e625d;
font-size: 11px;
}
.swatch {
width: 15px;
height: 8px;
flex: 0 0 auto;
border-radius: 1px;
box-shadow: inset 0 0 0 1px rgba(28, 48, 43, 0.10);
}
.controls {
position: absolute;
right: 24px;
bottom: 22px;
display: flex;
align-items: center;
gap: 8px;
pointer-events: auto;
}
button {
appearance: none;
border: 1px solid var(--line);
border-radius: 3px;
padding: 9px 12px;
color: #39524c;
background: rgba(248, 251, 248, 0.88);
font: inherit;
font-size: 10px;
font-weight: 700;
letter-spacing: 0.08em;
text-transform: uppercase;
cursor: pointer;
backdrop-filter: blur(10px);
transition: background 150ms ease, transform 150ms ease;
}
button:hover {
background: rgba(255, 255, 255, 0.98);
transform: translateY(-1px);
}
button:focus-visible {
outline: 2px solid #62a8ad;
outline-offset: 2px;
}
.hint {
position: absolute;
left: 24px;
bottom: 24px;
padding: 9px 11px;
border: 1px solid var(--line);
border-radius: 3px;
color: #5e716c;
background: rgba(245, 249, 246, 0.72);
font-size: 10px;
letter-spacing: 0.025em;
backdrop-filter: blur(8px);
}
.hint b {
color: #344e48;
font-weight: 750;
}
#bar-label {
position: absolute;
left: 50%;
top: 50%;
padding: 5px 7px;
border: 1px solid rgba(61, 75, 68, 0.23);
border-radius: 2px;
color: #455851;
background: rgba(247, 244, 235, 0.82);
font-size: 8px;
font-weight: 800;
letter-spacing: 0.14em;
text-transform: uppercase;
white-space: nowrap;
transform: translate(-50%, calc(-100% - 22px));
pointer-events: none;
box-shadow: 0 5px 18px rgba(51, 64, 56, 0.08);
}
#bar-label::after {
content: "";
position: absolute;
left: 50%;
top: 100%;
width: 1px;
height: 17px;
background: rgba(61, 75, 68, 0.40);
}
#loading {
position: fixed;
inset: 0;
z-index: 10;
display: grid;
place-items: center;
color: #55706a;
background: #d4e0dc;
font-size: 10px;
font-weight: 750;
letter-spacing: 0.18em;
text-transform: uppercase;
transition: opacity 500ms ease, visibility 500ms ease;
}
#loading.hidden {
opacity: 0;
visibility: hidden;
}
@media (max-width: 720px) {
.title-card {
top: 14px;
left: 14px;
width: calc(100vw - 28px);
padding: 14px 16px;
}
.subtitle { display: none; }
h1 { max-width: 280px; font-size: 27px; }
.legend {
top: auto;
right: 14px;
bottom: 64px;
min-width: 158px;
padding: 11px 12px;
}
.hint {
left: 14px;
bottom: 14px;
max-width: calc(100vw - 180px);
}
.controls {
right: 14px;
bottom: 14px;
}
button { padding: 8px 9px; font-size: 9px; }
#bar-label { display: none; }
}
</style>
</head>
<body>
<div id="scene"></div>
<div id="vignette"></div>
<div class="hud">
<section class="title-card">
<div class="eyebrow">NZ Alpine Hydrology · Field Tile 07</div>
<h1>Braided river,<br>split and reunited</h1>
<p class="subtitle">
A procedural voxel study of a New Zealand alpine river fragment:
two shallow channels divide around a low gravel bar before rejoining.
</p>
</section>
<aside class="legend" aria-label="Scene legend">
<div class="legend-title">Surface Key</div>
<div class="legend-row">
<span class="swatch" style="background:var(--water)"></span>
Shallow channel
</div>
<div class="legend-row">
<span class="swatch" style="background:var(--gravel)"></span>
Gravel bar
</div>
<div class="legend-row">
<span class="swatch" style="background:var(--alpine)"></span>
Alpine bank
</div>
<div class="legend-row">
<span class="swatch" style="background:var(--snow)"></span>
Snow / schist
</div>
</aside>
<div id="bar-label">Gravel bar</div>
<div class="hint">
<b>Drag</b> orbit · <b>Scroll / pinch</b> zoom · <b>Shift-drag</b> pan
</div>
<div class="controls">
<button id="flow" type="button" aria-pressed="false">Pause current</button>
<button id="reset" type="button">Reset view</button>
</div>
</div>
<div id="loading">Building alpine reach</div>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const container = document.getElementById("scene");
const loading = document.getElementById("loading");
const flowButton = document.getElementById("flow");
const resetButton = document.getElementById("reset");
const barLabel = document.getElementById("bar-label");
const reducedMotion = window.matchMedia("(prefers-reduced-motion: reduce)").matches;
// -------------------------------------------------------------------------
// Renderer, camera and lighting
// -------------------------------------------------------------------------
const renderer = new THREE.WebGLRenderer({
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio || 1, 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;
renderer.domElement.setAttribute("aria-label", "Interactive voxel model of a braided alpine river");
renderer.domElement.tabIndex = 0;
container.appendChild(renderer.domElement);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xd4e0dc);
scene.fog = new THREE.Fog(0xd4e0dc, 42, 82);
const camera = new THREE.OrthographicCamera(-20, 20, 14.5, -14.5, 0.1, 120);
const view = {
yaw: 0.62,
pitch: 0.66,
zoom: 1,
target: new THREE.Vector3(0, 0.15, -0.8)
};
const hemisphere = new THREE.HemisphereLight(0xe7f5f3, 0x665a48, 1.65);
scene.add(hemisphere);
const sun = new THREE.DirectionalLight(0xfff0d2, 3.05);
sun.position.set(-13, 24, 10);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -25;
sun.shadow.camera.right = 25;
sun.shadow.camera.top = 27;
sun.shadow.camera.bottom = -27;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 75;
sun.shadow.bias = -0.00015;
sun.shadow.normalBias = 0.045;
sun.target.position.set(0, 0, -2);
scene.add(sun, sun.target);
const rim = new THREE.DirectionalLight(0x9ccbd0, 0.65);
rim.position.set(15, 10, -18);
scene.add(rim);
// -------------------------------------------------------------------------
// River geometry helpers
// -------------------------------------------------------------------------
const WIDTH = 22;
const LENGTH = 32;
const CELL = 0.5;
const COLS = Math.round(WIDTH / CELL);
const ROWS = Math.round(LENGTH / CELL);
const BOTTOM = -1.15;
const WATER_LEVEL = 0.14;
function clamp(value, min, max) {
return Math.min(max, Math.max(min, value));
}
function smoothstep(a, b, x) {
const t = clamp((x - a) / (b - a), 0, 1);
return t * t * (3 - 2 * t);
}
function hash2(ix, iz) {
let h = Math.imul(ix + 1013, 374761393) ^ Math.imul(iz + 2027, 668265263);
h = Math.imul(h ^ (h >>> 13), 1274126177);
return ((h ^ (h >>> 16)) >>> 0) / 4294967295;
}
function randomGenerator(seed) {
return function () {
seed |= 0;
seed = seed + 0x6D2B79F5 | 0;
let t = Math.imul(seed ^ seed >>> 15, 1 | seed);
t = t + Math.imul(t ^ t >>> 7, 61 | t) ^ t;
return ((t ^ t >>> 14) >>> 0) / 4294967296;
};
}
const random = randomGenerator(724051);
// River meanders gently upstream to downstream.
function riverCenter(z) {
return 0.66 * Math.sin((z + 5) * 0.14) + 0.22 * Math.sin(z * 0.36);
}
// Zero upstream, wide around the bar, then converging downstream.
function channelGap(z) {
if (z <= -8) return 0;
if (z < -4) return 3.25 * smoothstep(-8, -4, z);
if (z <= 6) return 3.25;
if (z < 12) return 3.25 * (1 - smoothstep(6, 12, z));
return 0;
}
function channelHalfWidth(z) {
let width = 1.03 + 0.08 * Math.sin(z * 0.31 + 0.7);
if (z < -9) width += 0.20;
if (z > 12) width += 0.16;
return width;
}
function riverState(x, z) {
const center = riverCenter(z);
const gap = channelGap(z);
const halfWidth = channelHalfWidth(z);
const leftDistance = Math.abs(x - (center - gap));
const rightDistance = Math.abs(x - (center + gap));
const distance = Math.min(leftDistance, rightDistance);
return {
center,
gap,
halfWidth,
distance,
water: distance <= halfWidth
};
}
function cellIndex(x, z) {
const ix = clamp(Math.floor((x + WIDTH * 0.5) / CELL), 0, COLS - 1);
const iz = clamp(Math.floor((z + LENGTH * 0.5) / CELL), 0, ROWS - 1);
return { ix, iz, index: iz * COLS + ix };
}
// -------------------------------------------------------------------------
// Tile base
// -------------------------------------------------------------------------
const baseMaterial = new THREE.MeshStandardMaterial({
color: 0x59645f,
roughness: 0.92,
metalness: 0.02
});
const base = new THREE.Mesh(
new THREE.BoxGeometry(WIDTH + 0.65, 1, LENGTH + 0.65),
baseMaterial
);
base.position.y = -1.65;
base.castShadow = true;
base.receiveShadow = true;
scene.add(base);
const plinth = new THREE.Mesh(
new THREE.BoxGeometry(WIDTH + 1.05, 0.25, LENGTH + 1.05),
new THREE.MeshStandardMaterial({
color: 0x424d49,
roughness: 0.95
})
);
plinth.position.y = -2.275;
plinth.castShadow = true;
plinth.receiveShadow = true;
scene.add(plinth);
const baseEdges = new THREE.LineSegments(
new THREE.EdgesGeometry(base.geometry),
new THREE.LineBasicMaterial({
color: 0x2d3c38,
transparent: true,
opacity: 0.42
})
);
baseEdges.position.copy(base.position);
scene.add(baseEdges);
const ground = new THREE.Mesh(
new THREE.PlaneGeometry(130, 130),
new THREE.MeshStandardMaterial({
color: 0xd4e0dc,
roughness: 1
})
);
ground.rotation.x = -Math.PI * 0.5;
ground.position.y = -2.42;
ground.receiveShadow = true;
scene.add(ground);
// -------------------------------------------------------------------------
// Voxel terrain columns
// -------------------------------------------------------------------------
const terrainData = new Array(COLS * ROWS);
const terrainColors = [];
const gravelPalette = [
new THREE.Color(0xa79e8e),
new THREE.Color(0xb7aa91),
new THREE.Color(0x918f86),
new THREE.Color(0xc1b49b),
new THREE.Color(0x99988e)
];
const wetPalette = [
new THREE.Color(0x777b72),
new THREE.Color(0x6c746e),
new THREE.Color(0x85877d)
];
const alpinePalette = [
new THREE.Color(0x68745d),
new THREE.Color(0x778167),
new THREE.Color(0x596653),
new THREE.Color(0x838b70)
];
const rockPalette = [
new THREE.Color(0x68716c),
new THREE.Color(0x7d857e),
new THREE.Color(0x59635f),
new THREE.Color(0x8b9188)
];
function paletteColor(palette, n) {
return palette[Math.min(palette.length - 1, Math.floor(n * palette.length))];
}
for (let iz = 0; iz < ROWS; iz++) {
for (let ix = 0; ix < COLS; ix++) {
const x = -WIDTH * 0.5 + (ix + 0.5) * CELL;
const z = -LENGTH * 0.5 + (iz + 0.5) * CELL;
const index = iz * COLS + ix;
const state = riverState(x, z);
const n = hash2(ix, iz);
const detail = hash2(ix + 51, iz - 27);
let top;
if (state.water) {
top = -0.30 + (n - 0.5) * 0.11;
} else {
const barMask =
smoothstep(-7.5, -4.1, z) *
(1 - smoothstep(6.2, 9.3, z));
top = 0.27 + (n - 0.5) * 0.18;
if (barMask > 0.12 && Math.abs(x - state.center) < 2.55) {
top += barMask * (0.16 + detail * 0.11);
}
// Wet gravel along the shallow channel margins.
if (state.distance < state.halfWidth + 0.34) {
top = Math.min(top, 0.17 + n * 0.08);
}
const distanceFromRiver = Math.abs(x - state.center);
const bank = smoothstep(5.8, 10.7, distanceFromRiver);
const upstream = 1 - smoothstep(-14, -7, z);
const downstream = smoothstep(6, 15, z);
const bankHeight = 2.25 + 1.75 * upstream - 0.55 * downstream;
top += bank * bankHeight;
top += bank * (detail - 0.5) * 1.35;
top += bank * Math.sin(z * 0.67 + x * 0.83) * 0.28;
}
top = Math.round(top / 0.16) * 0.16;
let color;
if (state.water) {
color = paletteColor(wetPalette, n).clone();
color.offsetHSL(0, -0.04, (detail - 0.5) * 0.06);
} else if (state.distance < state.halfWidth + 0.34) {
color = paletteColor(wetPalette, detail).clone();
color.lerp(gravelPalette[1], 0.28);
} else if (top < 0.78) {
color = paletteColor(gravelPalette, n).clone();
color.offsetHSL(0, 0, (detail - 0.5) * 0.08);
} else if (top < 1.85) {
color = paletteColor(alpinePalette, n).clone();
color.lerp(gravelPalette[2], 0.18);
} else {
color = paletteColor(rockPalette, detail).clone();
color.offsetHSL(0, -0.03, (n - 0.5) * 0.08);
}
terrainData[index] = {
ix, iz, x, z, top, n, detail,
water: state.water,
state
};
terrainColors.push(color);
}
}
const terrainMaterial = new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 0.94,
metalness: 0.01
});
const terrain = new THREE.InstancedMesh(
new THREE.BoxGeometry(1, 1, 1),
terrainMaterial,
terrainData.length
);
terrain.castShadow = true;
terrain.receiveShadow = true;
const dummy = new THREE.Object3D();
for (let i = 0; i < terrainData.length; i++) {
const d = terrainData[i];
dummy.position.set(d.x, (BOTTOM + d.top) * 0.5, d.z);
dummy.scale.set(CELL * 0.986, d.top - BOTTOM, CELL * 0.986);
dummy.rotation.set(0, 0, 0);
dummy.updateMatrix();
terrain.setMatrixAt(i, dummy.matrix);
terrain.setColorAt(i, terrainColors[i]);
}
terrain.instanceMatrix.needsUpdate = true;
terrain.instanceColor.needsUpdate = true;
scene.add(terrain);
// -------------------------------------------------------------------------
// Thin snow caps on the upper schier slopes
// -------------------------------------------------------------------------
const snowCaps = terrainData.filter(d =>
d.top > 3.52 && hash2(d.ix * 3 + 11, d.iz * 7 - 9) > 0.31
);
const snowMaterial = new THREE.MeshStandardMaterial({
color: 0xe8eee8,
roughness: 0.78,
metalness: 0
});
const snowMesh = new THREE.InstancedMesh(
new THREE.BoxGeometry(1, 1, 1),
snowMaterial,
snowCaps.length
);
snowMesh.castShadow = true;
snowMesh.receiveShadow = true;
snowCaps.forEach((d, i) => {
const thickness = 0.09 + d.detail * 0.08;
dummy.position.set(d.x, d.top + thickness * 0.5, d.z);
dummy.scale.set(CELL * 0.975, thickness, CELL * 0.975);
dummy.rotation.set(0, 0, 0);
dummy.updateMatrix();
snowMesh.setMatrixAt(i, dummy.matrix);
});
snowMesh.instanceMatrix.needsUpdate = true;
scene.add(snowMesh);
// -------------------------------------------------------------------------
// Shallow water: a tiled, semi-transparent voxel surface
// -------------------------------------------------------------------------
const waterCells = terrainData.filter(d => d.water);
const waterGroup = new THREE.Group();
scene.add(waterGroup);
const waterMaterial = new THREE.MeshStandardMaterial({
color: 0xffffff,
transparent: true,
opacity: 0.76,
roughness: 0.20,
metalness: 0.08,
depthWrite: false
});
const waterMesh = new THREE.InstancedMesh(
new THREE.BoxGeometry(1, 1, 1),
waterMaterial,
waterCells.length
);
waterMesh.renderOrder = 2;
const coolWater = new THREE.Color(0x58aeb9);
const brightWater = new THREE.Color(0x78c4c8);
waterCells.forEach((d, i) => {
dummy.position.set(d.x, WATER_LEVEL, d.z);
dummy.scale.set(CELL * 0.965, 0.065, CELL * 0.965);
dummy.rotation.set(0, 0, 0);
dummy.updateMatrix();
waterMesh.setMatrixAt(i, dummy.matrix);
const color = coolWater.clone().lerp(brightWater, d.detail * 0.78);
waterMesh.setColorAt(i, color);
});
waterMesh.instanceMatrix.needsUpdate = true;
waterMesh.instanceColor.needsUpdate = true;
waterGroup.add(waterMesh);
// -------------------------------------------------------------------------
// Loose gravel and cobbles on the exposed bar
// -------------------------------------------------------------------------
const pebblePalette = [
new THREE.Color(0xb3a68f),
new THREE.Color(0x8b8b82),
new THREE.Color(0xc5b89e),
new THREE.Color(0x767c78),
new THREE.Color(0x9b927f)
];
const pebbles = [];
let pebbleAttempts = 0;
while (pebbles.length < 270 && pebbleAttempts < 12000) {
pebbleAttempts++;
const z = -6.5 + random() * 15.2;
const center = riverCenter(z);
const gap = channelGap(z);
const halfWidth = channelHalfWidth(z);
const halfBar = Math.min(2.35, Math.max(0, gap - halfWidth - 0.08));
if (halfBar < 0.25) continue;
const x = center + (random() * 2 - 1) * halfBar;
const state = riverState(x, z);
if (state.water) continue;
const cell = terrainData[cellIndex(x, z).index];
if (!cell || cell.water) continue;
const large = random() > 0.91;
const sx = large ? 0.28 + random() * 0.30 : 0.08 + random() * 0.19;
const sz = large ? 0.24 + random() * 0.32 : 0.08 + random() * 0.19;
const sy = large ? 0.18 + random() * 0.20 : 0.055 + random() * 0.12;
pebbles.push({
x, z,
y: cell.top + sy * 0.5,
sx, sy, sz,
color: paletteColor(pebblePalette, random())
});
}
const pebbleMaterial = new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 0.98
});
const pebbleMesh = new THREE.InstancedMesh(
new THREE.BoxGeometry(1, 1, 1),
pebbleMaterial,
pebbles.length
);
pebbleMesh.castShadow = true;
pebbleMesh.receiveShadow = true;
pebbles.forEach((p, i) => {
dummy.position.set(p.x, p.y, p.z);
dummy.scale.set(p.sx, p.sy, p.sz);
dummy.rotation.set(0, (i % 4) * Math.PI * 0.5, 0);
dummy.updateMatrix();
pebbleMesh.setMatrixAt(i, dummy.matrix);
pebbleMesh.setColorAt(i, p.color);
});
pebbleMesh.instanceMatrix.needsUpdate = true;
pebbleMesh.instanceColor.needsUpdate = true;
scene.add(pebbleMesh);
// -------------------------------------------------------------------------
// Sparse alpine tussock on the banks
// -------------------------------------------------------------------------
const tussocks = [];
let tussockAttempts = 0;
while (tussocks.length < 155 && tussockAttempts < 7000) {
tussockAttempts++;
const x = -10.6 + random() * 21.2;
const z = -15.6 + random() * 31.2;
const state = riverState(x, z);
if (state.water) continue;
const cell = terrainData[cellIndex(x, z).index];
if (!cell || cell.top < 0.72 || cell.top > 2.75) continue;
const height = 0.10 + random() * 0.23;
tussocks.push({
x,
z,
y: cell.top + height * 0.5,
sx: 0.035 + random() * 0.055,
sy: height,
sz: 0.035 + random() * 0.055,
color: new THREE.Color().setHSL(
0.23 + random() * 0.035,
0.18 + random() * 0.13,
0.31 + random() * 0.12
)
});
}
const tussockMesh = new THREE.InstancedMesh(
new THREE.BoxGeometry(1, 1, 1),
new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 1
}),
tussocks.length
);
tussockMesh.castShadow = true;
tussocks.forEach((t, i) => {
dummy.position.set(t.x, t.y, t.z);
dummy.scale.set(t.sx, t.sy, t.sz);
dummy.rotation.set(0, random() * 0.35, 0);
dummy.updateMatrix();
tussockMesh.setMatrixAt(i, dummy.matrix);
tussockMesh.setColorAt(i, t.color);
});
tussockMesh.instanceMatrix.needsUpdate = true;
tussockMesh.instanceColor.needsUpdate = true;
scene.add(tussockMesh);
// -------------------------------------------------------------------------
// Animated current streaks
// -------------------------------------------------------------------------
const rippleCount = 120;
const rippleData = [];
for (let i = 0; i < rippleCount; i++) {
rippleData.push({
phase: random(),
lane: i % 2 === 0 ? -1 : 1,
jitter: (random() - 0.5) * 0.58,
length: 0.10 + random() * 0.31,
width: 0.025 + random() * 0.045
});
}
const rippleMaterial = new THREE.MeshBasicMaterial({
color: 0xe9ffff,
transparent: true,
opacity: 0.68,
depthWrite: false
});
const rippleMesh = new THREE.InstancedMesh(
new THREE.BoxGeometry(1, 1, 1),
rippleMaterial,
rippleCount
);
rippleMesh.instanceMatrix.setUsage(THREE.DynamicDrawUsage);
rippleMesh.renderOrder = 3;
rippleMesh.frustumCulled = false;
waterGroup.add(rippleMesh);
// -------------------------------------------------------------------------
// Direct camera controls: orbit, pan, wheel zoom and touch pinch
// -------------------------------------------------------------------------
function updateCamera() {
const aspect = window.innerWidth / window.innerHeight;
const halfHeight = Math.max(14.5, 19 / aspect);
camera.left = -halfHeight * aspect;
camera.right = halfHeight * aspect;
camera.top = halfHeight;
camera.bottom = -halfHeight;
camera.zoom = view.zoom;
camera.updateProjectionMatrix();
const radius = 42;
const horizontal = Math.cos(view.pitch);
camera.position.set(
view.target.x + Math.sin(view.yaw) * horizontal * radius,
view.target.y + Math.sin(view.pitch) * radius,
view.target.z + Math.cos(view.yaw) * horizontal * radius
);
camera.lookAt(view.target);
camera.updateMatrixWorld();
}
function resetView() {
view.yaw = 0.62;
view.pitch = 0.66;
view.zoom = 1;
view.target.set(0, 0.15, -0.8);
updateCamera();
}
const pointers = new Map();
let pinchDistance = 0;
renderer.domElement.addEventListener("pointerdown", event => {
renderer.domElement.setPointerCapture(event.pointerId);
pointers.set(event.pointerId, { x: event.clientX, y: event.clientY });
if (pointers.size === 2) {
const points = [...pointers.values()];
pinchDistance = Math.hypot(
points[0].x - points[1].x,
points[0].y - points[1].y
);
}
});
renderer.domElement.addEventListener("pointermove", event => {
const previous = pointers.get(event.pointerId);
if (!previous) return;
const dx = event.clientX - previous.x;
const dy = event.clientY - previous.y;
previous.x = event.clientX;
previous.y = event.clientY;
if (pointers.size === 1) {
if (event.shiftKey) {
const panScale = 0.024 / view.zoom;
const right = new THREE.Vector3(Math.cos(view.yaw), 0, -Math.sin(view.yaw));
const forward = new THREE.Vector3(-Math.sin(view.yaw), 0, -Math.cos(view.yaw));
view.target.addScaledVector(right, -dx * panScale);
view.target.addScaledVector(forward, dy * panScale);
view.target.x = clamp(view.target.x, -7, 7);
view.target.z = clamp(view.target.z, -8, 8);
} else {
view.yaw -= dx * 0.006;
view.pitch = clamp(view.pitch + dy * 0.005, 0.31, 1.25);
}
updateCamera();
} else if (pointers.size === 2) {
const points = [...pointers.values()];
const distance = Math.hypot(
points[0].x - points[1].x,
points[0].y - points[1].y
);
if (pinchDistance > 0 && distance > 0) {
view.zoom = clamp(view.zoom * distance / pinchDistance, 0.68, 2.25);
updateCamera();
}
pinchDistance = distance;
}
});
function releasePointer(event) {
pointers.delete(event.pointerId);
if (pointers.size < 2) pinchDistance = 0;
}
renderer.domElement.addEventListener("pointerup", releasePointer);
renderer.domElement.addEventListener("pointercancel", releasePointer);
renderer.domElement.addEventListener("contextmenu", event => event.preventDefault());
renderer.domElement.addEventListener("wheel", event => {
event.preventDefault();
view.zoom = clamp(view.zoom * Math.exp(event.deltaY * 0.001), 0.68, 2.25);
updateCamera();
}, { passive: false });
let flowPlaying = !reducedMotion;
function setFlowPlaying(playing) {
flowPlaying = playing;
flowButton.textContent = flowPlaying ? "Pause current" : "Resume current";
flowButton.setAttribute("aria-pressed", String(!flowPlaying));
}
flowButton.addEventListener("click", () => setFlowPlaying(!flowPlaying));
resetButton.addEventListener("click", resetView);
window.addEventListener("keydown", event => {
if (event.key.toLowerCase() === "r") resetView();
if (event.code === "Space") {
event.preventDefault();
setFlowPlaying(!flowPlaying);
}
});
window.addEventListener("resize", () => {
renderer.setPixelRatio(Math.min(window.devicePixelRatio || 1, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
updateCamera();
});
// -------------------------------------------------------------------------
// Animation and screen-space annotation
// -------------------------------------------------------------------------
const clock = new THREE.Clock();
let elapsed = 0;
let firstFrame = true;
const markerPoint = new THREE.Vector3();
const projectedPoint = new THREE.Vector3();
function updateBarLabel() {
markerPoint.set(riverCenter(1), 0.78, 1).project(camera);
const visible = markerPoint.z > -1 && markerPoint.z < 1;
barLabel.style.opacity = visible ? "1" : "0";
barLabel.style.left = `${(markerPoint.x * 0.5 + 0.5) * window.innerWidth}px`;
barLabel.style.top = `${(-markerPoint.y * 0.5 + 0.5) * window.innerHeight}px`;
}
renderer.setAnimationLoop(() => {
const delta = Math.min(clock.getDelta(), 0.05);
if (flowPlaying) elapsed += delta;
waterGroup.position.y = reducedMotion ? 0 : Math.sin(elapsed * 0.78) * 0.012;
rippleData.forEach((r, i) => {
const travel = (r.phase + elapsed * 0.065) % 1;
const z = -15.25 + travel * 30.5;
const center = riverCenter(z);
const gap = channelGap(z);
const x = center + r.lane * gap + r.jitter;
const y = WATER_LEVEL + 0.055 + Math.sin(elapsed * 2.4 + i) * 0.003;
dummy.position.set(x, y, z);
dummy.scale.set(r.width, 0.018, r.length);
dummy.rotation.set(0, 0, 0);
dummy.updateMatrix();
rippleMesh.setMatrixAt(i, dummy.matrix);
});
rippleMesh.instanceMatrix.needsUpdate = true;
renderer.render(scene, camera);
updateBarLabel();
if (firstFrame) {
firstFrame = false;
loading.classList.add("hidden");
}
});
setFlowPlaying(flowPlaying);
resetView();
</script>
</body>
</html>
```