Prompt specification
Build or customize the Three.js 3D scene "Saturated Versus Dry Initial Roof" 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: Saturated Versus Dry Initial Roof
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/saturated-versus-dry-initial-roof-x3940
Scene Description Prompt:
Make a standalone HTML scene with Three.js from cdnjs that depicts green-roof layers and rainwater retention. Compare identical synthetic rainfall on two roof-storage models with different initial water contents. Display runoff delay and total discharge under the same declared rules. Voxel style.
Working Three.js Starter (saturated-versus-dry-initial-roof-x3940):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Green Roof Retention Lab</title>
<style>
:root {
--bg: #07131d;
--panel: rgba(10, 24, 35, 0.88);
--line: rgba(163, 205, 220, 0.22);
--text: #edf7f6;
--muted: #91aab3;
--cyan: #4dd7ed;
--amber: #ffb24d;
--green: #76c66c;
--blue: #37a9e8;
}
* { box-sizing: border-box; }
html, body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: var(--bg);
color: var(--text);
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;
}
button {
font: inherit;
}
#ui {
position: fixed;
inset: 0;
pointer-events: none;
}
.panel {
position: absolute;
background: var(--panel);
border: 1px solid var(--line);
box-shadow: 0 12px 36px rgba(0, 0, 0, 0.24);
backdrop-filter: blur(12px);
}
#topbar {
top: 14px;
left: 14px;
right: 14px;
min-height: 62px;
display: flex;
align-items: center;
justify-content: space-between;
gap: 16px;
padding: 10px 12px 10px 16px;
pointer-events: auto;
}
.brand {
display: flex;
align-items: center;
gap: 12px;
min-width: 260px;
}
.brand-mark {
width: 32px;
height: 32px;
display: grid;
grid-template-columns: repeat(4, 1fr);
grid-template-rows: repeat(4, 1fr);
gap: 2px;
transform: rotate(-2deg);
}
.brand-mark i {
background: var(--green);
}
.brand-mark i:nth-child(1),
.brand-mark i:nth-child(6),
.brand-mark i:nth-child(8),
.brand-mark i:nth-child(12),
.brand-mark i:nth-child(15) {
background: var(--blue);
}
.brand-mark i:nth-child(3),
.brand-mark i:nth-child(5),
.brand-mark i:nth-child(10),
.brand-mark i:nth-child(14) {
background: #a87945;
}
.eyebrow {
color: var(--muted);
font-size: 10px;
line-height: 1;
font-weight: 800;
letter-spacing: 0.18em;
text-transform: uppercase;
}
.brand-name {
margin-top: 5px;
font-size: 14px;
font-weight: 850;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.transport {
display: flex;
align-items: center;
gap: 8px;
}
.control {
pointer-events: auto;
min-height: 36px;
border: 1px solid var(--line);
background: rgba(255, 255, 255, 0.035);
color: var(--text);
padding: 0 13px;
cursor: pointer;
font-size: 11px;
font-weight: 800;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.control:hover {
background: rgba(77, 215, 237, 0.1);
border-color: rgba(77, 215, 237, 0.55);
}
.control.primary {
min-width: 88px;
color: #06141c;
background: var(--cyan);
border-color: var(--cyan);
}
.speed-group {
display: flex;
border: 1px solid var(--line);
}
.speed-group .control {
min-height: 34px;
min-width: 43px;
border: 0;
border-right: 1px solid var(--line);
padding: 0 9px;
}
.speed-group .control:last-child {
border-right: 0;
}
.speed-group .control.active {
background: rgba(77, 215, 237, 0.17);
color: var(--cyan);
}
.clock-wrap {
min-width: 154px;
text-align: right;
}
.clock {
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
font-size: 20px;
font-weight: 800;
letter-spacing: 0.06em;
}
#stormStatus {
margin-top: 3px;
color: var(--muted);
font-size: 9px;
font-weight: 800;
letter-spacing: 0.13em;
text-transform: uppercase;
}
#overview {
top: 88px;
left: 14px;
width: 330px;
padding: 17px;
}
h1 {
margin: 7px 0 8px;
font-size: 25px;
line-height: 1.08;
letter-spacing: -0.03em;
}
.intro {
margin: 0;
color: var(--muted);
font-size: 12px;
line-height: 1.55;
}
.timeline {
position: relative;
height: 10px;
margin-top: 17px;
background: rgba(255, 255, 255, 0.07);
border: 1px solid rgba(255, 255, 255, 0.08);
}
#timelineFill {
width: 0%;
height: 100%;
background: linear-gradient(90deg, var(--cyan), var(--blue));
}
.timeline-marker {
position: absolute;
top: -4px;
width: 2px;
height: 16px;
background: var(--amber);
}
.timeline-marker.a { left: 40%; }
.timeline-marker.rain { left: 80%; background: #d9e5e4; }
.timeline-marker.end { left: 100%; background: #61737c; }
.timeline-caption {
display: flex;
justify-content: space-between;
margin-top: 8px;
color: var(--muted);
font-size: 9px;
font-weight: 750;
letter-spacing: 0.07em;
text-transform: uppercase;
}
#rules {
top: 88px;
right: 14px;
width: 320px;
padding: 17px;
}
.panel-title {
margin-bottom: 12px;
font-size: 11px;
font-weight: 900;
letter-spacing: 0.15em;
text-transform: uppercase;
}
.rules-list {
margin: 0;
padding: 0;
list-style: none;
}
.rules-list li {
position: relative;
margin: 0;
padding: 8px 0 8px 17px;
border-top: 1px solid rgba(255, 255, 255, 0.06);
color: #c7d6da;
font-size: 11px;
line-height: 1.45;
}
.rules-list li::before {
content: "";
position: absolute;
top: 14px;
left: 1px;
width: 6px;
height: 6px;
background: var(--cyan);
}
.rules-list strong {
color: var(--text);
}
#comparison {
left: 14px;
bottom: 14px;
width: min(720px, calc(100vw - 28px));
padding: 14px;
pointer-events: auto;
}
.comparison-head {
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: 11px;
}
.comparison-head .panel-title { margin: 0; }
.identical {
color: var(--muted);
font-size: 9px;
font-weight: 800;
letter-spacing: 0.12em;
text-transform: uppercase;
}
.compare-grid {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 10px;
}
.model-card {
position: relative;
min-width: 0;
padding: 13px;
background: rgba(255, 255, 255, 0.025);
border: 1px solid rgba(255, 255, 255, 0.08);
border-top: 3px solid var(--cyan);
}
.model-card.b { border-top-color: var(--amber); }
.model-top {
display: flex;
align-items: center;
justify-content: space-between;
gap: 10px;
}
.model-id {
display: flex;
align-items: center;
gap: 8px;
font-size: 13px;
font-weight: 900;
}
.model-id .chip {
display: inline-grid;
place-items: center;
width: 24px;
height: 24px;
color: #07131d;
background: var(--cyan);
font-size: 12px;
}
.b .model-id .chip { background: var(--amber); }
.model-state {
color: var(--muted);
font-size: 9px;
font-weight: 900;
letter-spacing: 0.12em;
text-transform: uppercase;
}
.model-state.flowing { color: var(--blue); }
.model-state.ended { color: var(--muted); }
.metric-row {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 8px;
margin-top: 13px;
}
.metric-label {
color: var(--muted);
font-size: 8px;
font-weight: 850;
letter-spacing: 0.11em;
text-transform: uppercase;
}
.metric-value {
margin-top: 4px;
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
font-size: 17px;
font-weight: 850;
white-space: nowrap;
}
.metric-value.liters {
color: var(--cyan);
}
.b .metric-value.liters { color: var(--amber); }
.storage-row {
display: grid;
grid-template-columns: 66px 1fr 46px;
align-items: center;
gap: 8px;
margin-top: 12px;
color: var(--muted);
font-size: 9px;
font-weight: 800;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.storage-bar {
height: 7px;
overflow: hidden;
background: rgba(255, 255, 255, 0.07);
}
.storage-fill {
width: 0;
height: 100%;
background: var(--cyan);
}
.b .storage-fill { background: var(--amber); }
.balance {
margin: 11px 1px 0;
color: var(--muted);
font-size: 9px;
line-height: 1.45;
}
#legend {
right: 14px;
bottom: 14px;
width: 236px;
padding: 14px;
}
.legend-grid {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 9px;
}
.legend-item {
display: flex;
align-items: center;
gap: 7px;
color: #bdcdd1;
font-size: 9px;
font-weight: 750;
letter-spacing: 0.05em;
text-transform: uppercase;
}
.swatch {
width: 12px;
height: 12px;
flex: 0 0 auto;
background: var(--green);
}
.swatch.soil { background: #9b7049; }
.swatch.filter { background: #68757b; }
.swatch.water { background: var(--blue); }
.camera-hint {
margin-top: 11px;
color: var(--muted);
font-size: 9px;
line-height: 1.5;
}
.world-label {
position: absolute;
z-index: 3;
min-width: 155px;
padding: 9px 11px;
transform: translate(-50%, -100%);
background: rgba(6, 18, 27, 0.9);
border: 1px solid rgba(77, 215, 237, 0.55);
box-shadow: 0 8px 22px rgba(0, 0, 0, 0.25);
pointer-events: none;
}
.world-label.b {
border-color: rgba(255, 178, 77, 0.6);
}
.world-label strong {
display: block;
font-size: 11px;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.world-label span {
display: block;
margin-top: 3px;
color: var(--muted);
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
font-size: 10px;
}
@media (max-width: 1000px) {
#rules, #legend { display: none; }
#overview { width: 300px; }
.brand { min-width: 190px; }
.brand-name { font-size: 11px; }
}
@media (max-width: 720px) {
#topbar {
flex-wrap: wrap;
gap: 8px;
}
.brand { min-width: 0; flex: 1; }
.clock-wrap { min-width: 105px; }
.clock { font-size: 16px; }
.transport { order: 3; width: 100%; }
.transport .control { flex: 1; }
#overview { top: 126px; width: calc(100vw - 28px); }
#overview h1 { font-size: 20px; }
.intro { display: none; }
#comparison { bottom: 10px; left: 10px; width: calc(100vw - 20px); }
.metric-row { gap: 4px; }
.metric-value { font-size: 14px; }
.model-card { padding: 10px; }
.balance { display: none; }
.world-label { min-width: 130px; transform: translate(-50%, -100%) scale(.85); }
}
</style>
</head>
<body>
<div id="ui">
<div id="topbar" class="panel">
<div class="brand">
<div class="brand-mark" aria-hidden="true">
<i></i><i></i><i></i><i></i>
<i></i><i></i><i></i><i></i>
<i></i><i></i><i></i><i></i>
<i></i><i></i><i></i><i></i>
</div>
<div>
<div class="eyebrow">Water systems laboratory</div>
<div class="brand-name">Green Roof / Retention Test</div>
</div>
</div>
<div class="transport">
<button id="playButton" class="control primary">Pause</button>
<button id="resetButton" class="control">Reset</button>
<div class="speed-group" aria-label="Simulation speed">
<button class="control speed" data-speed="2">2×</button>
<button class="control speed active" data-speed="4">4×</button>
<button class="control speed" data-speed="8">8×</button>
</div>
</div>
<div class="clock-wrap">
<div id="clock" class="clock">00:00</div>
<div id="stormStatus">Rainfall active</div>
</div>
</div>
<section id="overview" class="panel">
<div class="eyebrow">Controlled comparison · identical geometry</div>
<h1>Different starting water. Same storm.</h1>
<p class="intro">
Both roofs receive the same synthetic rainfall. The only changed input is
the water already held in the roof-storage layer.
</p>
<div class="timeline">
<div id="timelineFill"></div>
<div class="timeline-marker a" title="Model A begins runoff"></div>
<div class="timeline-marker rain" title="Rain ends"></div>
<div class="timeline-marker end" title="Observation ends"></div>
</div>
<div class="timeline-caption">
<span>Start</span>
<span>A runoff: 30 min</span>
<span>Rain ends: 60 min</span>
</div>
</section>
<aside id="rules" class="panel">
<div class="panel-title">Declared rules</div>
<ul class="rules-list">
<li><strong>Storm:</strong> 60 mm in 60 min, or 1 mm/min, on both roofs.</li>
<li><strong>Catchment:</strong> 24 m² for each model; 1 mm equals 24 L.</li>
<li><strong>Storage:</strong> integrated green-roof reservoir, capacity 40 mm = 960 L.</li>
<li><strong>Outlet:</strong> runoff occurs only when storage reaches capacity; excess input discharges immediately.</li>
<li><strong>No losses:</strong> no evaporation, lateral seepage, or scheduled drain during the test.</li>
<li><strong>Observation:</strong> 15 dry minutes follow the storm; retained water remains stored.</li>
</ul>
</aside>
<section id="comparison" class="panel">
<div class="comparison-head">
<div class="panel-title">Live model comparison</div>
<div class="identical">Same rainfall · same capacity · same outlet rules</div>
</div>
<div class="compare-grid">
<article class="model-card a">
<div class="model-top">
<div class="model-id"><span class="chip">A</span> Dry-start</div>
<div id="aState" class="model-state">Waiting</div>
</div>
<div class="metric-row">
<div>
<div class="metric-label">Initial</div>
<div class="metric-value">240 L</div>
</div>
<div>
<div class="metric-label">First runoff</div>
<div class="metric-value">30 min</div>
</div>
<div>
<div class="metric-label">Discharged</div>
<div id="aDischarge" class="metric-value liters">0 L</div>
</div>
</div>
<div class="storage-row">
<span>Stored</span>
<div class="storage-bar"><div id="aStorage" class="storage-fill"></div></div>
<span id="aStorageText">240 L</span>
</div>
</article>
<article class="model-card b">
<div class="model-top">
<div class="model-id"><span class="chip">B</span> Pre-wet</div>
<div id="bState" class="model-state">Ready</div>
</div>
<div class="metric-row">
<div>
<div class="metric-label">Initial</div>
<div class="metric-value">960 L</div>
</div>
<div>
<div class="metric-label">First runoff</div>
<div class="metric-value">0 min</div>
</div>
<div>
<div class="metric-label">Discharged</div>
<div id="bDischarge" class="metric-value liters">0 L</div>
</div>
</div>
<div class="storage-row">
<span>Stored</span>
<div class="storage-bar"><div id="bStorage" class="storage-fill"></div></div>
<span id="bStorageText">960 L</span>
</div>
</article>
</div>
<div class="balance">
Final event totals — A: 720 L discharged / 960 L retained;
B: 1,440 L discharged / 960 L retained.
</div>
</section>
<aside id="legend" class="panel">
<div class="panel-title">Roof section</div>
<div class="legend-grid">
<div class="legend-item"><i class="swatch"></i>Vegetation</div>
<div class="legend-item"><i class="swatch soil"></i>Soil</div>
<div class="legend-item"><i class="swatch filter"></i>Filter</div>
<div class="legend-item"><i class="swatch water"></i>Storage</div>
</div>
<div class="camera-hint">
Drag to orbit · wheel to zoom · space to pause.<br>
Blue blocks in the lower basins show cumulative discharge.
</div>
</aside>
<div id="labelA" class="world-label">
<strong>A · Dry-start</strong>
<span>10 mm / 240 L initial</span>
</div>
<div id="labelB" class="world-label b">
<strong>B · Pre-wet</strong>
<span>40 mm / 960 L initial</span>
</div>
</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
/* ------------------------------------------------------------------
Declared physical model
------------------------------------------------------------------ */
const AREA_M2 = 24;
const CAPACITY_MM = 40;
const INITIAL_A_MM = 10;
const INITIAL_B_MM = 40;
const RAIN_END_MIN = 60;
const OBSERVATION_END_MIN = 75;
const RAIN_RATE_MM_PER_MIN = 1;
const litersPerMm = AREA_M2; // 1 mm over 24 m² = 24 L
let simTime = 0;
let visualTime = 0;
let speed = 4;
let playing = true;
/* ------------------------------------------------------------------
Renderer, scene and camera
------------------------------------------------------------------ */
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x07131d);
scene.fog = new THREE.Fog(0x07131d, 24, 48);
const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.08;
document.body.prepend(renderer.domElement);
const camera = new THREE.PerspectiveCamera(
38,
window.innerWidth / window.innerHeight,
0.1,
100
);
const cameraTarget = new THREE.Vector3(0, 2.4, 0.7);
let cameraTheta = 0.38;
let cameraPhi = 0.82;
let cameraRadius = 24;
function updateCamera() {
const sinPhi = Math.sin(cameraPhi);
camera.position.set(
cameraTarget.x + cameraRadius * sinPhi * Math.sin(cameraTheta),
cameraTarget.y + cameraRadius * Math.cos(cameraPhi),
cameraTarget.z + cameraRadius * sinPhi * Math.cos(cameraTheta)
);
camera.lookAt(cameraTarget);
}
updateCamera();
scene.add(new THREE.HemisphereLight(0xbfe8f3, 0x26342a, 1.55));
const sun = new THREE.DirectionalLight(0xfff1d3, 2.4);
sun.position.set(-9, 16, 11);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -18;
sun.shadow.camera.right = 18;
sun.shadow.camera.top = 16;
sun.shadow.camera.bottom = -14;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 45;
sun.shadow.bias = -0.0008;
scene.add(sun);
const rimLight = new THREE.DirectionalLight(0x4db9ff, 0.65);
rimLight.position.set(12, 7, -12);
scene.add(rimLight);
/* ------------------------------------------------------------------
Shared geometry helpers
------------------------------------------------------------------ */
const unitBox = new THREE.BoxGeometry(1, 1, 1);
const dummy = new THREE.Object3D();
function standardMaterial(color, options = {}) {
return new THREE.MeshStandardMaterial({
color,
roughness: 0.82,
metalness: 0.02,
...options
});
}
function box(parent, w, h, d, color, x, y, z, material = null) {
const mesh = new THREE.Mesh(
unitBox,
material || standardMaterial(color)
);
mesh.scale.set(w, h, d);
mesh.position.set(x, y, z);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function seededRandom(seed) {
let value = seed >>> 0;
return () => {
value = (1664525 * value + 1013904223) >>> 0;
return value / 4294967296;
};
}
/* ------------------------------------------------------------------
Voxel landscape
------------------------------------------------------------------ */
box(scene, 31, 0.3, 17, 0x111f26, 0, -0.15, 0.3);
const groundRandom = seededRandom(4107);
const groundCols = 31;
const groundRows = 17;
const groundTiles = new THREE.InstancedMesh(
unitBox,
standardMaterial(0xffffff, { roughness: 1 }),
groundCols * groundRows
);
const groundPalette = [
0x29433d, 0x2f4b42, 0x355147, 0x29443d,
0x3b5548, 0x304b44, 0x426052
];
let groundIndex = 0;
for (let iz = 0; iz < groundRows; iz++) {
for (let ix = 0; ix < groundCols; ix++) {
dummy.position.set(
-15 + ix,
0.055,
-7.7 + iz
);
dummy.scale.set(0.965, 0.12, 0.965);
dummy.rotation.y = 0;
dummy.updateMatrix();
groundTiles.setMatrixAt(groundIndex, dummy.matrix);
groundTiles.setColorAt(
groundIndex,
new THREE.Color(
groundPalette[Math.floor(groundRandom() * groundPalette.length)]
)
);
groundIndex++;
}
}
groundTiles.receiveShadow = true;
groundTiles.instanceMatrix.needsUpdate = true;
groundTiles.instanceColor.needsUpdate = true;
scene.add(groundTiles);
// Low-contrast voxel skyline.
const skylineRandom = seededRandom(992);
for (let i = 0; i < 14; i++) {
const x = -15 + i * 2.3 + skylineRandom() * 0.6;
const height = 1.4 + skylineRandom() * 3.8;
const width = 1.2 + skylineRandom() * 1.2;
const depth = 1.2 + skylineRandom();
box(
scene,
width,
height,
depth,
i % 2 ? 0x102630 : 0x142b32,
x,
height / 2,
-7.4 - skylineRandom() * 1.3
);
}
/* ------------------------------------------------------------------
Green-roof model construction
------------------------------------------------------------------ */
function createLayer(parent, nx, nz, width, depth, y, height, palette, seed) {
const random = seededRandom(seed);
const mesh = new THREE.InstancedMesh(
unitBox,
standardMaterial(0xffffff, { roughness: 0.96 }),
nx * nz
);
let index = 0;
for (let iz = 0; iz < nz; iz++) {
for (let ix = 0; ix < nx; ix++) {
const x = -width / 2 + (ix + 0.5) * (width / nx);
const z = -depth / 2 + (iz + 0.5) * (depth / nz);
dummy.position.set(x, y, z);
dummy.scale.set(
width / nx * 0.92,
height,
depth / nz * 0.92
);
dummy.rotation.y = 0;
dummy.updateMatrix();
mesh.setMatrixAt(index, dummy.matrix);
mesh.setColorAt(
index,
new THREE.Color(palette[Math.floor(random() * palette.length)])
);
index++;
}
}
mesh.castShadow = true;
mesh.receiveShadow = true;
mesh.instanceMatrix.needsUpdate = true;
mesh.instanceColor.needsUpdate = true;
parent.add(mesh);
return mesh;
}
function createVoxelPlants(parent, seed) {
const random = seededRandom(seed);
const count = 84;
const mesh = new THREE.InstancedMesh(
unitBox,
standardMaterial(0xffffff, { roughness: 1 }),
count
);
const greens = [0x66b85c, 0x78c96b, 0x4fa653, 0x8ed47a];
for (let i = 0; i < count; i++) {
const x = -3.28 + random() * 6.56;
const z = -2.26 + random() * 4.52;
const y = 5.08 + (random() > 0.6 ? 0.08 : 0);
dummy.position.set(x, y, z);
dummy.scale.set(
0.08 + random() * 0.07,
0.14 + random() * 0.16,
0.08 + random() * 0.07
);
dummy.rotation.y = 0;
dummy.updateMatrix();
mesh.setMatrixAt(i, dummy.matrix);
mesh.setColorAt(
i,
new THREE.Color(greens[Math.floor(random() * greens.length)])
);
}
mesh.castShadow = true;
mesh.instanceMatrix.needsUpdate = true;
mesh.instanceColor.needsUpdate = true;
parent.add(mesh);
}
function createCloud(parent) {
const cloudMaterial = standardMaterial(0x58727d, {
roughness: 1,
transparent: true,
opacity: 0.92
});
const blocks = [
[-2.35, 0.00, 0.00, 1.55, 0.62, 1.65],
[-1.15, 0.18, -0.05, 1.75, 0.78, 1.80],
[0.15, 0.12, 0.00, 2.10, 0.72, 1.85],
[1.60, 0.18, -0.04, 1.65, 0.78, 1.70],
[2.65, -0.02, 0.00, 1.25, 0.58, 1.55],
[-0.60, -0.22, 0.08, 3.80, 0.50, 1.90]
];
for (const [x, y, z, w, h, d] of blocks) {
box(parent, w, h, d, 0xffffff, x, 7.95 + y, -0.72 + z, cloudMaterial);
}
}
function createRain(parent, seed) {
const random = seededRandom(seed);
const count = 72;
const drops = [];
for (let i = 0; i < count; i++) {
drops.push({
x: -3.2 + random() * 6.4,
z: -2.15 + random() * 4.3,
phase: random(),
speed: 0.75 + random() * 0.42
});
}
const mesh = new THREE.InstancedMesh(
unitBox,
standardMaterial(0x70cfff, {
emissive: 0x174f73,
emissiveIntensity: 0.45,
transparent: true,
opacity: 0.82,
depthWrite: false
}),
count
);
mesh.frustumCulled = false;
parent.add(mesh);
return { mesh, drops };
}
function createStorageVisual(parent) {
const nx = 12;
const nz = 8;
const layers = 4;
const cellH = 0.145;
const baseY = 3.56;
const mesh = new THREE.InstancedMesh(
unitBox,
standardMaterial(0x2b9fda, {
emissive: 0x063a59,
emissiveIntensity: 0.58,
roughness: 0.32,
metalness: 0.02,
transparent: true,
opacity: 0.9
}),
nx * nz * layers
);
mesh.instanceMatrix.setUsage(THREE.DynamicDrawUsage);
mesh.frustumCulled = false;
parent.add(mesh);
// Transparent reservoir envelope and dark voxel frame.
const tankMaterial = standardMaterial(0x5db8df, {
transparent: true,
opacity: 0.09,
depthWrite: false,
roughness: 0.25
});
const tank = box(
parent,
7.0,
0.62,
4.95,
0xffffff,
0,
3.85,
0,
tankMaterial
);
tank.castShadow = false;
tank.renderOrder = 1;
const edges = new THREE.LineSegments(
new THREE.EdgesGeometry(new THREE.BoxGeometry(7.0, 0.62, 4.95)),
new THREE.LineBasicMaterial({
color: 0x7ebbd0,
transparent: true,
opacity: 0.5
})
);
edges.position.set(0, 3.85, 0);
parent.add(edges);
const frameMaterial = standardMaterial(0x314c58);
box(parent, 0.12, 0.68, 5.08, 0xffffff, -3.54, 3.85, 0, frameMaterial);
box(parent, 0.12, 0.68, 5.08, 0xffffff, 3.54, 3.85, 0, frameMaterial);
box(parent, 7.08, 0.68, 0.12, 0xffffff, 0, 3.85, -2.49, frameMaterial);
box(parent, 7.08, 0.68, 0.12, 0xffffff, 0, 3.85, 2.49, frameMaterial);
function update(storageMm) {
const ratio = THREE.MathUtils.clamp(storageMm / CAPACITY_MM, 0, 1);
const totalLayers = ratio * layers;
let count = 0;
for (let layer = 0; layer < layers; layer++) {
const fill = THREE.MathUtils.clamp(totalLayers - layer, 0, 1);
if (fill <= 0.001) continue;
const h = fill * cellH;
const y = baseY + layer * cellH + h / 2;
for (let iz = 0; iz < nz; iz++) {
for (let ix = 0; ix < nx; ix++) {
const x = -3.18 + (ix + 0.5) * (6.36 / nx);
const z = -2.19 + (iz + 0.5) * (4.38 / nz);
dummy.position.set(x, y, z);
dummy.scale.set(0.50, h, 0.52);
dummy.rotation.y = 0;
dummy.updateMatrix();
mesh.setMatrixAt(count++, dummy.matrix);
}
}
}
mesh.count = count;
mesh.instanceMatrix.needsUpdate = true;
}
update(0);
return { mesh, update };
}
function createRunoffSystem(parent) {
const pipeMaterial = standardMaterial(0x243b45, {
roughness: 0.62,
metalness: 0.18
});
// Square voxel outlet and drain pipe.
box(parent, 0.27, 2.72, 0.27, 0xffffff, 2.85, 2.14, 2.78, pipeMaterial);
box(parent, 0.27, 0.27, 0.96, 0xffffff, 2.85, 0.78, 3.24, pipeMaterial);
box(parent, 0.27, 0.44, 0.27, 0xffffff, 2.85, 0.56, 3.68, pipeMaterial);
box(parent, 0.48, 0.12, 0.44, 0xffffff, 2.85, 3.48, 2.78, pipeMaterial);
box(parent, 0.45, 0.13, 0.40, 0xffffff, 2.85, 2.72, 2.78, pipeMaterial);
box(parent, 0.45, 0.13, 0.40, 0xffffff, 2.85, 1.18, 2.78, pipeMaterial);
// Open collection basin.
const basinMaterial = standardMaterial(0x405763);
box(parent, 2.05, 0.12, 1.46, 0xffffff, 2.85, 0.30, 4.58, basinMaterial);
box(parent, 0.13, 0.62, 1.46, 0xffffff, 1.88, 0.61, 4.58, basinMaterial);
box(parent, 0.13, 0.62, 1.46, 0xffffff, 3.82, 0.61, 4.58, basinMaterial);
box(parent, 1.94, 0.62, 0.13, 0xffffff, 2.85, 0.61, 3.91, basinMaterial);
box(parent, 1.94, 0.62, 0.13, 0xffffff, 2.85, 0.61, 5.25, basinMaterial);
const basinWater = box(
parent,
1.80,
0.01,
1.17,
0x2b9fda,
2.85,
0.38,
4.58,
standardMaterial(0x2b9fda, {
emissive: 0x063a59,
emissiveIntensity: 0.55,
roughness: 0.35
})
);
basinWater.castShadow = false;
const flowCount = 18;
const flowMesh = new THREE.InstancedMesh(
unitBox,
standardMaterial(0x64d4f5, {
emissive: 0x0b668c,
emissiveIntensity: 0.8,
transparent: true,
opacity: 0.94
}),
flowCount
);
flowMesh.frustumCulled = false;
parent.add(flowMesh);
const path = [
new THREE.Vector3(2.85, 3.42, 2.78),
new THREE.Vector3(2.85, 0.88, 2.78),
new THREE.Vector3(2.85, 0.88, 3.64),
new THREE.Vector3(2.85, 0.43, 4.12)
];
const segmentLengths = [];
let pathLength = 0;
for (let i = 0; i < path.length - 1; i++) {
const length = path[i].distanceTo(path[i + 1]);
segmentLengths.push(length);
pathLength += length;
}
function update(active, dischargedLiters) {
flowMesh.count = active ? flowCount : 0;
if (active) {
for (let i = 0; i < flowCount; i++) {
let distance = ((visualTime * 0.68 + i / flowCount) % 1) * pathLength;
let segment = 0;
while (
segment < segmentLengths.length - 1 &&
distance > segmentLengths[segment]
) {
distance -= segmentLengths[segment];
segment++;
}
const t = distance / segmentLengths[segment];
dummy.position.lerpVectors(path[segment], path[segment + 1], t);
dummy.scale.set(0.15, 0.15, 0.15);
dummy.rotation.set(0, 0, 0);
dummy.updateMatrix();
flowMesh.setMatrixAt(i, dummy.matrix);
}
flowMesh.instanceMatrix.needsUpdate = true;
}
const ratio = THREE.MathUtils.clamp(dischargedLiters / 1440, 0, 1);
const height = Math.max(0.01, ratio * 0.58);
basinWater.scale.set(1.80, height, 1.17);
basinWater.position.y = 0.37 + height / 2;
}
update(false, 0);
return { update, flowMesh };
}
function createModel(x, labelLetter, seed) {
const group = new THREE.Group();
group.position.x = x;
scene.add(group);
// Identical plinth and building for both models.
box(group, 8.15, 0.24, 6.15, 0x3c5158, 0, 0.24, 0);
box(group, 7.20, 2.95, 5.20, 0xc7d0c7, 0, 1.815, 0);
box(group, 7.34, 0.22, 5.30, 0x63757a, 0, 3.39, 0);
// Voxel facade details.
const windowMaterial = standardMaterial(0x274d61, {
emissive: 0x082434,
emissiveIntensity: 0.7,
roughness: 0.3
});
const frameMaterial = standardMaterial(0xecede2);
for (const wx of [-2.35, 0, 2.35]) {
for (const wy of [1.08, 2.18]) {
box(group, 0.88, 0.68, 0.08, 0xffffff, wx, wy, 2.64, frameMaterial);
box(group, 0.66, 0.47, 0.09, 0xffffff, wx, wy, 2.69, windowMaterial);
}
}
for (const wz of [-1.55, 0, 1.55]) {
for (const wy of [1.08, 2.18]) {
box(group, 0.08, 0.68, 0.88, 0xffffff, 3.64, wy, wz, frameMaterial);
box(group, 0.09, 0.47, 0.66, 0xffffff, 3.69, wy, wz, windowMaterial);
box(group, 0.08, 0.68, 0.88, 0xffffff, -3.64, wy, wz, frameMaterial);
box(group, 0.09, 0.47, 0.66, 0xffffff, -3.69, wy, wz, windowMaterial);
}
}
// Front door.
box(group, 0.98, 1.72, 0.10, 0x334b53, 0, 1.20, 2.66);
box(group, 0.10, 0.10, 0.10, 0xc7d7d5, 0.31, 1.18, 2.74);
// Green-roof layers, top to bottom: plants, soil, filter, storage.
const storage = createStorageVisual(group);
createLayer(
group, 14, 10, 7.02, 5.00,
4.23, 0.22,
[0x59666b, 0x657278, 0x707c7f, 0x4c5a61],
seed + 10
);
createLayer(
group, 14, 10, 7.02, 5.00,
4.51, 0.38,
[0x805b39, 0x936843, 0xa0784e, 0x6f4d32],
seed + 20
);
createLayer(
group, 14, 10, 7.02, 5.00,
4.86, 0.38,
[0x579c4d, 0x65ad57, 0x73bd63, 0x4e9348, 0x80c56d],
seed + 30
);
createVoxelPlants(group, seed + 40);
createCloud(group);
const rain = createRain(group, seed + 50);
const runoff = createRunoffSystem(group);
// Small model plate.
box(group, 2.4, 0.16, 0.48, labelLetter === "A" ? 0x277f96 : 0x9c6427,
0, 0.34, 3.05);
return {
group,
x,
storage,
rain,
runoff,
lastStorage: -1
};
}
const modelA = createModel(-5.35, "A", 100);
const modelB = createModel( 5.35, "B", 200);
/* ------------------------------------------------------------------
Model calculations
------------------------------------------------------------------ */
function modelState(initialMm) {
const rainfallInputMm = Math.min(simTime, RAIN_END_MIN);
const totalAvailableMm = initialMm + rainfallInputMm;
const overflowMm = Math.max(0, totalAvailableMm - CAPACITY_MM);
const storageMm = Math.min(CAPACITY_MM, totalAvailableMm);
const firstRunoffMin = Math.max(0, CAPACITY_MM - initialMm) / RAIN_RATE_MM_PER_MIN;
const flowing =
simTime > 0 &&
simTime < RAIN_END_MIN &&
simTime >= firstRunoffMin;
return {
storageMm,
storageLiters: storageMm * litersPerMm,
dischargeLiters: overflowMm * litersPerMm,
firstRunoffMin,
flowing
};
}
function formatClock(minutes) {
const wholeMinutes = Math.floor(minutes);
const seconds = Math.floor((minutes - wholeMinutes) * 60);
return (
String(wholeMinutes).padStart(2, "0") + ":" +
String(seconds).padStart(2, "0")
);
}
function updateModelVisual(model, state) {
if (Math.abs(model.lastStorage - state.storageMm) > 0.001) {
model.storage.update(state.storageMm);
model.lastStorage = state.storageMm;
}
model.runoff.update(state.flowing, state.dischargeLiters);
const raining = simTime < RAIN_END_MIN;
model.rain.mesh.count = raining ? model.rain.drops.length : 0;
if (raining) {
for (let i = 0; i < model.rain.drops.length; i++) {
const drop = model.rain.drops[i];
const cycle = (drop.phase + visualTime * drop.speed) % 1;
const y = 7.46 - cycle * 2.34;
dummy.position.set(drop.x, y, drop.z);
dummy.scale.set(0.055, 0.28 + cycle * 0.12, 0.055);
dummy.rotation.set(0, 0, 0);
dummy.updateMatrix();
model.rain.mesh.setMatrixAt(i, dummy.matrix);
}
model.rain.mesh.instanceMatrix.needsUpdate = true;
}
}
function setStateText(element, state) {
element.className = "model-state";
if (state.flowing) {
element.textContent = "Runoff flowing";
element.classList.add("flowing");
} else if (simTime >= RAIN_END_MIN) {
element.textContent = "Storm ended";
element.classList.add("ended");
} else if (state.firstRunoffMin === 0) {
element.textContent = simTime > 0 ? "Outlet ready" : "Ready";
} else {
element.textContent = "Waiting";
}
}
const clockElement = document.getElementById("clock");
const stormStatusElement = document.getElementById("stormStatus");
const timelineFillElement = document.getElementById("timelineFill");
const aDischargeElement = document.getElementById("aDischarge");
const bDischargeElement = document.getElementById("bDischarge");
const aStorageElement = document.getElementById("aStorage");
const bStorageElement = document.getElementById("bStorage");
const aStorageTextElement = document.getElementById("aStorageText");
const bStorageTextElement = document.getElementById("bStorageText");
const aStateElement = document.getElementById("aState");
const bStateElement = document.getElementById("bState");
function updateUI() {
const stateA = modelState(INITIAL_A_MM);
const stateB = modelState(INITIAL_B_MM);
clockElement.textContent = formatClock(simTime);
timelineFillElement.style.width = `${simTime / OBSERVATION_END_MIN * 100}%`;
if (simTime < RAIN_END_MIN) {
stormStatusElement.textContent = "Rainfall active · 1 mm/min";
} else if (simTime < OBSERVATION_END_MIN) {
stormStatusElement.textContent = "Dry observation";
} else {
stormStatusElement.textContent = "Test complete";
}
aDischargeElement.textContent = `${Math.round(stateA.dischargeLiters).toLocaleString()} L`;
bDischargeElement.textContent = `${Math.round(stateB.dischargeLiters).toLocaleString()} L`;
aStorageElement.style.width = `${stateA.storageMm / CAPACITY_MM * 100}%`;
bStorageElement.style.width = `${stateB.storageMm / CAPACITY_MM * 100}%`;
aStorageTextElement.textContent = `${Math.round(stateA.storageLiters)} L`;
bStorageTextElement.textContent = `${Math.round(stateB.storageLiters)} L`;
setStateText(aStateElement, stateA);
setStateText(bStateElement, stateB);
updateModelVisual(modelA, stateA);
updateModelVisual(modelB, stateB);
}
/* ------------------------------------------------------------------
Direct camera controls — no addon imports
------------------------------------------------------------------ */
let dragging = false;
let previousX = 0;
let previousY = 0;
renderer.domElement.addEventListener("pointerdown", (event) => {
dragging = true;
previousX = event.clientX;
previousY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!dragging) return;
const dx = event.clientX - previousX;
const dy = event.clientY - previousY;
previousX = event.clientX;
previousY = event.clientY;
cameraTheta -= dx * 0.006;
cameraPhi = THREE.MathUtils.clamp(cameraPhi + dy * 0.005, 0.53, 1.27);
});
function endDrag(event) {
dragging = false;
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();
cameraRadius = THREE.MathUtils.clamp(
cameraRadius + event.deltaY * 0.012,
14,
35
);
}, { passive: false });
renderer.domElement.addEventListener("dblclick", () => {
cameraTheta = 0.38;
cameraPhi = 0.82;
cameraRadius = 24;
});
/* ------------------------------------------------------------------
UI controls
------------------------------------------------------------------ */
const playButton = document.getElementById("playButton");
const resetButton = document.getElementById("resetButton");
const speedButtons = [...document.querySelectorAll(".speed")];
function updatePlayButton() {
playButton.textContent = playing ? "Pause" : "Play";
}
playButton.addEventListener("click", () => {
if (simTime >= OBSERVATION_END_MIN) simTime = 0;
playing = !playing;
updatePlayButton();
});
resetButton.addEventListener("click", () => {
simTime = 0;
visualTime = 0;
playing = true;
updatePlayButton();
});
speedButtons.forEach((button) => {
button.addEventListener("click", () => {
speed = Number(button.dataset.speed);
speedButtons.forEach((item) => item.classList.toggle("active", item === button));
});
});
window.addEventListener("keydown", (event) => {
if (event.code === "Space" && event.target === document.body) {
event.preventDefault();
playButton.click();
}
});
/* ------------------------------------------------------------------
DOM labels projected from 3D positions
------------------------------------------------------------------ */
const labelAElement = document.getElementById("labelA");
const labelBElement = document.getElementById("labelB");
const projected = new THREE.Vector3();
function positionWorldLabel(element, x, y, z) {
projected.set(x, y, z).project(camera);
const visible = projected.z > -1 && projected.z < 1;
element.style.display = visible ? "block" : "none";
if (!visible) return;
const screenX = (projected.x * 0.5 + 0.5) * window.innerWidth;
const screenY = (-projected.y * 0.5 + 0.5) * window.innerHeight;
element.style.left = `${screenX}px`;
element.style.top = `${screenY}px`;
}
/* ------------------------------------------------------------------
Resize and animation loop
------------------------------------------------------------------ */
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
});
const clock = new THREE.Clock();
function animate() {
const dt = Math.min(clock.getDelta(), 0.05);
visualTime += dt;
if (playing) {
simTime = Math.min(
OBSERVATION_END_MIN,
simTime + dt * speed
);
if (simTime >= OBSERVATION_END_MIN) {
playing = false;
updatePlayButton();
}
}
updateCamera();
updateUI();
positionWorldLabel(labelAElement, modelA.x, 5.72, 0.2);
positionWorldLabel(labelBElement, modelB.x, 5.72, 0.2);
renderer.render(scene, camera);
}
updatePlayButton();
updateUI();
renderer.setAnimationLoop(animate);
</script>
</body>
</html>
```