Prompt specification
Build or customize the Three.js 3D scene "Two roofs filling comparison barrels" 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: Two roofs filling comparison barrels
Category: Space & Earth · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/two-roofs-filling-comparison-barrels-x1234
Scene Description Prompt:
Please create one self-contained HTML file with Three.js from cdnjs showing rainfall collection. Place equal rainfall above two differently sized roofs and connected barrels; animate collection proportional to catchment area while keeping rainfall depth identical across both roofs. Stylized solid forms.
Working Three.js Starter (two-roofs-filling-comparison-barrels-x1234):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Rainfall Collection Comparison</title>
<style>
:root {
--ink: #17333d;
--muted: #60777c;
--paper: rgba(255, 255, 255, 0.88);
--line: rgba(23, 51, 61, 0.13);
--water: #238dc1;
--water-dark: #176f9a;
--coral: #dc705b;
--teal: #4e8d8c;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #e8f0e8;
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;
cursor: grab;
}
canvas.dragging {
cursor: grabbing;
}
#hud {
position: fixed;
inset: 0;
pointer-events: none;
}
.panel {
background: var(--paper);
border: 1px solid rgba(255, 255, 255, 0.8);
box-shadow: 0 14px 35px rgba(29, 63, 70, 0.12);
backdrop-filter: blur(12px);
-webkit-backdrop-filter: blur(12px);
border-radius: 18px;
}
#intro {
position: absolute;
top: 22px;
left: 22px;
width: min(380px, calc(100vw - 44px));
padding: 18px 20px;
}
.eyebrow {
margin-bottom: 7px;
color: var(--water-dark);
font-size: 11px;
font-weight: 800;
letter-spacing: 0.16em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: clamp(22px, 2.4vw, 32px);
line-height: 1.05;
letter-spacing: -0.035em;
}
#intro p {
margin: 9px 0 0;
color: var(--muted);
font-size: 13px;
line-height: 1.45;
}
#rain-card {
position: absolute;
top: 22px;
right: 22px;
width: 232px;
padding: 16px 18px;
}
.rain-row {
display: flex;
align-items: flex-end;
justify-content: space-between;
gap: 12px;
}
.rain-number {
color: var(--water-dark);
font-size: 38px;
font-weight: 850;
line-height: 0.95;
letter-spacing: -0.05em;
}
.rain-number span {
margin-left: 3px;
font-size: 15px;
letter-spacing: 0;
}
.rain-icon {
display: flex;
align-items: flex-end;
gap: 5px;
height: 31px;
}
.rain-icon i {
display: block;
width: 4px;
border-radius: 5px;
background: var(--water);
transform: rotate(13deg);
}
.rain-icon i:nth-child(1) { height: 14px; }
.rain-icon i:nth-child(2) { height: 27px; }
.rain-icon i:nth-child(3) { height: 20px; }
.small-label {
margin-top: 7px;
color: var(--muted);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.12em;
text-transform: uppercase;
}
.rain-note {
margin-top: 12px;
padding-top: 11px;
border-top: 1px solid var(--line);
color: var(--muted);
font-size: 11px;
line-height: 1.4;
}
#stats {
position: absolute;
bottom: 22px;
left: 22px;
display: flex;
gap: 10px;
}
.stat-card {
min-width: 205px;
padding: 14px 16px;
}
.stat-top {
display: flex;
align-items: center;
gap: 8px;
color: var(--muted);
font-size: 11px;
font-weight: 800;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.tag {
display: inline-grid;
width: 23px;
height: 23px;
place-items: center;
flex: 0 0 auto;
border-radius: 8px;
color: white;
background: var(--coral);
font-size: 12px;
font-weight: 900;
}
.tag.b {
background: var(--teal);
}
.stat-value {
margin-top: 7px;
font-size: 25px;
font-weight: 850;
letter-spacing: -0.035em;
}
.stat-value span {
font-size: 13px;
font-weight: 700;
letter-spacing: 0;
}
.stat-detail {
margin-top: 3px;
color: var(--muted);
font-size: 11px;
}
.meter {
position: relative;
height: 6px;
margin-top: 10px;
overflow: hidden;
border-radius: 999px;
background: rgba(23, 51, 61, 0.1);
}
.meter > div {
width: 0%;
height: 100%;
border-radius: inherit;
background: var(--water);
transition: width 0.1s linear;
}
#ratio-card {
position: absolute;
right: 22px;
bottom: 22px;
width: 232px;
padding: 15px 17px;
}
#ratio-card strong {
display: block;
color: var(--water-dark);
font-size: 24px;
letter-spacing: -0.04em;
}
#ratio-card p {
margin: 5px 0 0;
color: var(--muted);
font-size: 11px;
line-height: 1.45;
}
#controls {
position: absolute;
right: 22px;
bottom: 146px;
display: flex;
gap: 8px;
pointer-events: auto;
}
button {
border: 1px solid rgba(23, 51, 61, 0.13);
border-radius: 12px;
padding: 10px 14px;
background: rgba(255, 255, 255, 0.9);
color: var(--ink);
font: inherit;
font-size: 12px;
font-weight: 750;
cursor: pointer;
box-shadow: 0 7px 18px rgba(29, 63, 70, 0.09);
}
button:hover {
transform: translateY(-1px);
background: white;
}
#camera-hint {
position: absolute;
top: 50%;
right: 24px;
color: rgba(23, 51, 61, 0.56);
font-size: 10px;
font-weight: 750;
letter-spacing: 0.06em;
text-transform: uppercase;
writing-mode: vertical-rl;
transform: translateY(-50%);
}
.world-label {
position: absolute;
left: 0;
top: 0;
z-index: 2;
display: none;
min-width: 112px;
padding: 8px 10px;
border: 1px solid rgba(255, 255, 255, 0.9);
border-radius: 12px;
background: rgba(255, 255, 255, 0.9);
box-shadow: 0 8px 20px rgba(29, 63, 70, 0.13);
text-align: center;
transform: translate(-50%, -100%);
backdrop-filter: blur(8px);
-webkit-backdrop-filter: blur(8px);
}
.world-label strong {
display: block;
font-size: 12px;
letter-spacing: -0.01em;
}
.world-label small {
display: block;
margin-top: 2px;
color: var(--muted);
font-size: 9px;
font-weight: 700;
}
.world-label .label-tag {
display: inline-grid;
width: 17px;
height: 17px;
margin-right: 4px;
place-items: center;
border-radius: 5px;
color: white;
background: var(--coral);
font-size: 9px;
vertical-align: 1px;
}
.world-label .label-tag.b {
background: var(--teal);
}
@media (max-width: 820px) {
#intro {
top: 12px;
left: 12px;
width: calc(100vw - 24px);
padding: 13px 15px;
}
#intro p {
display: none;
}
#rain-card {
top: 92px;
right: 12px;
width: 174px;
padding: 12px 14px;
}
.rain-number {
font-size: 29px;
}
#stats {
right: 12px;
bottom: 12px;
left: 12px;
}
.stat-card {
min-width: 0;
flex: 1;
padding: 11px 12px;
}
.stat-value {
font-size: 20px;
}
#ratio-card {
right: 12px;
bottom: 123px;
width: 174px;
padding: 11px 13px;
}
#ratio-card strong {
font-size: 19px;
}
#ratio-card p {
display: none;
}
#controls {
right: 12px;
bottom: 225px;
}
#camera-hint {
display: none;
}
}
</style>
</head>
<body>
<div id="hud">
<section id="intro" class="panel">
<div class="eyebrow">Catchment comparison</div>
<h1>Same rain.<br />More roof, more water.</h1>
<p>
Both roofs receive the same vertical rainfall depth. The larger roof
collects 2.25 times as much water because its catchment area is larger.
</p>
</section>
<section id="rain-card" class="panel">
<div class="rain-row">
<div>
<div class="rain-number">12<span>mm/h</span></div>
<div class="small-label">Rainfall depth</div>
</div>
<div class="rain-icon" aria-hidden="true">
<i></i><i></i><i></i>
</div>
</div>
<div class="rain-note">
Identical depth over roofs A and B.<br />
Only the horizontal catchment area differs.
</div>
</section>
<section id="stats">
<article class="stat-card panel">
<div class="stat-top">
<span class="tag">A</span>
Small roof · 24 m²
</div>
<div class="stat-value">
<span id="liters-a">0.0</span> <span>L</span>
</div>
<div class="stat-detail">4.8 L/min · 240 L barrel</div>
<div class="meter"><div id="meter-a"></div></div>
</article>
<article class="stat-card panel">
<div class="stat-top">
<span class="tag b">B</span>
Large roof · 54 m²
</div>
<div class="stat-value">
<span id="liters-b">0.0</span> <span>L</span>
</div>
<div class="stat-detail">10.8 L/min · 240 L barrel</div>
<div class="meter"><div id="meter-b"></div></div>
</article>
</section>
<section id="ratio-card" class="panel">
<strong>2.25× collection</strong>
<p>54 m² ÷ 24 m² = 2.25. The larger roof receives 2.25× the runoff.</p>
</section>
<div id="controls">
<button id="pause" type="button">Pause</button>
<button id="restart" type="button">Restart</button>
</div>
<div id="camera-hint">Drag to orbit · Scroll to zoom</div>
</div>
<div id="label-a-roof" class="world-label">
<strong><span class="label-tag">A</span>24 m² roof</strong>
<small>smaller catchment</small>
</div>
<div id="label-b-roof" class="world-label">
<strong><span class="label-tag b">B</span>54 m² roof</strong>
<small>larger catchment</small>
</div>
<div id="label-a-barrel" class="world-label">
<strong><span class="label-tag">A</span><span id="world-lit-a">0.0</span> L</strong>
<small>240 L collection barrel</small>
</div>
<div id="label-b-barrel" class="world-label">
<strong><span class="label-tag b">B</span><span id="world-lit-b">0.0</span> L</strong>
<small>240 L collection barrel</small>
</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// ------------------------------------------------------------
// Configuration
// ------------------------------------------------------------
const RAIN_DEPTH_MM_PER_HOUR = 12;
const SIM_MINUTES_PER_SECOND = 0.12;
const BARREL_CAPACITY_L = 240;
const systems = [
{
id: "A",
x: -7,
area: 24,
roofWidth: 6,
roofDepth: 4,
roofRise: 1.35,
wallHeight: 3.0,
wallColor: 0xf0c89c,
roofColor: 0xd96f59,
outer: -1
},
{
id: "B",
x: 7,
area: 54,
roofWidth: 9,
roofDepth: 6,
roofRise: 1.8,
wallHeight: 3.8,
wallColor: 0xa8d0c2,
roofColor: 0x4f8290,
outer: 1
}
];
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xe8f0e8);
scene.fog = new THREE.Fog(0xe8f0e8, 32, 61);
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.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 hemi = new THREE.HemisphereLight(0xf7fbff, 0x9aab8d, 2.15);
scene.add(hemi);
const sun = new THREE.DirectionalLight(0xfff4dc, 3.25);
sun.position.set(-9, 17, 10);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -23;
sun.shadow.camera.right = 23;
sun.shadow.camera.top = 20;
sun.shadow.camera.bottom = -20;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 50;
sun.shadow.bias = -0.00035;
scene.add(sun);
const fillLight = new THREE.DirectionalLight(0xb9dcf4, 0.65);
fillLight.position.set(12, 8, -10);
scene.add(fillLight);
const materials = {
groundTop: new THREE.MeshStandardMaterial({
color: 0xcdddc9,
roughness: 0.96,
metalness: 0
}),
groundSide: new THREE.MeshStandardMaterial({
color: 0xaec3aa,
roughness: 1
}),
pipe: new THREE.MeshStandardMaterial({
color: 0x294650,
roughness: 0.62,
metalness: 0.08
}),
gutter: new THREE.MeshStandardMaterial({
color: 0x355d68,
roughness: 0.55,
metalness: 0.08
}),
water: new THREE.MeshStandardMaterial({
color: 0x269ac7,
roughness: 0.3,
metalness: 0.03
}),
rain: new THREE.MeshStandardMaterial({
color: 0x3d91c0,
roughness: 0.42,
metalness: 0
}),
barrel: new THREE.MeshStandardMaterial({
color: 0xd59a58,
roughness: 0.76,
metalness: 0,
side: THREE.DoubleSide
}),
barrelBand: new THREE.MeshStandardMaterial({
color: 0x80583b,
roughness: 0.72,
metalness: 0.05
}),
gaugePanel: new THREE.MeshStandardMaterial({
color: 0xf5f0df,
roughness: 0.8
}),
window: new THREE.MeshStandardMaterial({
color: 0x91c7d3,
roughness: 0.28,
metalness: 0.05
}),
door: new THREE.MeshStandardMaterial({
color: 0x315762,
roughness: 0.65
})
};
// ------------------------------------------------------------
// Helpers
// ------------------------------------------------------------
function box(width, height, depth, material, parent = scene) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(width, height, depth),
material
);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function cylinderBetween(a, b, radius, material, parent = scene) {
const direction = new THREE.Vector3().subVectors(b, a);
const length = direction.length();
const mesh = new THREE.Mesh(
new THREE.CylinderGeometry(radius, radius, length, 14),
material
);
mesh.position.copy(a).add(b).multiplyScalar(0.5);
mesh.quaternion.setFromUnitVectors(
new THREE.Vector3(0, 1, 0),
direction.clone().normalize()
);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function seededRandom(seed) {
return function () {
let t = (seed += 0x6d2b79f5);
t = Math.imul(t ^ (t >>> 15), t | 1);
t ^= t + Math.imul(t ^ (t >>> 7), t | 61);
return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
};
}
// ------------------------------------------------------------
// Ground and decorative forms
// ------------------------------------------------------------
const ground = new THREE.Mesh(
new THREE.CylinderGeometry(22, 22.4, 0.62, 96),
materials.groundSide
);
ground.position.y = -0.31;
ground.receiveShadow = true;
scene.add(ground);
const groundTop = new THREE.Mesh(
new THREE.CircleGeometry(21.9, 96),
materials.groundTop
);
groundTop.rotation.x = -Math.PI / 2;
groundTop.position.y = 0.012;
groundTop.receiveShadow = true;
scene.add(groundTop);
function addTree(x, z, scale = 1, color = 0x719875) {
const tree = new THREE.Group();
tree.position.set(x, 0, z);
tree.scale.setScalar(scale);
const trunk = new THREE.Mesh(
new THREE.CylinderGeometry(0.16, 0.22, 1.7, 8),
new THREE.MeshStandardMaterial({ color: 0x856044, roughness: 1 })
);
trunk.position.y = 0.85;
trunk.castShadow = true;
tree.add(trunk);
const foliageMaterial = new THREE.MeshStandardMaterial({
color,
roughness: 0.95,
flatShading: true
});
const crownA = new THREE.Mesh(
new THREE.DodecahedronGeometry(1.1, 0),
foliageMaterial
);
crownA.position.set(0, 2.05, 0);
crownA.scale.set(1, 1.15, 1);
crownA.castShadow = true;
tree.add(crownA);
const crownB = new THREE.Mesh(
new THREE.DodecahedronGeometry(0.78, 0),
foliageMaterial
);
crownB.position.set(0.55, 2.45, 0.08);
crownB.castShadow = true;
tree.add(crownB);
scene.add(tree);
}
addTree(-2.8, -8.6, 1.05, 0x709675);
addTree(1.3, -10.2, 0.78, 0x83a77b);
addTree(14.6, -7.8, 1.0, 0x648e78);
const pebbleMaterial = new THREE.MeshStandardMaterial({
color: 0xaab9a7,
roughness: 1,
flatShading: true
});
[
[-14, 5.8, 0.45],
[4.4, 7.2, 0.34],
[15.7, 4.1, 0.5],
[-3.2, 7.7, 0.28]
].forEach(([x, z, size]) => {
const pebble = new THREE.Mesh(
new THREE.DodecahedronGeometry(size, 0),
pebbleMaterial
);
pebble.position.set(x, size * 0.55, z);
pebble.scale.y = 0.7;
pebble.castShadow = true;
pebble.receiveShadow = true;
scene.add(pebble);
});
// ------------------------------------------------------------
// Houses, gutters, pipes, barrels, and runoff
// ------------------------------------------------------------
const systemVisuals = [];
function addHouse(system) {
const group = new THREE.Group();
group.position.x = system.x;
scene.add(group);
const wallWidth = system.roofWidth - 0.72;
const wallDepth = system.roofDepth - 0.72;
const wallMaterial = new THREE.MeshStandardMaterial({
color: system.wallColor,
roughness: 0.88,
metalness: 0
});
const roofMaterial = new THREE.MeshStandardMaterial({
color: system.roofColor,
roughness: 0.76,
metalness: 0
});
const foundation = box(
wallWidth + 0.18,
0.18,
wallDepth + 0.18,
new THREE.MeshStandardMaterial({
color: 0xc3b8a1,
roughness: 0.95
}),
group
);
foundation.position.y = 0.09;
const walls = box(
wallWidth,
system.wallHeight,
wallDepth,
wallMaterial,
group
);
walls.position.y = system.wallHeight / 2;
const halfWidth = system.roofWidth / 2;
const slopeLength = Math.hypot(halfWidth, system.roofRise);
const slopeAngle = Math.atan2(system.roofRise, halfWidth);
[-1, 1].forEach((side) => {
const slab = box(
slopeLength + 0.08,
0.2,
system.roofDepth,
roofMaterial,
group
);
slab.position.set(
(side * halfWidth) / 2,
system.wallHeight + system.roofRise / 2,
0
);
slab.rotation.z = side === -1 ? slopeAngle : -slopeAngle;
});
const ridge = box(
0.22,
0.2,
system.roofDepth + 0.22,
roofMaterial,
group
);
ridge.position.y = system.wallHeight + system.roofRise + 0.08;
// Front door and windows.
const frontZ = wallDepth / 2 + 0.045;
const door = box(0.9, 1.68, 0.1, materials.door, group);
door.position.set(0, 0.91, frontZ);
const knob = new THREE.Mesh(
new THREE.SphereGeometry(0.055, 10, 8),
new THREE.MeshStandardMaterial({
color: 0xf0c86c,
roughness: 0.42,
metalness: 0.2
})
);
knob.position.set(0.28, 0.95, frontZ + 0.08);
group.add(knob);
const windowXs =
system.id === "A" ? [-1.65, 1.65] : [-2.65, 2.65];
windowXs.forEach((windowX) => {
const frame = box(
1.0,
0.86,
0.08,
new THREE.MeshStandardMaterial({
color: 0xf7f1df,
roughness: 0.9
}),
group
);
frame.position.set(windowX, 1.95, frontZ + 0.01);
const glass = box(
0.79,
0.65,
0.08,
materials.window,
group
);
glass.position.set(windowX, 1.95, frontZ + 0.065);
const vertical = box(
0.055,
0.65,
0.035,
materials.door,
group
);
vertical.position.set(windowX, 1.95, frontZ + 0.115);
const horizontal = box(
0.79,
0.055,
0.035,
materials.door,
group
);
horizontal.position.set(windowX, 1.95, frontZ + 0.115);
});
// Gutter at the outer roof edge.
const outerX =
system.x + system.outer * (system.roofWidth / 2);
const frontPipeZ = system.roofDepth / 2 + 0.18;
const gutter = box(
0.2,
0.19,
system.roofDepth + 0.12,
materials.gutter
);
gutter.position.set(
outerX,
system.wallHeight - 0.035,
0
);
// Barrel and its position.
const barrelRadius = 0.72;
const barrelHeight = 2.15;
const barrelBottom = 0.18;
const barrelTop = barrelBottom + barrelHeight;
const barrelX = outerX + system.outer * 1.35;
const barrelZ = frontPipeZ;
const barrelBody = new THREE.Mesh(
new THREE.CylinderGeometry(
barrelRadius,
barrelRadius * 0.94,
barrelHeight,
32,
1,
true
),
materials.barrel
);
barrelBody.position.set(
barrelX,
barrelBottom + barrelHeight / 2,
barrelZ
);
barrelBody.castShadow = true;
barrelBody.receiveShadow = true;
scene.add(barrelBody);
const barrelBottomMesh = new THREE.Mesh(
new THREE.CylinderGeometry(
barrelRadius * 0.96,
barrelRadius * 0.94,
0.13,
32
),
materials.barrelBand
);
barrelBottomMesh.position.set(
barrelX,
barrelBottom + 0.065,
barrelZ
);
barrelBottomMesh.castShadow = true;
scene.add(barrelBottomMesh);
[0.74, 1.62].forEach((y) => {
const band = new THREE.Mesh(
new THREE.TorusGeometry(barrelRadius + 0.01, 0.035, 8, 32),
materials.barrelBand
);
band.rotation.x = Math.PI / 2;
band.position.set(barrelX, y, barrelZ);
band.castShadow = true;
scene.add(band);
});
const topRim = new THREE.Mesh(
new THREE.TorusGeometry(barrelRadius, 0.055, 10, 32),
materials.barrelBand
);
topRim.rotation.x = Math.PI / 2;
topRim.position.set(barrelX, barrelTop, barrelZ);
topRim.castShadow = true;
scene.add(topRim);
// Visible internal water volume.
const innerBottom = barrelBottom + 0.14;
const innerHeight = barrelHeight - 0.28;
const water = new THREE.Mesh(
new THREE.CylinderGeometry(
barrelRadius - 0.09,
barrelRadius - 0.09,
1,
32
),
materials.water
);
water.position.set(barrelX, innerBottom, barrelZ);
water.castShadow = true;
scene.add(water);
// Front-mounted fill gauge.
const gaugeBottom = 0.38;
const gaugeHeight = 1.5;
const gaugeWidth = 0.31;
const gaugeZ = barrelZ + barrelRadius + 0.07;
const gaugePanel = box(
gaugeWidth,
gaugeHeight,
0.055,
materials.gaugePanel
);
gaugePanel.position.set(
barrelX,
gaugeBottom + gaugeHeight / 2,
gaugeZ
);
const gaugeFill = box(
0.14,
1,
0.035,
materials.water
);
gaugeFill.position.set(barrelX, gaugeBottom, gaugeZ + 0.04);
for (let i = 0; i <= 4; i++) {
const tick = box(
0.09,
0.025,
0.025,
materials.barrelBand
);
tick.position.set(
barrelX + 0.23,
gaugeBottom + (gaugeHeight * i) / 4,
gaugeZ + 0.045
);
}
// Connected gutter -> downspout -> horizontal pipe -> inlet.
cylinderBetween(
new THREE.Vector3(
outerX,
system.wallHeight - 0.12,
frontPipeZ
),
new THREE.Vector3(
outerX,
barrelTop + 0.21,
frontPipeZ
),
0.095,
materials.pipe
);
cylinderBetween(
new THREE.Vector3(
outerX,
barrelTop + 0.21,
frontPipeZ
),
new THREE.Vector3(
barrelX,
barrelTop + 0.21,
frontPipeZ
),
0.095,
materials.pipe
);
cylinderBetween(
new THREE.Vector3(
barrelX,
barrelTop + 0.21,
frontPipeZ
),
new THREE.Vector3(
barrelX,
barrelTop - 0.03,
frontPipeZ
),
0.095,
materials.pipe
);
[outerX, barrelX].forEach((x) => {
const elbow = new THREE.Mesh(
new THREE.SphereGeometry(0.115, 12, 8),
materials.pipe
);
elbow.position.set(x, barrelTop + 0.21, frontPipeZ);
elbow.castShadow = true;
scene.add(elbow);
});
// Beads running down the outside of the pipe.
const pipeBeads = [];
const pipeBeadCount = Math.round(system.area * 0.32);
for (let i = 0; i < pipeBeadCount; i++) {
const bead = new THREE.Mesh(
new THREE.SphereGeometry(0.065, 8, 6),
materials.water
);
bead.castShadow = true;
scene.add(bead);
pipeBeads.push({
mesh: bead,
offset: i / pipeBeadCount,
direction: i % 2 === 0 ? 1 : -1
});
}
// Beads traveling over the roof toward the outer gutter.
const roofBeads = [];
const roofBeadCount = Math.round(system.area * 0.38);
const random = seededRandom(system.area * 981 + 17);
for (let i = 0; i < roofBeadCount; i++) {
const bead = new THREE.Mesh(
new THREE.SphereGeometry(0.052, 8, 6),
materials.water
);
bead.castShadow = true;
scene.add(bead);
roofBeads.push({
mesh: bead,
offset: random(),
z: (random() - 0.5) * (system.roofDepth - 0.25)
});
}
// Small stepping stones emphasize the collection path.
const stepCount = system.id === "A" ? 3 : 4;
for (let i = 0; i < stepCount; i++) {
const step = new THREE.Mesh(
new THREE.CylinderGeometry(
0.26,
0.29,
0.07,
6
),
new THREE.MeshStandardMaterial({
color: 0xd9d3bd,
roughness: 1
})
);
step.position.set(
barrelX + system.outer * (0.1 + i * 0.43),
0.05,
barrelZ + 0.95 + i * 0.16
);
step.rotation.y = i * 0.35;
step.receiveShadow = true;
scene.add(step);
}
systemVisuals.push({
system,
water,
gaugeFill,
gaugeBottom,
gaugeHeight,
innerBottom,
innerHeight,
pipeBeads,
roofBeads,
outerX,
frontPipeZ,
barrelTop,
barrelX,
barrelZ
});
}
systems.forEach(addHouse);
// ------------------------------------------------------------
// Rain fields
// Same number of drops per square meter and the same fall speed
// means identical rainfall depth, not identical total volume.
// ------------------------------------------------------------
const rainSystems = [];
systems.forEach((system, systemIndex) => {
const count = Math.round(system.area * 14);
const random = seededRandom(4000 + systemIndex * 733);
const mesh = new THREE.InstancedMesh(
new THREE.BoxGeometry(0.028, 0.34, 0.028),
materials.rain,
count
);
mesh.instanceMatrix.setUsage(THREE.DynamicDrawUsage);
mesh.frustumCulled = false;
mesh.position.x = system.x;
scene.add(mesh);
const drops = [];
for (let i = 0; i < count; i++) {
const localX =
(random() - 0.5) * (system.roofWidth - 0.12);
const localZ =
(random() - 0.5) * (system.roofDepth - 0.12);
const y =
system.wallHeight +
system.roofRise +
1.0 +
random() * 5.0;
drops.push({
x: localX,
z: localZ,
y,
speed: 6.8 + random() * 2.1
});
}
rainSystems.push({ system, mesh, drops });
});
// ------------------------------------------------------------
// Camera controls, implemented directly without add-ons
// ------------------------------------------------------------
const cameraTarget = new THREE.Vector3(0.7, 2.2, 0);
const cameraState = {
theta: 0.28,
phi: 1.03,
radius: 33
};
const cameraGoal = {
theta: cameraState.theta,
phi: cameraState.phi,
radius: cameraState.radius
};
const pointers = new Map();
let pinchDistance = 0;
renderer.domElement.addEventListener("pointerdown", (event) => {
renderer.domElement.setPointerCapture(event.pointerId);
pointers.set(event.pointerId, {
x: event.clientX,
y: event.clientY
});
renderer.domElement.classList.add("dragging");
if (pointers.size === 2) {
const values = [...pointers.values()];
pinchDistance = Math.hypot(
values[0].x - values[1].x,
values[0].y - values[1].y
);
}
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!pointers.has(event.pointerId)) return;
const previous = pointers.get(event.pointerId);
const dx = event.clientX - previous.x;
const dy = event.clientY - previous.y;
pointers.set(event.pointerId, {
x: event.clientX,
y: event.clientY
});
if (pointers.size === 1) {
cameraGoal.theta -= dx * 0.006;
cameraGoal.phi = THREE.MathUtils.clamp(
cameraGoal.phi - dy * 0.0055,
0.47,
1.34
);
} else if (pointers.size === 2) {
const values = [...pointers.values()];
const distance = Math.hypot(
values[0].x - values[1].x,
values[0].y - values[1].y
);
if (pinchDistance > 0) {
cameraGoal.radius = THREE.MathUtils.clamp(
cameraGoal.radius * (pinchDistance / distance),
19,
44
);
}
pinchDistance = distance;
}
});
function releasePointer(event) {
pointers.delete(event.pointerId);
if (pointers.size < 2) pinchDistance = 0;
if (pointers.size === 0) {
renderer.domElement.classList.remove("dragging");
}
}
renderer.domElement.addEventListener("pointerup", releasePointer);
renderer.domElement.addEventListener("pointercancel", releasePointer);
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraGoal.radius = THREE.MathUtils.clamp(
cameraGoal.radius * Math.exp(event.deltaY * 0.001),
19,
44
);
},
{ passive: false }
);
function updateCamera(dt) {
const smoothing = 1 - Math.exp(-8 * dt);
cameraState.theta = THREE.MathUtils.lerp(
cameraState.theta,
cameraGoal.theta,
smoothing
);
cameraState.phi = THREE.MathUtils.lerp(
cameraState.phi,
cameraGoal.phi,
smoothing
);
cameraState.radius = THREE.MathUtils.lerp(
cameraState.radius,
cameraGoal.radius,
smoothing
);
const sinPhi = Math.sin(cameraState.phi);
camera.position.set(
cameraTarget.x +
cameraState.radius *
sinPhi *
Math.sin(cameraState.theta),
cameraTarget.y +
cameraState.radius * Math.cos(cameraState.phi),
cameraTarget.z +
cameraState.radius *
sinPhi *
Math.cos(cameraState.theta)
);
camera.lookAt(cameraTarget);
camera.updateMatrixWorld();
}
// ------------------------------------------------------------
// Collection simulation
// Liters = area (m²) × rain depth (L/m²) × time (hours)
// ------------------------------------------------------------
let paused = false;
let simulationClock = 0;
let simulatedMinutes = 8;
const pauseButton = document.getElementById("pause");
const restartButton = document.getElementById("restart");
pauseButton.addEventListener("click", () => {
paused = !paused;
pauseButton.textContent = paused ? "Resume" : "Pause";
pauseButton.setAttribute("aria-pressed", String(paused));
});
restartButton.addEventListener("click", () => {
simulatedMinutes = 0;
simulationClock = 0;
});
const litersAElement = document.getElementById("liters-a");
const litersBElement = document.getElementById("liters-b");
const worldLitAElement = document.getElementById("world-lit-a");
const worldLitBElement = document.getElementById("world-lit-b");
const meterAElement = document.getElementById("meter-a");
const meterBElement = document.getElementById("meter-b");
function getLiters(system) {
const litersPerMinute =
(system.area * RAIN_DEPTH_MM_PER_HOUR) / 60;
return litersPerMinute * simulatedMinutes;
}
function updateCollectionUI() {
const litersA = getLiters(systems[0]);
const litersB = getLiters(systems[1]);
litersAElement.textContent = litersA.toFixed(1);
litersBElement.textContent = litersB.toFixed(1);
worldLitAElement.textContent = litersA.toFixed(1);
worldLitBElement.textContent = litersB.toFixed(1);
const percentA = THREE.MathUtils.clamp(
(litersA / BARREL_CAPACITY_L) * 100,
0,
100
);
const percentB = THREE.MathUtils.clamp(
(litersB / BARREL_CAPACITY_L) * 100,
0,
100
);
meterAElement.style.width = `${percentA}%`;
meterBElement.style.width = `${percentB}%`;
systemVisuals.forEach((visual) => {
const liters = getLiters(visual.system);
const fill = THREE.MathUtils.clamp(
liters / BARREL_CAPACITY_L,
0,
1
);
const waterHeight = visual.innerHeight * fill;
visual.water.scale.y = Math.max(waterHeight, 0.001);
visual.water.position.y =
visual.innerBottom + waterHeight / 2;
const gaugeHeight = visual.gaugeHeight * fill;
visual.gaugeFill.visible = fill > 0.001;
visual.gaugeFill.scale.y = Math.max(gaugeHeight, 0.001);
visual.gaugeFill.position.y =
visual.gaugeBottom + gaugeHeight / 2;
});
}
// ------------------------------------------------------------
// World-space HTML labels
// ------------------------------------------------------------
const labelElements = {
aRoof: document.getElementById("label-a-roof"),
bRoof: document.getElementById("label-b-roof"),
aBarrel: document.getElementById("label-a-barrel"),
bBarrel: document.getElementById("label-b-barrel")
};
function positionLabel(element, worldPosition) {
const projected = worldPosition.clone().project(camera);
if (
projected.z < -1 ||
projected.z > 1 ||
Math.abs(projected.x) > 1.15 ||
Math.abs(projected.y) > 1.15
) {
element.style.display = "none";
return;
}
element.style.display = "block";
element.style.left = `${(projected.x * 0.5 + 0.5) * innerWidth}px`;
element.style.top = `${(-projected.y * 0.5 + 0.5) * innerHeight}px`;
}
function updateLabels() {
const a = systems[0];
const b = systems[1];
const aVisual = systemVisuals[0];
const bVisual = systemVisuals[1];
positionLabel(
labelElements.aRoof,
new THREE.Vector3(
a.x,
a.wallHeight + a.roofRise + 1.05,
0
)
);
positionLabel(
labelElements.bRoof,
new THREE.Vector3(
b.x,
b.wallHeight + b.roofRise + 1.05,
0
)
);
positionLabel(
labelElements.aBarrel,
new THREE.Vector3(
aVisual.barrelX,
aVisual.barrelTop + 0.72,
aVisual.barrelZ
)
);
positionLabel(
labelElements.bBarrel,
new THREE.Vector3(
bVisual.barrelX,
bVisual.barrelTop + 0.72,
bVisual.barrelZ
)
);
}
// ------------------------------------------------------------
// Animation
// ------------------------------------------------------------
const dummy = new THREE.Object3D();
const clock = new THREE.Clock();
function animate() {
requestAnimationFrame(animate);
const dt = Math.min(clock.getDelta(), 0.05);
if (!paused) {
simulationClock += dt;
simulatedMinutes += dt * SIM_MINUTES_PER_SECOND;
// Rain falls at the same speed over both roofs.
rainSystems.forEach(({ system, mesh, drops }) => {
const halfWidth = system.roofWidth / 2;
drops.forEach((drop, index) => {
drop.y -= drop.speed * dt;
const roofSurface =
system.wallHeight +
system.roofRise *
(1 - Math.abs(drop.x) / halfWidth);
if (drop.y <= roofSurface + 0.13) {
drop.y =
system.wallHeight +
system.roofRise +
1.2 +
((index * 0.61803398875) % 1) * 5.2;
}
dummy.position.set(drop.x, drop.y, drop.z);
dummy.rotation.set(0, 0, 0);
dummy.scale.set(1, 1, 1);
dummy.updateMatrix();
mesh.setMatrixAt(index, dummy.matrix);
});
mesh.instanceMatrix.needsUpdate = true;
});
// Animated runoff over the roofs and through the pipes.
systemVisuals.forEach((visual) => {
const { system } = visual;
const halfWidth = system.roofWidth / 2;
visual.roofBeads.forEach((bead) => {
const t = (simulationClock * 0.48 + bead.offset) % 1;
const localX = system.outer * t * halfWidth;
const roofY =
system.wallHeight +
system.roofRise *
(1 - Math.abs(localX) / halfWidth);
bead.mesh.position.set(
system.x + localX,
roofY + 0.15,
bead.z
);
});
visual.pipeBeads.forEach((bead) => {
const t = (simulationClock * 0.62 + bead.offset) % 1;
if (bead.direction > 0) {
bead.mesh.position.set(
visual.outerX + system.outer * 0.1,
THREE.MathUtils.lerp(
system.wallHeight - 0.15,
visual.barrelTop + 0.22,
t
),
visual.frontPipeZ + 0.11
);
} else {
bead.mesh.position.set(
THREE.MathUtils.lerp(
visual.outerX,
visual.barrelX,
t
),
visual.barrelTop + 0.3,
visual.frontPipeZ + 0.11
);
}
});
});
}
updateCamera(dt);
updateCollectionUI();
updateLabels();
renderer.render(scene, camera);
}
window.addEventListener("resize", () => {
camera.aspect = innerWidth / innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(innerWidth, innerHeight);
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
});
updateCollectionUI();
animate();
</script>
</body>
</html>
```