Prompt specification
Build or customize the Three.js 3D scene "Island water cycle diorama" 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: Island water cycle diorama
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/island-water-cycle-diorama-x1267
Scene Description Prompt:
Make a voxel Three.js scene in a self-contained HTML file using cdnjs: island water cycle diorama. Compose ocean, upland forest, river, lake, and groundwater cutaway; circulate labeled tracers between reservoirs without implying that every droplet follows the same path.
Working Three.js Starter (island-water-cycle-diorama-x1267):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Island Water Cycle — Voxel Diorama</title>
<style>
:root {
--ink: #17333b;
--muted: #5c7378;
--paper: rgba(250, 253, 250, 0.9);
--line: rgba(30, 66, 72, 0.14);
--evap: #42dac9;
--precip: #63bdf5;
--transp: #96db70;
--infil: #f5bd4f;
--ground: #ad83ee;
--runoff: #e9dc62;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background:
radial-gradient(circle at 50% 32%, #f3f8f4 0%, #dce9e5 54%, #cddfdc 100%);
color: var(--ink);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
#scene,
#labels {
position: fixed;
inset: 0;
}
#scene {
z-index: 1;
}
#scene canvas {
display: block;
width: 100%;
height: 100%;
outline: none;
touch-action: none;
cursor: grab;
}
#scene canvas:active {
cursor: grabbing;
}
#labels {
z-index: 3;
pointer-events: none;
overflow: hidden;
transition: opacity 180ms ease;
}
#labels.hidden {
opacity: 0;
}
.ui {
position: fixed;
z-index: 10;
}
.panel {
border: 1px solid var(--line);
background: var(--paper);
box-shadow:
0 18px 45px rgba(39, 73, 78, 0.12),
inset 0 1px 0 rgba(255, 255, 255, 0.72);
backdrop-filter: blur(16px);
-webkit-backdrop-filter: blur(16px);
}
.brand {
top: 22px;
left: 22px;
width: min(370px, calc(100vw - 44px));
padding: 19px 21px 18px;
border-radius: 18px;
}
.eyebrow {
display: flex;
align-items: center;
gap: 8px;
margin-bottom: 8px;
color: #557176;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.16em;
text-transform: uppercase;
}
.eyebrow::before {
width: 22px;
height: 3px;
border-radius: 4px;
background: var(--evap);
content: "";
}
h1 {
margin: 0;
font-size: clamp(25px, 3vw, 37px);
font-weight: 780;
letter-spacing: -0.045em;
line-height: 0.98;
}
.brand p {
max-width: 320px;
margin: 11px 0 0;
color: var(--muted);
font-size: 12px;
line-height: 1.5;
}
.controls {
top: 22px;
right: 22px;
display: flex;
gap: 7px;
padding: 7px;
border-radius: 15px;
}
button {
border: 1px solid rgba(30, 66, 72, 0.12);
border-radius: 10px;
padding: 9px 11px;
background: rgba(255, 255, 255, 0.74);
color: var(--ink);
font: inherit;
font-size: 11px;
font-weight: 750;
cursor: pointer;
transition:
transform 140ms ease,
background 140ms ease,
border-color 140ms ease;
}
button:hover {
transform: translateY(-1px);
border-color: rgba(30, 66, 72, 0.28);
background: #fff;
}
button:focus-visible {
outline: 3px solid rgba(66, 218, 201, 0.38);
outline-offset: 2px;
}
.legend {
bottom: 22px;
left: 22px;
width: min(310px, calc(100vw - 44px));
padding: 16px 17px 15px;
border-radius: 17px;
}
.legend-title {
display: flex;
align-items: baseline;
justify-content: space-between;
margin-bottom: 10px;
}
.legend-title strong {
font-size: 11px;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.legend-title span {
color: var(--muted);
font-size: 9px;
font-weight: 700;
letter-spacing: 0.06em;
text-transform: uppercase;
}
.legend-row {
display: grid;
grid-template-columns: 12px 1fr;
align-items: center;
gap: 9px;
margin: 7px 0;
color: #355158;
font-size: 11px;
line-height: 1.25;
}
.swatch {
width: 9px;
height: 9px;
border-radius: 2px;
box-shadow: 0 0 0 3px rgba(255, 255, 255, 0.65);
}
.legend-note {
margin-top: 11px;
padding-top: 10px;
border-top: 1px solid var(--line);
color: var(--muted);
font-size: 10px;
line-height: 1.48;
}
.hint {
bottom: 22px;
right: 22px;
width: min(365px, calc(100vw - 44px));
padding: 14px 16px;
border-radius: 15px;
color: #476066;
font-size: 11px;
line-height: 1.45;
}
.hint strong {
color: var(--ink);
}
.label {
position: absolute;
transform: translate(-50%, -50%);
white-space: nowrap;
will-change: left, top;
}
.label.reservoir {
padding: 6px 9px;
border: 1px solid rgba(28, 60, 66, 0.18);
border-radius: 7px;
background: rgba(252, 254, 251, 0.9);
box-shadow: 0 5px 14px rgba(31, 67, 72, 0.13);
color: #1d3c43;
font-size: 10px;
font-weight: 850;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.label.reservoir::before {
display: inline-block;
width: 5px;
height: 5px;
margin-right: 6px;
border-radius: 1px;
background: var(--c, #2b5961);
content: "";
vertical-align: 1px;
}
.label.process {
padding: 5px 8px;
border: 1px solid color-mix(in srgb, var(--c) 48%, white);
border-left: 4px solid var(--c);
border-radius: 6px;
background: rgba(252, 254, 251, 0.84);
box-shadow: 0 4px 12px rgba(31, 67, 72, 0.1);
color: #365159;
font-size: 9px;
font-weight: 760;
letter-spacing: 0.015em;
}
.label.process.subtle {
padding: 4px 7px;
background: rgba(248, 252, 249, 0.74);
font-size: 8px;
}
#loading {
position: fixed;
inset: 0;
z-index: 30;
display: grid;
place-items: center;
background: #dce9e5;
color: #365159;
font-size: 12px;
font-weight: 800;
letter-spacing: 0.12em;
text-transform: uppercase;
transition: opacity 350ms ease;
}
#loading.done {
opacity: 0;
pointer-events: none;
}
@media (max-width: 760px) {
.brand {
top: 12px;
left: 12px;
width: min(310px, calc(100vw - 24px));
padding: 14px 16px;
}
.brand p {
display: none;
}
.controls {
top: 12px;
right: 12px;
flex-direction: column;
}
.controls button {
padding: 8px 10px;
font-size: 10px;
}
.legend {
bottom: 12px;
left: 12px;
width: min(270px, calc(100vw - 24px));
padding: 12px 14px;
}
.legend-row {
margin: 5px 0;
font-size: 10px;
}
.hint {
display: none;
}
.label.process {
font-size: 8px;
}
}
</style>
</head>
<body>
<div id="scene"></div>
<div id="labels" aria-hidden="true"></div>
<header class="ui panel brand">
<div class="eyebrow">Reservoir network / 01</div>
<h1>Island Water Cycle</h1>
<p>
A voxel cutaway linking atmosphere, surface water, soil, groundwater,
vegetation, and ocean.
</p>
</header>
<nav class="ui panel controls" aria-label="Scene controls">
<button id="pause" type="button">Pause tracers</button>
<button id="toggle-labels" type="button">Hide labels</button>
<button id="reset-view" type="button">Reset view</button>
</nav>
<aside class="ui panel legend">
<div class="legend-title">
<strong>Possible transfers</strong>
<span>Local packets</span>
</div>
<div class="legend-row">
<span class="swatch" style="background: var(--evap)"></span>
<span>Evaporation · ocean / lake → air</span>
</div>
<div class="legend-row">
<span class="swatch" style="background: var(--precip)"></span>
<span>Precipitation · air → land / water</span>
</div>
<div class="legend-row">
<span class="swatch" style="background: var(--transp)"></span>
<span>Transpiration · forest → air</span>
</div>
<div class="legend-row">
<span class="swatch" style="background: var(--infil)"></span>
<span>Infiltration · soil → groundwater</span>
</div>
<div class="legend-row">
<span class="swatch" style="background: var(--ground)"></span>
<span>Groundwater flow · aquifer → lake / ocean</span>
</div>
<div class="legend-row">
<span class="swatch" style="background: var(--runoff)"></span>
<span>Surface runoff · upland / lake → river / ocean</span>
</div>
<div class="legend-note">
Each tracer represents one local transfer. The colored packets are
independent examples—not one droplet completing a fixed loop.
</div>
</aside>
<aside class="ui panel hint">
<strong>Explore the diorama.</strong>
Drag to orbit, right-drag to pan, and scroll to zoom. Double-click to reset.
The exposed blue stratum is saturated groundwater; the blocky terrain is
sliced open at the front.
</aside>
<div id="loading">Building water-cycle diorama…</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 labelsHost = document.getElementById("labels");
const loading = document.getElementById("loading");
// ------------------------------------------------------------
// Renderer, scene, camera
// ------------------------------------------------------------
const renderer = new THREE.WebGLRenderer({
antialias: true,
alpha: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.08;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.domElement.setAttribute("aria-label", "Interactive voxel island water-cycle diorama");
sceneHost.appendChild(renderer.domElement);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xdce9e5);
scene.fog = new THREE.Fog(0xdce9e5, 34, 62);
const camera = new THREE.PerspectiveCamera(
38,
window.innerWidth / window.innerHeight,
0.1,
100
);
const cameraState = {
yaw: 0.68,
pitch: 0.56,
distance: 28,
target: new THREE.Vector3(0, 3.0, 0)
};
const defaultCamera = {
yaw: 0.68,
pitch: 0.56,
distance: 28,
target: new THREE.Vector3(0, 3.0, 0)
};
function updateCamera() {
const { yaw, pitch, distance, target } = cameraState;
const horizontal = Math.cos(pitch) * distance;
camera.position.set(
target.x + Math.sin(yaw) * horizontal,
target.y + Math.sin(pitch) * distance,
target.z + Math.cos(yaw) * horizontal
);
camera.lookAt(target);
camera.updateMatrixWorld();
}
updateCamera();
const hemi = new THREE.HemisphereLight(0xf7fff9, 0x53666a, 2.05);
scene.add(hemi);
const sun = new THREE.DirectionalLight(0xfff4d8, 3.1);
sun.position.set(-12, 20, 13);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -19;
sun.shadow.camera.right = 19;
sun.shadow.camera.top = 18;
sun.shadow.camera.bottom = -18;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 55;
sun.shadow.bias = -0.0004;
scene.add(sun);
const fill = new THREE.DirectionalLight(0x8fd7df, 0.8);
fill.position.set(12, 8, -14);
scene.add(fill);
// ------------------------------------------------------------
// Shared geometry and voxel batching
// ------------------------------------------------------------
const cubeGeometry = new THREE.BoxGeometry(1, 1, 1);
const voxelMaterial = new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 0.88,
metalness: 0.02
});
const voxelTransforms = [];
const dummy = new THREE.Object3D();
function addVoxel(x, y, z, sx, sy, sz, color) {
voxelTransforms.push({
x, y, z, sx, sy, sz,
color: new THREE.Color(color)
});
}
function colorVariant(base, amount) {
const c = new THREE.Color(base);
c.offsetHSL(0, 0, amount);
return c;
}
function hash2(x, z) {
const n = Math.sin(x * 127.1 + z * 311.7) * 43758.5453123;
return n - Math.floor(n);
}
// ------------------------------------------------------------
// Diorama base and ocean
// ------------------------------------------------------------
const plinth = new THREE.Mesh(
new THREE.BoxGeometry(24.7, 0.62, 16.7),
new THREE.MeshStandardMaterial({
color: 0x29484f,
roughness: 0.78
})
);
plinth.position.set(0, -3.08, 0);
plinth.castShadow = true;
plinth.receiveShadow = true;
scene.add(plinth);
const lowerBase = new THREE.Mesh(
new THREE.BoxGeometry(24, 0.42, 16),
new THREE.MeshStandardMaterial({
color: 0x60767a,
roughness: 0.9
})
);
lowerBase.position.set(0, -2.55, 0);
lowerBase.castShadow = true;
lowerBase.receiveShadow = true;
scene.add(lowerBase);
const oceanMaterial = new THREE.MeshPhysicalMaterial({
color: 0x43b9c4,
roughness: 0.28,
metalness: 0.02,
transparent: true,
opacity: 0.88,
clearcoat: 0.45,
clearcoatRoughness: 0.25
});
const ocean = new THREE.Mesh(
new THREE.BoxGeometry(23.3, 0.56, 15.3),
oceanMaterial
);
ocean.position.set(0, 1.02, 0);
ocean.receiveShadow = true;
scene.add(ocean);
// A quiet shadow-catching floor beneath the diorama.
const floor = new THREE.Mesh(
new THREE.PlaneGeometry(90, 90),
new THREE.MeshStandardMaterial({
color: 0xd7e5e1,
roughness: 1
})
);
floor.rotation.x = -Math.PI / 2;
floor.position.y = -3.42;
floor.receiveShadow = true;
scene.add(floor);
// ------------------------------------------------------------
// Island terrain, river and lake masks
// ------------------------------------------------------------
const terrainHeight = new Map();
const waterCells = new Set();
const key = (x, z) => `${x},${z}`;
for (let x = -2; x <= 10; x++) {
for (let z = -6; z <= 5; z++) {
const nx = (x - 4.1) / 6.8;
const nz = (z + 0.1) / 5.5;
const radius = Math.sqrt(nx * nx + nz * nz);
if (radius > 1.04) continue;
const mountain =
2.45 * Math.exp(-((x - 4) * (x - 4) / 4 + (z + 3) * (z + 3) / 3));
let h =
2 +
Math.floor(
3.05 * (1 - Math.min(radius, 1)) +
mountain +
0.4 * Math.sin(x * 1.7 + z * 0.9)
);
h = THREE.MathUtils.clamp(h, 2, 7);
terrainHeight.set(key(x, z), h);
}
}
function rasterize(points) {
const cells = new Set();
for (let i = 0; i < points.length - 1; i++) {
const [x0, z0] = points[i];
const [x1, z1] = points[i + 1];
const steps = Math.max(1, Math.ceil(Math.hypot(x1 - x0, z1 - z0) * 3));
for (let s = 0; s <= steps; s++) {
const t = s / steps;
const x = Math.round(THREE.MathUtils.lerp(x0, x1, t));
const z = Math.round(THREE.MathUtils.lerp(z0, z1, t));
cells.add(key(x, z));
}
}
return cells;
}
const upperRiver = rasterize([
[4, -4], [4, -3], [5, -2], [5, -1], [6, 0], [6, 1]
]);
const lakeOutlet = rasterize([
[6, 1], [5, 2], [4, 2], [3, 2], [2, 2],
[1, 2], [0, 2], [-1, 2], [-2, 2], [-3, 2]
]);
upperRiver.forEach((k) => waterCells.add(k));
lakeOutlet.forEach((k) => waterCells.add(k));
for (let x = 4; x <= 8; x++) {
for (let z = 0; z <= 3; z++) {
const ellipse =
((x - 6) * (x - 6)) / (2.15 * 2.15) +
((z - 1.5) * (z - 1.5)) / (1.55 * 1.55);
if (ellipse <= 1) waterCells.add(key(x, z));
}
}
const grassColors = [0x79a85d, 0x83b362, 0x6e9f56, 0x91bb6b];
const rockColors = [0x8e9287, 0x9b9a8b, 0x7e897e];
for (const [cellKey, height] of terrainHeight) {
if (waterCells.has(cellKey)) continue;
const [x, z] = cellKey.split(",").map(Number);
for (let level = 0; level < height; level++) {
const isTop = level === height - 1;
let color;
if (!isTop) {
color = level === 0 ? 0x806d4d : 0x927b58;
} else if (height <= 2) {
color = 0xc8b783;
} else if (height >= 6) {
color = rockColors[(x + z + level) % rockColors.length];
} else {
color = grassColors[(x * 3 + z * 5 + level) % grassColors.length];
}
addVoxel(
x,
0.5 + level,
z,
0.98,
0.98,
0.98,
colorVariant(color, (hash2(x + level, z) - 0.5) * 0.08)
);
}
}
// River and lake edge tiles are a slightly brighter, thinner water layer.
for (const cellKey of waterCells) {
const [x, z] = cellKey.split(",").map(Number);
const h = terrainHeight.get(cellKey) ?? 2;
// A one-cell-high bank is retained at the edge of the larger lake.
if (h >= 2 && !lakeOutlet.has(cellKey)) {
addVoxel(x, 0.5, z, 0.98, 0.98, 0.98, 0x8a7656);
}
}
// Forest trees, kept away from the water channels.
for (const [cellKey, height] of terrainHeight) {
if (height < 4 || waterCells.has(cellKey)) continue;
const [x, z] = cellKey.split(",").map(Number);
const chance = hash2(x * 2.1, z * 3.7);
if (chance > 0.34 || height >= 7) continue;
const trunk = hash2(x + 9, z) > 0.5 ? 0x70543d : 0x5d4936;
const leaf = hash2(x - 4, z + 8) > 0.5 ? 0x326d4d : 0x3b7b53;
addVoxel(x, height + 0.5, z, 0.22, 1.0, 0.22, trunk);
addVoxel(x, height + 1.12, z, 1.02, 0.78, 1.02, leaf);
addVoxel(x, height + 1.72, z, 0.72, 0.64, 0.72, leaf);
addVoxel(x, height + 2.12, z, 0.38, 0.36, 0.38, 0x4d8d5e);
}
// Small exposed rocks around the summit.
for (let i = 0; i < 18; i++) {
const x = 2 + Math.floor(hash2(i, 4) * 5);
const z = -5 + Math.floor(hash2(i, 11) * 4);
const h = terrainHeight.get(key(x, z));
if (!h || h < 5 || waterCells.has(key(x, z))) continue;
const s = 0.25 + hash2(i, 21) * 0.32;
addVoxel(
x + 0.12,
h + s * 0.5,
z - 0.08,
s,
s * 0.75,
s,
hash2(i, 8) > 0.5 ? 0x8d9388 : 0x777f76
);
}
// ------------------------------------------------------------
// Groundwater cutaway
// ------------------------------------------------------------
for (let x = -1; x <= 10; x++) {
const frontRadius =
Math.pow((x - 4.1) / 6.8, 2) +
Math.pow((5.1 + 0.1) / 5.5, 2);
// Bedrock.
addVoxel(
x, -2.33, 5.57,
0.98, 0.66, 0.66,
colorVariant(0x5d6b6c, (hash2(x, 2) - 0.5) * 0.08)
);
// Saturated aquifer.
addVoxel(
x, -1.55, 5.62,
0.98, 0.55, 0.68,
colorVariant(0x38a9bb, (hash2(x, 5) - 0.5) * 0.11)
);
// Unsaturated soil and the water table interface.
if (frontRadius < 1.08) {
addVoxel(
x, -0.78, 5.64,
0.98, 0.52, 0.68,
colorVariant(0x9a805c, (hash2(x, 9) - 0.5) * 0.08)
);
}
}
// Dark rails make the exposed subsurface read as a deliberate cutaway.
const frameMaterial = new THREE.MeshStandardMaterial({
color: 0x35545a,
roughness: 0.72
});
function addFrameBlock(x, y, z, sx, sy, sz) {
const mesh = new THREE.Mesh(cubeGeometry, frameMaterial);
mesh.position.set(x, y, z);
mesh.scale.set(sx, sy, sz);
mesh.castShadow = true;
scene.add(mesh);
}
addFrameBlock(-1.5, -1.25, 5.96, 0.12, 3.0, 0.12);
addFrameBlock(10.5, -1.25, 5.96, 0.12, 3.0, 0.12);
addFrameBlock(4.5, -2.73, 5.96, 12.0, 0.12, 0.12);
// ------------------------------------------------------------
// Ocean glints
// ------------------------------------------------------------
const glintMaterial = new THREE.MeshBasicMaterial({
color: 0xd8ffff,
transparent: true,
opacity: 0.42,
depthWrite: false
});
const glintCount = 68;
const glints = new THREE.InstancedMesh(cubeGeometry, glintMaterial, glintCount);
for (let i = 0; i < glintCount; i++) {
const x = -11.3 + hash2(i, 3) * 8.2;
const z = -7.1 + hash2(i, 17) * 14.2;
const sx = 0.22 + hash2(i, 5) * 0.52;
const sz = 0.055;
dummy.position.set(x, 1.315, z);
dummy.rotation.set(0, hash2(i, 22) > 0.5 ? 0 : Math.PI / 2, 0);
dummy.scale.set(sx, 0.035, sz);
dummy.updateMatrix();
glints.setMatrixAt(i, dummy.matrix);
}
glints.instanceMatrix.needsUpdate = true;
scene.add(glints);
// Build all static terrain voxels in one instanced draw.
const terrainMesh = new THREE.InstancedMesh(
cubeGeometry,
voxelMaterial,
voxelTransforms.length
);
voxelTransforms.forEach((voxel, index) => {
dummy.position.set(voxel.x, voxel.y, voxel.z);
dummy.rotation.set(0, 0, 0);
dummy.scale.set(voxel.sx, voxel.sy, voxel.sz);
dummy.updateMatrix();
terrainMesh.setMatrixAt(index, dummy.matrix);
terrainMesh.setColorAt(index, voxel.color);
});
terrainMesh.instanceMatrix.needsUpdate = true;
terrainMesh.instanceColor.needsUpdate = true;
terrainMesh.castShadow = true;
terrainMesh.receiveShadow = true;
scene.add(terrainMesh);
// ------------------------------------------------------------
// Voxel clouds
// ------------------------------------------------------------
const cloudMaterial = new THREE.MeshStandardMaterial({
color: 0xf7fbf8,
roughness: 1,
transparent: true,
opacity: 0.92
});
const cloudShadeMaterial = new THREE.MeshStandardMaterial({
color: 0xdce9e7,
roughness: 1,
transparent: true,
opacity: 0.9
});
const clouds = [];
function createCloud(x, y, z, scale = 1) {
const group = new THREE.Group();
group.position.set(x, y, z);
group.userData.baseX = x;
const puffs = [
[-1.05, 0.00, 0.05, 1.28, 0.72, 0.95, cloudShadeMaterial],
[-0.25, 0.22, 0.00, 1.45, 1.00, 1.05, cloudMaterial],
[0.72, 0.05, 0.08, 1.28, 0.80, 0.92, cloudMaterial],
[0.10, -0.22, 0.52, 1.60, 0.48, 0.68, cloudShadeMaterial],
[1.22, -0.16, -0.18, 0.70, 0.50, 0.62, cloudShadeMaterial]
];
for (const [px, py, pz, sx, sy, sz, material] of puffs) {
const puff = new THREE.Mesh(cubeGeometry, material);
puff.position.set(px * scale, py * scale, pz * scale);
puff.scale.set(sx * scale, sy * scale, sz * scale);
puff.castShadow = true;
group.add(puff);
}
scene.add(group);
clouds.push(group);
return group;
}
createCloud(-6.0, 9.7, 0.0, 1.05);
createCloud(3.2, 10.1, -3.0, 1.12);
createCloud(7.0, 9.25, 0.55, 0.92);
// ------------------------------------------------------------
// Ambient voxel rain, separate from the labeled tracer packets
// ------------------------------------------------------------
const rainMaterial = new THREE.MeshBasicMaterial({
color: 0x7fd4f4,
transparent: true,
opacity: 0.55,
depthWrite: false
});
const rainData = [];
const cloudBounds = [
{ x: -6, y: 9.7, z: 0, rx: 2.0, rz: 1.2 },
{ x: 3.2, y: 10.1, z: -3, rx: 2.1, rz: 1.25 },
{ x: 7, y: 9.25, z: 0.55, rx: 1.7, rz: 1.05 }
];
for (let i = 0; i < 54; i++) {
const cloud = cloudBounds[i % cloudBounds.length];
const x = cloud.x + (hash2(i, 2) - 0.5) * cloud.rx * 2;
const z = cloud.z + (hash2(i, 7) - 0.5) * cloud.rz * 2;
const landHeight = terrainHeight.get(key(Math.round(x), Math.round(z)));
const bottom = waterCells.has(key(Math.round(x), Math.round(z)))
? 1.3
: Math.max(1.3, landHeight ?? 1.3);
rainData.push({
x,
z,
top: cloud.y - 0.45,
bottom,
speed: 0.13 + hash2(i, 11) * 0.13,
offset: hash2(i, 19)
});
}
const rainMesh = new THREE.InstancedMesh(
cubeGeometry,
rainMaterial,
rainData.length
);
rainMesh.instanceMatrix.setUsage(THREE.DynamicDrawUsage);
scene.add(rainMesh);
// ------------------------------------------------------------
// Labeled tracer routes
// ------------------------------------------------------------
const colors = {
evaporation: 0x42dac9,
precipitation: 0x63bdf5,
transpiration: 0x96db70,
infiltration: 0xf5bd4f,
groundwater: 0xad83ee,
runoff: 0xe9dc62
};
const routes = [];
const labelItems = [];
function addRoute({
points,
color,
count = 3,
speed = 0.085,
dashSize = 0.22,
gapSize = 0.14,
opacity = 0.42
}) {
const vectors = points.map(
(p) => new THREE.Vector3(p[0], p[1], p[2])
);
const curve = new THREE.CatmullRomCurve3(
vectors,
false,
"catmullrom",
0.18
);
const geometry = new THREE.BufferGeometry().setFromPoints(
curve.getPoints(72)
);
const lineMaterial = new THREE.LineDashedMaterial({
color,
transparent: true,
opacity,
dashSize,
gapSize,
depthWrite: false
});
const line = new THREE.Line(geometry, lineMaterial);
line.computeLineDistances();
line.renderOrder = 2;
scene.add(line);
const material = new THREE.MeshBasicMaterial({ color });
for (let i = 0; i < count; i++) {
const packet = new THREE.Mesh(cubeGeometry, material);
packet.scale.setScalar(0.16 + (i % 2) * 0.025);
packet.renderOrder = 3;
scene.add(packet);
routes.push({
curve,
packet,
speed: speed * (0.82 + hash2(i, count) * 0.42),
phase: hash2(i * 4.1, count * 2.3),
size: 0.16 + (i % 2) * 0.025
});
}
}
// Evaporation: ocean and lake to atmospheric storage.
addRoute({
points: [
[-7.7, 1.35, 1.0],
[-8.0, 3.5, 0.8],
[-7.4, 6.6, 0.3],
[-6.2, 9.5, 0.0]
],
color: colors.evaporation,
count: 4,
speed: 0.09
});
addRoute({
points: [
[6.2, 1.35, 1.5],
[7.1, 3.7, 1.3],
[7.6, 6.5, 0.9],
[7.0, 9.1, 0.55]
],
color: colors.evaporation,
count: 3,
speed: 0.075
});
// Precipitation: cloud to ocean, upland, and lake.
addRoute({
points: [
[-6.2, 9.5, 0.0],
[-6.5, 7.0, 0.5],
[-7.3, 4.0, 0.9],
[-7.7, 1.35, 1.0]
],
color: colors.precipitation,
count: 4,
speed: 0.12
});
addRoute({
points: [
[3.1, 9.7, -3.0],
[3.0, 8.0, -2.8],
[3.2, 6.6, -3.0]
],
color: colors.precipitation,
count: 3,
speed: 0.14
});
addRoute({
points: [
[7.0, 9.1, 0.55],
[6.7, 6.4, 0.8],
[6.2, 3.5, 1.2],
[6.2, 1.35, 1.5]
],
color: colors.precipitation,
count: 3,
speed: 0.105
});
// Transpiration: forest canopy to cloud.
addRoute({
points: [
[3.6, 5.9, -3.4],
[3.7, 7.3, -3.3],
[3.4, 8.8, -3.0],
[3.1, 9.7, -3.0]
],
color: colors.transpiration,
count: 3,
speed: 0.08
});
// Infiltration: soil moisture into the saturated aquifer.
addRoute({
points: [
[1.0, 2.0, 4.0],
[1.0, 0.7, 4.7],
[1.0, -1.4, 5.7]
],
color: colors.infiltration,
count: 3,
speed: 0.085
});
addRoute({
points: [
[7.0, 2.0, 4.8],
[7.1, 0.3, 5.2],
[7.1, -1.4, 5.7]
],
color: colors.infiltration,
count: 2,
speed: 0.075
});
// Groundwater can discharge toward either lake or ocean.
addRoute({
points: [
[7.1, -1.45, 5.78],
[7.0, -0.7, 4.4],
[6.4, -0.1, 2.7],
[6.2, 1.35, 1.5]
],
color: colors.groundwater,
count: 4,
speed: 0.07
});
addRoute({
points: [
[1.0, -1.42, 5.78],
[-0.7, -1.35, 5.6],
[-2.3, -1.05, 4.1],
[-3.3, 0.4, 2.8],
[-4.0, 1.35, 2.2]
],
color: colors.groundwater,
count: 4,
speed: 0.068
});
// Baseflow from the water table into the river.
addRoute({
points: [
[0.5, -0.85, 5.8],
[0.8, 0.2, 4.4],
[1.2, 1.35, 2.6]
],
color: colors.groundwater,
count: 2,
speed: 0.065
});
// Surface runoff: summit to river, then lake outlet to ocean.
addRoute({
points: [
[4.0, 6.95, -4.0],
[4.0, 5.0, -3.0],
[5.0, 3.1, -2.0],
[5.1, 1.9, -1.0],
[6.1, 1.35, 0.2],
[6.2, 1.35, 1.5]
],
color: colors.runoff,
count: 4,
speed: 0.095
});
addRoute({
points: [
[6.2, 1.35, 1.5],
[5.0, 1.35, 2.0],
[3.0, 1.35, 2.0],
[1.0, 1.35, 2.0],
[-1.0, 1.35, 2.0],
[-3.0, 1.35, 2.0]
],
color: colors.runoff,
count: 5,
speed: 0.11
});
// ------------------------------------------------------------
// HTML labels projected from 3D anchors
// ------------------------------------------------------------
function addLabel(
text,
anchor,
{
kind = "reservoir",
color = "#2b5961",
offset = [0, 0],
subtle = false
} = {}
) {
const element = document.createElement("div");
element.className = `label ${kind}${subtle ? " subtle" : ""}`;
element.textContent = text;
element.style.setProperty("--c", color);
labelsHost.appendChild(element);
labelItems.push({
element,
anchor: new THREE.Vector3(anchor[0], anchor[1], anchor[2]),
offset
});
}
addLabel("Ocean", [-8.0, 1.4, 2.6], { color: "#35aebe" });
addLabel("Upland forest", [4.0, 6.2, -3.5], { color: "#397651" });
addLabel("River", [2.4, 1.55, 2.0], { color: "#4e9fb6" });
addLabel("Lake", [6.3, 1.5, 1.5], { color: "#3fa6bd" });
addLabel("Cloud / atmosphere", [-6.2, 10.45, 0.0], { color: "#8ca6aa" });
addLabel("Soil moisture", [0.0, -0.45, 5.95], { color: "#a17f56" });
addLabel("Groundwater aquifer", [7.2, -1.45, 5.95], { color: "#48aabd" });
addLabel("Bedrock", [5.0, -2.3, 5.95], { color: "#657174" });
addLabel(
"Evaporation · ocean → air",
[-7.8, 5.4, 0.7],
{ kind: "process", color: "#42dac9", offset: [-42, 0] }
);
addLabel(
"Precipitation · air → land",
[2.3, 7.9, -2.7],
{ kind: "process", color: "#63bdf5", offset: [30, -15] }
);
addLabel(
"Transpiration · forest → air",
[3.3, 8.1, -3.3],
{ kind: "process", color: "#96db70", offset: [12, -13] }
);
addLabel(
"Infiltration · soil → groundwater",
[0.8, 0.1, 5.8],
{ kind: "process", color: "#f5bd4f", offset: [-28, -18] }
);
addLabel(
"Groundwater flow · aquifer → lake / ocean",
[5.6, -0.35, 6.0],
{
kind: "process",
color: "#ad83ee",
offset: [4, -20],
subtle: true
}
);
addLabel(
"Surface runoff · river / lake → ocean",
[1.0, 2.2, 2.1],
{ kind: "process", color: "#b6a92f", offset: [-8, 25] }
);
const projected = new THREE.Vector3();
function updateLabels() {
const width = window.innerWidth;
const height = window.innerHeight;
for (const item of labelItems) {
projected.copy(item.anchor).project(camera);
const visible =
projected.z > -1 &&
projected.z < 1 &&
projected.x > -1.25 &&
projected.x < 1.25 &&
projected.y > -1.25 &&
projected.y < 1.25;
item.element.style.display = visible ? "block" : "none";
if (!visible) continue;
const x = (projected.x * 0.5 + 0.5) * width + item.offset[0];
const y = (-projected.y * 0.5 + 0.5) * height + item.offset[1];
item.element.style.left = `${x}px`;
item.element.style.top = `${y}px`;
}
}
// ------------------------------------------------------------
// Camera controls, implemented without addons
// ------------------------------------------------------------
const canvas = renderer.domElement;
const activePointers = new Map();
let pointerMode = "rotate";
let previousPinchDistance = 0;
function resetCamera() {
cameraState.yaw = defaultCamera.yaw;
cameraState.pitch = defaultCamera.pitch;
cameraState.distance = defaultCamera.distance;
cameraState.target.copy(defaultCamera.target);
updateCamera();
}
canvas.addEventListener("contextmenu", (event) => event.preventDefault());
canvas.addEventListener("pointerdown", (event) => {
canvas.setPointerCapture(event.pointerId);
activePointers.set(event.pointerId, {
x: event.clientX,
y: event.clientY
});
pointerMode = event.button === 2 ? "pan" : "rotate";
if (activePointers.size === 2) {
const points = [...activePointers.values()];
previousPinchDistance = Math.hypot(
points[0].x - points[1].x,
points[0].y - points[1].y
);
}
});
canvas.addEventListener("pointermove", (event) => {
if (!activePointers.has(event.pointerId)) return;
const previous = activePointers.get(event.pointerId);
const dx = event.clientX - previous.x;
const dy = event.clientY - previous.y;
activePointers.set(event.pointerId, {
x: event.clientX,
y: event.clientY
});
if (activePointers.size === 2) {
const points = [...activePointers.values()];
const distance = Math.hypot(
points[0].x - points[1].x,
points[0].y - points[1].y
);
if (previousPinchDistance > 0) {
cameraState.distance = THREE.MathUtils.clamp(
cameraState.distance * (previousPinchDistance / distance),
16,
44
);
updateCamera();
}
previousPinchDistance = distance;
return;
}
if (pointerMode === "rotate") {
cameraState.yaw -= dx * 0.006;
cameraState.pitch = THREE.MathUtils.clamp(
cameraState.pitch + dy * 0.005,
0.2,
1.18
);
} else {
const right = new THREE.Vector3();
camera.matrixWorld.extractBasis(right, new THREE.Vector3(), new THREE.Vector3());
right.y = 0;
right.normalize();
cameraState.target.addScaledVector(
right,
-dx * cameraState.distance * 0.0016
);
cameraState.target.y = THREE.MathUtils.clamp(
cameraState.target.y + dy * cameraState.distance * 0.0011,
0.4,
6
);
cameraState.target.x = THREE.MathUtils.clamp(
cameraState.target.x,
-7,
7
);
cameraState.target.z = THREE.MathUtils.clamp(
cameraState.target.z,
-6,
6
);
}
updateCamera();
});
function endPointer(event) {
activePointers.delete(event.pointerId);
if (activePointers.size < 2) previousPinchDistance = 0;
}
canvas.addEventListener("pointerup", endPointer);
canvas.addEventListener("pointercancel", endPointer);
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraState.distance = THREE.MathUtils.clamp(
cameraState.distance + event.deltaY * 0.018,
16,
44
);
updateCamera();
},
{ passive: false }
);
canvas.addEventListener("dblclick", resetCamera);
document.getElementById("reset-view").addEventListener("click", resetCamera);
let paused = false;
const pauseButton = document.getElementById("pause");
pauseButton.addEventListener("click", () => {
paused = !paused;
pauseButton.textContent = paused ? "Resume tracers" : "Pause tracers";
});
const labelToggle = document.getElementById("toggle-labels");
let labelsVisible = true;
labelToggle.addEventListener("click", () => {
labelsVisible = !labelsVisible;
labelsHost.classList.toggle("hidden", !labelsVisible);
labelToggle.textContent = labelsVisible ? "Hide labels" : "Show labels";
});
// ------------------------------------------------------------
// Animation
// ------------------------------------------------------------
const clock = new THREE.Clock();
let tracerTime = 0;
let firstFrame = true;
function animate() {
const delta = Math.min(clock.getDelta(), 0.05);
const elapsed = clock.elapsedTime;
if (!paused) tracerTime += delta;
for (const route of routes) {
const t = (route.phase + tracerTime * route.speed) % 1;
route.curve.getPointAt(t, route.packet.position);
const pulse =
route.size *
(0.92 + 0.12 * Math.sin(elapsed * 5.2 + route.phase * 13));
route.packet.scale.setScalar(pulse);
route.packet.rotation.y = elapsed * 0.65 + route.phase * 4;
route.packet.rotation.x = elapsed * 0.4;
}
clouds.forEach((cloud, index) => {
cloud.position.x =
cloud.userData.baseX + Math.sin(elapsed * 0.16 + index * 1.7) * 0.24;
cloud.position.y +=
Math.sin(elapsed * 0.35 + index) * 0.00045;
});
rainData.forEach((drop, index) => {
const progress = (drop.offset + elapsed * drop.speed) % 1;
const y = drop.top - progress * (drop.top - drop.bottom);
dummy.position.set(drop.x, y, drop.z);
dummy.rotation.set(0, 0, 0);
dummy.scale.set(0.055, 0.24 + hash2(index, 3) * 0.12, 0.055);
dummy.updateMatrix();
rainMesh.setMatrixAt(index, dummy.matrix);
});
rainMesh.instanceMatrix.needsUpdate = true;
glintMaterial.opacity = 0.32 + Math.sin(elapsed * 0.8) * 0.08;
updateLabels();
renderer.render(scene, camera);
if (firstFrame) {
firstFrame = false;
loading.classList.add("done");
setTimeout(() => loading.remove(), 450);
}
}
renderer.setAnimationLoop(animate);
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
});
</script>
</body>
</html>
```