Prompt specification
Build or customize the Three.js 3D scene "Canal splitting water between two terraces" 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: Canal splitting water between two terraces
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/canal-splitting-water-between-two-terraces-x2254
Scene Description Prompt:
In a standalone HTML scene using Three.js from cdnjs, animate downhill water splitting through gates between two voxel Andean planting terraces.
Working Three.js Starter (canal-splitting-water-between-two-terraces-x2254):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Andes — Split Flow</title>
<style>
:root {
color-scheme: dark;
--cream: #fff6df;
--muted: rgba(255, 246, 223, 0.72);
--glass: rgba(13, 45, 49, 0.64);
--line: rgba(255, 255, 255, 0.16);
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background:
radial-gradient(circle at 76% 15%, rgba(255, 242, 190, 0.95) 0 4%, transparent 19%),
linear-gradient(180deg, #83c7d0 0%, #c8e3d7 54%, #edd4a3 100%);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
canvas {
position: fixed;
inset: 0;
display: block;
width: 100%;
height: 100%;
touch-action: none;
cursor: grab;
}
canvas:active {
cursor: grabbing;
}
#vignette {
position: fixed;
inset: 0;
pointer-events: none;
background: radial-gradient(circle at 50% 48%, transparent 42%, rgba(13, 44, 45, 0.2));
}
#hud {
position: fixed;
inset: 0;
pointer-events: none;
color: var(--cream);
text-shadow: 0 2px 14px rgba(16, 48, 48, 0.35);
}
.title {
position: absolute;
top: clamp(20px, 4vw, 48px);
left: clamp(20px, 4vw, 52px);
max-width: 430px;
}
.eyebrow {
display: flex;
align-items: center;
gap: 10px;
margin-bottom: 12px;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.24em;
text-transform: uppercase;
opacity: 0.82;
}
.eyebrow::before {
content: "";
width: 30px;
height: 2px;
background: #ffe7a5;
box-shadow: 0 0 12px rgba(255, 231, 165, 0.8);
}
h1 {
margin: 0;
font-size: clamp(38px, 6vw, 76px);
font-weight: 780;
line-height: 0.88;
letter-spacing: -0.065em;
}
h1 span {
display: block;
color: #ffe3a0;
font-weight: 430;
}
.subtitle {
max-width: 360px;
margin: 20px 0 0;
color: var(--muted);
font-size: 13px;
line-height: 1.65;
}
.readout {
position: absolute;
left: clamp(20px, 4vw, 52px);
bottom: clamp(20px, 4vw, 42px);
display: flex;
flex-wrap: wrap;
gap: 8px;
}
.chip {
padding: 9px 12px;
border: 1px solid var(--line);
border-radius: 999px;
background: var(--glass);
backdrop-filter: blur(12px);
font-size: 9px;
font-weight: 750;
letter-spacing: 0.13em;
text-transform: uppercase;
}
.chip b {
margin-right: 7px;
color: #77e3ec;
font-size: 11px;
}
.controls {
position: absolute;
right: clamp(20px, 4vw, 48px);
bottom: clamp(20px, 4vw, 42px);
padding: 12px 15px;
border: 1px solid var(--line);
border-radius: 13px;
background: var(--glass);
backdrop-filter: blur(12px);
color: rgba(255, 255, 255, 0.76);
font-size: 10px;
font-weight: 650;
letter-spacing: 0.08em;
text-transform: uppercase;
}
#loading {
position: fixed;
inset: 0;
z-index: 10;
display: grid;
place-items: center;
color: #16484a;
background: #b9ddd3;
font-size: 11px;
font-weight: 800;
letter-spacing: 0.22em;
text-transform: uppercase;
transition: opacity 500ms ease, visibility 500ms ease;
}
#loading.gone {
opacity: 0;
visibility: hidden;
}
@media (max-width: 700px) {
.subtitle {
max-width: 270px;
font-size: 11px;
}
.readout {
max-width: 235px;
}
.controls {
bottom: 95px;
right: 18px;
font-size: 8px;
}
}
</style>
</head>
<body>
<canvas id="scene" aria-label="Interactive voxel Andean terraces with flowing water"></canvas>
<div id="vignette"></div>
<div id="hud">
<section class="title">
<div class="eyebrow">Highland waterworks · Study 01</div>
<h1>Andes<span>Split Flow</span></h1>
<p class="subtitle">
A gravity-fed watershed divides at a stone splitter, passes through twin
timber sluices, and descends between planted Andean terraces.
</p>
</section>
<div class="readout">
<div class="chip"><b>03</b>Terrace levels</div>
<div class="chip"><b>02</b>Timber gates</div>
<div class="chip"><b>01</b>Continuous current</div>
</div>
<div class="controls">Drag to orbit · Scroll to zoom · R to reset</div>
</div>
<div id="loading">Building the watershed…</div>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const canvas = document.querySelector("#scene");
// ---------------------------------------------------------------------
// Renderer, scene, lighting
// ---------------------------------------------------------------------
const renderer = new THREE.WebGLRenderer({
canvas,
antialias: true,
alpha: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 1.8));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setClearColor(0x000000, 0);
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.15;
const scene = new THREE.Scene();
scene.fog = new THREE.Fog(0xc8dfd7, 25, 49);
const camera = new THREE.PerspectiveCamera(
39,
window.innerWidth / window.innerHeight,
0.1,
100
);
const hemi = new THREE.HemisphereLight(0xdff8ff, 0x65462f, 2.15);
scene.add(hemi);
const sun = new THREE.DirectionalLight(0xffedc5, 3.5);
sun.position.set(-10, 18, 10);
sun.target.position.set(0, 1.5, -1);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -18;
sun.shadow.camera.right = 18;
sun.shadow.camera.top = 18;
sun.shadow.camera.bottom = -18;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 48;
sun.shadow.bias = -0.00035;
scene.add(sun, sun.target);
const rim = new THREE.DirectionalLight(0x8de9f0, 1.1);
rim.position.set(10, 7, -10);
scene.add(rim);
// ---------------------------------------------------------------------
// Materials
// ---------------------------------------------------------------------
const standard = (color, roughness = 0.85, metalness = 0) =>
new THREE.MeshStandardMaterial({ color, roughness, metalness });
const M = {
earth: standard(0x6d4930),
earthDark: standard(0x493326),
earthLight: standard(0x8d6040),
grass: standard(0x668c48),
grassDark: standard(0x45693d),
grassLight: standard(0x83a858),
stone: standard(0x777d72),
stoneLight: standard(0x9a9b86),
stoneDark: standard(0x555d57),
stoneWarm: standard(0x8b816c),
mountain: standard(0x657064),
mountainDark: standard(0x4c5a53),
mountainLight: standard(0x7b8273),
snow: standard(0xf1f4e9),
cloud: standard(0xf7f4df),
wood: standard(0x71472d),
woodLight: standard(0xa16d3d),
woodDark: standard(0x3e3027),
metal: standard(0x38494a, 0.48, 0.5),
leaf: standard(0x3f783f),
leafLight: standard(0x6d9845),
maize: standard(0xe4b94d),
maizeLight: standard(0xf5d66d),
purple: standard(0x765080),
potato: standard(0x9b6847),
water: new THREE.MeshStandardMaterial({
color: 0x32c3d5,
emissive: 0x0a6975,
emissiveIntensity: 0.48,
roughness: 0.2,
metalness: 0.06,
transparent: true,
opacity: 0.82,
side: THREE.DoubleSide,
depthWrite: false
}),
waterfall: new THREE.MeshStandardMaterial({
color: 0x67dce6,
emissive: 0x176d76,
emissiveIntensity: 0.38,
roughness: 0.24,
transparent: true,
opacity: 0.67,
side: THREE.DoubleSide,
depthWrite: false
}),
foam: new THREE.MeshStandardMaterial({
color: 0xe9ffff,
emissive: 0x8de4e7,
emissiveIntensity: 0.25,
roughness: 0.45
})
};
// ---------------------------------------------------------------------
// Static voxel batching
// ---------------------------------------------------------------------
const unitBox = new THREE.BoxGeometry(1, 1, 1);
const dummy = new THREE.Object3D();
const batches = new Map();
let seed = 48127;
function random() {
seed = (seed * 1664525 + 1013904223) >>> 0;
return seed / 4294967296;
}
function voxel(
material,
x, y, z,
sx, sy, sz,
ry = 0,
rx = 0,
rz = 0
) {
if (!batches.has(material)) batches.set(material, []);
dummy.position.set(x, y, z);
dummy.rotation.set(rx, ry, rz);
dummy.scale.set(sx, sy, sz);
dummy.updateMatrix();
batches.get(material).push(dummy.matrix.clone());
}
function flushBatches() {
for (const [material, matrices] of batches) {
const mesh = new THREE.InstancedMesh(unitBox, material, matrices.length);
matrices.forEach((matrix, i) => mesh.setMatrixAt(i, matrix));
mesh.instanceMatrix.needsUpdate = true;
mesh.castShadow = true;
mesh.receiveShadow = true;
scene.add(mesh);
}
}
// ---------------------------------------------------------------------
// Small geometry helpers
// ---------------------------------------------------------------------
const V = (x = 0, y = 0, z = 0) => new THREE.Vector3(x, y, z);
const X_AXIS = V(1, 0, 0);
function beam(a, b, width, height, material) {
const direction = b.clone().sub(a);
const length = direction.length();
if (length < 0.001) return null;
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(length, height, width),
material
);
mesh.position.copy(a).add(b).multiplyScalar(0.5);
mesh.quaternion.setFromUnitVectors(X_AXIS, direction.normalize());
mesh.castShadow = true;
mesh.receiveShadow = true;
scene.add(mesh);
return mesh;
}
function waterRibbon(a, b, width, material = M.water) {
const direction = b.clone().sub(a);
const horizontal = V(direction.x, 0, direction.z).normalize();
const perpendicular = V(-horizontal.z, 0, horizontal.x).multiplyScalar(width / 2);
const aLeft = a.clone().add(perpendicular);
const aRight = a.clone().sub(perpendicular);
const bLeft = b.clone().add(perpendicular);
const bRight = b.clone().sub(perpendicular);
const geometry = new THREE.BufferGeometry();
geometry.setAttribute(
"position",
new THREE.Float32BufferAttribute([
aLeft.x, aLeft.y, aLeft.z,
aRight.x, aRight.y, aRight.z,
bLeft.x, bLeft.y, bLeft.z,
bRight.x, bRight.y, bRight.z
], 3)
);
geometry.setIndex([
0, 2, 1,
2, 3, 1
]);
geometry.computeVertexNormals();
const mesh = new THREE.Mesh(geometry, material);
mesh.receiveShadow = true;
scene.add(mesh);
return mesh;
}
function waterBox(x, y, z, width, depth, material = M.water) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(width, 0.08, depth),
material
);
mesh.position.set(x, y, z);
mesh.receiveShadow = true;
scene.add(mesh);
return mesh;
}
function horizontalPerpendicular(a, b) {
const direction = b.clone().sub(a);
const horizontal = V(direction.x, 0, direction.z).normalize();
return V(-horizontal.z, 0, horizontal.x);
}
function channel(a, b, waterWidth = 0.78) {
const floorA = a.clone().add(V(0, -0.2, 0));
const floorB = b.clone().add(V(0, -0.2, 0));
beam(floorA, floorB, waterWidth + 0.28, 0.3, M.stoneDark);
waterRibbon(a, b, waterWidth);
const perpendicular = horizontalPerpendicular(a, b);
for (const side of [-1, 1]) {
const offset = perpendicular.clone().multiplyScalar(
side * (waterWidth / 2 + 0.1)
);
beam(
a.clone().add(offset).add(V(0, 0.09, 0)),
b.clone().add(offset).add(V(0, 0.09, 0)),
0.18,
0.46,
side === 1 ? M.stone : M.stoneWarm
);
}
}
function waterfall(a, b, width = 0.82) {
const height = Math.abs(b.y - a.y);
const mesh = new THREE.Mesh(
new THREE.PlaneGeometry(width, height),
M.waterfall
);
mesh.position.set(
(a.x + b.x) / 2,
(a.y + b.y) / 2,
(a.z + b.z) / 2
);
mesh.rotation.y = Math.PI / 2;
scene.add(mesh);
// Small foam shelf at the bottom.
voxel(
M.foam,
b.x,
b.y + 0.045,
b.z + 0.04,
width * 0.72,
0.045,
0.22
);
}
// ---------------------------------------------------------------------
// Diorama foundation and distant voxel Andes
// ---------------------------------------------------------------------
voxel(M.earthDark, 0, -0.55, 0, 24, 1.05, 17);
voxel(M.earth, 0, -0.02, 0, 23.6, 0.2, 16.6);
voxel(M.grassDark, 0, 0.1, 0, 23.2, 0.12, 16.2);
// Exposed geological strata along the front of the cutaway.
for (let x = -11.2; x <= 11.2; x += 0.9) {
voxel(
random() > 0.5 ? M.stoneWarm : M.earthLight,
x,
-0.32 + random() * 0.1,
8.35,
0.82,
0.38 + random() * 0.18,
0.18
);
}
function mountainPeak(x, z, width, depth, height) {
const levels = Math.ceil(height / 0.85);
for (let level = 0; level < levels; level++) {
const t = level / Math.max(1, levels - 1);
const taper = 1 - t * 0.62;
const layerWidth = width * taper + 0.4;
const layerDepth = depth * taper + 0.35;
let material;
if (t > 0.72) {
material = M.snow;
} else if (t > 0.48) {
material = random() > 0.45 ? M.mountainLight : M.mountain;
} else {
material = random() > 0.5 ? M.mountainDark : M.mountain;
}
voxel(
material,
x + (random() - 0.5) * 0.16,
0.4 + level * 0.85 + 0.42,
z + (random() - 0.5) * 0.12,
layerWidth,
0.85,
layerDepth
);
}
}
mountainPeak(-8.2, -6.7, 4.6, 3.4, 6.1);
mountainPeak(-4.7, -7.1, 3.8, 3.3, 8.0);
mountainPeak(0.0, -7.6, 4.0, 3.2, 6.7);
mountainPeak(4.8, -7.2, 3.9, 3.5, 8.4);
mountainPeak(8.7, -6.7, 4.5, 3.5, 6.3);
// Voxel clouds.
const cloudPuffs = [
[-8.8, 9.3, -6.5, 2.2, 0.65, 0.8],
[-7.8, 9.5, -6.4, 1.2, 0.85, 0.9],
[-9.7, 9.0, -6.4, 1.0, 0.55, 0.7],
[7.8, 9.6, -7.0, 2.1, 0.65, 0.9],
[8.8, 9.2, -7.0, 1.2, 0.8, 0.75]
];
for (const [x, y, z, w, h, d] of cloudPuffs) {
voxel(M.cloud, x, y, z, w, h, d);
}
// ---------------------------------------------------------------------
// Two stepped planting terraces
// ---------------------------------------------------------------------
const rowZ = [-2.7, -0.05, 2.6];
const rowTop = [3.2, 2.15, 1.1];
const rowDepth = 2.45;
function addCrop(side, x, z, baseY, type) {
const px = side * x;
if (type === 0) {
// Maize: stalk, leaves, and golden cob.
voxel(M.leaf, px, baseY + 0.42, z, 0.12, 0.8, 0.12);
voxel(M.leafLight, px - 0.15, baseY + 0.53, z, 0.28, 0.1, 0.13);
voxel(M.leafLight, px + 0.15, baseY + 0.42, z, 0.28, 0.1, 0.13);
voxel(M.maize, px + 0.1, baseY + 0.42, z, 0.22, 0.34, 0.19);
voxel(M.maizeLight, px, baseY + 0.83, z, 0.2, 0.13, 0.16);
} else if (type === 1) {
// Low potato bushes with visible tubers.
voxel(M.leaf, px, baseY + 0.13, z, 0.48, 0.22, 0.42);
voxel(M.leafLight, px - 0.12, baseY + 0.22, z + 0.08, 0.24, 0.17, 0.25);
voxel(M.leaf, px + 0.13, baseY + 0.18, z - 0.05, 0.24, 0.16, 0.23);
if (random() > 0.52) {
voxel(M.potato, px + 0.1, baseY + 0.08, z + 0.22, 0.17, 0.13, 0.17);
}
} else {
// Purple quinoa-like flowering stalks.
voxel(M.leaf, px, baseY + 0.32, z, 0.1, 0.62, 0.1);
voxel(M.leafLight, px - 0.12, baseY + 0.33, z, 0.23, 0.1, 0.1);
voxel(M.purple, px, baseY + 0.67, z, 0.2, 0.18, 0.2);
voxel(M.maizeLight, px, baseY + 0.78, z, 0.12, 0.12, 0.12);
}
}
for (const side of [-1, 1]) {
rowZ.forEach((z, rowIndex) => {
const top = rowTop[rowIndex];
const centerX = side * 6.55;
voxel(
M.earth,
centerX,
(top + 0.14) / 2,
z,
6.9,
top - 0.14,
rowDepth
);
voxel(
rowIndex % 2 ? M.grassDark : M.grass,
centerX,
top + 0.08,
z,
6.85,
0.16,
rowDepth - 0.06
);
// Small irregular grass patches.
for (let x = 3.5; x < 9.8; x += 0.72) {
if (random() > 0.35) {
voxel(
random() > 0.5 ? M.grassLight : M.grassDark,
side * x,
top + 0.18,
z + (random() - 0.5) * 1.65,
0.52,
0.035,
0.44
);
}
}
// Voxel retaining wall on the downhill-facing edge.
const wallZ = z + rowDepth / 2 + 0.06;
const levelCount = Math.ceil((top - 0.28) / 0.52);
for (let level = 0; level < levelCount; level++) {
for (let x = 3.35; x < 9.9; x += 0.91) {
voxel(
random() > 0.68 ? M.stoneLight :
random() > 0.5 ? M.stoneWarm : M.stone,
side * x,
0.38 + level * 0.52,
wallZ,
0.86,
0.48,
0.28
);
}
}
// Outer and inner terrace edging.
for (let edgeZ = z - 0.95; edgeZ < z + 1.0; edgeZ += 0.64) {
voxel(
M.stoneWarm,
side * 10.02,
top - 0.2,
edgeZ,
0.28,
0.52,
0.59
);
voxel(
M.stoneLight,
side * 3.04,
top - 0.13,
edgeZ,
0.24,
0.44,
0.59
);
}
// Planted rows.
for (let x = 3.8; x <= 9.35; x += 1.04) {
for (const plantZ of [z - 0.57, z + 0.57]) {
addCrop(side, x, plantZ, top + 0.16, rowIndex);
}
}
});
}
// ---------------------------------------------------------------------
// Central stone spine, upper reservoir, splitter, and channels
// ---------------------------------------------------------------------
voxel(M.stoneDark, 0, 1.65, -0.35, 2.5, 3.15, 6.2);
for (let z = -3.25; z <= 2.5; z += 0.78) {
for (let y = 0.45; y < 3.2; y += 0.72) {
for (const x of [-0.8, 0, 0.8]) {
voxel(
random() > 0.5 ? M.stone : M.stoneWarm,
x,
y,
z,
0.72,
0.65,
0.72
);
}
}
}
// Upper reservoir platform.
voxel(M.stoneDark, 0, 4.32, -4.72, 3.25, 0.85, 2.65);
voxel(M.stoneWarm, 0, 4.78, -4.72, 3.1, 0.18, 2.5);
// Reservoir rim.
voxel(M.stoneLight, -1.56, 5.18, -4.68, 0.27, 0.78, 2.35);
voxel(M.stoneLight, 1.56, 5.18, -4.68, 0.27, 0.78, 2.35);
voxel(M.stoneWarm, 0, 5.18, -5.75, 3.25, 0.78, 0.28);
const reservoirWater = waterBox(
0,
5.18,
-4.68,
2.76,
2.05,
M.water
);
const fork = V(0, 4.68, -2.83);
const reservoirFront = V(0, 5.18, -3.76);
channel(reservoirFront, fork, 0.72);
const gates = [];
const gatePoints = [];
for (const side of [-1, 1]) {
const gatePoint = V(side * 1.95, 4.05, -1.62);
const sideStart = V(side * 2.25, 3.78, -1.05);
gatePoints.push(gatePoint);
channel(fork, gatePoint, 0.68);
channel(gatePoint, sideStart, 0.78);
// Voxel abutments around the sluice.
voxel(M.stoneLight, gatePoint.x, 3.77, gatePoint.z - 0.66, 0.55, 0.8, 0.55);
voxel(M.stoneWarm, gatePoint.x, 3.77, gatePoint.z + 0.66, 0.55, 0.8, 0.55);
const gate = new THREE.Group();
const direction = gatePoint.clone().sub(fork);
gate.position.copy(gatePoint);
gate.rotation.y = -Math.atan2(direction.z, direction.x);
const postGeometry = new THREE.BoxGeometry(0.18, 1.58, 0.18);
for (const z of [-0.62, 0.62]) {
const post = new THREE.Mesh(postGeometry, M.woodDark);
post.position.set(0, 0.73, z);
post.castShadow = true;
gate.add(post);
}
const crossbar = new THREE.Mesh(
new THREE.BoxGeometry(0.25, 0.2, 1.48),
M.woodLight
);
crossbar.position.set(0, 1.48, 0);
crossbar.castShadow = true;
gate.add(crossbar);
const panel = new THREE.Group();
for (let z = -0.37; z <= 0.38; z += 0.185) {
const slat = new THREE.Mesh(
new THREE.BoxGeometry(0.13, 0.52, 0.145),
M.wood
);
slat.position.set(0, 0.43, z);
slat.castShadow = true;
panel.add(slat);
}
for (const y of [0.28, 0.59]) {
const band = new THREE.Mesh(
new THREE.BoxGeometry(0.17, 0.065, 1.02),
M.metal
);
band.position.set(0, y, 0);
panel.add(band);
}
gate.add(panel);
scene.add(gate);
gates.push({ group: gate, panel });
}
// Splitting platform beneath the Y-junction.
voxel(M.stoneLight, 0, 4.38, -2.83, 1.22, 0.35, 1.22);
voxel(M.stoneWarm, 0.66, 4.4, -2.83, 0.35, 0.38, 1.3, 0.62);
voxel(M.stoneWarm, -0.66, 4.4, -2.83, 0.35, 0.38, 1.3, -0.62);
// Stepped side channels: each side descends beside its terrace.
const sideNodes = [
V(2.25, 3.78, -1.05),
V(2.25, 3.62, 0.25),
V(2.25, 2.58, 0.35),
V(2.25, 2.58, 1.55),
V(2.25, 1.53, 1.68),
V(2.25, 1.53, 3.48),
V(2.25, 0.62, 3.65),
V(2.25, 0.62, 5.25)
];
for (const side of [-1, 1]) {
let previous = gatePoints[side === 1 ? 1 : 0];
for (const node of sideNodes) {
const point = node.clone();
point.x *= side;
if (Math.abs(point.y - previous.y) > 0.4) {
waterfall(previous, point, 0.86);
} else {
channel(previous, point, 0.8);
}
previous = point;
}
// Lower collection pool.
const poolX = side * 2.25;
voxel(M.stoneDark, poolX, 0.28, 5.62, 1.75, 0.5, 1.85);
voxel(M.stoneWarm, poolX, 0.52, 6.48, 1.8, 0.5, 0.28);
voxel(M.stoneLight, poolX - 0.8, 0.52, 5.62, 0.28, 0.5, 1.72);
voxel(M.stoneLight, poolX + 0.8, 0.52, 5.62, 0.28, 0.5, 1.72);
waterBox(poolX, 0.58, 5.62, 1.35, 1.42);
// A few bright foam blocks around the impact zone.
for (let i = 0; i < 4; i++) {
voxel(
M.foam,
poolX + (random() - 0.5) * 0.72,
0.64,
5.42 + random() * 0.3,
0.13,
0.04,
0.13
);
}
}
flushBatches();
// ---------------------------------------------------------------------
// Animated water particles following exact stepped paths
// ---------------------------------------------------------------------
function makePath(points) {
const segments = [];
let totalLength = 0;
for (let i = 0; i < points.length - 1; i++) {
const a = points[i];
const b = points[i + 1];
const length = a.distanceTo(b);
segments.push({ a, b, length, start: totalLength });
totalLength += length;
}
return {
points,
length: totalLength,
sample(t) {
const distance = ((t % 1) + 1) % 1 * totalLength;
let segment = segments[segments.length - 1];
for (const candidate of segments) {
if (distance <= candidate.start + candidate.length) {
segment = candidate;
break;
}
}
const local = segment.length === 0
? 0
: (distance - segment.start) / segment.length;
const position = segment.a.clone().lerp(segment.b, local);
const tangent = segment.b.clone().sub(segment.a).normalize();
return { position, tangent };
}
};
}
const mainPath = makePath([
V(0, 5.26, -5.25),
V(0, 5.24, -4.45),
V(0, 5.08, -3.62),
fork.clone()
]);
const branchPaths = [-1, 1].map(side => {
const point = (x, y, z) => V(side * x, y, z);
return makePath([
fork.clone(),
point(0.72, 4.53, -2.55),
point(1.44, 4.28, -2.05),
point(1.95, 4.05, -1.62),
point(2.25, 3.78, -1.05),
point(2.25, 3.62, 0.25),
point(2.25, 2.58, 0.35),
point(2.25, 2.58, 1.55),
point(2.25, 1.53, 1.68),
point(2.25, 1.53, 3.48),
point(2.25, 0.62, 3.65),
point(2.25, 0.62, 5.25)
]);
});
const cyanParticles = [];
const foamParticles = [];
for (let i = 0; i < 30; i++) {
cyanParticles.push({
path: mainPath,
offset: i / 30,
speed: 0.15 + random() * 0.035
});
}
for (const path of branchPaths) {
for (let i = 0; i < 48; i++) {
cyanParticles.push({
path,
offset: i / 48,
speed: 0.095 + random() * 0.026
});
}
for (let i = 0; i < 21; i++) {
foamParticles.push({
path,
offset: i / 21,
speed: 0.092 + random() * 0.025
});
}
}
for (let i = 0; i < 8; i++) {
foamParticles.push({
path: mainPath,
offset: i / 8,
speed: 0.145 + random() * 0.025
});
}
function createParticleMesh(count, material) {
const mesh = new THREE.InstancedMesh(unitBox, material, count);
mesh.instanceMatrix.setUsage(THREE.DynamicDrawUsage);
mesh.frustumCulled = false;
mesh.castShadow = false;
scene.add(mesh);
return mesh;
}
const cyanMesh = createParticleMesh(cyanParticles.length, M.foam);
const foamMesh = createParticleMesh(foamParticles.length, M.foam);
function updateParticles(mesh, particles, time, foam = false) {
particles.forEach((particle, index) => {
const phase = (time * particle.speed + particle.offset) % 1;
const { position, tangent } = particle.path.sample(phase);
position.y += foam ? 0.105 : 0.075;
dummy.position.copy(position);
dummy.quaternion.setFromUnitVectors(X_AXIS, tangent);
if (foam) {
const pulse = 0.85 + Math.sin(time * 7 + index) * 0.13;
dummy.scale.set(0.16 * pulse, 0.055, 0.12 * pulse);
} else {
const pulse = 0.86 + Math.sin(time * 5 + index * 0.8) * 0.14;
dummy.scale.set(0.28 * pulse, 0.065, 0.13);
}
dummy.updateMatrix();
mesh.setMatrixAt(index, dummy.matrix);
});
mesh.instanceMatrix.needsUpdate = true;
}
// ---------------------------------------------------------------------
// Direct camera controls: orbit, zoom, keyboard, and reset
// ---------------------------------------------------------------------
const cameraState = {
target: V(0, 2.25, -0.15),
radius: 23.5,
theta: 0.56,
phi: 1.03
};
const initialCamera = { ...cameraState };
function updateCamera() {
const sinPhi = Math.sin(cameraState.phi);
camera.position.set(
cameraState.target.x + cameraState.radius * sinPhi * Math.sin(cameraState.theta),
cameraState.target.y + cameraState.radius * Math.cos(cameraState.phi),
cameraState.target.z + cameraState.radius * sinPhi * Math.cos(cameraState.theta)
);
camera.lookAt(cameraState.target);
}
function resetCamera() {
cameraState.target.copy(initialCamera.target);
cameraState.radius = initialCamera.radius;
cameraState.theta = initialCamera.theta;
cameraState.phi = initialCamera.phi;
updateCamera();
}
let dragging = false;
let previousX = 0;
let previousY = 0;
canvas.addEventListener("pointerdown", event => {
dragging = true;
previousX = event.clientX;
previousY = event.clientY;
canvas.setPointerCapture(event.pointerId);
});
canvas.addEventListener("pointermove", event => {
if (!dragging) return;
const deltaX = event.clientX - previousX;
const deltaY = event.clientY - previousY;
previousX = event.clientX;
previousY = event.clientY;
cameraState.theta -= deltaX * 0.006;
cameraState.phi = THREE.MathUtils.clamp(
cameraState.phi + deltaY * 0.005,
0.38,
1.35
);
updateCamera();
});
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("contextmenu", event => event.preventDefault());
canvas.addEventListener("wheel", event => {
event.preventDefault();
cameraState.radius = THREE.MathUtils.clamp(
cameraState.radius * Math.exp(event.deltaY * 0.001),
12,
35
);
updateCamera();
}, { passive: false });
canvas.addEventListener("dblclick", resetCamera);
window.addEventListener("keydown", event => {
const key = event.key.toLowerCase();
if (key === "r") {
resetCamera();
} else if (key === "arrowleft") {
cameraState.theta -= 0.08;
} else if (key === "arrowright") {
cameraState.theta += 0.08;
} else if (key === "arrowup") {
cameraState.phi = Math.max(0.38, cameraState.phi - 0.06);
} else if (key === "arrowdown") {
cameraState.phi = Math.min(1.35, cameraState.phi + 0.06);
} else if (key === "+" || key === "=") {
cameraState.radius = Math.max(12, cameraState.radius - 1);
} else if (key === "-") {
cameraState.radius = Math.min(35, cameraState.radius + 1);
} else {
return;
}
updateCamera();
});
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 1.8));
renderer.setSize(window.innerWidth, window.innerHeight);
});
updateCamera();
// ---------------------------------------------------------------------
// Animation loop
// ---------------------------------------------------------------------
const clock = new THREE.Clock();
function animate() {
requestAnimationFrame(animate);
const time = clock.getElapsedTime();
reservoirWater.position.y = 5.18 + Math.sin(time * 1.35) * 0.025;
reservoirWater.material.opacity = 0.79 + Math.sin(time * 1.1) * 0.025;
M.water.emissiveIntensity = 0.45 + Math.sin(time * 2.0) * 0.07;
gates.forEach((gate, index) => {
gate.panel.position.y = Math.sin(time * 1.8 + index) * 0.012;
});
updateParticles(cyanMesh, cyanParticles, time, false);
updateParticles(foamMesh, foamParticles, time, true);
renderer.render(scene, camera);
}
animate();
requestAnimationFrame(() => {
document.querySelector("#loading").classList.add("gone");
});
</script>
</body>
</html>
```