Prompt specification
Build or customize the Three.js 3D scene "Tank Size and Seasonal Demand Comparison" 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: Tank Size and Seasonal Demand Comparison
Category: Everyday & Industrial · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/tank-size-and-seasonal-demand-comparison-x3956
Scene Description Prompt:
Compare rainwater tank sizes under identical synthetic rainfall and demand, showing overflow and shortages. Low-poly Three.js from cdnjs, one HTML file.
Working Three.js Starter (tank-size-and-seasonal-demand-comparison-x3956):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Rainvault — Tank Sizing Comparison</title>
<style>
:root {
--bg: #071116;
--panel: #0d1a20;
--panel-2: #101f26;
--line: rgba(159, 204, 214, 0.14);
--text: #e8f3f2;
--muted: #81999f;
--cyan: #55dfc4;
--blue: #65b9ff;
--purple: #b39aff;
--amber: #ffc267;
--red: #ff6f7d;
--radius: 16px;
}
* { box-sizing: border-box; }
html, body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: 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;
cursor: pointer;
}
.app {
height: 100%;
display: grid;
grid-template-rows: 66px minmax(0, 1fr);
}
header {
display: flex;
align-items: center;
justify-content: space-between;
gap: 20px;
padding: 0 24px;
border-bottom: 1px solid var(--line);
background: rgba(7, 17, 22, 0.92);
z-index: 10;
}
.brand {
display: flex;
align-items: center;
gap: 13px;
min-width: 0;
}
.brand-mark {
width: 34px;
height: 38px;
position: relative;
flex: 0 0 auto;
}
.brand-mark::before,
.brand-mark::after {
content: "";
position: absolute;
bottom: 3px;
width: 14px;
border: 2px solid var(--cyan);
border-top: 0;
}
.brand-mark::before {
left: 1px;
height: 22px;
border-radius: 0 0 5px 5px;
}
.brand-mark::after {
right: 1px;
height: 30px;
border-color: var(--blue);
border-top: 0;
border-radius: 0 0 5px 5px;
}
.brand h1 {
margin: 0;
font-size: 16px;
line-height: 1;
letter-spacing: 0.17em;
font-weight: 800;
}
.brand p {
margin: 6px 0 0;
color: var(--muted);
font-size: 11px;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.header-actions {
display: flex;
align-items: center;
gap: 10px;
}
.scenario-badge {
padding: 8px 12px;
border: 1px solid rgba(85, 223, 196, 0.25);
border-radius: 999px;
color: #a8e9dc;
background: rgba(85, 223, 196, 0.07);
font-size: 11px;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.icon-button {
border: 1px solid var(--line);
background: var(--panel);
border-radius: 10px;
padding: 9px 12px;
color: #b9cccf;
font-size: 12px;
transition: 160ms ease;
}
.icon-button:hover {
border-color: rgba(85, 223, 196, 0.45);
color: white;
background: #14262d;
}
.workspace {
min-height: 0;
display: grid;
grid-template-columns: 326px minmax(0, 1fr);
}
aside {
min-height: 0;
overflow-y: auto;
border-right: 1px solid var(--line);
background: #0a151a;
scrollbar-width: thin;
scrollbar-color: #263b42 transparent;
}
.sidebar-section {
padding: 20px;
border-bottom: 1px solid var(--line);
}
.section-kicker {
color: var(--muted);
font-size: 10px;
font-weight: 750;
letter-spacing: 0.14em;
text-transform: uppercase;
}
h2 {
margin: 7px 0 0;
font-size: 17px;
line-height: 1.3;
}
.intro {
margin: 9px 0 15px;
color: #8da5aa;
font-size: 12px;
line-height: 1.55;
}
.rain-strip {
height: 48px;
display: flex;
align-items: flex-end;
gap: 2px;
padding: 7px;
border: 1px solid var(--line);
border-radius: 10px;
background: rgba(255,255,255,0.018);
overflow: hidden;
}
.rain-bar {
flex: 1;
min-width: 2px;
border-radius: 2px 2px 0 0;
background: #327ba8;
opacity: 0.7;
}
.rain-bar.storm {
background: var(--blue);
box-shadow: 0 0 9px rgba(101,185,255,0.25);
opacity: 1;
}
.facts {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 8px;
margin-top: 12px;
}
.fact {
padding: 10px;
border: 1px solid var(--line);
border-radius: 10px;
background: rgba(255,255,255,0.018);
}
.fact span {
display: block;
color: var(--muted);
font-size: 9px;
letter-spacing: 0.1em;
text-transform: uppercase;
}
.fact strong {
display: block;
margin-top: 5px;
font: 650 14px ui-monospace, SFMono-Regular, Menlo, monospace;
}
.play-row {
display: grid;
grid-template-columns: 1fr auto;
gap: 9px;
margin-top: 15px;
}
.primary-button {
border: 0;
border-radius: 10px;
padding: 10px 16px;
background: var(--cyan);
color: #06221d;
font-weight: 800;
font-size: 12px;
box-shadow: 0 6px 24px rgba(85,223,196,0.12);
}
.primary-button:hover { filter: brightness(1.08); }
.secondary-button {
border: 1px solid var(--line);
border-radius: 10px;
padding: 9px 12px;
background: var(--panel-2);
color: #b8c9cc;
font-size: 11px;
}
.speed-row {
display: flex;
align-items: center;
justify-content: space-between;
gap: 10px;
margin-top: 13px;
}
.speed-row > span {
color: var(--muted);
font-size: 10px;
text-transform: uppercase;
letter-spacing: 0.1em;
}
.segmented {
display: flex;
gap: 3px;
padding: 3px;
background: #071115;
border: 1px solid var(--line);
border-radius: 9px;
}
.segmented button {
border: 0;
border-radius: 6px;
padding: 5px 8px;
background: transparent;
color: #718b90;
font-size: 10px;
}
.segmented button.active {
color: #071713;
background: var(--cyan);
font-weight: 800;
}
.timeline {
width: 100%;
margin: 15px 0 4px;
accent-color: var(--cyan);
cursor: pointer;
}
.timeline-labels {
display: flex;
justify-content: space-between;
color: #6e878d;
font: 10px ui-monospace, SFMono-Regular, Menlo, monospace;
}
.comparison {
width: 100%;
border-collapse: collapse;
margin-top: 12px;
font-size: 11px;
}
.comparison th {
padding: 0 0 9px;
color: #6f888e;
text-align: right;
font-size: 9px;
font-weight: 650;
letter-spacing: 0.07em;
text-transform: uppercase;
}
.comparison th:first-child,
.comparison td:first-child { text-align: left; }
.comparison td {
padding: 10px 0;
border-top: 1px solid var(--line);
text-align: right;
vertical-align: middle;
}
.tank-name {
display: flex;
align-items: center;
gap: 8px;
font-weight: 700;
}
.tank-dot {
width: 8px;
height: 8px;
border-radius: 50%;
box-shadow: 0 0 9px currentColor;
}
.amount {
font: 700 11px ui-monospace, SFMono-Regular, Menlo, monospace;
}
.amount.amber { color: var(--amber); }
.amount.red { color: var(--red); }
.event-days {
display: block;
margin-top: 3px;
color: #617a80;
font-size: 9px;
}
.verdict {
margin-top: 14px;
padding: 12px;
border: 1px solid rgba(85,223,196,0.18);
border-radius: 11px;
background: rgba(85,223,196,0.055);
}
.verdict span {
color: #76aaa1;
font-size: 9px;
text-transform: uppercase;
letter-spacing: 0.11em;
}
.verdict strong {
display: block;
margin-top: 6px;
font-size: 12px;
line-height: 1.45;
}
.verdict p {
margin: 6px 0 0;
color: #7e989d;
font-size: 10px;
line-height: 1.5;
}
.assumption-list {
display: grid;
gap: 9px;
margin-top: 12px;
}
.assumption {
display: flex;
justify-content: space-between;
gap: 14px;
color: #789096;
font-size: 10px;
}
.assumption b {
color: #c2d1d3;
font-weight: 650;
text-align: right;
}
.visual {
min-width: 0;
min-height: 0;
display: grid;
grid-template-rows: minmax(390px, 1fr) 245px;
background: radial-gradient(circle at 55% 35%, #10262d 0, #071116 58%);
}
.scene-wrap {
position: relative;
min-width: 0;
min-height: 0;
overflow: hidden;
}
#scene {
display: block;
width: 100%;
height: 100%;
touch-action: none;
cursor: grab;
outline: none;
}
#scene:active { cursor: grabbing; }
.scene-top {
position: absolute;
top: 18px;
left: 20px;
right: 20px;
display: flex;
align-items: flex-start;
justify-content: space-between;
gap: 14px;
pointer-events: none;
}
.scene-title {
padding: 12px 14px;
border: 1px solid var(--line);
border-radius: 12px;
background: rgba(7,17,22,0.72);
backdrop-filter: blur(10px);
}
.scene-title strong {
display: block;
font-size: 13px;
}
.scene-title span {
display: block;
margin-top: 5px;
color: var(--muted);
font-size: 10px;
}
.weather-chips {
display: flex;
gap: 7px;
flex-wrap: wrap;
justify-content: flex-end;
}
.weather-chip {
min-width: 94px;
padding: 9px 11px;
border: 1px solid var(--line);
border-radius: 10px;
background: rgba(7,17,22,0.72);
backdrop-filter: blur(10px);
}
.weather-chip span {
display: block;
color: var(--muted);
font-size: 8px;
letter-spacing: 0.1em;
text-transform: uppercase;
}
.weather-chip strong {
display: block;
margin-top: 4px;
font: 700 12px ui-monospace, SFMono-Regular, Menlo, monospace;
}
#labels {
position: absolute;
inset: 0;
overflow: hidden;
pointer-events: none;
}
.tank-label {
position: absolute;
left: 0;
top: 0;
width: 128px;
padding: 9px 10px;
border: 1px solid rgba(173,211,217,0.18);
border-radius: 10px;
background: rgba(7,17,22,0.83);
box-shadow: 0 10px 25px rgba(0,0,0,0.22);
backdrop-filter: blur(8px);
will-change: transform;
}
.tank-label-top {
display: flex;
align-items: center;
gap: 6px;
}
.tank-label-top b {
font-size: 11px;
}
.tank-label-top .dot {
width: 6px;
height: 6px;
border-radius: 50%;
flex: 0 0 auto;
}
.tank-state {
margin-left: auto;
color: #719096;
font-size: 7px;
font-weight: 800;
letter-spacing: 0.05em;
}
.tank-label small {
display: block;
margin-top: 5px;
color: #90a5aa;
font: 9px ui-monospace, SFMono-Regular, Menlo, monospace;
}
.tank-bar {
height: 3px;
margin-top: 7px;
border-radius: 4px;
overflow: hidden;
background: #203138;
}
.tank-bar i {
display: block;
height: 100%;
width: 0;
border-radius: inherit;
background: var(--cyan);
}
.scene-legend {
position: absolute;
left: 20px;
bottom: 17px;
display: flex;
align-items: center;
gap: 14px;
padding: 9px 11px;
border: 1px solid var(--line);
border-radius: 10px;
background: rgba(7,17,22,0.7);
color: #82999e;
font-size: 9px;
pointer-events: none;
}
.legend-item {
display: flex;
align-items: center;
gap: 6px;
}
.legend-swatch {
width: 8px;
height: 8px;
border-radius: 2px;
}
.scene-help {
position: absolute;
right: 20px;
bottom: 17px;
color: #607b81;
font-size: 9px;
pointer-events: none;
}
.chart-panel {
min-height: 0;
padding: 14px 18px 12px;
border-top: 1px solid var(--line);
background: #0a151a;
}
.chart-header {
display: flex;
align-items: flex-start;
justify-content: space-between;
gap: 20px;
}
.chart-header h3 {
margin: 0;
font-size: 13px;
}
.chart-header p {
margin: 5px 0 0;
color: #718a90;
font-size: 9px;
}
.chart-legend {
display: flex;
gap: 12px;
flex-wrap: wrap;
justify-content: flex-end;
color: #81999f;
font-size: 9px;
}
.chart-legend span {
display: flex;
align-items: center;
gap: 5px;
}
#chart {
display: block;
width: 100%;
height: 177px;
margin-top: 3px;
cursor: crosshair;
}
.loading {
position: absolute;
inset: 0;
display: grid;
place-items: center;
color: var(--muted);
font-size: 12px;
pointer-events: none;
}
@media (max-width: 900px) {
html, body { overflow: auto; }
.app {
height: auto;
min-height: 100%;
grid-template-rows: 66px auto;
}
.workspace {
display: flex;
flex-direction: column;
}
aside {
order: 2;
overflow: visible;
border-right: 0;
}
.visual {
order: 1;
grid-template-rows: 62vh 245px;
}
.sidebar-section { padding: 18px; }
}
@media (max-width: 600px) {
header { padding: 0 14px; }
.scenario-badge { display: none; }
.brand p { display: none; }
.scene-top { top: 12px; left: 12px; right: 12px; }
.scene-title { display: none; }
.weather-chips { margin-left: auto; }
.weather-chip { min-width: 78px; padding: 8px; }
.scene-help { display: none; }
.scene-legend { left: 12px; bottom: 12px; gap: 9px; }
.tank-label { width: 106px; padding: 7px 8px; }
.tank-label small { font-size: 8px; }
.chart-panel { padding-left: 10px; padding-right: 10px; }
.chart-header p { max-width: 210px; }
.chart-legend { display: none; }
}
</style>
</head>
<body>
<div class="app">
<header>
<div class="brand">
<div class="brand-mark" aria-hidden="true"></div>
<div>
<h1>RAINVAULT</h1>
<p>Tank sizing laboratory</p>
</div>
</div>
<div class="header-actions">
<div class="scenario-badge">Synthetic storm train · 46 days</div>
<button class="icon-button" id="resetCamera" title="Reset camera">Reset view</button>
</div>
</header>
<main class="workspace">
<aside>
<section class="sidebar-section">
<div class="section-kicker">Shared scenario</div>
<h2>Identical rain. Identical demand.</h2>
<p class="intro">
Every tank receives the same storm sequence and serves the same daily
household demand. Compare how capacity changes overflow and shortages.
</p>
<div class="rain-strip" id="rainStrip" aria-label="Synthetic daily rainfall profile"></div>
<div class="facts">
<div class="fact">
<span>Catchment</span>
<strong>30 m²</strong>
</div>
<div class="fact">
<span>Demand</span>
<strong>55 L/day</strong>
</div>
<div class="fact">
<span>Runoff</span>
<strong>80%</strong>
</div>
<div class="fact">
<span>Tank range</span>
<strong>600–1800 L</strong>
</div>
</div>
</section>
<section class="sidebar-section">
<div class="section-kicker">Simulation</div>
<h2>Timeline</h2>
<div class="play-row">
<button class="primary-button" id="playButton">Pause</button>
<button class="secondary-button" id="resetButton">Restart</button>
</div>
<div class="speed-row">
<span>Playback speed</span>
<div class="segmented" id="speedControls">
<button data-speed="0.5">0.5×</button>
<button class="active" data-speed="1">1×</button>
<button data-speed="2">2×</button>
<button data-speed="4">4×</button>
</div>
</div>
<input class="timeline" id="timeline" type="range" min="0" max="45" value="0" step="0.01" aria-label="Simulation day">
<div class="timeline-labels">
<span>DAY 01</span>
<span id="timelineValue">DAY 01 / 46</span>
<span>DAY 46</span>
</div>
</section>
<section class="sidebar-section">
<div class="section-kicker">Cumulative balance</div>
<h2>Tank comparison</h2>
<table class="comparison">
<thead>
<tr>
<th>Tank</th>
<th>Overflow</th>
<th>Shortage</th>
</tr>
</thead>
<tbody id="comparisonBody"></tbody>
</table>
<div class="verdict">
<span>End-of-run result</span>
<strong id="verdictTitle"></strong>
<p id="verdictText"></p>
</div>
</section>
<section class="sidebar-section">
<div class="section-kicker">Method</div>
<h2>Water balance</h2>
<div class="assumption-list">
<div class="assumption"><span>Rainfall sequence</span><b>Fixed synthetic events</b></div>
<div class="assumption"><span>Initial storage</span><b>0 L</b></div>
<div class="assumption"><span>Daily demand</span><b>55 L</b></div>
<div class="assumption"><span>Overflow</span><b>Water above capacity</b></div>
<div class="assumption"><span>Shortage</span><b>Demand not supplied</b></div>
</div>
</section>
</aside>
<section class="visual">
<div class="scene-wrap">
<canvas id="scene" tabindex="0" aria-label="Interactive 3D rainwater tank comparison"></canvas>
<div class="loading" id="loading">Building simulation…</div>
<div id="labels"></div>
<div class="scene-top">
<div class="scene-title">
<strong>Catchment-to-storage comparison</strong>
<span>Drag to orbit · wheel to zoom · two fingers to pinch</span>
</div>
<div class="weather-chips">
<div class="weather-chip">
<span>Current rain</span>
<strong id="rainReadout">0 mm/day</strong>
</div>
<div class="weather-chip">
<span>Daily demand</span>
<strong>55 L</strong>
</div>
<div class="weather-chip">
<span>Runoff today</span>
<strong id="runoffReadout">0 L</strong>
</div>
</div>
</div>
<div class="scene-legend">
<div class="legend-item">
<span class="legend-swatch" style="background:var(--cyan)"></span>
Stored water
</div>
<div class="legend-item">
<span class="legend-swatch" style="background:var(--amber)"></span>
Overflow
</div>
<div class="legend-item">
<span class="legend-swatch" style="background:var(--red)"></span>
Shortage
</div>
</div>
<div class="scene-help">Space: play/pause · ←/→: step day · R: restart</div>
</div>
<section class="chart-panel">
<div class="chart-header">
<div>
<h3>Storage trajectories</h3>
<p>Solid lines: storage · dashed lines: capacity · rings: overflow · red notches: shortage</p>
</div>
<div class="chart-legend" id="chartLegend"></div>
</div>
<canvas id="chart" aria-label="Storage comparison chart"></canvas>
</section>
</section>
</main>
</div>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// ------------------------------------------------------------
// Shared synthetic scenario and daily water-balance model
// ------------------------------------------------------------
const RAIN = [
0, 0, 12, 38, 62, 18, 0, 0, 8, 28, 54, 15,
0, 0, 20, 44, 66, 10, 0, 0, 5, 22, 48, 58,
0, 0, 16, 35, 52, 20, 0, 0, 10, 30, 46, 0,
0, 0, 25, 42, 55, 8, 0, 0, 0
];
const TANKS = [
{ capacity: 600, color: 0x55dfc4, css: "#55dfc4" },
{ capacity: 1000, color: 0x65b9ff, css: "#65b9ff" },
{ capacity: 1400, color: 0xb39aff, css: "#b39aff" },
{ capacity: 1800, color: 0xffc267, css: "#ffc267" }
];
const ROOF_AREA = 30;
const RUNOFF = 0.80;
const DAILY_DEMAND = 55;
const DAYS = RAIN.length;
const MAX_RAIN = Math.max(...RAIN);
function simulateTank(capacity) {
let storage = 0;
let cumulativeOverflow = 0;
let cumulativeShortage = 0;
let overflowDays = 0;
let shortageDays = 0;
const daily = RAIN.map((rain, day) => {
// 1 mm over 1 m² equals 1 litre.
const inflow = rain * ROOF_AREA * RUNOFF;
const available = storage + inflow;
const served = Math.min(DAILY_DEMAND, available);
const shortage = DAILY_DEMAND - served;
// Demand is served before excess water is counted as overflow.
const remaining = available - served;
const overflow = Math.max(0, remaining - capacity);
storage = Math.min(capacity, remaining);
cumulativeOverflow += overflow;
cumulativeShortage += shortage;
if (overflow > 0.01) overflowDays++;
if (shortage > 0.01) shortageDays++;
return {
day,
rain,
inflow,
served,
shortage,
overflow,
storage,
cumulativeOverflow,
cumulativeShortage,
overflowDays,
shortageDays
};
});
return daily;
}
const simulations = TANKS.map(tank => simulateTank(tank.capacity));
const finalResults = simulations.map(result => result[result.length - 1]);
const savedShortage =
1 - finalResults[3].cumulativeShortage / Math.max(1, finalResults[0].cumulativeShortage);
const overflowDifference =
finalResults[0].cumulativeOverflow - finalResults[3].cumulativeOverflow;
document.getElementById("verdictTitle").textContent =
`The 1,800 L tank cuts unmet demand by ${Math.round(savedShortage * 100)}%.`;
document.getElementById("verdictText").textContent =
`By day 46, it avoids ${Math.round(overflowDifference).toLocaleString()} L of overflow ` +
`versus the 600 L tank, but still loses ${Math.round(finalResults[3].cumulativeOverflow).toLocaleString()} L during peak storms.`;
// ------------------------------------------------------------
// Interface construction
// ------------------------------------------------------------
const rainStrip = document.getElementById("rainStrip");
RAIN.forEach(rain => {
const bar = document.createElement("div");
bar.className = `rain-bar${rain >= 40 ? " storm" : ""}`;
bar.style.height = `${Math.max(4, rain / MAX_RAIN * 100)}%`;
bar.title = `${rain} mm/day`;
rainStrip.appendChild(bar);
});
const comparisonBody = document.getElementById("comparisonBody");
const tableRows = TANKS.map((tank, index) => {
const row = document.createElement("tr");
row.innerHTML = `
<td>
<div class="tank-name">
<span class="tank-dot" style="background:${tank.css};color:${tank.css}"></span>
${tank.capacity.toLocaleString()} L
</div>
</td>
<td>
<span class="amount amber">0 L</span>
<span class="event-days">0 days</span>
</td>
<td>
<span class="amount red">0 L</span>
<span class="event-days">0 days</span>
</td>
`;
comparisonBody.appendChild(row);
return {
overflow: row.querySelectorAll(".amount")[0],
overflowDays: row.querySelectorAll(".event-days")[0],
shortage: row.querySelectorAll(".amount")[1],
shortageDays: row.querySelectorAll(".event-days")[1]
};
});
const chartLegend = document.getElementById("chartLegend");
TANKS.forEach(tank => {
const item = document.createElement("span");
item.innerHTML = `<i class="legend-swatch" style="background:${tank.css}"></i>${tank.capacity} L`;
chartLegend.appendChild(item);
});
// ------------------------------------------------------------
// Three.js scene
// ------------------------------------------------------------
const sceneCanvas = document.getElementById("scene");
const renderer = new THREE.WebGLRenderer({
canvas: sceneCanvas,
antialias: true,
alpha: false,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 1.8));
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.15;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x071116);
scene.fog = new THREE.Fog(0x071116, 10, 22);
const camera = new THREE.PerspectiveCamera(42, 1, 0.1, 100);
const hemisphere = new THREE.HemisphereLight(0xc6efff, 0x142128, 2.1);
scene.add(hemisphere);
const keyLight = new THREE.DirectionalLight(0xe4f7ff, 3.2);
keyLight.position.set(4, 8, 5);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(1024, 1024);
keyLight.shadow.camera.left = -7;
keyLight.shadow.camera.right = 7;
keyLight.shadow.camera.top = 7;
keyLight.shadow.camera.bottom = -5;
keyLight.shadow.camera.near = 1;
keyLight.shadow.camera.far = 25;
scene.add(keyLight);
const rimLight = new THREE.DirectionalLight(0x3da4ff, 1.5);
rimLight.position.set(-5, 4, -4);
scene.add(rimLight);
const floor = new THREE.Mesh(
new THREE.PlaneGeometry(18, 11),
new THREE.MeshStandardMaterial({
color: 0x0c1b21,
roughness: 0.92,
metalness: 0.08
})
);
floor.rotation.x = -Math.PI / 2;
floor.position.y = -0.015;
floor.receiveShadow = true;
scene.add(floor);
const grid = new THREE.GridHelper(18, 24, 0x24414a, 0x152a31);
grid.position.y = 0.004;
grid.material.transparent = true;
grid.material.opacity = 0.45;
scene.add(grid);
// Low-poly catchment building.
const house = new THREE.Group();
house.position.set(0, 0, -2.7);
const houseBody = new THREE.Mesh(
new THREE.BoxGeometry(1.45, 0.95, 1.1),
new THREE.MeshStandardMaterial({
color: 0x253a42,
roughness: 0.75,
metalness: 0.12
})
);
houseBody.position.y = 0.475;
houseBody.castShadow = true;
houseBody.receiveShadow = true;
house.add(houseBody);
const houseRoof = new THREE.Mesh(
new THREE.ConeGeometry(1.04, 0.66, 4),
new THREE.MeshStandardMaterial({
color: 0x41616b,
roughness: 0.66,
metalness: 0.15,
flatShading: true
})
);
houseRoof.position.y = 1.27;
houseRoof.rotation.y = Math.PI / 4;
houseRoof.scale.z = 0.82;
houseRoof.castShadow = true;
house.add(houseRoof);
const windowMesh = new THREE.Mesh(
new THREE.BoxGeometry(0.28, 0.28, 0.025),
new THREE.MeshStandardMaterial({
color: 0x73d8e5,
emissive: 0x235d68,
emissiveIntensity: 0.7,
roughness: 0.3
})
);
windowMesh.position.set(0.35, 0.54, 0.565);
house.add(windowMesh);
const downspout = new THREE.Mesh(
new THREE.CylinderGeometry(0.035, 0.035, 1.25, 8),
new THREE.MeshStandardMaterial({ color: 0x62818a, metalness: 0.5, roughness: 0.4 })
);
downspout.position.set(0.77, 0.63, 0.12);
house.add(downspout);
scene.add(house);
// Pipe manifold and branches.
const pipeMaterial = new THREE.MeshStandardMaterial({
color: 0x36545d,
metalness: 0.55,
roughness: 0.38
});
const manifold = new THREE.Mesh(
new THREE.CylinderGeometry(0.045, 0.045, 6.5, 8),
pipeMaterial
);
manifold.rotation.z = Math.PI / 2;
manifold.position.set(0, 0.27, -1.45);
scene.add(manifold);
// Low-poly storm cloud.
const cloud = new THREE.Group();
cloud.position.set(0, 4.55, -2.25);
const cloudMaterial = new THREE.MeshStandardMaterial({
color: 0x263d48,
roughness: 1,
flatShading: true
});
const cloudOffsets = [
[-0.85, 0, 0, 0.66],
[-0.28, 0.2, 0.02, 0.82],
[0.36, 0.12, 0, 0.88],
[0.95, -0.03, 0.03, 0.58],
[0.05, -0.15, 0.1, 0.62]
];
cloudOffsets.forEach(([x, y, z, scale]) => {
const puff = new THREE.Mesh(
new THREE.DodecahedronGeometry(scale, 0),
cloudMaterial
);
puff.position.set(x, y, z);
puff.scale.set(1.25, 0.72, 0.9);
cloud.add(puff);
});
scene.add(cloud);
const tankViews = [];
function createTank(tank, index) {
const x = (index - 1.5) * 2.2;
const z = 0.42;
const radius = 0.72;
const height = 1.25 + tank.capacity / 1000 * 0.58;
const baseY = 0.18;
const group = new THREE.Group();
group.position.set(x, 0, z);
const platform = new THREE.Mesh(
new THREE.CylinderGeometry(radius + 0.17, radius + 0.20, 0.14, 12),
new THREE.MeshStandardMaterial({
color: 0x152a31,
roughness: 0.78,
metalness: 0.12,
flatShading: true
})
);
platform.position.y = 0.07;
platform.receiveShadow = true;
platform.castShadow = true;
group.add(platform);
const shellMaterial = new THREE.MeshStandardMaterial({
color: 0x86aeb7,
roughness: 0.38,
metalness: 0.12,
transparent: true,
opacity: 0.29,
side: THREE.DoubleSide,
depthWrite: false
});
const shell = new THREE.Mesh(
new THREE.CylinderGeometry(radius, radius, height, 12, 1, true),
shellMaterial
);
shell.position.y = baseY + height / 2;
group.add(shell);
const waterMaterial = new THREE.MeshStandardMaterial({
color: tank.color,
emissive: tank.color,
emissiveIntensity: 0.13,
roughness: 0.24,
metalness: 0.02,
transparent: true,
opacity: 0.88
});
const waterGeometry = new THREE.CylinderGeometry(
radius * 0.86, radius * 0.86, 1, 12, 1, false
);
waterGeometry.translate(0, 0.5, 0);
const water = new THREE.Mesh(waterGeometry, waterMaterial);
water.position.y = baseY + 0.025;
water.castShadow = true;
group.add(water);
const surfaceMaterial = new THREE.MeshStandardMaterial({
color: tank.color,
emissive: tank.color,
emissiveIntensity: 0.2,
roughness: 0.18,
metalness: 0.02,
side: THREE.DoubleSide
});
const surface = new THREE.Mesh(
new THREE.CircleGeometry(radius * 0.855, 12),
surfaceMaterial
);
surface.rotation.x = -Math.PI / 2;
surface.position.y = baseY + 0.03;
group.add(surface);
const rimMaterial = new THREE.MeshStandardMaterial({
color: 0x7897a0,
roughness: 0.38,
metalness: 0.48
});
function addRing(y, tube = 0.026, material = rimMaterial) {
const ring = new THREE.Mesh(
new THREE.TorusGeometry(radius, tube, 6, 12),
material
);
ring.rotation.x = Math.PI / 2;
ring.position.y = y;
group.add(ring);
return ring;
}
addRing(baseY + height, 0.036);
addRing(baseY + 0.02, 0.03);
[0.25, 0.5, 0.75].forEach(fraction => {
addRing(baseY + height * fraction, 0.014);
});
const statusRingMaterial = new THREE.MeshBasicMaterial({
color: tank.color,
transparent: true,
opacity: 0.12
});
const statusRing = addRing(
0.155,
0.018,
statusRingMaterial
);
const outlet = new THREE.Mesh(
new THREE.CylinderGeometry(0.045, 0.045, 0.48, 8),
pipeMaterial
);
outlet.position.set(radius + 0.03, 0.25, 0);
group.add(outlet);
const beacon = new THREE.Mesh(
new THREE.OctahedronGeometry(0.085, 0),
new THREE.MeshStandardMaterial({
color: 0x4f747c,
emissive: 0x000000,
roughness: 0.4
})
);
beacon.position.set(radius + 0.12, 0.4, 0);
group.add(beacon);
const branch = new THREE.Mesh(
new THREE.CylinderGeometry(0.027, 0.027, 1.68, 7),
pipeMaterial
);
branch.rotation.x = Math.PI / 2;
branch.position.set(x, 0.27, -0.64);
scene.add(branch);
// Overflow droplets.
const overflowCount = 24;
const overflowPositions = new Float32Array(overflowCount * 3);
const overflowGeometry = new THREE.BufferGeometry();
overflowGeometry.setAttribute(
"position",
new THREE.BufferAttribute(overflowPositions, 3)
);
const overflowMaterial = new THREE.PointsMaterial({
color: 0xffbd61,
size: 0.075,
transparent: true,
opacity: 0.95,
depthWrite: false
});
const overflowParticles = new THREE.Points(overflowGeometry, overflowMaterial);
overflowParticles.frustumCulled = false;
overflowParticles.visible = false;
scene.add(overflowParticles);
scene.add(group);
const label = document.createElement("div");
label.className = "tank-label";
label.innerHTML = `
<div class="tank-label-top">
<span class="dot" style="background:${tank.css}"></span>
<b>${tank.capacity.toLocaleString()} L</b>
<span class="tank-state">BALANCED</span>
</div>
<div class="tank-bar"><i style="background:${tank.css}"></i></div>
<small>0% · 0 L</small>
`;
document.getElementById("labels").appendChild(label);
return {
tank,
index,
x,
z,
radius,
height,
baseY,
group,
water,
waterMaterial,
surface,
surfaceMaterial,
statusRing,
statusRingMaterial,
beacon,
overflowParticles,
overflowPositions,
overflowGeometry,
overflowCount,
label,
labelState: label.querySelector(".tank-state"),
labelBar: label.querySelector(".tank-bar i"),
labelDetail: label.querySelector("small")
};
}
TANKS.forEach((tank, index) => tankViews.push(createTank(tank, index)));
// Rain streaks.
const RAIN_COUNT = 150;
const rainPositions = new Float32Array(RAIN_COUNT * 6);
const rainDrops = [];
for (let i = 0; i < RAIN_COUNT; i++) {
rainDrops.push({
x: Math.random() * 12 - 6,
y: Math.random() * 6,
z: Math.random() * 6 - 3.3,
speed: 4 + Math.random() * 4
});
}
const rainGeometry = new THREE.BufferGeometry();
rainGeometry.setAttribute(
"position",
new THREE.BufferAttribute(rainPositions, 3)
);
const rainMaterial = new THREE.LineBasicMaterial({
color: 0x65b9ff,
transparent: true,
opacity: 0.34,
depthWrite: false
});
const rainLines = new THREE.LineSegments(rainGeometry, rainMaterial);
rainLines.frustumCulled = false;
rainLines.visible = false;
scene.add(rainLines);
// ------------------------------------------------------------
// Direct camera controls: orbit, zoom, pinch
// ------------------------------------------------------------
const cameraState = {
theta: 0.48,
phi: 1.12,
radius: 10.4,
target: new THREE.Vector3(0, 1.05, 0)
};
function resetCamera() {
cameraState.theta = 0.48;
cameraState.phi = 1.12;
cameraState.radius = 10.4;
cameraState.target.set(0, 1.05, 0);
}
function updateCamera() {
const sinPhi = Math.sin(cameraState.phi);
camera.position.set(
cameraState.target.x + cameraState.radius * sinPhi * Math.sin(cameraState.theta),
cameraState.target.y + cameraState.radius * Math.cos(cameraState.phi),
cameraState.target.z + cameraState.radius * sinPhi * Math.cos(cameraState.theta)
);
camera.lookAt(cameraState.target);
}
const pointers = new Map();
let previousPinchDistance = 0;
sceneCanvas.addEventListener("pointerdown", event => {
sceneCanvas.setPointerCapture(event.pointerId);
pointers.set(event.pointerId, {
x: event.clientX,
y: event.clientY
});
if (pointers.size === 2) {
const [a, b] = [...pointers.values()];
previousPinchDistance = Math.hypot(a.x - b.x, a.y - b.y);
}
});
sceneCanvas.addEventListener("pointermove", event => {
if (!pointers.has(event.pointerId)) return;
const previous = pointers.get(event.pointerId);
const current = { x: event.clientX, y: event.clientY };
pointers.set(event.pointerId, current);
if (pointers.size === 1) {
const dx = current.x - previous.x;
const dy = current.y - previous.y;
cameraState.theta -= dx * 0.006;
cameraState.phi = THREE.MathUtils.clamp(
cameraState.phi - dy * 0.005,
0.48,
1.38
);
} else if (pointers.size === 2) {
const [a, b] = [...pointers.values()];
const distance = Math.hypot(a.x - b.x, a.y - b.y);
if (previousPinchDistance > 0) {
cameraState.radius = THREE.MathUtils.clamp(
cameraState.radius * (previousPinchDistance / distance),
6.6,
16
);
}
previousPinchDistance = distance;
}
});
function releasePointer(event) {
pointers.delete(event.pointerId);
previousPinchDistance = 0;
}
sceneCanvas.addEventListener("pointerup", releasePointer);
sceneCanvas.addEventListener("pointercancel", releasePointer);
sceneCanvas.addEventListener("wheel", event => {
event.preventDefault();
cameraState.radius = THREE.MathUtils.clamp(
cameraState.radius * Math.exp(event.deltaY * 0.001),
6.6,
16
);
}, { passive: false });
sceneCanvas.addEventListener("dblclick", resetCamera);
document.getElementById("resetCamera").addEventListener("click", resetCamera);
// ------------------------------------------------------------
// Playback state
// ------------------------------------------------------------
let dayFloat = 0;
let playing = true;
let speed = 1;
const playButton = document.getElementById("playButton");
const timeline = document.getElementById("timeline");
const timelineValue = document.getElementById("timelineValue");
const rainReadout = document.getElementById("rainReadout");
const runoffReadout = document.getElementById("runoffReadout");
function updatePlayButton() {
playButton.textContent = playing ? "Pause" : (dayFloat >= DAYS ? "Replay" : "Play");
}
function togglePlayback() {
if (dayFloat >= DAYS) dayFloat = 0;
playing = !playing;
updatePlayButton();
}
playButton.addEventListener("click", togglePlayback);
document.getElementById("resetButton").addEventListener("click", () => {
dayFloat = 0;
playing = true;
updatePlayButton();
});
timeline.addEventListener("input", () => {
dayFloat = Number(timeline.value);
playing = false;
updatePlayButton();
});
document.getElementById("speedControls").addEventListener("click", event => {
const button = event.target.closest("button[data-speed]");
if (!button) return;
speed = Number(button.dataset.speed);
document.querySelectorAll("#speedControls button").forEach(item => {
item.classList.toggle("active", item === button);
});
});
window.addEventListener("keydown", event => {
if (event.target instanceof HTMLInputElement) return;
if (event.code === "Space") {
event.preventDefault();
togglePlayback();
} else if (event.code === "ArrowRight") {
event.preventDefault();
dayFloat = Math.min(DAYS, dayFloat + 1);
playing = false;
updatePlayButton();
} else if (event.code === "ArrowLeft") {
event.preventDefault();
dayFloat = Math.max(0, dayFloat - 1);
playing = false;
updatePlayButton();
} else if (event.code === "KeyR") {
dayFloat = 0;
playing = true;
updatePlayButton();
}
});
// ------------------------------------------------------------
// Formatting and scene-state updates
// ------------------------------------------------------------
function formatLitres(value) {
if (value >= 10000) return `${(value / 1000).toFixed(1)}k L`;
if (value >= 1000) return `${(value / 1000).toFixed(2)}k L`;
return `${Math.round(value)} L`;
}
const normalWater = new THREE.Color();
const shortageWater = new THREE.Color(0xff6f7d);
const amberColor = new THREE.Color(0xffc267);
const projected = new THREE.Vector3();
function updateTankViews(dayIndex, fraction, elapsed) {
tankViews.forEach((view, index) => {
const result = simulations[index][dayIndex];
const next = simulations[index][Math.min(dayIndex + 1, DAYS - 1)];
const storage = THREE.MathUtils.lerp(result.storage, next.storage, fraction);
const fill = THREE.MathUtils.clamp(storage / view.tank.capacity, 0, 1);
const waterHeight = Math.max(0.012, fill * view.height);
view.water.scale.y = waterHeight;
view.surface.position.y = view.baseY + 0.025 + waterHeight;
normalWater.setHex(view.tank.color);
const shortageActive = result.shortage > 0.01;
const overflowActive = result.overflow > 0.01;
if (shortageActive) {
view.waterMaterial.color.copy(shortageWater);
view.waterMaterial.emissive.copy(shortageWater);
view.surfaceMaterial.color.copy(shortageWater);
view.surfaceMaterial.emissive.copy(shortageWater);
view.statusRingMaterial.color.setHex(0xff6f7d);
view.statusRingMaterial.opacity = 0.95;
view.beacon.material.color.setHex(0xff6f7d);
view.beacon.material.emissive.setHex(0x8b1625);
view.beacon.visible = true;
} else if (overflowActive) {
view.waterMaterial.color.copy(normalWater);
view.waterMaterial.emissive.setHex(view.tank.color);
view.surfaceMaterial.color.copy(normalWater);
view.surfaceMaterial.emissive.setHex(view.tank.color);
view.statusRingMaterial.color.setHex(0xffc267);
view.statusRingMaterial.opacity = 0.95;
view.beacon.material.color.setHex(0xffc267);
view.beacon.material.emissive.setHex(0x8b5a0e);
view.beacon.visible = true;
} else {
view.waterMaterial.color.copy(normalWater);
view.waterMaterial.emissive.setHex(view.tank.color);
view.surfaceMaterial.color.copy(normalWater);
view.surfaceMaterial.emissive.setHex(view.tank.color);
view.statusRingMaterial.color.setHex(view.tank.color);
view.statusRingMaterial.opacity = 0.16;
view.beacon.material.color.setHex(0x4f747c);
view.beacon.material.emissive.setHex(0x000000);
view.beacon.visible = false;
}
// Overflow particles fall from the tank rim.
view.overflowParticles.visible = overflowActive;
if (overflowActive) {
for (let i = 0; i < view.overflowCount; i++) {
const phase = (elapsed * 0.95 + i / view.overflowCount) % 1;
const angle = i * 2.39996;
const radial = view.radius + 0.03 + phase * 0.18;
view.overflowPositions[i * 3] =
view.x + Math.cos(angle) * radial;
view.overflowPositions[i * 3 + 1] =
view.baseY + view.height + 0.04 - phase * view.height;
view.overflowPositions[i * 3 + 2] =
view.z + Math.sin(angle) * radial;
}
view.overflowGeometry.attributes.position.needsUpdate = true;
}
// Project the HTML tank label above each tank.
projected.set(
view.x,
view.baseY + view.height + 0.56,
view.z
).project(camera);
const visible = projected.z > -1 && projected.z < 1;
view.label.style.display = visible ? "block" : "none";
if (visible) {
const x = (projected.x * 0.5 + 0.5) * sceneCanvas.clientWidth;
const y = (-projected.y * 0.5 + 0.5) * sceneCanvas.clientHeight;
view.label.style.transform =
`translate(${x}px, ${y}px) translate(-50%, -100%)`;
view.labelBar.style.width = `${fill * 100}%`;
let statusText = "BALANCED";
let statusColor = "#78959a";
if (shortageActive) {
statusText = `SHORT ${Math.round(result.shortage)} L`;
statusColor = "#ff6f7d";
} else if (overflowActive) {
statusText = `OVER ${Math.round(result.overflow)} L`;
statusColor = "#ffc267";
}
view.labelState.textContent = statusText;
view.labelState.style.color = statusColor;
view.labelDetail.textContent =
`${Math.round(fill * 100)}% · ${Math.round(storage).toLocaleString()} L`;
}
});
}
function updateInterface(dayIndex) {
const result = simulations.map(run => run[dayIndex]);
rainReadout.textContent = `${RAIN[dayIndex]} mm/day`;
runoffReadout.textContent = `${Math.round(RAIN[dayIndex] * ROOF_AREA * RUNOFF)} L`;
timeline.value = dayFloat;
timelineValue.textContent = `DAY ${String(dayIndex + 1).padStart(2, "0")} / ${DAYS}`;
result.forEach((state, index) => {
const row = tableRows[index];
row.overflow.textContent = formatLitres(state.cumulativeOverflow);
row.overflow.title = `${Math.round(state.cumulativeOverflow).toLocaleString()} litres`;
row.overflowDays.textContent = `${state.overflowDays} day${state.overflowDays === 1 ? "" : "s"}`;
row.shortage.textContent = formatLitres(state.cumulativeShortage);
row.shortage.title = `${Math.round(state.cumulativeShortage).toLocaleString()} litres`;
row.shortageDays.textContent = `${state.shortageDays} day${state.shortageDays === 1 ? "" : "s"}`;
});
}
// ------------------------------------------------------------
// Rain animation
// ------------------------------------------------------------
function updateRain(dt, rain) {
const active = rain > 0;
rainLines.visible = active;
if (!active) return;
rainMaterial.opacity = 0.13 + rain / MAX_RAIN * 0.46;
for (let i = 0; i < RAIN_COUNT; i++) {
const drop = rainDrops[i];
drop.y -= dt * (drop.speed + rain * 0.1);
if (drop.y < 0.05) {
drop.y = 5.5 + Math.random() * 1.2;
drop.x = Math.random() * 12 - 6;
drop.z = Math.random() * 6 - 3.3;
}
const offset = i * 6;
rainPositions[offset] = drop.x;
rainPositions[offset + 1] = drop.y;
rainPositions[offset + 2] = drop.z;
rainPositions[offset + 3] = drop.x;
rainPositions[offset + 4] = drop.y + 0.18 + rain * 0.004;
rainPositions[offset + 5] = drop.z;
}
rainGeometry.attributes.position.needsUpdate = true;
cloudMaterial.color.setHex(rain > 35 ? 0x1d303a : 0x263d48);
}
// ------------------------------------------------------------
// Comparison chart
// ------------------------------------------------------------
const chartCanvas = document.getElementById("chart");
const chartContext = chartCanvas.getContext("2d");
let chartWidth = 1;
let chartHeight = 1;
let hoverDay = null;
function resizeChart() {
const rect = chartCanvas.getBoundingClientRect();
const ratio = Math.min(window.devicePixelRatio, 2);
chartWidth = Math.max(1, rect.width);
chartHeight = Math.max(1, rect.height);
chartCanvas.width = Math.round(chartWidth * ratio);
chartCanvas.height = Math.round(chartHeight * ratio);
chartContext.setTransform(ratio, 0, 0, ratio, 0, 0);
}
function drawChart(dayIndex) {
const ctx = chartContext;
const padding = { left: 39, right: 13, top: 20, bottom: 25 };
const plotWidth = chartWidth - padding.left - padding.right;
const plotHeight = chartHeight - padding.top - padding.bottom;
const yMax = 2000;
const xForDay = day => padding.left + day / (DAYS - 1) * plotWidth;
const yForLitres = litres =>
padding.top + plotHeight - litres / yMax * plotHeight;
ctx.clearRect(0, 0, chartWidth, chartHeight);
// Rainstorm bands.
for (let day = 0; day < DAYS; day++) {
if (!RAIN[day]) continue;
const x = xForDay(day);
const width = Math.max(1, plotWidth / (DAYS - 1));
const stormHeight = RAIN[day] / MAX_RAIN * plotHeight * 0.22;
ctx.fillStyle = RAIN[day] >= 40
? "rgba(101,185,255,0.13)"
: "rgba(101,185,255,0.07)";
ctx.fillRect(
x - width * 0.35,
padding.top,
width * 0.7,
stormHeight
);
}
// Horizontal grid.
ctx.font = "9px ui-monospace, SFMono-Regular, Menlo, monospace";
ctx.textAlign = "right";
ctx.textBaseline = "middle";
for (let litres = 0; litres <= yMax; litres += 500) {
const y = yForLitres(litres);
ctx.beginPath();
ctx.strokeStyle = "rgba(157,196,204,0.10)";
ctx.lineWidth = 1;
ctx.moveTo(padding.left, y);
ctx.lineTo(chartWidth - padding.right, y);
ctx.stroke();
ctx.fillStyle = "#627b81";
ctx.fillText(litres === 0 ? "0" : `${litres / 1000}k`, padding.left - 8, y);
}
ctx.fillStyle = "#627b81";
ctx.fillText("L", 12, padding.top - 9);
// X-axis labels.
ctx.textBaseline = "bottom";
ctx.textAlign = "center";
[0, 10, 20, 30, 40, DAYS - 1].forEach(day => {
ctx.fillText(
`D${day + 1}`,
xForDay(day),
chartHeight - 5
);
});
// Capacity guides.
TANKS.forEach((tank, index) => {
const y = yForLitres(tank.capacity);
ctx.beginPath();
ctx.setLineDash([4, 5]);
ctx.strokeStyle = `${tank.css}38`;
ctx.lineWidth = 1;
ctx.moveTo(padding.left, y);
ctx.lineTo(chartWidth - padding.right, y);
ctx.stroke();
ctx.setLineDash([]);
// Storage line.
ctx.beginPath();
simulations[index].forEach((state, day) => {
const x = xForDay(day);
const yStorage = yForLitres(state.storage);
if (day === 0) ctx.moveTo(x, yStorage);
else ctx.lineTo(x, yStorage);
});
ctx.strokeStyle = tank.css;
ctx.lineWidth = 1.8;
ctx.stroke();
// Event markers.
simulations[index].forEach((state, day) => {
const x = xForDay(day);
const yStorage = yForLitres(state.storage);
if (state.overflow > 0.01) {
ctx.beginPath();
ctx.arc(x, yStorage, 3.1, 0, Math.PI * 2);
ctx.fillStyle = "#071116";
ctx.fill();
ctx.strokeStyle = "#ffc267";
ctx.lineWidth = 1.5;
ctx.stroke();
} else if (state.shortage > 0.01) {
ctx.beginPath();
ctx.moveTo(x, yStorage - 5);
ctx.lineTo(x - 4, yStorage + 2);
ctx.lineTo(x + 4, yStorage + 2);
ctx.closePath();
ctx.fillStyle = "#ff6f7d";
ctx.fill();
}
});
});
// Current time cursor.
const currentX = padding.left + dayFloat / (DAYS - 1) * plotWidth;
ctx.beginPath();
ctx.strokeStyle = "rgba(230,245,246,0.72)";
ctx.lineWidth = 1;
ctx.moveTo(currentX, padding.top);
ctx.lineTo(currentX, padding.top + plotHeight);
ctx.stroke();
ctx.beginPath();
ctx.arc(currentX, padding.top - 8, 3.3, 0, Math.PI * 2);
ctx.fillStyle = "#e8f3f2";
ctx.fill();
// Hover crosshair and tooltip.
if (hoverDay !== null) {
const hoverX = xForDay(hoverDay);
ctx.beginPath();
ctx.strokeStyle = "rgba(230,245,246,0.22)";
ctx.setLineDash([3, 4]);
ctx.moveTo(hoverX, padding.top);
ctx.lineTo(hoverX, padding.top + plotHeight);
ctx.stroke();
ctx.setLineDash([]);
const values = simulations.map(run => run[hoverDay].storage);
const lines = [
`Day ${hoverDay + 1}`,
...TANKS.map((tank, index) =>
`${tank.capacity} L ${Math.round(values[index]).toLocaleString()} L`
)
];
ctx.font = "10px ui-monospace, SFMono-Regular, Menlo, monospace";
const boxWidth = 150;
const boxHeight = 80;
const boxX = hoverX + boxWidth + 16 > chartWidth
? hoverX - boxWidth - 10
: hoverX + 10;
const boxY = padding.top + 3;
ctx.fillStyle = "rgba(7,17,22,0.94)";
ctx.strokeStyle = "rgba(170,210,217,0.23)";
ctx.lineWidth = 1;
ctx.beginPath();
ctx.roundRect(boxX, boxY, boxWidth, boxHeight, 7);
ctx.fill();
ctx.stroke();
ctx.textAlign = "left";
ctx.textBaseline = "top";
lines.forEach((text, index) => {
ctx.fillStyle = index === 0 ? "#e8f3f2" : TANKS[index - 1].css;
ctx.fillText(text, boxX + 10, boxY + 8 + index * 15);
});
}
}
chartCanvas.addEventListener("pointermove", event => {
const rect = chartCanvas.getBoundingClientRect();
const x = event.clientX - rect.left;
const plotWidth = chartWidth - 52;
const ratio = THREE.MathUtils.clamp((x - 39) / plotWidth, 0, 1);
hoverDay = Math.round(ratio * (DAYS - 1));
});
chartCanvas.addEventListener("pointerleave", () => {
hoverDay = null;
});
// ------------------------------------------------------------
// Resize and animation loop
// ------------------------------------------------------------
function resizeScene() {
const width = sceneCanvas.clientWidth;
const height = sceneCanvas.clientHeight;
renderer.setSize(width, height, false);
camera.aspect = width / Math.max(1, height);
camera.updateProjectionMatrix();
}
const sceneResizeObserver = new ResizeObserver(resizeScene);
sceneResizeObserver.observe(sceneCanvas);
const chartResizeObserver = new ResizeObserver(resizeChart);
chartResizeObserver.observe(chartCanvas);
resizeScene();
resizeChart();
updateCamera();
updatePlayButton();
let lastTime = performance.now();
const clock = new THREE.Clock();
function animate(now) {
requestAnimationFrame(animate);
const dt = THREE.MathUtils.clamp((now - lastTime) / 1000, 0, 0.05);
lastTime = now;
if (playing) {
dayFloat += dt * speed;
if (dayFloat >= DAYS) {
dayFloat = DAYS;
playing = false;
updatePlayButton();
}
}
const dayIndex = Math.min(DAYS - 1, Math.floor(dayFloat));
const nextIndex = Math.min(DAYS - 1, dayIndex + 1);
const fraction = THREE.MathUtils.clamp(dayFloat - dayIndex, 0, 1);
updateCamera();
updateTankViews(dayIndex, fraction, clock.getElapsedTime());
updateInterface(dayIndex);
const visualDt = playing ? dt : 0;
updateRain(visualDt, RAIN[dayIndex]);
cloud.position.x = Math.sin(clock.getElapsedTime() * 0.14) * 0.08;
cloud.position.y = 4.55 + Math.sin(clock.getElapsedTime() * 0.21) * 0.035;
drawChart(dayIndex);
renderer.render(scene, camera);
}
requestAnimationFrame(animate);
document.getElementById("loading").style.display = "none";
</script>
</body>
</html>
```