Prompt specification
Build or customize the Three.js 3D scene "Water reservoir routing explorer" 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: Water reservoir routing explorer
Category: Space & Earth · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/water-reservoir-routing-explorer-x1268
Scene Description Prompt:
Make a standalone HTML scene with Three.js from cdnjs that depicts the water cycle. Let viewers choose surface or underground pathways in a bounded landscape; highlight transfers and storage changes qualitatively while keeping a visible inventory of the modeled reservoirs. Low-poly.
Working Three.js Starter (water-reservoir-routing-explorer-x1268):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="color-scheme" content="dark">
<title>Watershed — An Interactive Water Cycle</title>
<style>
:root {
--ink: #eef8fb;
--muted: #abc4cf;
--panel: rgba(7, 25, 36, 0.83);
--panel-edge: rgba(180, 226, 238, 0.18);
--surface: #52e0db;
--ground: #f3b85b;
--snow: #e9f7f7;
--water: #55bde7;
--soil: #b88454;
--aquifer: #5c9bc6;
--air: #9bc9f2;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #a8d9e6;
color: var(--ink);
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
button {
font: inherit;
}
#app,
#scene {
position: fixed;
inset: 0;
width: 100%;
height: 100%;
}
#scene {
display: block;
touch-action: none;
cursor: grab;
}
#scene:active {
cursor: grabbing;
}
.glass {
background:
linear-gradient(145deg, rgba(14, 43, 57, 0.9), rgba(5, 22, 32, 0.84));
border: 1px solid var(--panel-edge);
box-shadow:
0 18px 50px rgba(0, 20, 29, 0.22),
inset 0 1px 0 rgba(255, 255, 255, 0.05);
backdrop-filter: blur(15px) saturate(125%);
-webkit-backdrop-filter: blur(15px) saturate(125%);
}
header {
position: fixed;
top: 24px;
left: 24px;
z-index: 5;
width: min(420px, calc(100vw - 48px));
pointer-events: none;
text-shadow: 0 2px 18px rgba(9, 35, 45, 0.35);
}
.eyebrow {
display: flex;
align-items: center;
gap: 9px;
margin-bottom: 8px;
color: #d7f4f7;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.19em;
text-transform: uppercase;
}
.eyebrow::before {
width: 9px;
height: 9px;
border: 2px solid var(--surface);
border-radius: 50% 50% 50% 0;
transform: rotate(-45deg);
content: "";
box-shadow: 0 0 12px rgba(82, 224, 219, 0.65);
}
h1 {
margin: 0;
font-size: clamp(25px, 3.1vw, 43px);
font-weight: 720;
letter-spacing: -0.045em;
line-height: 1.02;
}
header p {
max-width: 370px;
margin: 9px 0 0;
color: rgba(245, 255, 255, 0.86);
font-size: 13px;
line-height: 1.45;
}
.panel {
position: fixed;
z-index: 10;
border-radius: 18px;
}
#route-panel {
top: 151px;
left: 24px;
width: 320px;
padding: 17px;
}
.panel-heading {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
margin-bottom: 13px;
}
.panel-heading h2 {
margin: 0;
font-size: 11px;
font-weight: 800;
letter-spacing: 0.16em;
text-transform: uppercase;
}
.panel-heading span {
color: var(--muted);
font-size: 10px;
}
.mode-switch {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 8px;
}
.mode-button {
position: relative;
min-height: 67px;
padding: 11px 10px 10px 35px;
border: 1px solid rgba(182, 221, 231, 0.16);
border-radius: 12px;
background: rgba(255, 255, 255, 0.035);
color: #cfe0e6;
text-align: left;
cursor: pointer;
transition:
background 160ms ease,
border-color 160ms ease,
transform 160ms ease,
color 160ms ease;
}
.mode-button:hover {
transform: translateY(-1px);
background: rgba(255, 255, 255, 0.075);
}
.mode-button.active {
color: white;
background: rgba(255, 255, 255, 0.11);
border-color: rgba(255, 255, 255, 0.32);
}
.mode-button[data-mode="surface"].active {
box-shadow: inset 0 0 0 1px rgba(82, 224, 219, 0.32);
}
.mode-button[data-mode="underground"].active {
box-shadow: inset 0 0 0 1px rgba(243, 184, 91, 0.32);
}
.mode-button .dot {
position: absolute;
top: 14px;
left: 13px;
width: 10px;
height: 10px;
border-radius: 50%;
}
[data-mode="surface"] .dot {
background: var(--surface);
box-shadow: 0 0 12px rgba(82, 224, 219, 0.75);
}
[data-mode="underground"] .dot {
background: var(--ground);
box-shadow: 0 0 12px rgba(243, 184, 91, 0.7);
}
.mode-button strong {
display: block;
font-size: 13px;
}
.mode-button small {
display: block;
margin-top: 4px;
color: var(--muted);
font-size: 10px;
}
.route-copy {
margin: 14px 0 12px;
}
.route-copy strong {
display: block;
font-size: 14px;
}
.route-copy p {
min-height: 42px;
margin: 5px 0 9px;
color: var(--muted);
font-size: 11px;
line-height: 1.45;
}
.sequence {
color: #d9f2f4;
font-size: 10px;
line-height: 1.55;
letter-spacing: 0.015em;
}
.timeline {
margin-top: 14px;
padding-top: 13px;
border-top: 1px solid rgba(255, 255, 255, 0.1);
}
.timeline-labels {
display: flex;
justify-content: space-between;
color: var(--muted);
font-size: 9px;
letter-spacing: 0.09em;
text-transform: uppercase;
}
.progress-track {
position: relative;
height: 4px;
margin: 8px 0 6px;
overflow: hidden;
border-radius: 10px;
background: rgba(255, 255, 255, 0.1);
}
.progress-fill {
width: 0;
height: 100%;
border-radius: inherit;
background: linear-gradient(90deg, #61d6df, #d7f6f5);
box-shadow: 0 0 12px rgba(82, 224, 219, 0.55);
}
.controls {
display: grid;
grid-template-columns: 1fr 1fr 1fr;
gap: 7px;
margin-top: 12px;
}
.small-button {
min-height: 34px;
padding: 7px 6px;
border: 1px solid rgba(255, 255, 255, 0.13);
border-radius: 9px;
background: rgba(255, 255, 255, 0.045);
color: #d8eaef;
font-size: 10px;
font-weight: 700;
cursor: pointer;
transition: background 150ms ease;
}
.small-button:hover {
background: rgba(255, 255, 255, 0.11);
}
#inventory {
top: 24px;
right: 24px;
width: 304px;
padding: 17px;
}
.inventory-note {
margin: -5px 0 13px;
color: var(--muted);
font-size: 10px;
line-height: 1.4;
}
.reservoir {
padding: 10px 0;
border-top: 1px solid rgba(255, 255, 255, 0.075);
transition: opacity 180ms ease;
}
.reservoir:first-of-type {
border-top: 0;
}
.reservoir.dimmed {
opacity: 0.52;
}
.reservoir.related {
opacity: 1;
}
.reservoir-head {
display: grid;
grid-template-columns: 11px 1fr auto;
align-items: center;
gap: 8px;
}
.reservoir-icon {
width: 9px;
height: 9px;
border-radius: 2px;
transform: rotate(45deg);
}
.reservoir-name {
font-size: 11px;
font-weight: 720;
}
.reservoir-state {
color: var(--muted);
font-size: 9px;
font-weight: 700;
letter-spacing: 0.04em;
text-transform: uppercase;
}
.reservoir-state.up {
color: #7ee3b4;
}
.reservoir-state.down {
color: #ffbd83;
}
.storage-bar {
height: 5px;
margin: 7px 0 5px 19px;
overflow: hidden;
border-radius: 10px;
background: rgba(255, 255, 255, 0.09);
}
.storage-bar i {
display: block;
width: 0;
height: 100%;
border-radius: inherit;
transition: width 120ms linear;
}
.reservoir-meta {
display: flex;
justify-content: space-between;
margin-left: 19px;
color: #88a7b2;
font-size: 9px;
}
.reservoir-meta span:last-child {
color: #c4dbe2;
}
#event-card {
position: fixed;
left: 50%;
bottom: 24px;
z-index: 8;
display: grid;
grid-template-columns: auto 1fr;
gap: 4px 12px;
width: min(520px, calc(100vw - 48px));
padding: 13px 16px;
border-radius: 15px;
transform: translateX(-50%);
pointer-events: none;
}
#event-kicker {
grid-row: 1 / span 2;
align-self: center;
padding: 7px 8px;
border-radius: 8px;
background: rgba(82, 224, 219, 0.14);
color: var(--surface);
font-size: 9px;
font-weight: 900;
letter-spacing: 0.12em;
}
#event-title {
font-size: 12px;
}
#event-detail {
color: var(--muted);
font-size: 10px;
line-height: 1.35;
}
#camera-help {
position: fixed;
right: 24px;
bottom: 24px;
z-index: 6;
padding: 8px 11px;
border-radius: 10px;
background: rgba(7, 25, 36, 0.56);
color: rgba(239, 249, 251, 0.78);
font-size: 9px;
pointer-events: none;
backdrop-filter: blur(8px);
}
.scene-label {
position: fixed;
z-index: 4;
display: flex;
align-items: center;
gap: 6px;
padding: 5px 8px;
border: 1px solid rgba(255, 255, 255, 0.23);
border-radius: 8px;
background: rgba(7, 25, 36, 0.68);
color: #f2fbfc;
font-size: 9px;
font-weight: 750;
letter-spacing: 0.04em;
pointer-events: none;
transform: translate(-50%, -50%);
white-space: nowrap;
backdrop-filter: blur(8px);
}
.scene-label::before {
width: 7px;
height: 7px;
border-radius: 50%;
background: var(--label-color);
box-shadow: 0 0 8px var(--label-color);
content: "";
}
@media (max-width: 850px) {
header {
top: 12px;
left: 12px;
width: calc(100vw - 24px);
}
header p {
display: none;
}
h1 {
font-size: 25px;
}
#route-panel {
top: 72px;
left: 10px;
width: min(315px, calc(100vw - 20px));
padding: 12px;
border-radius: 14px;
}
.panel-heading {
margin-bottom: 8px;
}
.mode-button {
min-height: 50px;
padding-top: 8px;
}
.route-copy {
margin: 9px 0 0;
}
.route-copy p,
.sequence,
.timeline,
#inventory {
display: none;
}
.controls {
margin-top: 9px;
}
#event-card {
bottom: 12px;
width: calc(100vw - 20px);
}
#camera-help,
.scene-label {
display: none;
}
}
@media (max-height: 690px) and (min-width: 851px) {
#route-panel {
top: 127px;
}
.route-copy p {
min-height: 0;
}
}
</style>
</head>
<body>
<main id="app">
<canvas id="scene" aria-label="Interactive low-poly watershed model"></canvas>
<header>
<div class="eyebrow">Interactive watershed model</div>
<h1>Where does the water go?</h1>
<p>
Choose a surface or underground pathway and watch transfers reshape
the watershed’s reservoirs.
</p>
</header>
<section id="route-panel" class="panel glass" aria-label="Pathway controls">
<div class="panel-heading">
<h2>Transfer pathway</h2>
<span>Restart to compare</span>
</div>
<div class="mode-switch">
<button class="mode-button active" data-mode="surface" type="button">
<span class="dot"></span>
<strong>Surface</strong>
<small>Runoff & evaporation</small>
</button>
<button class="mode-button" data-mode="underground" type="button">
<span class="dot"></span>
<strong>Underground</strong>
<small>Infiltration & discharge</small>
</button>
</div>
<div class="route-copy">
<strong id="route-title">Surface return</strong>
<p id="route-description">
Precipitation falls, snow melts, and runoff returns to the ocean.
</p>
<div id="route-sequence" class="sequence">
Cloud → Rain / snow → Melt → Stream → Ocean
</div>
</div>
<div class="timeline">
<div class="timeline-labels">
<span>Cycle</span>
<span id="time-label">00:00 / 00:24</span>
</div>
<div class="progress-track">
<div id="progress-fill" class="progress-fill"></div>
</div>
</div>
<div class="controls">
<button id="pause-button" class="small-button" type="button">Pause</button>
<button id="replay-button" class="small-button" type="button">Replay</button>
<button id="view-button" class="small-button" type="button">Reset view</button>
</div>
</section>
<section id="inventory" class="panel glass" aria-label="Reservoir inventory">
<div class="panel-heading">
<h2>Reservoir inventory</h2>
<span>5 stores</span>
</div>
<p class="inventory-note">
Relative storage only. Bars show illustrative balance, not measured volume.
</p>
<div class="reservoir related" data-reservoir="atm">
<div class="reservoir-head">
<span class="reservoir-icon" style="background:var(--air)"></span>
<span class="reservoir-name">Atmosphere</span>
<span class="reservoir-state">steady</span>
</div>
<div class="storage-bar">
<i data-bar="atm" style="background:var(--air)"></i>
</div>
<div class="reservoir-meta">
<span>Clouds + vapor</span>
<span data-level="atm">medium</span>
</div>
</div>
<div class="reservoir related" data-reservoir="snow">
<div class="reservoir-head">
<span class="reservoir-icon" style="background:var(--snow)"></span>
<span class="reservoir-name">Snowpack</span>
<span class="reservoir-state">steady</span>
</div>
<div class="storage-bar">
<i data-bar="snow" style="background:var(--snow)"></i>
</div>
<div class="reservoir-meta">
<span>Mountain snow</span>
<span data-level="snow">low</span>
</div>
</div>
<div class="reservoir related" data-reservoir="surface">
<div class="reservoir-head">
<span class="reservoir-icon" style="background:var(--water)"></span>
<span class="reservoir-name">Surface water</span>
<span class="reservoir-state">steady</span>
</div>
<div class="storage-bar">
<i data-bar="surface" style="background:var(--water)"></i>
</div>
<div class="reservoir-meta">
<span>Ocean, lake + stream</span>
<span data-level="surface">low</span>
</div>
</div>
<div class="reservoir" data-reservoir="soil">
<div class="reservoir-head">
<span class="reservoir-icon" style="background:var(--soil)"></span>
<span class="reservoir-name">Soil moisture</span>
<span class="reservoir-state">steady</span>
</div>
<div class="storage-bar">
<i data-bar="soil" style="background:var(--soil)"></i>
</div>
<div class="reservoir-meta">
<span>Upper soil layer</span>
<span data-level="soil">low</span>
</div>
</div>
<div class="reservoir" data-reservoir="ground">
<div class="reservoir-head">
<span class="reservoir-icon" style="background:var(--aquifer)"></span>
<span class="reservoir-name">Groundwater</span>
<span class="reservoir-state">steady</span>
</div>
<div class="storage-bar">
<i data-bar="ground" style="background:var(--aquifer)"></i>
</div>
<div class="reservoir-meta">
<span>Saturated aquifer</span>
<span data-level="ground">medium</span>
</div>
</div>
</section>
<div id="event-card" class="glass" aria-live="polite">
<div id="event-kicker">Now</div>
<strong id="event-title">Precipitation</strong>
<span id="event-detail">Rain and snow lower atmospheric storage.</span>
</div>
<div id="camera-help">Drag to orbit · scroll to zoom · double-click to reset</div>
<div class="scene-label" id="label-snow" style="--label-color:#e9f7f7">Snowpack</div>
<div class="scene-label" id="label-water" style="--label-color:#55bde7">Surface water</div>
<div class="scene-label" id="label-soil" style="--label-color:#b88454">Soil moisture</div>
<div class="scene-label" id="label-ground" style="--label-color:#5c9bc6">Groundwater</div>
</main>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const $ = (id) => document.getElementById(id);
const canvas = $("scene");
// -------------------------------------------------------------------------
// Renderer, camera, and lighting
// -------------------------------------------------------------------------
const renderer = new THREE.WebGLRenderer({
canvas,
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.08;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xa8d9e6);
scene.fog = new THREE.Fog(0xb9dce5, 38, 78);
const camera = new THREE.PerspectiveCamera(
43,
window.innerWidth / window.innerHeight,
0.1,
140
);
const cameraState = {
theta: 0.04,
phi: 1.05,
radius: 30,
target: new THREE.Vector3(0, 0.65, 0)
};
function applyCamera() {
const narrowFactor = Math.max(1, 0.78 / Math.max(camera.aspect, 0.45));
const radius = cameraState.radius * narrowFactor;
const sinPhi = Math.sin(cameraState.phi);
const cosPhi = Math.cos(cameraState.phi);
camera.position.set(
cameraState.target.x + radius * sinPhi * Math.sin(cameraState.theta),
cameraState.target.y + radius * cosPhi,
cameraState.target.z + radius * sinPhi * Math.cos(cameraState.theta)
);
camera.lookAt(cameraState.target);
}
const hemisphere = new THREE.HemisphereLight(0xdaf7ff, 0x5a4937, 2.25);
scene.add(hemisphere);
const sun = new THREE.DirectionalLight(0xfff4d8, 3.15);
sun.position.set(-9, 18, 10);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -18;
sun.shadow.camera.right = 18;
sun.shadow.camera.top = 15;
sun.shadow.camera.bottom = -15;
sun.shadow.camera.near = 2;
sun.shadow.camera.far = 48;
sun.shadow.bias = -0.00035;
scene.add(sun);
const fillLight = new THREE.DirectionalLight(0x8bd9ef, 0.75);
fillLight.position.set(12, 8, -8);
scene.add(fillLight);
// -------------------------------------------------------------------------
// Small deterministic helpers
// -------------------------------------------------------------------------
let seed = 7319;
function random() {
seed = (seed * 1664525 + 1013904223) >>> 0;
return seed / 4294967296;
}
function clamp(value, min, max) {
return Math.max(min, Math.min(max, value));
}
function smooth(min, max, value) {
const t = clamp((value - min) / (max - min), 0, 1);
return t * t * (3 - 2 * t);
}
function addBox(width, height, depth, color, x, y, z, roughness = 1) {
const material = new THREE.MeshStandardMaterial({
color,
roughness,
flatShading: true
});
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(width, height, depth),
material
);
mesh.position.set(x, y, z);
mesh.castShadow = true;
mesh.receiveShadow = true;
scene.add(mesh);
return mesh;
}
// -------------------------------------------------------------------------
// Low-poly bounded terrain and exposed subsurface
// -------------------------------------------------------------------------
function heightAt(x, z) {
const coast = smooth(-6.7, -4.4, x);
const base = THREE.MathUtils.lerp(-0.62, 0.18, coast);
const mountainA =
2.25 *
Math.exp(
-(
Math.pow(x - 4.2, 2) / 12 +
Math.pow(z + 2.5, 2) / 16
)
);
const mountainB =
1.05 *
Math.exp(
-(
Math.pow(x - 8.1, 2) / 10 +
Math.pow(z + 1.2, 2) / 13
)
);
const hills =
0.22 *
Math.sin(x * 0.73 + z * 0.35) *
Math.cos(z * 0.65 - x * 0.12);
return base + mountainA + mountainB + hills * coast;
}
function buildTerrain() {
const nx = 28;
const nz = 18;
const positions = [];
const indices = [];
for (let iz = 0; iz <= nz; iz++) {
const z = -8 + (16 * iz) / nz;
for (let ix = 0; ix <= nx; ix++) {
const x = -12 + (24 * ix) / nx;
positions.push(x, heightAt(x, z), z);
}
}
for (let iz = 0; iz < nz; iz++) {
for (let ix = 0; ix < nx; ix++) {
const a = iz * (nx + 1) + ix;
const b = a + 1;
const c = a + nx + 1;
const d = c + 1;
indices.push(a, c, b, b, c, d);
}
}
const indexed = new THREE.BufferGeometry();
indexed.setAttribute(
"position",
new THREE.Float32BufferAttribute(positions, 3)
);
indexed.setIndex(indices);
const geometry = indexed.toNonIndexed();
indexed.dispose();
geometry.computeVertexNormals();
const position = geometry.getAttribute("position");
const colors = new Float32Array(position.count * 3);
for (let i = 0; i < position.count; i += 3) {
const x =
(position.getX(i) +
position.getX(i + 1) +
position.getX(i + 2)) /
3;
const y =
(position.getY(i) +
position.getY(i + 1) +
position.getY(i + 2)) /
3;
let color;
if (x < -5.3 && y < 0) {
color = new THREE.Color(0xb7aa7b);
} else if (y > 1.72) {
color = new THREE.Color(0xeaf3f1);
} else if (y > 1.12) {
color = new THREE.Color(0x79877a);
} else if (y > 0.48) {
color = new THREE.Color(0x62865c);
} else {
color = new THREE.Color(0x92aa62);
}
color.offsetHSL(
(random() - 0.5) * 0.025,
(random() - 0.5) * 0.06,
(random() - 0.5) * 0.07
);
for (let j = 0; j < 3; j++) {
const index = (i + j) * 3;
colors[index] = color.r;
colors[index + 1] = color.g;
colors[index + 2] = color.b;
}
}
geometry.setAttribute("color", new THREE.BufferAttribute(colors, 3));
const material = new THREE.MeshStandardMaterial({
vertexColors: true,
roughness: 1,
flatShading: true
});
const terrain = new THREE.Mesh(geometry, material);
terrain.castShadow = true;
terrain.receiveShadow = true;
scene.add(terrain);
}
buildTerrain();
// Dark foundation and an outlined plinth make the model visibly bounded.
addBox(24.25, 0.48, 16.25, 0x24353a, 0, -3.02, 0, 0.9);
const outline = new THREE.LineSegments(
new THREE.EdgesGeometry(new THREE.BoxGeometry(24.25, 0.48, 16.25)),
new THREE.LineBasicMaterial({ color: 0x0e252d, transparent: true, opacity: 0.7 })
);
outline.position.set(0, -3.02, 0);
scene.add(outline);
// Land layers terminate at the coastline, leaving the ocean side open.
addBox(17.15, 0.5, 16, 0x64462f, 3.43, -0.34, 0);
addBox(17.15, 0.82, 16, 0x98704a, 3.43, -1.0, 0);
addBox(17.15, 1.0, 16, 0x8b684a, 3.43, -1.91, 0);
addBox(17.15, 0.5, 16, 0x65534a, 3.43, -2.66, 0);
// Visible moisture zones on the cutaway face.
const soilMoistureMesh = addBox(
12.8,
0.43,
0.12,
0x60402c,
4.15,
-0.32,
8.07
);
soilMoistureMesh.material.transparent = true;
soilMoistureMesh.material.opacity = 0.88;
const groundwaterMesh = addBox(
18.3,
0.76,
0.14,
0x5c9bc6,
2.1,
-1.72,
8.09
);
groundwaterMesh.material.transparent = true;
groundwaterMesh.material.opacity = 0.48;
groundwaterMesh.material.depthWrite = false;
// -------------------------------------------------------------------------
// Ocean, river, and lake
// -------------------------------------------------------------------------
const oceanGeometry = new THREE.PlaneGeometry(6.9, 16, 8, 14);
oceanGeometry.rotateX(-Math.PI / 2);
const oceanPosition = oceanGeometry.getAttribute("position");
const oceanColors = new Float32Array(oceanPosition.count * 3);
const deepOcean = new THREE.Color(0x237f9f);
const shallowOcean = new THREE.Color(0x52b6cb);
for (let i = 0; i < oceanPosition.count; i++) {
const localX = oceanPosition.getX(i);
const color = deepOcean.clone().lerp(
shallowOcean,
clamp((localX + 3.45) / 6.9, 0, 1)
);
color.offsetHSL(0, 0, (random() - 0.5) * 0.035);
oceanColors[i * 3] = color.r;
oceanColors[i * 3 + 1] = color.g;
oceanColors[i * 3 + 2] = color.b;
}
oceanGeometry.setAttribute(
"color",
new THREE.BufferAttribute(oceanColors, 3)
);
const oceanMaterial = new THREE.MeshStandardMaterial({
vertexColors: true,
roughness: 0.34,
metalness: 0.06,
transparent: true,
opacity: 0.87,
flatShading: true,
side: THREE.DoubleSide
});
const ocean = new THREE.Mesh(oceanGeometry, oceanMaterial);
ocean.position.set(-8.55, -0.07, 0);
ocean.receiveShadow = true;
scene.add(ocean);
const oceanVolume = new THREE.Mesh(
new THREE.BoxGeometry(6.9, 2.4, 16),
new THREE.MeshStandardMaterial({
color: 0x2587a9,
roughness: 0.5,
transparent: true,
opacity: 0.25,
depthWrite: false
})
);
oceanVolume.position.set(-8.55, -1.29, 0);
scene.add(oceanVolume);
const riverPoints = [
new THREE.Vector3(4.2, heightAt(4.2, -2.5) + 0.08, -2.5),
new THREE.Vector3(3.45, heightAt(3.45, -1.55) + 0.08, -1.55),
new THREE.Vector3(1.85, heightAt(1.85, -0.65) + 0.08, -0.65),
new THREE.Vector3(0.2, heightAt(0.2, 0.2) + 0.08, 0.2),
new THREE.Vector3(-2.25, heightAt(-2.25, 0.7) + 0.08, 0.7),
new THREE.Vector3(-4.65, 0.04, 0.8),
new THREE.Vector3(-7.25, 0.015, 0.8)
];
const riverCurve = new THREE.CatmullRomCurve3(
riverPoints,
false,
"catmullrom",
0.35
);
const river = new THREE.Mesh(
new THREE.TubeGeometry(riverCurve, 72, 0.13, 5, false),
new THREE.MeshStandardMaterial({
color: 0x43b9dc,
roughness: 0.3,
metalness: 0.04,
flatShading: true
})
);
river.castShadow = true;
scene.add(river);
const lake = new THREE.Mesh(
new THREE.CircleGeometry(1, 14),
new THREE.MeshStandardMaterial({
color: 0x48b8d1,
roughness: 0.32,
transparent: true,
opacity: 0.9,
side: THREE.DoubleSide
})
);
lake.rotation.x = -Math.PI / 2;
lake.scale.set(1.28, 0.72, 1);
lake.position.set(-0.65, heightAt(-0.65, 2.4) + 0.065, 2.4);
scene.add(lake);
// -------------------------------------------------------------------------
// Snow, trees, and low-poly clouds
// -------------------------------------------------------------------------
const snowMeshes = [];
for (const [x, z, sx, sy, sz] of [
[4.15, -2.55, 1.25, 0.28, 1.0],
[3.55, -2.15, 0.82, 0.23, 0.7],
[4.95, -2.05, 0.88, 0.25, 0.72]
]) {
const snow = new THREE.Mesh(
new THREE.IcosahedronGeometry(1, 1),
new THREE.MeshStandardMaterial({
color: 0xf0f8f5,
roughness: 1,
transparent: true,
opacity: 0.9,
flatShading: true
})
);
snow.position.set(x, heightAt(x, z) + 0.08, z);
snow.scale.set(sx, sy, sz);
snow.castShadow = true;
scene.add(snow);
snowMeshes.push({
mesh: snow,
baseScale: snow.scale.clone()
});
}
const treePositions = [];
for (let i = 0; i < 27; i++) {
const x = -3.9 + random() * 14;
const z = -5.6 + random() * 11.2;
const y = heightAt(x, z);
if (y < 0.15 || y > 1.35 || Math.abs(z - 0.5) < 0.75) {
continue;
}
const scale = 0.7 + random() * 0.5;
const tree = new THREE.Group();
const trunk = new THREE.Mesh(
new THREE.CylinderGeometry(0.08, 0.12, 0.55, 5),
new THREE.MeshStandardMaterial({ color: 0x65452e, roughness: 1 })
);
trunk.position.y = 0.27;
const foliage = new THREE.Mesh(
new THREE.ConeGeometry(0.39, 0.9, 6),
new THREE.MeshStandardMaterial({
color: random() > 0.45 ? 0x356c4e : 0x477a53,
roughness: 1,
flatShading: true
})
);
foliage.position.y = 0.83;
tree.add(trunk, foliage);
tree.position.set(x, y, z);
tree.scale.setScalar(scale);
tree.rotation.y = random() * Math.PI * 2;
tree.castShadow = true;
scene.add(tree);
treePositions.push(new THREE.Vector3(x, y + 1.15 * scale, z));
}
function createCloud(position, scale = 1) {
const group = new THREE.Group();
const material = new THREE.MeshStandardMaterial({
color: 0xf3fbfc,
roughness: 1,
transparent: true,
opacity: 0.9,
flatShading: true,
depthWrite: false
});
const blobs = [
[-1.05, 0, 0, 0.95],
[-0.35, 0.22, 0.05, 1.15],
[0.45, 0.08, 0, 1.05],
[1.05, -0.03, 0.05, 0.8],
[0.05, -0.15, 0.45, 0.78]
];
for (const [x, y, z, s] of blobs) {
const mesh = new THREE.Mesh(
new THREE.IcosahedronGeometry(1, 1),
material
);
mesh.position.set(x, y, z);
mesh.scale.set(s, s * 0.55, s * 0.72);
mesh.castShadow = true;
group.add(mesh);
}
group.position.copy(position);
group.scale.setScalar(scale);
scene.add(group);
return { group, material };
}
const stormCloud = createCloud(
new THREE.Vector3(4.15, 5.42, -2.45),
1.02
);
const vaporCloud = createCloud(
new THREE.Vector3(-1.5, 5.75, 1.0),
0.9
);
// -------------------------------------------------------------------------
// Transfer paths
// -------------------------------------------------------------------------
const routes = [];
function makeRoute(points, color, options = {}) {
const {
mode = "surface",
start = 0,
end = 24,
radius = 0.045,
speed = 0.14
} = options;
const curve = new THREE.CatmullRomCurve3(
points,
false,
"catmullrom",
0.38
);
const group = new THREE.Group();
const lineMaterial = new THREE.MeshBasicMaterial({
color,
transparent: true,
opacity: 0.8,
depthWrite: false
});
const line = new THREE.Mesh(
new THREE.TubeGeometry(curve, 72, radius, 5, false),
lineMaterial
);
group.add(line);
const arrowMaterial = new THREE.MeshBasicMaterial({
color,
transparent: true,
opacity: 0.95,
depthWrite: false
});
const arrows = [];
for (const t of [0.16, 0.38, 0.61, 0.83]) {
const arrow = new THREE.Mesh(
new THREE.ConeGeometry(radius * 3.4, radius * 8.5, 5),
arrowMaterial
);
arrow.position.copy(curve.getPointAt(t));
arrow.quaternion.setFromUnitVectors(
new THREE.Vector3(0, 1, 0),
curve.getTangentAt(t).normalize()
);
group.add(arrow);
arrows.push(arrow);
}
const particleMaterial = new THREE.MeshBasicMaterial({
color,
transparent: true,
opacity: 0.98,
depthWrite: false
});
const particles = [];
for (let i = 0; i < 5; i++) {
const particle = new THREE.Mesh(
new THREE.IcosahedronGeometry(radius * 2.25, 0),
particleMaterial
);
particle.userData.offset = i / 5;
group.add(particle);
particles.push(particle);
}
scene.add(group);
const route = {
curve,
group,
lineMaterial,
arrowMaterial,
particleMaterial,
particles,
mode,
start,
end,
speed
};
routes.push(route);
return route;
}
const surfaceRunoff = makeRoute(
[
new THREE.Vector3(4.2, heightAt(4.2, -2.5) + 0.27, -2.5),
new THREE.Vector3(3.45, heightAt(3.45, -1.55) + 0.27, -1.55),
new THREE.Vector3(1.85, heightAt(1.85, -0.65) + 0.27, -0.65),
new THREE.Vector3(0.2, heightAt(0.2, 0.2) + 0.25, 0.2),
new THREE.Vector3(-2.25, heightAt(-2.25, 0.7) + 0.24, 0.7),
new THREE.Vector3(-4.65, 0.2, 0.8),
new THREE.Vector3(-7.25, 0.18, 0.8)
],
0x52e0db,
{
mode: "surface",
start: 3.2,
end: 15.2,
speed: 0.13
}
);
const evaporation = makeRoute(
[
new THREE.Vector3(-8.1, 0.05, 3.0),
new THREE.Vector3(-7.5, 1.55, 2.65),
new THREE.Vector3(-5.4, 3.85, 1.8),
new THREE.Vector3(-3.0, 5.2, 1.25),
new THREE.Vector3(-1.5, 5.7, 1.0)
],
0x9fe9ec,
{
mode: "surface",
start: 15,
end: 24,
radius: 0.037,
speed: 0.1
}
);
const precipitation = makeRoute(
[
new THREE.Vector3(4.15, 4.85, -2.45),
new THREE.Vector3(4.15, 3.7, -2.45),
new THREE.Vector3(4.15, heightAt(4.15, -2.45) + 0.12, -2.45)
],
0xc9f5ff,
{
mode: "common",
start: 0,
end: 5.1,
radius: 0.035,
speed: 0.2
}
);
const infiltration = makeRoute(
[
new THREE.Vector3(5.8, heightAt(5.8, 2.3) + 0.12, 2.3),
new THREE.Vector3(5.8, 0.25, 2.6),
new THREE.Vector3(5.8, -0.25, 3.4),
new THREE.Vector3(5.8, -0.65, 5.9),
new THREE.Vector3(5.8, -0.88, 8.12)
],
0xf3b85b,
{
mode: "underground",
start: 2.1,
end: 8.4,
radius: 0.05,
speed: 0.11
}
);
const percolation = makeRoute(
[
new THREE.Vector3(5.8, -0.88, 8.12),
new THREE.Vector3(5.8, -1.15, 8.34),
new THREE.Vector3(5.8, -1.52, 8.38)
],
0xf3b85b,
{
mode: "underground",
start: 6.3,
end: 14,
radius: 0.05,
speed: 0.09
}
);
const groundwaterFlow = makeRoute(
[
new THREE.Vector3(5.8, -1.73, 8.38),
new THREE.Vector3(2.7, -1.73, 8.38),
new THREE.Vector3(-1.7, -1.73, 8.38),
new THREE.Vector3(-5.4, -1.6, 8.2),
new THREE.Vector3(-7.55, -0.85, 7.55),
new THREE.Vector3(-8.65, -0.04, 6.45)
],
0xf3b85b,
{
mode: "underground",
start: 10.2,
end: 22.5,
radius: 0.055,
speed: 0.075
}
);
// -------------------------------------------------------------------------
// Weather and vapor particles
// -------------------------------------------------------------------------
function createPoints(count, color, size) {
const geometry = new THREE.BufferGeometry();
const positions = new Float32Array(count * 3);
geometry.setAttribute(
"position",
new THREE.BufferAttribute(positions, 3)
);
const material = new THREE.PointsMaterial({
color,
size,
transparent: true,
opacity: 0,
depthWrite: false,
sizeAttenuation: true
});
const points = new THREE.Points(geometry, material);
points.frustumCulled = false;
scene.add(points);
return {
points,
geometry,
material,
positions,
offsets: Array.from({ length: count }, () => random()),
positions2: Array.from({ length: count }, () => ({
x: random(),
y: random(),
z: random()
}))
};
}
const rain = createPoints(105, 0xbcefff, 0.065);
const snow = createPoints(65, 0xffffff, 0.115);
const vapor = createPoints(70, 0xe0fbff, 0.09);
const transpiration = createPoints(45, 0xbdeacf, 0.08);
// -------------------------------------------------------------------------
// Illustrative reservoir model
// -------------------------------------------------------------------------
const duration = 24;
const stateKeys = {
surface: [
{ t: 0, atm: 56, snow: 25, surface: 38, soil: 28, ground: 52 },
{ t: 4, atm: 38, snow: 43, surface: 39, soil: 29, ground: 52 },
{ t: 8, atm: 39, snow: 34, surface: 47, soil: 29, ground: 52 },
{ t: 13, atm: 43, snow: 25, surface: 55, soil: 28, ground: 52 },
{ t: 18, atm: 51, snow: 23, surface: 45, soil: 28, ground: 52 },
{ t: 24, atm: 56, snow: 25, surface: 38, soil: 28, ground: 52 }
],
underground: [
{ t: 0, atm: 56, snow: 25, surface: 38, soil: 28, ground: 52 },
{ t: 4, atm: 40, snow: 40, surface: 38, soil: 34, ground: 52 },
{ t: 8, atm: 41, snow: 39, surface: 38, soil: 45, ground: 54 },
{ t: 13, atm: 43, snow: 38, surface: 38, soil: 42, ground: 64 },
{ t: 18, atm: 49, snow: 37, surface: 39, soil: 36, ground: 67 },
{ t: 24, atm: 56, snow: 39, surface: 38, soil: 29, ground: 58 }
]
};
function sampleState(mode, time) {
const keys = stateKeys[mode];
const t = clamp(time, 0, duration);
for (let i = 0; i < keys.length - 1; i++) {
const a = keys[i];
const b = keys[i + 1];
if (t >= a.t && t <= b.t) {
const f = smooth(a.t, b.t, t);
return {
atm: THREE.MathUtils.lerp(a.atm, b.atm, f),
snow: THREE.MathUtils.lerp(a.snow, b.snow, f),
surface: THREE.MathUtils.lerp(a.surface, b.surface, f),
soil: THREE.MathUtils.lerp(a.soil, b.soil, f),
ground: THREE.MathUtils.lerp(a.ground, b.ground, f)
};
}
}
return { ...keys[keys.length - 1] };
}
let mode = "surface";
let elapsed = 0;
let playing = true;
let lastEventKey = "";
function storageWord(value) {
if (value >= 72) return "high";
if (value >= 47) return "medium";
if (value >= 27) return "low";
return "minimal";
}
function updateInventory(state) {
const next = sampleState(mode, Math.min(duration, elapsed + 0.08));
for (const key of ["atm", "snow", "surface", "soil", "ground"]) {
const bar = document.querySelector(`[data-bar="${key}"]`);
const level = document.querySelector(`[data-level="${key}"]`);
const badge = document
.querySelector(`[data-reservoir="${key}"]`)
.querySelector(".reservoir-state");
const value = state[key];
const change = next[key] - value;
bar.style.width = `${clamp(value, 0, 100)}%`;
level.textContent = storageWord(value);
badge.classList.remove("up", "down");
if (change > 0.08) {
badge.textContent = "gaining";
badge.classList.add("up");
} else if (change < -0.08) {
badge.textContent = "releasing";
badge.classList.add("down");
} else {
badge.textContent = "steady";
}
}
const related =
mode === "surface"
? new Set(["atm", "snow", "surface"])
: new Set(["soil", "ground"]);
document.querySelectorAll(".reservoir").forEach((row) => {
row.classList.toggle(
"related",
related.has(row.dataset.reservoir)
);
row.classList.toggle(
"dimmed",
!related.has(row.dataset.reservoir)
);
});
}
function updateVisualReservoirs(state) {
const cloudScale = 0.82 + (state.atm / 100) * 0.3;
stormCloud.group.scale.setScalar(cloudScale);
vaporCloud.group.scale.setScalar(cloudScale * 0.9);
stormCloud.material.opacity = 0.52 + (state.atm / 100) * 0.42;
vaporCloud.material.opacity = 0.48 + (state.atm / 100) * 0.4;
const snowFactor = clamp((state.snow - 16) / 29, 0.08, 1);
for (const item of snowMeshes) {
item.mesh.material.opacity = snowFactor * 0.94;
item.mesh.scale
.copy(item.baseScale)
.multiplyScalar(0.58 + snowFactor * 0.42);
}
oceanMaterial.opacity = 0.78 + (state.surface / 100) * 0.16;
lake.scale.set(
0.86 + (state.surface / 100) * 0.62,
0.48 + (state.surface / 100) * 0.42,
1
);
const drySoil = new THREE.Color(0x73503a);
const wetSoil = new THREE.Color(0x3d2b22);
soilMoistureMesh.material.color.copy(
drySoil.lerp(wetSoil, clamp((state.soil - 24) / 30, 0, 1))
);
groundwaterMesh.material.opacity =
0.24 + clamp(state.ground / 100, 0, 1) * 0.5;
groundwaterMesh.scale.y = 0.68 + (state.ground / 100) * 0.52;
}
function updateRoutes() {
for (const route of routes) {
const selected =
route.mode === "common" || route.mode === mode;
const active =
elapsed >= route.start && elapsed <= route.end;
const lineOpacity = selected
? active
? 0.9
: 0.2
: 0.07;
route.lineMaterial.opacity = lineOpacity;
route.arrowMaterial.opacity = selected
? active
? 0.96
: 0.16
: 0.05;
route.particleMaterial.opacity = selected && active ? 0.98 : 0;
for (const particle of route.particles) {
particle.visible = selected && active;
if (particle.visible) {
const t = (elapsed * route.speed + particle.userData.offset) % 1;
particle.position.copy(route.curve.getPointAt(t));
}
}
}
}
function updateParticles() {
const rainFade =
elapsed < 4.5
? 1
: clamp(1 - (elapsed - 4.5) / 0.9, 0, 1);
rain.material.opacity = rainFade * 0.78;
for (let i = 0; i < rain.positions.length / 3; i++) {
const p = rain.positions2[i];
const fall = (elapsed * 0.58 + rain.offsets[i]) % 1;
rain.positions[i * 3] =
4.15 + (p.x - 0.5) * 4.2 + Math.sin(fall * 8 + i) * 0.05;
rain.positions[i * 3 + 1] = 5.0 - fall * 4.8;
rain.positions[i * 3 + 2] =
-2.45 + (p.z - 0.5) * 3.7;
}
rain.geometry.attributes.position.needsUpdate = true;
snow.material.opacity = rainFade * 0.85;
for (let i = 0; i < snow.positions.length / 3; i++) {
const p = snow.positions2[i];
const fall = (elapsed * 0.35 + snow.offsets[i]) % 1;
snow.positions[i * 3] =
4.15 + (p.x - 0.5) * 4.0 + Math.sin(elapsed + i) * 0.12;
snow.positions[i * 3 + 1] = 5.0 - fall * 4.5;
snow.positions[i * 3 + 2] =
-2.45 + (p.z - 0.5) * 3.4;
}
snow.geometry.attributes.position.needsUpdate = true;
const vaporActive =
elapsed > 14.5 && elapsed < duration + 0.5;
vapor.material.opacity = vaporActive
? Math.sin(clamp((elapsed - 14.5) / 9.5, 0, 1) * Math.PI) * 0.62
: 0;
for (let i = 0; i < vapor.positions.length / 3; i++) {
const p = vapor.positions2[i];
const rise = (elapsed * 0.18 + vapor.offsets[i]) % 1;
vapor.positions[i * 3] =
-7.8 + (p.x - 0.5) * 2.5 + rise * 3.8;
vapor.positions[i * 3 + 1] = 0.05 + rise * 5.1;
vapor.positions[i * 3 + 2] =
2.5 + (p.z - 0.5) * 4.0;
}
vapor.geometry.attributes.position.needsUpdate = true;
transpiration.material.opacity =
elapsed > 17 && elapsed < duration
? 0.48
: 0;
for (let i = 0; i < transpiration.positions.length / 3; i++) {
const tree =
treePositions[
Math.floor(transpiration.offsets[i] * treePositions.length)
] || new THREE.Vector3(2, 0, 0);
const rise =
(elapsed * 0.16 + transpiration.offsets[i]) % 1;
transpiration.positions[i * 3] =
tree.x + Math.sin(rise * 7 + i) * 0.18;
transpiration.positions[i * 3 + 1] =
tree.y + rise * 3.4;
transpiration.positions[i * 3 + 2] =
tree.z + Math.cos(rise * 6 + i) * 0.18;
}
transpiration.geometry.attributes.position.needsUpdate = true;
}
const events = {
surface: [
{
start: 0,
title: "Precipitation",
detail: "Rain and snow lower atmospheric storage and add to the mountain snowpack."
},
{
start: 4,
title: "Snowmelt",
detail: "Meltwater leaves snow storage and feeds the stream network."
},
{
start: 8,
title: "Surface runoff",
detail: "Surface storage rises as water moves downslope toward the ocean."
},
{
start: 15,
title: "Evaporation",
detail: "Ocean water returns to the atmosphere as vapor and clouds reform."
},
{
start: 22,
title: "Balancing",
detail: "Runoff recedes, snow remains, and the surface system settles."
}
],
underground: [
{
start: 0,
title: "Precipitation",
detail: "Rainfall lands on the watershed and begins to soak into the ground."
},
{
start: 3,
title: "Infiltration",
detail: "Soil moisture rises as surface water enters the upper soil layer."
},
{
start: 7,
title: "Percolation",
detail: "Water moves downward into the aquifer, recharging groundwater storage."
},
{
start: 13,
title: "Groundwater discharge",
detail: "Groundwater travels laterally and discharges toward the ocean."
},
{
start: 22,
title: "Groundwater recession",
detail: "After recharge stops, groundwater storage slowly recedes."
}
]
};
function updateEvent() {
const list = events[mode];
let selected = list[0];
for (const item of list) {
if (elapsed >= item.start) selected = item;
}
const key = `${mode}-${selected.start}`;
if (key !== lastEventKey) {
$("event-title").textContent = selected.title;
$("event-detail").textContent = selected.detail;
lastEventKey = key;
}
}
function updateTimeline() {
$("progress-fill").style.width = `${(elapsed / duration) * 100}%`;
const seconds = Math.floor(elapsed);
$("time-label").textContent =
`00:${String(seconds).padStart(2, "0")} / 00:${duration}`;
}
// -------------------------------------------------------------------------
// Projected scene labels
// -------------------------------------------------------------------------
const labelData = [
{
element: $("label-snow"),
position: new THREE.Vector3(4.1, 3.15, -2.5)
},
{
element: $("label-water"),
position: new THREE.Vector3(-7.2, 0.35, 4.7)
},
{
element: $("label-soil"),
position: new THREE.Vector3(3.5, -0.25, 8.3)
},
{
element: $("label-ground"),
position: new THREE.Vector3(-0.4, -1.68, 8.4)
}
];
const projected = new THREE.Vector3();
function updateLabels() {
const width = window.innerWidth;
const height = window.innerHeight;
for (const item of labelData) {
projected.copy(item.position).project(camera);
const visible =
projected.z > -1 &&
projected.z < 1 &&
Math.abs(projected.x) < 1.1 &&
Math.abs(projected.y) < 1.1;
item.element.style.display = visible ? "flex" : "none";
if (visible) {
const x = (projected.x * 0.5 + 0.5) * width;
const y = (-projected.y * 0.5 + 0.5) * height;
item.element.style.left = `${x}px`;
item.element.style.top = `${y}px`;
}
}
}
// -------------------------------------------------------------------------
// UI interaction
// -------------------------------------------------------------------------
function setMode(nextMode) {
mode = nextMode;
elapsed = 0;
playing = true;
lastEventKey = "";
document.querySelectorAll(".mode-button").forEach((button) => {
button.classList.toggle("active", button.dataset.mode === mode);
});
if (mode === "surface") {
$("route-title").textContent = "Surface return";
$("route-description").textContent =
"Precipitation falls, snow melts, and runoff returns to the ocean.";
$("route-sequence").textContent =
"Cloud → Rain / snow → Melt → Stream → Ocean";
$("event-kicker").style.background =
"rgba(82, 224, 219, 0.14)";
$("event-kicker").style.color = "#52e0db";
} else {
$("route-title").textContent = "Subsurface return";
$("route-description").textContent =
"Rain infiltrates, percolates, and travels through groundwater before discharge.";
$("route-sequence").textContent =
"Rain → Soil moisture → Aquifer → Groundwater → Ocean";
$("event-kicker").style.background =
"rgba(243, 184, 91, 0.14)";
$("event-kicker").style.color = "#f3b85b";
}
$("pause-button").textContent = "Pause";
}
document.querySelectorAll(".mode-button").forEach((button) => {
button.addEventListener("click", () => {
setMode(button.dataset.mode);
});
});
$("pause-button").addEventListener("click", () => {
if (elapsed >= duration) {
elapsed = 0;
playing = true;
} else {
playing = !playing;
}
$("pause-button").textContent =
elapsed >= duration
? "Replay"
: playing
? "Pause"
: "Play";
});
$("replay-button").addEventListener("click", () => {
elapsed = 0;
playing = true;
lastEventKey = "";
$("pause-button").textContent = "Pause";
});
function resetCamera() {
cameraState.theta = 0.04;
cameraState.phi = 1.05;
cameraState.radius = 30;
cameraState.target.set(0, 0.65, 0);
applyCamera();
}
$("view-button").addEventListener("click", resetCamera);
// -------------------------------------------------------------------------
// Direct camera controls: orbit, zoom, and reset
// -------------------------------------------------------------------------
let dragging = false;
let lastPointerX = 0;
let lastPointerY = 0;
canvas.addEventListener("pointerdown", (event) => {
dragging = true;
lastPointerX = event.clientX;
lastPointerY = event.clientY;
canvas.setPointerCapture(event.pointerId);
});
canvas.addEventListener("pointermove", (event) => {
if (!dragging) return;
const dx = event.clientX - lastPointerX;
const dy = event.clientY - lastPointerY;
lastPointerX = event.clientX;
lastPointerY = event.clientY;
cameraState.theta = clamp(
cameraState.theta - dx * 0.005,
-0.92,
0.92
);
cameraState.phi = clamp(
cameraState.phi + dy * 0.0045,
0.68,
1.36
);
applyCamera();
});
function endDrag(event) {
dragging = false;
if (event.pointerId !== undefined && canvas.hasPointerCapture(event.pointerId)) {
canvas.releasePointerCapture(event.pointerId);
}
}
canvas.addEventListener("pointerup", endDrag);
canvas.addEventListener("pointercancel", endDrag);
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraState.radius = clamp(
cameraState.radius * Math.exp(event.deltaY * 0.001),
20,
43
);
applyCamera();
},
{ passive: false }
);
canvas.addEventListener("dblclick", resetCamera);
window.addEventListener("resize", () => {
renderer.setSize(window.innerWidth, window.innerHeight, false);
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
applyCamera();
});
// -------------------------------------------------------------------------
// Main animation loop
// -------------------------------------------------------------------------
const clock = new THREE.Clock();
function animate() {
requestAnimationFrame(animate);
const delta = Math.min(clock.getDelta(), 0.05);
if (playing) {
elapsed = Math.min(duration, elapsed + delta);
if (elapsed >= duration) {
playing = false;
$("pause-button").textContent = "Replay";
}
}
const state = sampleState(mode, elapsed);
updateVisualReservoirs(state);
updateInventory(state);
updateRoutes();
updateParticles();
updateEvent();
updateTimeline();
renderer.render(scene, camera);
updateLabels();
}
renderer.setSize(window.innerWidth, window.innerHeight, false);
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
applyCamera();
setMode("surface");
animate();
</script>
</body>
</html>
```