Prompt specification
Build or customize the Three.js 3D scene "Reservoir and its upstream catchment" 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: Reservoir and its upstream catchment
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/reservoir-and-its-upstream-catchment-x1195
Scene Description Prompt:
Build a self-contained HTML scene with Three.js from cdnjs featuring watershed divides. Compose tributaries joining a main river above a dam, with hillside farms and forest patches; show all marked upstream branches connecting to the reservoir. Voxel style.
Working Three.js Starter (reservoir-and-its-upstream-catchment-x1195):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Voxel Watershed</title>
<style>
:root {
--ink: #17343b;
--paper: rgba(249, 244, 226, 0.92);
--line: rgba(23, 52, 59, 0.18);
--main: #f3c969;
--north: #ff8b6a;
--west: #7dd6a7;
--east: #70b7ff;
--lower: #d79cff;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #b8d8d2;
color: var(--ink);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
#viewport {
position: fixed;
inset: 0;
}
canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
}
.panel {
position: fixed;
z-index: 20;
border: 1px solid var(--line);
background: var(--paper);
box-shadow: 0 12px 40px rgba(33, 68, 72, 0.14);
backdrop-filter: blur(12px);
}
#title-card {
top: 20px;
left: 20px;
width: min(390px, calc(100vw - 40px));
padding: 18px 20px 16px;
border-radius: 16px;
pointer-events: none;
}
.eyebrow {
margin-bottom: 8px;
color: #55747a;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.18em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: clamp(25px, 3vw, 38px);
line-height: 0.98;
letter-spacing: -0.045em;
}
.subtitle {
margin: 12px 0 0;
max-width: 330px;
color: #4e6d73;
font-size: 13px;
line-height: 1.45;
}
#legend {
left: 20px;
bottom: 20px;
width: min(350px, calc(100vw - 40px));
padding: 14px 16px;
border-radius: 14px;
pointer-events: none;
}
.legend-heading {
display: flex;
justify-content: space-between;
gap: 12px;
margin-bottom: 10px;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.14em;
text-transform: uppercase;
}
.legend-heading span:last-child {
color: #668087;
font-weight: 700;
letter-spacing: 0.04em;
text-transform: none;
}
.legend-row {
display: grid;
grid-template-columns: 12px 27px 1fr auto;
align-items: center;
gap: 8px;
min-height: 27px;
font-size: 12px;
}
.swatch {
width: 9px;
height: 9px;
border-radius: 2px;
box-shadow: 0 0 0 2px rgba(255, 255, 255, 0.65);
}
.route-number {
color: #668087;
font-size: 10px;
font-weight: 900;
letter-spacing: 0.08em;
}
.route-arrow {
color: #668087;
font-size: 10px;
letter-spacing: 0.08em;
}
#controls {
right: 20px;
bottom: 20px;
display: flex;
gap: 6px;
padding: 6px;
border-radius: 12px;
}
#controls button {
width: 38px;
height: 34px;
border: 0;
border-radius: 8px;
background: transparent;
color: var(--ink);
font-size: 19px;
font-weight: 700;
cursor: pointer;
}
#controls button:hover {
background: rgba(23, 52, 59, 0.09);
}
#hint {
position: fixed;
z-index: 12;
right: 20px;
top: 20px;
padding: 9px 12px;
border-radius: 999px;
background: rgba(249, 244, 226, 0.78);
color: #4f6d73;
font-size: 11px;
pointer-events: none;
backdrop-filter: blur(8px);
}
#markers {
position: fixed;
inset: 0;
z-index: 8;
overflow: hidden;
pointer-events: none;
}
.map-label {
position: absolute;
display: flex;
align-items: center;
gap: 7px;
padding: 5px 9px 5px 6px;
border: 1px solid rgba(23, 52, 59, 0.2);
border-radius: 7px;
background: rgba(252, 248, 234, 0.93);
box-shadow: 0 5px 15px rgba(23, 52, 59, 0.14);
color: var(--ink);
font-size: 10px;
font-weight: 850;
letter-spacing: 0.075em;
white-space: nowrap;
transform: translate(-50%, -115%);
}
.map-label::after {
content: "";
position: absolute;
left: 50%;
top: 100%;
width: 1px;
height: 9px;
background: rgba(23, 52, 59, 0.45);
}
.label-number {
display: grid;
place-items: center;
min-width: 23px;
height: 20px;
padding: 0 4px;
border-radius: 4px;
color: #17343b;
font-size: 9px;
font-weight: 950;
}
.place-label {
padding: 5px 9px;
border-radius: 999px;
background: rgba(23, 52, 59, 0.86);
border-color: transparent;
color: #fff9e8;
font-size: 9px;
}
.place-label::after {
display: none;
}
@media (max-width: 720px) {
#title-card {
top: 12px;
left: 12px;
width: calc(100vw - 24px);
padding: 14px 16px;
}
.subtitle {
display: none;
}
#legend {
left: 12px;
bottom: 12px;
width: calc(100vw - 88px);
padding: 10px 12px;
}
.legend-row {
min-height: 23px;
font-size: 10px;
}
.legend-heading span:last-child,
.route-arrow {
display: none;
}
#hint {
display: none;
}
#controls {
right: 12px;
bottom: 12px;
}
}
</style>
</head>
<body>
<div id="viewport"></div>
<section id="title-card" class="panel">
<div class="eyebrow">Connected drainage study</div>
<h1>Voxel<br>Watershed</h1>
<p class="subtitle">
Four marked tributaries join the main stem, feed the reservoir,
and pass the downstream dam.
</p>
</section>
<section id="legend" class="panel">
<div class="legend-heading">
<span>Marked channels</span>
<span>Pale stone = watershed divide</span>
</div>
<div class="legend-row">
<i class="swatch" style="background:var(--main)"></i>
<span class="route-number">M</span>
<span>MAIN STEM</span>
<span class="route-arrow">→ RESERVOIR</span>
</div>
<div class="legend-row">
<i class="swatch" style="background:var(--north)"></i>
<span class="route-number">01</span>
<span>NORTH FORK</span>
<span class="route-arrow">→ MAIN</span>
</div>
<div class="legend-row">
<i class="swatch" style="background:var--west"></i>
<span class="route-number">02</span>
<span>WEST SPUR</span>
<span class="route-arrow">→ MAIN</span>
</div>
<div class="legend-row">
<i class="swatch" style="background:var(--east)"></i>
<span class="route-number">03</span>
<span>EAST FORK</span>
<span class="route-arrow">→ MAIN</span>
</div>
<div class="legend-row">
<i class="swatch" style="background:var(--lower)"></i>
<span class="route-number">04</span>
<span>LOWER SPUR</span>
<span class="route-arrow">→ MAIN</span>
</div>
</section>
<div id="hint">Drag to orbit · Shift/right-drag to pan · Scroll to zoom</div>
<nav id="controls" class="panel" aria-label="Camera controls">
<button id="zoom-out" title="Zoom out">−</button>
<button id="reset" title="Reset camera">↺</button>
<button id="zoom-in" title="Zoom in">+</button>
</nav>
<div id="markers"></div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// -------------------------------------------------------------------------
// Renderer, scene, lighting
// -------------------------------------------------------------------------
const viewport = document.getElementById("viewport");
const renderer = new THREE.WebGLRenderer({ antialias: true });
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;
viewport.appendChild(renderer.domElement);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xb8d8d2);
scene.fog = new THREE.Fog(0xb8d8d2, 68, 125);
const camera = new THREE.PerspectiveCamera(
39,
window.innerWidth / window.innerHeight,
0.1,
180
);
const hemisphere = new THREE.HemisphereLight(0xe6fbff, 0x706448, 2.15);
scene.add(hemisphere);
const sun = new THREE.DirectionalLight(0xfff2ce, 3.0);
sun.position.set(-24, 42, -18);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -36;
sun.shadow.camera.right = 36;
sun.shadow.camera.top = 36;
sun.shadow.camera.bottom = -36;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 100;
sun.shadow.normalBias = 0.025;
scene.add(sun);
const fill = new THREE.DirectionalLight(0xaedfff, 0.65);
fill.position.set(26, 18, 24);
scene.add(fill);
const cubeGeometry = new THREE.BoxGeometry(1, 1, 1);
const dummy = new THREE.Object3D();
// -------------------------------------------------------------------------
// Deterministic terrain
// -------------------------------------------------------------------------
const X_MIN = -24;
const X_MAX = 23;
const Z_MIN = -24;
const Z_MAX = 23;
const farms = [
{ x0: -17, x1: -9, z0: 1, z1: 6, base: 4.25, slope: 0.27 },
{ x0: 10, x1: 15, z0: -8, z1: -5, base: 4.15, slope: 0.24 },
{ x0: 1, x1: 7, z0: -23, z1: -18, base: 6.65, slope: 0.19 }
];
const divides = [
[[-22, -23], [-18, -21], [-14, -19], [-11, -17], [-8, -15]],
[[-21, -9], [-16, -8], [-11, -8], [-7, -7], [-2, -6]],
[[10, -23], [10, -18], [10, -13], [8, -9], [7, -7]],
[[20, -15], [19, -10], [18, -5], [15, 0], [12, 3]],
[[-18, 9], [-10, 8], [-3, 7], [0, 6]],
[[10, 6], [16, 7], [20, 10]]
];
const forestPatches = [
{ x: -15, z: -9, rx: 5.3, rz: 6.2 },
{ x: 14, z: -14, rx: 5.4, rz: 5.4 },
{ x: -13, z: 5, rx: 5.2, rz: 3.1 },
{ x: 18, z: 2, rx: 4.1, rz: 5.3 }
];
function hash(x, z, salt = 0) {
const n = Math.sin(x * 127.1 + z * 311.7 + salt * 74.7) * 43758.5453;
return n - Math.floor(n);
}
function clamp(value, min, max) {
return Math.max(min, Math.min(max, value));
}
function distanceToSegment(px, pz, ax, az, bx, bz) {
const dx = bx - ax;
const dz = bz - az;
const lengthSquared = dx * dx + dz * dz;
if (lengthSquared === 0) {
return Math.hypot(px - ax, pz - az);
}
const t = clamp(
((px - ax) * dx + (pz - az) * dz) / lengthSquared,
0,
1
);
return Math.hypot(px - (ax + dx * t), pz - (az + dz * t));
}
function distanceToPolyline(x, z, points) {
let distance = Infinity;
for (let i = 0; i < points.length - 1; i++) {
distance = Math.min(
distance,
distanceToSegment(
x,
z,
points[i][0],
points[i][1],
points[i + 1][0],
points[i + 1][1]
)
);
}
return distance;
}
function farmAt(x, z) {
return farms.find(
(farm) =>
x >= farm.x0 &&
x <= farm.x1 &&
z >= farm.z0 &&
z <= farm.z1
);
}
function forestAt(x, z) {
return forestPatches.some(
(patch) =>
((x - patch.x) / patch.rx) ** 2 +
((z - patch.z) / patch.rz) ** 2 <
1
);
}
function terrainHeight(x, z) {
let height = 3.8 + Math.max(0, 5 - z) * 0.07;
const hills = [
[-14, -10, 3.2, 4.8, 6.0],
[14, -14, 3.7, 4.8, 5.8],
[-14, 4, 2.2, 4.4, 4.2],
[17, 2, 2.7, 4.0, 5.0],
[-20, -3, 1.7, 3.8, 5.0]
];
for (const [cx, cz, amplitude, rx, rz] of hills) {
const d =
((x - cx) / rx) ** 2 + ((z - cz) / rz) ** 2;
height += amplitude * Math.exp(-d * 0.5);
}
let ridgeDistance = Infinity;
for (const divide of divides) {
ridgeDistance = Math.min(
ridgeDistance,
distanceToPolyline(x, z, divide)
);
}
height += 2.05 * Math.exp(-(ridgeDistance * ridgeDistance) / 2.8);
height += (hash(x, z, 4) - 0.5) * 0.42;
const farm = farmAt(x, z);
if (farm) {
height = farm.base + (z - farm.z0) * farm.slope;
}
return Math.round(height * 4) / 4;
}
// -------------------------------------------------------------------------
// Hydrology: every marked tributary ends at a node on the main stem.
// -------------------------------------------------------------------------
const routeDefinitions = [
{
name: "Main stem",
main: true,
points: [
[-8, -22], [-8, -18], [-5, -18], [-5, -14],
[-2, -14], [-2, -10], [1, -10], [1, -6],
[4, -6], [4, -2], [7, -2], [7, 6]
]
},
{
name: "North fork",
startLevel: 6.0,
endLevel: mainLevel(-14),
points: [
[-17, -23], [-17, -19], [-10, -19], [-10, -16],
[-5, -16], [-5, -14]
]
},
{
name: "West spur",
startLevel: 5.7,
endLevel: mainLevel(-10),
points: [
[-21, -17], [-17, -17], [-17, -14], [-13, -14],
[-13, -12], [-9, -12], [-9, -10], [-2, -10]
]
},
{
name: "East fork",
startLevel: 5.8,
endLevel: mainLevel(-6),
points: [
[17, -20], [17, -16], [13, -16], [13, -13],
[10, -13], [10, -9], [7, -9], [7, -6], [4, -6]
]
},
{
name: "Lower spur",
startLevel: 4.8,
endLevel: mainLevel(-2),
points: [
[20, -4], [16, -4], [16, -1], [12, -1],
[12, 1], [9, 1], [9, 2], [7, 2], [7, -2]
]
}
];
function mainLevel(z) {
return 3.6 + Math.max(0, 6 - z) * 0.09;
}
function buildRoute(definition) {
const points = definition.points;
const cumulative = [0];
for (let i = 1; i < points.length; i++) {
cumulative.push(
cumulative[i - 1] +
Math.hypot(
points[i][0] - points[i - 1][0],
points[i][1] - points[i - 1][1]
)
);
}
const total = cumulative[cumulative.length - 1];
const levels = points.map((point, index) => {
if (definition.main) {
return mainLevel(point[1]);
}
return THREE.MathUtils.lerp(
definition.startLevel,
definition.endLevel,
cumulative[index] / total
);
});
const cells = [];
for (let segment = 0; segment < points.length - 1; segment++) {
const a = points[segment];
const b = points[segment + 1];
const steps = Math.max(
Math.abs(b[0] - a[0]),
Math.abs(b[1] - a[1])
);
for (let step = 1; step <= steps; step++) {
const t = step / steps;
cells.push({
x: THREE.MathUtils.lerp(a[0], b[0], t),
z: THREE.MathUtils.lerp(a[1], b[1], t),
level: THREE.MathUtils.lerp(levels[segment], levels[segment + 1], t)
});
}
}
return {
...definition,
points,
levels,
cumulative,
total,
cells
};
}
const routes = routeDefinitions.map(buildRoute);
const riverCells = new Map();
for (const route of routes) {
for (const cell of route.cells) {
riverCells.set(`${cell.x},${cell.z}`, cell);
}
}
const reservoirCells = new Map();
for (let x = 3; x <= 15; x++) {
for (let z = 7; z <= 16; z++) {
reservoirCells.set(`${x},${z}`, { x, z, level: 3.6 });
}
}
const downstreamCells = new Map();
for (let z = 18; z <= 22; z++) {
const t = (z - 18) / 4;
const level = THREE.MathUtils.lerp(3.35, 2.3, t);
for (let x = 6; x <= 8; x++) {
downstreamCells.set(`${x},${z}`, { x, z, level });
}
}
function isWaterCell(x, z) {
return (
riverCells.has(`${x},${z}`) ||
reservoirCells.has(`${x},${z}`) ||
downstreamCells.has(`${x},${z}`)
);
}
const heights = new Map();
for (let z = Z_MIN; z <= Z_MAX; z++) {
for (let x = X_MIN; x <= X_MAX; x++) {
let height = terrainHeight(x, z);
const river = riverCells.get(`${x},${z}`);
if (river) {
height = Math.min(height, Math.max(0.25, river.level - 0.72));
}
if (reservoirCells.has(`${x},${z}`)) {
height = 2.88;
}
if (downstreamCells.has(`${x},${z}`)) {
const cell = downstreamCells.get(`${x},${z}`);
height = Math.min(height, cell.level - 0.65);
}
if (
!isWaterCell(x, z) &&
x >= 1 &&
x <= 17 &&
z >= 5 &&
z <= 18
) {
height = Math.max(height, 4.05);
}
heights.set(`${x},${z}`, height);
}
}
// -------------------------------------------------------------------------
// Instanced voxel helpers
// -------------------------------------------------------------------------
function makeInstances(items, material, castShadow = true) {
if (!items.length) return null;
const mesh = new THREE.InstancedMesh(cubeGeometry, material, items.length);
mesh.castShadow = castShadow;
mesh.receiveShadow = true;
items.forEach((item, index) => {
dummy.position.set(item.x, item.y, item.z);
dummy.rotation.set(0, 0, 0);
dummy.scale.set(item.sx, item.sy, item.sz);
dummy.updateMatrix();
mesh.setMatrixAt(index, dummy.matrix);
if (item.color !== undefined) {
mesh.setColorAt(index, new THREE.Color(item.color));
}
});
mesh.instanceMatrix.needsUpdate = true;
if (mesh.instanceColor) mesh.instanceColor.needsUpdate = true;
scene.add(mesh);
return mesh;
}
// Diorama base and soil strata.
const baseMaterial = new THREE.MeshStandardMaterial({
color: 0x695441,
roughness: 1
});
const base = new THREE.Mesh(
new THREE.BoxGeometry(49, 2.25, 49),
baseMaterial
);
base.position.y = -1.16;
base.receiveShadow = true;
scene.add(base);
makeInstances(
[
{
x: 0,
y: -0.33,
z: 0,
sx: 49.08,
sy: 0.22,
sz: 49.08,
color: 0x80684b
},
{
x: 0,
y: -0.72,
z: 0,
sx: 49.13,
sy: 0.2,
sz: 49.13,
color: 0x584839
}
],
new THREE.MeshStandardMaterial({ roughness: 1 }),
false
);
// Terrain tiles.
const groundItems = [];
for (let z = Z_MIN; z <= Z_MAX; z++) {
for (let x = X_MIN; x <= X_MAX; x++) {
const height = heights.get(`${x},${z}`);
const variation = (hash(x, z, 9) - 0.5) * 0.08;
let color = new THREE.Color(0x78a65d);
if (isWaterCell(x, z)) {
color = new THREE.Color(0xc8aa77);
} else if (farmAt(x, z)) {
color = new THREE.Color(
(x + z) % 3 === 0 ? 0x987247 : 0xaa8654
);
} else if (forestAt(x, z)) {
color = new THREE.Color(0x4f7e49);
} else {
const ridgeDistance = Math.min(
...divides.map((line) =>
distanceToPolyline(x, z, line)
)
);
if (ridgeDistance < 1.65) {
color = new THREE.Color(0xc8b98d);
} else if (height > 6.9) {
color = new THREE.Color(0x8e9880);
}
}
color.offsetHSL(0, 0, variation);
groundItems.push({
x,
y: height / 2,
z,
sx: 0.965,
sy: height,
sz: 0.965,
color: color.getHex()
});
}
}
makeInstances(
groundItems,
new THREE.MeshStandardMaterial({
roughness: 0.98,
metalness: 0
})
);
// Exposed watershed-divide stones.
const divideStones = [];
for (let z = Z_MIN; z <= Z_MAX; z++) {
for (let x = X_MIN; x <= X_MAX; x++) {
if (isWaterCell(x, z) || farmAt(x, z)) continue;
const distance = Math.min(
...divides.map((line) => distanceToPolyline(x, z, line))
);
if (distance < 1.3 && hash(x, z, 17) > 0.48) {
const height = heights.get(`${x},${z}`);
const size = 0.58 + hash(x, z, 18) * 0.25;
divideStones.push({
x,
y: height + 0.18,
z,
sx: size,
sy: 0.42 + hash(x, z, 19) * 0.34,
sz: size,
color: hash(x, z, 20) > 0.5 ? 0xd8ccaa : 0xb9ae8c
});
}
}
}
makeInstances(
divideStones,
new THREE.MeshStandardMaterial({
roughness: 0.95
})
);
// -------------------------------------------------------------------------
// Water surfaces and vertical voxel drops
// -------------------------------------------------------------------------
const waterItems = [];
for (const cell of [
...riverCells.values(),
...reservoirCells.values(),
...downstreamCells.values()
]) {
waterItems.push({
x: cell.x,
y: cell.level - 0.07,
z: cell.z,
sx: 1.025,
sy: 0.14,
sz: 1.025
});
}
for (const route of routes) {
for (let i = 1; i < route.cells.length; i++) {
const a = route.cells[i - 1];
const b = route.cells[i];
const difference = b.level - a.level;
if (Math.abs(difference) < 0.005) continue;
waterItems.push({
x: (a.x + b.x) / 2,
y: (a.level + b.level) / 2,
z: (a.z + b.z) / 2,
sx: a.x !== b.x ? 0.18 : 1.025,
sy: Math.abs(difference) + 0.16,
sz: a.z !== b.z ? 0.18 : 1.025
});
}
}
const waterMaterial = new THREE.MeshStandardMaterial({
color: 0x2b9fd0,
emissive: 0x073c55,
emissiveIntensity: 0.25,
roughness: 0.28,
metalness: 0.08
});
makeInstances(waterItems, waterMaterial, false);
// Reservoir glints.
const glints = [];
for (let z = 8; z <= 15; z++) {
for (let x = 4; x <= 14; x++) {
if (hash(x, z, 31) > 0.83) {
glints.push({
x,
y: 3.69,
z,
sx: 0.35,
sy: 0.04,
sz: 0.12,
color: 0x8fd9ec
});
}
}
}
makeInstances(
glints,
new THREE.MeshStandardMaterial({
color: 0x9ce1ef,
emissive: 0x327b91,
emissiveIntensity: 0.35,
roughness: 0.25
}),
false
);
// -------------------------------------------------------------------------
// Hillside farms
// -------------------------------------------------------------------------
const furrowItems = [];
const cropItems = [];
for (const farm of farms) {
for (let z = farm.z0; z <= farm.z1; z += 2) {
furrowItems.push({
x: (farm.x0 + farm.x1) / 2,
y: heights.get(`${Math.round((farm.x0 + farm.x1) / 2)},${z}`) + 0.035,
z,
sx: farm.x1 - farm.x0 + 0.55,
sy: 0.09,
sz: 0.45,
color: 0x755333
});
for (let x = farm.x0; x <= farm.x1; x++) {
if (isWaterCell(x, z)) continue;
cropItems.push({
x,
y: heights.get(`${x},${z}`) + 0.18,
z,
sx: 0.48,
sy: 0.25,
sz: 0.48,
color:
hash(x, z, 38) > 0.62
? 0xe2bd55
: hash(x, z, 39) > 0.45
? 0x8fbd50
: 0x5e9d46
});
}
}
}
makeInstances(
furrowItems,
new THREE.MeshStandardMaterial({ roughness: 1 })
);
makeInstances(
cropItems,
new THREE.MeshStandardMaterial({ roughness: 0.9 })
);
// -------------------------------------------------------------------------
// Forest patches
// -------------------------------------------------------------------------
const trunks = [];
const lowerCanopies = [];
const upperCanopies = [];
for (let z = Z_MIN; z <= Z_MAX; z++) {
for (let x = X_MIN; x <= X_MAX; x++) {
if (!forestAt(x, z) || farmAt(x, z) || isWaterCell(x, z)) {
continue;
}
if (hash(x, z, 45) < 0.56) continue;
const height = heights.get(`${x},${z}`);
const scale = 0.78 + hash(x, z, 46) * 0.35;
trunks.push({
x,
y: height + 0.43 * scale,
z,
sx: 0.22 * scale,
sy: 0.86 * scale,
sz: 0.22 * scale,
color: 0x74513a
});
lowerCanopies.push({
x,
y: height + 1.02 * scale,
z,
sx: 1.03 * scale,
sy: 0.78 * scale,
sz: 1.03 * scale,
color:
hash(x, z, 47) > 0.5 ? 0x3f7544 : 0x4f884b
});
upperCanopies.push({
x,
y: height + 1.61 * scale,
z,
sx: 0.7 * scale,
sy: 0.63 * scale,
sz: 0.7 * scale,
color:
hash(x, z, 48) > 0.5 ? 0x568f4e : 0x679c54
});
}
}
makeInstances(
trunks,
new THREE.MeshStandardMaterial({ roughness: 1 })
);
makeInstances(
lowerCanopies,
new THREE.MeshStandardMaterial({ roughness: 0.95 })
);
makeInstances(
upperCanopies,
new THREE.MeshStandardMaterial({ roughness: 0.95 })
);
// -------------------------------------------------------------------------
// Dam, gate, walkway, and spillway
// -------------------------------------------------------------------------
const damBlocks = [];
const walkway = [];
const railings = [];
for (let x = 2; x <= 16; x++) {
damBlocks.push({
x,
y: 2.25,
z: 17.15,
sx: 0.98,
sy: 4.5,
sz: 1.25,
color:
(x + 17) % 3 === 0
? 0x9fa8a2
: (x + 17) % 3 === 1
? 0xb3bbb4
: 0x929d98
});
walkway.push({
x,
y: 4.58,
z: 17.15,
sx: 0.98,
sy: 0.2,
sz: 1.48,
color: 0xd0d3c9
});
if (x % 2 === 0) {
railings.push({
x,
y: 5.05,
z: 17.78,
sx: 0.12,
sy: 0.75,
sz: 0.12,
color: 0x5c6968
});
}
}
makeInstances(
damBlocks,
new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 0.9
})
);
makeInstances(
walkway,
new THREE.MeshStandardMaterial({ roughness: 0.86 })
);
makeInstances(
railings,
new THREE.MeshStandardMaterial({ roughness: 0.7 })
);
makeInstances(
[
{
x: 7,
y: 2.05,
z: 17.83,
sx: 1.72,
sy: 2.18,
sz: 0.13,
color: 0x344c52
},
{
x: 7,
y: 3.25,
z: 17.91,
sx: 1.95,
sy: 0.16,
sz: 0.16,
color: 0xe2b85b
}
],
new THREE.MeshStandardMaterial({ roughness: 0.72 })
);
// -------------------------------------------------------------------------
// Confluence frames
// -------------------------------------------------------------------------
const confluenceFrames = [];
const junctions = [
[-5, -14],
[-2, -10],
[4, -6],
[7, -2]
];
for (const [x, z] of junctions) {
const level = mainLevel(z);
confluenceFrames.push(
{
x,
y: level + 0.09,
z: z - 0.64,
sx: 1.35,
sy: 0.12,
sz: 0.09,
color: 0xffd074
},
{
x,
y: level + 0.09,
z: z + 0.64,
sx: 1.35,
sy: 0.12,
sz: 0.09,
color: 0xffd074
},
{
x: x - 0.64,
y: level + 0.09,
z,
sx: 0.09,
sy: 0.12,
sz: 1.35,
color: 0xffd074
},
{
x: x + 0.64,
y: level + 0.09,
z,
sx: 0.09,
sy: 0.12,
sz: 1.35,
color: 0xffd074
}
);
}
makeInstances(
confluenceFrames,
new THREE.MeshStandardMaterial({
color: 0xffd074,
emissive: 0x7a4210,
emissiveIntensity: 0.18,
roughness: 0.7
}),
false
);
// -------------------------------------------------------------------------
// Source flags and projected HTML labels
// -------------------------------------------------------------------------
const markerContainer = document.getElementById("markers");
const labels = [];
const markerDefinitions = [
{
text: "NORTH FORK",
number: "01",
color: "#ff8b6a",
x: -17,
z: -23,
offsetX: -0.72
},
{
text: "WEST SPUR",
number: "02",
color: "#7dd6a7",
x: -21,
z: -17,
offsetX: -0.72
},
{
text: "EAST FORK",
number: "03",
color: "#70b7ff",
x: 17,
z: -20,
offsetX: 0.72
},
{
text: "LOWER SPUR",
number: "04",
color: "#d79cff",
x: 20,
z: -4,
offsetX: 0.72
},
{
text: "MAIN STEM",
number: "M",
color: "#f3c969",
x: -8,
z: -22,
offsetX: -0.72
}
];
function addHtmlLabel(text, position, options = {}) {
const element = document.createElement("div");
element.className = options.place ? "map-label place-label" : "map-label";
if (options.number) {
const badge = document.createElement("span");
badge.className = "label-number";
badge.textContent = options.number;
badge.style.background = options.color || "#f3c969";
element.appendChild(badge);
}
element.appendChild(document.createTextNode(text));
markerContainer.appendChild(element);
labels.push({
element,
position: position.clone(),
visible: true
});
}
const poleGeometry = new THREE.BoxGeometry(0.08, 1.45, 0.08);
const flagGeometry = new THREE.BoxGeometry(0.68, 0.4, 0.075);
for (const marker of markerDefinitions) {
const x = marker.x + marker.offsetX;
const z = marker.z;
const ground = heights.get(`${Math.round(x)},${Math.round(z)}`) || 4;
const poleMaterial = new THREE.MeshStandardMaterial({
color: 0x4e5049,
roughness: 0.9
});
const flagMaterial = new THREE.MeshStandardMaterial({
color: marker.color,
emissive: marker.color,
emissiveIntensity: 0.12,
roughness: 0.72
});
const pole = new THREE.Mesh(poleGeometry, poleMaterial);
pole.position.set(x, ground + 0.72, z);
pole.castShadow = true;
scene.add(pole);
const flag = new THREE.Mesh(flagGeometry, flagMaterial);
flag.position.set(
x + (marker.offsetX < 0 ? -0.31 : 0.31),
ground + 1.27,
z
);
flag.castShadow = true;
scene.add(flag);
addHtmlLabel(marker.text, new THREE.Vector3(x, ground + 1.75, z), {
number: marker.number,
color: marker.color
});
}
addHtmlLabel(
"RESERVOIR",
new THREE.Vector3(9.2, 4.25, 12.2),
{ place: true }
);
addHtmlLabel(
"DAM",
new THREE.Vector3(7, 5.6, 17.4),
{ place: true }
);
// -------------------------------------------------------------------------
// Moving voxel flow markers
// -------------------------------------------------------------------------
const flowTracers = [];
routes.forEach((route, routeIndex) => {
const count = Math.max(2, Math.floor(route.total / 4.4));
for (let i = 0; i < count; i++) {
flowTracers.push({
route,
routeIndex,
phase: i / count + routeIndex * 0.137
});
}
});
const flowMesh = new THREE.InstancedMesh(
cubeGeometry,
new THREE.MeshStandardMaterial({
color: 0xe7fbff,
emissive: 0x70cbe5,
emissiveIntensity: 0.45,
roughness: 0.2
}),
flowTracers.length
);
flowMesh.frustumCulled = false;
scene.add(flowMesh);
function updateFlowMarkers(elapsed) {
flowTracers.forEach((tracer, index) => {
const route = tracer.route;
const speed = route.main ? 0.043 : 0.034;
const normalized =
(elapsed * speed + tracer.phase) % 1;
const distance = normalized * route.total;
let segment = route.cumulative.length - 2;
for (let i = 0; i < route.cumulative.length - 1; i++) {
if (distance <= route.cumulative[i + 1]) {
segment = i;
break;
}
}
const a = route.points[segment];
const b = route.points[segment + 1];
const segmentStart = route.cumulative[segment];
const segmentLength = route.cumulative[segment + 1] - segmentStart;
const t = clamp(
(distance - segmentStart) / Math.max(segmentLength, 0.001),
0,
1
);
const level = THREE.MathUtils.lerp(
route.levels[segment],
route.levels[segment + 1],
t
);
dummy.position.set(
THREE.MathUtils.lerp(a[0], b[0], t),
level + 0.14,
THREE.MathUtils.lerp(a[1], b[1], t)
);
dummy.rotation.set(0, 0, 0);
dummy.scale.set(0.17, 0.17, 0.17);
dummy.updateMatrix();
flowMesh.setMatrixAt(index, dummy.matrix);
});
flowMesh.instanceMatrix.needsUpdate = true;
}
// -------------------------------------------------------------------------
// Direct camera controls: orbit, pan, zoom, reset
// -------------------------------------------------------------------------
const cameraState = {
yaw: 0.76,
pitch: 0.66,
distance: 70,
target: new THREE.Vector3(0, 2.1, -1.8)
};
function updateCamera() {
const horizontal = cameraState.distance * Math.cos(cameraState.pitch);
camera.position.set(
cameraState.target.x + horizontal * Math.sin(cameraState.yaw),
cameraState.target.y + cameraState.distance * Math.sin(cameraState.pitch),
cameraState.target.z + horizontal * Math.cos(cameraState.yaw)
);
camera.lookAt(cameraState.target);
}
function resetCamera() {
cameraState.yaw = 0.76;
cameraState.pitch = 0.66;
cameraState.distance = 70;
cameraState.target.set(0, 2.1, -1.8);
updateCamera();
}
let drag = null;
renderer.domElement.addEventListener("contextmenu", (event) => {
event.preventDefault();
});
renderer.domElement.addEventListener("pointerdown", (event) => {
drag = {
x: event.clientX,
y: event.clientY,
button: event.button,
shift: event.shiftKey
};
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!drag) return;
const dx = event.clientX - drag.x;
const dy = event.clientY - drag.y;
drag.x = event.clientX;
drag.y = event.clientY;
const panning = drag.button === 2 || drag.shift;
if (panning) {
const right = new THREE.Vector3(
Math.cos(cameraState.yaw),
0,
-Math.sin(cameraState.yaw)
);
const forward = new THREE.Vector3(
-Math.sin(cameraState.yaw),
0,
-Math.cos(cameraState.yaw)
);
const amount = cameraState.distance * 0.0015;
cameraState.target.addScaledVector(right, -dx * amount);
cameraState.target.addScaledVector(forward, -dy * amount);
cameraState.target.x = clamp(cameraState.target.x, -18, 18);
cameraState.target.z = clamp(cameraState.target.z, -18, 18);
} else {
cameraState.yaw -= dx * 0.006;
cameraState.pitch = clamp(
cameraState.pitch + dy * 0.0045,
0.28,
1.18
);
}
updateCamera();
});
function endDrag(event) {
drag = null;
if (
event.pointerId !== undefined &&
renderer.domElement.hasPointerCapture(event.pointerId)
) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
}
renderer.domElement.addEventListener("pointerup", endDrag);
renderer.domElement.addEventListener("pointercancel", endDrag);
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraState.distance = clamp(
cameraState.distance * Math.exp(event.deltaY * 0.001),
38,
100
);
updateCamera();
},
{ passive: false }
);
renderer.domElement.addEventListener("dblclick", resetCamera);
document.getElementById("zoom-in").addEventListener("click", () => {
cameraState.distance = clamp(cameraState.distance - 6, 38, 100);
updateCamera();
});
document.getElementById("zoom-out").addEventListener("click", () => {
cameraState.distance = clamp(cameraState.distance + 6, 38, 100);
updateCamera();
});
document.getElementById("reset").addEventListener("click", resetCamera);
window.addEventListener("keydown", (event) => {
if (event.key === "+" || event.key === "=") {
cameraState.distance = clamp(cameraState.distance - 5, 38, 100);
updateCamera();
}
if (event.key === "-" || event.key === "_") {
cameraState.distance = clamp(cameraState.distance + 5, 38, 100);
updateCamera();
}
if (event.key === "0") {
resetCamera();
}
});
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
});
// -------------------------------------------------------------------------
// Project HTML labels and render
// -------------------------------------------------------------------------
const projected = new THREE.Vector3();
function updateLabels() {
const width = window.innerWidth;
const height = window.innerHeight;
for (const label of labels) {
projected.copy(label.position).project(camera);
const onScreen =
projected.z > -1 &&
projected.z < 1 &&
projected.x > -1.25 &&
projected.x < 1.25 &&
projected.y > -1.25 &&
projected.y < 1.25;
label.element.style.display = onScreen ? "flex" : "none";
if (onScreen) {
label.element.style.left = `${(projected.x * 0.5 + 0.5) * width}px`;
label.element.style.top = `${(-projected.y * 0.5 + 0.5) * height}px`;
}
}
}
updateCamera();
const clock = new THREE.Clock();
renderer.setAnimationLoop(() => {
const elapsed = clock.getElapsedTime();
updateFlowMarkers(elapsed);
updateLabels();
renderer.render(scene, camera);
});
</script>
</body>
</html>
```