Prompt specification
Build or customize the Three.js 3D scene "One Pipe Closure With Reserve Drawdown" 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: One Pipe Closure With Reserve Drawdown
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/one-pipe-closure-with-reserve-drawdown-x3962
Scene Description Prompt:
Please use Three.js from cdnjs for a voxel scene based on this idea: one pipe closure with reserve drawdown. Animate a toy supply interruption while a local reserve serves demand. Track remaining storage and show service ending if no alternate supply arrives in time. Return one self-contained HTML file.
Working Three.js Starter (one-pipe-closure-with-reserve-drawdown-x3962):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta name="theme-color" content="#08151d" />
<title>One Pipe. One Reserve.</title>
<style>
:root {
--bg: #08151d;
--panel: rgba(10, 25, 33, 0.92);
--line: rgba(180, 225, 235, 0.14);
--text: #eef8f8;
--muted: #8ba6ad;
--cyan: #53d9f4;
--green: #66e3a6;
--amber: #ffbd62;
--red: #ff6868;
--orange: #ff984d;
--accent: var(--cyan);
}
* { 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;
}
body {
background:
radial-gradient(circle at 28% 18%, rgba(36, 92, 102, 0.18), transparent 35%),
var(--bg);
}
canvas {
position: fixed;
inset: 0;
width: 100%;
height: 100%;
display: block;
touch-action: none;
cursor: grab;
}
canvas:active { cursor: grabbing; }
#vignette {
position: fixed;
inset: 0;
z-index: 2;
pointer-events: none;
box-shadow: inset 0 0 130px rgba(0, 0, 0, 0.46);
transition: box-shadow 0.5s ease;
}
body.urgent #vignette {
box-shadow: inset 0 0 150px rgba(255, 70, 70, 0.28);
}
body.is-ended #vignette {
box-shadow: inset 0 0 170px rgba(255, 45, 45, 0.42);
}
#labels {
position: fixed;
inset: 0;
z-index: 3;
pointer-events: none;
overflow: hidden;
}
.scene-label {
position: absolute;
transform: translate(-50%, -100%);
padding: 6px 9px;
border: 1px solid rgba(142, 221, 235, 0.25);
border-radius: 4px;
background: rgba(5, 17, 23, 0.82);
color: #cfe5e8;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.1em;
white-space: nowrap;
box-shadow: 0 5px 20px rgba(0, 0, 0, 0.2);
transition: color 0.2s, border-color 0.2s, background 0.2s;
}
.scene-label::after {
content: "";
position: absolute;
left: 50%;
top: 100%;
width: 1px;
height: 14px;
background: rgba(142, 221, 235, 0.4);
}
.scene-label.danger {
color: #ff9b9b;
border-color: rgba(255, 104, 104, 0.5);
background: rgba(46, 12, 15, 0.9);
}
.scene-label.warning {
color: #ffd18a;
border-color: rgba(255, 189, 98, 0.45);
}
.scene-label.safe {
color: #91f4be;
border-color: rgba(102, 227, 166, 0.45);
}
#intro {
position: fixed;
z-index: 5;
top: 26px;
left: 28px;
width: min(470px, calc(100vw - 440px));
pointer-events: none;
text-shadow: 0 2px 18px rgba(0, 0, 0, 0.65);
}
.eyebrow {
display: flex;
align-items: center;
gap: 9px;
margin-bottom: 9px;
color: #91b6bd;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.18em;
text-transform: uppercase;
}
.eyebrow::before {
content: "";
width: 22px;
height: 2px;
background: var(--cyan);
box-shadow: 0 0 12px rgba(83, 217, 244, 0.65);
}
#intro h1 {
margin: 0;
font-size: clamp(28px, 3.1vw, 47px);
line-height: 0.98;
letter-spacing: -0.055em;
text-transform: uppercase;
}
#intro p {
max-width: 430px;
margin: 13px 0 0;
color: #9db4b9;
font-size: 12px;
line-height: 1.55;
}
#cameraHint {
position: fixed;
z-index: 5;
left: 28px;
bottom: 25px;
color: rgba(179, 207, 211, 0.55);
font-size: 10px;
font-weight: 700;
letter-spacing: 0.09em;
text-transform: uppercase;
pointer-events: none;
}
#panel {
position: fixed;
z-index: 10;
top: 18px;
right: 18px;
width: 350px;
max-height: calc(100vh - 36px);
padding: 17px;
overflow: auto;
border: 1px solid var(--line);
border-radius: 12px;
background: var(--panel);
box-shadow: 0 24px 70px rgba(0, 0, 0, 0.38);
backdrop-filter: blur(18px);
scrollbar-width: thin;
scrollbar-color: rgba(120, 175, 185, 0.25) transparent;
}
.panel-top {
display: flex;
justify-content: space-between;
align-items: center;
gap: 12px;
margin-bottom: 14px;
}
.panel-kicker {
color: #76949b;
font-size: 9px;
font-weight: 800;
letter-spacing: 0.16em;
text-transform: uppercase;
}
.mode-switch {
display: flex;
padding: 3px;
border: 1px solid rgba(174, 219, 225, 0.13);
border-radius: 7px;
background: rgba(0, 0, 0, 0.18);
}
.mode-switch button {
border: 0;
border-radius: 5px;
padding: 6px 8px;
background: transparent;
color: #78949b;
font: inherit;
font-size: 9px;
font-weight: 800;
cursor: pointer;
}
.mode-switch button.active {
background: rgba(83, 217, 244, 0.13);
color: #aeeefa;
}
#statusCard {
position: relative;
overflow: hidden;
padding: 15px;
border: 1px solid rgba(83, 217, 244, 0.23);
border-radius: 9px;
background:
linear-gradient(120deg, rgba(83, 217, 244, 0.09), transparent 65%),
rgba(255, 255, 255, 0.025);
transition: border-color 0.25s, background 0.25s;
}
#statusCard::before {
content: "";
position: absolute;
inset: 0 auto 0 0;
width: 3px;
background: var(--accent);
box-shadow: 0 0 16px var(--accent);
}
.status-row {
display: flex;
align-items: center;
gap: 9px;
}
#statusDot {
width: 8px;
height: 8px;
flex: 0 0 auto;
border-radius: 50%;
background: var(--accent);
box-shadow: 0 0 13px var(--accent);
}
#statusTitle {
margin: 0;
font-size: 17px;
letter-spacing: -0.025em;
}
#statusDetail {
min-height: 34px;
margin: 7px 0 0 17px;
color: #8fa9af;
font-size: 11px;
line-height: 1.45;
}
.section-label {
margin: 17px 0 9px;
color: #6f8b92;
font-size: 9px;
font-weight: 800;
letter-spacing: 0.16em;
text-transform: uppercase;
}
.storage-row {
display: flex;
align-items: flex-end;
justify-content: space-between;
gap: 14px;
}
#storageValue {
font-size: 38px;
font-weight: 760;
line-height: 0.9;
letter-spacing: -0.06em;
font-variant-numeric: tabular-nums;
}
#storageUnit {
margin-left: 4px;
color: #749198;
font-size: 12px;
font-weight: 700;
letter-spacing: 0;
}
#runwayValue {
padding-bottom: 2px;
color: var(--accent);
font-size: 14px;
font-weight: 800;
font-variant-numeric: tabular-nums;
}
.meter {
position: relative;
height: 11px;
margin-top: 12px;
overflow: visible;
border-radius: 3px;
background: rgba(255, 255, 255, 0.07);
box-shadow: inset 0 1px 2px rgba(0, 0, 0, 0.25);
}
#meterFill {
width: 100%;
height: 100%;
border-radius: 3px;
background: var(--accent);
box-shadow: 0 0 14px color-mix(in srgb, var(--accent) 60%, transparent);
transition: width 0.08s linear, background 0.25s;
}
#floorMark {
position: absolute;
left: 15%;
top: -4px;
width: 1px;
height: 19px;
background: rgba(255, 255, 255, 0.65);
opacity: 0.75;
}
#floorMark::after {
content: "FLOOR";
position: absolute;
top: 18px;
left: -13px;
color: #6f858a;
font-size: 7px;
font-weight: 800;
letter-spacing: 0.08em;
}
.meter-caption {
display: flex;
justify-content: space-between;
margin-top: 9px;
color: #69848b;
font-size: 9px;
}
.metrics {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 7px;
}
.metric {
min-height: 60px;
padding: 10px;
border: 1px solid rgba(174, 219, 225, 0.1);
border-radius: 7px;
background: rgba(255, 255, 255, 0.025);
}
.metric span {
display: block;
margin-bottom: 7px;
color: #66838a;
font-size: 8px;
font-weight: 800;
letter-spacing: 0.12em;
text-transform: uppercase;
}
.metric strong {
display: block;
overflow: hidden;
color: #c9dcdf;
font-size: 11px;
font-weight: 800;
text-overflow: ellipsis;
white-space: nowrap;
}
.metric strong.good { color: var(--green); }
.metric strong.warn { color: var(--amber); }
.metric strong.bad { color: var(--red); }
.metric strong.info { color: var(--cyan); }
.actions {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 7px;
margin-top: 13px;
}
button.action {
min-height: 40px;
padding: 9px 10px;
border: 1px solid rgba(174, 219, 225, 0.16);
border-radius: 7px;
background: rgba(255, 255, 255, 0.045);
color: #c9dcdf;
font: inherit;
font-size: 10px;
font-weight: 800;
cursor: pointer;
transition: transform 0.15s, background 0.15s, border-color 0.15s, opacity 0.15s;
}
button.action:hover:not(:disabled) {
transform: translateY(-1px);
border-color: rgba(83, 217, 244, 0.42);
background: rgba(83, 217, 244, 0.09);
}
button.action.primary {
border-color: rgba(83, 217, 244, 0.32);
background: rgba(83, 217, 244, 0.11);
color: #bceffd;
}
button.action.urgent {
border-color: rgba(255, 189, 98, 0.5);
background: rgba(255, 189, 98, 0.1);
color: #ffd899;
}
button.action:disabled {
opacity: 0.32;
cursor: not-allowed;
}
#resetButton {
grid-column: 1 / -1;
min-height: 31px;
padding: 6px;
color: #78939a;
font-size: 9px;
}
#eventFeed {
display: flex;
flex-direction: column;
gap: 6px;
margin: 0;
padding: 0;
list-style: none;
}
#eventFeed li {
display: grid;
grid-template-columns: 37px 1fr;
gap: 8px;
color: #89a1a7;
font-size: 9px;
line-height: 1.35;
}
#eventFeed time {
color: #58747b;
font-size: 8px;
font-weight: 800;
font-variant-numeric: tabular-nums;
}
#eventFeed li.warn { color: #e7bd85; }
#eventFeed li.good { color: #96dfb8; }
#eventFeed li.bad { color: #ff9999; }
#timeline {
position: fixed;
z-index: 6;
left: 28px;
right: 388px;
bottom: 54px;
pointer-events: none;
}
.timeline-track {
position: relative;
height: 3px;
margin: 0 8px 9px;
border-radius: 3px;
background: rgba(255, 255, 255, 0.1);
overflow: hidden;
}
#timelineFill {
width: 0;
height: 100%;
background: linear-gradient(90deg, var(--cyan), var(--amber));
box-shadow: 0 0 12px rgba(83, 217, 244, 0.45);
}
.timeline-stops {
display: flex;
justify-content: space-between;
color: #57737a;
font-size: 8px;
font-weight: 800;
letter-spacing: 0.1em;
text-transform: uppercase;
}
.timeline-stops span.active { color: #c7e5e9; }
.timeline-stops span.bad { color: var(--red); }
.timeline-stops span.good { color: var(--green); }
#endBanner {
position: fixed;
z-index: 8;
left: calc((100vw - 370px) / 2);
top: 53%;
transform: translate(-50%, -50%) scale(0.94);
width: min(460px, calc(100vw - 420px));
padding: 18px 22px;
border: 1px solid rgba(255, 104, 104, 0.5);
border-radius: 9px;
background: rgba(43, 10, 13, 0.92);
box-shadow: 0 20px 70px rgba(0, 0, 0, 0.45), 0 0 40px rgba(255, 70, 70, 0.12);
opacity: 0;
pointer-events: none;
transition: opacity 0.4s, transform 0.4s;
text-align: center;
}
#endBanner strong {
display: block;
margin-bottom: 6px;
color: #ff8585;
font-size: 18px;
letter-spacing: 0.12em;
text-transform: uppercase;
}
#endBanner span {
color: #d2a4a4;
font-size: 10px;
line-height: 1.45;
}
body.is-ended #endBanner {
opacity: 1;
transform: translate(-50%, -50%) scale(1);
}
@media (max-width: 900px) {
#intro {
top: 18px;
left: 18px;
width: calc(100vw - 36px);
}
#intro h1 { font-size: 29px; }
#intro p { display: none; }
#panel {
top: auto;
right: 12px;
bottom: 12px;
left: 12px;
width: auto;
max-height: 47vh;
padding: 13px;
}
#timeline, #cameraHint, .scene-label { display: none; }
#endBanner {
left: 50%;
top: 28%;
width: calc(100vw - 36px);
}
}
@media (max-width: 470px) {
.panel-top {
align-items: flex-start;
flex-direction: column;
}
.mode-switch {
width: 100%;
}
.mode-switch button { flex: 1; }
.metrics { grid-template-columns: 1fr 1fr; }
#statusDetail { min-height: 0; }
.section-label { margin-top: 13px; }
}
</style>
</head>
<body>
<canvas id="scene" aria-label="Interactive voxel water network showing a main pipe closure and local reserve drawdown"></canvas>
<div id="vignette"></div>
<div id="labels" aria-hidden="true"></div>
<header id="intro">
<div class="eyebrow">Water resilience / toy scenario 01</div>
<h1>One pipe.<br />One reserve.</h1>
<p>
A main-line closure interrupts the neighborhood supply. Watch local storage carry demand,
then dispatch an alternate supply before the usable reserve runs out.
</p>
</header>
<aside id="panel">
<div class="panel-top">
<div class="panel-kicker">Scenario controls</div>
<div class="mode-switch" aria-label="Scenario mode">
<button type="button" data-mode="no-rescue" class="active">Closure only</button>
<button type="button" data-mode="rescue">Rescue on time</button>
</div>
</div>
<section id="statusCard" aria-live="polite">
<div class="status-row">
<div id="statusDot"></div>
<h2 id="statusTitle">Preparing interruption</h2>
</div>
<p id="statusDetail">The main line is healthy while the closure sequence arms.</p>
</section>
<div class="section-label">Local reserve</div>
<div class="storage-row">
<div><span id="storageValue">100</span><span id="storageUnit">% remaining</span></div>
<div id="runwayValue">20.2 s runway</div>
</div>
<div class="meter" aria-label="Remaining reserve storage">
<div id="meterFill"></div>
<div id="floorMark"></div>
</div>
<div class="meter-caption">
<span>Capacity</span>
<span>15% operating floor</span>
</div>
<div class="section-label">Network state</div>
<div class="metrics">
<div class="metric">
<span>Main pipe</span>
<strong id="mainState" class="good">FLOWING</strong>
</div>
<div class="metric">
<span>Demand service</span>
<strong id="demandState" class="good">SERVED</strong>
</div>
<div class="metric">
<span>Alternate</span>
<strong id="alternateState">STANDBY</strong>
</div>
<div class="metric">
<span>Decision point</span>
<strong id="decisionState">20.2 SECONDS</strong>
</div>
</div>
<div class="actions">
<button type="button" id="interruptButton" class="action primary">Close main pipe</button>
<button type="button" id="alternateButton" class="action">Dispatch alternate</button>
<button type="button" id="resetButton" class="action">Reset scenario</button>
</div>
<div class="section-label">Event log</div>
<ul id="eventFeed"></ul>
</aside>
<div id="timeline" aria-hidden="true">
<div class="timeline-track"><div id="timelineFill"></div></div>
<div class="timeline-stops">
<span id="stopNormal">Normal</span>
<span id="stopClosure">Closure</span>
<span id="stopReserve">Reserve drawdown</span>
<span id="stopAlternate">Alternate</span>
<span id="stopEnd">Service end</span>
</div>
</div>
<div id="cameraHint">Drag to orbit · Scroll to zoom · Double-click to reset view</div>
<div id="endBanner" role="status">
<strong>Service ended</strong>
<span id="endBannerText">Usable reserve was exhausted before alternate supply arrived.</span>
</div>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// -------------------------------------------------------------------------
// DOM and scenario state
// -------------------------------------------------------------------------
const canvas = document.querySelector("#scene");
const labelsRoot = document.querySelector("#labels");
const statusCard = document.querySelector("#statusCard");
const statusTitle = document.querySelector("#statusTitle");
const statusDetail = document.querySelector("#statusDetail");
const storageValue = document.querySelector("#storageValue");
const runwayValue = document.querySelector("#runwayValue");
const meterFill = document.querySelector("#meterFill");
const mainState = document.querySelector("#mainState");
const demandState = document.querySelector("#demandState");
const alternateState = document.querySelector("#alternateState");
const decisionState = document.querySelector("#decisionState");
const interruptButton = document.querySelector("#interruptButton");
const alternateButton = document.querySelector("#alternateButton");
const resetButton = document.querySelector("#resetButton");
const eventFeed = document.querySelector("#eventFeed");
const timelineFill = document.querySelector("#timelineFill");
const endBannerText = document.querySelector("#endBannerText");
const modeButtons = [...document.querySelectorAll("[data-mode]")];
const STOP_NORMAL = document.querySelector("#stopNormal");
const STOP_CLOSURE = document.querySelector("#stopClosure");
const STOP_RESERVE = document.querySelector("#stopReserve");
const STOP_ALTERNATE = document.querySelector("#stopAlternate");
const STOP_END = document.querySelector("#stopEnd");
const RESERVE_FLOOR = 15;
const DRAWDOWN_RATE = 4.2; // percentage points per simulated second
const ALTERNATE_TRAVEL = 4.5; // tanker travel time in seconds
const FULL_RUNWAY = (100 - RESERVE_FLOOR) / DRAWDOWN_RATE;
let scenarioMode = "no-rescue";
let phase = "normal";
let storage = 100;
let simTime = 0;
let outageElapsed = 0;
let alternateRequested = false;
let alternateArrived = false;
let alternateProgress = 0;
let alternateWasLate = false;
let visualTime = 0;
function addEvent(message, tone = "") {
const item = document.createElement("li");
if (tone) item.className = tone;
const time = document.createElement("time");
time.textContent = `T+${simTime.toFixed(1)}`;
const text = document.createElement("span");
text.textContent = message;
item.append(time, text);
eventFeed.prepend(item);
while (eventFeed.children.length > 5) {
eventFeed.lastElementChild.remove();
}
}
function setStateClass(element, className) {
element.className = "";
if (className) element.classList.add(className);
}
function currentRunway() {
if (phase === "outage") return Math.max(0, (storage - RESERVE_FLOOR) / DRAWDOWN_RATE);
if (phase === "normal") return FULL_RUNWAY;
return 0;
}
// -------------------------------------------------------------------------
// Three.js scene
// -------------------------------------------------------------------------
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x08151d);
scene.fog = new THREE.Fog(0x08151d, 29, 53);
const renderer = new THREE.WebGLRenderer({
canvas,
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.16;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
const camera = new THREE.PerspectiveCamera(42, window.innerWidth / window.innerHeight, 0.1, 100);
const cameraTarget = new THREE.Vector3(-0.5, 1.0, 0.2);
let cameraYaw = 0.58;
let cameraPolar = 0.88;
let cameraRadius = 30;
function updateCamera() {
const horizontal = cameraRadius * Math.sin(cameraPolar);
camera.position.set(
cameraTarget.x + horizontal * Math.sin(cameraYaw),
cameraTarget.y + cameraRadius * Math.cos(cameraPolar),
cameraTarget.z + horizontal * Math.cos(cameraYaw)
);
camera.lookAt(cameraTarget);
}
function resetCamera() {
cameraYaw = 0.58;
cameraPolar = 0.88;
cameraRadius = 30;
}
updateCamera();
const hemi = new THREE.HemisphereLight(0xb8e9f2, 0x18251f, 2.0);
scene.add(hemi);
const sun = new THREE.DirectionalLight(0xfff0d5, 3.1);
sun.position.set(-9, 18, 11);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -19;
sun.shadow.camera.right = 19;
sun.shadow.camera.top = 15;
sun.shadow.camera.bottom = -15;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 45;
sun.shadow.bias = -0.0003;
scene.add(sun);
const rim = new THREE.DirectionalLight(0x4fc9ef, 1.2);
rim.position.set(12, 8, -12);
scene.add(rim);
const world = new THREE.Group();
scene.add(world);
const materials = {
groundBase: new THREE.MeshStandardMaterial({ color: 0x10272b, roughness: 1 }),
pipe: new THREE.MeshStandardMaterial({ color: 0x55717a, roughness: 0.58, metalness: 0.28 }),
pipeJoint: new THREE.MeshStandardMaterial({ color: 0x78939a, roughness: 0.45, metalness: 0.35 }),
inactivePipe: new THREE.MeshStandardMaterial({
color: 0x364b50, roughness: 0.72, metalness: 0.12,
emissive: 0x000000
}),
darkMetal: new THREE.MeshStandardMaterial({ color: 0x263a40, roughness: 0.62, metalness: 0.34 }),
red: new THREE.MeshStandardMaterial({
color: 0xef5353, roughness: 0.4, metalness: 0.2,
emissive: 0x6d1010, emissiveIntensity: 0.32
}),
water: new THREE.MeshStandardMaterial({
color: 0x43c8e8, roughness: 0.25, metalness: 0.1,
emissive: 0x0e718a, emissiveIntensity: 0.6
}),
brightWater: new THREE.MeshBasicMaterial({ color: 0x76ecff }),
orange: new THREE.MeshStandardMaterial({
color: 0xf39743, roughness: 0.5, metalness: 0.16,
emissive: 0x4b1e03, emissiveIntensity: 0.2
}),
truckBody: new THREE.MeshStandardMaterial({ color: 0xd98939, roughness: 0.52, metalness: 0.18 }),
glass: new THREE.MeshStandardMaterial({
color: 0x8bd8ed, roughness: 0.18, metalness: 0.02,
transparent: true, opacity: 0.13, depthWrite: false
}),
tankWater: new THREE.MeshStandardMaterial({
color: 0x29a9d8, roughness: 0.22, metalness: 0.08,
transparent: true, opacity: 0.76,
emissive: 0x075b74, emissiveIntensity: 0.48
}),
building: new THREE.MeshStandardMaterial({ color: 0x354b53, roughness: 0.9 }),
buildingLight: new THREE.MeshStandardMaterial({ color: 0x657a80, roughness: 0.85 }),
roof: new THREE.MeshStandardMaterial({ color: 0x22363e, roughness: 0.82 }),
window: new THREE.MeshStandardMaterial({
color: 0xffd68a, emissive: 0xffaa3d, emissiveIntensity: 1.7,
roughness: 0.32
}),
rubber: new THREE.MeshStandardMaterial({ color: 0x11191d, roughness: 0.94 }),
white: new THREE.MeshStandardMaterial({ color: 0xc4d6d7, roughness: 0.64 })
};
function makeBox(width, height, depth, material, x, y, z, parent = world) {
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 makeAxisPipe(x1, z1, x2, z2, material = materials.pipe, height = 0.34, width = 0.46) {
const dx = Math.abs(x2 - x1);
const dz = Math.abs(z2 - z1);
const length = dx || dz;
const centerX = (x1 + x2) / 2;
const centerZ = (z1 + z2) / 2;
return makeBox(
dx > 0 ? length : width,
height,
dz > 0 ? length : width,
material,
centerX,
0.52,
centerZ
);
}
// Base and voxel tiles.
makeBox(28.6, 0.8, 18.6, materials.groundBase, 0, -0.48, 0);
const tileGeometry = new THREE.BoxGeometry(1.92, 0.14, 1.92);
const tileMaterial = new THREE.MeshStandardMaterial({ color: 0xffffff, roughness: 1 });
const tileCount = 14 * 9;
const tiles = new THREE.InstancedMesh(tileGeometry, tileMaterial, tileCount);
const tileDummy = new THREE.Object3D();
const tileColor = new THREE.Color();
let tileIndex = 0;
for (let ix = 0; ix < 14; ix++) {
for (let iz = 0; iz < 9; iz++) {
tileDummy.position.set(-13 + ix * 2, -0.005, -8 + iz * 2);
tileDummy.updateMatrix();
tiles.setMatrixAt(tileIndex, tileDummy.matrix);
const variation = ((ix * 17 + iz * 11) % 7) * 0.006;
tileColor.setHSL(0.47, 0.19, 0.145 + variation);
tiles.setColorAt(tileIndex, tileColor);
tileIndex++;
}
}
tiles.receiveShadow = true;
world.add(tiles);
// Main pipeline.
makeAxisPipe(-10.6, 1.3, 9.6, 1.3, materials.pipe, 0.38, 0.52);
for (let x = -10; x <= 8; x += 2) {
makeBox(0.18, 0.54, 0.7, materials.pipeJoint, x, 0.53, 1.3);
}
makeBox(19.7, 0.045, 0.18, materials.water, -0.5, 0.735, 1.3);
// Reserve branch and alternate route.
makeAxisPipe(4, -1.65, 4, 1.3, materials.pipe, 0.31, 0.43);
makeAxisPipe(4, 1.3, 9.6, 1.3, materials.pipe, 0.31, 0.43);
const alternatePipeMaterial = materials.inactivePipe.clone();
makeAxisPipe(0, -5.4, 8.8, -5.4, alternatePipeMaterial, 0.29, 0.4);
makeAxisPipe(8.8, -5.4, 8.8, 1.3, alternatePipeMaterial, 0.29, 0.4);
// Pumping station.
makeBox(2.8, 0.25, 2.8, materials.darkMetal, -10.2, 0.18, 1.3);
makeBox(2.25, 2.3, 2.35, materials.building, -10.2, 1.45, 1.3);
makeBox(2.55, 0.28, 2.65, materials.roof, -10.2, 2.72, 1.3);
makeBox(0.55, 1.25, 0.55, materials.pipeJoint, -10.85, 3.42, 1.05);
makeBox(0.78, 0.17, 0.78, materials.darkMetal, -10.85, 4.1, 1.05);
makeBox(1.4, 0.24, 0.12, materials.water, -10.2, 1.75, 2.49);
const pumpRotor = new THREE.Group();
pumpRotor.position.set(-10.2, 1.9, 2.53);
makeBox(1.1, 0.13, 0.12, materials.pipeJoint, 0, 0, 0, pumpRotor);
makeBox(0.13, 1.1, 0.12, materials.pipeJoint, 0, 0, 0, pumpRotor);
world.add(pumpRotor);
// Closure valve.
const valveX = -2.5;
makeBox(0.92, 0.82, 0.9, materials.darkMetal, valveX, 0.68, 1.3);
makeBox(0.2, 0.66, 0.2, materials.pipeJoint, valveX, 1.34, 1.3);
const valveHandle = new THREE.Group();
valveHandle.position.set(valveX, 1.72, 1.3);
makeBox(1.35, 0.15, 0.16, materials.red, 0, 0, 0, valveHandle);
makeBox(0.16, 0.15, 0.72, materials.red, 0, 0, 0, valveHandle);
world.add(valveHandle);
// Local reserve tank.
const tankX = 4;
const tankZ = -3.7;
makeBox(4.45, 0.3, 4.45, materials.darkMetal, tankX, 0.2, tankZ);
const tankPosts = [
[tankX - 1.88, tankZ - 1.88],
[tankX + 1.88, tankZ - 1.88],
[tankX - 1.88, tankZ + 1.88],
[tankX + 1.88, tankZ + 1.88]
];
for (const [x, z] of tankPosts) {
makeBox(0.16, 3.5, 0.16, materials.pipeJoint, x, 1.95, z);
}
makeBox(3.76, 3.22, 0.07, materials.glass, tankX, 1.88, tankZ - 1.78);
makeBox(3.76, 3.22, 0.07, materials.glass, tankX, 1.88, tankZ + 1.78);
makeBox(0.07, 3.22, 3.56, materials.glass, tankX - 1.84, 1.88, tankZ);
makeBox(0.07, 3.22, 3.56, materials.glass, tankX + 1.84, 1.88, tankZ);
makeBox(4.05, 0.16, 4.05, materials.pipeJoint, tankX, 3.72, tankZ);
const tankFill = new THREE.Mesh(new THREE.BoxGeometry(3.5, 1, 3.5), materials.tankWater);
tankFill.position.set(tankX, 1.7, tankZ);
tankFill.castShadow = true;
world.add(tankFill);
const levelLights = [];
for (let i = 0; i < 8; i++) {
const lightMaterial = new THREE.MeshStandardMaterial({
color: 0x174a58,
emissive: 0x0b2730,
emissiveIntensity: 0.2,
roughness: 0.45
});
const light = makeBox(
0.34, 0.1, 0.08,
lightMaterial,
tankX + 1.72, 0.55 + i * 0.39, tankZ + 1.83
);
levelLights.push(light);
}
const beaconMaterial = new THREE.MeshStandardMaterial({
color: 0x66e3a6,
emissive: 0x2d9d68,
emissiveIntensity: 1.4,
roughness: 0.35
});
makeBox(0.25, 0.25, 0.25, beaconMaterial, tankX + 1.7, 3.96, tankZ + 1.7);
// Demand buildings.
function makeBuilding(x, z, width, depth, height) {
makeBox(width + 0.25, 0.22, depth + 0.25, materials.darkMetal, x, 0.17, z);
makeBox(width, height, depth, materials.building, x, 0.28 + height / 2, z);
makeBox(width + 0.28, 0.26, depth + 0.28, materials.roof, x, 0.41 + height, z);
const front = z + depth / 2 + 0.055;
const columns = Math.max(2, Math.floor(width / 0.72));
const rows = Math.max(2, Math.floor(height / 0.8));
for (let row = 0; row < rows; row++) {
for (let col = 0; col < columns; col++) {
const wx = x - width / 2 + 0.42 + col * ((width - 0.84) / Math.max(1, columns - 1));
const wy = 0.85 + row * 0.73;
if (wy < 0.32 + height - 0.2) {
makeBox(0.38, 0.34, 0.055, materials.window, wx, wy, front);
}
}
}
makeBox(0.6, 0.95, 0.06, materials.roof, x, 0.78, front + 0.02);
}
makeBuilding(7.0, 3.0, 2.35, 2.2, 2.65);
makeBuilding(10.0, 3.7, 2.7, 2.65, 3.75);
makeBuilding(7.5, 6.25, 3.0, 2.0, 2.15);
const demandLight = new THREE.PointLight(0xffb75f, 0.7, 8, 2);
demandLight.position.set(8.1, 3.2, 4.1);
scene.add(demandLight);
// Alternate-supply dock.
makeBox(3.6, 0.22, 2.6, materials.darkMetal, 0, 0.16, -5.4);
makeBox(0.55, 1.2, 0.55, materials.pipeJoint, 0.65, 0.78, -5.4);
makeBox(0.9, 0.16, 0.9, materials.orange, 0.65, 1.45, -5.4);
makeBox(0.42, 0.15, 2.3, materials.orange, 0.65, 0.35, -4.55);
// Voxel tanker truck.
const truck = new THREE.Group();
makeBox(3.05, 0.38, 1.38, materials.darkMetal, 0, 0.62, 0, truck);
makeBox(1.75, 1.18, 1.25, materials.truckBody, -0.45, 1.36, 0, truck);
makeBox(1.05, 1.28, 1.28, materials.orange, 1.02, 1.28, 0, truck);
makeBox(0.78, 0.42, 1.31, materials.glass, 1.05, 1.52, 0, truck);
makeBox(0.18, 0.18, 1.4, materials.white, 1.58, 0.82, 0, truck);
makeBox(0.5, 0.28, 0.5, materials.pipeJoint, -0.48, 2.08, 0, truck);
makeBox(0.65, 0.13, 0.65, materials.darkMetal, -0.48, 2.27, 0, truck);
for (const x of [-0.95, 0.9]) {
for (const z of [-0.72, 0.72]) {
makeBox(0.66, 0.66, 0.28, materials.rubber, x, 0.43, z, truck);
}
}
truck.visible = false;
world.add(truck);
// Flow packets.
const mainPackets = [];
for (let i = 0; i < 14; i++) {
const packet = makeBox(0.25, 0.13, 0.25, materials.brightWater, 0, 0.83, 1.3);
packet.castShadow = false;
packet.userData.offset = i / 14;
mainPackets.push(packet);
}
const reservePath = [
[4, -1.65],
[4, 1.3],
[9.6, 1.3]
];
const alternatePath = [
[0, -5.4],
[8.8, -5.4],
[8.8, 1.3]
];
const reservePackets = [];
const alternatePackets = [];
for (let i = 0; i < 8; i++) {
const packet = makeBox(0.23, 0.12, 0.23, materials.brightWater, 0, 0.82, 0);
packet.castShadow = false;
packet.userData.offset = i / 8;
reservePackets.push(packet);
}
for (let i = 0; i < 12; i++) {
const packet = makeBox(0.23, 0.12, 0.23, materials.brightWater, 0, 0.73, 0);
packet.castShadow = false;
packet.userData.offset = i / 12;
alternatePackets.push(packet);
}
function pointAlongPath(points, u, target) {
const lengths = [];
let total = 0;
for (let i = 0; i < points.length - 1; i++) {
const dx = points[i + 1][0] - points[i][0];
const dz = points[i + 1][1] - points[i][1];
const length = Math.hypot(dx, dz);
lengths.push(length);
total += length;
}
let distance = (u % 1) * total;
for (let i = 0; i < lengths.length; i++) {
if (distance <= lengths[i] || i === lengths.length - 1) {
const t = lengths[i] === 0 ? 0 : distance / lengths[i];
const x = THREE.MathUtils.lerp(points[i][0], points[i + 1][0], t);
const z = THREE.MathUtils.lerp(points[i][1], points[i + 1][1], t);
target.set(x, 0, z);
return;
}
distance -= lengths[i];
}
}
// Projected scene labels.
const sceneLabels = [];
function addSceneLabel(text, x, y, z) {
const element = document.createElement("div");
element.className = "scene-label";
element.textContent = text;
labelsRoot.appendChild(element);
sceneLabels.push({
element,
position: new THREE.Vector3(x, y, z)
});
return element;
}
const mainLabel = addSceneLabel("MAIN LINE · FLOWING", valveX, 2.65, 1.3);
const reserveLabel = addSceneLabel("LOCAL RESERVE · 100%", tankX, 4.45, tankZ);
const townLabel = addSceneLabel("DEMAND · SERVED", 8.2, 4.75, 4.0);
const alternateLabel = addSceneLabel("ALTERNATE · STANDBY", 0.4, 2.15, -5.4);
const sourceLabel = addSceneLabel("SOURCE", -10.2, 4.85, 1.3);
// -------------------------------------------------------------------------
// Simulation
// -------------------------------------------------------------------------
function closeMain() {
if (phase !== "normal") return;
phase = "outage";
outageElapsed = 0;
addEvent("Main isolation valve closed. Local reserve takes over demand.", "warn");
updatePanel();
}
function dispatchAlternate() {
if (phase !== "outage" || alternateRequested) return;
alternateRequested = true;
alternateProgress = 0;
truck.visible = true;
addEvent(`Emergency tanker dispatched — ${ALTERNATE_TRAVEL.toFixed(1)} second arrival.`, "warn");
updatePanel();
}
function alternateArrives() {
alternateArrived = true;
alternateProgress = 1;
if (phase === "outage") {
phase = "secured";
addEvent("Alternate supply connected. Demand is secure and reserve charging is paused.", "good");
} else if (phase === "ended") {
alternateWasLate = true;
addEvent("Alternate tanker arrived after the usable reserve was exhausted.", "bad");
endBannerText.textContent = "Alternate supply arrived too late to prevent the loss of demand service.";
}
updatePanel();
}
function endService() {
if (phase === "ended") return;
phase = "ended";
storage = RESERVE_FLOOR;
addEvent("Usable reserve exhausted. Neighborhood demand service has ended.", "bad");
updatePanel();
}
function resetScenario() {
phase = "normal";
storage = 100;
simTime = 0;
outageElapsed = 0;
alternateRequested = false;
alternateArrived = false;
alternateProgress = 0;
alternateWasLate = false;
truck.visible = false;
eventFeed.innerHTML = "";
addEvent(
scenarioMode === "rescue"
? "Scenario armed. Rescue tanker will dispatch shortly after closure."
: "Scenario armed. No alternate is scheduled."
);
updatePanel();
}
modeButtons.forEach(button => {
button.addEventListener("click", () => {
scenarioMode = button.dataset.mode;
modeButtons.forEach(item => item.classList.toggle("active", item === button));
resetScenario();
});
});
interruptButton.addEventListener("click", closeMain);
alternateButton.addEventListener("click", dispatchAlternate);
resetButton.addEventListener("click", resetScenario);
function updateSimulation(dt) {
simTime += dt;
if (phase === "normal" && simTime >= 1.4) {
closeMain();
}
if (phase === "outage") {
outageElapsed += dt;
storage = Math.max(RESERVE_FLOOR, storage - DRAWDOWN_RATE * dt);
if (scenarioMode === "rescue" && outageElapsed >= 2.7 && !alternateRequested) {
dispatchAlternate();
}
}
if (alternateRequested && !alternateArrived) {
alternateProgress = Math.min(1, alternateProgress + dt / ALTERNATE_TRAVEL);
if (alternateProgress >= 1) {
alternateArrives();
}
}
if (phase === "outage" && storage <= RESERVE_FLOOR + 0.001) {
endService();
}
if (phase === "secured") {
storage = Math.min(100, storage + 0.65 * dt);
}
}
function updatePanel() {
const runway = currentRunway();
const serviceActive = phase !== "ended";
let accent = "#53d9f4";
let title = "Preparing interruption";
let detail = "The main line is healthy while the closure sequence arms.";
if (phase === "normal") {
accent = "#53d9f4";
title = "Network steady";
detail = "Main supply is carrying neighborhood demand with the reserve at full capacity.";
} else if (phase === "outage") {
accent = runway < 6 ? "#ff6868" : "#ffbd62";
title = "Main line closed";
detail = alternateRequested
? "The reserve is still carrying demand while the alternate tanker approaches."
: "The local reserve is carrying demand. Alternate supply has not arrived.";
} else if (phase === "secured") {
accent = "#66e3a6";
title = "Demand secured";
detail = "Alternate supply is online before depletion. The reserve is held and slowly recharging.";
} else {
accent = "#ff6868";
title = "Service ended";
detail = alternateWasLate
? "Alternate supply arrived too late to preserve service."
: "The usable reserve was exhausted before alternate supply arrived.";
}
document.documentElement.style.setProperty("--accent", accent);
statusTitle.textContent = title;
statusDetail.textContent = detail;
statusCard.style.borderColor = accent + "55";
storageValue.textContent = Math.round(storage);
meterFill.style.width = `${storage}%`;
meterFill.style.background = accent;
if (phase === "secured") {
runwayValue.textContent = "Demand secured";
} else {
runwayValue.textContent = `${runway.toFixed(1)} s runway`;
}
setStateClass(mainState, phase === "normal" ? "good" : "bad");
mainState.textContent = phase === "normal" ? "FLOWING" : "CLOSED";
setStateClass(demandState, serviceActive ? "good" : "bad");
demandState.textContent = serviceActive ? "SERVED" : "INTERRUPTED";
if (alternateArrived) {
setStateClass(alternateState, phase === "secured" ? "good" : "bad");
alternateState.textContent = phase === "secured" ? "CONNECTED" : "ARRIVED LATE";
} else if (alternateRequested) {
const eta = Math.max(0, ALTERNATE_TRAVEL * (1 - alternateProgress));
setStateClass(alternateState, eta <= runway ? "info" : "warn");
alternateState.textContent = `EN ROUTE · ${eta.toFixed(1)}s`;
} else {
setStateClass(alternateState, "");
alternateState.textContent = "STANDBY";
}
setStateClass(decisionState, phase === "secured" ? "good" : phase === "ended" ? "bad" : "warn");
decisionState.textContent = phase === "secured"
? "SAFE"
: phase === "ended"
? "EXPIRED"
: `${runway.toFixed(1)} SECONDS`;
interruptButton.disabled = phase !== "normal";
alternateButton.disabled = phase !== "outage" || alternateRequested;
const wouldBeLate = phase === "outage" && runway < ALTERNATE_TRAVEL;
alternateButton.classList.toggle("urgent", wouldBeLate);
alternateButton.textContent = alternateRequested
? "Alternate dispatched"
: wouldBeLate
? "Dispatch — likely late"
: "Dispatch alternate";
const urgent = phase === "outage" && runway < 6;
document.body.classList.toggle("urgent", urgent);
document.body.classList.toggle("is-ended", phase === "ended");
const progress = phase === "normal"
? Math.min(12, (simTime / 1.4) * 15)
: phase === "outage"
? 15 + Math.min(85, (outageElapsed / FULL_RUNWAY) * 85)
: 100;
timelineFill.style.width = `${progress}%`;
STOP_NORMAL.className = phase === "normal" ? "active" : "complete";
STOP_CLOSURE.className = phase === "outage" ? "active" : phase === "normal" ? "" : "complete";
STOP_RESERVE.className = phase === "outage" ? "active" : phase === "secured" ? "complete" : "";
STOP_ALTERNATE.className = phase === "secured" ? "good active" : "";
STOP_END.className = phase === "ended" ? "bad active" : "";
mainLabel.textContent = phase === "normal" ? "MAIN LINE · FLOWING" : "MAIN LINE · CLOSED";
mainLabel.className = "scene-label" + (phase === "normal" ? "" : " danger");
reserveLabel.textContent = `LOCAL RESERVE · ${Math.round(storage)}%`;
reserveLabel.className = "scene-label" + (phase === "ended" ? " danger" : phase === "outage" ? " warning" : "");
townLabel.textContent = serviceActive ? "DEMAND · SERVED" : "DEMAND · SERVICE ENDED";
townLabel.className = "scene-label" + (serviceActive ? " safe" : " danger");
if (alternateArrived) {
alternateLabel.textContent = phase === "secured" ? "ALTERNATE · CONNECTED" : "ALTERNATE · TOO LATE";
alternateLabel.className = "scene-label" + (phase === "secured" ? " safe" : " danger");
} else if (alternateRequested) {
alternateLabel.textContent = "ALTERNATE · EN ROUTE";
alternateLabel.className = "scene-label warning";
} else {
alternateLabel.textContent = "ALTERNATE · STANDBY";
alternateLabel.className = "scene-label";
}
sourceLabel.className = "scene-label";
}
// -------------------------------------------------------------------------
// Scene animation
// -------------------------------------------------------------------------
const packetPosition = new THREE.Vector3();
function updateVisuals(dt) {
const serviceActive = phase !== "ended";
const valveTarget = phase === "normal" ? 0 : Math.PI / 2;
valveHandle.rotation.z = THREE.MathUtils.lerp(
valveHandle.rotation.z,
valveTarget,
1 - Math.exp(-dt * 7)
);
const waterHeight = 3.0 * (storage / 100);
tankFill.scale.y = waterHeight;
tankFill.position.y = 0.3 + waterHeight / 2;
for (let i = 0; i < levelLights.length; i++) {
const active = (i + 1) / levelLights.length <= storage / 100;
levelLights[i].material.color.set(active ? 0x46d8ef : 0x173b45);
levelLights[i].material.emissive.set(active ? 0x168cae : 0x071c23);
levelLights[i].material.emissiveIntensity = active ? 0.75 : 0.12;
}
const beaconColor = phase === "ended"
? 0xff3f4d
: phase === "secured"
? 0x3ef0a0
: phase === "outage"
? 0xffb84f
: 0x54d9ff;
beaconMaterial.color.set(beaconColor);
beaconMaterial.emissive.set(beaconColor);
beaconMaterial.emissiveIntensity = phase === "normal" ? 1.0 : 1.8 + Math.sin(visualTime * 5) * 0.35;
if (serviceActive) {
materials.window.color.set(0xffd68a);
materials.window.emissive.set(0xffaa3d);
materials.window.emissiveIntensity = 1.65;
demandLight.color.set(0xffb75f);
demandLight.intensity = 0.7;
} else {
materials.window.color.set(0x28383c);
materials.window.emissive.set(0x0a1114);
materials.window.emissiveIntensity = 0.04;
demandLight.color.set(0xff4b4b);
demandLight.intensity = 0.12;
}
pumpRotor.rotation.z += dt * (phase === "normal" ? 2.2 : 0);
for (const packet of mainPackets) {
const x = -10.45 + ((packet.userData.offset + visualTime * 0.115) % 1) * 19.7;
packet.position.x = x;
packet.visible = phase === "normal" && x < valveX - 0.4;
}
for (const packet of reservePackets) {
const u = (packet.userData.offset + visualTime * 0.18) % 1;
pointAlongPath(reservePath, u, packetPosition);
packet.position.x = packetPosition.x;
packet.position.z = packetPosition.z;
packet.visible = phase === "outage";
}
for (const packet of alternatePackets) {
const u = (packet.userData.offset + visualTime * 0.2) % 1;
pointAlongPath(alternatePath, u, packetPosition);
packet.position.x = packetPosition.x;
packet.position.z = packetPosition.z;
packet.visible = alternateArrived && phase === "secured";
}
if (alternateRequested) {
truck.visible = true;
truck.position.x = THREE.MathUtils.lerp(-13, 0, alternateProgress);
truck.position.z = -5.4;
truck.rotation.z = Math.sin(visualTime * 13) * 0.008;
} else {
truck.visible = false;
}
const alternateActive = alternateRequested;
alternatePipeMaterial.color.set(alternateActive ? 0x8c7253 : 0x364b50);
alternatePipeMaterial.emissive.set(alternateActive ? 0x63300c : 0x000000);
alternatePipeMaterial.emissiveIntensity = alternateActive ? 0.35 : 0;
}
function updateProjectedLabels() {
const width = window.innerWidth;
const height = window.innerHeight;
for (const label of sceneLabels) {
const projected = label.position.clone().project(camera);
const visible = projected.z > -1 && projected.z < 1 &&
Math.abs(projected.x) < 1.15 && Math.abs(projected.y) < 1.15;
label.element.style.display = visible ? "block" : "none";
if (visible) {
label.element.style.left = `${(projected.x * 0.5 + 0.5) * width}px`;
label.element.style.top = `${(-projected.y * 0.5 + 0.5) * height}px`;
}
}
}
// -------------------------------------------------------------------------
// Camera controls implemented directly without Three.js addons
// -------------------------------------------------------------------------
let dragging = false;
let pointerX = 0;
let pointerY = 0;
canvas.addEventListener("pointerdown", event => {
dragging = true;
pointerX = event.clientX;
pointerY = event.clientY;
canvas.setPointerCapture(event.pointerId);
});
canvas.addEventListener("pointermove", event => {
if (!dragging) return;
const dx = event.clientX - pointerX;
const dy = event.clientY - pointerY;
pointerX = event.clientX;
pointerY = event.clientY;
cameraYaw -= dx * 0.006;
cameraPolar = THREE.MathUtils.clamp(cameraPolar + dy * 0.005, 0.48, 1.25);
});
function endDrag(event) {
dragging = false;
if (canvas.hasPointerCapture(event.pointerId)) {
canvas.releasePointerCapture(event.pointerId);
}
}
canvas.addEventListener("pointerup", endDrag);
canvas.addEventListener("pointercancel", endDrag);
canvas.addEventListener("wheel", event => {
event.preventDefault();
cameraRadius = THREE.MathUtils.clamp(cameraRadius + event.deltaY * 0.018, 19, 44);
}, { passive: false });
canvas.addEventListener("dblclick", resetCamera);
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
});
// -------------------------------------------------------------------------
// Main loop
// -------------------------------------------------------------------------
const clock = new THREE.Clock();
function animate() {
const dt = Math.min(clock.getDelta(), 0.05);
visualTime += dt;
updateSimulation(dt);
updateVisuals(dt);
updateCamera();
updateProjectedLabels();
updatePanel();
renderer.render(scene, camera);
}
resetScenario();
renderer.setAnimationLoop(animate);
</script>
</body>
</html>
```