Prompt specification
Build or customize the Three.js 3D scene "Neighborhood stormwater journey" 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: Neighborhood stormwater journey
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/neighborhood-stormwater-journey-x1235
Scene Description Prompt:
Follow voxel raindrops downhill from three rooftops through gutters and a planted swale into a retention pond. Three.js from cdnjs, one HTML file.
Working Three.js Starter (neighborhood-stormwater-journey-x1235):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="color-scheme" content="dark">
<title>Voxel Stormwater — Rooftop to Retention Pond</title>
<style>
:root {
--ink: #eaf7f7;
--muted: #9fb9bd;
--glass: rgba(11, 29, 34, 0.78);
--line: rgba(183, 229, 231, 0.16);
--cyan: #70e5f2;
--green: #9bd78c;
--yellow: #f6d889;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #b9d7da;
color: var(--ink);
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
}
#hud {
position: fixed;
inset: 0;
pointer-events: none;
z-index: 10;
}
.panel {
background: var(--glass);
border: 1px solid var(--line);
box-shadow: 0 18px 50px rgba(9, 27, 31, 0.2);
backdrop-filter: blur(15px);
-webkit-backdrop-filter: blur(15px);
}
#title-card {
position: absolute;
top: 22px;
left: 22px;
width: min(410px, calc(100vw - 44px));
padding: 18px 19px 16px;
border-radius: 18px;
}
.eyebrow {
display: flex;
align-items: center;
gap: 8px;
margin-bottom: 8px;
color: var(--cyan);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.19em;
text-transform: uppercase;
}
.live-dot {
width: 7px;
height: 7px;
border-radius: 50%;
background: var(--cyan);
box-shadow: 0 0 12px var(--cyan);
animation: pulse 1.8s ease-in-out infinite;
}
@keyframes pulse {
0%,
100% {
opacity: 0.45;
transform: scale(0.8);
}
50% {
opacity: 1;
transform: scale(1.15);
}
}
h1 {
margin: 0;
font-size: clamp(23px, 3vw, 35px);
line-height: 1.02;
letter-spacing: -0.045em;
font-weight: 780;
}
.subtitle {
margin: 9px 0 0;
color: var(--muted);
font-size: 13px;
line-height: 1.45;
}
#route-card {
position: absolute;
top: 22px;
right: 22px;
width: 238px;
padding: 16px;
border-radius: 16px;
}
.small-label {
color: var(--muted);
font-size: 9px;
font-weight: 800;
letter-spacing: 0.16em;
text-transform: uppercase;
}
.route-row {
display: flex;
align-items: baseline;
justify-content: space-between;
gap: 12px;
margin-top: 7px;
}
#stage-name {
font-size: 16px;
font-weight: 750;
letter-spacing: -0.02em;
}
#elevation {
color: var(--cyan);
font: 700 13px ui-monospace, SFMono-Regular, Menlo, monospace;
white-space: nowrap;
}
#stage-copy {
min-height: 44px;
margin: 9px 0 13px;
color: var(--muted);
font-size: 11px;
line-height: 1.45;
}
.progress-track {
height: 5px;
overflow: hidden;
border-radius: 99px;
background: rgba(255, 255, 255, 0.09);
}
#progress-fill {
width: 0;
height: 100%;
border-radius: inherit;
background: linear-gradient(90deg, var(--cyan), #b4f2d0);
box-shadow: 0 0 13px rgba(112, 229, 242, 0.5);
}
.progress-caption {
display: flex;
justify-content: space-between;
margin-top: 7px;
color: #799397;
font-size: 9px;
letter-spacing: 0.08em;
text-transform: uppercase;
}
#controls {
position: absolute;
left: 50%;
bottom: 20px;
display: flex;
align-items: center;
gap: 10px;
width: max-content;
max-width: calc(100vw - 28px);
padding: 10px;
border-radius: 17px;
transform: translateX(-50%);
pointer-events: auto;
}
.group {
display: flex;
align-items: center;
gap: 5px;
}
.group-label {
padding: 0 7px 0 4px;
color: var(--muted);
font-size: 9px;
font-weight: 800;
letter-spacing: 0.12em;
text-transform: uppercase;
}
button {
min-height: 34px;
padding: 0 12px;
border: 1px solid transparent;
border-radius: 10px;
background: rgba(255, 255, 255, 0.055);
color: #c9dddd;
font: inherit;
font-size: 11px;
font-weight: 720;
cursor: pointer;
transition:
color 160ms ease,
background 160ms ease,
border-color 160ms ease,
transform 160ms ease;
}
button:hover {
color: white;
background: rgba(255, 255, 255, 0.1);
}
button:active {
transform: translateY(1px);
}
button.active {
border-color: rgba(112, 229, 242, 0.46);
background: rgba(112, 229, 242, 0.15);
color: #bff6fb;
}
.separator {
width: 1px;
height: 27px;
background: var(--line);
}
#speed {
width: 86px;
accent-color: var(--cyan);
}
#speed-value {
width: 35px;
color: var(--cyan);
font: 700 10px ui-monospace, SFMono-Regular, Menlo, monospace;
}
#hint {
position: absolute;
right: 22px;
bottom: 22px;
padding: 8px 11px;
border-radius: 10px;
color: rgba(235, 247, 247, 0.75);
font-size: 10px;
letter-spacing: 0.035em;
}
#labels {
position: absolute;
inset: 0;
overflow: hidden;
pointer-events: none;
}
.world-label {
position: absolute;
padding: 5px 8px;
border: 1px solid rgba(255, 255, 255, 0.24);
border-radius: 7px;
background: rgba(13, 37, 42, 0.72);
box-shadow: 0 5px 18px rgba(0, 0, 0, 0.14);
color: white;
font-size: 9px;
font-weight: 850;
letter-spacing: 0.12em;
text-transform: uppercase;
transform: translate(-50%, -50%);
white-space: nowrap;
will-change: left, top;
}
.world-label.cyan {
border-color: rgba(112, 229, 242, 0.42);
color: #baf7fb;
}
.world-label.green {
border-color: rgba(155, 215, 140, 0.42);
color: #c9f0bc;
}
#loading {
position: fixed;
inset: 0;
z-index: 100;
display: grid;
place-items: center;
background: #b9d7da;
color: #173b40;
transition: opacity 500ms ease;
}
#loading.out {
opacity: 0;
pointer-events: none;
}
.loading-box {
text-align: center;
font-size: 11px;
font-weight: 850;
letter-spacing: 0.17em;
text-transform: uppercase;
}
.loading-blocks {
display: flex;
justify-content: center;
gap: 5px;
margin-bottom: 13px;
}
.loading-blocks i {
width: 10px;
height: 10px;
background: #2e7379;
animation: load 900ms ease-in-out infinite alternate;
}
.loading-blocks i:nth-child(2) {
animation-delay: 150ms;
}
.loading-blocks i:nth-child(3) {
animation-delay: 300ms;
}
@keyframes load {
to {
transform: translateY(-9px);
opacity: 0.4;
}
}
@media (max-width: 760px) {
#title-card {
top: 12px;
left: 12px;
width: calc(100vw - 24px);
padding: 14px;
}
#route-card {
top: auto;
right: 12px;
bottom: 86px;
width: 205px;
padding: 12px;
}
#stage-copy {
display: none;
}
#controls {
bottom: 10px;
gap: 5px;
padding: 7px;
overflow-x: auto;
}
.group-label,
#hint {
display: none;
}
button {
min-height: 32px;
padding: 0 9px;
font-size: 10px;
}
#speed {
width: 65px;
}
}
</style>
</head>
<body>
<div id="loading">
<div class="loading-box">
<div class="loading-blocks"><i></i><i></i><i></i></div>
Building the watershed
</div>
</div>
<div id="hud">
<div id="labels"></div>
<section id="title-card" class="panel">
<div class="eyebrow"><span class="live-dot"></span> Live stormwater route</div>
<h1>Three roofs.<br>One shared pond.</h1>
<p class="subtitle">
Follow block-built rain from rooftop catchments, through gutters and a
planted swale, into the retention pond.
</p>
</section>
<section id="route-card" class="panel">
<div class="small-label">Currently following</div>
<div class="route-row">
<div id="stage-name">Rain onto roof</div>
<div id="elevation">0.0 m</div>
</div>
<p id="stage-copy">The route is beginning.</p>
<div class="progress-track"><div id="progress-fill"></div></div>
<div class="progress-caption">
<span>Rooftop</span>
<span>Pond</span>
</div>
</section>
<section id="controls" class="panel">
<div class="group">
<span class="group-label">Route</span>
<button class="route-button active" data-system="0">A</button>
<button class="route-button" data-system="1">B</button>
<button class="route-button" data-system="2">C</button>
</div>
<div class="separator"></div>
<div class="group">
<button id="follow-button" class="active">Follow</button>
<button id="overview-button">Overview</button>
<button id="reset-button" title="Reset camera">Reset</button>
</div>
<div class="separator"></div>
<button id="pause-button">Pause</button>
<div class="group">
<span class="group-label">Speed</span>
<input id="speed" type="range" min="0.25" max="2" step="0.05" value="1">
<span id="speed-value">1.0×</span>
</div>
</section>
<div id="hint" class="panel">Drag to orbit · wheel to zoom · space to pause</div>
</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// -------------------------------------------------------------------------
// Scene setup
// -------------------------------------------------------------------------
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xb9d7da);
scene.fog = new THREE.Fog(0xb9d7da, 34, 72);
const camera = new THREE.PerspectiveCamera(
42,
innerWidth / innerHeight,
0.1,
120
);
const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setSize(innerWidth, innerHeight);
renderer.setPixelRatio(Math.min(devicePixelRatio, 1.8));
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.05;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
document.body.prepend(renderer.domElement);
const hemi = new THREE.HemisphereLight(0xdaf4f2, 0x557b57, 2.1);
scene.add(hemi);
const sun = new THREE.DirectionalLight(0xfff1cf, 3.2);
sun.position.set(-13, 24, 11);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -25;
sun.shadow.camera.right = 25;
sun.shadow.camera.top = 25;
sun.shadow.camera.bottom = -25;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 65;
sun.shadow.bias = -0.00035;
scene.add(sun);
const fill = new THREE.DirectionalLight(0x8bc9d7, 0.65);
fill.position.set(15, 10, -15);
scene.add(fill);
const systems = [
{
id: "A",
name: "West studio",
x: -10,
z: -5,
w: 5,
d: 4,
base: 5.1,
roofY: 8.3,
outletX: -7.35,
frontZ: -2.82,
wall: 0xd6c5a2,
roof: 0x52666b,
duration: 12
},
{
id: "B",
name: "North house",
x: 0,
z: -8,
w: 5,
d: 4,
base: 5.9,
roofY: 9.1,
outletX: 2.35,
frontZ: -5.82,
wall: 0xc3d1c4,
roof: 0x45565e,
duration: 14
},
{
id: "C",
name: "East workshop",
x: 10,
z: -4,
w: 5,
d: 4,
base: 4.7,
roofY: 7.5,
outletX: 12.35,
frontZ: -1.82,
wall: 0xd8b69c,
roof: 0x615a58,
duration: 11.5
}
];
const swales = [
[
{ x: -7.35, z: -2.82 },
{ x: -6.1, z: -1.4 },
{ x: -5.2, z: 1.0 },
{ x: -4.3, z: 3.4 },
{ x: -3.5, z: 5.4 },
{ x: -2.1, z: 7.2 },
{ x: 0, z: 7.8 }
],
[
{ x: 2.35, z: -5.82 },
{ x: 3.0, z: -3.2 },
{ x: 3.25, z: -0.5 },
{ x: 3.05, z: 2.5 },
{ x: 2.2, z: 5.0 },
{ x: 1.3, z: 7.1 },
{ x: 0, z: 7.8 }
],
[
{ x: 12.35, z: -1.82 },
{ x: 11.1, z: 0.1 },
{ x: 9.2, z: 2.1 },
{ x: 7.1, z: 4.0 },
{ x: 4.6, z: 6.0 },
{ x: 2.5, z: 7.3 },
{ x: 0, z: 7.8 }
]
];
// -------------------------------------------------------------------------
// Utility functions
// -------------------------------------------------------------------------
function clamp(v, min, max) {
return Math.max(min, Math.min(max, v));
}
function smoothstep(a, b, x) {
const t = clamp((x - a) / (b - a), 0, 1);
return t * t * (3 - 2 * t);
}
function distanceToSegment2D(x, z, a, b) {
const dx = b.x - a.x;
const dz = b.z - a.z;
const lengthSq = dx * dx + dz * dz;
if (lengthSq === 0) {
return Math.hypot(x - a.x, z - a.z);
}
const t = clamp(
((x - a.x) * dx + (z - a.z) * dz) / lengthSq,
0,
1
);
return Math.hypot(x - (a.x + t * dx), z - (a.z + t * dz));
}
function closestSwaleDistance(x, z) {
let closest = Infinity;
for (const line of swales) {
for (let i = 0; i < line.length - 1; i++) {
closest = Math.min(
closest,
distanceToSegment2D(x, z, line[i], line[i + 1])
);
}
}
return closest;
}
function pondRadius(x, z) {
return Math.hypot(x / 5.4, (z - 7.8) / 3.9);
}
function rawTerrainHeight(x, z) {
let h = 6.15 - 0.145 * (z + 10);
// Broad retention pond depression.
const pr = pondRadius(x, z);
if (pr < 1.15) {
const factor = 1 - smoothstep(0.72, 1.15, pr);
h = THREE.MathUtils.lerp(h, 0.72, factor);
}
// Planted swale cells are cut slightly below the surrounding grade.
const sd = closestSwaleDistance(x, z);
if (sd < 1.35) {
h -= 0.58 * (1 - smoothstep(0.25, 1.35, sd));
}
// Flatten building pads after channel carving.
for (const b of systems) {
const dx = Math.max(Math.abs(x - b.x) - b.w / 2, 0);
const dz = Math.max(Math.abs(z - b.z) - b.d / 2, 0);
const d = Math.hypot(dx, dz);
if (d < 1.1) {
const influence = 1 - smoothstep(0, 1.1, d);
h = THREE.MathUtils.lerp(h, b.base, influence);
}
}
return h;
}
function terrainHeight(x, z) {
return Math.round(rawTerrainHeight(x, z) * 4) / 4;
}
function resamplePolyline(points, spacing = 0.62) {
const lengths = [];
let total = 0;
for (let i = 0; i < points.length - 1; i++) {
const length = Math.hypot(
points[i + 1].x - points[i].x,
points[i + 1].z - points[i].z
);
lengths.push(length);
total += length;
}
const count = Math.max(2, Math.ceil(total / spacing));
const result = [];
for (let i = 0; i <= count; i++) {
const target = (i / count) * total;
let accumulated = 0;
for (let j = 0; j < lengths.length; j++) {
if (target <= accumulated + lengths[j] || j === lengths.length - 1) {
const t = lengths[j] === 0
? 0
: clamp((target - accumulated) / lengths[j], 0, 1);
result.push({
x: THREE.MathUtils.lerp(points[j].x, points[j + 1].x, t),
z: THREE.MathUtils.lerp(points[j].z, points[j + 1].z, t)
});
break;
}
accumulated += lengths[j];
}
}
return result;
}
function addBox(
width,
height,
depth,
x,
y,
z,
material,
parent = scene,
castShadow = true
) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(width, height, depth),
material
);
mesh.position.set(x, y, z);
mesh.castShadow = castShadow;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
// -------------------------------------------------------------------------
// Voxel terrain
// -------------------------------------------------------------------------
const soilMaterial = new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 1,
vertexColors: true
});
const capMaterial = new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 0.96,
vertexColors: true
});
const minX = -18;
const maxX = 18;
const minZ = -12;
const maxZ = 14;
const gridWidth = maxX - minX + 1;
const gridDepth = maxZ - minZ + 1;
const cellCount = gridWidth * gridDepth;
const soil = new THREE.InstancedMesh(
new THREE.BoxGeometry(0.98, 1, 0.98),
soilMaterial,
cellCount
);
const caps = new THREE.InstancedMesh(
new THREE.BoxGeometry(0.98, 0.14, 0.98),
capMaterial,
cellCount
);
const dummy = new THREE.Object3D();
const soilColor = new THREE.Color();
const capColor = new THREE.Color();
let instance = 0;
for (let z = minZ; z <= maxZ; z++) {
for (let x = minX; x <= maxX; x++) {
const h = terrainHeight(x, z);
const soilBottom = -2.3;
const soilTop = h - 0.14;
const soilHeight = soilTop - soilBottom;
dummy.position.set(x, soilBottom + soilHeight / 2, z);
dummy.scale.set(1, soilHeight, 1);
dummy.updateMatrix();
soil.setMatrixAt(instance, dummy.matrix);
const variation = ((x * 17 + z * 31) % 7) / 100;
soilColor.set(0x80654a);
soilColor.offsetHSL(0, 0, variation - 0.025);
soil.setColorAt(instance, soilColor);
dummy.position.set(x, h - 0.07, z);
dummy.scale.set(1, 1, 1);
dummy.updateMatrix();
caps.setMatrixAt(instance, dummy.matrix);
const swaleDistance = closestSwaleDistance(x, z);
const pr = pondRadius(x, z);
if (pr < 0.96) {
capColor.set(0x3e6259);
} else if (swaleDistance < 0.9) {
capColor.set(0x4f7048);
} else {
capColor.set(0x789b5d);
capColor.offsetHSL(0, 0, variation - 0.035);
}
caps.setColorAt(instance, capColor);
instance++;
}
}
soil.instanceMatrix.needsUpdate = true;
soil.instanceColor.needsUpdate = true;
caps.instanceMatrix.needsUpdate = true;
caps.instanceColor.needsUpdate = true;
soil.receiveShadow = true;
caps.receiveShadow = true;
scene.add(soil, caps);
const foundationMaterial = new THREE.MeshStandardMaterial({
color: 0x41584b,
roughness: 1
});
addBox(
42,
1,
34,
0,
-2.8,
1,
foundationMaterial,
scene,
false
);
// -------------------------------------------------------------------------
// Buildings, roofs, gutters, and downspouts
// -------------------------------------------------------------------------
const gutterMaterial = new THREE.MeshStandardMaterial({
color: 0x35464a,
metalness: 0.38,
roughness: 0.43
});
const gutterLipMaterial = new THREE.MeshStandardMaterial({
color: 0x7b9497,
metalness: 0.42,
roughness: 0.36
});
const windowMaterial = new THREE.MeshStandardMaterial({
color: 0x234a57,
metalness: 0.15,
roughness: 0.23
});
const litWindowMaterial = new THREE.MeshStandardMaterial({
color: 0xf4d483,
emissive: 0x9e6d22,
emissiveIntensity: 0.35,
roughness: 0.4
});
const trimMaterial = new THREE.MeshStandardMaterial({
color: 0xe8e0cd,
roughness: 0.85
});
function buildBuilding(b, index) {
const wallMaterial = new THREE.MeshStandardMaterial({
color: b.wall,
roughness: 0.88
});
const roofMaterial = new THREE.MeshStandardMaterial({
color: b.roof,
roughness: 0.78,
vertexColors: true
});
const bodyHeight = b.roofY - 0.25 - b.base;
addBox(
b.w,
bodyHeight,
b.d,
b.x,
b.base + bodyHeight / 2,
b.z,
wallMaterial
);
// Corner pilasters give the walls a voxel-built silhouette.
for (const sx of [-1, 1]) {
for (const sz of [-1, 1]) {
addBox(
0.28,
bodyHeight + 0.08,
0.28,
b.x + sx * (b.w / 2 - 0.12),
b.base + bodyHeight / 2,
b.z + sz * (b.d / 2 - 0.12),
trimMaterial
);
}
}
// Front windows.
for (let i = -1; i <= 1; i++) {
addBox(
0.68,
0.72,
0.09,
b.x + i * 1.28,
b.base + 1.62,
b.z + b.d / 2 + 0.055,
index === 1 && i === 1 ? litWindowMaterial : windowMaterial
);
addBox(
0.78,
0.08,
0.12,
b.x + i * 1.28,
b.base + 1.25,
b.z + b.d / 2 + 0.065,
trimMaterial
);
}
// Side windows.
for (const sx of [-1, 1]) {
addBox(
0.08,
0.68,
0.7,
b.x + sx * (b.w / 2 + 0.045),
b.base + 1.58,
b.z + (sx === -1 ? -0.65 : 0.65),
windowMaterial
);
}
// Door.
addBox(
0.78,
1.65,
0.11,
b.x + 0.1,
b.base + 0.825,
b.z + b.d / 2 + 0.07,
index === 2 ? trimMaterial : gutterMaterial
);
// Individual roof tiles.
const roofTileCount = b.w * b.d;
const roofTiles = new THREE.InstancedMesh(
new THREE.BoxGeometry(0.94, 0.16, 0.94),
roofMaterial,
roofTileCount
);
let tileIndex = 0;
for (let ix = 0; ix < b.w; ix++) {
for (let iz = 0; iz < b.d; iz++) {
dummy.position.set(
b.x - b.w / 2 + ix + 0.5,
b.roofY - 0.08,
b.z - b.d / 2 + iz + 0.5
);
dummy.scale.set(1, 1, 1);
dummy.updateMatrix();
roofTiles.setMatrixAt(tileIndex, dummy.matrix);
const color = new THREE.Color(b.roof);
color.offsetHSL(
0,
0,
(((ix + iz * 2 + index) % 5) - 2) * 0.018
);
roofTiles.setColorAt(tileIndex, color);
tileIndex++;
}
}
roofTiles.instanceMatrix.needsUpdate = true;
roofTiles.instanceColor.needsUpdate = true;
roofTiles.castShadow = true;
roofTiles.receiveShadow = true;
scene.add(roofTiles);
// Front gutter and bright lip.
addBox(
b.w + 0.45,
0.18,
0.34,
b.x,
b.roofY - 0.17,
b.frontZ,
gutterMaterial
);
addBox(
b.w + 0.46,
0.055,
0.37,
b.x,
b.roofY - 0.07,
b.frontZ,
gutterLipMaterial
);
// Square-voxel downspout.
const groundY = rawTerrainHeight(b.outletX, b.frontZ) + 0.1;
const downspoutTop = b.roofY - 0.2;
const downspoutHeight = downspoutTop - groundY;
addBox(
0.17,
downspoutHeight,
0.17,
b.outletX,
groundY + downspoutHeight / 2,
b.frontZ + 0.22,
gutterMaterial
);
addBox(
0.38,
0.17,
0.38,
b.outletX,
downspoutTop,
b.frontZ + 0.22,
gutterMaterial
);
// Small outlet at grade.
addBox(
0.38,
0.16,
0.45,
b.outletX,
groundY + 0.08,
b.frontZ + 0.22,
gutterLipMaterial
);
// Voxel clouds above the roof.
const cloudMaterial = new THREE.MeshStandardMaterial({
color: 0xf3f7ef,
roughness: 1,
transparent: true,
opacity: 0.82,
depthWrite: false
});
for (let i = 0; i < 6; i++) {
const cw = 1.0 + (i % 3) * 0.45;
addBox(
cw,
0.48 + (i % 2) * 0.18,
1.0 + (i % 2) * 0.38,
b.x - 1.7 + i * 0.68,
b.roofY + 4.2 + (i % 3) * 0.28,
b.z - 0.25 + Math.sin(i * 2.1) * 0.4,
cloudMaterial,
scene,
false
);
}
}
systems.forEach(buildBuilding);
// -------------------------------------------------------------------------
// Planted swale: reeds, grasses, and subtle flow markers
// -------------------------------------------------------------------------
const plantLocations = [];
for (const line of swales) {
for (let i = 0; i < line.length - 1; i++) {
const a = line[i];
const b = line[i + 1];
const dx = b.x - a.x;
const dz = b.z - a.z;
const length = Math.hypot(dx, dz);
const count = Math.max(1, Math.floor(length / 0.82));
for (let j = 0; j < count; j++) {
const t = (j + 0.5) / count;
const x = THREE.MathUtils.lerp(a.x, b.x, t);
const z = THREE.MathUtils.lerp(a.z, b.z, t);
const nx = -dz / length;
const nz = dx / length;
for (const side of [-1, 1]) {
if (Math.random() < 0.22) continue;
const offset = 0.55 + Math.random() * 0.5;
const px = x + nx * offset * side;
const pz = z + nz * offset * side;
if (pondRadius(px, pz) < 1.05) continue;
let inBuilding = false;
for (const building of systems) {
if (
Math.abs(px - building.x) < building.w / 2 + 0.3 &&
Math.abs(pz - building.z) < building.d / 2 + 0.3
) {
inBuilding = true;
}
}
if (!inBuilding) {
plantLocations.push({
x: px,
z: pz,
height: 0.38 + Math.random() * 0.72
});
}
}
}
}
}
const stems = new THREE.InstancedMesh(
new THREE.BoxGeometry(0.065, 1, 0.065),
new THREE.MeshStandardMaterial({
color: 0x51783f,
roughness: 1
}),
plantLocations.length
);
const leaves = new THREE.InstancedMesh(
new THREE.BoxGeometry(0.22, 0.1, 0.11),
new THREE.MeshStandardMaterial({
color: 0x7ca85b,
roughness: 1
}),
plantLocations.length * 2
);
plantLocations.forEach((plant, i) => {
const ground = terrainHeight(plant.x, plant.z);
dummy.position.set(plant.x, ground + plant.height / 2, plant.z);
dummy.scale.set(1, plant.height, 1);
dummy.updateMatrix();
stems.setMatrixAt(i, dummy.matrix);
for (let j = 0; j < 2; j++) {
dummy.position.set(
plant.x + (j === 0 ? -0.09 : 0.09),
ground + plant.height * (0.45 + j * 0.22),
plant.z
);
dummy.scale.set(1, 1, 1);
dummy.updateMatrix();
leaves.setMatrixAt(i * 2 + j, dummy.matrix);
}
});
stems.instanceMatrix.needsUpdate = true;
leaves.instanceMatrix.needsUpdate = true;
stems.castShadow = true;
leaves.castShadow = true;
scene.add(stems, leaves);
const flowPipLocations = [];
for (const line of swales) {
const samples = resamplePolyline(line, 1.45);
for (const p of samples) {
flowPipLocations.push(p);
}
}
const flowPips = new THREE.InstancedMesh(
new THREE.BoxGeometry(0.14, 0.08, 0.14),
new THREE.MeshBasicMaterial({
color: 0x68d7e4,
transparent: true,
opacity: 0.28,
depthWrite: false
}),
flowPipLocations.length
);
flowPipLocations.forEach((p, i) => {
dummy.position.set(
p.x,
rawTerrainHeight(p.x, p.z) + 0.48,
p.z
);
dummy.scale.set(1, 1, 1);
dummy.updateMatrix();
flowPips.setMatrixAt(i, dummy.matrix);
});
flowPips.instanceMatrix.needsUpdate = true;
scene.add(flowPips);
// -------------------------------------------------------------------------
// Retention pond
// -------------------------------------------------------------------------
const waterCells = [];
for (let x = -6; x <= 6; x++) {
for (let z = 3; z <= 12; z++) {
const r = pondRadius(x, z);
if (r < 0.88 && terrainHeight(x, z) < 1.85) {
waterCells.push({ x, z, phase: Math.random() * Math.PI * 2 });
}
}
}
const pondWater = new THREE.InstancedMesh(
new THREE.BoxGeometry(0.97, 0.13, 0.97),
new THREE.MeshStandardMaterial({
color: 0x4ab6c7,
emissive: 0x0b5360,
emissiveIntensity: 0.28,
metalness: 0.08,
roughness: 0.24,
transparent: true,
opacity: 0.84,
depthWrite: false
}),
waterCells.length
);
pondWater.instanceMatrix.setUsage(THREE.DynamicDrawUsage);
pondWater.renderOrder = 2;
scene.add(pondWater);
const rockLocations = [];
for (let i = 0; i < 54; i++) {
const angle = (i / 54) * Math.PI * 2 + Math.random() * 0.08;
const r = 0.95 + Math.random() * 0.2;
const x = Math.cos(angle) * 5.4 * r;
const z = 7.8 + Math.sin(angle) * 3.9 * r;
rockLocations.push({
x,
z,
size: 0.18 + Math.random() * 0.25,
rotation: Math.floor(Math.random() * 4) * Math.PI / 2
});
}
const rocks = new THREE.InstancedMesh(
new THREE.BoxGeometry(1, 1, 1),
new THREE.MeshStandardMaterial({
color: 0x7b8078,
roughness: 1
}),
rockLocations.length
);
rockLocations.forEach((rock, i) => {
dummy.position.set(
rock.x,
terrainHeight(rock.x, rock.z) + rock.size / 2,
rock.z
);
dummy.rotation.set(0, rock.rotation, 0);
dummy.scale.set(
rock.size * 1.25,
rock.size,
rock.size * 0.95
);
dummy.updateMatrix();
rocks.setMatrixAt(i, dummy.matrix);
});
rocks.instanceMatrix.needsUpdate = true;
rocks.castShadow = true;
rocks.receiveShadow = true;
scene.add(rocks);
// Square voxel ripple rings.
const ripples = [];
for (let i = 0; i < 3; i++) {
const material = new THREE.MeshBasicMaterial({
color: 0xc5f7f6,
transparent: true,
opacity: 0.2,
depthWrite: false
});
const group = new THREE.Group();
group.position.set(0, 1.56, 7.8);
addBox(1, 0.025, 0.045, 0, 0, 0.5, material, group, false);
addBox(1, 0.025, 0.045, 0, 0, -0.5, material, group, false);
addBox(0.045, 0.025, 1, 0.5, 0, 0, material, group, false);
addBox(0.045, 0.025, 1, -0.5, 0, 0, material, group, false);
group.userData.material = material;
group.userData.offset = i / 3;
scene.add(group);
ripples.push(group);
}
// -------------------------------------------------------------------------
// Route paths and animated voxel raindrops
// -------------------------------------------------------------------------
function createRoute(b, index) {
const points = [];
points.push({
p: new THREE.Vector3(b.x - 0.6, b.roofY + 3.2, b.z - 0.7),
stage: "Rain onto roof"
});
points.push({
p: new THREE.Vector3(b.x - 0.6, b.roofY + 0.38, b.z - 0.7),
stage: "Rain onto roof"
});
points.push({
p: new THREE.Vector3(
b.outletX,
b.roofY + 0.35,
b.frontZ - 0.45
),
stage: "Roof runoff"
});
points.push({
p: new THREE.Vector3(
b.outletX,
b.roofY + 0.22,
b.frontZ + 0.2
),
stage: "Gutter"
});
points.push({
p: new THREE.Vector3(
b.outletX,
b.roofY - 0.35,
b.frontZ + 0.22
),
stage: "Gutter"
});
points.push({
p: new THREE.Vector3(
b.outletX,
rawTerrainHeight(b.outletX, b.frontZ) + 0.42,
b.frontZ + 0.22
),
stage: "Downspout"
});
const samples = resamplePolyline(swales[index], 0.58);
for (const sample of samples) {
const distance = Math.hypot(
sample.x - points[points.length - 1].p.x,
sample.z - points[points.length - 1].p.z
);
if (distance < 0.06) continue;
points.push({
p: new THREE.Vector3(
sample.x,
rawTerrainHeight(sample.x, sample.z) + 0.42,
sample.z
),
stage: pondRadius(sample.x, sample.z) < 1.05
? "Retention pond"
: "Planted swale"
});
}
points.push({
p: new THREE.Vector3(0, 1.03, 7.8),
stage: "Retention pond"
});
const lengths = [];
let total = 0;
for (let i = 0; i < points.length - 1; i++) {
const length = points[i].p.distanceTo(points[i + 1].p);
lengths.push(length);
total += length;
}
return { points, lengths, total };
}
function sampleRoute(route, t, output) {
t = ((t % 1) + 1) % 1;
const target = t * route.total;
let accumulated = 0;
for (let i = 0; i < route.lengths.length; i++) {
const length = route.lengths[i];
if (target <= accumulated + length || i === route.lengths.length - 1) {
const localT = length === 0
? 0
: clamp((target - accumulated) / length, 0, 1);
output.p.lerpVectors(
route.points[i].p,
route.points[i + 1].p,
localT
);
output.stage = route.points[i].stage;
return output;
}
accumulated += length;
}
output.p.copy(route.points[route.points.length - 1].p);
output.stage = "Retention pond";
return output;
}
const dropGeometry = new THREE.BoxGeometry(0.2, 0.2, 0.2);
const heroGeometry = new THREE.BoxGeometry(0.31, 0.31, 0.31);
const trailGeometry = new THREE.BoxGeometry(0.1, 0.1, 0.1);
const dropMaterial = new THREE.MeshStandardMaterial({
color: 0x4eb9d0,
emissive: 0x135568,
emissiveIntensity: 0.45,
roughness: 0.25,
metalness: 0.05
});
const activeHeroMaterial = new THREE.MeshStandardMaterial({
color: 0x9af5ff,
emissive: 0x28c7e1,
emissiveIntensity: 1.35,
roughness: 0.18,
metalness: 0.02
});
const quietHeroMaterial = new THREE.MeshStandardMaterial({
color: 0x70d9e7,
emissive: 0x176776,
emissiveIntensity: 0.65,
roughness: 0.22
});
const trailMaterial = new THREE.MeshBasicMaterial({
color: 0x8beaf2,
transparent: true,
opacity: 0.32,
depthWrite: false
});
const routeData = systems.map((b, i) => {
const route = createRoute(b, i);
const drops = [];
const dropCount = 12;
for (let j = 0; j < dropCount; j++) {
const isHero = j === 0;
const mesh = new THREE.Mesh(
isHero ? heroGeometry : dropGeometry,
isHero ? quietHeroMaterial : dropMaterial
);
mesh.castShadow = true;
scene.add(mesh);
const trails = [];
for (let k = 0; k < 3; k++) {
const trail = new THREE.Mesh(trailGeometry, trailMaterial);
trail.renderOrder = 3;
scene.add(trail);
trails.push(trail);
}
drops.push({ mesh, trails });
}
return { route, drops, heroSample: { p: new THREE.Vector3(), stage: "" } };
});
const heroLight = new THREE.PointLight(0x65e6f2, 1.1, 4.5, 2);
scene.add(heroLight);
// -------------------------------------------------------------------------
// Ambient rainfall over the rooftop catchments
// -------------------------------------------------------------------------
const rainCount = 54;
const rain = new THREE.InstancedMesh(
new THREE.BoxGeometry(0.045, 0.32, 0.045),
new THREE.MeshBasicMaterial({
color: 0x8edbe7,
transparent: true,
opacity: 0.48,
depthWrite: false
}),
rainCount
);
rain.instanceMatrix.setUsage(THREE.DynamicDrawUsage);
scene.add(rain);
const rainData = [];
for (let i = 0; i < rainCount; i++) {
const b = systems[i % systems.length];
rainData.push({
b,
x: b.x + (Math.random() - 0.5) * (b.w + 1.7),
z: b.z + (Math.random() - 0.5) * (b.d + 1.7),
y: b.roofY + 2 + Math.random() * 4.5,
speed: 5 + Math.random() * 3.5
});
}
// -------------------------------------------------------------------------
// Custom camera controls and follow rig
// -------------------------------------------------------------------------
const rig = {
target: new THREE.Vector3(0, 2.2, 0.2),
yaw: 0.58,
pitch: 0.72,
radius: 36
};
const overviewTarget = new THREE.Vector3(0, 2.2, 0.2);
let cameraMode = "follow";
let selectedSystem = 0;
let playing = true;
let simulationTime = 0;
let playbackSpeed = 1;
let dragging = false;
let lastPointerX = 0;
let lastPointerY = 0;
const canvas = renderer.domElement;
canvas.addEventListener("pointerdown", (event) => {
dragging = true;
lastPointerX = event.clientX;
lastPointerY = event.clientY;
canvas.setPointerCapture(event.pointerId);
canvas.style.cursor = "grabbing";
});
canvas.addEventListener("pointermove", (event) => {
if (!dragging) return;
const dx = event.clientX - lastPointerX;
const dy = event.clientY - lastPointerY;
lastPointerX = event.clientX;
lastPointerY = event.clientY;
rig.yaw -= dx * 0.005;
rig.pitch = clamp(rig.pitch + dy * 0.004, 0.24, 1.35);
});
function endDrag(event) {
dragging = false;
canvas.style.cursor = "grab";
if (event.pointerId !== undefined && canvas.hasPointerCapture(event.pointerId)) {
canvas.releasePointerCapture(event.pointerId);
}
}
canvas.addEventListener("pointerup", endDrag);
canvas.addEventListener("pointercancel", endDrag);
canvas.style.cursor = "grab";
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
rig.radius = clamp(
rig.radius * Math.exp(event.deltaY * 0.001),
3.8,
48
);
},
{ passive: false }
);
// -------------------------------------------------------------------------
// UI and projected labels
// -------------------------------------------------------------------------
const stageName = document.getElementById("stage-name");
const stageCopy = document.getElementById("stage-copy");
const elevation = document.getElementById("elevation");
const progressFill = document.getElementById("progress-fill");
const followButton = document.getElementById("follow-button");
const overviewButton = document.getElementById("overview-button");
const pauseButton = document.getElementById("pause-button");
const speedInput = document.getElementById("speed");
const speedValue = document.getElementById("speed-value");
const routeButtons = [...document.querySelectorAll(".route-button")];
const stageDescriptions = {
"Rain onto roof": "A voxel raindrop lands on the roof and enters the local catchment.",
"Roof runoff": "Surface water crosses the roof toward the front-edge collection gutter.",
"Gutter": "The gutter gathers roof runoff and directs it toward the corner downspout.",
"Downspout": "A square downspout carries the concentrated flow down to grade.",
"Planted swale": "Reed-lined cells slow the runoff and guide it downhill toward the pond.",
"Retention pond": "The rooftop route enters the shared retention pool."
};
function setCameraMode(mode) {
cameraMode = mode;
if (mode === "overview") {
rig.radius = 36;
rig.pitch = 0.72;
rig.yaw = 0.58;
} else {
rig.radius = 8.5;
rig.pitch = 0.62;
rig.yaw = 0.58;
}
followButton.classList.toggle("active", mode === "follow");
overviewButton.classList.toggle("active", mode === "overview");
}
function selectSystem(index) {
selectedSystem = index;
routeButtons.forEach((button, i) => {
button.classList.toggle("active", i === index);
});
routeData.forEach((data, i) => {
data.drops[0].mesh.material =
i === selectedSystem ? activeHeroMaterial : quietHeroMaterial;
});
}
routeButtons.forEach((button) => {
button.addEventListener("click", () => {
selectSystem(Number(button.dataset.system));
});
});
followButton.addEventListener("click", () => setCameraMode("follow"));
overviewButton.addEventListener("click", () => setCameraMode("overview"));
document.getElementById("reset-button").addEventListener("click", () => {
setCameraMode(cameraMode);
});
pauseButton.addEventListener("click", () => {
playing = !playing;
pauseButton.textContent = playing ? "Pause" : "Resume";
});
speedInput.addEventListener("input", () => {
playbackSpeed = Number(speedInput.value);
speedValue.textContent = `${playbackSpeed.toFixed(1)}×`;
});
addEventListener("keydown", (event) => {
if (event.code === "Space") {
event.preventDefault();
pauseButton.click();
}
if (event.key === "1") selectSystem(0);
if (event.key === "2") selectSystem(1);
if (event.key === "3") selectSystem(2);
if (event.key.toLowerCase() === "o") setCameraMode("overview");
if (event.key.toLowerCase() === "f") setCameraMode("follow");
});
function createWorldLabel(text, className = "") {
const element = document.createElement("div");
element.className = `world-label ${className}`;
element.textContent = text;
document.getElementById("labels").appendChild(element);
return element;
}
const labels = systems.map((b) =>
createWorldLabel(`Roof ${b.id}`, "cyan")
);
const swaleLabel = createWorldLabel("Planted swale", "green");
const pondLabel = createWorldLabel("Retention pond", "cyan");
const projection = new THREE.Vector3();
function positionLabel(element, worldPosition) {
projection.copy(worldPosition).project(camera);
const visible =
projection.z > -1 &&
projection.z < 1 &&
Math.abs(projection.x) < 1.15 &&
Math.abs(projection.y) < 1.15;
element.style.display = visible ? "block" : "none";
if (!visible) return;
const x = (projection.x * 0.5 + 0.5) * innerWidth;
const y = (-projection.y * 0.5 + 0.5) * innerHeight;
element.style.left = `${x}px`;
element.style.top = `${y}px`;
}
// -------------------------------------------------------------------------
// Animation
// -------------------------------------------------------------------------
let previousTime = performance.now();
let firstFrame = true;
function animate(now) {
requestAnimationFrame(animate);
const dt = Math.min((now - previousTime) / 1000, 0.05);
previousTime = now;
if (playing) {
simulationTime += dt * playbackSpeed;
}
const selected = routeData[selectedSystem];
const selectedProgress =
(simulationTime / systems[selectedSystem].duration) % 1;
sampleRoute(selected.route, selectedProgress, selected.heroSample);
// Update all route droplets.
routeData.forEach((data, systemIndex) => {
const duration = systems[systemIndex].duration;
data.drops.forEach((drop, dropIndex) => {
const progress =
(simulationTime / duration + dropIndex / data.drops.length) % 1;
const sample = sampleRoute(
data.route,
progress,
{ p: new THREE.Vector3(), stage: "" }
);
drop.mesh.position.copy(sample.p);
const snap = Math.floor(simulationTime * 2 + dropIndex) % 4;
drop.mesh.rotation.y = snap * Math.PI / 2;
drop.trails.forEach((trail, trailIndex) => {
const trailSample = sampleRoute(
data.route,
progress - (trailIndex + 1) * 0.0065,
{ p: new THREE.Vector3(), stage: "" }
);
trail.position.copy(trailSample.p);
const scale = 1 - (trailIndex + 1) * 0.22;
trail.scale.setScalar(scale);
});
});
});
// Active drop light.
heroLight.position.copy(selected.heroSample.p);
heroLight.position.y += 0.25;
// Update rain.
rainData.forEach((drop, i) => {
drop.y -= drop.speed * dt * playbackSpeed;
const overRoof =
Math.abs(drop.x - drop.b.x) < drop.b.w / 2 &&
Math.abs(drop.z - drop.b.z) < drop.b.d / 2;
const stopHeight = overRoof
? drop.b.roofY
: terrainHeight(drop.x, drop.z);
if (drop.y < stopHeight + 0.05) {
drop.x = drop.b.x + (Math.random() - 0.5) * (drop.b.w + 1.7);
drop.z = drop.b.z + (Math.random() - 0.5) * (drop.b.d + 1.7);
drop.y = drop.b.roofY + 2.5 + Math.random() * 4;
}
dummy.position.set(drop.x, drop.y, drop.z);
dummy.rotation.set(0, 0, 0);
dummy.scale.set(1, 1, 1);
dummy.updateMatrix();
rain.setMatrixAt(i, dummy.matrix);
});
rain.instanceMatrix.needsUpdate = true;
// Animate pond tiles.
waterCells.forEach((cell, i) => {
dummy.position.set(
cell.x,
1.46 + Math.sin(simulationTime * 1.6 + cell.phase) * 0.018,
cell.z
);
dummy.rotation.set(0, 0, 0);
dummy.scale.set(1, 1, 1);
dummy.updateMatrix();
pondWater.setMatrixAt(i, dummy.matrix);
});
pondWater.instanceMatrix.needsUpdate = true;
// Square ripple expansion.
ripples.forEach((ripple) => {
const phase = (simulationTime * 0.33 + ripple.userData.offset) % 1;
const scale = 0.5 + phase * 2.6;
ripple.scale.set(scale, 1, scale);
ripple.userData.material.opacity = (1 - phase) * 0.28;
});
// Camera target follows the selected hero droplet in follow mode.
const desiredTarget =
cameraMode === "follow"
? selected.heroSample.p.clone().add(new THREE.Vector3(0, 0.35, 0))
: overviewTarget;
const targetEase = 1 - Math.exp(-dt * (cameraMode === "follow" ? 5 : 3));
rig.target.lerp(desiredTarget, targetEase);
const horizontalRadius = Math.cos(rig.pitch) * rig.radius;
camera.position.set(
rig.target.x + Math.sin(rig.yaw) * horizontalRadius,
rig.target.y + Math.cos(rig.pitch) * rig.radius,
rig.target.z + Math.cos(rig.yaw) * horizontalRadius
);
camera.lookAt(rig.target);
camera.updateMatrixWorld();
// Route status.
stageName.textContent = selected.heroSample.stage;
stageCopy.textContent = stageDescriptions[selected.heroSample.stage];
elevation.textContent = `${Math.max(0, selected.heroSample.p.y).toFixed(1)} m`;
progressFill.style.width = `${selectedProgress * 100}%`;
// Projected labels.
systems.forEach((b, i) => {
positionLabel(
labels[i],
new THREE.Vector3(b.x, b.roofY + 1.05, b.z)
);
});
positionLabel(
swaleLabel,
new THREE.Vector3(
-4.4,
rawTerrainHeight(-4.4, 3.4) + 1.25,
3.4
)
);
positionLabel(
pondLabel,
new THREE.Vector3(0, 2.65, 7.8)
);
renderer.render(scene, camera);
if (firstFrame) {
firstFrame = false;
document.getElementById("loading").classList.add("out");
}
}
addEventListener("resize", () => {
camera.aspect = innerWidth / innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(innerWidth, innerHeight);
renderer.setPixelRatio(Math.min(devicePixelRatio, 1.8));
});
selectSystem(0);
setCameraMode("follow");
requestAnimationFrame(animate);
</script>
</body>
</html>
```