Prompt specification
Build or customize the Three.js 3D scene "Rain Pulse Through a Roof Sponge Model" 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: Rain Pulse Through a Roof Sponge Model
Category: Everyday & Industrial · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/rain-pulse-through-a-roof-sponge-model-x3938
Scene Description Prompt:
Build a self-contained HTML scene with Three.js from cdnjs featuring green-roof layers and rainwater retention. Animate water entering a simplified storage layer before draining. State capacity and release rules, conserve volume, and show overflow when storage is exceeded. A 3D cutaway view.
Working Three.js Starter (rain-pulse-through-a-roof-sponge-model-x3938):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Green Roof — Rainwater Retention Cutaway</title>
<style>
:root {
color-scheme: dark;
--bg: #071b20;
--panel: rgba(8, 30, 35, 0.88);
--panel-border: rgba(165, 229, 224, 0.18);
--text: #e9fbf7;
--muted: #93b8b5;
--cyan: #60def4;
--green: #72d49b;
--amber: #ffb25b;
--red: #ff6f61;
}
* { box-sizing: border-box; }
html, body {
margin: 0;
width: 100%;
height: 100%;
overflow: hidden;
background: var(--bg);
color: var(--text);
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
button { font: inherit; }
#stage {
position: fixed;
inset: 0;
}
#stage canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
cursor: grab;
}
#stage canvas:active { cursor: grabbing; }
.brand {
position: fixed;
top: 25px;
left: 28px;
z-index: 5;
max-width: 520px;
pointer-events: none;
text-shadow: 0 2px 18px rgba(0, 0, 0, .45);
}
.eyebrow {
color: var(--green);
font-size: 11px;
font-weight: 800;
letter-spacing: .18em;
text-transform: uppercase;
}
h1 {
margin: 7px 0 8px;
font-size: clamp(26px, 3vw, 43px);
line-height: 1;
letter-spacing: -.045em;
font-weight: 760;
}
.brand p {
margin: 0;
color: #b7d4d0;
font-size: 13px;
line-height: 1.55;
}
.panel {
position: fixed;
z-index: 10;
top: 22px;
right: 22px;
width: 350px;
max-height: calc(100vh - 44px);
overflow: auto;
padding: 17px;
border: 1px solid var(--panel-border);
border-radius: 19px;
background: var(--panel);
box-shadow:
0 24px 70px rgba(0, 0, 0, .38),
inset 0 1px 0 rgba(255, 255, 255, .04);
backdrop-filter: blur(16px);
}
.panel::-webkit-scrollbar { width: 6px; }
.panel::-webkit-scrollbar-thumb {
background: rgba(112, 212, 155, .28);
border-radius: 6px;
}
.status-row {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
margin-bottom: 14px;
}
.status {
display: inline-flex;
align-items: center;
gap: 7px;
color: var(--green);
font-size: 11px;
font-weight: 800;
letter-spacing: .08em;
text-transform: uppercase;
}
.status::before {
content: "";
width: 8px;
height: 8px;
border-radius: 50%;
background: currentColor;
box-shadow: 0 0 14px currentColor;
}
.status.overflow { color: var(--amber); }
#mode {
color: var(--muted);
font-size: 11px;
text-align: right;
}
.tank-readout {
padding: 14px;
border: 1px solid rgba(112, 212, 155, .14);
border-radius: 14px;
background: rgba(112, 212, 155, .045);
}
.readout-top {
display: flex;
align-items: baseline;
justify-content: space-between;
gap: 10px;
}
.readout-top span {
color: var(--muted);
font-size: 12px;
}
#stored {
color: var(--cyan);
font-size: 29px;
font-weight: 780;
letter-spacing: -.04em;
}
.meter {
height: 8px;
margin: 12px 0 7px;
overflow: hidden;
border-radius: 99px;
background: rgba(255, 255, 255, .08);
}
#fill {
width: 0%;
height: 100%;
border-radius: inherit;
background: linear-gradient(90deg, #49b8bc, #66e5f4);
box-shadow: 0 0 16px rgba(96, 222, 244, .45);
transition: width .12s linear;
}
.meter-labels {
display: flex;
justify-content: space-between;
color: var(--muted);
font-size: 10px;
}
.metrics {
display: grid;
grid-template-columns: repeat(2, 1fr);
gap: 8px;
margin-top: 10px;
}
.metric {
min-width: 0;
padding: 10px 11px;
border: 1px solid rgba(255, 255, 255, .065);
border-radius: 11px;
background: rgba(255, 255, 255, .025);
}
.metric span {
display: block;
color: var(--muted);
font-size: 10px;
margin-bottom: 4px;
}
.metric strong {
font-size: 15px;
font-weight: 720;
white-space: nowrap;
}
.metric.cyan strong { color: var(--cyan); }
.metric.green strong { color: var(--green); }
.metric.amber strong { color: var(--amber); }
.metric.red strong { color: var(--red); }
.balance {
display: flex;
justify-content: space-between;
align-items: center;
gap: 12px;
margin-top: 10px;
padding: 10px 11px;
border-radius: 11px;
color: var(--muted);
background: rgba(96, 222, 244, .055);
font-size: 10px;
}
.balance strong {
color: var(--green);
font-size: 11px;
white-space: nowrap;
}
.section-title {
margin: 17px 0 9px;
color: #cce6e2;
font-size: 10px;
font-weight: 800;
letter-spacing: .13em;
text-transform: uppercase;
}
.rules {
margin: 0;
padding: 0;
list-style: none;
color: var(--muted);
font-size: 11px;
line-height: 1.55;
}
.rules li {
position: relative;
padding-left: 17px;
margin: 6px 0;
}
.rules li::before {
content: "";
position: absolute;
left: 0;
top: .63em;
width: 6px;
height: 6px;
border-radius: 50%;
background: var(--green);
}
.controls {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 8px;
margin-top: 14px;
}
.controls button {
min-height: 38px;
padding: 8px 10px;
border: 1px solid rgba(255, 255, 255, .1);
border-radius: 10px;
color: var(--text);
background: rgba(255, 255, 255, .045);
cursor: pointer;
font-size: 11px;
font-weight: 700;
transition: .16s ease;
}
.controls button:hover {
border-color: rgba(96, 222, 244, .45);
background: rgba(96, 222, 244, .09);
transform: translateY(-1px);
}
.controls button.primary {
color: #06231d;
border-color: transparent;
background: var(--green);
}
.controls button.primary:hover {
background: #8be1af;
}
.controls button.active {
border-color: var(--cyan);
color: var(--cyan);
background: rgba(96, 222, 244, .09);
}
.phase {
position: fixed;
z-index: 6;
left: 28px;
bottom: 25px;
display: flex;
align-items: center;
gap: 10px;
max-width: min(570px, calc(100vw - 56px));
padding: 10px 14px;
border: 1px solid rgba(165, 229, 224, .12);
border-radius: 12px;
color: #c4dfdb;
background: rgba(8, 30, 35, .68);
backdrop-filter: blur(10px);
font-size: 11px;
pointer-events: none;
}
.phase-dot {
width: 8px;
height: 8px;
flex: 0 0 auto;
border-radius: 50%;
background: var(--green);
box-shadow: 0 0 13px var(--green);
}
.phase.overflow .phase-dot {
background: var(--amber);
box-shadow: 0 0 13px var(--amber);
}
.hint {
position: fixed;
z-index: 5;
right: 27px;
bottom: 24px;
color: rgba(204, 229, 225, .65);
font-size: 10px;
pointer-events: none;
}
#labels {
position: fixed;
inset: 0;
z-index: 4;
pointer-events: none;
overflow: hidden;
}
.scene-label {
position: absolute;
transform: translate(-50%, -50%);
min-width: 125px;
padding: 7px 10px;
border: 1px solid rgba(166, 230, 224, .18);
border-radius: 9px;
color: #d8efeb;
background: rgba(7, 27, 32, .78);
box-shadow: 0 8px 25px rgba(0, 0, 0, .2);
backdrop-filter: blur(8px);
font-size: 10px;
line-height: 1.3;
white-space: nowrap;
}
.scene-label b {
display: block;
margin-bottom: 2px;
color: white;
font-size: 10px;
}
.scene-label small {
color: var(--muted);
font-size: 9px;
}
.scene-label::before {
content: "";
display: inline-block;
width: 6px;
height: 6px;
margin-right: 6px;
border-radius: 50%;
background: var(--dot, var(--green));
box-shadow: 0 0 9px var(--dot, var(--green));
}
@media (max-width: 780px) {
.brand {
top: 17px;
left: 17px;
max-width: calc(100vw - 34px);
}
.brand p { display: none; }
.panel {
top: auto;
right: 12px;
bottom: 12px;
width: calc(100vw - 24px);
max-height: 45vh;
padding: 13px;
border-radius: 15px;
}
.metrics { grid-template-columns: repeat(4, 1fr); }
.metric { padding: 8px 6px; }
.metric strong { font-size: 12px; }
.rules, .section-title { display: none; }
.phase, .hint, #labels { display: none; }
}
</style>
</head>
<body>
<div id="stage"></div>
<header class="brand">
<div class="eyebrow">Stormwater systems / interactive cutaway</div>
<h1>Green Roof Retention</h1>
<p>
Rain enters the roof assembly, fills a below-grade storage layer,
then leaves through a controlled gravity drain or overflow spillway.
</p>
</header>
<div id="labels" aria-hidden="true">
<div class="scene-label" id="label-plants"
style="--dot:#72d49b">
<b>Vegetation</b><small>Planting + substrate</small>
</div>
<div class="scene-label" id="label-filter"
style="--dot:#60def4">
<b>Filter / drainage mat</b><small>Routes runoff downward</small>
</div>
<div class="scene-label" id="label-cells"
style="--dot:#8aeadb">
<b>Retention cells</b><small>Roof-level water holding</small>
</div>
<div class="scene-label" id="label-tank"
style="--dot:#60def4">
<b>Storage layer</b><small>1,000 L hard capacity</small>
</div>
<div class="scene-label" id="label-drain"
style="--dot:#72d49b">
<b>Controlled release</b><small>30 L/s gravity drain</small>
</div>
<div class="scene-label" id="label-overflow"
style="--dot:#ffb25b">
<b>Overflow spillway</b><small>Only above capacity</small>
</div>
</div>
<aside class="panel" aria-label="Water balance controls">
<div class="status-row">
<div class="status" id="status">System active</div>
<div id="mode">Preparing storm pulse</div>
</div>
<div class="tank-readout">
<div class="readout-top">
<span>Stored water</span>
<strong id="stored">0 L</strong>
</div>
<div class="meter"><div id="fill"></div></div>
<div class="meter-labels">
<span>0 L</span>
<span>Hard limit: 1,000 L</span>
</div>
</div>
<div class="metrics">
<div class="metric cyan">
<span>Rain received</span>
<strong id="rainTotal">0 L</strong>
</div>
<div class="metric green">
<span>Released</span>
<strong id="released">0 L</strong>
</div>
<div class="metric amber">
<span>Overflow</span>
<strong id="overflow">0 L</strong>
</div>
<div class="metric">
<span>Live flow</span>
<strong id="liveFlow">0 L/s</strong>
</div>
</div>
<div class="balance">
<span>Inflow = stored + released + overflow</span>
<strong id="balance">Δ 0.00 L</strong>
</div>
<div class="section-title">Capacity & release rules</div>
<ul class="rules">
<li>Storage capacity is a hard limit of <b>1,000 L</b>.</li>
<li>Gravity release is <b>30 L/s</b> whenever stored water exists.</li>
<li>Rain entering empty capacity is accepted into storage.</li>
<li>Any inflow above remaining capacity is routed to overflow.</li>
<li>Released and overflow volumes are never counted as stored.</li>
</ul>
<div class="controls">
<button class="primary" id="stormBtn">Run overflow demo</button>
<button id="gentleBtn">Gentle rain</button>
<button id="stopBtn">Stop rain</button>
<button id="resetBtn">Reset</button>
<button id="pauseBtn">Pause</button>
<button id="speedBtn">Speed: 1×</button>
</div>
</aside>
<div class="phase" id="phase">
<span class="phase-dot"></span>
<span id="phaseText">Storm pulse active — storage is filling.</span>
</div>
<div class="hint">Drag to orbit · wheel to zoom · double-click to reset view</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// ------------------------------------------------------------
// Renderer, scene and camera
// ------------------------------------------------------------
const stage = document.getElementById("stage");
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x071b20);
scene.fog = new THREE.FogExp2(0x071b20, 0.022);
const camera = new THREE.PerspectiveCamera(
38,
innerWidth / innerHeight,
0.1,
100
);
const renderer = new THREE.WebGLRenderer({
antialias: true,
powerPreference: "high-performance"
});
renderer.setSize(innerWidth, innerHeight);
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.08;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
stage.appendChild(renderer.domElement);
scene.add(new THREE.HemisphereLight(0xb9f4ef, 0x102b30, 1.9));
const keyLight = new THREE.DirectionalLight(0xe8fff7, 3.1);
keyLight.position.set(-7, 13, 8);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -15;
keyLight.shadow.camera.right = 15;
keyLight.shadow.camera.top = 15;
keyLight.shadow.camera.bottom = -12;
keyLight.shadow.camera.near = 1;
keyLight.shadow.camera.far = 40;
keyLight.shadow.bias = -0.00035;
scene.add(keyLight);
const rimLight = new THREE.DirectionalLight(0x4dcfe2, 1.5);
rimLight.position.set(10, 7, -10);
scene.add(rimLight);
const cameraTarget = new THREE.Vector3(0, 2.7, 0);
let cameraTheta = 0.70;
let cameraPhi = 0.96;
let cameraRadius = 19.5;
function updateCamera() {
const spherical = new THREE.Spherical(
cameraRadius,
cameraPhi,
cameraTheta
);
camera.position.setFromSpherical(spherical).add(cameraTarget);
camera.lookAt(cameraTarget);
}
updateCamera();
// Direct orbit controls: no examples/addons are used.
let dragging = false;
let pointerX = 0;
let pointerY = 0;
renderer.domElement.addEventListener("pointerdown", event => {
dragging = true;
pointerX = event.clientX;
pointerY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", event => {
if (!dragging) return;
const dx = event.clientX - pointerX;
const dy = event.clientY - pointerY;
pointerX = event.clientX;
pointerY = event.clientY;
cameraTheta -= dx * 0.006;
cameraPhi = THREE.MathUtils.clamp(
cameraPhi - dy * 0.005,
0.42,
1.42
);
updateCamera();
});
function stopDragging() {
dragging = false;
}
renderer.domElement.addEventListener("pointerup", stopDragging);
renderer.domElement.addEventListener("pointercancel", stopDragging);
renderer.domElement.addEventListener("wheel", event => {
event.preventDefault();
cameraRadius = THREE.MathUtils.clamp(
cameraRadius + event.deltaY * 0.012,
10.5,
30
);
updateCamera();
}, { passive: false });
renderer.domElement.addEventListener("dblclick", () => {
cameraTheta = 0.70;
cameraPhi = 0.96;
cameraRadius = 19.5;
updateCamera();
});
// ------------------------------------------------------------
// Geometry helpers
// ------------------------------------------------------------
function standard(color, options = {}) {
return new THREE.MeshStandardMaterial({
color,
roughness: 0.72,
metalness: 0,
...options
});
}
function makeBox(
width, height, depth,
colorOrMaterial,
x, y, z,
options = {}
) {
const material =
typeof colorOrMaterial === "number"
? standard(colorOrMaterial, options)
: colorOrMaterial;
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(width, height, depth),
material
);
mesh.position.set(x, y, z);
mesh.castShadow = options.castShadow !== false;
mesh.receiveShadow = true;
scene.add(mesh);
return mesh;
}
function makePipe(points, radius, material) {
const group = new THREE.Group();
for (let i = 0; i < points.length - 1; i++) {
const a = points[i];
const b = points[i + 1];
const direction = new THREE.Vector3().subVectors(b, a);
const length = direction.length();
const geometry = new THREE.CylinderGeometry(
radius,
radius,
length,
14,
1,
true
);
const pipe = new THREE.Mesh(geometry, material);
pipe.position.copy(a).add(b).multiplyScalar(0.5);
pipe.quaternion.setFromUnitVectors(
new THREE.Vector3(0, 1, 0),
direction.normalize()
);
pipe.castShadow = true;
group.add(pipe);
}
for (const point of points.slice(1, -1)) {
const elbow = new THREE.Mesh(
new THREE.SphereGeometry(radius * 1.12, 12, 8),
material
);
elbow.position.copy(point);
group.add(elbow);
}
scene.add(group);
return group;
}
function makeEdges(width, height, depth, x, y, z, color) {
const geometry = new THREE.EdgesGeometry(
new THREE.BoxGeometry(width, height, depth)
);
const lines = new THREE.LineSegments(
geometry,
new THREE.LineBasicMaterial({
color,
transparent: true,
opacity: 0.52
})
);
lines.position.set(x, y, z);
scene.add(lines);
return lines;
}
let randomSeed = 91733;
function random() {
randomSeed |= 0;
randomSeed = randomSeed + 0x6D2B79F5 | 0;
let t = Math.imul(randomSeed ^ randomSeed >>> 15, 1 | randomSeed);
t = t + Math.imul(t ^ t >>> 7, 61 | t) ^ t;
return ((t ^ t >>> 14) >>> 0) / 4294967296;
}
// ------------------------------------------------------------
// Ground and cutaway building
// ------------------------------------------------------------
const ground = new THREE.Mesh(
new THREE.PlaneGeometry(42, 32),
standard(0x0d292e, { roughness: 0.94 })
);
ground.rotation.x = -Math.PI / 2;
ground.position.y = -0.13;
ground.receiveShadow = true;
scene.add(ground);
const grid = new THREE.GridHelper(40, 40, 0x31565a, 0x17383c);
grid.position.y = -0.11;
grid.material.transparent = true;
grid.material.opacity = 0.38;
scene.add(grid);
makeBox(10.1, 0.34, 6.2, 0x29484b, 0, 0.04, 0.05);
makeBox(10.1, 4.45, 0.25, 0x29474b, 0, 2.55, -3.0);
makeBox(0.25, 4.45, 6.0, 0x213d42, -4.95, 2.55, 0);
// Roof cutaway layers.
makeBox(14.0, 0.42, 6.3, 0x718183, 0, 4.86, -1.15);
makeBox(
13.55, 0.08, 5.85,
standard(0x22383b, { roughness: 0.48 }),
0, 5.11, -1.15
);
const retentionMaterial = standard(0x5abfc0, {
roughness: 0.4,
metalness: 0.08
});
makeBox(13.2, 0.24, 5.55, retentionMaterial, 0, 5.27, -1.15);
makeBox(
13.0, 0.12, 5.42,
standard(0x234f54, { roughness: 0.9 }),
0, 5.45, -1.15
);
const drySoil = new THREE.Color(0x684833);
const wetSoil = new THREE.Color(0x302f26);
const soilMaterial = standard(drySoil.clone(), { roughness: 1 });
makeBox(12.75, 0.56, 5.2, soilMaterial, 0, 5.79, -1.15);
// Cell pattern on the exposed front face.
const cellMaterial = standard(0xa4e4df, {
roughness: 0.42,
metalness: 0.05
});
for (let i = 0; i < 13; i++) {
makeBox(
0.57, 0.15, 0.045,
cellMaterial,
-6.15 + i * 1.025,
5.27,
1.635,
{ castShadow: false }
);
}
// Structural beams visible inside the cutaway.
const beamMaterial = standard(0x4a6265);
for (let i = 0; i < 5; i++) {
makeBox(
0.22, 0.34, 5.55,
beamMaterial,
-5.2 + i * 2.6,
4.45,
-1.15
);
}
// ------------------------------------------------------------
// Storage layer / cistern
// ------------------------------------------------------------
const tankBottom = 0.42;
const tankWaterHeight = 3.05;
const acrylicMaterial = standard(0x8bc5c3, {
transparent: true,
opacity: 0.16,
depthWrite: false,
roughness: 0.18
});
makeBox(9.3, 0.18, 5.7, 0x37585d, 0, tankBottom - 0.05, 0.15);
makeBox(9.2, 3.45, 0.16, acrylicMaterial, 0, 2.1, -2.62);
makeBox(0.16, 3.45, 5.55, acrylicMaterial, -4.52, 2.1, 0.15);
makeBox(0.16, 3.45, 5.55, acrylicMaterial, 4.52, 2.1, 0.15);
makeEdges(9.2, 3.45, 5.55, 0, 2.1, 0.15, 0x86d9d3);
const waterMaterial = standard(0x168eaf, {
transparent: true,
opacity: 0.7,
roughness: 0.18,
metalness: 0.08,
depthWrite: false
});
const waterMesh = new THREE.Mesh(
new THREE.BoxGeometry(8.95, 1, 5.2),
waterMaterial
);
waterMesh.position.set(0, tankBottom, 0.15);
waterMesh.receiveShadow = true;
scene.add(waterMesh);
const surfaceGeometry = new THREE.PlaneGeometry(
8.95,
5.2,
22,
14
);
surfaceGeometry.rotateX(-Math.PI / 2);
const surfaceMaterial = standard(0x53d7ef, {
transparent: true,
opacity: 0.68,
roughness: 0.12,
metalness: 0.12,
side: THREE.DoubleSide,
depthWrite: false
});
const waterSurface = new THREE.Mesh(
surfaceGeometry,
surfaceMaterial
);
scene.add(waterSurface);
// ------------------------------------------------------------
// Pipes, outlet and overflow catch basin
// ------------------------------------------------------------
const pipeMaterial = standard(0x36575c, {
transparent: true,
opacity: 0.48,
roughness: 0.28,
depthWrite: false
});
const overflowPipeMaterial = standard(0x8f7047, {
transparent: true,
opacity: 0.48,
roughness: 0.4,
depthWrite: false
});
const inletPoints = [
new THREE.Vector3(-3.4, 5.36, 1.56),
new THREE.Vector3(-3.4, 3.25, 1.56),
new THREE.Vector3(-3.4, 2.1, 1.56)
];
const outletPoints = [
new THREE.Vector3(4.48, 0.95, 0.45),
new THREE.Vector3(6.15, 0.95, 0.45),
new THREE.Vector3(6.65, 0.24, 0.45)
];
const overflowPoints = [
new THREE.Vector3(4.48, 3.46, 0.65),
new THREE.Vector3(7.0, 3.46, 0.65),
new THREE.Vector3(7.68, 0.34, 0.65)
];
makePipe(inletPoints, 0.13, pipeMaterial);
makePipe(outletPoints, 0.13, pipeMaterial);
makePipe(overflowPoints, 0.16, overflowPipeMaterial);
makeBox(
0.65, 0.16, 0.7,
standard(0x253e42),
-3.4, 5.35, 1.5
);
// Controlled-release grate.
makeBox(0.8, 0.08, 0.85, 0x182d30, 6.65, 0.12, 0.45);
for (let i = 0; i < 5; i++) {
makeBox(
0.055, 0.07, 0.75,
standard(0x587274),
6.36 + i * 0.145,
0.19,
0.45,
{ castShadow: false }
);
}
// Overflow catch basin.
makeBox(2.7, 0.12, 2.2, 0x4b4b40, 8.25, 0.08, 0.65);
makeBox(0.12, 0.65, 2.2, 0x725f42, 9.55, 0.35, 0.65);
makeBox(0.12, 0.65, 2.2, 0x725f42, 6.95, 0.35, 0.65);
makeBox(2.65, 0.65, 0.12, 0x725f42, 8.25, 0.35, -0.4);
makeBox(2.65, 0.65, 0.12, 0x725f42, 8.25, 0.35, 1.7);
const basinWaterMaterial = standard(0x238da6, {
transparent: true,
opacity: 0.76,
roughness: 0.16,
depthWrite: false
});
const basinWater = new THREE.Mesh(
new THREE.BoxGeometry(2.45, 1, 2.02),
basinWaterMaterial
);
basinWater.position.set(8.25, 0.17, 0.65);
scene.add(basinWater);
// ------------------------------------------------------------
// Roof vegetation
// ------------------------------------------------------------
const plantMaterials = [
standard(0x3f985c),
standard(0x61b66e),
standard(0x8acb76),
standard(0x2f8052)
];
const bladeGeometry = new THREE.ConeGeometry(0.08, 0.52, 4);
const shrubGeometry = new THREE.IcosahedronGeometry(0.22, 0);
for (let i = 0; i < 52; i++) {
const x = -6.05 + random() * 12.1;
const z = -3.55 + random() * 4.9;
const baseY = 6.08;
if (i % 5 === 0) {
const shrub = new THREE.Mesh(
shrubGeometry,
plantMaterials[i % plantMaterials.length]
);
shrub.position.set(x, baseY + 0.19, z);
shrub.scale.set(1.15, 0.72, 1.05);
shrub.rotation.y = random() * Math.PI;
shrub.castShadow = true;
scene.add(shrub);
} else {
for (let blade = 0; blade < 3; blade++) {
const mesh = new THREE.Mesh(
bladeGeometry,
plantMaterials[(i + blade) % plantMaterials.length]
);
mesh.position.set(
x + (random() - 0.5) * 0.2,
baseY + 0.23,
z + (random() - 0.5) * 0.2
);
mesh.rotation.z = (random() - 0.5) * 0.55;
mesh.rotation.x = (random() - 0.5) * 0.4;
mesh.scale.y = 0.65 + random() * 0.6;
mesh.castShadow = true;
scene.add(mesh);
}
}
}
const rockMaterial = standard(0x788283);
const rockGeometry = new THREE.DodecahedronGeometry(0.12, 0);
for (let i = 0; i < 16; i++) {
const rock = new THREE.Mesh(rockGeometry, rockMaterial);
rock.position.set(
-5.9 + random() * 11.8,
6.08,
-3.45 + random() * 4.75
);
rock.scale.setScalar(0.7 + random() * 0.7);
rock.rotation.set(random(), random(), random());
rock.castShadow = true;
scene.add(rock);
}
// ------------------------------------------------------------
// Flow curves and moving water beads
// ------------------------------------------------------------
const inletCurve = new THREE.CatmullRomCurve3(inletPoints);
const outletCurve = new THREE.CatmullRomCurve3(outletPoints);
const overflowCurve = new THREE.CatmullRomCurve3(overflowPoints);
function makeFlowTube(curve, color) {
const tube = new THREE.Mesh(
new THREE.TubeGeometry(curve, 36, 0.065, 8, false),
new THREE.MeshBasicMaterial({
color,
transparent: true,
opacity: 0.62,
depthWrite: false
})
);
scene.add(tube);
return tube;
}
function makeBeads(curve, count, color, radius) {
const group = new THREE.Group();
const geometry = new THREE.SphereGeometry(radius, 8, 6);
const material = new THREE.MeshBasicMaterial({ color });
for (let i = 0; i < count; i++) {
const bead = new THREE.Mesh(geometry, material);
group.add(bead);
}
scene.add(group);
return group;
}
const inletTube = makeFlowTube(inletCurve, 0x76eaff);
const outletTube = makeFlowTube(outletCurve, 0x75efbf);
const overflowTube = makeFlowTube(overflowCurve, 0xffc276);
const inletBeads = makeBeads(inletCurve, 14, 0x9af3ff, 0.075);
const outletBeads = makeBeads(outletCurve, 10, 0x99ffd0, 0.065);
const overflowBeads = makeBeads(overflowCurve, 18, 0xffd08c, 0.075);
function animateBeads(group, curve, activity, time, rate) {
group.visible = activity > 0.05;
if (!group.visible) return;
group.children.forEach((bead, index) => {
const t = (
time * (0.26 + Math.min(activity, 150) / 280) +
index / group.children.length
) % 1;
bead.position.copy(curve.getPointAt(t));
});
}
// ------------------------------------------------------------
// Rain particles
// ------------------------------------------------------------
const rainCount = 180;
const rainState = [];
for (let i = 0; i < rainCount; i++) {
rainState.push({
x: -6.5 + random() * 13,
y: 6.4 + random() * 7.2,
z: -4.1 + random() * 5.8,
speed: 7 + random() * 5
});
}
const rainPositions = new Float32Array(rainCount * 6);
const rainGeometry = new THREE.BufferGeometry();
rainGeometry.setAttribute(
"position",
new THREE.BufferAttribute(rainPositions, 3)
);
const rainMaterial = new THREE.LineBasicMaterial({
color: 0x8aeaff,
transparent: true,
opacity: 0.68
});
const rainLines = new THREE.LineSegments(
rainGeometry,
rainMaterial
);
rainLines.frustumCulled = false;
scene.add(rainLines);
// ------------------------------------------------------------
// Hydrologic model
//
// All values are liters. The same model drives the UI and scene.
//
// Each fixed step:
// 1. Release up to 30 L/s from existing storage.
// 2. Accept rain only into the remaining tank capacity.
// 3. Route all excess rain directly to overflow.
//
// Therefore:
// rainTotal = stored + releasedTotal + overflowTotal
// ------------------------------------------------------------
const CAPACITY = 1000;
const RELEASE_RATE = 30;
const STORM_RATE = 120;
const STORM_DURATION = 12;
const GENTLE_RATE = 45;
const state = {
stored: 0,
rainTotal: 0,
releasedTotal: 0,
overflowTotal: 0,
rainRate: 0,
inflowRate: 0,
releaseRate: 0,
overflowRate: 0,
mode: null,
stormRemaining: 0,
paused: false,
speed: 1,
simulationTime: 0
};
function clearBalance() {
state.stored = 0;
state.rainTotal = 0;
state.releasedTotal = 0;
state.overflowTotal = 0;
state.rainRate = 0;
state.inflowRate = 0;
state.releaseRate = 0;
state.overflowRate = 0;
state.stormRemaining = 0;
state.simulationTime = 0;
}
function startStorm() {
clearBalance();
state.mode = "storm";
state.rainRate = STORM_RATE;
state.stormRemaining = STORM_DURATION;
state.paused = false;
}
function startGentleRain() {
clearBalance();
state.mode = "gentle";
state.rainRate = GENTLE_RATE;
state.paused = false;
}
function stopRain() {
state.rainRate = 0;
state.mode = "drained";
state.stormRemaining = 0;
}
function resetSystem() {
clearBalance();
state.mode = null;
state.paused = false;
}
function simulationStep(dt) {
let incoming = 0;
if (state.mode === "storm") {
const rainingDuration = Math.min(dt, state.stormRemaining);
incoming = STORM_RATE * rainingDuration;
state.stormRemaining -= dt;
if (state.stormRemaining <= 0) {
state.stormRemaining = 0;
state.rainRate = 0;
state.mode = "drained";
}
} else if (state.mode === "gentle") {
incoming = GENTLE_RATE * dt;
}
// Controlled gravity release from stored water.
const released = Math.min(
state.stored,
RELEASE_RATE * dt
);
state.stored -= released;
state.releasedTotal += released;
// Incoming water fills only the remaining capacity.
const accepted = Math.min(
incoming,
Math.max(0, CAPACITY - state.stored)
);
const overflow = incoming - accepted;
state.stored += accepted;
state.rainTotal += incoming;
state.overflowTotal += overflow;
state.inflowRate = incoming / dt;
state.releaseRate = released / dt;
state.overflowRate = overflow / dt;
state.simulationTime += dt;
}
// ------------------------------------------------------------
// UI
// ------------------------------------------------------------
const ui = {
stored: document.getElementById("stored"),
fill: document.getElementById("fill"),
rainTotal: document.getElementById("rainTotal"),
released: document.getElementById("released"),
overflow: document.getElementById("overflow"),
liveFlow: document.getElementById("liveFlow"),
balance: document.getElementById("balance"),
status: document.getElementById("status"),
mode: document.getElementById("mode"),
phase: document.getElementById("phase"),
phaseText: document.getElementById("phaseText"),
stormBtn: document.getElementById("stormBtn"),
gentleBtn: document.getElementById("gentleBtn"),
stopBtn: document.getElementById("stopBtn"),
resetBtn: document.getElementById("resetBtn"),
pauseBtn: document.getElementById("pauseBtn"),
speedBtn: document.getElementById("speedBtn")
};
function formatLiters(value) {
if (value >= 1000) {
return value.toLocaleString(undefined, {
minimumFractionDigits: 0,
maximumFractionDigits: 0
});
}
return value.toLocaleString(undefined, {
minimumFractionDigits: value < 10 ? 1 : 0,
maximumFractionDigits: 1
});
}
function updateUI() {
const balanceError =
state.rainTotal -
(state.stored + state.releasedTotal + state.overflowTotal);
ui.stored.textContent = `${formatLiters(state.stored)} L`;
ui.fill.style.width =
`${THREE.MathUtils.clamp(state.stored / CAPACITY * 100, 0, 100)}%`;
ui.rainTotal.textContent = `${formatLiters(state.rainTotal)} L`;
ui.released.textContent = `${formatLiters(state.releasedTotal)} L`;
ui.overflow.textContent = `${formatLiters(state.overflowTotal)} L`;
ui.liveFlow.textContent = `${state.inflowRate.toFixed(0)} L/s`;
ui.balance.textContent = `Δ ${balanceError.toFixed(2)} L`;
const overflowing = state.overflowRate > 0.05;
ui.status.classList.toggle("overflow", overflowing);
ui.status.textContent = overflowing
? "Overflow active"
: state.paused
? "Simulation paused"
: "System active";
ui.phase.classList.toggle("overflow", overflowing);
if (overflowing) {
ui.phaseText.textContent =
"Capacity reached — incoming excess is bypassing storage through the spillway.";
} else if (state.mode === "storm") {
ui.phaseText.textContent =
`Storm pulse active — storage is filling. ${state.stormRemaining.toFixed(1)} s of rain remaining.`;
} else if (state.mode === "gentle") {
ui.phaseText.textContent =
"Gentle rainfall is entering storage while the gravity drain continues to release water.";
} else if (state.stored > 0.05) {
ui.phaseText.textContent =
"Rain has stopped. Stored water is draining at the fixed gravity-release rate.";
} else {
ui.phaseText.textContent =
"Runoff complete. All received volume is accounted for as stored, released, or overflow.";
}
if (state.mode === "storm") {
ui.mode.textContent = `Storm pulse · ${state.stormRemaining.toFixed(1)} s`;
} else if (state.mode === "gentle") {
ui.mode.textContent = "Gentle rainfall";
} else if (state.mode === "drained") {
ui.mode.textContent = "Rain stopped · draining";
} else {
ui.mode.textContent = "Ready";
}
ui.pauseBtn.textContent = state.paused ? "Resume" : "Pause";
ui.pauseBtn.classList.toggle("active", state.paused);
ui.speedBtn.textContent = `Speed: ${state.speed}×`;
ui.speedBtn.classList.toggle("active", state.speed !== 1);
ui.stormBtn.classList.toggle("active", state.mode === "storm");
ui.gentleBtn.classList.toggle("active", state.mode === "gentle");
}
ui.stormBtn.addEventListener("click", startStorm);
ui.gentleBtn.addEventListener("click", startGentleRain);
ui.stopBtn.addEventListener("click", stopRain);
ui.resetBtn.addEventListener("click", () => {
resetSystem();
updateUI();
});
ui.pauseBtn.addEventListener("click", () => {
state.paused = !state.paused;
updateUI();
});
ui.speedBtn.addEventListener("click", () => {
state.speed = state.speed === 1 ? 4 : 1;
updateUI();
});
// ------------------------------------------------------------
// Projected HTML labels
// ------------------------------------------------------------
const labelDefinitions = [
{
element: document.getElementById("label-plants"),
anchor: new THREE.Vector3(-4.5, 6.15, 1.55),
offset: [-35, -28]
},
{
element: document.getElementById("label-filter"),
anchor: new THREE.Vector3(4.6, 5.46, 1.58),
offset: [52, 14]
},
{
element: document.getElementById("label-cells"),
anchor: new THREE.Vector3(5.6, 5.27, 1.64),
offset: [68, 42]
},
{
element: document.getElementById("label-tank"),
anchor: new THREE.Vector3(-0.6, 1.55, 2.98),
offset: [-45, 28]
},
{
element: document.getElementById("label-drain"),
anchor: new THREE.Vector3(6.25, 0.83, 0.45),
offset: [48, 30]
},
{
element: document.getElementById("label-overflow"),
anchor: new THREE.Vector3(7.7, 2.85, 0.65),
offset: [52, -10]
}
];
const projected = new THREE.Vector3();
function updateLabels() {
const mobile = innerWidth <= 780;
for (const label of labelDefinitions) {
if (mobile) {
label.element.style.display = "none";
continue;
}
projected.copy(label.anchor).project(camera);
const x = (projected.x * 0.5 + 0.5) * innerWidth;
const y = (-projected.y * 0.5 + 0.5) * innerHeight;
const visible =
projected.z > -1 &&
projected.z < 1 &&
x > -100 &&
x < innerWidth + 100 &&
y > -50 &&
y < innerHeight + 50;
label.element.style.display = visible ? "block" : "none";
label.element.style.left = `${x + label.offset[0]}px`;
label.element.style.top = `${y + label.offset[1]}px`;
}
}
// ------------------------------------------------------------
// Visual state updates
// ------------------------------------------------------------
function updateRain(dt) {
const visibleCount = Math.floor(
rainCount * THREE.MathUtils.clamp(state.rainRate / STORM_RATE, 0, 1)
);
rainGeometry.setDrawRange(0, visibleCount * 2);
rainLines.visible = visibleCount > 0;
rainMaterial.opacity = 0.38 + Math.min(state.rainRate / 120, 1) * 0.35;
for (let i = 0; i < rainCount; i++) {
const drop = rainState[i];
if (dt > 0) {
drop.y -= drop.speed * dt;
if (drop.y < 6.13) {
drop.y = 12.4 + random() * 1.7;
drop.x = -6.5 + random() * 13;
drop.z = -4.1 + random() * 5.8;
}
}
const index = i * 6;
rainPositions[index] = drop.x;
rainPositions[index + 1] = drop.y;
rainPositions[index + 2] = drop.z;
rainPositions[index + 3] = drop.x;
rainPositions[index + 4] = drop.y - 0.43;
rainPositions[index + 5] = drop.z;
}
rainGeometry.attributes.position.needsUpdate = true;
}
function updateWaterSurface() {
const fill = state.stored / CAPACITY;
const height = Math.max(0.001, fill * tankWaterHeight);
waterMesh.scale.y = height;
waterMesh.position.y = tankBottom + height * 0.5;
waterSurface.position.y = tankBottom + height + 0.015;
waterSurface.visible = state.stored > 0.2;
const positions = surfaceGeometry.attributes.position;
for (let i = 0; i < positions.count; i++) {
const x = positions.getX(i);
const z = positions.getZ(i);
const wave =
Math.sin(x * 1.25 + state.simulationTime * 2.1) * 0.021 +
Math.cos(z * 1.65 - state.simulationTime * 1.45) * 0.017;
positions.setY(i, wave);
}
positions.needsUpdate = true;
// Roof substrate darkens slightly as it receives rainfall.
const moisture = THREE.MathUtils.clamp(
state.rainTotal / 550,
0,
1
);
soilMaterial.color.copy(drySoil).lerp(wetSoil, moisture);
const basinFill = THREE.MathUtils.clamp(
state.overflowTotal / 260,
0,
1
);
const basinHeight = Math.max(0.001, basinFill * 0.48);
basinWater.scale.y = basinHeight;
basinWater.position.y = 0.17 + basinHeight * 0.5;
basinWater.visible = state.overflowTotal > 0.2;
}
function updateFlows() {
inletTube.visible = state.inflowRate > 0.05;
outletTube.visible = state.releaseRate > 0.05;
overflowTube.visible = state.overflowRate > 0.05;
animateBeads(
inletBeads,
inletCurve,
state.inflowRate,
state.simulationTime,
state.inflowRate
);
animateBeads(
outletBeads,
outletCurve,
state.releaseRate,
state.simulationTime,
state.releaseRate
);
animateBeads(
overflowBeads,
overflowCurve,
state.overflowRate,
state.simulationTime,
state.overflowRate
);
}
// ------------------------------------------------------------
// Main loop
// ------------------------------------------------------------
const FIXED_STEP = 1 / 60;
let accumulator = 0;
let previousTime = performance.now();
let uiAccumulator = 0;
function animate(now) {
requestAnimationFrame(animate);
const realDt = Math.min((now - previousTime) / 1000, 0.05);
previousTime = now;
const simulationDt = state.paused ? 0 : realDt * state.speed;
accumulator += simulationDt;
while (accumulator >= FIXED_STEP) {
simulationStep(FIXED_STEP);
accumulator -= FIXED_STEP;
}
updateRain(simulationDt);
updateWaterSurface();
updateFlows();
updateLabels();
uiAccumulator += realDt;
if (uiAccumulator > 0.08) {
updateUI();
uiAccumulator = 0;
}
renderer.render(scene, camera);
}
window.addEventListener("resize", () => {
camera.aspect = innerWidth / innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(innerWidth, innerHeight);
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
});
// Begin with a storm pulse that reaches the 1,000 L capacity,
// then demonstrates overflow and the subsequent drawdown.
startStorm();
updateUI();
requestAnimationFrame(animate);
</script>
</body>
</html>
```