Prompt specification
Build or customize the Three.js 3D scene "Rain fills then overflows a tank" 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 fills then overflows a tank
Category: Biology & Nature · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/rain-fills-then-overflows-a-tank-x1098
Scene Description Prompt:
Create a single self-contained HTML file using Three.js from cdnjs that shows garden rainwater storage. Animate a rain pulse filling the reservoir until its level reaches the overflow outlet; further water follows the overflow pipe instead of increasing volume indefinitely. Low-poly.
Working Three.js Starter (rain-fills-then-overflows-a-tank-x1098):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Garden Rainwater Storage</title>
<style>
:root {
color-scheme: dark;
--panel: rgba(20, 37, 38, 0.82);
--panel-border: rgba(207, 240, 226, 0.18);
--text: #eef8ef;
--muted: #b7c9c3;
--mint: #7ce5c1;
--water: #58c9ef;
--orange: #ffbb62;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #b9cac7;
font-family: Inter, ui-sans-serif, system-ui, -apple-system,
BlinkMacSystemFont, "Segoe UI", sans-serif;
}
#scene {
position: fixed;
inset: 0;
}
canvas {
display: block;
cursor: grab;
touch-action: none;
}
canvas:active {
cursor: grabbing;
}
.panel {
position: fixed;
top: 18px;
left: 18px;
width: min(350px, calc(100vw - 36px));
padding: 18px 18px 16px;
color: var(--text);
background: var(--panel);
border: 1px solid var(--panel-border);
border-radius: 18px;
box-shadow: 0 18px 55px rgba(22, 43, 40, 0.2);
backdrop-filter: blur(14px);
-webkit-backdrop-filter: blur(14px);
pointer-events: none;
user-select: none;
}
.eyebrow {
color: var(--mint);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.16em;
text-transform: uppercase;
}
h1 {
margin: 5px 0 8px;
font-size: clamp(22px, 3vw, 30px);
line-height: 1.05;
letter-spacing: -0.035em;
}
.intro {
margin: 0 0 14px;
color: var(--muted);
font-size: 12px;
line-height: 1.55;
}
.status {
display: flex;
align-items: center;
gap: 9px;
min-height: 34px;
padding: 8px 10px;
margin-bottom: 13px;
border-radius: 10px;
background: rgba(255, 255, 255, 0.055);
font-size: 12px;
font-weight: 700;
}
.status-dot {
width: 9px;
height: 9px;
flex: 0 0 auto;
border-radius: 50%;
background: var(--water);
box-shadow: 0 0 0 4px rgba(88, 201, 239, 0.13);
transition: background 0.2s, box-shadow 0.2s;
}
.status-dot.overflow {
background: var(--orange);
box-shadow: 0 0 0 4px rgba(255, 187, 98, 0.14);
}
.metric {
margin-top: 11px;
}
.metric-row {
display: flex;
justify-content: space-between;
align-items: baseline;
margin-bottom: 5px;
color: var(--muted);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.1em;
text-transform: uppercase;
}
.metric-value {
color: var(--text);
font-size: 11px;
letter-spacing: 0;
}
.track {
height: 6px;
overflow: hidden;
border-radius: 99px;
background: rgba(255, 255, 255, 0.09);
}
.fill {
width: 100%;
height: 100%;
transform: scaleX(0);
transform-origin: left center;
border-radius: inherit;
transition: transform 0.08s linear;
}
#rain-fill {
background: linear-gradient(90deg, #75b9de, #b5e6f2);
}
#level-fill {
background: linear-gradient(90deg, #2e9ed0, #66e1d1);
}
#flow-fill {
background: linear-gradient(90deg, #ee9d42, #ffd27e);
}
.controls {
position: fixed;
left: 18px;
bottom: 18px;
display: flex;
gap: 8px;
pointer-events: auto;
}
button {
appearance: none;
padding: 10px 14px;
color: var(--text);
background: rgba(20, 37, 38, 0.86);
border: 1px solid rgba(207, 240, 226, 0.22);
border-radius: 12px;
box-shadow: 0 10px 30px rgba(22, 43, 40, 0.15);
font: inherit;
font-size: 11px;
font-weight: 800;
cursor: pointer;
backdrop-filter: blur(12px);
-webkit-backdrop-filter: blur(12px);
}
button:hover {
background: rgba(35, 61, 59, 0.95);
border-color: rgba(124, 229, 193, 0.5);
transform: translateY(-1px);
}
button:active {
transform: translateY(0);
}
.hint {
position: fixed;
right: 18px;
bottom: 18px;
padding: 9px 12px;
color: rgba(239, 248, 240, 0.85);
background: rgba(20, 37, 38, 0.67);
border: 1px solid rgba(255, 255, 255, 0.12);
border-radius: 10px;
font-size: 10px;
font-weight: 700;
letter-spacing: 0.03em;
pointer-events: none;
user-select: none;
}
.tag {
position: fixed;
top: 0;
left: 0;
z-index: 3;
padding: 5px 8px;
color: #f2fff8;
background: rgba(27, 48, 47, 0.78);
border: 1px solid rgba(255, 255, 255, 0.2);
border-radius: 7px;
font-size: 9px;
font-weight: 800;
letter-spacing: 0.08em;
text-transform: uppercase;
white-space: nowrap;
pointer-events: none;
transform: translate(-50%, -125%);
box-shadow: 0 6px 18px rgba(20, 40, 38, 0.15);
}
.tag::after {
content: "";
position: absolute;
top: 100%;
left: 50%;
width: 1px;
height: 13px;
background: rgba(255, 255, 255, 0.65);
}
@media (max-width: 650px) {
.panel {
top: 10px;
left: 10px;
width: min(320px, calc(100vw - 20px));
padding: 14px;
}
.intro {
display: none;
}
.status {
margin-bottom: 8px;
}
.metric {
margin-top: 8px;
}
.controls {
left: 10px;
bottom: 10px;
}
.hint {
right: 10px;
bottom: 10px;
max-width: 145px;
text-align: right;
}
}
</style>
</head>
<body>
<div id="scene"></div>
<section class="panel" aria-label="Rainwater storage simulation">
<div class="eyebrow">Garden systems · live simulation</div>
<h1>Rainwater Reservoir</h1>
<p class="intro">
A rain pulse fills the open reservoir. Once the water reaches the orange
overflow outlet, the level stays capped and incoming water travels through
the pipe to the stone drain.
</p>
<div class="status">
<span id="status-dot" class="status-dot"></span>
<span id="status-text">Preparing rain pulse…</span>
</div>
<div class="metric">
<div class="metric-row">
<span>Rainfall</span>
<span id="rain-value" class="metric-value">0%</span>
</div>
<div class="track"><div id="rain-fill" class="fill"></div></div>
</div>
<div class="metric">
<div class="metric-row">
<span>Stored level</span>
<span id="level-value" class="metric-value">0%</span>
</div>
<div class="track"><div id="level-fill" class="fill"></div></div>
</div>
<div class="metric">
<div class="metric-row">
<span>Overflow flow</span>
<span id="flow-value" class="metric-value">0%</span>
</div>
<div class="track"><div id="flow-fill" class="fill"></div></div>
</div>
</section>
<div class="controls">
<button id="rain-button" type="button">Pause rain</button>
<button id="replay-button" type="button">Replay fill pulse</button>
</div>
<div class="hint">Drag to orbit · Scroll to zoom · Double-click to reset view</div>
<div id="tank-tag" class="tag">Rainwater reservoir</div>
<div id="overflow-tag" class="tag">Overflow outlet</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, camera, and lighting
// -------------------------------------------------------------------------
const sceneHost = document.getElementById("scene");
const renderer = new THREE.WebGLRenderer({
antialias: true,
powerPreference: "high-performance"
});
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.05;
sceneHost.appendChild(renderer.domElement);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xb8c9c6);
scene.fog = new THREE.Fog(0xb8c9c6, 13, 29);
const camera = new THREE.PerspectiveCamera(
42,
window.innerWidth / window.innerHeight,
0.1,
70
);
const hemi = new THREE.HemisphereLight(0xd7f1f0, 0x536541, 2.15);
scene.add(hemi);
const sun = new THREE.DirectionalLight(0xfff1d6, 3.1);
sun.position.set(-5, 10, 7);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -12;
sun.shadow.camera.right = 12;
sun.shadow.camera.top = 12;
sun.shadow.camera.bottom = -12;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 32;
sun.shadow.bias = -0.0005;
scene.add(sun);
const fillLight = new THREE.DirectionalLight(0x9edbe0, 0.65);
fillLight.position.set(7, 5, -5);
scene.add(fillLight);
const clock = new THREE.Clock();
// -------------------------------------------------------------------------
// Small procedural helpers
// -------------------------------------------------------------------------
let seed = 38192;
function random() {
seed = (seed * 1664525 + 1013904223) >>> 0;
return seed / 4294967296;
}
function range(min, max) {
return min + (max - min) * random();
}
function material(color, options = {}) {
return new THREE.MeshStandardMaterial({
color,
roughness: 0.78,
metalness: 0,
flatShading: true,
...options
});
}
function addMesh(
geometry,
mat,
position = [0, 0, 0],
rotation = [0, 0, 0],
scale = [1, 1, 1],
parent = scene
) {
const object = new THREE.Mesh(geometry, mat);
object.position.set(...position);
object.rotation.set(...rotation);
object.scale.set(...scale);
object.castShadow = true;
object.receiveShadow = true;
parent.add(object);
return object;
}
const grassMat = material(0x789360);
const darkGrassMat = material(0x617c50);
const soilMat = material(0x75533c);
const woodMat = material(0x8c6543);
const stoneMat = material(0x89928d);
const darkStoneMat = material(0x666f6a);
const concreteMat = material(0xadb3aa);
const leafMat = material(0x4f7951);
const leafLightMat = material(0x74965d);
const trunkMat = material(0x75513a);
// -------------------------------------------------------------------------
// Garden environment
// -------------------------------------------------------------------------
const ground = addMesh(
new THREE.PlaneGeometry(30, 30),
grassMat,
[0, -0.045, 0],
[-Math.PI / 2, 0, 0]
);
ground.castShadow = false;
addMesh(
new THREE.CircleGeometry(5.8, 11),
darkGrassMat,
[-3.8, -0.032, -1.6],
[-Math.PI / 2, 0, 0]
);
// Raised vegetable bed.
const bed = new THREE.Group();
bed.position.set(-3.15, 0, -1.15);
bed.rotation.y = 0.08;
scene.add(bed);
addMesh(
new THREE.BoxGeometry(3.35, 0.3, 1.35),
woodMat,
[0, 0.15, 0],
[0, 0, 0],
[1, 1, 1],
bed
);
addMesh(
new THREE.BoxGeometry(3.08, 0.055, 1.08),
soilMat,
[0, 0.315, 0],
[0, 0, 0],
[1, 1, 1],
bed
);
const cabbageGeo = new THREE.IcosahedronGeometry(0.2, 0);
for (let row = 0; row < 3; row++) {
for (let col = 0; col < 6; col++) {
const plant = addMesh(
cabbageGeo,
(row + col) % 2 ? leafMat : leafLightMat,
[-1.18 + col * 0.47, 0.42, -0.33 + row * 0.33],
[0, range(0, Math.PI), 0],
[range(0.85, 1.15), range(0.7, 1.05), range(0.85, 1.15)],
bed
);
plant.castShadow = true;
}
}
// Second small bed.
const herbBed = new THREE.Group();
herbBed.position.set(-4.45, 0, 1.25);
herbBed.rotation.y = -0.12;
scene.add(herbBed);
addMesh(
new THREE.BoxGeometry(2.1, 0.25, 1.05),
woodMat,
[0, 0.125, 0],
[0, 0, 0],
[1, 1, 1],
herbBed
);
addMesh(
new THREE.BoxGeometry(1.9, 0.05, 0.84),
soilMat,
[0, 0.265, 0],
[0, 0, 0],
[1, 1, 1],
herbBed
);
const herbGeo = new THREE.ConeGeometry(0.12, 0.43, 5);
for (let i = 0; i < 12; i++) {
addMesh(
herbGeo,
leafLightMat,
[range(-0.78, 0.78), 0.43, range(-0.28, 0.28)],
[0, range(0, Math.PI), 0],
[1, range(0.8, 1.25), 1],
herbBed
);
}
// Stepping stones.
const stepGeo = new THREE.CylinderGeometry(0.31, 0.36, 0.075, 6);
const stepPositions = [
[-1.6, 0.0, 3.15],
[-0.95, 0.0, 3.55],
[-0.25, 0.0, 3.9],
[0.5, 0.0, 4.12],
[1.28, 0.0, 4.3]
];
stepPositions.forEach((position, index) => {
addMesh(
stepGeo,
index % 2 ? stoneMat : concreteMat,
position,
[0, range(0, Math.PI), 0]
);
});
// Low-poly tree.
const tree = new THREE.Group();
tree.position.set(-4.8, 0, -3.55);
scene.add(tree);
addMesh(
new THREE.CylinderGeometry(0.16, 0.25, 1.9, 6),
trunkMat,
[0, 0.95, 0],
[0, 0, 0],
[1, 1, 1],
tree
);
addMesh(
new THREE.IcosahedronGeometry(1.05, 0),
leafMat,
[0, 2.35, 0],
[0.1, 0.2, 0],
[1.2, 1.05, 1],
tree
);
addMesh(
new THREE.IcosahedronGeometry(0.78, 0),
leafLightMat,
[0.62, 2.13, 0.12],
[0, 0.4, 0.1],
[1, 1, 1],
tree
);
addMesh(
new THREE.IcosahedronGeometry(0.72, 0),
leafMat,
[-0.55, 2.02, -0.12],
[0.2, 0.8, 0],
[1, 1, 1],
tree
);
// Fence.
const fenceMat = material(0xa9a18c);
for (let i = 0; i < 9; i++) {
addMesh(
new THREE.BoxGeometry(0.13, 1.25, 0.13),
fenceMat,
[-6 + i * 1.5, 0.62, -5.15],
[0, 0, 0]
);
}
addMesh(
new THREE.BoxGeometry(12.2, 0.11, 0.1),
fenceMat,
[0, 0.48, -5.15]
);
addMesh(
new THREE.BoxGeometry(12.2, 0.11, 0.1),
fenceMat,
[0, 0.94, -5.15]
);
// Instanced grass tufts.
const grassGeo = new THREE.ConeGeometry(0.035, 0.28, 3);
const grassCount = 95;
const grassMesh = new THREE.InstancedMesh(
grassGeo,
material(0x526f47),
grassCount
);
const dummy = new THREE.Object3D();
for (let i = 0; i < grassCount; i++) {
let x;
let z;
do {
x = range(-7, 7);
z = range(-5.4, 5.4);
} while (
Math.hypot(x, z) < 1.8 ||
(x > -4.9 && x < -1.4 && z > -1.9 && z < -0.4) ||
(x > -5.6 && x < -3.3 && z > 0.6 && z < 1.9)
);
dummy.position.set(x, 0.13, z);
dummy.rotation.set(0, range(0, Math.PI), range(-0.12, 0.12));
dummy.scale.set(1, range(0.7, 1.45), 1);
dummy.updateMatrix();
grassMesh.setMatrixAt(i, dummy.matrix);
}
grassMesh.castShadow = true;
scene.add(grassMesh);
// A few low-poly flowers.
const flowerColors = [0xf3ba55, 0xe97c8d, 0xd9e8f0];
for (let i = 0; i < 14; i++) {
const flower = addMesh(
new THREE.IcosahedronGeometry(0.07, 0),
material(flowerColors[i % flowerColors.length]),
[range(-1.8, 2.1), 0.12, range(-3.7, -2.8)]
);
flower.castShadow = false;
}
// Clouds.
const cloudMat = material(0xdce6df, {
roughness: 1,
transparent: true,
opacity: 0.82
});
function makeCloud(x, y, z, scale) {
const group = new THREE.Group();
group.position.set(x, y, z);
group.scale.setScalar(scale);
scene.add(group);
const pieces = [
[-0.9, 0, 0, 0.72],
[-0.3, 0.2, 0.05, 0.9],
[0.45, 0.13, 0, 0.82],
[1.0, -0.03, 0.05, 0.58]
];
pieces.forEach(([px, py, pz, s]) => {
const cloud = addMesh(
new THREE.IcosahedronGeometry(0.8, 0),
cloudMat,
[px, py, pz],
[0, 0, 0],
[s, s * 0.65, s * 0.75],
group
);
cloud.castShadow = false;
});
return group;
}
const clouds = [
makeCloud(-4.2, 6.1, -6.3, 1.15),
makeCloud(3.7, 6.8, -7.2, 0.9)
];
// -------------------------------------------------------------------------
// Reservoir
// -------------------------------------------------------------------------
const TANK_OUTER_RADIUS = 1.24;
const TANK_INNER_RADIUS = 1.13;
const TANK_BOTTOM = 0.17;
const WATER_BOTTOM = 0.28;
const TANK_TOP = 3.05;
const OUTLET_Y = 2.58;
const INITIAL_LEVEL = 0.42;
const MAX_INFLOW = 0.325;
addMesh(
new THREE.CylinderGeometry(1.65, 1.72, 0.12, 12),
concreteMat,
[0, 0.01, 0]
);
const tankBase = addMesh(
new THREE.CylinderGeometry(
TANK_OUTER_RADIUS,
TANK_OUTER_RADIUS,
0.18,
20
),
material(0x647776, {
metalness: 0.18,
roughness: 0.55
}),
[0, TANK_BOTTOM, 0]
);
const wallMat = material(0x91aaa7, {
roughness: 0.32,
metalness: 0.08,
transparent: true,
opacity: 0.27,
side: THREE.DoubleSide,
depthWrite: false
});
const wall = addMesh(
new THREE.CylinderGeometry(
TANK_OUTER_RADIUS,
TANK_OUTER_RADIUS,
TANK_TOP - TANK_BOTTOM,
20,
1,
true
),
wallMat,
[0, (TANK_TOP + TANK_BOTTOM) / 2, 0]
);
wall.castShadow = false;
wall.renderOrder = 4;
const frameMat = material(0x596d6d, {
metalness: 0.3,
roughness: 0.55
});
const ribGeo = new THREE.BoxGeometry(0.045, 2.82, 0.065);
for (let i = 0; i < 12; i++) {
const angle = (i / 12) * Math.PI * 2;
addMesh(
ribGeo,
frameMat,
[
Math.sin(angle) * 1.265,
(TANK_TOP + TANK_BOTTOM) / 2,
Math.cos(angle) * 1.265
],
[0, angle, 0]
);
}
addMesh(
new THREE.TorusGeometry(TANK_OUTER_RADIUS, 0.058, 6, 32),
frameMat,
[0, TANK_TOP, 0],
[Math.PI / 2, 0, 0]
);
addMesh(
new THREE.TorusGeometry(TANK_OUTER_RADIUS, 0.035, 5, 32),
frameMat,
[0, 0.27, 0],
[Math.PI / 2, 0, 0]
);
const thresholdMat = material(0xe9a44e, {
emissive: 0x7c3d08,
emissiveIntensity: 0.22,
roughness: 0.48
});
const thresholdRing = addMesh(
new THREE.TorusGeometry(TANK_OUTER_RADIUS + 0.012, 0.019, 5, 32),
thresholdMat,
[0, OUTLET_Y, 0],
[Math.PI / 2, 0, 0]
);
thresholdRing.castShadow = false;
// Water volume.
const waterSideMat = material(0x1b9ec7, {
roughness: 0.24,
metalness: 0.04,
transparent: true,
opacity: 0.6,
depthWrite: false,
side: THREE.DoubleSide
});
const waterSide = addMesh(
new THREE.CylinderGeometry(
TANK_INNER_RADIUS,
TANK_INNER_RADIUS,
1,
20,
1,
true
),
waterSideMat,
[0, WATER_BOTTOM, 0]
);
waterSide.castShadow = false;
waterSide.renderOrder = 1;
const waterSurfaceGeo = new THREE.CircleGeometry(TANK_INNER_RADIUS, 20);
const waterSurfaceMat = material(0x49c8e0, {
roughness: 0.18,
metalness: 0.05,
transparent: true,
opacity: 0.9,
side: THREE.DoubleSide,
depthWrite: false,
emissive: 0x073d50,
emissiveIntensity: 0.16
});
const waterSurface = addMesh(
waterSurfaceGeo,
waterSurfaceMat,
[0, INITIAL_LEVEL, 0],
[-Math.PI / 2, 0, 0]
);
waterSurface.castShadow = false;
waterSurface.renderOrder = 2;
// Decorative gravel around the reservoir.
const gravelGeo = new THREE.DodecahedronGeometry(0.13, 0);
for (let i = 0; i < 17; i++) {
const angle = (i / 17) * Math.PI * 2 + range(-0.08, 0.08);
const radius = range(1.45, 1.65);
addMesh(
gravelGeo,
i % 2 ? stoneMat : darkStoneMat,
[
Math.cos(angle) * radius,
range(0.05, 0.12),
Math.sin(angle) * radius
],
[range(0, 1), range(0, 1), range(0, 1)],
[range(0.75, 1.25), range(0.55, 1), range(0.75, 1.25)]
);
}
// -------------------------------------------------------------------------
// Overflow pipe and stone drain
// -------------------------------------------------------------------------
const pipePoints = [
new THREE.Vector3(1.17, OUTLET_Y, 0),
new THREE.Vector3(1.68, OUTLET_Y, 0),
new THREE.Vector3(2.08, 2.53, 0),
new THREE.Vector3(2.32, 2.35, 0.02),
new THREE.Vector3(2.43, 1.92, 0.08),
new THREE.Vector3(2.43, 0.78, 0.2),
new THREE.Vector3(2.82, 0.46, 0.46),
new THREE.Vector3(3.67, 0.39, 0.67)
];
const pipeCurve = new THREE.CatmullRomCurve3(
pipePoints,
false,
"catmullrom",
0.25
);
const pipeMat = material(0x4c6463, {
metalness: 0.28,
roughness: 0.42,
transparent: true,
opacity: 0.62,
depthWrite: false
});
const pipe = addMesh(
new THREE.TubeGeometry(pipeCurve, 64, 0.12, 8, false),
pipeMat
);
pipe.castShadow = false;
pipe.renderOrder = 5;
const collarMat = material(0x405958, {
metalness: 0.34,
roughness: 0.4
});
function addPipeCollar(t) {
const position = pipeCurve.getPointAt(t);
const tangent = pipeCurve.getTangentAt(t).normalize();
const collar = addMesh(
new THREE.TorusGeometry(0.142, 0.025, 5, 12),
collarMat,
[position.x, position.y, position.z]
);
collar.quaternion.setFromUnitVectors(
new THREE.Vector3(0, 0, 1),
tangent
);
collar.renderOrder = 6;
return collar;
}
addPipeCollar(0);
addPipeCollar(0.27);
addPipeCollar(0.56);
addPipeCollar(0.8);
// Overflow flow particles inside the transparent pipe.
const MAX_FLOW_PARTICLES = 42;
const flowMesh = new THREE.InstancedMesh(
new THREE.IcosahedronGeometry(0.068, 0),
material(0x55d8f3, {
roughness: 0.2,
emissive: 0x07536a,
emissiveIntensity: 0.65
}),
MAX_FLOW_PARTICLES
);
flowMesh.count = 0;
flowMesh.frustumCulled = false;
flowMesh.instanceMatrix.setUsage(THREE.DynamicDrawUsage);
flowMesh.renderOrder = 2;
scene.add(flowMesh);
// Stone drain at the end of the overflow line.
const drainPoint = pipeCurve.getPointAt(1);
addMesh(
new THREE.CylinderGeometry(0.76, 0.8, 0.12, 10),
material(0x7c8179),
[drainPoint.x, 0.01, drainPoint.z]
);
const drainWater = addMesh(
new THREE.CircleGeometry(0.52, 12),
material(0x43bcd1, {
transparent: true,
opacity: 0.8,
roughness: 0.2,
depthWrite: false
}),
[drainPoint.x, 0.075, drainPoint.z],
[-Math.PI / 2, 0, 0]
);
drainWater.castShadow = false;
for (let i = 0; i < 12; i++) {
const angle = (i / 12) * Math.PI * 2;
const radius = range(0.56, 0.72);
addMesh(
gravelGeo,
i % 2 ? stoneMat : darkStoneMat,
[
drainPoint.x + Math.cos(angle) * radius,
0.12,
drainPoint.z + Math.sin(angle) * radius
],
[range(0, 1), range(0, 1), range(0, 1)],
[0.9, 0.7, 0.9]
);
}
// Reeds beside the drain.
const reedGeo = new THREE.ConeGeometry(0.035, 0.72, 4);
for (let i = 0; i < 9; i++) {
const angle = range(0, Math.PI * 2);
const radius = range(0.75, 1.0);
addMesh(
reedGeo,
i % 2 ? leafMat : leafLightMat,
[
drainPoint.x + Math.cos(angle) * radius,
0.36,
drainPoint.z + Math.sin(angle) * radius
],
[range(-0.12, 0.12), 0, range(-0.15, 0.15)]
);
}
// -------------------------------------------------------------------------
// Rain particles and splash rings
// -------------------------------------------------------------------------
const MAX_RAIN_DROPS = 440;
const rainPositions = new Float32Array(MAX_RAIN_DROPS * 2 * 3);
const rainGeometry = new THREE.BufferGeometry();
rainGeometry.setAttribute(
"position",
new THREE.BufferAttribute(rainPositions, 3)
);
const rainMaterial = new THREE.LineBasicMaterial({
color: 0xc0e8ee,
transparent: true,
opacity: 0.63,
depthWrite: false
});
const rainLines = new THREE.LineSegments(rainGeometry, rainMaterial);
rainLines.frustumCulled = false;
scene.add(rainLines);
const drops = [];
function resetDrop(drop) {
const angle = Math.random() * Math.PI * 2;
const radius =
Math.random() < 0.7
? Math.sqrt(Math.random()) * 1.55
: Math.sqrt(Math.random()) * 3.1;
drop.x = Math.cos(angle) * radius;
drop.z = Math.sin(angle) * radius;
drop.y = 5.5 + Math.random() * 4;
drop.speed = 5.4 + Math.random() * 3.7;
drop.length = 0.16 + Math.random() * 0.2;
}
for (let i = 0; i < MAX_RAIN_DROPS; i++) {
const drop = {};
resetDrop(drop);
drops.push(drop);
}
const splashRings = [];
const splashGeo = new THREE.TorusGeometry(0.075, 0.012, 4, 12);
for (let i = 0; i < 18; i++) {
const mat = new THREE.MeshBasicMaterial({
color: 0xa9eff7,
transparent: true,
opacity: 0,
depthWrite: false
});
const ring = new THREE.Mesh(splashGeo, mat);
ring.rotation.x = Math.PI / 2;
ring.visible = false;
scene.add(ring);
splashRings.push({
mesh: ring,
age: 1,
duration: 0.58
});
}
let nextSplash = 0;
function spawnSplash(x, z) {
const splash = splashRings[nextSplash];
nextSplash = (nextSplash + 1) % splashRings.length;
splash.age = 0;
splash.mesh.position.set(x, waterLevel + 0.012, z);
splash.mesh.scale.setScalar(1);
splash.mesh.material.opacity = 0.8;
splash.mesh.visible = true;
}
// -------------------------------------------------------------------------
// Overflow spray at the stone drain
// -------------------------------------------------------------------------
const sprayDrops = [];
const sprayGeo = new THREE.IcosahedronGeometry(0.038, 0);
const sprayMat = material(0x64dcf2, {
roughness: 0.22,
emissive: 0x07566d,
emissiveIntensity: 0.45
});
for (let i = 0; i < 28; i++) {
const drop = addMesh(sprayGeo, sprayMat);
drop.visible = false;
sprayDrops.push({
mesh: drop,
active: false,
life: 0,
velocity: new THREE.Vector3()
});
}
function spawnSpray() {
const drop = sprayDrops.find((item) => !item.active);
if (!drop) return;
drop.active = true;
drop.life = 0.45 + Math.random() * 0.35;
drop.mesh.position.set(
drainPoint.x + (Math.random() - 0.5) * 0.22,
drainPoint.y,
drainPoint.z + (Math.random() - 0.5) * 0.22
);
drop.velocity.set(
(Math.random() - 0.5) * 0.9,
0.8 + Math.random() * 1.1,
(Math.random() - 0.5) * 0.9
);
drop.mesh.visible = true;
}
const drainRipple = addMesh(
new THREE.TorusGeometry(0.18, 0.012, 4, 16),
new THREE.MeshBasicMaterial({
color: 0xb4f1f6,
transparent: true,
opacity: 0,
depthWrite: false
}),
[drainPoint.x, 0.09, drainPoint.z],
[Math.PI / 2, 0, 0]
);
drainRipple.castShadow = false;
// -------------------------------------------------------------------------
// Simulation state
// -------------------------------------------------------------------------
let simulationTime = 0;
let waterLevel = INITIAL_LEVEL;
let overflowRate = 0;
let rainEnabled = true;
let flowOffset = 0;
let drainRippleTime = 0;
function getRainIntensity(time) {
if (!rainEnabled) return 0;
// Initial long pulse fills the tank.
if (time < 2) return 0;
if (time < 11) {
const p = (time - 2) / 9;
return 0.65 + 0.35 * Math.sin(Math.PI * p);
}
// Subsequent pulses repeatedly test and exercise the overflow.
const cycle = (time - 11) % 7;
if (cycle < 1.1 || cycle > 6.1) return 0;
const p = (cycle - 1.1) / 5;
return Math.pow(Math.sin(Math.PI * p), 0.78);
}
function updateWaterMesh() {
const height = Math.max(0.025, waterLevel - WATER_BOTTOM);
waterSide.scale.set(1, height, 1);
waterSide.position.y = WATER_BOTTOM + height / 2;
waterSurface.position.y = waterLevel;
}
updateWaterMesh();
// -------------------------------------------------------------------------
// Direct camera controls: orbit, zoom, and reset
// -------------------------------------------------------------------------
const controls = {
azimuth: 0.69,
polar: 1.08,
radius: 11.2,
targetAzimuth: 0.69,
targetPolar: 1.08,
targetRadius: 11.2,
target: new THREE.Vector3(0.25, 1.28, 0.15)
};
let dragging = false;
let lastPointerX = 0;
let lastPointerY = 0;
renderer.domElement.addEventListener("pointerdown", (event) => {
dragging = true;
lastPointerX = event.clientX;
lastPointerY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!dragging) return;
const dx = event.clientX - lastPointerX;
const dy = event.clientY - lastPointerY;
lastPointerX = event.clientX;
lastPointerY = event.clientY;
controls.targetAzimuth -= dx * 0.006;
controls.targetPolar = THREE.MathUtils.clamp(
controls.targetPolar - dy * 0.005,
0.36,
1.43
);
});
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();
controls.targetRadius = THREE.MathUtils.clamp(
controls.targetRadius * Math.exp(event.deltaY * 0.001),
6.5,
17
);
},
{ passive: false }
);
renderer.domElement.addEventListener("dblclick", () => {
controls.targetAzimuth = 0.69;
controls.targetPolar = 1.08;
controls.targetRadius = 11.2;
});
function updateCamera(dt) {
const smoothing = 1 - Math.exp(-dt * 10);
controls.azimuth +=
(controls.targetAzimuth - controls.azimuth) * smoothing;
controls.polar +=
(controls.targetPolar - controls.polar) * smoothing;
controls.radius +=
(controls.targetRadius - controls.radius) * smoothing;
const sinPolar = Math.sin(controls.polar);
camera.position.set(
controls.target.x +
controls.radius * sinPolar * Math.sin(controls.azimuth),
controls.target.y + controls.radius * Math.cos(controls.polar),
controls.target.z +
controls.radius * sinPolar * Math.cos(controls.azimuth)
);
camera.lookAt(controls.target);
camera.updateMatrixWorld();
}
// -------------------------------------------------------------------------
// UI
// -------------------------------------------------------------------------
const rainButton = document.getElementById("rain-button");
const replayButton = document.getElementById("replay-button");
const rainValue = document.getElementById("rain-value");
const levelValue = document.getElementById("level-value");
const flowValue = document.getElementById("flow-value");
const rainFill = document.getElementById("rain-fill");
const levelFill = document.getElementById("level-fill");
const flowFill = document.getElementById("flow-fill");
const statusText = document.getElementById("status-text");
const statusDot = document.getElementById("status-dot");
rainButton.addEventListener("click", () => {
rainEnabled = !rainEnabled;
rainButton.textContent = rainEnabled ? "Pause rain" : "Resume rain";
});
replayButton.addEventListener("click", () => {
simulationTime = 0;
waterLevel = INITIAL_LEVEL;
overflowRate = 0;
flowOffset = 0;
rainEnabled = true;
rainButton.textContent = "Pause rain";
updateWaterMesh();
});
const tankTag = document.getElementById("tank-tag");
const overflowTag = document.getElementById("overflow-tag");
const tagPosition = new THREE.Vector3();
function positionTag(element, worldPosition) {
tagPosition.copy(worldPosition).project(camera);
const visible = tagPosition.z > -1 && tagPosition.z < 1;
element.style.display = visible ? "block" : "none";
if (!visible) return;
const x = (tagPosition.x * 0.5 + 0.5) * window.innerWidth;
const y = (-tagPosition.y * 0.5 + 0.5) * window.innerHeight;
element.style.left = `${x}px`;
element.style.top = `${y}px`;
}
function updateHUD(intensity) {
const levelPercent = THREE.MathUtils.clamp(
((waterLevel - WATER_BOTTOM) / (OUTLET_Y - WATER_BOTTOM)) * 100,
0,
100
);
const flowPercent = THREE.MathUtils.clamp(
(overflowRate / MAX_INFLOW) * 100,
0,
100
);
const rainPercent = intensity * 100;
rainValue.textContent = `${Math.round(rainPercent)}%`;
levelValue.textContent = `${Math.round(levelPercent)}%`;
flowValue.textContent = `${Math.round(flowPercent)}%`;
rainFill.style.transform = `scaleX(${rainPercent / 100})`;
levelFill.style.transform = `scaleX(${levelPercent / 100})`;
flowFill.style.transform = `scaleX(${flowPercent / 100})`;
const overflowing = overflowRate > 0.012;
if (overflowing) {
statusText.textContent = "Overflow active — level capped at outlet";
statusDot.classList.add("overflow");
} else if (!rainEnabled) {
statusText.textContent = "Rain paused — stored water is held";
statusDot.classList.remove("overflow");
} else if (waterLevel < OUTLET_Y - 0.015) {
statusText.textContent = "Filling toward the overflow outlet";
statusDot.classList.remove("overflow");
} else if (intensity < 0.02) {
statusText.textContent = "Reservoir full — waiting for next pulse";
statusDot.classList.remove("overflow");
} else {
statusText.textContent = "Rain pulse active";
statusDot.classList.remove("overflow");
}
}
// -------------------------------------------------------------------------
// Per-frame updates
// -------------------------------------------------------------------------
function updateSimulation(dt) {
simulationTime += dt;
const intensity = getRainIntensity(simulationTime);
const incomingWater = intensity * MAX_INFLOW;
if (waterLevel < OUTLET_Y - 0.00001) {
waterLevel = Math.min(
OUTLET_Y,
waterLevel + incomingWater * dt
);
overflowRate = 0;
} else {
// Hard cap: additional rain cannot increase stored volume.
waterLevel = OUTLET_Y;
overflowRate = incomingWater;
}
updateWaterMesh();
updateHUD(intensity);
return intensity;
}
function updateRain(dt, intensity) {
const activeCount = Math.floor(MAX_RAIN_DROPS * intensity);
rainGeometry.setDrawRange(0, activeCount * 2);
rainMaterial.opacity = 0.3 + intensity * 0.42;
for (let i = 0; i < activeCount; i++) {
const drop = drops[i];
drop.y -= drop.speed * dt;
drop.x -= dt * 0.18;
const radialDistance = Math.hypot(drop.x, drop.z);
if (
radialDistance < TANK_INNER_RADIUS &&
drop.y <= waterLevel
) {
spawnSplash(drop.x, drop.z);
resetDrop(drop);
} else if (drop.y < 0.03) {
resetDrop(drop);
}
const offset = i * 6;
rainPositions[offset] = drop.x;
rainPositions[offset + 1] = drop.y;
rainPositions[offset + 2] = drop.z;
rainPositions[offset + 3] = drop.x + 0.025;
rainPositions[offset + 4] = drop.y + drop.length;
rainPositions[offset + 5] = drop.z;
}
rainGeometry.attributes.position.needsUpdate = true;
for (const splash of splashRings) {
if (splash.age >= splash.duration) continue;
splash.age += dt;
const p = Math.min(1, splash.age / splash.duration);
splash.mesh.scale.setScalar(1 + p * 3.5);
splash.mesh.material.opacity = (1 - p) * 0.8;
if (p >= 1) {
splash.mesh.visible = false;
}
}
}
function updateWaterSurface(time) {
const positions = waterSurfaceGeo.attributes.position;
for (let i = 0; i < positions.count; i++) {
const x = positions.getX(i);
const y = positions.getY(i);
const wave =
Math.sin(x * 5.2 + time * 2.4) * 0.0035 +
Math.cos(y * 4.3 - time * 1.9) * 0.002;
positions.setZ(i, wave);
}
positions.needsUpdate = true;
waterSurfaceGeo.computeVertexNormals();
}
function updateFlow(dt) {
const flowRatio = THREE.MathUtils.clamp(
overflowRate / MAX_INFLOW,
0,
1
);
const activeCount =
overflowRate > 0.008
? Math.max(4, Math.round(MAX_FLOW_PARTICLES * flowRatio))
: 0;
flowMesh.count = activeCount;
flowOffset = (flowOffset + dt * (0.16 + flowRatio * 0.34)) % 1;
if (activeCount > 0) {
for (let i = 0; i < activeCount; i++) {
const t = (i / activeCount + flowOffset) % 1;
const point = pipeCurve.getPointAt(t);
dummy.position.copy(point);
dummy.quaternion.identity();
dummy.scale.setScalar(
0.78 + Math.sin((i + 1) * 2.1 + flowOffset * 9) * 0.18
);
dummy.updateMatrix();
flowMesh.setMatrixAt(i, dummy.matrix);
}
flowMesh.instanceMatrix.needsUpdate = true;
}
if (overflowRate > 0.01) {
const spawnChance = dt * overflowRate * 24;
if (Math.random() < spawnChance) spawnSpray();
drainRippleTime += dt;
const ripplePhase = (drainRippleTime * 1.5) % 1;
drainRipple.scale.setScalar(1 + ripplePhase * 3.2);
drainRipple.material.opacity = (1 - ripplePhase) * 0.55;
} else {
drainRipple.material.opacity *= Math.exp(-dt * 8);
}
}
function updateSpray(dt) {
for (const drop of sprayDrops) {
if (!drop.active) continue;
drop.life -= dt;
if (drop.life <= 0) {
drop.active = false;
drop.mesh.visible = false;
continue;
}
drop.velocity.y -= 5.2 * dt;
drop.mesh.position.addScaledVector(drop.velocity, dt);
drop.mesh.scale.setScalar(
Math.max(0.05, Math.min(1, drop.life * 4))
);
}
}
function updateEnvironment(time) {
clouds[0].position.x = -4.2 + Math.sin(time * 0.08) * 0.55;
clouds[1].position.x = 3.7 + Math.sin(time * 0.06 + 1.5) * 0.7;
}
function animate() {
const dt = Math.min(clock.getDelta(), 0.05);
const time = clock.elapsedTime;
updateCamera(dt);
const intensity = updateSimulation(dt);
updateRain(dt, intensity);
updateWaterSurface(time);
updateFlow(dt);
updateSpray(dt);
updateEnvironment(time);
renderer.render(scene, camera);
positionTag(tankTag, new THREE.Vector3(-0.2, 3.28, 0));
positionTag(overflowTag, new THREE.Vector3(1.85, 2.92, 0));
}
renderer.setAnimationLoop(animate);
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
});
</script>
</body>
</html>
```