Prompt specification
Build or customize the Three.js 3D scene "Leak Signal in a Synthetic Meter Trace" 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: Leak Signal in a Synthetic Meter Trace
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/leak-signal-in-a-synthetic-meter-trace-x3974
Scene Description Prompt:
Please create one self-contained HTML file with Three.js from cdnjs showing resource meters, sensors, and measurement errors. Animate a fictional household water network with a known modeled leak. Compare inflow and intended use while labeling the trace synthetic and the diagnosis illustrative. Voxel style.
Working Three.js Starter (leak-signal-in-a-synthetic-meter-trace-x3974):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Household Water Lab — Synthetic Voxel Network</title>
<style>
:root {
color-scheme: dark;
--bg: #071018;
--panel: rgba(12, 25, 35, 0.88);
--panel-strong: #0d1c27;
--line: rgba(135, 190, 207, 0.16);
--text: #e7f5f7;
--muted: #8ba5af;
--cyan: #50e3ff;
--cyan-soft: #1d94af;
--amber: #ffc857;
--red: #ff657d;
--green: #72e6a2;
--shadow: 0 18px 50px rgba(0, 0, 0, 0.28);
}
* { box-sizing: border-box; }
html, body {
margin: 0;
width: 100%;
height: 100%;
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 {
border: 1px solid var(--line);
border-radius: 8px;
color: var(--text);
background: rgba(18, 39, 51, 0.88);
padding: 8px 11px;
cursor: pointer;
transition: border-color 150ms, background 150ms, transform 150ms;
}
button:hover {
border-color: rgba(80, 227, 255, 0.55);
background: rgba(25, 57, 72, 0.95);
}
button:active { transform: translateY(1px); }
button.primary {
color: #032029;
background: var(--cyan);
border-color: var(--cyan);
font-weight: 750;
}
.app {
display: grid;
grid-template-rows: 68px minmax(0, 1fr);
height: 100%;
}
header {
display: flex;
align-items: center;
justify-content: space-between;
gap: 20px;
padding: 0 22px;
border-bottom: 1px solid var(--line);
background: rgba(7, 16, 24, 0.94);
z-index: 10;
}
.brand {
display: flex;
align-items: center;
gap: 13px;
min-width: 0;
}
.brand-mark {
width: 34px;
height: 34px;
position: relative;
flex: 0 0 auto;
}
.brand-mark i {
position: absolute;
display: block;
width: 16px;
height: 16px;
border-radius: 3px;
transform: rotate(30deg) skew(-5deg);
}
.brand-mark i:nth-child(1) {
left: 1px;
top: 8px;
background: var(--cyan);
box-shadow: 0 0 22px rgba(80, 227, 255, 0.4);
}
.brand-mark i:nth-child(2) {
left: 15px;
top: 1px;
background: var(--amber);
}
.brand-mark i:nth-child(3) {
left: 15px;
top: 16px;
background: var(--red);
}
h1 {
margin: 0;
font-size: 17px;
letter-spacing: 0.02em;
white-space: nowrap;
}
.subtitle {
margin-top: 3px;
color: var(--muted);
font-size: 11px;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.badges {
display: flex;
gap: 8px;
align-items: center;
flex-wrap: wrap;
justify-content: flex-end;
}
.badge {
display: inline-flex;
align-items: center;
gap: 7px;
border: 1px solid rgba(255, 200, 87, 0.32);
background: rgba(255, 200, 87, 0.08);
color: #ffe09a;
border-radius: 999px;
padding: 7px 10px;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.09em;
text-transform: uppercase;
white-space: nowrap;
}
.badge::before {
content: "";
width: 6px;
height: 6px;
border-radius: 1px;
background: currentColor;
box-shadow: 0 0 10px currentColor;
}
.badge.illustrative {
color: #ff9aaa;
border-color: rgba(255, 101, 125, 0.35);
background: rgba(255, 101, 125, 0.08);
}
main {
display: grid;
grid-template-columns: minmax(0, 1fr) 390px;
min-height: 0;
}
.viewport {
position: relative;
min-width: 0;
min-height: 0;
overflow: hidden;
background:
radial-gradient(circle at 48% 40%, rgba(31, 93, 112, 0.18), transparent 45%),
linear-gradient(180deg, #08151e, #050b11);
}
#scene {
position: absolute;
inset: 0;
}
#scene canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
cursor: grab;
}
#scene canvas:active { cursor: grabbing; }
.scene-top {
position: absolute;
z-index: 4;
top: 16px;
left: 16px;
right: 16px;
display: flex;
align-items: flex-start;
justify-content: space-between;
gap: 14px;
pointer-events: none;
}
.readout,
.camera-tools {
border: 1px solid var(--line);
background: var(--panel);
box-shadow: var(--shadow);
backdrop-filter: blur(12px);
}
.readout {
border-radius: 10px;
padding: 11px 13px;
min-width: 245px;
}
.eyebrow {
color: var(--muted);
font-size: 9px;
font-weight: 800;
letter-spacing: 0.14em;
text-transform: uppercase;
}
.readout-row {
display: flex;
align-items: baseline;
justify-content: space-between;
gap: 20px;
margin-top: 5px;
}
.readout strong {
color: var(--cyan);
font-size: 22px;
font-variant-numeric: tabular-nums;
}
#clock {
color: var(--muted);
font: 11px ui-monospace, SFMono-Regular, Menlo, monospace;
}
.camera-tools {
border-radius: 10px;
padding: 8px;
display: flex;
gap: 6px;
pointer-events: auto;
}
.camera-tools button {
padding: 7px 9px;
font-size: 11px;
}
.scene-bottom {
position: absolute;
z-index: 4;
bottom: 15px;
left: 16px;
right: 16px;
display: flex;
justify-content: space-between;
align-items: flex-end;
gap: 16px;
pointer-events: none;
}
.legend {
display: flex;
flex-wrap: wrap;
gap: 12px;
border: 1px solid var(--line);
border-radius: 9px;
padding: 9px 11px;
background: rgba(7, 16, 24, 0.82);
backdrop-filter: blur(10px);
font-size: 10px;
color: #b9ccd3;
}
.legend span {
display: inline-flex;
align-items: center;
gap: 6px;
}
.legend i {
width: 8px;
height: 8px;
border-radius: 2px;
background: var(--cyan);
box-shadow: 0 0 9px currentColor;
}
.legend .use { background: var(--amber); }
.legend .leak { background: var(--red); }
.legend .sensor { background: var(--green); }
.scene-note {
max-width: 330px;
border-left: 2px solid var(--amber);
padding: 7px 10px;
color: #d4e0e4;
background: rgba(7, 16, 24, 0.78);
font-size: 10px;
line-height: 1.45;
text-align: right;
}
.world-label {
position: absolute;
z-index: 3;
transform: translate(-50%, -50%);
pointer-events: none;
white-space: nowrap;
border: 1px solid rgba(114, 230, 162, 0.38);
border-radius: 5px;
padding: 4px 7px;
background: rgba(6, 18, 25, 0.82);
color: #dff8e8;
font-size: 9px;
font-weight: 750;
letter-spacing: 0.06em;
text-transform: uppercase;
box-shadow: 0 5px 18px rgba(0, 0, 0, 0.2);
}
.world-label::before {
content: "";
display: inline-block;
width: 5px;
height: 5px;
margin-right: 6px;
border-radius: 1px;
background: var(--green);
box-shadow: 0 0 7px var(--green);
vertical-align: 1px;
}
.world-label.leak {
color: #ffd4dc;
border-color: rgba(255, 101, 125, 0.55);
background: rgba(48, 12, 22, 0.88);
}
.world-label.leak::before { background: var(--red); box-shadow: 0 0 8px var(--red); }
.world-label.use::before { background: var(--amber); box-shadow: 0 0 7px var(--amber); }
.world-label.source::before { background: var(--cyan); box-shadow: 0 0 7px var(--cyan); }
aside {
min-height: 0;
overflow-y: auto;
padding: 14px;
background: #08131c;
border-left: 1px solid var(--line);
scrollbar-width: thin;
scrollbar-color: #284654 transparent;
}
.panel {
border: 1px solid var(--line);
border-radius: 12px;
background: var(--panel-strong);
padding: 14px;
margin-bottom: 12px;
box-shadow: 0 10px 30px rgba(0, 0, 0, 0.12);
}
.panel-header {
display: flex;
align-items: center;
justify-content: space-between;
gap: 10px;
margin-bottom: 13px;
}
.panel-title {
margin: 0;
font-size: 11px;
font-weight: 850;
letter-spacing: 0.12em;
text-transform: uppercase;
}
.micro {
color: var(--muted);
font-size: 10px;
line-height: 1.45;
}
.balance-grid {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 7px;
margin-bottom: 15px;
}
.balance-cell {
border: 1px solid var(--line);
border-radius: 9px;
padding: 10px 8px;
background: rgba(4, 12, 18, 0.42);
min-width: 0;
}
.balance-cell .value {
margin-top: 5px;
font-size: 21px;
font-weight: 760;
font-variant-numeric: tabular-nums;
letter-spacing: -0.04em;
white-space: nowrap;
}
.balance-cell .unit {
color: var(--muted);
font-size: 9px;
margin-top: 2px;
}
.cyan { color: var(--cyan); }
.amber { color: var(--amber); }
.red { color: var(--red); }
.meter {
padding: 10px 0;
border-top: 1px solid rgba(135, 190, 207, 0.10);
}
.meter:first-of-type { border-top: 0; }
.meter-top {
display: flex;
justify-content: space-between;
align-items: baseline;
gap: 10px;
}
.meter-name {
font-size: 11px;
font-weight: 650;
}
.meter-value {
font-size: 14px;
font-weight: 800;
font-variant-numeric: tabular-nums;
white-space: nowrap;
}
.meter-sub {
display: flex;
justify-content: space-between;
color: var(--muted);
font-size: 9px;
margin-top: 5px;
}
.track {
position: relative;
height: 7px;
margin-top: 7px;
overflow: visible;
border-radius: 3px;
background: #142732;
}
.fill {
height: 100%;
width: 0;
border-radius: 3px;
background: var(--cyan);
box-shadow: 0 0 10px rgba(80, 227, 255, 0.35);
transition: width 120ms linear;
}
.fill.amber-fill {
background: var(--amber);
box-shadow: 0 0 10px rgba(255, 200, 87, 0.25);
}
.fill.red-fill {
background: var(--red);
box-shadow: 0 0 10px rgba(255, 101, 125, 0.3);
}
.truth-marker {
position: absolute;
top: -3px;
width: 2px;
height: 13px;
background: white;
opacity: 0.8;
box-shadow: 0 0 6px rgba(255, 255, 255, 0.45);
}
.truth-key {
color: #b9c8ce;
font-size: 8px;
}
.control {
margin-top: 12px;
}
.control label {
display: flex;
justify-content: space-between;
align-items: baseline;
gap: 10px;
font-size: 10px;
color: #b8cbd2;
}
.control output {
color: var(--text);
font-weight: 750;
font-variant-numeric: tabular-nums;
}
input[type="range"] {
width: 100%;
margin: 9px 0 0;
accent-color: var(--cyan);
}
.button-row {
display: flex;
gap: 7px;
margin-top: 13px;
}
.button-row button { flex: 1; font-size: 10px; }
.switch-line {
display: flex;
align-items: center;
justify-content: space-between;
gap: 10px;
margin-top: 12px;
padding-top: 11px;
border-top: 1px solid var(--line);
color: #b8cbd2;
font-size: 10px;
}
.switch-line input { accent-color: var(--cyan); }
#chart {
width: 100%;
height: 170px;
display: block;
border-radius: 7px;
background: #07121a;
}
.chart-legend {
display: flex;
flex-wrap: wrap;
gap: 10px;
margin-top: 8px;
color: var(--muted);
font-size: 8px;
}
.chart-legend span {
display: inline-flex;
align-items: center;
gap: 5px;
}
.chart-legend i {
display: inline-block;
width: 12px;
height: 2px;
background: var(--cyan);
}
.chart-legend .intended { background: var(--amber); }
.chart-legend .measured { background: white; }
.chart-legend .band { height: 7px; background: rgba(255, 101, 125, 0.35); }
.sensor {
display: grid;
grid-template-columns: 34px 1fr auto;
gap: 9px;
align-items: center;
padding: 10px 0;
border-top: 1px solid rgba(135, 190, 207, 0.10);
}
.sensor:first-of-type { border-top: 0; }
.sensor-icon {
width: 30px;
height: 30px;
display: grid;
place-items: center;
border-radius: 7px;
border: 1px solid rgba(114, 230, 162, 0.25);
color: var(--green);
background: rgba(114, 230, 162, 0.07);
font: 700 10px ui-monospace, monospace;
}
.sensor-name {
font-size: 10px;
font-weight: 750;
}
.sensor-detail {
color: var(--muted);
font-size: 8px;
margin-top: 3px;
}
.sensor-reading {
text-align: right;
font-size: 11px;
font-weight: 750;
font-variant-numeric: tabular-nums;
white-space: nowrap;
}
.sensor-error {
color: var(--amber);
font-size: 8px;
margin-top: 3px;
}
.diagnosis {
border-color: rgba(255, 101, 125, 0.24);
background:
linear-gradient(135deg, rgba(255, 101, 125, 0.08), transparent 55%),
var(--panel-strong);
}
.diagnosis-title {
color: #ffacb9;
font-size: 10px;
font-weight: 850;
letter-spacing: 0.08em;
text-transform: uppercase;
margin-bottom: 8px;
}
.diagnosis p {
margin: 0 0 8px;
color: #c7d7dc;
font-size: 10px;
line-height: 1.55;
}
.truth-box {
margin-top: 9px;
border: 1px dashed rgba(255, 200, 87, 0.28);
border-radius: 7px;
padding: 9px;
color: #e5d5ae;
background: rgba(255, 200, 87, 0.045);
font-size: 9px;
line-height: 1.45;
}
.disclaimer {
padding: 0 4px 10px;
color: #718b95;
font-size: 9px;
line-height: 1.55;
}
@media (max-width: 980px) {
html, body { overflow: auto; }
.app {
height: auto;
min-height: 100%;
grid-template-rows: auto auto;
}
header {
min-height: 68px;
padding: 12px 14px;
align-items: flex-start;
}
.badges { max-width: 240px; }
main {
display: block;
}
.viewport {
height: 62vh;
min-height: 480px;
}
aside {
overflow: visible;
border-left: 0;
border-top: 1px solid var(--line);
}
}
@media (max-width: 560px) {
.subtitle { display: none; }
.badges { max-width: 150px; }
.badge { font-size: 8px; padding: 6px 8px; }
.scene-top { left: 10px; right: 10px; top: 10px; }
.readout { min-width: 190px; }
.camera-tools { flex-direction: column; }
.scene-bottom { left: 10px; right: 10px; bottom: 10px; }
.scene-note { display: none; }
.world-label { font-size: 8px; }
}
</style>
</head>
<body>
<div class="app">
<header>
<div class="brand">
<div class="brand-mark" aria-hidden="true"><i></i><i></i><i></i></div>
<div>
<h1>Household Water Lab</h1>
<div class="subtitle">Fictional cutaway · voxel resource network</div>
</div>
</div>
<div class="badges">
<div class="badge">Synthetic trace</div>
<div class="badge illustrative">Illustrative diagnosis</div>
</div>
</header>
<main>
<section class="viewport" aria-label="Animated fictional household water network">
<div id="scene"></div>
<div class="scene-top">
<div class="readout">
<div class="eyebrow">Synthetic simulation clock</div>
<div class="readout-row">
<strong id="clock">00:00.0</strong>
<span id="run-state">RUNNING</span>
</div>
</div>
<div class="camera-tools">
<button id="view-reset" title="Reset camera">Reset view</button>
<button id="view-top" title="Top-down camera">Top</button>
<button id="pause" class="primary" title="Pause or resume">Pause</button>
</div>
</div>
<div class="world-label source" id="label-source">Synthetic source</div>
<div class="world-label" id="label-meter">Flow meter F-01</div>
<div class="world-label leak" id="label-leak">Modeled leak</div>
<div class="world-label use" id="label-kitchen">Kitchen</div>
<div class="world-label use" id="label-bath">Bath</div>
<div class="world-label use" id="label-laundry">Laundry</div>
<div class="world-label use" id="label-toilet">Toilet</div>
<div class="scene-bottom">
<div class="legend">
<span><i></i>Inflow</span>
<span><i class="use"></i>Intended use</span>
<span><i class="leak"></i>Modeled loss</span>
<span><i class="sensor"></i>Sensor</span>
</div>
<div class="scene-note">
Drag to orbit · wheel to zoom<br />
All geometry, flow, and readings are fictional.
</div>
</div>
</section>
<aside>
<section class="panel">
<div class="panel-header">
<h2 class="panel-title">Resource balance</h2>
<span class="micro">L/min · synthetic</span>
</div>
<div class="balance-grid">
<div class="balance-cell">
<div class="eyebrow">Inflow</div>
<div class="value cyan" id="balance-inflow">0.0</div>
<div class="unit">measured L/min</div>
</div>
<div class="balance-cell">
<div class="eyebrow">Intended</div>
<div class="value amber" id="balance-intended">0.0</div>
<div class="unit">measured L/min</div>
</div>
<div class="balance-cell">
<div class="eyebrow">Unexplained</div>
<div class="value red" id="balance-loss">0.0</div>
<div class="unit">difference L/min</div>
</div>
</div>
<div class="meter">
<div class="meter-top">
<span class="meter-name">Total inflow · F-01</span>
<span class="meter-value cyan" id="inflow-value">0.0</span>
</div>
<div class="track">
<div class="fill" id="inflow-fill"></div>
<div class="truth-marker" id="inflow-truth"></div>
</div>
<div class="meter-sub">
<span id="inflow-error">measurement error +0.00</span>
<span class="truth-key">│ modeled truth</span>
</div>
</div>
<div class="meter">
<div class="meter-top">
<span class="meter-name">Intended branch use · B-01</span>
<span class="meter-value amber" id="intended-value">0.0</span>
</div>
<div class="track">
<div class="fill amber-fill" id="intended-fill"></div>
<div class="truth-marker" id="intended-truth"></div>
</div>
<div class="meter-sub">
<span id="intended-error">measurement error +0.00</span>
<span class="truth-key">│ modeled truth</span>
</div>
</div>
<div class="meter">
<div class="meter-top">
<span class="meter-name">Unaccounted · F-01 − B-01</span>
<span class="meter-value red" id="loss-value">0.0</span>
</div>
<div class="track">
<div class="fill red-fill" id="loss-fill"></div>
</div>
<div class="meter-sub">
<span id="loss-error">combined uncertainty ±0.00</span>
<span>illustrative estimate</span>
</div>
</div>
</section>
<section class="panel">
<div class="panel-header">
<h2 class="panel-title">Scenario controls</h2>
<span class="micro">Known model inputs</span>
</div>
<div class="control">
<label for="leak">
Modeled leak rate
<output id="leak-output">2.4 L/min</output>
</label>
<input id="leak" type="range" min="0" max="4" step="0.1" value="2.4" />
</div>
<div class="control">
<label for="demand">
Intended-use multiplier
<output id="demand-output">1.00×</output>
</label>
<input id="demand" type="range" min="0.6" max="1.4" step="0.05" value="1" />
</div>
<div class="switch-line">
<label for="errors">Inject synthetic meter errors</label>
<input id="errors" type="checkbox" checked />
</div>
<div class="button-row">
<button id="reset-scenario">Reset scenario</button>
<button id="pause-2">Pause trace</button>
</div>
</section>
<section class="panel">
<div class="panel-header">
<h2 class="panel-title">Synthetic trace</h2>
<span class="micro">Last 10 seconds</span>
</div>
<canvas id="chart" aria-label="Synthetic inflow and intended-use chart"></canvas>
<div class="chart-legend">
<span><i></i>Modeled inflow</span>
<span><i class="intended"></i>Intended use</span>
<span><i class="measured"></i>Measured inflow</span>
<span><i class="band"></i>Difference</span>
</div>
</section>
<section class="panel">
<div class="panel-header">
<h2 class="panel-title">Sensor readings</h2>
<span class="micro">Simulated uncertainty</span>
</div>
<div class="sensor">
<div class="sensor-icon">F01</div>
<div>
<div class="sensor-name">Service flow meter</div>
<div class="sensor-detail">Inline electromagnetic sensor</div>
</div>
<div class="sensor-reading">
<div id="sensor-flow">0.0 L/min</div>
<div class="sensor-error" id="sensor-flow-error">Δ +0.00</div>
</div>
</div>
<div class="sensor">
<div class="sensor-icon">B01</div>
<div>
<div class="sensor-name">Intended-use branches</div>
<div class="sensor-detail">Aggregated fixture submeter</div>
</div>
<div class="sensor-reading">
<div id="sensor-branch">0.0 L/min</div>
<div class="sensor-error" id="sensor-branch-error">Δ +0.00</div>
</div>
</div>
<div class="sensor">
<div class="sensor-icon">P04</div>
<div>
<div class="sensor-name">Pressure transducer</div>
<div class="sensor-detail">Downstream service pressure</div>
</div>
<div class="sensor-reading">
<div id="sensor-pressure">0.00 bar</div>
<div class="sensor-error" id="sensor-pressure-error">Δ +0.00</div>
</div>
</div>
<div class="sensor">
<div class="sensor-icon">L03</div>
<div>
<div class="sensor-name">Acoustic leak cue</div>
<div class="sensor-detail">Relative synthetic amplitude</div>
</div>
<div class="sensor-reading">
<div id="sensor-acoustic">0.0 dB</div>
<div class="sensor-error" id="sensor-acoustic-error">Δ +0.0</div>
</div>
</div>
</section>
<section class="panel diagnosis">
<div class="diagnosis-title">Illustrative diagnosis only</div>
<p id="diagnosis-text">
Comparing aggregate inflow with intended fixture use suggests an unaccounted
water loss. The model places that loss at the service-side leak branch.
</p>
<p class="micro">
This is not a diagnosis of a real property and does not establish where a
physical leak would be.
</p>
<div class="truth-box">
<strong>Known simulation truth:</strong>
<span id="truth-leak">2.4 L/min</span> leaves at the marked service-side leak.
The meters above contain deliberately injected biases and noise.
</div>
</section>
<div class="disclaimer">
Educational synthetic visualization. Meter, sensor, pressure, acoustic, and
diagnostic outputs are generated by this page; they are not measurements from
real plumbing equipment.
</div>
</aside>
</main>
</div>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const $ = id => document.getElementById(id);
const clamp = THREE.MathUtils.clamp;
// Deterministic pseudo-randomness keeps the synthetic errors reproducible.
let randomState = 0x72f4a981;
function random() {
randomState += 0x6D2B79F5;
let t = randomState;
t = Math.imul(t ^ (t >>> 15), t | 1);
t ^= t + Math.imul(t ^ (t >>> 7), t | 61);
return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
}
function gaussian() {
const u = Math.max(random(), 1e-8);
const v = random();
return Math.sqrt(-2 * Math.log(u)) * Math.cos(Math.PI * 2 * v);
}
const sceneHost = $("scene");
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x07131c);
scene.fog = new THREE.FogExp2(0x07131c, 0.026);
const camera = new THREE.PerspectiveCamera(
48,
sceneHost.clientWidth / sceneHost.clientHeight,
0.1,
100
);
const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(sceneHost.clientWidth, sceneHost.clientHeight);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
sceneHost.appendChild(renderer.domElement);
scene.add(new THREE.HemisphereLight(0xbfefff, 0x10232d, 1.7));
const keyLight = new THREE.DirectionalLight(0xc6f4ff, 2.1);
keyLight.position.set(-5, 12, 8);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(1024, 1024);
keyLight.shadow.camera.left = -13;
keyLight.shadow.camera.right = 13;
keyLight.shadow.camera.top = 10;
keyLight.shadow.camera.bottom = -10;
scene.add(keyLight);
const rimLight = new THREE.PointLight(0x2ccfff, 18, 18, 2);
rimLight.position.set(2, 3.5, -3);
scene.add(rimLight);
const warmLight = new THREE.PointLight(0xffa958, 8, 10, 2);
warmLight.position.set(-5, 2.5, 2);
scene.add(warmLight);
const world = new THREE.Group();
scene.add(world);
const materials = {
dark: new THREE.MeshStandardMaterial({
color: 0x19303b,
roughness: 0.82,
metalness: 0.16
}),
wall: new THREE.MeshStandardMaterial({
color: 0x385462,
roughness: 0.9,
metalness: 0.03
}),
wallLight: new THREE.MeshStandardMaterial({
color: 0x5d7b85,
roughness: 0.9,
metalness: 0.03
}),
cyan: new THREE.MeshStandardMaterial({
color: 0x50e3ff,
emissive: 0x0b6070,
emissiveIntensity: 0.85,
roughness: 0.42,
metalness: 0.18
}),
amber: new THREE.MeshStandardMaterial({
color: 0xffc857,
emissive: 0x684209,
emissiveIntensity: 0.75,
roughness: 0.5,
metalness: 0.1
}),
red: new THREE.MeshStandardMaterial({
color: 0xff657d,
emissive: 0x781329,
emissiveIntensity: 1.1,
roughness: 0.42,
metalness: 0.12
}),
green: new THREE.MeshStandardMaterial({
color: 0x72e6a2,
emissive: 0x185f3b,
emissiveIntensity: 1.0,
roughness: 0.45
}),
glass: new THREE.MeshStandardMaterial({
color: 0x5ad9ef,
transparent: true,
opacity: 0.32,
roughness: 0.25,
metalness: 0.1,
depthWrite: false
})
};
function addBox(parent, x, y, z, w, h, d, material, castShadow = true) {
const mesh = new THREE.Mesh(new THREE.BoxGeometry(w, h, d), material);
mesh.position.set(x, y, z);
mesh.castShadow = castShadow;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
// Voxel ground tiles.
{
const tileGeometry = new THREE.BoxGeometry(0.74, 0.07, 0.74);
const tileMaterial = new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 0.93,
metalness: 0.03
});
const columns = 25;
const rows = 16;
const tiles = new THREE.InstancedMesh(tileGeometry, tileMaterial, columns * rows);
const dummy = new THREE.Object3D();
const color = new THREE.Color();
let index = 0;
for (let z = 0; z < rows; z++) {
for (let x = 0; x < columns; x++) {
dummy.position.set(-9.2 + x * 0.78, -0.10, -5.85 + z * 0.78);
dummy.updateMatrix();
tiles.setMatrixAt(index, dummy.matrix);
const shade = ((x + z) % 3 === 0) ? 0x10242e : 0x0d1d26;
color.setHex(shade);
tiles.setColorAt(index, color);
index++;
}
}
tiles.receiveShadow = true;
world.add(tiles);
}
// Routes are used for both voxel pipe construction and animated packets.
const routes = {
inflow: [
new THREE.Vector3(-8.45, 0.34, -3.15),
new THREE.Vector3(-6.15, 0.34, -3.15),
new THREE.Vector3(-6.15, 0.34, -1.15),
new THREE.Vector3(-3.45, 0.34, -1.15),
new THREE.Vector3(-2.35, 0.34, -1.15)
],
kitchen: [
new THREE.Vector3(-2.35, 0.34, -1.15),
new THREE.Vector3(-0.20, 0.34, -1.15),
new THREE.Vector3(-0.20, 0.86, -1.15)
],
bath: [
new THREE.Vector3(-2.35, 0.34, -1.15),
new THREE.Vector3(-2.35, 0.34, 0.55),
new THREE.Vector3(-0.80, 0.34, 0.55),
new THREE.Vector3(-0.80, 0.88, 0.55)
],
laundry: [
new THREE.Vector3(-2.35, 0.34, -1.15),
new THREE.Vector3(1.30, 0.34, -1.15),
new THREE.Vector3(1.30, 0.34, 0.95)
],
toilet: [
new THREE.Vector3(-2.35, 0.34, -1.15),
new THREE.Vector3(-2.35, 0.34, 1.55),
new THREE.Vector3(-0.20, 0.34, 1.55)
],
leak: [
new THREE.Vector3(-6.15, 0.34, -3.15),
new THREE.Vector3(-6.15, 0.08, -3.15),
new THREE.Vector3(-6.15, 0.08, -2.65)
]
};
function addVoxelPipe(route, color, size = 0.20, step = 0.25) {
const records = [];
for (let i = 0; i < route.length - 1; i++) {
const a = route[i];
const b = route[i + 1];
const length = a.distanceTo(b);
const count = Math.max(1, Math.ceil(length / step));
for (let j = 0; j < count; j++) {
const p = new THREE.Vector3().lerpVectors(a, b, (j + 0.5) / count);
records.push({ position: p, color });
}
}
for (const p of route) records.push({ position: p.clone(), color, node: true });
return records;
}
const pipeRecords = [
...addVoxelPipe(routes.inflow, 0x3bcfe8, 0.23),
...addVoxelPipe(routes.kitchen, 0xe7ad37, 0.18),
...addVoxelPipe(routes.bath, 0xe7ad37, 0.18),
...addVoxelPipe(routes.laundry, 0xe7ad37, 0.18),
...addVoxelPipe(routes.toilet, 0xe7ad37, 0.18),
...addVoxelPipe(routes.leak, 0xef4e68, 0.19)
];
{
const geometry = new THREE.BoxGeometry(1, 1, 1);
const material = new THREE.MeshStandardMaterial({
color: 0xffffff,
emissive: 0x071d23,
emissiveIntensity: 0.5,
roughness: 0.48,
metalness: 0.28
});
const mesh = new THREE.InstancedMesh(geometry, material, pipeRecords.length);
const dummy = new THREE.Object3D();
const color = new THREE.Color();
pipeRecords.forEach((record, i) => {
const size = record.node ? 0.27 : 0.20;
dummy.position.copy(record.position);
dummy.scale.set(size, size, size);
dummy.rotation.set(0, 0, 0);
dummy.updateMatrix();
mesh.setMatrixAt(i, dummy.matrix);
color.setHex(record.color);
mesh.setColorAt(i, color);
});
mesh.castShadow = true;
mesh.receiveShadow = true;
world.add(mesh);
}
// House shell, intentionally cut away for visibility.
addBox(world, -0.15, 0.03, -0.40, 5.75, 0.18, 4.70, materials.dark);
addBox(world, -0.15, 1.43, -2.72, 5.75, 2.72, 0.17, materials.wall);
addBox(world, -2.98, 0.52, -0.40, 0.17, 0.90, 4.70, materials.wallLight);
addBox(world, -0.15, 2.83, -2.72, 5.85, 0.12, 0.22, materials.dark);
addBox(world, -2.98, 2.83, -0.40, 0.16, 0.12, 4.75, materials.dark);
// Front window and doorway blocks.
addBox(world, 1.65, 1.65, -2.61, 1.05, 0.95, 0.05, materials.glass, false);
addBox(world, -2.11, 1.15, -2.60, 0.85, 2.05, 0.05, materials.glass, false);
// Kitchen counter, cabinets, and sink.
addBox(world, 0.42, 0.48, -1.93, 1.55, 0.78, 0.72, materials.dark);
addBox(world, 0.42, 0.90, -1.93, 1.66, 0.10, 0.80, materials.wallLight);
addBox(world, 0.20, 0.97, -1.90, 0.62, 0.08, 0.46, materials.dark);
addBox(world, 1.10, 0.97, -1.88, 0.28, 0.08, 0.28, materials.cyan);
// Bath tray and voxel shower.
addBox(world, -0.95, 0.17, 0.65, 1.65, 0.22, 1.30, materials.wallLight);
addBox(world, -1.52, 1.15, 0.55, 0.11, 2.10, 0.11, materials.cyan);
addBox(world, -1.20, 2.12, 0.55, 0.70, 0.11, 0.11, materials.cyan);
addBox(world, -0.92, 1.98, 0.55, 0.28, 0.10, 0.28, materials.cyan);
// Laundry machine.
addBox(world, 1.30, 0.57, 1.00, 0.94, 0.94, 0.82, materials.wallLight);
addBox(world, 1.30, 0.63, 0.58, 0.52, 0.52, 0.06, materials.dark);
addBox(world, 1.30, 0.63, 0.54, 0.34, 0.34, 0.05, materials.cyan);
addBox(world, 1.60, 0.90, 0.52, 0.18, 0.08, 0.05, materials.amber);
// Toilet.
addBox(world, -0.36, 0.30, 1.57, 0.70, 0.48, 0.72, materials.wallLight);
addBox(world, -0.36, 0.60, 1.54, 0.78, 0.16, 0.82, materials.dark);
addBox(world, -0.36, 0.72, 1.54, 0.58, 0.09, 0.60, materials.cyan);
addBox(world, -0.36, 0.91, 1.90, 0.66, 0.88, 0.20, materials.wallLight);
// Synthetic source tower.
addBox(world, -8.45, 0.13, -3.15, 1.60, 0.24, 1.45, materials.dark);
addBox(world, -8.45, 1.12, -3.15, 1.22, 1.72, 1.10, materials.glass, false);
addBox(world, -8.45, 1.16, -3.15, 0.95, 1.35, 0.82, materials.cyan, false);
addBox(world, -8.45, 2.03, -3.15, 1.48, 0.16, 1.34, materials.dark);
for (const x of [-9.02, -7.88]) {
addBox(world, x, 0.55, -3.15, 0.16, 1.10, 0.16, materials.dark);
}
// Service flow meter.
addBox(world, -5.26, 0.52, -3.15, 0.62, 0.42, 0.50, materials.dark);
addBox(world, -5.26, 0.56, -2.88, 0.39, 0.18, 0.04, materials.cyan);
// Sensor beacons.
const sensorBeacons = [];
function addSensor(x, z, material) {
addBox(world, x, 0.50, z, 0.12, 0.72, 0.12, materials.dark);
const beacon = addBox(world, x, 0.91, z, 0.23, 0.23, 0.23, material, false);
sensorBeacons.push(beacon);
}
addSensor(-5.26, -3.60, materials.green);
addSensor(-2.35, -1.65, materials.green);
addSensor(-5.76, -2.78, materials.green);
// Leak marker and animated puddle voxels.
const leakMarker = addBox(
world, -6.15, 0.48, -3.15, 0.34, 0.34, 0.34, materials.red, false
);
const puddleMaterial = new THREE.MeshStandardMaterial({
color: 0xff5571,
emissive: 0x5b0c1b,
emissiveIntensity: 0.85,
transparent: true,
opacity: 0.65,
roughness: 0.35
});
const puddle = [];
for (let i = 0; i < 13; i++) {
const angle = i * 2.399;
const radius = 0.14 + (i % 4) * 0.13;
const tile = addBox(
world,
-6.15 + Math.cos(angle) * radius,
0.005,
-2.76 + Math.sin(angle) * radius * 0.65,
0.27,
0.025,
0.27,
puddleMaterial,
false
);
tile.rotation.y = angle * 0.25;
puddle.push(tile);
}
// Animated packet helpers.
function routeMetrics(route) {
const cumulative = [0];
for (let i = 1; i < route.length; i++) {
cumulative.push(cumulative[i - 1] + route[i].distanceTo(route[i - 1]));
}
return { cumulative, length: cumulative[cumulative.length - 1] };
}
function sampleRoute(route, t) {
const metrics = routeMetrics(route);
const distance = clamp(t, 0, 0.999999) * metrics.length;
for (let i = 1; i < route.length; i++) {
if (distance <= metrics.cumulative[i]) {
const segmentLength = metrics.cumulative[i] - metrics.cumulative[i - 1];
const local = (distance - metrics.cumulative[i - 1]) / segmentLength;
return new THREE.Vector3().lerpVectors(route[i - 1], route[i], local);
}
}
return route[route.length - 1].clone();
}
function makePacketSystem(route, count, color, size) {
const geometry = new THREE.BoxGeometry(size, size, size);
const material = new THREE.MeshStandardMaterial({
color,
emissive: color,
emissiveIntensity: 1.0,
roughness: 0.28,
metalness: 0.08
});
const mesh = new THREE.InstancedMesh(geometry, material, count);
mesh.instanceMatrix.setUsage(THREE.DynamicDrawUsage);
world.add(mesh);
const phases = Array.from({ length: count }, () => random());
return { route, count, mesh, phases };
}
const packetSystems = [
makePacketSystem(routes.inflow, 24, 0x8cf2ff, 0.105),
makePacketSystem(routes.kitchen, 8, 0xffd77b, 0.09),
makePacketSystem(routes.bath, 8, 0xffd77b, 0.09),
makePacketSystem(routes.laundry, 7, 0xffd77b, 0.09),
makePacketSystem(routes.toilet, 6, 0xffd77b, 0.09),
makePacketSystem(routes.leak, 10, 0xff7890, 0.09)
];
// Camera controls implemented directly, without Three.js addons.
const cameraState = {
target: new THREE.Vector3(-0.1, 0.75, -0.45),
radius: 15.8,
theta: 0.69,
phi: 1.03
};
function updateCamera() {
const { target, radius, theta, phi } = cameraState;
camera.position.set(
target.x + radius * Math.sin(phi) * Math.sin(theta),
target.y + radius * Math.cos(phi),
target.z + radius * Math.sin(phi) * Math.cos(theta)
);
camera.lookAt(target);
}
function resetCamera(top = false) {
cameraState.target.set(-0.1, 0.75, -0.45);
cameraState.radius = top ? 16.8 : 15.8;
cameraState.theta = top ? 0 : 0.69;
cameraState.phi = top ? 0.20 : 1.03;
updateCamera();
}
let dragging = false;
let previousX = 0;
let previousY = 0;
renderer.domElement.addEventListener("pointerdown", event => {
dragging = true;
previousX = event.clientX;
previousY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", event => {
if (!dragging) return;
const dx = event.clientX - previousX;
const dy = event.clientY - previousY;
previousX = event.clientX;
previousY = event.clientY;
cameraState.theta -= dx * 0.006;
cameraState.phi = clamp(cameraState.phi - dy * 0.005, 0.20, 1.42);
updateCamera();
});
function stopDragging() { dragging = false; }
renderer.domElement.addEventListener("pointerup", stopDragging);
renderer.domElement.addEventListener("pointercancel", stopDragging);
renderer.domElement.addEventListener(
"wheel",
event => {
event.preventDefault();
cameraState.radius = clamp(
cameraState.radius + event.deltaY * 0.012,
9.5,
24
);
updateCamera();
},
{ passive: false }
);
renderer.domElement.addEventListener("dblclick", () => resetCamera());
// World-space labels.
const labels = [
["label-source", new THREE.Vector3(-8.45, 2.45, -3.15)],
["label-meter", new THREE.Vector3(-5.25, 1.02, -3.05)],
["label-leak", new THREE.Vector3(-6.15, 1.00, -3.12)],
["label-kitchen", new THREE.Vector3(-0.15, 1.37, -1.65)],
["label-bath", new THREE.Vector3(-0.90, 2.43, 0.55)],
["label-laundry", new THREE.Vector3(1.30, 1.30, 1.00)],
["label-toilet", new THREE.Vector3(-0.35, 1.48, 1.75)]
].map(([id, position]) => ({ element: $(id), position }));
function updateLabels() {
const width = sceneHost.clientWidth;
const height = sceneHost.clientHeight;
for (const label of labels) {
const projected = label.position.clone().project(camera);
const visible = projected.z > -1 && projected.z < 1;
const x = (projected.x * 0.5 + 0.5) * width;
const y = (-projected.y * 0.5 + 0.5) * height;
label.element.style.display = visible ? "block" : "none";
label.element.style.left = `${x}px`;
label.element.style.top = `${y}px`;
}
}
// Simulation state.
let running = true;
let simulationTime = 0;
let modeledLeak = 2.4;
let demandMultiplier = 1;
let injectErrors = true;
const history = [];
const maxHistory = 100;
const latest = {
trueInflow: 0,
trueIntended: 0,
measuredInflow: 0,
measuredIntended: 0,
flowError: 0,
branchError: 0,
pressure: 0,
pressureError: 0,
acoustic: 0,
acousticError: 0
};
function signed(value, digits = 2) {
return `${value >= 0 ? "+" : ""}${value.toFixed(digits)}`;
}
function updateData() {
const demandWave =
8.7 +
2.0 * Math.sin(simulationTime * 0.38) +
0.9 * Math.sin(simulationTime * 0.91 + 1.2);
const trueIntended = Math.max(0, demandWave * demandMultiplier);
const trueInflow = trueIntended + modeledLeak;
const flowBias = injectErrors ? 0.22 + 0.07 * Math.sin(simulationTime * 0.42) : 0;
const branchBias = injectErrors ? -0.15 + 0.06 * Math.sin(simulationTime * 0.57 + 1) : 0;
const flowNoise = injectErrors ? gaussian() * 0.16 : 0;
const branchNoise = injectErrors ? gaussian() * 0.12 : 0;
const measuredInflow = Math.max(0, trueInflow + flowBias + flowNoise);
const measuredIntended = Math.max(0, trueIntended + branchBias + branchNoise);
const unexplained = Math.max(0, measuredInflow - measuredIntended);
const pressureTruth = 3.45 - modeledLeak * 0.15;
const pressure = pressureTruth + (injectErrors ? gaussian() * 0.035 : 0);
const acousticTruth = 7.2 + modeledLeak * 2.25;
const acoustic = acousticTruth + (injectErrors ? gaussian() * 0.7 : 0);
latest.trueInflow = trueInflow;
latest.trueIntended = trueIntended;
latest.measuredInflow = measuredInflow;
latest.measuredIntended = measuredIntended;
latest.flowError = measuredInflow - trueInflow;
latest.branchError = measuredIntended - trueIntended;
latest.pressure = pressure;
latest.pressureError = pressure - pressureTruth;
latest.acoustic = acoustic;
latest.acousticError = acoustic - acousticTruth;
$("balance-inflow").textContent = measuredInflow.toFixed(1);
$("balance-intended").textContent = measuredIntended.toFixed(1);
$("balance-loss").textContent = unexplained.toFixed(1);
$("inflow-value").textContent = `${measuredInflow.toFixed(1)} L/min`;
$("intended-value").textContent = `${measuredIntended.toFixed(1)} L/min`;
$("loss-value").textContent = `${unexplained.toFixed(1)} L/min`;
$("inflow-error").textContent = `measurement error ${signed(latest.flowError)}`;
$("intended-error").textContent = `measurement error ${signed(latest.branchError)}`;
const combinedUncertainty = injectErrors ? 0.36 : 0;
$("loss-error").textContent = `combined uncertainty ±${combinedUncertainty.toFixed(2)}`;
$("inflow-fill").style.width = `${clamp(measuredInflow / 18 * 100, 0, 100)}%`;
$("intended-fill").style.width = `${clamp(measuredIntended / 18 * 100, 0, 100)}%`;
$("loss-fill").style.width = `${clamp(unexplained / 6 * 100, 0, 100)}%`;
$("inflow-truth").style.left = `${clamp(trueInflow / 18 * 100, 0, 100)}%`;
$("intended-truth").style.left = `${clamp(trueIntended / 18 * 100, 0, 100)}%`;
$("sensor-flow").textContent = `${measuredInflow.toFixed(1)} L/min`;
$("sensor-flow-error").textContent = `Δ ${signed(latest.flowError)}`;
$("sensor-branch").textContent = `${measuredIntended.toFixed(1)} L/min`;
$("sensor-branch-error").textContent = `Δ ${signed(latest.branchError)}`;
$("sensor-pressure").textContent = `${pressure.toFixed(2)} bar`;
$("sensor-pressure-error").textContent = `Δ ${signed(latest.pressureError)}`;
$("sensor-acoustic").textContent = `${acoustic.toFixed(1)} dB`;
$("sensor-acoustic-error").textContent = `Δ ${signed(latest.acousticError, 1)}`;
$("truth-leak").textContent = `${modeledLeak.toFixed(1)} L/min`;
const difference = Math.abs(unexplained - modeledLeak);
if (modeledLeak < 0.15) {
$("diagnosis-text").textContent =
"The synthetic balance indicates little or no unaccounted flow. No meaningful loss branch is active in this modeled scenario.";
} else if (difference < 0.8) {
$("diagnosis-text").textContent =
`The measured balance suggests approximately ${unexplained.toFixed(1)} L/min of unaccounted use. ` +
`In this fictional model, the service-side leak branch produces the known ${modeledLeak.toFixed(1)} L/min loss.`;
} else {
$("diagnosis-text").textContent =
`The noisy meters currently suggest ${unexplained.toFixed(1)} L/min of unaccounted use, ` +
`which differs from the known synthetic leak value. This illustrates why meter uncertainty must be considered.`;
}
history.push({
trueInflow,
trueIntended,
measuredInflow
});
if (history.length > maxHistory) history.shift();
drawChart();
updateClock();
}
function updateClock() {
const minutes = Math.floor(simulationTime / 60);
const seconds = simulationTime % 60;
$("clock").textContent =
`${String(minutes).padStart(2, "0")}:${seconds.toFixed(1).padStart(4, "0")}`;
$("run-state").textContent = running ? "RUNNING" : "PAUSED";
$("pause").textContent = running ? "Pause" : "Resume";
$("pause-2").textContent = running ? "Pause trace" : "Resume trace";
}
// Lightweight canvas chart.
const chart = $("chart");
const chartContext = chart.getContext("2d");
function drawChart() {
const rect = chart.getBoundingClientRect();
if (!rect.width || !rect.height) return;
const dpr = Math.min(window.devicePixelRatio, 2);
const width = Math.floor(rect.width * dpr);
const height = Math.floor(rect.height * dpr);
if (chart.width !== width || chart.height !== height) {
chart.width = width;
chart.height = height;
}
const ctx = chartContext;
ctx.setTransform(dpr, 0, 0, dpr, 0, 0);
const w = rect.width;
const h = rect.height;
ctx.clearRect(0, 0, w, h);
const pad = { left: 28, right: 8, top: 10, bottom: 18 };
const chartW = w - pad.left - pad.right;
const chartH = h - pad.top - pad.bottom;
const maxY = 18;
const xAt = i => pad.left + (i / Math.max(1, maxHistory - 1)) * chartW;
const yAt = value => pad.top + chartH - clamp(value / maxY, 0, 1) * chartH;
ctx.font = "8px ui-monospace, monospace";
ctx.lineWidth = 1;
for (let i = 0; i <= 3; i++) {
const value = i * 6;
const y = yAt(value);
ctx.strokeStyle = "rgba(139, 165, 175, 0.12)";
ctx.beginPath();
ctx.moveTo(pad.left, y);
ctx.lineTo(w - pad.right, y);
ctx.stroke();
ctx.fillStyle = "#67808a";
ctx.fillText(String(value), 3, y + 3);
}
if (history.length < 2) return;
// Difference band.
ctx.beginPath();
history.forEach((sample, i) => {
const x = xAt(i);
const y = yAt(sample.trueInflow);
if (i === 0) ctx.moveTo(x, y);
else ctx.lineTo(x, y);
});
for (let i = history.length - 1; i >= 0; i--) {
ctx.lineTo(xAt(i), yAt(history[i].trueIntended));
}
ctx.closePath();
ctx.fillStyle = "rgba(255, 101, 125, 0.22)";
ctx.fill();
function drawSeries(key, color, dashed = false) {
ctx.beginPath();
history.forEach((sample, i) => {
const x = xAt(i);
const y = yAt(sample[key]);
if (i === 0) ctx.moveTo(x, y);
else ctx.lineTo(x, y);
});
ctx.setLineDash(dashed ? [4, 3] : []);
ctx.strokeStyle = color;
ctx.lineWidth = dashed ? 1.4 : 2;
ctx.stroke();
ctx.setLineDash([]);
}
drawSeries("trueInflow", "#50e3ff");
drawSeries("trueIntended", "#ffc857");
drawSeries("measuredInflow", "#eefbff", true);
ctx.fillStyle = "#67808a";
ctx.fillText("now", w - 23, h - 4);
ctx.fillText("-10 s", pad.left, h - 4);
}
// UI events.
$("leak").addEventListener("input", event => {
modeledLeak = Number(event.target.value);
$("leak-output").textContent = `${modeledLeak.toFixed(1)} L/min`;
updateData();
});
$("demand").addEventListener("input", event => {
demandMultiplier = Number(event.target.value);
$("demand-output").textContent = `${demandMultiplier.toFixed(2)}×`;
updateData();
});
$("errors").addEventListener("change", event => {
injectErrors = event.target.checked;
updateData();
});
function togglePause() {
running = !running;
updateClock();
}
$("pause").addEventListener("click", togglePause);
$("pause-2").addEventListener("click", togglePause);
function resetScenario() {
simulationTime = 0;
history.length = 0;
modeledLeak = 2.4;
demandMultiplier = 1;
injectErrors = true;
$("leak").value = "2.4";
$("demand").value = "1";
$("errors").checked = true;
$("leak-output").textContent = "2.4 L/min";
$("demand-output").textContent = "1.00×";
updateData();
}
$("reset-scenario").addEventListener("click", resetScenario);
$("view-reset").addEventListener("click", () => resetCamera(false));
$("view-top").addEventListener("click", () => resetCamera(true));
window.addEventListener("keydown", event => {
if (event.code === "Space") {
event.preventDefault();
togglePause();
}
if (event.key.toLowerCase() === "r") resetScenario();
});
function resize() {
const width = sceneHost.clientWidth;
const height = sceneHost.clientHeight;
camera.aspect = width / height;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(width, height);
drawChart();
}
window.addEventListener("resize", resize);
// Animation loop.
const dummy = new THREE.Object3D();
const clock = new THREE.Clock();
let dataAccumulator = 0;
function animate() {
requestAnimationFrame(animate);
const delta = Math.min(clock.getDelta(), 0.05);
if (running) simulationTime += delta;
dataAccumulator += delta;
if (dataAccumulator >= 0.1) {
dataAccumulator %= 0.1;
updateData();
}
const flowFactor = clamp(latest.measuredInflow / 12, 0.35, 1.8);
const useFactor = clamp(latest.measuredIntended / 10, 0.25, 1.8);
packetSystems[0].phases.forEach((phase, i) => {
const t = (phase + simulationTime * 0.055 * flowFactor) % 1;
dummy.position.copy(sampleRoute(routes.inflow, t));
dummy.rotation.set(0, simulationTime * 1.4 + i, 0);
dummy.scale.setScalar(1);
dummy.updateMatrix();
packetSystems[0].mesh.setMatrixAt(i, dummy.matrix);
});
const branchSystems = packetSystems.slice(1, 5);
branchSystems.forEach((system, systemIndex) => {
system.phases.forEach((phase, i) => {
const t = (phase + simulationTime * 0.085 * useFactor) % 1;
dummy.position.copy(sampleRoute(system.route, t));
dummy.rotation.set(0, simulationTime + i, 0);
dummy.scale.setScalar(1);
dummy.updateMatrix();
system.mesh.setMatrixAt(i, dummy.matrix);
});
});
packetSystems[5].phases.forEach((phase, i) => {
const t = (phase + simulationTime * (0.23 + modeledLeak * 0.025)) % 1;
dummy.position.copy(sampleRoute(routes.leak, t));
dummy.rotation.set(0, simulationTime * 2 + i, 0);
dummy.scale.setScalar(1);
dummy.updateMatrix();
packetSystems[5].mesh.setMatrixAt(i, dummy.matrix);
});
for (const system of packetSystems) {
system.mesh.instanceMatrix.needsUpdate = true;
}
const leakPulse = 1 + Math.sin(simulationTime * 7) * 0.16;
leakMarker.scale.setScalar(leakPulse);
leakMarker.rotation.y = simulationTime * 0.7;
puddle.forEach((tile, i) => {
const pulse = 0.92 + Math.sin(simulationTime * 3.5 + i * 0.8) * 0.12;
tile.scale.set(pulse, 1, pulse);
tile.material.opacity = 0.48 + Math.sin(simulationTime * 2.2 + i) * 0.12;
});
sensorBeacons.forEach((beacon, i) => {
const pulse = 0.86 + Math.sin(simulationTime * 4 + i * 1.8) * 0.14;
beacon.scale.setScalar(pulse);
});
updateLabels();
renderer.render(scene, camera);
}
resetCamera(false);
updateData();
resize();
animate();
</script>
</body>
</html>
```