Prompt specification
Build or customize the Three.js 3D scene "Rain beads on two roof slopes" 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 beads on two roof slopes
Category: Architecture & Cities · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/rain-beads-on-two-roof-slopes-x2218
Scene Description Prompt:
Compare low-poly steep and shallow roofs draining illustrative rain into matching gutters; overflow follows stated capacity. Three.js from cdnjs, one HTML file.
Working Three.js Starter (rain-beads-on-two-roof-slopes-x2218):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Roof Pitch vs. Gutter Capacity</title>
<style>
:root {
color-scheme: dark;
--bg: #071018;
--panel: #0d1924;
--panel-2: #111f2c;
--line: #243747;
--text: #eef7fb;
--muted: #8fa8b8;
--steep: #59d9e7;
--shallow: #b69cff;
--capacity: #ffb35c;
--overflow: #ff745d;
}
* {
box-sizing: border-box;
}
body {
margin: 0;
min-width: 320px;
background:
radial-gradient(circle at 15% 0%, rgba(57, 142, 170, 0.14), transparent 35%),
radial-gradient(circle at 90% 10%, rgba(137, 99, 204, 0.12), transparent 30%),
var(--bg);
color: var(--text);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
button,
input {
font: inherit;
}
button {
color: inherit;
}
.app {
width: min(1580px, 100%);
margin: 0 auto;
padding: 26px;
}
.masthead {
display: flex;
align-items: flex-end;
justify-content: space-between;
gap: 24px;
margin-bottom: 20px;
}
.eyebrow {
display: flex;
align-items: center;
gap: 9px;
margin-bottom: 9px;
color: var(--steep);
font-size: 11px;
font-weight: 800;
letter-spacing: 0.16em;
text-transform: uppercase;
}
.eyebrow::before {
width: 8px;
height: 8px;
border-radius: 50%;
background: var(--steep);
box-shadow: 0 0 16px var(--steep);
content: "";
}
h1 {
max-width: 850px;
margin: 0;
font-size: clamp(30px, 4.3vw, 58px);
font-weight: 720;
letter-spacing: -0.045em;
line-height: 0.98;
}
.masthead p {
max-width: 450px;
margin: 0 0 3px;
color: var(--muted);
font-size: 14px;
line-height: 1.65;
}
.layout {
display: grid;
grid-template-columns: minmax(0, 1fr) 340px;
gap: 16px;
align-items: start;
}
.left-column {
display: grid;
gap: 16px;
min-width: 0;
}
.scene-card,
.control-panel,
.model-card,
.chart-card {
border: 1px solid var(--line);
border-radius: 18px;
background: linear-gradient(145deg, rgba(17, 31, 44, 0.98), rgba(10, 21, 31, 0.98));
box-shadow: 0 20px 55px rgba(0, 0, 0, 0.18);
}
.scene-card {
position: relative;
height: clamp(520px, 64vh, 700px);
min-height: 520px;
overflow: hidden;
isolation: isolate;
}
.scene-card::after {
position: absolute;
inset: 0;
z-index: 1;
pointer-events: none;
background:
linear-gradient(180deg, rgba(7, 16, 24, 0.3), transparent 24%),
linear-gradient(0deg, rgba(7, 16, 24, 0.45), transparent 28%);
content: "";
}
#scene {
position: absolute;
inset: 0;
}
#scene canvas {
display: block;
width: 100%;
height: 100%;
cursor: grab;
touch-action: none;
}
#scene canvas:active {
cursor: grabbing;
}
.scene-topbar {
position: absolute;
top: 16px;
left: 16px;
z-index: 3;
display: flex;
align-items: center;
gap: 10px;
padding: 9px 12px;
border: 1px solid rgba(143, 168, 184, 0.18);
border-radius: 999px;
background: rgba(7, 16, 24, 0.68);
backdrop-filter: blur(12px);
color: #c7d8e3;
font-size: 11px;
letter-spacing: 0.04em;
pointer-events: none;
}
.live-dot {
width: 7px;
height: 7px;
border-radius: 50%;
background: var(--steep);
box-shadow: 0 0 10px var(--steep);
}
.camera-hint {
position: absolute;
right: 16px;
bottom: 15px;
z-index: 3;
padding: 8px 11px;
border: 1px solid rgba(143, 168, 184, 0.16);
border-radius: 10px;
background: rgba(7, 16, 24, 0.6);
color: var(--muted);
font-size: 11px;
pointer-events: none;
}
.model-label {
position: absolute;
z-index: 2;
min-width: 150px;
padding: 9px 11px;
border: 1px solid rgba(143, 168, 184, 0.2);
border-radius: 12px;
background: rgba(7, 16, 24, 0.77);
box-shadow: 0 10px 30px rgba(0, 0, 0, 0.24);
backdrop-filter: blur(10px);
pointer-events: none;
transform: translate(-50%, -100%);
transition: border-color 180ms ease;
}
.model-label[data-model="steep"] {
border-top: 2px solid var(--steep);
}
.model-label[data-model="shallow"] {
border-top: 2px solid var(--shallow);
}
.model-label.is-overflowing {
border-color: rgba(255, 116, 93, 0.8);
}
.label-title {
display: flex;
justify-content: space-between;
gap: 14px;
font-size: 11px;
font-weight: 800;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.label-angle {
color: var(--muted);
}
.label-flow {
display: block;
margin-top: 4px;
font-size: 16px;
font-weight: 750;
}
.label-state {
color: var(--muted);
font-size: 10px;
}
.control-panel {
padding: 20px;
}
.panel-kicker {
color: var(--muted);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.15em;
text-transform: uppercase;
}
.control-panel h2 {
margin: 6px 0 8px;
font-size: 24px;
letter-spacing: -0.025em;
}
.panel-copy {
margin: 0 0 22px;
color: var(--muted);
font-size: 13px;
line-height: 1.55;
}
.control-block {
padding: 17px 0;
border-top: 1px solid var(--line);
}
.control-block:first-of-type {
border-top: 0;
padding-top: 0;
}
.control-label {
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: 11px;
font-size: 12px;
font-weight: 700;
}
.control-label output {
color: var(--steep);
font-size: 16px;
font-variant-numeric: tabular-nums;
}
input[type="range"] {
width: 100%;
height: 5px;
margin: 4px 0;
appearance: none;
border-radius: 999px;
outline: none;
background: linear-gradient(
90deg,
var(--steep) 0%,
var(--steep) var(--range-progress, 46.4%),
#243747 var(--range-progress, 46.4%),
#243747 100%
);
}
input[type="range"]::-webkit-slider-thumb {
width: 18px;
height: 18px;
appearance: none;
border: 3px solid #0c1822;
border-radius: 50%;
background: var(--steep);
box-shadow: 0 0 0 1px var(--steep);
cursor: pointer;
}
input[type="range"]::-moz-range-thumb {
width: 14px;
height: 14px;
border: 3px solid #0c1822;
border-radius: 50%;
background: var(--steep);
box-shadow: 0 0 0 1px var(--steep);
cursor: pointer;
}
.range-scale {
display: flex;
justify-content: space-between;
margin-top: 8px;
color: #698394;
font-size: 10px;
}
.scenario-grid {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 7px;
}
.scenario-button,
.action-button {
min-height: 38px;
border: 1px solid var(--line);
border-radius: 10px;
background: #0a1620;
color: #b9cbd6;
font-size: 11px;
font-weight: 700;
cursor: pointer;
transition: 150ms ease;
}
.scenario-button:hover,
.action-button:hover {
border-color: #456173;
color: white;
transform: translateY(-1px);
}
.scenario-button.active {
border-color: rgba(89, 217, 231, 0.65);
background: rgba(89, 217, 231, 0.1);
color: #dffbff;
}
.action-grid {
display: grid;
grid-template-columns: 1.35fr 1fr;
gap: 8px;
}
.action-button.primary {
border-color: rgba(89, 217, 231, 0.52);
background: var(--steep);
color: #06141a;
}
.action-button.primary:hover {
color: #06141a;
background: #83e6ef;
}
.capacity-card {
padding: 14px;
border: 1px solid rgba(255, 179, 92, 0.28);
border-radius: 13px;
background: rgba(255, 179, 92, 0.065);
}
.capacity-card .big {
display: block;
margin: 4px 0 2px;
color: var(--capacity);
font-size: 30px;
font-weight: 780;
letter-spacing: -0.04em;
}
.capacity-card p {
margin: 0;
color: #c8b697;
font-size: 11px;
line-height: 1.5;
}
.equation {
margin-top: 10px;
padding: 10px 11px;
border-radius: 10px;
background: #08131c;
color: #b9cbd6;
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
font-size: 10px;
line-height: 1.6;
}
.equation strong {
color: var(--overflow);
}
.compare-grid {
display: grid;
grid-template-columns: repeat(2, minmax(0, 1fr));
gap: 16px;
}
.model-card {
position: relative;
overflow: hidden;
padding: 18px;
}
.model-card::before {
position: absolute;
inset: 0 auto 0 0;
width: 3px;
background: var(--steep);
content: "";
}
.model-card.shallow::before {
background: var(--shallow);
}
.model-head {
display: flex;
align-items: flex-start;
justify-content: space-between;
gap: 12px;
}
.model-name {
display: flex;
align-items: center;
gap: 8px;
margin: 0;
font-size: 14px;
}
.model-name::before {
width: 8px;
height: 8px;
border-radius: 50%;
background: var(--steep);
content: "";
}
.shallow .model-name::before {
background: var(--shallow);
}
.angle-pill {
padding: 5px 8px;
border-radius: 999px;
background: #172a38;
color: #a9bfcd;
font-size: 10px;
font-weight: 800;
}
.flow-value {
display: flex;
align-items: baseline;
gap: 7px;
margin: 20px 0 12px;
}
.flow-value strong {
font-size: clamp(26px, 4vw, 39px);
font-weight: 760;
letter-spacing: -0.045em;
font-variant-numeric: tabular-nums;
}
.flow-value span {
color: var(--muted);
font-size: 11px;
}
.status {
display: inline-flex;
align-items: center;
gap: 6px;
margin-bottom: 12px;
padding: 5px 8px;
border-radius: 999px;
background: rgba(89, 217, 231, 0.1);
color: var(--steep);
font-size: 9px;
font-weight: 850;
letter-spacing: 0.09em;
text-transform: uppercase;
}
.status::before {
width: 5px;
height: 5px;
border-radius: 50%;
background: currentColor;
content: "";
}
.status.warning {
background: rgba(255, 116, 93, 0.12);
color: var(--overflow);
}
.status.idle {
background: #172532;
color: var(--muted);
}
.meter {
position: relative;
height: 9px;
border-radius: 999px;
background: #1a2b38;
overflow: visible;
}
.meter-fill {
width: 0;
height: 100%;
border-radius: inherit;
background: linear-gradient(90deg, #35bfd0, var(--steep));
transition: width 100ms linear;
}
.shallow .meter-fill {
background: linear-gradient(90deg, #8b73d5, var(--shallow));
}
.capacity-marker {
position: absolute;
top: -5px;
bottom: -5px;
left: 46.67%;
width: 2px;
border-radius: 2px;
background: var(--capacity);
box-shadow: 0 0 8px rgba(255, 179, 92, 0.4);
}
.meter-caption {
display: flex;
justify-content: space-between;
margin-top: 9px;
color: #728b9b;
font-size: 9px;
}
.meter-caption .limit {
color: var(--capacity);
}
.model-stats {
display: grid;
grid-template-columns: repeat(2, 1fr);
gap: 10px;
margin-top: 18px;
padding-top: 15px;
border-top: 1px solid var(--line);
}
.stat span {
display: block;
margin-bottom: 4px;
color: var(--muted);
font-size: 9px;
font-weight: 700;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.stat strong {
font-size: 16px;
font-variant-numeric: tabular-nums;
}
.stat strong.danger {
color: var(--overflow);
}
.chart-card {
padding: 18px 18px 13px;
}
.chart-header {
display: flex;
align-items: flex-start;
justify-content: space-between;
gap: 15px;
margin-bottom: 13px;
}
.chart-header h3 {
margin: 0 0 5px;
font-size: 15px;
}
.chart-header p {
margin: 0;
color: var(--muted);
font-size: 11px;
}
.chart-legend {
display: flex;
flex-wrap: wrap;
justify-content: flex-end;
gap: 12px;
color: #a9bfcd;
font-size: 10px;
}
.legend-item {
display: flex;
align-items: center;
gap: 6px;
}
.legend-line {
width: 16px;
height: 3px;
border-radius: 2px;
background: var(--steep);
}
.legend-line.shallow {
background: var(--shallow);
}
.legend-line.capacity {
height: 0;
border-top: 2px dashed var(--capacity);
background: transparent;
}
#flow-chart {
display: block;
width: 100%;
height: 180px;
}
.insight {
margin: 12px 0 0;
padding: 10px 12px;
border-left: 2px solid var(--capacity);
border-radius: 0 9px 9px 0;
background: rgba(255, 179, 92, 0.06);
color: #c7b99f;
font-size: 11px;
line-height: 1.55;
}
.footnote {
margin: 16px 3px 0;
color: #607987;
font-size: 10px;
line-height: 1.55;
}
@media (max-width: 1050px) {
.layout {
grid-template-columns: 1fr;
}
.control-panel {
display: grid;
grid-template-columns: repeat(2, minmax(0, 1fr));
gap: 0 24px;
}
.control-intro {
grid-column: 1 / -1;
}
.control-block {
padding-top: 17px;
}
}
@media (max-width: 680px) {
.app {
padding: 14px;
}
.masthead {
display: block;
}
.masthead p {
margin-top: 14px;
}
.scene-card {
height: 520px;
}
.control-panel {
display: block;
}
.compare-grid {
grid-template-columns: 1fr;
}
.chart-header {
display: block;
}
.chart-legend {
justify-content: flex-start;
margin-top: 10px;
}
.camera-hint {
display: none;
}
}
</style>
</head>
<body>
<div class="app">
<header class="masthead">
<div>
<div class="eyebrow">Illustrative drainage lab</div>
<h1>When the roof outruns the gutter</h1>
</div>
<p>
Two low-poly roofs. Two identical gutters. One storm.
Compare how roof pitch changes the peak flow reaching a fixed capacity.
</p>
</header>
<main class="layout">
<div class="left-column">
<section class="scene-card" aria-label="Interactive 3D roof drainage comparison">
<div id="scene"></div>
<div class="scene-topbar">
<span class="live-dot"></span>
<span>72-second repeating storm · time compressed</span>
</div>
<div class="model-label" id="label-steep" data-model="steep">
<div class="label-title">
<span>Steep roof</span>
<span class="label-angle">40°</span>
</div>
<strong class="label-flow">0 L/min</strong>
<span class="label-state">Waiting for rain</span>
</div>
<div class="model-label" id="label-shallow" data-model="shallow">
<div class="label-title">
<span>Shallow roof</span>
<span class="label-angle">20°</span>
</div>
<strong class="label-flow">0 L/min</strong>
<span class="label-state">Waiting for rain</span>
</div>
<div class="camera-hint">Drag to orbit · scroll to zoom · double-click to reset view</div>
</section>
<section class="compare-grid">
<article class="model-card steep">
<div class="model-head">
<h3 class="model-name">Steep roof</h3>
<span class="angle-pill">40° pitch</span>
</div>
<div class="flow-value">
<strong id="steep-flow">0</strong>
<span>L/min reaching gutter</span>
</div>
<div class="status" id="steep-status">Waiting</div>
<div class="meter" aria-label="Steep roof flow relative to gutter capacity">
<div class="meter-fill" id="steep-fill"></div>
<div class="capacity-marker" title="140 L/min capacity"></div>
</div>
<div class="meter-caption">
<span>0</span>
<span class="limit">140 L/min capacity</span>
<span>300</span>
</div>
<div class="model-stats">
<div class="stat">
<span>Peak outflow</span>
<strong id="steep-peak">0 L/min</strong>
</div>
<div class="stat">
<span>Total overflow</span>
<strong id="steep-overflow">0.0 L</strong>
</div>
</div>
</article>
<article class="model-card shallow">
<div class="model-head">
<h3 class="model-name">Shallow roof</h3>
<span class="angle-pill">20° pitch</span>
</div>
<div class="flow-value">
<strong id="shallow-flow">0</strong>
<span>L/min reaching gutter</span>
</div>
<div class="status" id="shallow-status">Waiting</div>
<div class="meter" aria-label="Shallow roof flow relative to gutter capacity">
<div class="meter-fill" id="shallow-fill"></div>
<div class="capacity-marker" title="140 L/min capacity"></div>
</div>
<div class="meter-caption">
<span>0</span>
<span class="limit">140 L/min capacity</span>
<span>300</span>
</div>
<div class="model-stats">
<div class="stat">
<span>Peak outflow</span>
<strong id="shallow-peak">0 L/min</strong>
</div>
<div class="stat">
<span>Total overflow</span>
<strong id="shallow-overflow">0.0 L</strong>
</div>
</div>
</article>
</section>
<section class="chart-card">
<div class="chart-header">
<div>
<h3>Roof outflow hydrograph</h3>
<p>The steep roof concentrates runoff into a sharper, earlier peak.</p>
</div>
<div class="chart-legend">
<span class="legend-item"><i class="legend-line"></i>Steep</span>
<span class="legend-item"><i class="legend-line shallow"></i>Shallow</span>
<span class="legend-item"><i class="legend-line capacity"></i>Capacity</span>
</div>
</div>
<canvas id="flow-chart" aria-label="Roof outflow compared with gutter capacity"></canvas>
<p class="insight" id="insight">
Run the storm to compare the two roof-response curves.
</p>
</section>
</div>
<aside class="control-panel">
<div class="control-intro">
<div class="panel-kicker">Storm controls</div>
<h2>Change the rainfall</h2>
<p class="panel-copy">
Peak intensity controls the runoff delivered to each 80 m² roof.
Pitch changes the timing and concentration of that runoff.
</p>
</div>
<div class="control-block">
<label class="control-label" for="intensity">
Peak rain intensity
<output id="intensity-output" for="intensity">150 mm/h</output>
</label>
<input id="intensity" type="range" min="40" max="240" step="5" value="150" />
<div class="range-scale">
<span>40 mm/h</span>
<span>240 mm/h</span>
</div>
</div>
<div class="control-block">
<div class="control-label">Quick scenarios</div>
<div class="scenario-grid">
<button class="scenario-button" data-level="90">Steady</button>
<button class="scenario-button active" data-level="150">Design</button>
<button class="scenario-button" data-level="220">Cloudburst</button>
</div>
</div>
<div class="control-block">
<div class="control-label">Simulation</div>
<div class="action-grid">
<button class="action-button primary" id="play-button">Pause storm</button>
<button class="action-button" id="restart-button">Restart</button>
</div>
</div>
<div class="control-block">
<div class="capacity-card">
<div class="panel-kicker">Matching gutter capacity</div>
<strong class="big">140 L/min</strong>
<p>
Both gutters have the same discharge limit. Overflow begins only
when roof outflow exceeds 140 L/min.
</p>
<div class="equation">
Rain input = I × 80 m² × 0.90 ÷ 60<br />
Overflow = <strong>max(0, roof outflow − 140)</strong>
</div>
</div>
</div>
<div class="control-block">
<div class="panel-kicker">Model notes</div>
<p class="panel-copy">
Both roofs use the same area and runoff coefficient. A simplified
roof reservoir creates the characteristic lag difference; all figures
are illustrative rather than engineering design values.
</p>
</div>
</aside>
</main>
<p class="footnote">
Illustrative model only. Actual gutter performance depends on geometry,
debris, entrance conditions, rainfall duration, and downstream drainage.
</p>
</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 DOM
// ------------------------------------------------------------
const GUTTER_CAPACITY = 140; // L/min
const ROOF_AREA = 80; // m²
const RUNOFF_COEFFICIENT = 0.9;
const STORM_DURATION = 30; // seconds of visible storm
const CYCLE_DURATION = 72; // storm + dry recovery
const CHART_MAX = 300; // L/min
const $ = (selector) => document.querySelector(selector);
const sceneHost = $("#scene");
const intensityInput = $("#intensity");
const intensityOutput = $("#intensity-output");
const playButton = $("#play-button");
const restartButton = $("#restart-button");
const chartCanvas = $("#flow-chart");
let rainIntensity = Number(intensityInput.value);
let running = true;
let cycleTime = 0;
let sampleAccumulator = 0;
let uiAccumulator = 0;
let history = [];
// ------------------------------------------------------------
// Three.js scene
// ------------------------------------------------------------
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x0a151f);
scene.fog = new THREE.Fog(0x0a151f, 24, 52);
const camera = new THREE.PerspectiveCamera(
42,
1,
0.1,
100
);
const renderer = new THREE.WebGLRenderer({
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.12;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.domElement.setAttribute("aria-label", "3D roof and gutter simulation");
sceneHost.appendChild(renderer.domElement);
const hemiLight = new THREE.HemisphereLight(0xb9e6ff, 0x17201e, 2.0);
scene.add(hemiLight);
const sun = new THREE.DirectionalLight(0xffefd5, 3.1);
sun.position.set(-8, 15, 9);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -20;
sun.shadow.camera.right = 20;
sun.shadow.camera.top = 15;
sun.shadow.camera.bottom = -10;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 45;
sun.shadow.bias = -0.0004;
scene.add(sun);
const rimLight = new THREE.DirectionalLight(0x5eb9d7, 1.25);
rimLight.position.set(12, 7, -9);
scene.add(rimLight);
const groundMaterial = new THREE.MeshStandardMaterial({
color: 0x14252d,
roughness: 0.95,
metalness: 0.02
});
const ground = new THREE.Mesh(
new THREE.PlaneGeometry(60, 38),
groundMaterial
);
ground.rotation.x = -Math.PI / 2;
ground.position.y = -0.04;
ground.receiveShadow = true;
scene.add(ground);
const pathMaterial = new THREE.MeshStandardMaterial({
color: 0x26363b,
roughness: 1
});
const path = new THREE.Mesh(
new THREE.PlaneGeometry(28, 2.1),
pathMaterial
);
path.rotation.x = -Math.PI / 2;
path.position.set(0, 0.012, 4.35);
path.receiveShadow = true;
scene.add(path);
// ------------------------------------------------------------
// Materials and geometry helpers
// ------------------------------------------------------------
function standardMaterial(color, options = {}) {
return new THREE.MeshStandardMaterial({
color,
roughness: 0.8,
metalness: 0.03,
flatShading: true,
...options
});
}
const wallMaterial = standardMaterial(0xd9d1bd);
const roofMaterial = standardMaterial(0xce6549, {
roughness: 0.72,
side: THREE.DoubleSide
});
const gutterMaterial = standardMaterial(0x83949c, {
metalness: 0.72,
roughness: 0.38
});
const darkMaterial = standardMaterial(0x20333e);
const doorMaterial = standardMaterial(0x365361);
const windowMaterial = new THREE.MeshStandardMaterial({
color: 0x183542,
emissive: 0x0d2b38,
emissiveIntensity: 0.75,
roughness: 0.28,
metalness: 0.18
});
const trunkMaterial = standardMaterial(0x5d4634);
const foliageMaterial = standardMaterial(0x3c6d61);
function addBox(parent, width, height, depth, material, x, y, z) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(width, height, depth),
material
);
mesh.position.set(x, y, z);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function addCylinder(
parent,
radius,
length,
material,
x,
y,
z,
radialSegments = 8,
axis = "y"
) {
const mesh = new THREE.Mesh(
new THREE.CylinderGeometry(radius, radius, length, radialSegments),
material
);
mesh.position.set(x, y, z);
if (axis === "x") mesh.rotation.z = Math.PI / 2;
if (axis === "z") mesh.rotation.x = Math.PI / 2;
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function createGableRoofGeometry(halfWidth, halfDepth, eaveY, rise) {
const xl = -halfWidth;
const xr = halfWidth;
const zf = halfDepth;
const zb = -halfDepth;
const ridgeY = eaveY + rise;
const positions = [];
function triangle(a, b, c) {
positions.push(...a, ...b, ...c);
}
// Front slope
triangle([xl, ridgeY, 0], [xr, ridgeY, 0], [xr, eaveY, zf]);
triangle([xl, ridgeY, 0], [xr, eaveY, zf], [xl, eaveY, zf]);
// Back slope
triangle([xr, eaveY, zb], [xr, ridgeY, 0], [xl, ridgeY, 0]);
triangle([xr, eaveY, zb], [xl, ridgeY, 0], [xl, eaveY, zb]);
// Gable end faces
triangle([xl, eaveY, zb], [xl, ridgeY, 0], [xl, eaveY, zf]);
triangle([xr, eaveY, zb], [xr, ridgeY, 0], [xr, eaveY, zf]);
const geometry = new THREE.BufferGeometry();
geometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(positions, 3)
);
geometry.computeVertexNormals();
return geometry;
}
function createPointCloud(count, color, size, opacity = 1) {
const geometry = new THREE.BufferGeometry();
const positions = new Float32Array(count * 3);
geometry.setAttribute(
"position",
new THREE.BufferAttribute(positions, 3)
);
const material = new THREE.PointsMaterial({
color,
size,
transparent: opacity < 1,
opacity,
sizeAttenuation: true,
depthWrite: false
});
const points = new THREE.Points(geometry, material);
points.frustumCulled = false;
return { points, geometry, positions };
}
// ------------------------------------------------------------
// Low-poly scenery
// ------------------------------------------------------------
function createTree(x, z, scale = 1) {
const tree = new THREE.Group();
tree.position.set(x, 0, z);
tree.scale.setScalar(scale);
addCylinder(tree, 0.14, 1.4, trunkMaterial, 0, 0.7, 0, 6);
const lower = new THREE.Mesh(
new THREE.ConeGeometry(1.15, 2.25, 7),
foliageMaterial
);
lower.position.y = 2.05;
lower.castShadow = true;
tree.add(lower);
const upper = new THREE.Mesh(
new THREE.ConeGeometry(0.82, 1.8, 7),
foliageMaterial
);
upper.position.y = 3.15;
upper.castShadow = true;
tree.add(upper);
scene.add(tree);
}
createTree(-14.2, -3.2, 1.15);
createTree(-12.3, -5.3, 0.8);
createTree(13.7, -3.8, 1.05);
createTree(15.1, -1.7, 0.72);
// ------------------------------------------------------------
// Model definitions
// ------------------------------------------------------------
const models = [
{
id: "steep",
name: "Steep roof",
angle: 40,
x: -6.35,
rise: 2.25,
halfDepth: 2.75,
roofTop: 5.3,
tau: 0.06, // minutes
runoffSpeed: 0.58,
lineColor: "#59d9e7",
state: {
storage: 0,
outflow: 0,
overflowRate: 0,
totalOverflow: 0,
peak: 0
}
},
{
id: "shallow",
name: "Shallow roof",
angle: 20,
x: 6.35,
rise: 0.91,
halfDepth: 2.75,
roofTop: 3.96,
tau: 0.38, // minutes
runoffSpeed: 0.19,
lineColor: "#b69cff",
state: {
storage: 0,
outflow: 0,
overflowRate: 0,
totalOverflow: 0,
peak: 0
}
}
];
function createHouse(model) {
const group = new THREE.Group();
group.position.x = model.x;
scene.add(group);
// Main wall and foundation
addBox(group, 6.0, 3.0, 4.4, wallMaterial, 0, 1.5, 0);
addBox(group, 6.15, 0.22, 4.55, darkMaterial, 0, 0.11, 0);
// Roof
const roofGeometry = createGableRoofGeometry(
3.3,
model.halfDepth,
3.05,
model.rise
);
const roof = new THREE.Mesh(roofGeometry, roofMaterial);
roof.castShadow = true;
roof.receiveShadow = true;
group.add(roof);
const roofEdges = new THREE.LineSegments(
new THREE.EdgesGeometry(roofGeometry, 18),
new THREE.LineBasicMaterial({
color: 0x7d3e32,
transparent: true,
opacity: 0.68
})
);
group.add(roofEdges);
addCylinder(
group,
0.075,
6.65,
standardMaterial(0xe27a5c),
0,
model.roofTop + 0.02,
0,
7,
"x"
);
// Front windows and door
addBox(group, 1.35, 1.2, 0.09, windowMaterial, -1.65, 1.65, 2.245);
addBox(group, 1.35, 1.2, 0.09, windowMaterial, 1.65, 1.65, 2.245);
addBox(group, 0.1, 1.23, 0.11, gutterMaterial, -1.65, 1.65, 2.3);
addBox(group, 0.1, 1.23, 0.11, gutterMaterial, 1.65, 1.65, 2.3);
addBox(group, 1.05, 2.05, 0.11, doorMaterial, 0, 1.08, 2.25);
addBox(group, 1.3, 0.12, 0.34, gutterMaterial, 0, 0.14, 2.39);
addBox(group, 0.08, 0.08, 0.08, gutterMaterial, 0.35, 1.03, 2.33);
// Identical gutter on each model
addCylinder(
group,
0.17,
6.65,
gutterMaterial,
0,
2.85,
2.84,
10,
"x"
);
// Flowing water visible along the front of the gutter
const gutterWaterMaterial = new THREE.MeshBasicMaterial({
color: model.id === "steep" ? 0x59d9e7 : 0xb69cff,
transparent: true,
opacity: 0.92,
depthWrite: false
});
const gutterWater = addBox(
group,
1,
0.11,
0.22,
gutterMaterial,
0,
2.81,
2.94
);
gutterWater.material = gutterWaterMaterial;
// A thin sheet appears over the lip only while the capacity is exceeded.
const spillMaterial = new THREE.MeshBasicMaterial({
color: 0xff8068,
transparent: true,
opacity: 0.5,
depthWrite: false
});
const spill = addBox(
group,
1,
0.07,
0.25,
spillMaterial,
0,
2.9,
2.98
);
spill.visible = false;
// Downspout and outlet
addCylinder(
group,
0.105,
2.55,
gutterMaterial,
2.86,
1.34,
2.86,
8
);
const elbow = new THREE.Mesh(
new THREE.SphereGeometry(0.15, 8, 6),
gutterMaterial
);
elbow.position.set(2.86, 2.61, 2.86);
elbow.castShadow = true;
group.add(elbow);
addCylinder(
group,
0.13,
0.55,
gutterMaterial,
2.86,
0.1,
3.12,
8,
"z"
);
// Status lamp
const statusMaterial = new THREE.MeshStandardMaterial({
color: 0x59d9e7,
emissive: 0x16383e,
emissiveIntensity: 1.2
});
const statusLight = new THREE.Mesh(
new THREE.SphereGeometry(0.1, 8, 6),
statusMaterial
);
statusLight.position.set(-2.7, 2.7, 2.38);
group.add(statusLight);
// Rain streaks
const rainCount = 190;
const rain = createPointCloud(0, 0x9bdcff, 0.018, 0.46);
const rainGeometry = new THREE.BufferGeometry();
const rainPositions = new Float32Array(rainCount * 6);
const rainY = new Float32Array(rainCount);
const rainX = new Float32Array(rainCount);
const rainZ = new Float32Array(rainCount);
const rainSpeed = new Float32Array(rainCount);
for (let i = 0; i < rainCount; i++) {
rainX[i] = (Math.random() - 0.5) * 8.0;
rainY[i] = 2.8 + Math.random() * 9.5;
rainZ[i] = (Math.random() - 0.5) * 7.2;
rainSpeed[i] = 8 + Math.random() * 5;
}
rainGeometry.setAttribute(
"position",
new THREE.BufferAttribute(rainPositions, 3)
);
const rainLines = new THREE.LineSegments(
rainGeometry,
new THREE.LineBasicMaterial({
color: 0x9bdcff,
transparent: true,
opacity: 0.48,
depthWrite: false
})
);
rainLines.frustumCulled = false;
group.add(rainLines);
// Roof runoff droplets
const runoffCount = 30;
const runoff = createPointCloud(
runoffCount,
model.id === "steep" ? 0x76e8f1 : 0xc8b5ff,
0.09,
0.96
);
group.add(runoff.points);
const runoffSeeds = Array.from(
{ length: runoffCount },
() => Math.random()
);
// Overflow droplets
const overflowCount = 75;
const overflow = createPointCloud(
overflowCount,
0xff8068,
0.095,
0.94
);
group.add(overflow.points);
const overflowY = new Float32Array(overflowCount);
const overflowX = new Float32Array(overflowCount);
const overflowZ = new Float32Array(overflowCount);
const overflowSpeed = new Float32Array(overflowCount);
for (let i = 0; i < overflowCount; i++) {
overflowX[i] = -3.0 + Math.random() * 6.0;
overflowY[i] = 2.83;
overflowZ[i] = 2.95 + Math.random() * 0.16;
overflowSpeed[i] = 3.2 + Math.random() * 2.8;
}
// Downspout flow droplets
const downspoutCount = 12;
const downspout = createPointCloud(
downspoutCount,
0x76e8f1,
0.075,
0.96
);
group.add(downspout.points);
// Ground puddle grows with cumulative overflow.
const puddleMaterial = new THREE.MeshBasicMaterial({
color: 0xff8068,
transparent: true,
opacity: 0.08,
depthWrite: false
});
const puddle = new THREE.Mesh(
new THREE.CircleGeometry(1.25, 14),
puddleMaterial
);
puddle.rotation.x = -Math.PI / 2;
puddle.position.set(-0.2, 0.025, 4.2);
puddle.scale.setScalar(0.001);
group.add(puddle);
Object.assign(model, {
group,
gutterWater,
spill,
statusMaterial,
rainGeometry,
rainPositions,
rainY,
rainX,
rainZ,
rainSpeed,
runoff,
runoffSeeds,
overflow,
overflowY,
overflowX,
overflowZ,
overflowSpeed,
downspout,
puddle
});
}
models.forEach(createHouse);
// ------------------------------------------------------------
// Direct camera controls — no add-ons
// ------------------------------------------------------------
const cameraTarget = new THREE.Vector3(0, 1.8, 0);
let cameraYaw = 0.12;
let cameraPitch = 0.43;
let cameraDistance = 25;
let pointerActive = false;
let pointerX = 0;
let pointerY = 0;
function updateCamera() {
const horizontal = Math.cos(cameraPitch) * cameraDistance;
camera.position.set(
cameraTarget.x + Math.sin(cameraYaw) * horizontal,
cameraTarget.y + Math.sin(cameraPitch) * cameraDistance,
cameraTarget.z + Math.cos(cameraYaw) * horizontal
);
camera.lookAt(cameraTarget);
}
function resetCamera() {
cameraYaw = 0.12;
cameraPitch = 0.43;
cameraDistance = window.innerWidth < 680 ? 29 : 25;
updateCamera();
}
renderer.domElement.addEventListener("pointerdown", (event) => {
pointerActive = true;
pointerX = event.clientX;
pointerY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!pointerActive) return;
const dx = event.clientX - pointerX;
const dy = event.clientY - pointerY;
pointerX = event.clientX;
pointerY = event.clientY;
cameraYaw -= dx * 0.005;
cameraPitch = THREE.MathUtils.clamp(
cameraPitch + dy * 0.004,
0.18,
1.02
);
updateCamera();
});
function stopPointer(event) {
pointerActive = false;
if (renderer.domElement.hasPointerCapture(event.pointerId)) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
}
renderer.domElement.addEventListener("pointerup", stopPointer);
renderer.domElement.addEventListener("pointercancel", stopPointer);
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraDistance = THREE.MathUtils.clamp(
cameraDistance * Math.exp(event.deltaY * 0.001),
15,
39
);
updateCamera();
},
{ passive: false }
);
renderer.domElement.addEventListener("dblclick", resetCamera);
// ------------------------------------------------------------
// Simulation
// ------------------------------------------------------------
function stormProfile(t) {
if (t < 6) return t / 6;
if (t < 18) return 1;
if (t < STORM_DURATION) {
return (STORM_DURATION - t) / 12;
}
return 0;
}
function resetModelStates() {
for (const model of models) {
model.state.storage = 0;
model.state.outflow = 0;
model.state.overflowRate = 0;
model.state.totalOverflow = 0;
model.state.peak = 0;
}
}
function resetSimulation() {
cycleTime = 0;
sampleAccumulator = 0;
history = [];
resetModelStates();
}
function updateSimulation(dt) {
cycleTime += dt;
if (cycleTime >= CYCLE_DURATION) {
cycleTime -= CYCLE_DURATION;
resetModelStates();
history = [];
sampleAccumulator = 0;
}
const profile = stormProfile(cycleTime);
// L/min:
// rainfall intensity mm/h × area m² × runoff coefficient ÷ 60
const rawInput =
rainIntensity * ROOF_AREA * RUNOFF_COEFFICIENT / 60 * profile;
for (const model of models) {
const state = model.state;
// First-order roof reservoir. The same input is routed through
// different time constants to represent pitch-dependent lag.
state.storage += (rawInput - state.outflow) * dt / 60;
state.outflow = state.storage / model.tau;
if (state.outflow < 0.001 && rawInput === 0) {
state.outflow = 0;
}
state.overflowRate = Math.max(
0,
state.outflow - GUTTER_CAPACITY
);
state.totalOverflow += state.overflowRate * dt / 60;
state.peak = Math.max(state.peak, state.outflow);
}
sampleAccumulator += dt;
if (sampleAccumulator >= 0.1) {
sampleAccumulator = 0;
if (cycleTime <= STORM_DURATION) {
history.push({
t: cycleTime,
steep: models[0].state.outflow,
shallow: models[1].state.outflow
});
}
}
}
// ------------------------------------------------------------
// Visual animation
// ------------------------------------------------------------
function updateHouseVisuals(model, dt, profile) {
const state = model.state;
const flowRatio = Math.min(state.outflow / GUTTER_CAPACITY, 1);
const visibleWidth = Math.max(0.001, flowRatio * 6.15);
model.gutterWater.scale.x = visibleWidth;
model.gutterWater.position.x = -3.075 + visibleWidth / 2;
const overflowAmount = state.overflowRate;
model.spill.visible = overflowAmount > 0.05;
if (model.spill.visible) {
const spillWidth = Math.min(overflowAmount / 50, 1) * 6.15;
model.spill.scale.x = Math.max(0.001, spillWidth);
model.spill.position.x = -3.075 + spillWidth / 2;
}
const overflowing = overflowAmount > 0.05;
model.statusMaterial.color.setHex(
overflowing ? 0xff745d : 0x59d9e7
);
model.statusMaterial.emissive.setHex(
overflowing ? 0x5b1c14 : 0x16383e
);
// Rain streak geometry
const rainVisibility = Math.floor(
190 * Math.min(1, rainIntensity / 180) * profile
);
model.rainGeometry.setDrawRange(0, rainVisibility * 2);
for (let i = 0; i < rainVisibility; i++) {
let y = model.rainY[i] - dt * model.rainSpeed[i];
if (y < 0.05) {
y = 9.0 + Math.random() * 4.0;
}
model.rainY[i] = y;
const x = model.rainX[i];
const z = model.rainZ[i];
const index = i * 6;
model.rainPositions[index] = x;
model.rainPositions[index + 1] = y;
model.rainPositions[index + 2] = z;
model.rainPositions[index + 3] = x + 0.035;
model.rainPositions[index + 4] = y - 0.52;
model.rainPositions[index + 5] = z;
}
model.rainGeometry.attributes.position.needsUpdate = true;
// Roof runoff points
const runoffCount = profile > 0.02 ? model.runoffSeeds.length : 0;
model.runoff.geometry.setDrawRange(0, runoffCount);
for (let i = 0; i < runoffCount; i++) {
const phase = (
model.runoffSeeds[i] +
cycleTime * model.runoffSpeed
) % 1;
const x = -3.05 + model.runoffSeeds[(i + 3) % model.runoffSeeds.length] * 6.1;
const y =
model.roofTop -
(model.roofTop - 3.08) * phase +
0.11;
const z = phase * model.halfDepth + 0.04;
model.runoff.positions[i * 3] = x;
model.runoff.positions[i * 3 + 1] = y;
model.runoff.positions[i * 3 + 2] = z;
}
model.runoff.geometry.attributes.position.needsUpdate = true;
// Overflow droplets
const overflowCount = Math.floor(
THREE.MathUtils.clamp(overflowAmount / 5, 0, 1) *
model.overflow.positions.length / 3
);
model.overflow.geometry.setDrawRange(0, overflowCount);
for (let i = 0; i < overflowCount; i++) {
let y = model.overflowY[i] - dt * model.overflowSpeed[i];
if (y < 0.05) {
y = 2.85;
model.overflowX[i] = -3.0 + Math.random() * 6.0;
model.overflowZ[i] = 2.95 + Math.random() * 0.16;
}
model.overflowY[i] = y;
model.overflow.positions[i * 3] = model.overflowX[i];
model.overflow.positions[i * 3 + 1] = y;
model.overflow.positions[i * 3 + 2] = model.overflowZ[i];
}
model.overflow.geometry.attributes.position.needsUpdate = true;
// Downspout flow: at most the stated gutter capacity exits.
const discharged = Math.min(state.outflow, GUTTER_CAPACITY);
const downspoutCount = discharged > 0.1 ? 12 : 0;
model.downspout.geometry.setDrawRange(0, downspoutCount);
for (let i = 0; i < downspoutCount; i++) {
const phase = (cycleTime * 0.85 + i / downspoutCount) % 1;
model.downspout.positions[i * 3] = 2.86;
model.downspout.positions[i * 3 + 1] = 0.15 + phase * 2.35;
model.downspout.positions[i * 3 + 2] = 2.86;
}
model.downspout.geometry.attributes.position.needsUpdate = true;
// Illustrative puddle size.
const puddleScale = THREE.MathUtils.clamp(
state.totalOverflow / 90,
0.001,
1.2
);
model.puddle.scale.setScalar(puddleScale);
model.puddle.material.opacity =
0.08 + Math.min(state.totalOverflow / 80, 0.23);
}
const projected = new THREE.Vector3();
function positionModelLabels() {
for (const model of models) {
projected.set(
model.x,
model.roofTop + 1.3,
0
).project(camera);
const x = (projected.x * 0.5 + 0.5) * sceneHost.clientWidth;
const y = (-projected.y * 0.5 + 0.5) * sceneHost.clientHeight;
const label = $(`#label-${model.id}`);
label.style.left = `${x}px`;
label.style.top = `${y}px`;
}
}
// ------------------------------------------------------------
// UI updates
// ------------------------------------------------------------
function modelStatus(model) {
if (model.state.outflow > GUTTER_CAPACITY + 0.05) {
return {
text: "Overflowing",
className: "warning"
};
}
if (model.state.outflow > 0.1) {
return {
text: "Within capacity",
className: ""
};
}
return {
text: "Waiting",
className: "idle"
};
}
function updateModelUI(model) {
const status = modelStatus(model);
const percent = THREE.MathUtils.clamp(
model.state.outflow / CHART_MAX * 100,
0,
100
);
$(`#${model.id}-flow`).textContent =
Math.round(model.state.outflow);
$(`#${model.id}-fill`).style.width = `${percent}%`;
const statusElement = $(`#${model.id}-status`);
statusElement.textContent = status.text;
statusElement.className = `status ${status.className}`.trim();
$(`#${model.id}-peak`).textContent =
`${Math.round(model.state.peak)} L/min`;
const overflowElement = $(`#${model.id}-overflow`);
overflowElement.textContent =
`${model.state.totalOverflow.toFixed(1)} L`;
overflowElement.classList.toggle(
"danger",
model.state.totalOverflow > 0.05
);
const label = $(`#label-${model.id}`);
label.querySelector(".label-flow").textContent =
`${Math.round(model.state.outflow)} L/min`;
label.querySelector(".label-state").textContent =
model.state.outflow > GUTTER_CAPACITY + 0.05
? `Overflowing by ${Math.round(model.state.outflow - GUTTER_CAPACITY)} L/min`
: status.text;
label.classList.toggle(
"is-overflowing",
model.state.outflow > GUTTER_CAPACITY + 0.05
);
}
function updateInsight() {
const steep = models[0].state;
const shallow = models[1].state;
const steepPeak = Math.max(0, steep.peak - GUTTER_CAPACITY);
const shallowPeak = Math.max(0, shallow.peak - GUTTER_CAPACITY);
if (steepPeak > 0 && shallowPeak > 0) {
$("#insight").textContent =
`Both roofs exceed the 140 L/min limit. The steep roof peaks ` +
`${Math.round(steepPeak)} L/min over capacity; the shallow roof peaks ` +
`${Math.round(shallowPeak)} L/min over capacity.`;
} else if (steepPeak > 0) {
$("#insight").textContent =
`The steep roof exceeds the matching gutter's 140 L/min capacity by ` +
`${Math.round(steepPeak)} L/min at peak. The shallow roof's delayed, ` +
`lower peak remains within capacity.`;
} else if (shallowPeak > 0) {
$("#insight").textContent =
`The shallow roof briefly exceeds capacity by ${Math.round(shallowPeak)} ` +
`L/min. The steep roof has already passed its sharper peak.`;
} else {
$("#insight").textContent =
`Neither roof currently exceeds the 140 L/min limit. ` +
`The steep roof still reaches the gutter sooner and more sharply.`;
}
}
// ------------------------------------------------------------
// Chart
// ------------------------------------------------------------
function drawChart() {
const rect = chartCanvas.getBoundingClientRect();
if (!rect.width || !rect.height) return;
const dpr = Math.min(window.devicePixelRatio, 2);
const pixelWidth = Math.round(rect.width * dpr);
const pixelHeight = Math.round(rect.height * dpr);
if (
chartCanvas.width !== pixelWidth ||
chartCanvas.height !== pixelHeight
) {
chartCanvas.width = pixelWidth;
chartCanvas.height = pixelHeight;
}
const ctx = chartCanvas.getContext("2d");
ctx.setTransform(dpr, 0, 0, dpr, 0, 0);
ctx.clearRect(0, 0, rect.width, rect.height);
const padding = {
left: 38,
right: 12,
top: 12,
bottom: 27
};
const width = rect.width - padding.left - padding.right;
const height = rect.height - padding.top - padding.bottom;
const x = (t) => padding.left + t / STORM_DURATION * width;
const y = (flow) =>
padding.top + height - flow / CHART_MAX * height;
// Grid and labels
ctx.font = "9px system-ui";
ctx.textAlign = "right";
ctx.textBaseline = "middle";
for (const value of [0, 75, 150, 225, 300]) {
const yy = y(value);
ctx.strokeStyle = "rgba(130, 157, 174, 0.10)";
ctx.lineWidth = 1;
ctx.beginPath();
ctx.moveTo(padding.left, yy);
ctx.lineTo(padding.left + width, yy);
ctx.stroke();
ctx.fillStyle = "#698394";
ctx.fillText(String(value), padding.left - 7, yy);
}
ctx.textAlign = "center";
ctx.textBaseline = "top";
for (const value of [0, 10, 20, 30]) {
const xx = x(value);
ctx.fillStyle = "#698394";
ctx.fillText(`${value}s`, xx, padding.top + height + 9);
}
// Capacity line
ctx.save();
ctx.setLineDash([5, 5]);
ctx.strokeStyle = "#ffb35c";
ctx.lineWidth = 1.4;
ctx.beginPath();
ctx.moveTo(padding.left, y(GUTTER_CAPACITY));
ctx.lineTo(padding.left + width, y(GUTTER_CAPACITY));
ctx.stroke();
ctx.restore();
ctx.textAlign = "left";
ctx.textBaseline = "bottom";
ctx.fillStyle = "#ffb35c";
ctx.fillText(
"140 L/min",
padding.left + 6,
y(GUTTER_CAPACITY) - 4
);
function drawSeries(key, color) {
if (!history.length) return;
ctx.strokeStyle = color;
ctx.lineWidth = 2.4;
ctx.lineJoin = "round";
ctx.lineCap = "round";
ctx.beginPath();
history.forEach((point, index) => {
const xx = x(point.t);
const yy = y(point[key]);
if (index === 0) ctx.moveTo(xx, yy);
else ctx.lineTo(xx, yy);
});
ctx.stroke();
const last = history[history.length - 1];
ctx.fillStyle = color;
ctx.beginPath();
ctx.arc(x(last.t), y(last[key]), 3.1, 0, Math.PI * 2);
ctx.fill();
}
drawSeries("steep", "#59d9e7");
drawSeries("shallow", "#b69cff");
// Storm progression marker
if (history.length && cycleTime <= STORM_DURATION) {
const xx = x(cycleTime);
ctx.strokeStyle = "rgba(218, 235, 243, 0.26)";
ctx.lineWidth = 1;
ctx.beginPath();
ctx.moveTo(xx, padding.top);
ctx.lineTo(xx, padding.top + height);
ctx.stroke();
}
}
// ------------------------------------------------------------
// Controls
// ------------------------------------------------------------
function updateRangeProgress() {
const min = Number(intensityInput.min);
const max = Number(intensityInput.max);
const value = Number(intensityInput.value);
const progress = (value - min) / (max - min) * 100;
intensityInput.style.setProperty(
"--range-progress",
`${progress}%`
);
}
function setIntensity(value) {
rainIntensity = Number(value);
intensityInput.value = rainIntensity;
intensityOutput.value = `${rainIntensity} mm/h`;
intensityOutput.textContent = `${rainIntensity} mm/h`;
updateRangeProgress();
document.querySelectorAll(".scenario-button").forEach((button) => {
button.classList.toggle(
"active",
Number(button.dataset.level) === rainIntensity
);
});
resetSimulation();
}
intensityInput.addEventListener("input", () => {
setIntensity(intensityInput.value);
});
document.querySelectorAll(".scenario-button").forEach((button) => {
button.addEventListener("click", () => {
setIntensity(button.dataset.level);
});
});
playButton.addEventListener("click", () => {
running = !running;
playButton.textContent = running ? "Pause storm" : "Resume storm";
});
restartButton.addEventListener("click", () => {
resetSimulation();
running = true;
playButton.textContent = "Pause storm";
});
// ------------------------------------------------------------
// Resize
// ------------------------------------------------------------
function resizeScene() {
const width = sceneHost.clientWidth;
const height = sceneHost.clientHeight;
renderer.setSize(width, height, false);
camera.aspect = width / height;
camera.updateProjectionMatrix();
updateCamera();
}
const resizeObserver = new ResizeObserver(resizeScene);
resizeObserver.observe(sceneHost);
resizeObserver.observe(chartCanvas.parentElement);
window.addEventListener("resize", resizeScene);
// ------------------------------------------------------------
// Main animation loop
// ------------------------------------------------------------
let previousTime = performance.now();
function animate(now) {
const realDt = Math.min((now - previousTime) / 1000, 0.05);
previousTime = now;
const simulationDt = running ? realDt : 0;
if (running) {
updateSimulation(simulationDt);
}
const profile = stormProfile(cycleTime);
for (const model of models) {
updateHouseVisuals(model, simulationDt, profile);
}
positionModelLabels();
uiAccumulator += realDt;
if (uiAccumulator >= 0.08) {
uiAccumulator = 0;
models.forEach(updateModelUI);
updateInsight();
drawChart();
}
renderer.render(scene, camera);
requestAnimationFrame(animate);
}
setIntensity(150);
resetSimulation();
resizeScene();
updateModelUI(models[0]);
updateModelUI(models[1]);
drawChart();
requestAnimationFrame(animate);
</script>
</body>
</html>
```