Prompt specification
Build or customize the Three.js 3D scene "Blocked gutter and alternate drainage route" 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: Blocked gutter and alternate drainage route
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/blocked-gutter-and-alternate-drainage-route-x2220
Scene Description Prompt:
Please make a voxel Three.js scene in one standalone HTML file using cdnjs. Allow one roof gutter to become obstructed in a simplified rainfall scene. Show changed overflow location and compare an added overflow outlet while conserving illustrative water particles.
Working Three.js Starter (blocked-gutter-and-alternate-drainage-route-x2220):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Voxel Gutter Lab</title>
<style>
:root {
color-scheme: light;
--ink: #17313d;
--muted: #5d737d;
--panel: rgba(250, 253, 252, 0.91);
--line: rgba(25, 65, 77, 0.13);
--teal: #19b9ad;
--amber: #f1a53b;
--green: #55c878;
--blue: #46bce9;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #bed9e2;
color: var(--ink);
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
button {
font: inherit;
}
#scene {
position: fixed;
inset: 0;
width: 100%;
height: 100%;
display: block;
touch-action: none;
cursor: grab;
}
#panel {
position: fixed;
z-index: 10;
top: 18px;
left: 18px;
width: min(350px, calc(100vw - 36px));
max-height: calc(100vh - 36px);
overflow: auto;
padding: 20px;
border: 1px solid rgba(255, 255, 255, 0.78);
border-radius: 22px;
background: var(--panel);
box-shadow:
0 20px 60px rgba(29, 58, 67, 0.17),
inset 0 1px 0 rgba(255, 255, 255, 0.8);
backdrop-filter: blur(16px) saturate(1.15);
}
.eyebrow {
display: flex;
align-items: center;
gap: 8px;
margin-bottom: 8px;
color: #267b78;
font-size: 11px;
font-weight: 800;
letter-spacing: 0.16em;
text-transform: uppercase;
}
.eyebrow::before {
width: 9px;
height: 9px;
border-radius: 2px;
background: var(--teal);
box-shadow: 0 0 0 4px rgba(25, 185, 173, 0.12);
content: "";
}
h1 {
margin: 0;
font-size: clamp(27px, 4vw, 38px);
line-height: 1.02;
letter-spacing: -0.045em;
}
.intro {
margin: 12px 0 18px;
color: var(--muted);
font-size: 13px;
line-height: 1.5;
}
.section-title {
display: flex;
justify-content: space-between;
align-items: center;
margin: 0 0 8px;
color: #4e6872;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.14em;
text-transform: uppercase;
}
.section-title span {
color: #8aa0a8;
font-weight: 650;
letter-spacing: 0.03em;
text-transform: none;
}
.scenarios {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 7px;
}
.scenario {
min-height: 68px;
padding: 9px 7px;
border: 1px solid var(--line);
border-radius: 13px;
background: rgba(255, 255, 255, 0.65);
color: #36515c;
cursor: pointer;
transition:
transform 150ms ease,
border-color 150ms ease,
background 150ms ease,
box-shadow 150ms ease;
}
.scenario:hover {
transform: translateY(-2px);
border-color: rgba(25, 185, 173, 0.4);
}
.scenario:focus-visible {
outline: 3px solid rgba(70, 188, 233, 0.35);
outline-offset: 2px;
}
.scenario .number {
display: block;
margin-bottom: 7px;
color: #8ba0a8;
font-size: 9px;
font-weight: 850;
letter-spacing: 0.12em;
}
.scenario strong {
display: block;
font-size: 11px;
line-height: 1.2;
}
.scenario small {
display: block;
margin-top: 4px;
color: #7b9098;
font-size: 9px;
}
.scenario.active {
border-color: rgba(25, 185, 173, 0.66);
background: linear-gradient(145deg, #e9fffa, #f9fffd);
box-shadow: 0 7px 18px rgba(25, 185, 173, 0.12);
color: #116c68;
}
.scenario[data-mode="blocked"].active {
border-color: rgba(241, 165, 59, 0.68);
background: linear-gradient(145deg, #fff3dc, #fffbf3);
color: #965c10;
}
.scenario[data-mode="outlet"].active {
border-color: rgba(85, 200, 120, 0.7);
background: linear-gradient(145deg, #e5fbe9, #f8fff9);
color: #24743d;
}
.status {
position: relative;
margin-top: 12px;
padding: 13px 13px 13px 16px;
overflow: hidden;
border: 1px solid rgba(25, 185, 173, 0.15);
border-radius: 14px;
background: rgba(229, 251, 247, 0.66);
}
.status::before {
position: absolute;
inset: 0 auto 0 0;
width: 4px;
background: var(--teal);
content: "";
}
.status[data-mode="blocked"] {
border-color: rgba(241, 165, 59, 0.2);
background: rgba(255, 242, 217, 0.7);
}
.status[data-mode="blocked"]::before {
background: var(--amber);
}
.status[data-mode="outlet"] {
border-color: rgba(85, 200, 120, 0.21);
background: rgba(230, 250, 234, 0.74);
}
.status[data-mode="outlet"]::before {
background: var(--green);
}
.status strong {
display: block;
margin-bottom: 4px;
font-size: 12px;
}
.status p {
margin: 0;
color: #61767f;
font-size: 11px;
line-height: 1.45;
}
.budget {
display: grid;
grid-template-columns: auto 1fr;
gap: 2px 11px;
align-items: center;
margin-top: 12px;
padding: 12px;
border: 1px solid var(--line);
border-radius: 14px;
background: rgba(255, 255, 255, 0.58);
}
.budget-number {
grid-row: span 2;
color: #187c78;
font-size: 29px;
font-weight: 850;
letter-spacing: -0.05em;
}
.budget-title {
font-size: 11px;
font-weight: 800;
}
.budget-detail {
color: #7b9098;
font-size: 9px;
line-height: 1.35;
}
.budget-note {
margin: 7px 2px 0;
color: #71868e;
font-size: 9.5px;
line-height: 1.45;
}
.legend {
display: flex;
flex-wrap: wrap;
gap: 7px 12px;
margin-top: 13px;
color: #667d86;
font-size: 9.5px;
}
.legend span {
display: inline-flex;
align-items: center;
gap: 5px;
}
.dot {
width: 7px;
height: 7px;
border-radius: 2px;
background: var(--blue);
}
.dot.normal { background: #35d7c8; }
.dot.blocked { background: #ffb74d; }
.dot.outlet { background: #6cdb87; }
.actions {
display: flex;
gap: 8px;
margin-top: 16px;
}
.action {
flex: 1;
padding: 9px 10px;
border: 1px solid var(--line);
border-radius: 11px;
background: rgba(255, 255, 255, 0.72);
color: #45616b;
font-size: 10px;
font-weight: 750;
cursor: pointer;
}
.action:hover {
border-color: rgba(25, 185, 173, 0.42);
color: #147873;
}
.tiny-note {
margin: 12px 0 0;
color: #84979e;
font-size: 9px;
line-height: 1.45;
}
#top-stats {
position: fixed;
z-index: 8;
top: 18px;
right: 18px;
display: flex;
gap: 8px;
align-items: center;
padding: 10px 13px;
border: 1px solid rgba(255, 255, 255, 0.7);
border-radius: 13px;
background: rgba(248, 253, 253, 0.78);
box-shadow: 0 10px 30px rgba(35, 69, 79, 0.12);
backdrop-filter: blur(12px);
color: #506b74;
font-size: 10px;
font-weight: 750;
}
#top-stats b {
color: #187c78;
font-size: 12px;
}
#help {
position: fixed;
z-index: 8;
right: 18px;
bottom: 16px;
padding: 9px 12px;
border-radius: 11px;
background: rgba(24, 49, 61, 0.72);
color: rgba(255, 255, 255, 0.86);
font-size: 10px;
pointer-events: none;
backdrop-filter: blur(8px);
}
#labels {
position: fixed;
inset: 0;
z-index: 5;
overflow: hidden;
pointer-events: none;
}
.tag {
position: absolute;
display: none;
transform: translate(-50%, -50%);
padding: 6px 8px;
border: 1px solid rgba(255, 255, 255, 0.8);
border-radius: 8px;
background: rgba(24, 49, 61, 0.83);
box-shadow: 0 5px 16px rgba(22, 44, 52, 0.15);
color: white;
font-size: 9px;
font-weight: 800;
letter-spacing: 0.08em;
text-transform: uppercase;
white-space: nowrap;
}
.tag.normal {
background: rgba(14, 132, 123, 0.9);
}
.tag.blocked {
background: rgba(184, 105, 20, 0.92);
}
.tag.outlet {
background: rgba(38, 124, 62, 0.92);
}
#loading {
position: fixed;
z-index: 30;
inset: 0;
display: grid;
place-items: center;
background: #c8dfe5;
color: #31525c;
font-size: 12px;
font-weight: 800;
letter-spacing: 0.13em;
text-transform: uppercase;
transition: opacity 300ms ease;
}
#loading.hidden {
opacity: 0;
pointer-events: none;
}
@media (max-width: 720px) {
#panel {
top: auto;
right: 10px;
bottom: 10px;
left: 10px;
width: auto;
max-height: 43vh;
padding: 14px;
border-radius: 17px;
}
h1 {
font-size: 25px;
}
.intro,
.budget-note,
.legend,
.tiny-note {
display: none;
}
#top-stats {
top: 10px;
right: 10px;
}
#help {
display: none;
}
.scenarios {
margin-top: 8px;
}
.scenario {
min-height: 52px;
padding: 7px;
}
.scenario small {
display: none;
}
}
</style>
</head>
<body>
<canvas id="scene" aria-label="Interactive voxel house and gutter rainfall scene"></canvas>
<div id="labels" aria-hidden="true">
<div id="tag-normal" class="tag normal">Normal outlet</div>
<div id="tag-blocker" class="tag blocked">Gutter blockage</div>
<div id="tag-spill" class="tag blocked">Changed overflow location</div>
<div id="tag-outlet" class="tag outlet">Added overflow outlet</div>
</div>
<div id="top-stats">
<span>RAINFALL</span>
<b>480 FIXED SLOTS</b>
<span>NO EXTRA EMITTERS</span>
</div>
<aside id="panel">
<div class="eyebrow">Voxel runoff study</div>
<h1>One gutter.<br>Three outcomes.</h1>
<p class="intro">
Block the front gutter and watch runoff leave from a different place.
Add the side outlet to compare a controlled discharge.
</p>
<div class="section-title">
Compare scenario
<span>keys 1 / 2 / 3</span>
</div>
<div class="scenarios">
<button class="scenario active" data-mode="clear" type="button">
<span class="number">01</span>
<strong>Clear</strong>
<small>Right downspout</small>
</button>
<button class="scenario" data-mode="blocked" type="button">
<span class="number">02</span>
<strong>Obstructed</strong>
<small>Low-point spill</small>
</button>
<button class="scenario" data-mode="outlet" type="button">
<span class="number">03</span>
<strong>+ Outlet</strong>
<small>Captured overflow</small>
</button>
</div>
<div id="status" class="status" data-mode="clear" aria-live="polite">
<strong id="status-title">Normal downspout</strong>
<p id="status-copy">
Runoff stays in the gutter and exits through the right-hand downspout.
</p>
</div>
<div class="budget">
<div class="budget-number">480</div>
<div class="budget-title">fixed particle slots</div>
<div class="budget-detail">
300 rain + 24 roof runoff + 156 gutter runoff
</div>
</div>
<p class="budget-note">
Scenario changes re-route the same 156 runoff particles. The added outlet
does not create a second water emitter or increase the particle budget.
</p>
<div class="legend" aria-label="Water color legend">
<span><i class="dot"></i>Rain</span>
<span><i class="dot normal"></i>Normal flow</span>
<span><i class="dot blocked"></i>Uncontrolled overflow</span>
<span><i class="dot outlet"></i>Outlet flow</span>
</div>
<div class="actions">
<button id="pause" class="action" type="button">Pause rainfall</button>
<button id="reset-camera" class="action" type="button">Reset camera</button>
</div>
<p class="tiny-note">
Drag to orbit · Shift/right-drag to pan · Scroll to zoom.<br>
You can also click the blockage or added outlet directly.
</p>
</aside>
<div id="help">DRAG · ORBIT SHIFT/DRAG · PAN SCROLL · ZOOM</div>
<div id="loading">Building voxel scene…</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// -------------------------------------------------------------------------
// Constants and deterministic random source
// -------------------------------------------------------------------------
const RAIN_COUNT = 300;
const ROOF_COUNT = 24;
const ROUTE_COUNT = 156;
const PARTICLE_BUDGET = RAIN_COUNT + ROOF_COUNT + ROUTE_COUNT;
const canvas = document.querySelector("#scene");
const loading = document.querySelector("#loading");
const pauseButton = document.querySelector("#pause");
const resetCameraButton = document.querySelector("#reset-camera");
const statusBox = document.querySelector("#status");
const statusTitle = document.querySelector("#status-title");
const statusCopy = document.querySelector("#status-copy");
const scenarioButtons = [...document.querySelectorAll("[data-mode]")];
let randomSeed = 812734;
function random() {
randomSeed =
(Math.imul(randomSeed, 1664525) + 1013904223) >>> 0;
return randomSeed / 4294967296;
}
function range(min, max) {
return min + (max - min) * random();
}
function choose(array) {
return array[Math.floor(random() * array.length)];
}
// -------------------------------------------------------------------------
// Renderer, scene, lighting and camera
// -------------------------------------------------------------------------
const renderer = new THREE.WebGLRenderer({
canvas,
antialias: true,
alpha: false,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight, false);
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.08;
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xc4dce3);
scene.fog = new THREE.Fog(0xc4dce3, 18, 36);
const camera = new THREE.PerspectiveCamera(
38,
window.innerWidth / window.innerHeight,
0.1,
80
);
const hemisphere = new THREE.HemisphereLight(0xe9f8ff, 0x65745c, 2.1);
scene.add(hemisphere);
const sun = new THREE.DirectionalLight(0xfff5df, 3.1);
sun.position.set(-7, 13, 8);
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 = 35;
sun.shadow.bias = -0.00035;
scene.add(sun);
const fill = new THREE.DirectionalLight(0x9bd7ef, 0.7);
fill.position.set(9, 7, -8);
scene.add(fill);
// -------------------------------------------------------------------------
// Custom camera controls. No examples/addons are imported.
// -------------------------------------------------------------------------
const cameraControl = {
target: new THREE.Vector3(0, 1.9, 0),
radius: 15.6,
theta: 0.66,
phi: 1.08,
initialRadius: 15.6,
initialTheta: 0.66,
initialPhi: 1.08
};
function updateCamera() {
const sinPhi = Math.sin(cameraControl.phi);
camera.position.set(
cameraControl.target.x +
cameraControl.radius * sinPhi * Math.sin(cameraControl.theta),
cameraControl.target.y +
cameraControl.radius * Math.cos(cameraControl.phi),
cameraControl.target.z +
cameraControl.radius * sinPhi * Math.cos(cameraControl.theta)
);
camera.lookAt(cameraControl.target);
}
updateCamera();
// -------------------------------------------------------------------------
// Reusable voxel construction helpers
// -------------------------------------------------------------------------
const unitBox = new THREE.BoxGeometry(1, 1, 1);
const materialCache = new Map();
function getMaterial(
color,
{ roughness = 0.82, metalness = 0 } = {}
) {
const key = `${color}:${roughness}:${metalness}`;
if (!materialCache.has(key)) {
materialCache.set(
key,
new THREE.MeshStandardMaterial({
color,
roughness,
metalness
})
);
}
return materialCache.get(key);
}
function addBox(parent, size, position, color, options = {}) {
const material =
options.material || getMaterial(color, options);
const mesh = new THREE.Mesh(unitBox, material);
mesh.scale.set(size[0], size[1], size[2]);
mesh.position.set(position[0], position[1], position[2]);
mesh.castShadow = options.castShadow !== false;
mesh.receiveShadow = options.receiveShadow !== false;
parent.add(mesh);
return mesh;
}
function addPuddle(x, z, color, opacity, spread = 1) {
const group = new THREE.Group();
group.position.set(x, 0.025, z);
const material = new THREE.MeshStandardMaterial({
color,
transparent: true,
opacity,
roughness: 0.18,
metalness: 0.08,
depthWrite: false
});
addBox(
group,
[1.25 * spread, 0.035, 0.72 * spread],
[0, 0, 0],
color,
{ material, castShadow: false }
);
addBox(
group,
[0.65 * spread, 0.036, 0.5 * spread],
[0.72 * spread, 0.004, 0.18 * spread],
color,
{ material, castShadow: false }
);
addBox(
group,
[0.55 * spread, 0.037, 0.45 * spread],
[-0.55 * spread, 0.008, -0.12 * spread],
color,
{ material, castShadow: false }
);
scene.add(group);
return group;
}
// -------------------------------------------------------------------------
// Voxel terrain
// -------------------------------------------------------------------------
const ground = new THREE.Group();
scene.add(ground);
const groundColors = [
0x789765,
0x7f9e6b,
0x71905f,
0x86a472,
0x789765
];
const cell = 0.62;
const groundColumns = 16;
const groundRows = 12;
for (let ix = 0; ix < groundColumns; ix++) {
for (let iz = 0; iz < groundRows; iz++) {
const x = (ix - (groundColumns - 1) / 2) * cell;
const z = (iz - (groundRows - 1) / 2) * cell;
const color = choose(groundColors);
addBox(
ground,
[0.58, 0.22, 0.58],
[x, -0.12, z],
color,
{ roughness: 1 }
);
}
}
// Foundation and a small path of stepping stones.
addBox(
scene,
[6.8, 0.3, 4.6],
[0, 0.12, 0],
0xb7aa91,
{ roughness: 0.95 }
);
for (let i = 0; i < 5; i++) {
addBox(
scene,
[0.72, 0.08, 0.5],
[1.75, 0.04, 3.25 + i * 0.57],
i % 2 ? 0xc8bea8 : 0xb8ad98,
{ roughness: 1 }
);
}
// -------------------------------------------------------------------------
// Voxel house
// -------------------------------------------------------------------------
const house = new THREE.Group();
scene.add(house);
addBox(
house,
[6.4, 3.1, 4.15],
[0, 1.77, 0],
0xe4d6b8
);
// Front brick voxels.
const brickColors = [0xe6d8ba, 0xddcba8, 0xeadfc5, 0xe2d0af];
for (let row = 0; row < 6; row++) {
for (let column = 0; column < 6; column++) {
addBox(
house,
[0.98, 0.47, 0.16],
[
-2.65 + column * 1.06,
0.53 + row * 0.5,
2.13
],
choose(brickColors)
);
}
}
// Side brick bands.
for (let row = 0; row < 6; row++) {
for (let column = 0; column < 4; column++) {
addBox(
house,
[0.15, 0.47, 0.98],
[
3.25,
0.53 + row * 0.5,
-1.6 + column * 1.06
],
choose(brickColors)
);
}
}
// Corner posts and trim.
addBox(house, [0.25, 3.15, 0.25], [-3.12, 1.78, 2.15], 0x8f7258);
addBox(house, [0.25, 3.15, 0.25], [3.12, 1.78, 2.15], 0x8f7258);
addBox(house, [6.45, 0.18, 0.22], [0, 3.16, 2.17], 0x84684f);
// Door.
addBox(house, [1.1, 2.15, 0.18], [1.72, 1.3, 2.26], 0x744b36);
addBox(house, [0.09, 0.09, 0.09], [2.12, 1.35, 2.39], 0xe3b75c);
addBox(house, [0.8, 0.12, 0.2], [1.72, 2.42, 2.3], 0x5d3e31);
// Front windows.
function addWindow(x, y, z, width = 0.9) {
addBox(house, [width + 0.16, 0.98, 0.13], [x, y, z], 0x775f4c);
addBox(
house,
[width, 0.82, 0.08],
[x, y, z + 0.08],
0x7ec7d8,
{ roughness: 0.25, metalness: 0.08 }
);
addBox(house, [0.065, 0.82, 0.11], [x, y, z + 0.13], 0xd7e5dc);
addBox(house, [width, 0.065, 0.11], [x, y, z + 0.13], 0xd7e5dc);
}
addWindow(-1.72, 1.85, 2.28);
addWindow(-0.2, 1.85, 2.28);
// Side window.
addBox(house, [0.13, 1.05, 1.2], [3.34, 1.9, -0.25], 0x755d49);
addBox(
house,
[0.08, 0.88, 1.02],
[3.42, 1.9, -0.25],
0x7ec7d8,
{ roughness: 0.25, metalness: 0.08 }
);
addBox(house, [0.11, 0.88, 0.06], [3.47, 1.9, -0.25], 0xd7e5dc);
// Stepped voxel roof.
const roofColors = [0x315d68, 0x386873, 0x2e5964, 0x3d6d76];
for (let row = 0; row < 7; row++) {
const z = -2.1 + row * 0.75;
const roofY = 4.95 - row * 0.2;
for (let column = 0; column < 10; column++) {
const x = -3.15 + column * 0.7;
addBox(
house,
[0.66, 0.22, 0.72],
[x, roofY, z],
choose(roofColors),
{ roughness: 0.9 }
);
}
}
addBox(house, [6.9, 0.32, 0.2], [0, 3.48, 2.43], 0x6e513c);
// Chimney.
addBox(house, [0.58, 1.25, 0.58], [-2.05, 5.42, -0.78], 0x9f7964);
addBox(house, [0.72, 0.18, 0.72], [-2.05, 6.1, -0.78], 0x705447);
// -------------------------------------------------------------------------
// Gutters, downspout, obstruction and added overflow outlet
// -------------------------------------------------------------------------
const gutterY = 3.48;
const gutterZ = 2.88;
addBox(scene, [7.2, 0.12, 0.44], [0, gutterY, gutterZ], 0x3c4a4d);
addBox(scene, [7.2, 0.22, 0.08], [0, gutterY + 0.05, gutterZ + 0.21], 0x526164);
addBox(scene, [7.2, 0.22, 0.08], [0, gutterY + 0.05, gutterZ - 0.21], 0x526164);
const gutterWaterMaterial = new THREE.MeshStandardMaterial({
color: 0x5fc8ee,
transparent: true,
opacity: 0.48,
roughness: 0.2,
depthWrite: false
});
addBox(
scene,
[7.0, 0.045, 0.26],
[0, gutterY + 0.075, gutterZ],
0x5fc8ee,
{ material: gutterWaterMaterial, castShadow: false }
);
const gutterPoolMaterial = new THREE.MeshStandardMaterial({
color: 0x48bde9,
transparent: true,
opacity: 0.68,
roughness: 0.16,
depthWrite: false
});
const gutterPool = addBox(
scene,
[3.35, 0.08, 0.31],
[-1.75, gutterY + 0.11, gutterZ],
0x48bde9,
{ material: gutterPoolMaterial, castShadow: false }
);
gutterPool.visible = false;
// Normal right-side downspout.
const normalOutlet = new THREE.Group();
scene.add(normalOutlet);
addBox(normalOutlet, [0.24, 2.85, 0.24], [3.55, 1.95, 3.0], 0x53676a);
addBox(normalOutlet, [0.24, 0.24, 0.72], [3.55, 0.55, 3.28], 0x53676a);
addBox(normalOutlet, [0.3, 0.3, 0.3], [3.55, 3.25, 2.93], 0x76898b);
addBox(normalOutlet, [0.62, 0.06, 0.48], [3.65, 0.04, 3.55], 0x37474a);
for (let i = 0; i < 5; i++) {
addBox(
normalOutlet,
[0.55, 0.045, 0.055],
[3.65, 0.09, 3.34 + i * 0.1],
0x91a1a1,
{ castShadow: false }
);
}
// A single removable-looking obstruction in the front gutter.
const blockerGroup = new THREE.Group();
blockerGroup.position.set(-0.15, gutterY + 0.2, gutterZ);
blockerGroup.userData.action = "toggle-block";
scene.add(blockerGroup);
const leafColors = [0x8a6137, 0x657a3d, 0xb7833d, 0x596c38];
for (let i = 0; i < 9; i++) {
addBox(
blockerGroup,
[
range(0.22, 0.46),
range(0.16, 0.28),
range(0.22, 0.42)
],
[range(-0.38, 0.38), range(-0.03, 0.17), range(-0.18, 0.18)],
choose(leafColors)
);
}
addBox(
blockerGroup,
[0.8, 0.08, 0.08],
[0.03, 0.13, 0],
0x6f472d
);
// A visible low-point lip where the obstructed gutter overflows.
const spillMarkerMaterial = new THREE.MeshStandardMaterial({
color: 0xffb74d,
emissive: 0xd66d10,
emissiveIntensity: 0.55,
roughness: 0.38
});
const spillMarker = addBox(
scene,
[0.14, 0.28, 0.48],
[-1.65, gutterY + 0.16, gutterZ],
0xffb74d,
{ material: spillMarkerMaterial }
);
// Added side overflow outlet.
const outletGroup = new THREE.Group();
outletGroup.userData.action = "toggle-outlet";
scene.add(outletGroup);
addBox(
outletGroup,
[0.24, 0.24, 0.95],
[-1.65, gutterY + 0.02, 3.28],
0x557074
);
addBox(
outletGroup,
[0.24, 2.85, 0.24],
[-1.65, 1.95, 3.66],
0x557074
);
addBox(
outletGroup,
[0.24, 0.24, 0.62],
[-1.65, 0.57, 3.91],
0x557074
);
addBox(
outletGroup,
[0.34, 0.34, 0.34],
[-1.65, gutterY + 0.02, 3.57],
0x62c987
);
addBox(
outletGroup,
[0.62, 0.06, 0.48],
[-1.65, 0.04, 4.05],
0x37474a
);
for (let i = 0; i < 5; i++) {
addBox(
outletGroup,
[0.55, 0.045, 0.055],
[-1.65, 0.09, 3.83 + i * 0.1],
0x91a1a1,
{ castShadow: false }
);
}
const normalMarkerMaterial = new THREE.MeshStandardMaterial({
color: 0x35d7c8,
emissive: 0x0a8e86,
emissiveIntensity: 0.55,
roughness: 0.35
});
const normalMarker = addBox(
scene,
[0.3, 0.3, 0.3],
[3.55, 3.2, 3.0],
0x35d7c8,
{ material: normalMarkerMaterial }
);
const outletMarkerMaterial = new THREE.MeshStandardMaterial({
color: 0x6cdb87,
emissive: 0x238b43,
emissiveIntensity: 0.65,
roughness: 0.35
});
const outletMarker = addBox(
scene,
[0.32, 0.32, 0.32],
[-1.65, 3.52, 3.66],
0x6cdb87,
{ material: outletMarkerMaterial }
);
const normalPuddle = addPuddle(3.68, 3.55, 0x55cae9, 0.42, 0.9);
const spillPuddle = addPuddle(-1.65, 3.15, 0x64bfe4, 0.55, 1.1);
const outletPuddle = addPuddle(-1.65, 4.05, 0x64c7a2, 0.4, 0.8);
// -------------------------------------------------------------------------
// Trees and other voxel scenery
// -------------------------------------------------------------------------
function createTree(x, z, scale = 1) {
const tree = new THREE.Group();
tree.position.set(x, 0, z);
tree.scale.setScalar(scale);
addBox(tree, [0.38, 2.1, 0.38], [0, 1.05, 0], 0x765039);
addBox(tree, [1.45, 0.8, 1.2], [0, 2.15, 0], 0x4f784c);
addBox(tree, [1.1, 0.75, 1.35], [-0.35, 2.75, 0.12], 0x5d8b55);
addBox(tree, [1.25, 0.72, 1.0], [0.42, 2.72, -0.1], 0x426c43);
addBox(tree, [0.85, 0.7, 0.95], [0.05, 3.3, 0.05], 0x6b965d);
scene.add(tree);
}
createTree(-4.15, -1.55, 0.9);
createTree(4.15, -1.85, 0.78);
createTree(-4.35, 2.65, 0.58);
// Small bench.
addBox(scene, [1.35, 0.16, 0.42], [-3.35, 0.75, 1.05], 0x8c6848);
addBox(scene, [1.35, 0.55, 0.12], [-3.35, 1.08, 0.82], 0x8c6848);
addBox(scene, [0.12, 0.65, 0.12], [-3.88, 0.38, 1.05], 0x4c5653);
addBox(scene, [0.12, 0.65, 0.12], [-2.82, 0.38, 1.05], 0x4c5653);
// -------------------------------------------------------------------------
// Fixed particle pool and route definitions
// -------------------------------------------------------------------------
const dropGeometry = new THREE.BoxGeometry(0.055, 0.22, 0.055);
const dropMaterial = new THREE.MeshBasicMaterial({
color: 0xffffff,
transparent: true,
opacity: 0.88,
depthWrite: false
});
const dropMesh = new THREE.InstancedMesh(
dropGeometry,
dropMaterial,
PARTICLE_BUDGET
);
dropMesh.instanceMatrix.setUsage(THREE.DynamicDrawUsage);
dropMesh.frustumCulled = false;
dropMesh.renderOrder = 2;
scene.add(dropMesh);
const rainParticles = Array.from({ length: RAIN_COUNT }, () => ({
x: range(-6.6, 6.6),
y: range(4.2, 11.5),
z: range(-5.2, 5.2),
speed: range(5.4, 8.2),
length: range(0.75, 1.65)
}));
const roofParticles = Array.from({ length: ROOF_COUNT }, (_, index) => ({
x: range(-3.0, -1.8),
phase: index / ROOF_COUNT,
rate: range(0.92, 1.08)
}));
const routeParticles = Array.from({ length: ROUTE_COUNT }, (_, index) => ({
phase: index / ROUTE_COUNT,
rate: range(0.94, 1.06)
}));
function makePath(points) {
const pathPoints = points.map((point) => new THREE.Vector3(...point));
const cumulative = [0];
for (let i = 1; i < pathPoints.length; i++) {
cumulative.push(
cumulative[i - 1] + pathPoints[i].distanceTo(pathPoints[i - 1])
);
}
return {
points: pathPoints,
cumulative,
length: cumulative[cumulative.length - 1]
};
}
const routePaths = {
clear: makePath([
[-3.4, 3.59, 2.88],
[3.48, 3.59, 2.88],
[3.55, 3.15, 3.0],
[3.55, 0.65, 3.0],
[4.08, 0.16, 3.32]
]),
blocked: makePath([
[-3.4, 3.59, 2.88],
[-1.65, 3.59, 2.88],
[-1.65, 3.25, 3.03],
[-1.65, 0.2, 3.14],
[-1.12, 0.12, 3.23],
[-1.65, 0.11, 2.96]
]),
outlet: makePath([
[-3.4, 3.59, 2.88],
[-1.65, 3.59, 2.88],
[-1.65, 3.52, 3.66],
[-1.65, 0.65, 3.66],
[-1.65, 0.16, 4.02]
])
};
const roofPath = makePath([
[-2.5, 5.1, -2.0],
[-2.5, 4.45, -0.45],
[-2.5, 3.88, 2.28],
[-2.5, 3.59, 2.88]
]);
function samplePath(path, progress, target) {
const distance = ((progress % 1) + 1) % 1 * path.length;
for (let i = 1; i < path.points.length; i++) {
if (distance <= path.cumulative[i]) {
const segmentLength =
path.cumulative[i] - path.cumulative[i - 1];
const local = (distance - path.cumulative[i - 1]) / segmentLength;
target.lerpVectors(
path.points[i - 1],
path.points[i],
local
);
return target;
}
}
target.copy(path.points[path.points.length - 1]);
return target;
}
const modeColors = {
clear: new THREE.Color(0x35d7c8),
blocked: new THREE.Color(0xffb74d),
outlet: new THREE.Color(0x6cdb87)
};
// Initialize instance colors.
const rainColor = new THREE.Color(0x76c9f1);
const roofColor = new THREE.Color(0x65d8ef);
for (let i = 0; i < RAIN_COUNT; i++) {
dropMesh.setColorAt(i, rainColor);
}
for (let i = 0; i < ROOF_COUNT; i++) {
dropMesh.setColorAt(RAIN_COUNT + i, roofColor);
}
for (let i = 0; i < ROUTE_COUNT; i++) {
dropMesh.setColorAt(
RAIN_COUNT + ROOF_COUNT + i,
modeColors.clear
);
}
dropMesh.instanceColor.needsUpdate = true;
// -------------------------------------------------------------------------
// Scenario state
// -------------------------------------------------------------------------
let mode = "clear";
let paused = false;
let simulationTime = 0;
const modeDetails = {
clear: {
title: "Normal downspout",
copy:
"Runoff stays in the gutter and exits through the right-hand downspout.",
color: "#19b9ad"
},
blocked: {
title: "Overflow moves to the low point",
copy:
"The blockage causes water to back up and leave at the amber low-point lip.",
color: "#f1a53b"
},
outlet: {
title: "Added outlet captures the overflow",
copy:
"The same runoff is captured by the green side outlet and sent to a separate drain.",
color: "#55c878"
}
};
function setMode(nextMode) {
if (!modeDetails[nextMode]) return;
mode = nextMode;
blockerGroup.visible = mode !== "clear";
outletGroup.visible = mode === "outlet";
gutterPool.visible = mode !== "clear";
gutterPoolMaterial.opacity = mode === "blocked" ? 0.68 : 0.3;
normalPuddle.visible = mode === "clear";
spillPuddle.visible = mode === "blocked";
outletPuddle.visible = mode === "outlet";
normalMarker.visible = mode === "clear";
spillMarker.visible = mode === "blocked";
outletMarker.visible = mode === "outlet";
for (let i = 0; i < ROUTE_COUNT; i++) {
dropMesh.setColorAt(
RAIN_COUNT + ROOF_COUNT + i,
modeColors[mode]
);
}
dropMesh.instanceColor.needsUpdate = true;
scenarioButtons.forEach((button) => {
const active = button.dataset.mode === mode;
button.classList.toggle("active", active);
button.setAttribute("aria-pressed", String(active));
});
const details = modeDetails[mode];
statusBox.dataset.mode = mode;
statusTitle.textContent = details.title;
statusCopy.textContent = details.copy;
}
scenarioButtons.forEach((button) => {
button.addEventListener("click", () => {
setMode(button.dataset.mode);
});
});
pauseButton.addEventListener("click", () => {
paused = !paused;
pauseButton.textContent = paused ? "Resume rainfall" : "Pause rainfall";
pauseButton.setAttribute("aria-pressed", String(paused));
});
resetCameraButton.addEventListener("click", () => {
cameraControl.target.set(0, 1.9, 0);
cameraControl.radius = cameraControl.initialRadius;
cameraControl.theta = cameraControl.initialTheta;
cameraControl.phi = cameraControl.initialPhi;
});
// -------------------------------------------------------------------------
// Direct camera interaction and object picking
// -------------------------------------------------------------------------
const raycaster = new THREE.Raycaster();
const pointer = new THREE.Vector2();
const interactiveObjects = [blockerGroup, outletGroup];
let dragState = null;
function setPointerFromEvent(event) {
const rect = canvas.getBoundingClientRect();
pointer.x =
((event.clientX - rect.left) / rect.width) * 2 - 1;
pointer.y =
-((event.clientY - rect.top) / rect.height) * 2 + 1;
}
function findAction(object) {
let current = object;
while (current) {
if (current.userData.action) {
const action = current.userData.action;
if (
action === "toggle-block" &&
mode === "clear"
) {
return null;
}
if (
action === "toggle-outlet" &&
mode !== "outlet"
) {
return null;
}
return action;
}
current = current.parent;
}
return null;
}
function pickAction(event) {
setPointerFromEvent(event);
raycaster.setFromCamera(pointer, camera);
const intersections = raycaster.intersectObjects(
interactiveObjects,
true
);
for (const intersection of intersections) {
const action = findAction(intersection.object);
if (action) return action;
}
return null;
}
canvas.addEventListener("pointerdown", (event) => {
canvas.setPointerCapture(event.pointerId);
dragState = {
pointerId: event.pointerId,
startX: event.clientX,
startY: event.clientY,
lastX: event.clientX,
lastY: event.clientY,
moved: false,
pan: event.button === 2 || event.shiftKey
};
canvas.style.cursor = "grabbing";
});
canvas.addEventListener("pointermove", (event) => {
if (!dragState) {
const action = pickAction(event);
canvas.style.cursor = action ? "pointer" : "grab";
return;
}
const dx = event.clientX - dragState.lastX;
const dy = event.clientY - dragState.lastY;
if (
Math.abs(event.clientX - dragState.startX) +
Math.abs(event.clientY - dragState.startY) >
4
) {
dragState.moved = true;
}
if (dragState.pan) {
const forward = new THREE.Vector3();
camera.getWorldDirection(forward);
forward.y = 0;
forward.normalize();
const right = new THREE.Vector3()
.crossVectors(forward, camera.up)
.normalize();
const panScale = cameraControl.radius * 0.0012;
cameraControl.target.addScaledVector(right, -dx * panScale);
cameraControl.target.addScaledVector(forward, -dy * panScale);
cameraControl.target.x = THREE.MathUtils.clamp(
cameraControl.target.x,
-4,
4
);
cameraControl.target.z = THREE.MathUtils.clamp(
cameraControl.target.z,
-4,
4
);
} else {
cameraControl.theta -= dx * 0.006;
cameraControl.phi = THREE.MathUtils.clamp(
cameraControl.phi - dy * 0.005,
0.38,
1.43
);
}
dragState.lastX = event.clientX;
dragState.lastY = event.clientY;
});
canvas.addEventListener("pointerup", (event) => {
if (
dragState &&
!dragState.moved &&
event.button === 0
) {
const action = pickAction(event);
if (action === "toggle-block") {
setMode(mode === "clear" ? "blocked" : "clear");
} else if (action === "toggle-outlet") {
setMode(mode === "outlet" ? "blocked" : "outlet");
}
}
dragState = null;
canvas.style.cursor = "grab";
});
canvas.addEventListener("pointercancel", () => {
dragState = null;
canvas.style.cursor = "grab";
});
canvas.addEventListener("wheel", (event) => {
event.preventDefault();
cameraControl.radius = THREE.MathUtils.clamp(
cameraControl.radius * Math.exp(event.deltaY * 0.001),
9,
25
);
}, { passive: false });
canvas.addEventListener("contextmenu", (event) => {
event.preventDefault();
});
window.addEventListener("keydown", (event) => {
if (event.key === "1") setMode("clear");
if (event.key === "2") setMode("blocked");
if (event.key === "3") setMode("outlet");
if (event.key.toLowerCase() === "r") {
cameraControl.target.set(0, 1.9, 0);
cameraControl.radius = cameraControl.initialRadius;
cameraControl.theta = cameraControl.initialTheta;
cameraControl.phi = cameraControl.initialPhi;
}
if (
event.code === "Space" &&
event.target === document.body
) {
event.preventDefault();
pauseButton.click();
}
});
// -------------------------------------------------------------------------
// World-space HTML labels
// -------------------------------------------------------------------------
const projected = new THREE.Vector3();
function projectLabel(element, worldPosition, visible, offsetX = 0, offsetY = 0) {
if (!visible) {
element.style.display = "none";
return;
}
projected.copy(worldPosition).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;
}
const x = (projected.x * 0.5 + 0.5) * window.innerWidth;
const y = (-projected.y * 0.5 + 0.5) * window.innerHeight;
element.style.display = "block";
element.style.left = `${x + offsetX}px`;
element.style.top = `${y + offsetY}px`;
}
const tagNormal = document.querySelector("#tag-normal");
const tagBlocker = document.querySelector("#tag-blocker");
const tagSpill = document.querySelector("#tag-spill");
const tagOutlet = document.querySelector("#tag-outlet");
const normalLabelPosition = new THREE.Vector3(3.55, 2.65, 3.0);
const blockerLabelPosition = new THREE.Vector3(-0.15, 4.0, 2.88);
const spillLabelPosition = new THREE.Vector3(-1.65, 3.86, 2.88);
const outletLabelPosition = new THREE.Vector3(-1.65, 2.25, 3.66);
// -------------------------------------------------------------------------
// Animation
// -------------------------------------------------------------------------
const clock = new THREE.Clock();
const dummy = new THREE.Object3D();
const particlePosition = new THREE.Vector3();
function roofHeight(z) {
const row = THREE.MathUtils.clamp(
Math.round((z + 2.1) / 0.75),
0,
6
);
return 5.06 - row * 0.2;
}
function resetRain(particle) {
particle.x = range(-6.6, 6.6);
particle.y = range(7.2, 11.5);
particle.z = range(-5.2, 5.2);
particle.speed = range(5.4, 8.2);
}
function updateParticles(deltaTime) {
// Rain.
for (let i = 0; i < RAIN_COUNT; i++) {
const particle = rainParticles[i];
particle.y -= particle.speed * deltaTime;
const overRoof =
Math.abs(particle.x) < 3.55 &&
particle.z > -2.72 &&
particle.z < 2.78;
if (
particle.y < 0.12 ||
(overRoof && particle.y < roofHeight(particle.z) + 0.09)
) {
resetRain(particle);
}
dummy.position.set(
particle.x,
particle.y,
particle.z
);
dummy.rotation.set(0, 0, 0);
dummy.scale.set(1, particle.length, 1);
dummy.updateMatrix();
dropMesh.setMatrixAt(i, dummy.matrix);
}
// Roof runoff: also drawn from the same fixed particle pool.
for (let i = 0; i < ROOF_COUNT; i++) {
const particle = roofParticles[i];
const progress =
(particle.phase + simulationTime * 0.16 * particle.rate) % 1;
samplePath(roofPath, progress, particlePosition);
dummy.position.copy(particlePosition);
dummy.rotation.set(0, 0, 0);
dummy.scale.set(0.78, 0.82, 0.78);
dummy.updateMatrix();
dropMesh.setMatrixAt(RAIN_COUNT + i, dummy.matrix);
}
// Gutter and overflow runoff. These particles are never respawned when
// the scenario changes; their route is simply sampled from a new path.
const activePath = routePaths[mode];
for (let i = 0; i < ROUTE_COUNT; i++) {
const particle = routeParticles[i];
const progress =
(particle.phase + simulationTime * 0.085 * particle.rate) % 1;
samplePath(activePath, progress, particlePosition);
dummy.position.copy(particlePosition);
dummy.rotation.set(0, 0, 0);
dummy.scale.set(0.78, 0.86, 0.78);
dummy.updateMatrix();
dropMesh.setMatrixAt(
RAIN_COUNT + ROOF_COUNT + i,
dummy.matrix
);
}
dropMesh.instanceMatrix.needsUpdate = true;
}
function animate() {
const deltaTime = Math.min(clock.getDelta(), 0.05);
const motionDelta = paused ? 0 : deltaTime;
if (!paused) {
simulationTime += deltaTime;
}
updateParticles(motionDelta);
updateCamera();
// Gentle beacon movement.
normalMarker.rotation.y = simulationTime * 1.4;
spillMarker.rotation.y = -simulationTime * 1.6;
outletMarker.rotation.y = simulationTime * 1.8;
gutterWaterMaterial.opacity =
mode === "blocked"
? 0.64 + Math.sin(simulationTime * 3) * 0.05
: 0.44 + Math.sin(simulationTime * 2.2) * 0.025;
projectLabel(
tagNormal,
normalLabelPosition,
mode === "clear",
44,
-2
);
projectLabel(
tagBlocker,
blockerLabelPosition,
mode !== "clear",
18,
-26
);
projectLabel(
tagSpill,
spillLabelPosition,
mode === "blocked",
-64,
-26
);
projectLabel(
tagOutlet,
outletLabelPosition,
mode === "outlet",
-64,
-6
);
renderer.render(scene, camera);
}
function resize() {
const width = window.innerWidth;
const height = window.innerHeight;
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(width, height, false);
camera.aspect = width / height;
camera.updateProjectionMatrix();
}
window.addEventListener("resize", resize);
setMode("clear");
renderer.setAnimationLoop(animate);
requestAnimationFrame(() => {
loading.classList.add("hidden");
setTimeout(() => loading.remove(), 350);
});
</script>
</body>
</html>
```