Prompt specification
Build or customize the Three.js 3D scene "Rain follows the awning slope" 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 follows the awning slope
Category: Architecture & Cities · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/rain-follows-the-awning-slope-x0482
Scene Description Prompt:
I'd like to see this as a low-poly Three.js scene: rain follows the awning slope. Animate sparse rain droplets landing on the canopy and moving toward the gutter; compare a correctly sloped sample with a labeled sagging-pocket illustration beside it. Use cdnjs and return one self-contained HTML file.
Working Three.js Starter (rain-follows-the-awning-slope-x0482):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Rain on Awnings — Three.js Comparison</title>
<style>
:root {
color-scheme: dark;
--ink: #e9f7ff;
--muted: #93aabd;
--cyan: #64f1ff;
--amber: #ffad68;
--panel: rgba(8, 20, 34, 0.78);
--border: rgba(151, 207, 235, 0.2);
}
* { box-sizing: border-box; }
html, body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #07111f;
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;
color: var(--ink);
}
.masthead {
position: absolute;
top: 28px;
left: 30px;
max-width: 440px;
}
.eyebrow {
display: flex;
align-items: center;
gap: 9px;
margin-bottom: 9px;
color: var(--cyan);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.2em;
text-transform: uppercase;
}
.eyebrow::before {
content: "";
width: 24px;
height: 2px;
background: currentColor;
box-shadow: 0 0 12px currentColor;
}
h1 {
margin: 0;
font-size: clamp(24px, 3vw, 42px);
font-weight: 760;
letter-spacing: -0.045em;
line-height: 0.98;
}
.subtitle {
margin-top: 12px;
max-width: 390px;
color: var(--muted);
font-size: 12px;
line-height: 1.6;
}
.status {
position: absolute;
top: 28px;
right: 30px;
display: flex;
align-items: center;
gap: 10px;
padding: 10px 13px;
border: 1px solid var(--border);
border-radius: 999px;
background: var(--panel);
color: #bfd3e1;
font-size: 10px;
font-weight: 750;
letter-spacing: 0.12em;
text-transform: uppercase;
backdrop-filter: blur(12px);
}
.status-dot {
width: 7px;
height: 7px;
border-radius: 50%;
background: var(--cyan);
box-shadow: 0 0 13px var(--cyan);
animation: pulse 1.6s ease-in-out infinite;
}
@keyframes pulse {
0%, 100% { opacity: 0.45; transform: scale(0.75); }
50% { opacity: 1; transform: scale(1); }
}
.scene-label,
.flow-note {
position: absolute;
transform: translate(-50%, -100%);
white-space: nowrap;
backdrop-filter: blur(12px);
}
.scene-label {
display: flex;
align-items: center;
gap: 10px;
padding: 10px 13px 10px 10px;
border: 1px solid var(--border);
border-radius: 9px;
background: var(--panel);
box-shadow: 0 12px 35px rgba(0, 0, 0, 0.24);
}
.scene-label .id {
display: grid;
width: 28px;
height: 28px;
place-items: center;
border-radius: 7px;
background: rgba(100, 241, 255, 0.13);
color: var(--cyan);
font-size: 12px;
font-weight: 850;
}
.scene-label.sag .id {
color: var(--amber);
background: rgba(255, 173, 104, 0.13);
}
.label-title {
font-size: 11px;
font-weight: 820;
letter-spacing: 0.1em;
text-transform: uppercase;
}
.label-detail {
margin-top: 3px;
color: var(--muted);
font-size: 10px;
letter-spacing: 0.025em;
}
.flow-note {
padding: 6px 9px;
border: 1px solid rgba(100, 241, 255, 0.25);
border-radius: 5px;
background: rgba(5, 20, 32, 0.72);
color: var(--cyan);
font-size: 9px;
font-weight: 800;
letter-spacing: 0.13em;
text-transform: uppercase;
}
.flow-note.pocket {
border-color: rgba(255, 173, 104, 0.35);
color: var(--amber);
}
.bottom-bar {
position: absolute;
right: 30px;
bottom: 27px;
left: 30px;
display: flex;
align-items: flex-end;
justify-content: space-between;
gap: 20px;
}
.legend {
display: flex;
flex-wrap: wrap;
gap: 15px;
color: var(--muted);
font-size: 10px;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.legend span {
display: inline-flex;
align-items: center;
gap: 7px;
}
.legend i {
width: 18px;
height: 2px;
border-radius: 2px;
background: var(--cyan);
box-shadow: 0 0 8px rgba(100, 241, 255, 0.7);
}
.legend i.amber {
background: var(--amber);
box-shadow: 0 0 8px rgba(255, 173, 104, 0.6);
}
.help {
color: #8299aa;
font-size: 10px;
letter-spacing: 0.08em;
text-transform: uppercase;
}
@media (max-width: 700px) {
.masthead { left: 18px; top: 18px; max-width: 310px; }
.status { right: 18px; top: 18px; }
.status .status-text { display: none; }
.subtitle { font-size: 11px; max-width: 290px; }
.bottom-bar { left: 18px; right: 18px; bottom: 17px; }
.help { display: none; }
.scene-label { transform: translate(-50%, -100%) scale(0.84); }
.flow-note { transform: translate(-50%, -100%) scale(0.82); }
}
</style>
</head>
<body>
<div id="app"></div>
<div id="hud">
<div class="masthead">
<div class="eyebrow">Awning Hydrology Study</div>
<h1>Where does the rain go?</h1>
<div class="subtitle">
A live low-poly comparison of runoff over a correctly sloped canopy and a
sagging fabric panel with a local low point.
</div>
</div>
<div class="status">
<span class="status-dot"></span>
<span class="status-text">Live flow · <span id="dropCount">0</span> surface drops</span>
</div>
<div id="labelA" class="scene-label">
<div class="id">A</div>
<div>
<div class="label-title">Correctly Sloped</div>
<div class="label-detail">12° fall toward the gutter</div>
</div>
</div>
<div id="labelB" class="scene-label sag">
<div class="id">B</div>
<div>
<div class="label-title">Sagging Pocket</div>
<div class="label-detail">Local low point intercepts runoff</div>
</div>
</div>
<div id="gutterNote" class="flow-note">Slope → gutter</div>
<div id="pocketNote" class="flow-note pocket">Water traps here</div>
<div class="bottom-bar">
<div class="legend">
<span><i></i> Rain and runoff</span>
<span><i class="amber"></i> Problem area</span>
</div>
<div class="help">Drag to orbit · Scroll to zoom · Double-click to reset</div>
</div>
</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const app = document.getElementById("app");
const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.15;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.domElement.setAttribute(
"aria-label",
"Interactive 3D comparison of rain flowing over two awnings"
);
app.appendChild(renderer.domElement);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x07111f);
scene.fog = new THREE.FogExp2(0x07111f, 0.025);
const camera = new THREE.PerspectiveCamera(
40,
window.innerWidth / window.innerHeight,
0.1,
100
);
const orbit = {
target: new THREE.Vector3(0, 1.25, 0),
radius: 17,
theta: 0.12,
phi: 1.12
};
function updateCamera() {
const s = Math.sin(orbit.phi);
camera.position.set(
orbit.target.x + orbit.radius * s * Math.sin(orbit.theta),
orbit.target.y + orbit.radius * Math.cos(orbit.phi),
orbit.target.z + orbit.radius * s * Math.cos(orbit.theta)
);
camera.lookAt(orbit.target);
}
updateCamera();
const hemi = new THREE.HemisphereLight(0x9fdcff, 0x1a1424, 2.35);
scene.add(hemi);
const key = new THREE.DirectionalLight(0xe9f7ff, 3.25);
key.position.set(-5, 11, 8);
key.castShadow = true;
key.shadow.mapSize.set(2048, 2048);
key.shadow.camera.left = -10;
key.shadow.camera.right = 10;
key.shadow.camera.top = 10;
key.shadow.camera.bottom = -7;
key.shadow.camera.near = 1;
key.shadow.camera.far = 30;
key.shadow.bias = -0.0004;
scene.add(key);
scene.add(key.target);
const rim = new THREE.DirectionalLight(0x48bfff, 1.45);
rim.position.set(8, 5, -7);
scene.add(rim);
const warmRim = new THREE.PointLight(0xff8d52, 13, 12, 2);
warmRim.position.set(5, 3.5, -1.5);
scene.add(warmRim);
const groundMaterial = new THREE.MeshStandardMaterial({
color: 0x0a1724,
roughness: 0.96,
metalness: 0.02
});
const ground = new THREE.Mesh(new THREE.PlaneGeometry(42, 42), groundMaterial);
ground.rotation.x = -Math.PI / 2;
ground.position.y = -0.015;
ground.receiveShadow = true;
scene.add(ground);
const grid = new THREE.GridHelper(36, 36, 0x254458, 0x193043);
grid.position.y = 0.003;
grid.material.transparent = true;
grid.material.opacity = 0.2;
grid.material.depthWrite = false;
scene.add(grid);
const width = 5.0;
const back = -1.55;
const front = 1.55;
const depth = front - back;
const frontHeight = 1.78;
const backHeight = 2.42;
const samples = {
correct: { cx: -3.15 },
sag: { cx: 3.15 }
};
function correctHeight(x, z) {
return frontHeight + ((front - z) / depth) * (backHeight - frontHeight);
}
function sagHeight(x, z) {
const base = correctHeight(x, z);
const dx = x - 0.15;
const dz = z + 0.12;
const depression = 0.9 * Math.exp(-(dx * dx + dz * dz) / (0.72 * 0.72));
return base - depression;
}
const pocket = { x: 0.15, z: -0.06 };
pocket.y = sagHeight(pocket.x, pocket.z);
const metalMaterial = new THREE.MeshStandardMaterial({
color: 0x263c4e,
roughness: 0.42,
metalness: 0.68
});
const darkMetalMaterial = new THREE.MeshStandardMaterial({
color: 0x111d29,
roughness: 0.5,
metalness: 0.72
});
function addBox(parent, size, position, material, castShadow = true) {
const mesh = new THREE.Mesh(new THREE.BoxGeometry(...size), material);
mesh.position.set(...position);
mesh.castShadow = castShadow;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function cylinderBetween(start, end, radius, material, sides = 6) {
const direction = new THREE.Vector3().subVectors(end, start);
const length = direction.length();
const mesh = new THREE.Mesh(
new THREE.CylinderGeometry(radius, radius, length, sides),
material
);
mesh.position.copy(start).add(end).multiplyScalar(0.5);
mesh.quaternion.setFromUnitVectors(
new THREE.Vector3(0, 1, 0),
direction.normalize()
);
mesh.castShadow = true;
mesh.receiveShadow = true;
return mesh;
}
function createSamplePad(cx, accent) {
const pad = new THREE.Mesh(
new THREE.CircleGeometry(3.08, 40),
new THREE.MeshStandardMaterial({
color: 0x0d1d2b,
roughness: 0.95
})
);
pad.rotation.x = -Math.PI / 2;
pad.position.set(cx, 0.005, 0);
pad.receiveShadow = true;
scene.add(pad);
const ring = new THREE.Mesh(
new THREE.RingGeometry(3.04, 3.075, 48),
new THREE.MeshBasicMaterial({
color: accent,
transparent: true,
opacity: 0.23,
side: THREE.DoubleSide,
toneMapped: false
})
);
ring.rotation.x = -Math.PI / 2;
ring.position.set(cx, 0.012, 0);
scene.add(ring);
}
createSamplePad(samples.correct.cx, 0x5ceeff);
createSamplePad(samples.sag.cx, 0xff9e5c);
function createCanopy(config, heightFunction, baseColor, secondaryColor, accent) {
const nx = 8;
const nz = 6;
const positions = [];
const colors = [];
const base = new THREE.Color(baseColor);
const secondary = new THREE.Color(secondaryColor);
function point(ix, iz) {
const x = -width / 2 + (ix / nx) * width;
const z = back + (iz / nz) * depth;
return new THREE.Vector3(x, heightFunction(x, z), z);
}
function addTriangle(a, b, c, ix, iz, flip) {
const n = Math.sin((ix + 1) * 12.9898 + (iz + 1) * 78.233 + flip * 37.719);
const variation = (n - Math.floor(n)) * 0.12 - 0.06;
const color = ((ix + iz + flip) % 3 === 0 ? secondary : base).clone();
color.offsetHSL(0, 0, variation);
for (const v of [a, b, c]) {
positions.push(v.x, v.y, v.z);
colors.push(color.r, color.g, color.b);
}
}
for (let iz = 0; iz < nz; iz++) {
for (let ix = 0; ix < nx; ix++) {
const p00 = point(ix, iz);
const p10 = point(ix + 1, iz);
const p01 = point(ix, iz + 1);
const p11 = point(ix + 1, iz + 1);
addTriangle(p00, p01, p11, ix, iz, 0);
addTriangle(p00, p11, p10, ix, iz, 1);
}
}
const geometry = new THREE.BufferGeometry();
geometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(positions, 3)
);
geometry.setAttribute("color", new THREE.Float32BufferAttribute(colors, 3));
geometry.computeVertexNormals();
const material = new THREE.MeshStandardMaterial({
vertexColors: true,
roughness: 0.88,
metalness: 0.02,
flatShading: true,
side: THREE.DoubleSide
});
const canopy = new THREE.Mesh(geometry, material);
canopy.position.x = config.cx;
canopy.castShadow = true;
canopy.receiveShadow = true;
scene.add(canopy);
const edgeGeometry = new THREE.EdgesGeometry(geometry, 14);
const edgeMaterial = new THREE.LineBasicMaterial({
color: accent,
transparent: true,
opacity: 0.25,
toneMapped: false
});
const edges = new THREE.LineSegments(edgeGeometry, edgeMaterial);
edges.position.x = config.cx;
scene.add(edges);
const seamMaterial = new THREE.LineBasicMaterial({
color: 0x102635,
transparent: true,
opacity: 0.32
});
for (let seam = 1; seam < 5; seam++) {
const x = -width / 2 + (seam / 5) * width;
const points = [];
for (let j = 0; j <= 18; j++) {
const z = back + (j / 18) * depth;
points.push(new THREE.Vector3(x, heightFunction(x, z) + 0.012, z));
}
const seamLine = new THREE.Line(
new THREE.BufferGeometry().setFromPoints(points),
seamMaterial
);
seamLine.position.x = config.cx;
scene.add(seamLine);
}
return canopy;
}
createCanopy(
samples.correct,
correctHeight,
0x2caaa4,
0x267e8a,
0x74f7ff
);
createCanopy(
samples.sag,
sagHeight,
0xd98154,
0xb85c54,
0xffb16e
);
function createFrame(config) {
const group = new THREE.Group();
const cx = config.cx;
for (const side of [-1, 1]) {
const x = cx + side * (width / 2 - 0.22);
const postTop = new THREE.Vector3(x, backHeight + 0.02, back + 0.08);
const postBottom = new THREE.Vector3(x, 0.08, back + 0.08);
group.add(
cylinderBetween(postBottom, postTop, 0.055, metalMaterial, 6)
);
group.add(
cylinderBetween(
postTop,
new THREE.Vector3(x, frontHeight - 0.12, front - 0.08),
0.035,
metalMaterial,
6
)
);
addBox(
group,
[0.38, 0.055, 0.38],
[x, 0.035, back + 0.08],
darkMetalMaterial
);
}
addBox(
group,
[width - 0.28, 0.09, 0.09],
[cx, backHeight + 0.02, back + 0.08],
metalMaterial
);
const gutterZ = front + 0.11;
addBox(
group,
[width + 0.2, 0.065, 0.34],
[cx, frontHeight - 0.21, gutterZ],
darkMetalMaterial
);
addBox(
group,
[width + 0.2, 0.22, 0.055],
[cx, frontHeight - 0.09, front - 0.015],
metalMaterial
);
addBox(
group,
[width + 0.2, 0.22, 0.055],
[cx, frontHeight - 0.09, front + 0.245],
metalMaterial
);
scene.add(group);
}
createFrame(samples.correct);
createFrame(samples.sag);
const waterMaterial = new THREE.MeshBasicMaterial({
color: 0x5df4ff,
transparent: true,
opacity: 0.34,
depthWrite: false,
toneMapped: false
});
const gutterWater = addBox(
scene,
[width - 0.26, 0.026, 0.105],
[samples.correct.cx, frontHeight - 0.17, front + 0.11],
waterMaterial,
false
);
const gutterBeads = [];
const beadGeometry = new THREE.IcosahedronGeometry(0.045, 0);
const beadMaterial = new THREE.MeshBasicMaterial({
color: 0xb7fbff,
toneMapped: false
});
for (let i = 0; i < 6; i++) {
const bead = new THREE.Mesh(beadGeometry, beadMaterial);
scene.add(bead);
gutterBeads.push(bead);
}
const puddleMaterial = new THREE.MeshBasicMaterial({
color: 0x48e9f7,
transparent: true,
opacity: 0.23,
depthWrite: false,
toneMapped: false
});
const puddle = new THREE.Mesh(
new THREE.IcosahedronGeometry(0.48, 1),
puddleMaterial
);
puddle.scale.set(1.18, 0.075, 0.8);
puddle.position.set(
samples.sag.cx + pocket.x,
pocket.y + 0.035,
pocket.z
);
scene.add(puddle);
const pocketRings = [];
for (let i = 0; i < 3; i++) {
const ring = new THREE.Mesh(
new THREE.RingGeometry(0.18, 0.195, 32),
new THREE.MeshBasicMaterial({
color: 0x8af8ff,
transparent: true,
opacity: 0.42,
side: THREE.DoubleSide,
depthWrite: false,
toneMapped: false
})
);
ring.rotation.x = -Math.PI / 2;
ring.scale.set(1, 0.72, 1);
ring.position.set(
samples.sag.cx + pocket.x,
pocket.y + 0.075,
pocket.z
);
scene.add(ring);
pocketRings.push(ring);
}
const marker = new THREE.Mesh(
new THREE.RingGeometry(0.28, 0.305, 32),
new THREE.MeshBasicMaterial({
color: 0xffa45e,
transparent: true,
opacity: 0.65,
side: THREE.DoubleSide,
toneMapped: false
})
);
marker.rotation.x = -Math.PI / 2;
marker.scale.set(1, 0.74, 1);
marker.position.set(
samples.sag.cx + pocket.x,
pocket.y + 0.09,
pocket.z
);
scene.add(marker);
const leaderTarget = new THREE.Vector3(
samples.sag.cx + pocket.x,
pocket.y + 0.1,
pocket.z
);
const leaderEnd = new THREE.Vector3(
samples.sag.cx + 1.35,
pocket.y + 1.05,
pocket.z
);
const leader = new THREE.Line(
new THREE.BufferGeometry().setFromPoints([leaderTarget, leaderEnd]),
new THREE.LineDashedMaterial({
color: 0xffa45e,
dashSize: 0.09,
gapSize: 0.065,
transparent: true,
opacity: 0.8,
toneMapped: false
})
);
leader.computeLineDistances();
scene.add(leader);
const arrowGeometry = new THREE.ConeGeometry(0.075, 0.22, 5);
const arrowMaterial = new THREE.MeshBasicMaterial({
color: 0x83f7ff,
transparent: true,
opacity: 0.7,
depthWrite: false,
toneMapped: false
});
const flowArrows = [];
for (let i = 0; i < 5; i++) {
const arrow = new THREE.Mesh(arrowGeometry, arrowMaterial);
scene.add(arrow);
flowArrows.push({
mesh: arrow,
kind: "correct",
lane: -1.8 + i * 0.9,
offset: i * 0.17
});
}
for (let i = 0; i < 7; i++) {
const arrow = new THREE.Mesh(arrowGeometry, arrowMaterial);
scene.add(arrow);
flowArrows.push({
mesh: arrow,
kind: "sag",
angle: (i / 7) * Math.PI * 2,
offset: i * 0.13
});
}
const rainCount = 22;
const rainPositions = new Float32Array(rainCount * 6);
const rainGeometry = new THREE.BufferGeometry();
rainGeometry.setAttribute(
"position",
new THREE.BufferAttribute(rainPositions, 3).setUsage(THREE.DynamicDrawUsage)
);
const rainMaterial = new THREE.LineBasicMaterial({
color: 0x8bdfff,
transparent: true,
opacity: 0.47,
depthWrite: false,
toneMapped: false
});
const rain = new THREE.LineSegments(rainGeometry, rainMaterial);
rain.frustumCulled = false;
scene.add(rain);
const rainDrops = [];
function resetRainDrop(drop, initial = false) {
const useSag = Math.random() < 0.54;
const cx = useSag ? samples.sag.cx : samples.correct.cx;
drop.x = cx + (Math.random() - 0.5) * 5.45;
drop.z = back - 0.3 + Math.random() * (depth + 0.65);
drop.y = initial ? 4.5 + Math.random() * 3.5 : 6.0 + Math.random() * 2.2;
drop.speed = 4.2 + Math.random() * 2.25;
drop.length = 0.18 + Math.random() * 0.19;
}
for (let i = 0; i < rainCount; i++) {
const drop = {};
resetRainDrop(drop, true);
rainDrops.push(drop);
}
const surfaceDropGeometry = new THREE.IcosahedronGeometry(0.052, 0);
const surfaceDropMaterial = new THREE.MeshBasicMaterial({
color: 0x9cf9ff,
toneMapped: false
});
const surfaceDrops = [];
const settledDrops = [];
function acquireSurfaceDrop() {
let entry = surfaceDrops.find((d) => !d.active);
if (!entry) {
const mesh = new THREE.Mesh(surfaceDropGeometry, surfaceDropMaterial);
mesh.visible = false;
scene.add(mesh);
entry = { mesh, active: false };
surfaceDrops.push(entry);
}
return entry;
}
function spawnSurfaceDrop(kind, localX, localZ) {
const entry = acquireSurfaceDrop();
entry.active = true;
entry.kind = kind;
entry.x = localX;
entry.z = localZ;
entry.age = 0;
entry.settled = false;
entry.phase = Math.random() * Math.PI * 2;
entry.speed = kind === "correct"
? 1.32 + Math.random() * 0.48
: 0.56 + Math.random() * 0.18;
entry.mesh.visible = true;
entry.mesh.scale.set(1, 1.25, 1);
return entry;
}
const impactRingGeometry = new THREE.RingGeometry(0.075, 0.102, 16);
const impactRings = [];
function spawnImpactRing(x, y, z, color = 0x85f7ff) {
const ring = new THREE.Mesh(
impactRingGeometry,
new THREE.MeshBasicMaterial({
color,
transparent: true,
opacity: 0.7,
side: THREE.DoubleSide,
depthWrite: false,
toneMapped: false
})
);
ring.rotation.x = -Math.PI / 2;
ring.position.set(x, y + 0.055, z);
scene.add(ring);
impactRings.push({ mesh: ring, age: 0 });
}
function surfaceAt(x, z) {
if (
x >= samples.correct.cx - width / 2 &&
x <= samples.correct.cx + width / 2 &&
z >= back && z <= front
) {
return {
kind: "correct",
x: x - samples.correct.cx,
z,
y: correctHeight(x - samples.correct.cx, z)
};
}
if (
x >= samples.sag.cx - width / 2 &&
x <= samples.sag.cx + width / 2 &&
z >= back && z <= front
) {
return {
kind: "sag",
x: x - samples.sag.cx,
z,
y: sagHeight(x - samples.sag.cx, z)
};
}
return null;
}
const temp = new THREE.Vector3();
const up = new THREE.Vector3(0, 1, 0);
function gradientAt(x, z) {
const e = 0.055;
const gx =
(sagHeight(x + e, z) - sagHeight(x - e, z)) / (2 * e);
const gz =
(sagHeight(x, z + e) - sagHeight(x, z - e)) / (2 * e);
return { gx, gz, magnitude: Math.hypot(gx, gz) };
}
function updateSurfaceDrops(dt, time) {
for (const drop of surfaceDrops) {
if (!drop.active) continue;
drop.age += dt;
if (drop.kind === "correct") {
drop.z += drop.speed * dt;
drop.x += Math.sin(drop.age * 4 + drop.phase) * 0.035 * dt;
const y = correctHeight(drop.x, drop.z);
drop.mesh.position.set(
samples.correct.cx + drop.x,
y + 0.055,
drop.z
);
drop.mesh.scale.set(0.9, 1.15, 1.15);
if (drop.z >= front - 0.025) {
drop.active = false;
drop.mesh.visible = false;
spawnImpactRing(
samples.correct.cx + drop.x,
frontHeight - 0.12,
front + 0.11,
0x9cf9ff
);
}
continue;
}
const gradient = gradientAt(drop.x, drop.z);
const nearPocket =
Math.abs(drop.x - pocket.x) < 0.2 &&
Math.abs(drop.z - pocket.z) < 0.2;
if (!drop.settled) {
if (nearPocket || (gradient.magnitude < 0.025 && drop.age > 0.25)) {
drop.settled = true;
settledDrops.push(drop);
if (settledDrops.length > 24) {
const oldest = settledDrops.shift();
oldest.active = false;
oldest.mesh.visible = false;
}
} else {
const inv = 1 / Math.max(gradient.magnitude, 0.0001);
const dirX = -gradient.gx * inv;
const dirZ = -gradient.gz * inv;
drop.x += dirX * drop.speed * dt;
drop.z += dirZ * drop.speed * dt;
}
} else {
drop.x += (pocket.x - drop.x) * Math.min(1, dt * 0.7);
drop.z += (pocket.z - drop.z) * Math.min(1, dt * 0.7);
}
if (
drop.x < -width / 2 + 0.03 ||
drop.x > width / 2 - 0.03 ||
drop.z < back + 0.03 ||
drop.z > front - 0.025
) {
drop.active = false;
drop.mesh.visible = false;
continue;
}
const y = sagHeight(drop.x, drop.z);
drop.mesh.position.set(
samples.sag.cx + drop.x,
y + 0.055,
drop.z
);
const pulse = drop.settled
? 0.95 + Math.sin(time * 3 + drop.phase) * 0.12
: 1;
drop.mesh.scale.set(pulse, pulse * 1.1, pulse);
}
}
function updateRain(dt) {
for (let i = 0; i < rainDrops.length; i++) {
const drop = rainDrops[i];
drop.y -= drop.speed * dt;
const surface = surfaceAt(drop.x, drop.z);
if (surface && drop.y <= surface.y + 0.02) {
spawnSurfaceDrop(surface.kind, surface.x, surface.z);
spawnImpactRing(drop.x, surface.y, drop.z);
resetRainDrop(drop);
} else if (drop.y < 0.02) {
resetRainDrop(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.018;
rainPositions[offset + 4] = drop.y + drop.length;
rainPositions[offset + 5] = drop.z;
}
rainGeometry.attributes.position.needsUpdate = true;
}
function updateFlowArrows(time) {
for (const arrow of flowArrows) {
if (arrow.kind === "correct") {
const t = (time * 0.18 + arrow.offset) % 1;
const z = back + 0.25 + t * (depth - 0.5);
const x = arrow.lane;
const y = correctHeight(x, z);
const slope = (correctHeight(x, z + 0.05) -
correctHeight(x, z - 0.05)) / 0.1;
arrow.mesh.position.set(
samples.correct.cx + x,
y + 0.105,
z
);
temp.set(0, slope, 1).normalize();
arrow.mesh.quaternion.setFromUnitVectors(up, temp);
} else {
const t = (time * 0.16 + arrow.offset) % 1;
const radius = 1.22 * (1 - t) + 0.09;
const x = pocket.x + Math.cos(arrow.angle) * radius;
const z = pocket.z + Math.sin(arrow.angle) * radius * 0.78;
const y = sagHeight(x, z);
const gradient = gradientAt(x, z);
arrow.mesh.position.set(
samples.sag.cx + x,
y + 0.105,
z
);
if (gradient.magnitude > 0.008) {
const inv = 1 / gradient.magnitude;
temp.set(
-gradient.gx * inv,
-gradient.magnitude,
-gradient.gz * inv
).normalize();
} else {
temp.set(pocket.x - x, 0, pocket.z - z).normalize();
}
arrow.mesh.quaternion.setFromUnitVectors(up, temp);
}
}
}
function updateImpactRings(dt) {
for (let i = impactRings.length - 1; i >= 0; i--) {
const ring = impactRings[i];
ring.age += dt;
const progress = ring.age / 0.48;
if (progress >= 1) {
scene.remove(ring.mesh);
ring.mesh.material.dispose();
impactRings.splice(i, 1);
} else {
ring.mesh.scale.setScalar(1 + progress * 5.8);
ring.mesh.material.opacity = (1 - progress) * 0.68;
}
}
}
const labelA = document.getElementById("labelA");
const labelB = document.getElementById("labelB");
const gutterNote = document.getElementById("gutterNote");
const pocketNote = document.getElementById("pocketNote");
const dropCount = document.getElementById("dropCount");
function placeLabel(element, worldPosition) {
const projected = worldPosition.clone().project(camera);
let x = (projected.x * 0.5 + 0.5) * window.innerWidth;
let y = (-projected.y * 0.5 + 0.5) * window.innerHeight;
x = THREE.MathUtils.clamp(x, 100, window.innerWidth - 100);
y = THREE.MathUtils.clamp(y, 92, window.innerHeight - 85);
element.style.left = `${x}px`;
element.style.top = `${y}px`;
}
function updateLabels() {
placeLabel(
labelA,
new THREE.Vector3(samples.correct.cx, 3.15, -0.72)
);
placeLabel(
labelB,
new THREE.Vector3(samples.sag.cx, 3.15, -0.72)
);
placeLabel(
gutterNote,
new THREE.Vector3(samples.correct.cx + 1.6, 2.0, front + 0.22)
);
placeLabel(
pocketNote,
leaderEnd.clone().add(new THREE.Vector3(0.16, 0.04, 0))
);
}
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.classList.add("dragging");
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!dragging) return;
const dx = event.clientX - lastPointerX;
const dy = event.clientY - lastPointerY;
lastPointerX = event.clientX;
lastPointerY = event.clientY;
orbit.theta -= dx * 0.0055;
orbit.phi = THREE.MathUtils.clamp(orbit.phi - dy * 0.0045, 0.58, 1.43);
updateCamera();
});
function endDrag(event) {
dragging = false;
renderer.domElement.classList.remove("dragging");
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();
orbit.radius = THREE.MathUtils.clamp(
orbit.radius * Math.exp(event.deltaY * 0.001),
10,
24
);
updateCamera();
},
{ passive: false }
);
renderer.domElement.addEventListener("dblclick", () => {
orbit.radius = 17;
orbit.theta = 0.12;
orbit.phi = 1.12;
updateCamera();
});
function resize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
}
window.addEventListener("resize", resize);
const clock = new THREE.Clock();
let hudCounter = 0;
function animate() {
requestAnimationFrame(animate);
const dt = Math.min(clock.getDelta(), 0.035);
const time = clock.elapsedTime;
updateRain(dt);
updateSurfaceDrops(dt, time);
updateFlowArrows(time);
updateImpactRings(dt);
gutterWater.material.opacity = 0.29 + Math.sin(time * 2.1) * 0.075;
gutterWater.scale.y = 1 + Math.sin(time * 2.1) * 0.12;
for (let i = 0; i < gutterBeads.length; i++) {
const travel = (time * 0.55 + i * (width / gutterBeads.length)) % width;
gutterBeads[i].position.set(
samples.correct.cx - width / 2 + travel,
frontHeight - 0.145,
front + 0.11
);
const pulse = 0.8 + Math.sin(time * 4 + i) * 0.18;
gutterBeads[i].scale.setScalar(pulse);
}
puddleMaterial.opacity = 0.19 + Math.sin(time * 1.8) * 0.045;
const puddlePulse = 1 + Math.sin(time * 1.8) * 0.035;
puddle.scale.set(1.18 * puddlePulse, 0.075, 0.8 * puddlePulse);
for (let i = 0; i < pocketRings.length; i++) {
const phase = (time * 0.48 + i / pocketRings.length) % 1;
const ring = pocketRings[i];
ring.scale.set(1 + phase * 3.4, 0.72 + phase * 2.5, 1);
ring.material.opacity = (1 - phase) * 0.38;
}
marker.material.opacity = 0.48 + Math.sin(time * 2.6) * 0.2;
hudCounter++;
if (hudCounter % 8 === 0) {
dropCount.textContent = String(
surfaceDrops.reduce((count, drop) => count + (drop.active ? 1 : 0), 0)
);
}
updateLabels();
renderer.render(scene, camera);
}
animate();
</script>
</body>
</html>
```