Prompt specification
Build or customize the Three.js 3D scene "Estuary channels at two tide stages" 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: Estuary channels at two tide stages
Category: Space & Earth · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/estuary-channels-at-two-tide-stages-x1287
Scene Description Prompt:
One HTML file using Three.js from cdnjs: a low-poly estuary comparing exposed mudflats and deep channels at two tides over identical terrain.
Working Three.js Starter (estuary-channels-at-two-tide-stages-x1287):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Estuary — Two Tides, One Terrain</title>
<style>
:root {
--ink: #24342f;
--muted: #66736d;
--paper: rgba(248, 247, 237, 0.88);
--line: rgba(36, 52, 47, 0.17);
--accent: #d36f45;
--water: #347f88;
--shadow: 0 16px 48px rgba(36, 52, 47, 0.13);
}
* {
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;
}
body {
user-select: none;
}
#scene {
position: fixed;
inset: 0;
}
#scene canvas {
display: block;
width: 100%;
height: 100%;
cursor: grab;
touch-action: none;
}
#scene canvas:active {
cursor: grabbing;
}
.vignette {
position: fixed;
inset: 0;
pointer-events: none;
background:
radial-gradient(circle at 50% 42%, transparent 42%, rgba(42, 61, 55, 0.08) 100%),
linear-gradient(180deg, rgba(245, 246, 235, 0.1), transparent 22%);
}
.topbar {
position: fixed;
z-index: 10;
top: 24px;
left: 28px;
right: 28px;
display: flex;
align-items: flex-start;
justify-content: space-between;
gap: 24px;
pointer-events: none;
}
.brand {
max-width: 430px;
}
.eyebrow {
margin-bottom: 7px;
color: var(--accent);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.2em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-family: Georgia, "Times New Roman", serif;
font-size: clamp(29px, 3.2vw, 48px);
font-weight: 500;
letter-spacing: -0.045em;
line-height: 0.98;
}
.subtitle {
margin: 9px 0 0;
color: var(--muted);
font-size: 12px;
letter-spacing: 0.02em;
}
.controls {
display: flex;
align-items: center;
gap: 6px;
padding: 6px;
border: 1px solid rgba(255, 255, 255, 0.58);
border-radius: 12px;
background: rgba(244, 246, 238, 0.72);
box-shadow: var(--shadow);
backdrop-filter: blur(14px);
pointer-events: auto;
}
.controls button {
min-height: 34px;
padding: 0 13px;
border: 0;
border-radius: 8px;
background: transparent;
color: var(--muted);
cursor: pointer;
font: inherit;
font-size: 11px;
font-weight: 750;
letter-spacing: 0.04em;
transition: background 160ms ease, color 160ms ease, transform 160ms ease;
}
.controls button:hover {
color: var(--ink);
background: rgba(255, 255, 255, 0.58);
}
.controls button.active {
color: #f9f8ee;
background: var(--ink);
}
.controls button:active {
transform: scale(0.97);
}
.controls .reset {
display: grid;
width: 34px;
padding: 0;
place-items: center;
border-left: 1px solid var(--line);
border-radius: 0 8px 8px 0;
font-size: 17px;
}
#divider {
position: fixed;
z-index: 4;
top: 0;
bottom: 0;
left: 50%;
width: 1px;
pointer-events: none;
background: rgba(255, 255, 255, 0.74);
box-shadow: 0 0 0 1px rgba(36, 52, 47, 0.08);
}
#divider::after {
position: absolute;
top: 50%;
left: 50%;
width: 28px;
height: 28px;
border: 1px solid rgba(36, 52, 47, 0.17);
border-radius: 50%;
background: rgba(247, 248, 239, 0.76);
content: "";
transform: translate(-50%, -50%);
box-shadow: 0 5px 18px rgba(36, 52, 47, 0.1);
}
.view-label {
position: fixed;
z-index: 6;
top: 126px;
width: min(295px, calc(50% - 58px));
pointer-events: none;
transition: opacity 180ms ease, transform 180ms ease;
}
#lowLabel {
left: 28px;
}
#highLabel {
right: 28px;
}
.mode-low #highLabel,
.mode-high #lowLabel {
opacity: 0;
transform: translateY(-8px);
}
.mode-low #lowLabel,
.mode-high #highLabel {
right: auto;
left: 28px;
width: min(330px, calc(100% - 56px));
}
.view-kicker {
display: flex;
align-items: center;
gap: 9px;
margin-bottom: 7px;
color: var(--muted);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.16em;
text-transform: uppercase;
}
.view-kicker::before {
width: 24px;
height: 2px;
background: var(--accent);
content: "";
}
.reading {
display: flex;
align-items: flex-end;
gap: 8px;
margin-bottom: 8px;
}
.reading strong {
font-family: Georgia, "Times New Roman", serif;
font-size: clamp(40px, 4vw, 64px);
font-weight: 500;
letter-spacing: -0.06em;
line-height: 0.88;
}
.reading span {
padding-bottom: 4px;
color: var(--muted);
font-size: 12px;
font-weight: 700;
}
.view-label p {
max-width: 280px;
margin: 0;
color: #485951;
font-size: 12px;
line-height: 1.55;
}
.statline {
display: flex;
flex-wrap: wrap;
gap: 6px 14px;
margin-top: 12px;
padding-top: 10px;
border-top: 1px solid var(--line);
color: var(--muted);
font-size: 10px;
font-weight: 700;
letter-spacing: 0.055em;
text-transform: uppercase;
}
.legend {
position: fixed;
z-index: 8;
bottom: 24px;
left: 28px;
display: flex;
align-items: center;
gap: 16px;
padding: 10px 13px;
border: 1px solid rgba(255, 255, 255, 0.55);
border-radius: 10px;
background: rgba(246, 247, 239, 0.7);
box-shadow: var(--shadow);
backdrop-filter: blur(12px);
pointer-events: none;
}
.legend-title {
color: var(--muted);
font-size: 9px;
font-weight: 800;
letter-spacing: 0.14em;
text-transform: uppercase;
}
.legend-item {
display: flex;
align-items: center;
gap: 6px;
color: #536159;
font-size: 10px;
font-weight: 650;
}
.swatch {
width: 10px;
height: 10px;
border: 1px solid rgba(36, 52, 47, 0.14);
border-radius: 2px;
}
.hint {
position: fixed;
z-index: 8;
right: 28px;
bottom: 27px;
color: rgba(36, 52, 47, 0.67);
font-size: 10px;
font-weight: 650;
letter-spacing: 0.035em;
pointer-events: none;
}
.hint kbd {
padding: 2px 5px;
border: 1px solid rgba(36, 52, 47, 0.18);
border-bottom-width: 2px;
border-radius: 4px;
background: rgba(255, 255, 255, 0.35);
font: inherit;
font-size: 9px;
}
#loading {
position: fixed;
z-index: 100;
inset: 0;
display: grid;
place-items: center;
background: #d4e0dc;
color: var(--ink);
font-size: 11px;
font-weight: 800;
letter-spacing: 0.16em;
text-transform: uppercase;
transition: opacity 350ms ease;
}
#loading.hidden {
opacity: 0;
pointer-events: none;
}
body.stacked #divider {
top: 50%;
right: 0;
bottom: auto;
left: 0;
width: auto;
height: 1px;
}
body.stacked #lowLabel {
top: 108px;
left: 20px;
width: min(320px, calc(100% - 40px));
}
body.stacked #highLabel {
top: calc(50% + 18px);
right: auto;
left: 20px;
width: min(320px, calc(100% - 40px));
}
body.stacked .legend {
display: none;
}
@media (max-width: 760px) {
.topbar {
top: 18px;
right: 18px;
left: 18px;
align-items: flex-start;
}
.brand {
max-width: 210px;
}
.subtitle {
max-width: 190px;
font-size: 10px;
}
.controls {
gap: 2px;
padding: 4px;
}
.controls button {
min-height: 30px;
padding: 0 8px;
font-size: 9px;
}
.controls .reset {
width: 29px;
}
.view-label {
top: 104px;
}
#lowLabel {
left: 18px;
}
#highLabel {
right: 18px;
}
.mode-low #lowLabel,
.mode-high #highLabel {
left: 18px;
}
.reading strong {
font-size: 38px;
}
.view-label p {
font-size: 10px;
}
.statline {
display: none;
}
.legend {
right: 18px;
bottom: 18px;
left: 18px;
justify-content: center;
gap: 12px;
}
.legend-title {
display: none;
}
.hint {
display: none;
}
}
</style>
</head>
<body class="mode-compare">
<div id="scene"></div>
<div class="vignette"></div>
<header class="topbar">
<div class="brand">
<div class="eyebrow">Field Study 07 · Estuarine Morphology</div>
<h1>Two tides,<br />one terrain.</h1>
<p class="subtitle">A synchronized low-poly study of an intertidal estuary.</p>
</div>
<nav class="controls" aria-label="Tide view controls">
<button type="button" data-mode="low">Low</button>
<button type="button" data-mode="compare" class="active">Compare</button>
<button type="button" data-mode="high">High</button>
<button type="button" class="reset" id="resetCamera" title="Reset camera" aria-label="Reset camera">↺</button>
</nav>
</header>
<div id="divider"></div>
<section class="view-label" id="lowLabel" aria-label="Low tide information">
<div class="view-kicker">01 · Low water · 06:10</div>
<div class="reading">
<strong>+0.3</strong>
<span>metres above datum</span>
</div>
<p>
The channel holds a narrow ribbon of water while broad mudflats emerge
between the salt-marsh banks.
</p>
<div class="statline">
<span id="lowExposed">— exposed</span>
<span id="lowDepth">— m channel</span>
</div>
</section>
<section class="view-label" id="highLabel" aria-label="High tide information">
<div class="view-kicker">02 · High water · 18:40</div>
<div class="reading">
<strong>+2.4</strong>
<span>metres above datum</span>
</div>
<p>
Floodwater spreads across the flats; only the higher marsh edge remains
above the surface.
</p>
<div class="statline">
<span id="highCovered">— inundated</span>
<span id="highRange">2.1 m tidal range</span>
</div>
</section>
<aside class="legend" aria-label="Terrain color legend">
<div class="legend-title">Substrate</div>
<div class="legend-item"><i class="swatch" style="background:#444b3d"></i>Deep channel</div>
<div class="legend-item"><i class="swatch" style="background:#a48a64"></i>Mudflat</div>
<div class="legend-item"><i class="swatch" style="background:#64764f"></i>Marsh edge</div>
</aside>
<div class="hint">
Drag to orbit · Scroll to zoom · <kbd>Shift</kbd> + drag to pan
</div>
<div id="loading">Loading estuary model…</div>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const sceneHost = document.getElementById("scene");
const loading = document.getElementById("loading");
const TERRAIN_WIDTH = 44;
const TERRAIN_DEPTH = 64;
const X_SEGMENTS = 44;
const Z_SEGMENTS = 64;
const TERRAIN_BOTTOM = -4.5;
const LOW_TIDE = 0.3;
const HIGH_TIDE = 2.4;
let viewMode = "compare";
let stacked = false;
let viewportWidth = window.innerWidth;
let viewportHeight = window.innerHeight;
const renderer = new THREE.WebGLRenderer({
antialias: true,
alpha: false,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio || 1, 1.8));
renderer.setSize(viewportWidth, viewportHeight);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.12;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.autoClear = true;
sceneHost.appendChild(renderer.domElement);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xd4e0dc);
scene.fog = new THREE.Fog(0xd4e0dc, 73, 116);
const camera = new THREE.PerspectiveCamera(
44,
viewportWidth / viewportHeight,
0.1,
240
);
const hemisphere = new THREE.HemisphereLight(0xf5fbff, 0x75664d, 1.65);
scene.add(hemisphere);
const sun = new THREE.DirectionalLight(0xffedcc, 2.45);
sun.position.set(-24, 38, 20);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -34;
sun.shadow.camera.right = 34;
sun.shadow.camera.top = 43;
sun.shadow.camera.bottom = -43;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 100;
sun.shadow.normalBias = 0.035;
sun.shadow.bias = -0.00015;
scene.add(sun);
const fill = new THREE.DirectionalLight(0xb9dce0, 0.55);
fill.position.set(26, 16, -25);
scene.add(fill);
const floor = new THREE.Mesh(
new THREE.PlaneGeometry(220, 220),
new THREE.MeshStandardMaterial({
color: 0xcbd8d3,
roughness: 1,
metalness: 0
})
);
floor.rotation.x = -Math.PI / 2;
floor.position.y = -5.18;
floor.receiveShadow = true;
scene.add(floor);
function clamp(value, min, max) {
return Math.max(min, Math.min(max, value));
}
function lerp(a, b, t) {
return a + (b - a) * t;
}
function smoothstep(edge0, edge1, value) {
const t = clamp((value - edge0) / (edge1 - edge0), 0, 1);
return t * t * (3 - 2 * t);
}
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(74193);
function channelCenter(z) {
return (
2.45 * Math.sin(z * 0.086) +
0.88 * Math.sin(z * 0.218 + 0.45) +
0.35 * Math.sin(z * 0.51)
);
}
function terrainHeight(x, z) {
const center = channelCenter(z);
const distance = Math.abs(x - center);
const noise =
Math.sin(x * 1.31 + z * 1.83) * 0.11 +
Math.sin(x * 0.61 - z * 1.17 + 0.7) * 0.09 +
Math.sin(x * 2.17 + z * 0.37) * 0.035;
const bank = smoothstep(7.5, 18.5, distance);
const halfWidth = 2.15 + 0.42 * Math.sin(z * 0.14 + 0.8);
const channelMix = 1 - smoothstep(
halfWidth * 0.52,
halfWidth + 2.45,
distance
);
let flat = 0.66 + noise;
flat += bank * 2.55;
flat += 0.16 * Math.sin(z * 0.19 + x * 0.08);
const bed = -2.72 + 0.24 * Math.sin(z * 0.28) +
0.13 * Math.sin(x * 1.5 + z * 0.12);
let height = lerp(flat, bed, channelMix);
// A narrow branching creek on the left bank.
const branch1Z = clamp((z + 24) / 21, 0, 1);
const branch1X = channelCenter(-13) - 8.8 + branch1Z * 8.6;
const branch1Fade =
smoothstep(-25, -22, z) * (1 - smoothstep(-5, -2, z));
const branch1Mix =
(1 - smoothstep(0.45, 2.25, Math.abs(x - branch1X))) *
branch1Fade;
height = lerp(
height,
-1.18 + 0.14 * Math.sin(z * 0.7 + x),
branch1Mix * 0.88
);
// A second branching creek on the right bank.
const branch2Z = clamp((z - 4) / 19, 0, 1);
const branch2X = channelCenter(14) + 8.5 - branch2Z * 8.4;
const branch2Fade =
smoothstep(3, 6, z) * (1 - smoothstep(21, 24, z));
const branch2Mix =
(1 - smoothstep(0.5, 2.4, Math.abs(x - branch2X))) *
branch2Fade;
height = lerp(
height,
-1.03 + 0.12 * Math.sin(z * 0.62 - x),
branch2Mix * 0.9
);
// The reach opens into deeper water at the seaward end.
const seaMix = smoothstep(24, 32, z);
height = lerp(height, -3.65, seaMix);
return height;
}
// Build the shared, faceted terrain.
const terrainGeometry = new THREE.PlaneGeometry(
TERRAIN_WIDTH,
TERRAIN_DEPTH,
X_SEGMENTS,
Z_SEGMENTS
);
terrainGeometry.rotateX(-Math.PI / 2);
const terrainPositions = terrainGeometry.attributes.position;
for (let i = 0; i < terrainPositions.count; i++) {
const x = terrainPositions.getX(i);
const z = terrainPositions.getZ(i);
terrainPositions.setY(i, terrainHeight(x, z));
}
terrainPositions.needsUpdate = true;
const facetedTerrain = terrainGeometry.toNonIndexed();
const terrainColors = [];
const terrainPositionArray = facetedTerrain.attributes.position.array;
const colorDeep = new THREE.Color(0x424a3e);
const colorSilt = new THREE.Color(0x69654b);
const colorMud = new THREE.Color(0xa28a66);
const colorMarsh = new THREE.Color(0x85815b);
const colorGreen = new THREE.Color(0x62754d);
function substrateColor(height, variation) {
let color;
if (height < -1.55) {
color = colorDeep.clone();
} else if (height < -0.15) {
color = colorSilt.clone().lerp(colorDeep, smoothstep(-0.15, -1.55, height) * 0.3);
} else if (height < 1.3) {
color = colorMud.clone().lerp(colorSilt, smoothstep(0.2, 1.3, height) * 0.34);
} else if (height < 2.25) {
color = colorMarsh.clone().lerp(colorMud, smoothstep(1.3, 2.25, height) * 0.42);
} else {
color = colorGreen.clone().lerp(colorMarsh, smoothstep(2.25, 3.4, height) * 0.42);
}
color.offsetHSL(
(random() - 0.5) * 0.012,
(random() - 0.5) * 0.035,
(random() - 0.5) * variation
);
return color;
}
for (let i = 0; i < facetedTerrain.attributes.position.count; i += 3) {
const h =
(terrainPositionArray[i * 3 + 1] +
terrainPositionArray[(i + 1) * 3 + 1] +
terrainPositionArray[(i + 2) * 3 + 1]) / 3;
const color = substrateColor(h, 0.095);
for (let j = 0; j < 3; j++) {
terrainColors.push(color.r, color.g, color.b);
}
}
facetedTerrain.setAttribute(
"color",
new THREE.Float32BufferAttribute(terrainColors, 3)
);
facetedTerrain.computeVertexNormals();
const terrain = new THREE.Mesh(
facetedTerrain,
new THREE.MeshStandardMaterial({
vertexColors: true,
roughness: 0.97,
metalness: 0,
flatShading: true
})
);
terrain.receiveShadow = true;
terrain.castShadow = true;
scene.add(terrain);
// A dark cutaway skirt gives the model a physical, survey-table quality.
const skirtPositions = [];
function addSkirtQuad(topA, topB) {
const bottomA = [topA[0], TERRAIN_BOTTOM, topA[2]];
const bottomB = [topB[0], TERRAIN_BOTTOM, topB[2]];
skirtPositions.push(
...topA, ...bottomA, ...topB,
...topB, ...bottomA, ...bottomB
);
}
for (let i = 0; i < X_SEGMENTS; i++) {
const x0 = -TERRAIN_WIDTH / 2 + (i / X_SEGMENTS) * TERRAIN_WIDTH;
const x1 = -TERRAIN_WIDTH / 2 + ((i + 1) / X_SEGMENTS) * TERRAIN_WIDTH;
addSkirtQuad(
[x0, terrainHeight(x0, TERRAIN_DEPTH / 2), TERRAIN_DEPTH / 2],
[x1, terrainHeight(x1, TERRAIN_DEPTH / 2), TERRAIN_DEPTH / 2]
);
addSkirtQuad(
[x0, terrainHeight(x0, -TERRAIN_DEPTH / 2), -TERRAIN_DEPTH / 2],
[x1, terrainHeight(x1, -TERRAIN_DEPTH / 2), -TERRAIN_DEPTH / 2]
);
}
for (let i = 0; i < Z_SEGMENTS; i++) {
const z0 = -TERRAIN_DEPTH / 2 + (i / Z_SEGMENTS) * TERRAIN_DEPTH;
const z1 = -TERRAIN_DEPTH / 2 + ((i + 1) / Z_SEGMENTS) * TERRAIN_DEPTH;
addSkirtQuad(
[-TERRAIN_WIDTH / 2, terrainHeight(-TERRAIN_WIDTH / 2, z0), z0],
[-TERRAIN_WIDTH / 2, terrainHeight(-TERRAIN_WIDTH / 2, z1), z1]
);
addSkirtQuad(
[TERRAIN_WIDTH / 2, terrainHeight(TERRAIN_WIDTH / 2, z0), z0],
[TERRAIN_WIDTH / 2, terrainHeight(TERRAIN_WIDTH / 2, z1), z1]
);
}
const skirtGeometry = new THREE.BufferGeometry();
skirtGeometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(skirtPositions, 3)
);
skirtGeometry.computeVertexNormals();
const skirt = new THREE.Mesh(
skirtGeometry,
new THREE.MeshStandardMaterial({
color: 0x504d3b,
roughness: 1,
flatShading: true,
side: THREE.DoubleSide
})
);
skirt.castShadow = true;
skirt.receiveShadow = true;
scene.add(skirt);
const base = new THREE.Mesh(
new THREE.BoxGeometry(TERRAIN_WIDTH + 0.4, 0.6, TERRAIN_DEPTH + 0.4),
new THREE.MeshStandardMaterial({
color: 0x3e4136,
roughness: 0.92
})
);
base.position.y = -4.8;
base.castShadow = true;
base.receiveShadow = true;
scene.add(base);
// Soil strata on the visible cut face.
const strataPoints = [];
[-0.8, -1.9, -3.1].forEach((y) => {
strataPoints.push(
-TERRAIN_WIDTH / 2, y, TERRAIN_DEPTH / 2 + 0.012,
TERRAIN_WIDTH / 2, y, TERRAIN_DEPTH / 2 + 0.012
);
});
const strata = new THREE.LineSegments(
new THREE.BufferGeometry().setAttribute(
"position",
new THREE.Float32BufferAttribute(strataPoints, 3)
),
new THREE.LineBasicMaterial({
color: 0x81765a,
transparent: true,
opacity: 0.42
})
);
scene.add(strata);
// Sparse marsh vegetation, concentrated on the higher banks.
const reedCount = 210;
const reedGeometry = new THREE.ConeGeometry(0.09, 0.78, 3, 1);
reedGeometry.translate(0, 0.39, 0);
const reeds = new THREE.InstancedMesh(
reedGeometry,
new THREE.MeshStandardMaterial({
color: 0x526344,
roughness: 1,
flatShading: true
}),
reedCount
);
const dummy = new THREE.Object3D();
let placedReeds = 0;
let attempts = 0;
while (placedReeds < reedCount && attempts < reedCount * 12) {
attempts++;
const x = (random() - 0.5) * (TERRAIN_WIDTH - 1.4);
const z = (random() - 0.5) * (TERRAIN_DEPTH - 1.4);
const h = terrainHeight(x, z);
if (h < 1.45 || h > 4.0) continue;
const scale = 0.65 + random() * 0.9;
dummy.position.set(x, h, z);
dummy.rotation.set(
(random() - 0.5) * 0.11,
random() * Math.PI * 2,
(random() - 0.5) * 0.11
);
dummy.scale.set(scale, scale, scale);
dummy.updateMatrix();
reeds.setMatrixAt(placedReeds, dummy.matrix);
reeds.setColorAt(
placedReeds,
new THREE.Color().setHSL(
0.22 + random() * 0.025,
0.18 + random() * 0.1,
0.25 + random() * 0.08
)
);
placedReeds++;
}
reeds.instanceMatrix.needsUpdate = true;
if (reeds.instanceColor) reeds.instanceColor.needsUpdate = true;
reeds.castShadow = true;
scene.add(reeds);
// Low-poly water surface.
const waterGeometry = new THREE.PlaneGeometry(
TERRAIN_WIDTH,
TERRAIN_DEPTH,
24,
34
);
waterGeometry.rotateX(-Math.PI / 2);
const waterPositions = waterGeometry.attributes.position;
for (let i = 0; i < waterPositions.count; i++) {
const x = waterPositions.getX(i);
const z = waterPositions.getZ(i);
const wave =
Math.sin(x * 0.52 + z * 0.17) * 0.035 +
Math.cos(z * 0.37 - x * 0.11) * 0.022;
waterPositions.setY(i, wave);
}
waterPositions.needsUpdate = true;
waterGeometry.computeVertexNormals();
const waterMaterial = new THREE.MeshPhongMaterial({
color: 0x347f88,
specular: 0xbfeef0,
shininess: 86,
transparent: true,
opacity: 0.61,
depthWrite: false,
side: THREE.DoubleSide,
flatShading: true
});
const water = new THREE.Mesh(waterGeometry, waterMaterial);
water.renderOrder = 2;
scene.add(water);
// Sparse surface ripples; terrain depth hides them over exposed ground.
const ripplePositions = [];
for (let i = 0; i < 95; i++) {
const x = (random() - 0.5) * (TERRAIN_WIDTH - 1);
const z = (random() - 0.5) * (TERRAIN_DEPTH - 1);
const length = 0.5 + random() * 2.0;
const angle = (random() - 0.5) * 0.45;
ripplePositions.push(
x, 0.07, z,
x + Math.cos(angle) * length, 0.07, z + Math.sin(angle) * length
);
}
const rippleMaterial = new THREE.LineBasicMaterial({
color: 0xd6f2eb,
transparent: true,
opacity: 0.18,
depthWrite: false
});
const ripples = new THREE.LineSegments(
new THREE.BufferGeometry().setAttribute(
"position",
new THREE.Float32BufferAttribute(ripplePositions, 3)
),
rippleMaterial
);
ripples.renderOrder = 3;
scene.add(ripples);
// Transparent perimeter faces suggest a water column in the cutaway model.
const waterSideMaterial = new THREE.MeshPhongMaterial({
color: 0x2e7882,
transparent: true,
opacity: 0.2,
depthWrite: false,
side: THREE.DoubleSide,
shininess: 40
});
const waterWalls = [];
function createWaterWall(width, x, z, rotationY) {
const wall = new THREE.Mesh(
new THREE.PlaneGeometry(width, 1),
waterSideMaterial
);
wall.position.set(x, 0, z);
wall.rotation.y = rotationY;
wall.renderOrder = 1;
scene.add(wall);
waterWalls.push(wall);
return wall;
}
createWaterWall(
TERRAIN_WIDTH,
0,
TERRAIN_DEPTH / 2 - 0.015,
0
);
createWaterWall(
TERRAIN_WIDTH,
0,
-TERRAIN_DEPTH / 2 + 0.015,
0
);
createWaterWall(
TERRAIN_DEPTH,
TERRAIN_WIDTH / 2 - 0.015,
0,
Math.PI / 2
);
createWaterWall(
TERRAIN_DEPTH,
-TERRAIN_WIDTH / 2 + 0.015,
0,
Math.PI / 2
);
function updateWaterWalls(level) {
const wallHeight = Math.max(0.001, level - TERRAIN_BOTTOM);
waterWalls.forEach((wall) => {
wall.scale.y = wallHeight;
wall.position.y = TERRAIN_BOTTOM + wallHeight / 2;
});
}
// Generate exact-ish waterline contours using marching squares.
function createContour(level) {
const points = [];
const dx = TERRAIN_WIDTH / X_SEGMENTS;
const dz = TERRAIN_DEPTH / Z_SEGMENTS;
function edgeIntersection(a, b) {
const denominator = b.h - a.h;
if (Math.abs(denominator) < 0.000001) return null;
const t = (level - a.h) / denominator;
if (t < 0 || t > 1) return null;
return {
x: lerp(a.x, b.x, t),
z: lerp(a.z, b.z, t)
};
}
for (let j = 0; j < Z_SEGMENTS; j++) {
const z0 = -TERRAIN_DEPTH / 2 + j * dz;
const z1 = z0 + dz;
for (let i = 0; i < X_SEGMENTS; i++) {
const x0 = -TERRAIN_WIDTH / 2 + i * dx;
const x1 = x0 + dx;
const a = { x: x0, z: z0, h: terrainHeight(x0, z0) };
const b = { x: x1, z: z0, h: terrainHeight(x1, z0) };
const c = { x: x1, z: z1, h: terrainHeight(x1, z1) };
const d = { x: x0, z: z1, h: terrainHeight(x0, z1) };
const crossings = [
edgeIntersection(a, b),
edgeIntersection(b, c),
edgeIntersection(c, d),
edgeIntersection(d, a)
].filter(Boolean);
if (crossings.length === 2 || crossings.length === 4) {
points.push(
crossings[0].x, level + 0.045, crossings[0].z,
crossings[1].x, level + 0.045, crossings[1].z
);
if (crossings.length === 4) {
points.push(
crossings[2].x, level + 0.045, crossings[2].z,
crossings[3].x, level + 0.045, crossings[3].z
);
}
}
}
}
const contour = new THREE.LineSegments(
new THREE.BufferGeometry().setAttribute(
"position",
new THREE.Float32BufferAttribute(points, 3)
),
new THREE.LineBasicMaterial({
color: 0xf0efd2,
transparent: true,
opacity: 0.48,
depthWrite: false
})
);
contour.renderOrder = 4;
scene.add(contour);
return contour;
}
const lowContour = createContour(LOW_TIDE);
const highContour = createContour(HIGH_TIDE);
// Compute summary statistics from the shared terrain.
let lowExposedCount = 0;
let highCoveredCount = 0;
let sampleCount = 0;
let minimumHeight = Infinity;
let maximumHeight = -Infinity;
const flatHeights = [];
for (let z = -TERRAIN_DEPTH / 2; z <= TERRAIN_DEPTH / 2; z += 0.5) {
for (let x = -TERRAIN_WIDTH / 2; x <= TERRAIN_WIDTH / 2; x += 0.5) {
const h = terrainHeight(x, z);
sampleCount++;
minimumHeight = Math.min(minimumHeight, h);
maximumHeight = Math.max(maximumHeight, h);
if (h > LOW_TIDE) lowExposedCount++;
if (h < HIGH_TIDE) highCoveredCount++;
if (h > 0 && h < 1.4) flatHeights.push(h);
}
}
flatHeights.sort((a, b) => a - b);
const flatMedian = flatHeights[Math.floor(flatHeights.length / 2)];
document.getElementById("lowExposed").textContent =
`${Math.round((lowExposedCount / sampleCount) * 100)}% exposed`;
document.getElementById("lowDepth").textContent =
`${Math.abs(minimumHeight).toFixed(1)} m channel`;
document.getElementById("highCovered").textContent =
`${Math.round((highCoveredCount / sampleCount) * 100)}% inundated`;
document.getElementById("highRange").textContent =
`${(HIGH_TIDE - LOW_TIDE).toFixed(1)} m tidal range`;
// Custom orbit, pan, zoom, and touch controls — no addons required.
const desired = {
radius: 74,
theta: 0.48,
phi: 0.79,
target: new THREE.Vector3(0, -0.1, -1.5)
};
const current = {
radius: desired.radius,
theta: desired.theta,
phi: desired.phi,
target: desired.target.clone()
};
function resetCamera() {
desired.radius = viewMode === "compare" ? 74 : 68;
desired.theta = 0.48;
desired.phi = 0.79;
desired.target.set(0, -0.1, -1.5);
}
function panCamera(dx, dy) {
const scale = desired.radius * 0.00135;
const right = new THREE.Vector3(
Math.cos(desired.theta),
0,
-Math.sin(desired.theta)
);
const forward = new THREE.Vector3(
-Math.sin(desired.theta),
0,
-Math.cos(desired.theta)
);
desired.target.addScaledVector(right, -dx * scale);
desired.target.addScaledVector(forward, dy * scale);
desired.target.x = clamp(desired.target.x, -18, 18);
desired.target.z = clamp(desired.target.z, -18, 18);
}
const pointers = new Map();
renderer.domElement.addEventListener("pointerdown", (event) => {
renderer.domElement.setPointerCapture(event.pointerId);
pointers.set(event.pointerId, {
x: event.clientX,
y: event.clientY,
pan: event.shiftKey || event.button === 2
});
});
renderer.domElement.addEventListener("pointermove", (event) => {
const pointer = pointers.get(event.pointerId);
if (!pointer) return;
const oldX = pointer.x;
const oldY = pointer.y;
if (pointers.size === 2) {
const entries = Array.from(pointers.entries());
const a = entries[0][1];
const b = entries[1][1];
const previousDistance = Math.hypot(a.x - b.x, a.y - b.y);
pointer.x = event.clientX;
pointer.y = event.clientY;
const updatedA = pointers.get(entries[0][0]);
const updatedB = pointers.get(entries[1][0]);
const nextDistance = Math.hypot(
updatedA.x - updatedB.x,
updatedA.y - updatedB.y
);
if (previousDistance > 0 && nextDistance > 0) {
desired.radius = clamp(
desired.radius * (previousDistance / nextDistance),
38,
108
);
}
return;
}
const dx = event.clientX - oldX;
const dy = event.clientY - oldY;
if (pointer.pan) {
panCamera(dx, dy);
} else {
desired.theta -= dx * 0.005;
desired.phi = clamp(desired.phi + dy * 0.004, 0.32, 1.34);
}
pointer.x = event.clientX;
pointer.y = event.clientY;
});
function removePointer(event) {
pointers.delete(event.pointerId);
}
renderer.domElement.addEventListener("pointerup", removePointer);
renderer.domElement.addEventListener("pointercancel", removePointer);
renderer.domElement.addEventListener("contextmenu", (event) => event.preventDefault());
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
desired.radius = clamp(
desired.radius * Math.exp(event.deltaY * 0.001),
38,
108
);
},
{ passive: false }
);
renderer.domElement.addEventListener("dblclick", resetCamera);
document.getElementById("resetCamera").addEventListener("click", resetCamera);
function updateLayout() {
viewportWidth = window.innerWidth;
viewportHeight = window.innerHeight;
stacked = viewMode === "compare" && viewportWidth < 760;
document.body.classList.toggle("stacked", stacked);
renderer.setSize(viewportWidth, viewportHeight);
renderer.setPixelRatio(Math.min(window.devicePixelRatio || 1, 1.8));
}
function setMode(mode) {
viewMode = mode;
document.body.classList.remove("mode-low", "mode-compare", "mode-high");
document.body.classList.add(`mode-${mode}`);
document.querySelectorAll("[data-mode]").forEach((button) => {
button.classList.toggle("active", button.dataset.mode === mode);
});
updateLayout();
if (mode !== "compare") {
desired.radius = 68;
} else {
desired.radius = 74;
}
}
document.querySelectorAll("[data-mode]").forEach((button) => {
button.addEventListener("click", () => setMode(button.dataset.mode));
});
window.addEventListener("resize", updateLayout);
updateLayout();
function updateCamera() {
current.radius = lerp(current.radius, desired.radius, 0.085);
current.theta = lerp(current.theta, desired.theta, 0.09);
current.phi = lerp(current.phi, desired.phi, 0.09);
current.target.lerp(desired.target, 0.09);
const sinPhiRadius = Math.sin(current.phi) * current.radius;
camera.position.set(
current.target.x + sinPhiRadius * Math.sin(current.theta),
current.target.y + Math.cos(current.phi) * current.radius,
current.target.z + sinPhiRadius * Math.cos(current.theta)
);
camera.lookAt(current.target);
}
function renderPanel(rect, tide, contour) {
camera.aspect = rect.width / rect.height;
camera.updateProjectionMatrix();
water.position.y = tide;
ripples.position.y = tide;
updateWaterWalls(tide);
lowContour.visible = contour === lowContour;
highContour.visible = contour === highContour;
renderer.setViewport(rect.x, rect.y, rect.width, rect.height);
renderer.setScissor(rect.x, rect.y, rect.width, rect.height);
renderer.clear(true, true, true);
renderer.render(scene, camera);
}
function renderScenes(time) {
renderer.setScissorTest(true);
if (viewMode === "compare") {
if (stacked) {
const half = Math.floor(viewportHeight / 2);
renderPanel(
{
x: 0,
y: half,
width: viewportWidth,
height: viewportHeight - half
},
LOW_TIDE,
lowContour
);
renderPanel(
{
x: 0,
y: 0,
width: viewportWidth,
height: half
},
HIGH_TIDE,
highContour
);
} else {
const half = Math.floor(viewportWidth / 2);
renderPanel(
{
x: 0,
y: 0,
width: half,
height: viewportHeight
},
LOW_TIDE,
lowContour
);
renderPanel(
{
x: half,
y: 0,
width: viewportWidth - half,
height: viewportHeight
},
HIGH_TIDE,
highContour
);
}
} else if (viewMode === "low") {
renderPanel(
{
x: 0,
y: 0,
width: viewportWidth,
height: viewportHeight
},
LOW_TIDE,
lowContour
);
} else {
renderPanel(
{
x: 0,
y: 0,
width: viewportWidth,
height: viewportHeight
},
HIGH_TIDE,
highContour
);
}
renderer.setScissorTest(false);
waterMaterial.opacity = 0.605 + Math.sin(time * 0.00065) * 0.012;
rippleMaterial.opacity = 0.17 + Math.sin(time * 0.00085) * 0.025;
}
function animate(time) {
requestAnimationFrame(animate);
updateCamera();
renderScenes(time);
}
requestAnimationFrame(() => {
loading.classList.add("hidden");
setTimeout(() => loading.remove(), 400);
});
animate(0);
</script>
</body>
</html>
```