Prompt specification
Build or customize the Three.js 3D scene "Neighborhood Resource Interface Block" 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: Neighborhood Resource Interface Block
Category: Everyday & Industrial · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/neighborhood-resource-interface-block-x3945
Scene Description Prompt:
Please create one self-contained HTML file with Three.js from cdnjs showing linked water, energy, and material flows in a neighborhood. Model a small building block with separately labeled water, electricity, food, and waste ports. Give each flow a distinct unit and prevent accidental unit mixing. Low-poly.
Working Three.js Starter (neighborhood-resource-interface-block-x3945):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Block 07 — Neighborhood Resource Exchange</title>
<style>
:root {
--ink: #17342f;
--muted: #60746e;
--paper: rgba(248, 250, 245, 0.92);
--paper-strong: rgba(252, 253, 249, 0.98);
--line: rgba(37, 68, 60, 0.14);
--shadow: 0 16px 50px rgba(24, 51, 44, 0.16);
--water: #299bd0;
--energy: #e8ae35;
--food: #48a66b;
--waste: #e36b59;
}
* { box-sizing: border-box; }
html, body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
color: var(--ink);
background: #dce7e0;
}
button, input { font: inherit; }
button {
color: inherit;
-webkit-tap-highlight-color: transparent;
}
#scene {
position: fixed;
inset: 0;
}
#scene canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
cursor: grab;
}
#scene canvas:active { cursor: grabbing; }
#labels {
position: fixed;
inset: 0;
z-index: 3;
pointer-events: none;
overflow: hidden;
}
.port-label,
.node-label,
.building-label {
position: absolute;
transform: translate(-50%, -50%);
white-space: nowrap;
transition: opacity 160ms ease, background 160ms ease,
border-color 160ms ease, transform 160ms ease;
}
.port-label {
pointer-events: auto;
display: flex;
align-items: center;
gap: 6px;
padding: 6px 9px;
border: 1px solid rgba(255,255,255,.7);
border-radius: 999px;
background: rgba(250,252,248,.92);
box-shadow: 0 5px 18px rgba(22,52,45,.16);
color: #29443e;
font-size: 10px;
font-weight: 800;
letter-spacing: .04em;
cursor: pointer;
}
.port-label::after {
content: attr(data-unit);
padding-left: 6px;
border-left: 1px solid var(--line);
color: var(--muted);
font-weight: 700;
}
.port-label .dot {
width: 8px;
height: 8px;
border-radius: 50%;
background: var(--flow-color);
box-shadow: 0 0 0 3px color-mix(in srgb, var(--flow-color), transparent 76%);
}
.port-label.active {
border-color: var(--flow-color);
background: #fff;
transform: translate(-50%, -50%) scale(1.06);
}
.node-label,
.building-label {
padding: 5px 8px;
border-radius: 7px;
background: rgba(244,248,243,.84);
border: 1px solid rgba(255,255,255,.68);
box-shadow: 0 4px 12px rgba(29,59,51,.10);
color: #3f5a53;
font-size: 9px;
font-weight: 800;
letter-spacing: .08em;
text-transform: uppercase;
}
.building-label {
color: #17342f;
background: rgba(252,253,249,.94);
font-size: 10px;
}
.topbar {
position: fixed;
top: 18px;
left: 20px;
right: 20px;
z-index: 8;
display: flex;
align-items: flex-start;
justify-content: space-between;
pointer-events: none;
}
.brand {
display: flex;
align-items: center;
gap: 12px;
padding: 10px 13px 10px 10px;
border: 1px solid rgba(255,255,255,.75);
border-radius: 14px;
background: rgba(248,251,246,.86);
box-shadow: 0 10px 32px rgba(27,54,47,.12);
backdrop-filter: blur(12px);
}
.brand-mark {
display: grid;
place-items: center;
width: 38px;
height: 38px;
border-radius: 10px;
background: #183e36;
color: #e9f5e6;
font-weight: 900;
letter-spacing: -.08em;
}
.brand strong {
display: block;
font-size: 13px;
letter-spacing: .02em;
}
.brand span {
display: block;
margin-top: 2px;
color: var(--muted);
font-size: 10px;
letter-spacing: .08em;
text-transform: uppercase;
}
.live-badge {
display: flex;
align-items: center;
gap: 7px;
padding: 9px 12px;
border: 1px solid rgba(255,255,255,.75);
border-radius: 999px;
background: rgba(248,251,246,.88);
box-shadow: 0 10px 30px rgba(27,54,47,.12);
font-size: 10px;
font-weight: 850;
letter-spacing: .1em;
text-transform: uppercase;
}
.live-badge i {
width: 7px;
height: 7px;
border-radius: 50%;
background: #4bab72;
box-shadow: 0 0 0 4px rgba(75,171,114,.15);
animation: livePulse 1.8s infinite;
}
@keyframes livePulse {
50% { box-shadow: 0 0 0 7px rgba(75,171,114,0); }
}
.panel {
position: fixed;
z-index: 7;
border: 1px solid rgba(255,255,255,.76);
border-radius: 18px;
background: var(--paper);
box-shadow: var(--shadow);
backdrop-filter: blur(16px);
overflow: hidden;
}
#left-panel {
top: 86px;
left: 20px;
bottom: 92px;
width: 326px;
overflow-y: auto;
scrollbar-width: thin;
scrollbar-color: #b7c7bf transparent;
}
.panel-intro {
padding: 20px 20px 16px;
border-bottom: 1px solid var(--line);
}
.eyebrow {
margin: 0 0 8px;
color: #668078;
font-size: 9px;
font-weight: 900;
letter-spacing: .16em;
text-transform: uppercase;
}
h1 {
margin: 0;
max-width: 270px;
font-size: 25px;
line-height: 1.04;
letter-spacing: -.04em;
}
.intro-copy {
margin: 11px 0 0;
color: var(--muted);
font-size: 11px;
line-height: 1.55;
}
.flow-list {
display: grid;
gap: 8px;
padding: 14px;
}
.flow-card {
position: relative;
display: grid;
grid-template-columns: 38px 1fr auto;
align-items: center;
gap: 10px;
width: 100%;
padding: 11px;
text-align: left;
border: 1px solid transparent;
border-radius: 12px;
background: rgba(255,255,255,.55);
cursor: pointer;
transition: transform 140ms ease, border-color 140ms ease,
background 140ms ease, box-shadow 140ms ease;
}
.flow-card:hover {
transform: translateY(-1px);
border-color: rgba(48,75,68,.18);
background: rgba(255,255,255,.88);
}
.flow-card.active {
border-color: var(--flow-color);
background: #fff;
box-shadow: 0 7px 20px rgba(30,58,51,.10);
}
.flow-card.active::before {
content: "";
position: absolute;
left: -1px;
top: 13px;
bottom: 13px;
width: 3px;
border-radius: 0 3px 3px 0;
background: var(--flow-color);
}
.flow-icon {
display: grid;
place-items: center;
width: 36px;
height: 36px;
border-radius: 10px;
color: var(--flow-color);
background: color-mix(in srgb, var(--flow-color), white 85%);
font-size: 17px;
font-weight: 900;
}
.flow-name {
display: block;
font-size: 12px;
font-weight: 850;
line-height: 1.2;
}
.flow-subtitle {
display: block;
margin-top: 3px;
color: var(--muted);
font-size: 9px;
}
.flow-reading {
text-align: right;
}
.flow-reading strong {
display: block;
font-size: 13px;
line-height: 1;
font-variant-numeric: tabular-nums;
}
.flow-reading span {
display: block;
margin-top: 4px;
color: var(--muted);
font-size: 9px;
font-weight: 800;
}
.unit-lock {
margin: 0 14px 14px;
padding: 13px;
border-radius: 12px;
background: #e9f0e8;
border: 1px solid rgba(54,91,78,.12);
}
.unit-lock-head {
display: flex;
align-items: center;
justify-content: space-between;
gap: 8px;
}
.unit-lock strong {
font-size: 10px;
letter-spacing: .06em;
text-transform: uppercase;
}
.audit-pill {
padding: 4px 7px;
border-radius: 999px;
background: #d5e7d8;
color: #287145;
font-size: 8px;
font-weight: 900;
letter-spacing: .08em;
text-transform: uppercase;
}
.unit-lock p {
margin: 8px 0 11px;
color: #587068;
font-size: 10px;
line-height: 1.5;
}
.audit-grid {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 6px;
}
.audit-item {
display: flex;
justify-content: space-between;
gap: 6px;
padding: 6px 7px;
border-radius: 7px;
background: rgba(255,255,255,.58);
font-size: 9px;
}
.audit-item span { color: var(--muted); }
.audit-item b { font-weight: 900; }
#inspector {
top: 86px;
right: 20px;
width: 286px;
padding: 18px;
}
.inspector-top {
display: flex;
align-items: flex-start;
justify-content: space-between;
gap: 12px;
}
#selected-title {
margin: 0;
font-size: 20px;
letter-spacing: -.03em;
}
#selected-subtitle {
margin: 4px 0 0;
color: var(--muted);
font-size: 10px;
}
.unit-badge {
flex: 0 0 auto;
padding: 6px 8px;
border-radius: 8px;
color: #fff;
background: var(--selected-color, var(--water));
font-size: 11px;
font-weight: 900;
letter-spacing: .04em;
}
.big-reading {
margin: 18px 0 14px;
padding: 14px;
border-radius: 12px;
background: #edf2eb;
}
.big-reading span {
display: block;
color: var(--muted);
font-size: 9px;
font-weight: 800;
letter-spacing: .08em;
text-transform: uppercase;
}
#selected-value {
display: block;
margin-top: 5px;
font-size: 27px;
font-weight: 850;
letter-spacing: -.05em;
font-variant-numeric: tabular-nums;
}
.route-map {
display: grid;
grid-template-columns: 1fr 28px 1fr;
align-items: center;
gap: 5px;
margin: 0 0 14px;
}
.route-node {
min-width: 0;
padding: 10px;
border: 1px solid var(--line);
border-radius: 10px;
background: rgba(255,255,255,.54);
}
.route-node span {
display: block;
color: var(--muted);
font-size: 8px;
font-weight: 800;
letter-spacing: .07em;
text-transform: uppercase;
}
.route-node b {
display: block;
margin-top: 4px;
overflow: hidden;
font-size: 10px;
text-overflow: ellipsis;
white-space: nowrap;
}
.route-arrow {
color: var(--selected-color, var(--water));
text-align: center;
font-size: 20px;
font-weight: 900;
}
.integrity {
display: flex;
align-items: flex-start;
gap: 9px;
padding: 11px;
border: 1px solid rgba(54,124,81,.17);
border-radius: 10px;
background: #e8f1e7;
color: #2d6444;
font-size: 9px;
line-height: 1.45;
}
.integrity b { display: block; margin-bottom: 2px; }
.shape-key {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
margin-top: 12px;
color: var(--muted);
font-size: 9px;
}
.shape-key strong { color: var(--ink); }
#camera-controls {
position: fixed;
z-index: 8;
right: 20px;
bottom: 92px;
display: flex;
gap: 6px;
padding: 6px;
border: 1px solid rgba(255,255,255,.74);
border-radius: 12px;
background: rgba(248,251,246,.88);
box-shadow: 0 8px 28px rgba(28,55,48,.14);
backdrop-filter: blur(12px);
}
.icon-button {
display: grid;
place-items: center;
min-width: 34px;
height: 34px;
padding: 0 9px;
border: 0;
border-radius: 8px;
background: transparent;
font-size: 11px;
font-weight: 800;
cursor: pointer;
}
.icon-button:hover { background: #e5ede4; }
.icon-button.primary { background: #183e36; color: white; }
#timeline {
position: fixed;
z-index: 8;
left: 50%;
bottom: 18px;
width: min(610px, calc(100vw - 40px));
transform: translateX(-50%);
display: grid;
grid-template-columns: auto 1fr auto;
align-items: center;
gap: 14px;
padding: 10px 13px;
border: 1px solid rgba(255,255,255,.78);
border-radius: 15px;
background: rgba(249,251,247,.92);
box-shadow: var(--shadow);
backdrop-filter: blur(14px);
}
#play-button {
display: grid;
place-items: center;
width: 36px;
height: 36px;
border: 0;
border-radius: 10px;
background: #183e36;
color: #fff;
font-size: 13px;
cursor: pointer;
}
.time-track {
display: grid;
grid-template-columns: 1fr auto;
align-items: center;
gap: 12px;
}
#time-slider {
width: 100%;
accent-color: #245a4b;
cursor: pointer;
}
#time-readout {
min-width: 48px;
color: var(--ink);
font-size: 11px;
font-weight: 900;
font-variant-numeric: tabular-nums;
}
#speed-button {
min-width: 42px;
padding: 7px 8px;
border: 1px solid var(--line);
border-radius: 8px;
background: rgba(255,255,255,.68);
font-size: 9px;
font-weight: 900;
cursor: pointer;
}
#hint {
position: fixed;
left: 50%;
bottom: 84px;
z-index: 6;
transform: translateX(-50%);
padding: 6px 10px;
border-radius: 999px;
background: rgba(29,61,53,.72);
color: rgba(255,255,255,.88);
font-size: 9px;
letter-spacing: .03em;
pointer-events: none;
transition: opacity 400ms ease;
}
#toast {
position: fixed;
z-index: 20;
left: 50%;
top: 80px;
transform: translate(-50%, -12px);
max-width: min(440px, calc(100vw - 40px));
padding: 11px 15px;
border-radius: 11px;
background: rgba(23,52,47,.94);
color: white;
box-shadow: 0 12px 35px rgba(20,40,35,.25);
font-size: 10px;
line-height: 1.45;
opacity: 0;
pointer-events: none;
transition: opacity 180ms ease, transform 180ms ease;
}
#toast.show {
opacity: 1;
transform: translate(-50%, 0);
}
#loading {
position: fixed;
inset: 0;
z-index: 50;
display: grid;
place-items: center;
background: #dce7e0;
transition: opacity 400ms ease;
}
#loading.hidden { opacity: 0; pointer-events: none; }
.loader {
text-align: center;
color: #25473f;
}
.loader-mark {
width: 46px;
height: 46px;
margin: 0 auto 12px;
border: 5px solid rgba(34,77,66,.15);
border-top-color: #285f50;
border-radius: 50%;
animation: spin .8s linear infinite;
}
.loader strong {
display: block;
font-size: 12px;
}
.loader span {
display: block;
margin-top: 5px;
color: #70837d;
font-size: 9px;
letter-spacing: .1em;
text-transform: uppercase;
}
@keyframes spin { to { transform: rotate(360deg); } }
@media (max-width: 1000px) {
#inspector { display: none; }
#left-panel { width: 300px; }
}
@media (max-width: 680px) {
.topbar { top: 10px; left: 10px; right: 10px; }
.brand span { display: none; }
.live-badge { padding: 9px; }
#left-panel {
top: auto;
left: 10px;
right: 10px;
bottom: 82px;
width: auto;
max-height: 38vh;
}
.panel-intro { display: none; }
.flow-list {
grid-template-columns: repeat(4, minmax(0, 1fr));
padding: 9px;
}
.flow-card {
grid-template-columns: 1fr;
justify-items: center;
gap: 4px;
padding: 8px 4px;
text-align: center;
}
.flow-name { font-size: 9px; }
.flow-subtitle, .flow-reading strong { display: none; }
.flow-reading span { margin: 0; font-size: 8px; }
.flow-icon { width: 30px; height: 30px; }
.unit-lock { display: none; }
#camera-controls { bottom: 82px; right: 10px; }
#hint { display: none; }
#timeline { bottom: 10px; }
.node-label { display: none; }
}
</style>
</head>
<body>
<div id="scene"></div>
<div id="labels"></div>
<header class="topbar">
<div class="brand">
<div class="brand-mark">07</div>
<div>
<strong>Neighborhood Resource Exchange</strong>
<span>Block 07 · operational model</span>
</div>
</div>
<div class="live-badge"><i></i> Live simulation</div>
</header>
<aside id="left-panel" class="panel">
<div class="panel-intro">
<p class="eyebrow">Urban systems overview</p>
<h1>Four flows. One neighborhood.</h1>
<p class="intro-copy">
Select a channel to trace its source-to-port path. Every channel keeps its
native unit; the dashboard never combines unlike quantities.
</p>
</div>
<div class="flow-list">
<button class="flow-card active" data-flow="water" style="--flow-color: var(--water)">
<span class="flow-icon">◉</span>
<span>
<span class="flow-name">Water</span>
<span class="flow-subtitle">Reclaimed mains → port W1</span>
</span>
<span class="flow-reading">
<strong id="value-water">12.6</strong>
<span>m³ / day</span>
</span>
</button>
<button class="flow-card" data-flow="energy" style="--flow-color: var(--energy)">
<span class="flow-icon">◆</span>
<span>
<span class="flow-name">Electricity</span>
<span class="flow-subtitle">Solar commons → port E1</span>
</span>
<span class="flow-reading">
<strong id="value-energy">48.0</strong>
<span>kWh / day</span>
</span>
</button>
<button class="flow-card" data-flow="food" style="--flow-color: var(--food)">
<span class="flow-icon">▲</span>
<span>
<span class="flow-name">Food</span>
<span class="flow-subtitle">Urban garden → port F1</span>
</span>
<span class="flow-reading">
<strong id="value-food">18.5</strong>
<span>kg / day</span>
</span>
</button>
<button class="flow-card" data-flow="waste" style="--flow-color: var(--waste)">
<span class="flow-icon">■</span>
<span>
<span class="flow-name">Waste</span>
<span class="flow-subtitle">Port S1 → recovery hub</span>
</span>
<span class="flow-reading">
<strong id="value-waste">0.032</strong>
<span>t / day</span>
</span>
</button>
</div>
<div class="unit-lock">
<div class="unit-lock-head">
<strong>Unit integrity</strong>
<span class="audit-pill">4 / 4 locked</span>
</div>
<p>
Each route requires matching units at both endpoints. No shared total is
calculated across volume, energy, food mass, and waste mass.
</p>
<div class="audit-grid">
<div class="audit-item"><span>Water</span><b>m³</b></div>
<div class="audit-item"><span>Energy</span><b>kWh</b></div>
<div class="audit-item"><span>Food</span><b>kg</b></div>
<div class="audit-item"><span>Waste</span><b>t</b></div>
</div>
</div>
</aside>
<aside id="inspector" class="panel">
<div class="inspector-top">
<div>
<p class="eyebrow">Channel inspector</p>
<h2 id="selected-title">Potable water</h2>
<p id="selected-subtitle">Reclaimed distribution loop</p>
</div>
<div id="selected-unit" class="unit-badge">m³</div>
</div>
<div class="big-reading">
<span>Current modeled flow</span>
<strong id="selected-value">12.6 m³ / day</strong>
</div>
<div class="route-map">
<div class="route-node">
<span>Source</span>
<b id="selected-source">Waterworks</b>
</div>
<div class="route-arrow">→</div>
<div class="route-node">
<span>Destination</span>
<b id="selected-target">Block 07 · W1</b>
</div>
</div>
<div class="integrity">
<span>✓</span>
<div>
<b>Endpoint unit check passed</b>
<span id="integrity-copy">Source m³ → port m³. No conversion or mixed-unit total.</span>
</div>
</div>
<div class="shape-key">
<span>Particle marker</span>
<strong id="shape-name">Icosahedron · volume</strong>
</div>
</aside>
<div id="camera-controls">
<button id="camera-home" class="icon-button" title="Reset camera">↺</button>
<button id="camera-top" class="icon-button" title="Top view">Top</button>
<button id="zoom-out" class="icon-button" title="Zoom out">−</button>
<button id="zoom-in" class="icon-button" title="Zoom in">+</button>
</div>
<div id="hint">Drag to orbit · scroll to zoom · click a route or port</div>
<div id="timeline">
<button id="play-button" aria-label="Pause simulation">Ⅱ</button>
<div class="time-track">
<input id="time-slider" type="range" min="0" max="23.99" step="0.05" value="10" />
<span id="time-readout">10:00</span>
</div>
<button id="speed-button">1×</button>
</div>
<div id="toast"></div>
<div id="loading">
<div class="loader">
<div class="loader-mark"></div>
<strong>Assembling Block 07</strong>
<span>Low-poly resource model</span>
</div>
</div>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// -------------------------------------------------------------------------
// Unit-safe flow registry. Endpoints are validated before any route is built.
// -------------------------------------------------------------------------
const UNIT_GROUPS = Object.freeze({
VOLUME: "volume",
ENERGY: "energy",
FOOD_MASS: "food-mass",
WASTE_MASS: "waste-mass"
});
function deepFreeze(value) {
Object.values(value).forEach((child) => {
if (child && typeof child === "object" && !Object.isFrozen(child)) {
deepFreeze(child);
}
});
return Object.freeze(value);
}
const FLOWS = deepFreeze([
{
id: "water",
name: "Potable water",
shortName: "Water",
subtitle: "Reclaimed distribution loop",
unit: "m³",
dimension: UNIT_GROUPS.VOLUME,
decimals: 1,
base: 12.6,
color: 0x299bd0,
css: "#299bd0",
shape: "Icosahedron · volume",
source: { name: "Waterworks", unit: "m³", dimension: UNIT_GROUPS.VOLUME },
target: { name: "Block 07 · port W1", unit: "m³", dimension: UNIT_GROUPS.VOLUME },
port: "water",
path: [
[-15.6, 4.7, -9.4],
[-13.0, 7.0, -8.1],
[-9.2, 6.1, -2.0],
[-7.0, 3.4, 3.9],
[-4.5, 1.45, 4.22]
]
},
{
id: "energy",
name: "Electricity",
shortName: "Electricity",
subtitle: "Community solar distribution",
unit: "kWh",
dimension: UNIT_GROUPS.ENERGY,
decimals: 1,
base: 48.0,
color: 0xe8ae35,
css: "#e8ae35",
shape: "Octahedron · energy",
source: { name: "Solar commons", unit: "kWh", dimension: UNIT_GROUPS.ENERGY },
target: { name: "Block 07 · port E1", unit: "kWh", dimension: UNIT_GROUPS.ENERGY },
port: "energy",
path: [
[-13.2, 1.45, 7.8],
[-11.0, 2.8, 8.2],
[-7.4, 5.8, 6.4],
[-4.1, 5.5, 4.5],
[-1.5, 4.05, 4.22]
]
},
{
id: "food",
name: "Food supply",
shortName: "Food",
subtitle: "Fresh-produce collection line",
unit: "kg",
dimension: UNIT_GROUPS.FOOD_MASS,
decimals: 1,
base: 18.5,
color: 0x48a66b,
css: "#48a66b",
shape: "Tetrahedron · food mass",
source: { name: "Urban garden", unit: "kg", dimension: UNIT_GROUPS.FOOD_MASS },
target: { name: "Block 07 · port F1", unit: "kg", dimension: UNIT_GROUPS.FOOD_MASS },
port: "food",
path: [
[13.0, 1.2, -9.0],
[10.7, 3.4, -8.1],
[7.0, 4.5, -4.8],
[4.2, 3.0, 1.5],
[1.5, 1.45, 4.22]
]
},
{
id: "waste",
name: "Recovered waste",
shortName: "Waste",
subtitle: "Separated material recovery",
unit: "t",
dimension: UNIT_GROUPS.WASTE_MASS,
decimals: 3,
base: 0.032,
color: 0xe36b59,
css: "#e36b59",
shape: "Cube · waste mass",
source: { name: "Block 07 · port S1", unit: "t", dimension: UNIT_GROUPS.WASTE_MASS },
target: { name: "Recovery hub", unit: "t", dimension: UNIT_GROUPS.WASTE_MASS },
port: "waste",
path: [
[4.5, 1.45, 4.22],
[7.0, 3.1, 6.0],
[10.3, 3.4, 8.1],
[14.4, 1.35, 8.0]
]
}
]);
function validateFlow(flow) {
const valid =
flow.source.unit === flow.unit &&
flow.target.unit === flow.unit &&
flow.source.dimension === flow.dimension &&
flow.target.dimension === flow.dimension;
if (!valid) {
throw new Error(`Unit mismatch rejected for flow: ${flow.id}`);
}
return true;
}
FLOWS.forEach(validateFlow);
const flowById = new Map(FLOWS.map((flow) => [flow.id, flow]));
// -------------------------------------------------------------------------
// Renderer and scene
// -------------------------------------------------------------------------
const sceneHost = document.querySelector("#scene");
const labelHost = document.querySelector("#labels");
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xdce7e0);
scene.fog = new THREE.Fog(0xdce7e0, 36, 78);
const camera = new THREE.PerspectiveCamera(
42,
window.innerWidth / window.innerHeight,
0.1,
160
);
const renderer = new THREE.WebGLRenderer({ antialias: true, powerPreference: "high-performance" });
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.05;
sceneHost.appendChild(renderer.domElement);
const hemi = new THREE.HemisphereLight(0xf7fff4, 0x61736b, 2.25);
scene.add(hemi);
const sun = new THREE.DirectionalLight(0xfff6db, 3.1);
sun.position.set(-17, 28, 16);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -30;
sun.shadow.camera.right = 30;
sun.shadow.camera.top = 25;
sun.shadow.camera.bottom = -25;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 70;
sun.shadow.bias = -0.0008;
scene.add(sun);
const fill = new THREE.DirectionalLight(0xb9d8dc, 0.75);
fill.position.set(22, 12, -20);
scene.add(fill);
const pickables = [];
const labels = [];
const routes = new Map();
const ports = new Map();
function standardMaterial(color, options = {}) {
return new THREE.MeshStandardMaterial({
color,
roughness: 0.82,
metalness: 0.02,
flatShading: true,
...options
});
}
function addBox(parent, width, height, depth, color, x, y, z, options = {}) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(width, height, depth),
standardMaterial(color, options.material || {})
);
mesh.position.set(x, y, z);
mesh.castShadow = options.castShadow !== false;
mesh.receiveShadow = options.receiveShadow !== false;
parent.add(mesh);
return mesh;
}
function addCylinder(
parent,
radiusTop,
radiusBottom,
height,
radialSegments,
color,
x,
y,
z,
options = {}
) {
const mesh = new THREE.Mesh(
new THREE.CylinderGeometry(radiusTop, radiusBottom, height, radialSegments),
standardMaterial(color, options.material || {})
);
mesh.position.set(x, y, z);
mesh.castShadow = options.castShadow !== false;
mesh.receiveShadow = options.receiveShadow !== false;
parent.add(mesh);
return mesh;
}
function addCone(parent, radius, height, segments, color, x, y, z) {
const mesh = new THREE.Mesh(
new THREE.ConeGeometry(radius, height, segments),
standardMaterial(color)
);
mesh.position.set(x, y, z);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function addLabel(text, position, className, options = {}) {
const element = document.createElement(options.interactive ? "button" : "div");
element.className = className;
if (options.interactive && options.color) {
element.style.setProperty("--flow-color", options.color);
}
if (options.dot) {
const dot = document.createElement("span");
dot.className = "dot";
element.appendChild(dot);
}
element.appendChild(document.createTextNode(text));
if (options.unit) element.dataset.unit = options.unit;
if (options.interactive) {
element.type = "button";
element.addEventListener("click", (event) => {
event.stopPropagation();
selectFlow(options.flowId);
});
}
labelHost.appendChild(element);
labels.push({
element,
world: position.clone(),
visible: true,
opacity: options.opacity ?? 1
});
return element;
}
function addNodeHit(position, size, name, detail) {
const material = new THREE.MeshBasicMaterial({
transparent: true,
opacity: 0,
depthWrite: false
});
const mesh = new THREE.Mesh(new THREE.BoxGeometry(...size), material);
mesh.position.copy(position);
mesh.userData = { kind: "node", name, detail };
scene.add(mesh);
pickables.push(mesh);
return mesh;
}
// -------------------------------------------------------------------------
// Neighborhood block
// -------------------------------------------------------------------------
const neighborhood = new THREE.Group();
scene.add(neighborhood);
addBox(neighborhood, 46, 0.9, 34, 0xa9c0b3, 0, -0.55, 0);
addBox(neighborhood, 44.5, 0.08, 3.4, 0x687772, 0, -0.03, 10.6);
addBox(neighborhood, 3.2, 0.08, 29, 0x687772, -10.6, -0.02, -1.8);
addBox(neighborhood, 3.2, 0.08, 29, 0x687772, 12.5, -0.02, -1.8);
addBox(neighborhood, 43, 0.08, 2.8, 0x687772, 0, -0.02, -14.6);
addBox(neighborhood, 15.7, 0.12, 13.8, 0xcbd5c8, 0.6, 0.04, 0.2);
// Road markings.
for (let x = -20; x <= 20; x += 4) {
addBox(neighborhood, 1.4, 0.015, 0.08, 0xd8d7c5, x, 0.035, 10.6, { castShadow: false });
}
for (let z = -13; z <= 8; z += 4) {
addBox(neighborhood, 0.08, 0.015, 1.35, 0xd8d7c5, -10.6, 0.035, z, { castShadow: false });
addBox(neighborhood, 0.08, 0.015, 1.35, 0xd8d7c5, 12.5, 0.035, z, { castShadow: false });
}
// Central mixed-use building.
const hub = new THREE.Group();
neighborhood.add(hub);
addBox(hub, 13.2, 0.3, 9.2, 0x9dafa0, 0, 0.17, 0);
addBox(hub, 12, 4.7, 8, 0xe9e6d7, 0, 2.5, 0);
addBox(hub, 12.4, 0.36, 8.4, 0x2e6258, 0, 4.98, 0);
addBox(hub, 7.2, 1.25, 6.1, 0xe2e1d3, -1.9, 5.75, -0.2);
addBox(hub, 7.5, 0.25, 6.35, 0x39766a, -1.9, 6.48, -0.2);
// Rooftop garden and solar canopy.
addBox(hub, 3.2, 0.16, 2.1, 0x719267, 3.6, 5.25, 0.7);
for (let i = 0; i < 4; i++) {
addCone(hub, 0.25, 0.65, 5, 0x4d8e58, 2.65 + i * 0.62, 5.62, 0.55);
}
for (let i = 0; i < 3; i++) {
const panel = addBox(
hub,
1.55,
0.08,
0.95,
0x335f72,
-4.5 + i * 1.7,
6.79,
-0.45,
{ material: { metalness: 0.25, roughness: 0.45 } }
);
panel.rotation.x = -0.22;
}
// Front facade.
addBox(hub, 1.55, 2.35, 0.12, 0x315b57, 0, 1.35, 4.04);
addBox(hub, 0.12, 2.45, 0.16, 0xd2a668, 0, 1.35, 4.1);
addBox(hub, 2.6, 0.14, 0.8, 0xbc8050, 0, 2.65, 4.32);
const windowPositions = [
[-4.4, 3.35], [-2.7, 3.35], [2.0, 3.35], [4.0, 3.35],
[-4.2, 1.75], [2.2, 1.75], [4.2, 1.75]
];
windowPositions.forEach(([x, y]) => {
addBox(hub, 0.95, 1.05, 0.09, 0x6c9ca4, x, y, 4.045, {
material: { metalness: 0.18, roughness: 0.38, emissive: 0x183b3e, emissiveIntensity: 0.18 }
});
});
// Side windows.
for (let z = -2.5; z <= 2.5; z += 1.65) {
addBox(hub, 0.08, 1.05, 0.95, 0x6c9ca4, -6.04, 3.2, z, {
material: { metalness: 0.15, roughness: 0.4 }
});
addBox(hub, 0.08, 1.05, 0.95, 0x6c9ca4, 6.04, 3.2, z, {
material: { metalness: 0.15, roughness: 0.4 }
});
}
// Selection halo.
const haloMaterial = new THREE.MeshBasicMaterial({
color: 0x2a8e75,
transparent: true,
opacity: 0.22,
depthWrite: false
});
const halo = new THREE.Mesh(
new THREE.TorusGeometry(8.15, 0.055, 4, 64),
haloMaterial
);
halo.rotation.x = Math.PI / 2;
halo.position.set(0, 0.14, 0);
scene.add(halo);
// Ports are modeled and individually labeled.
const portDefinitions = {
water: { position: [-4.5, 1.45, 4.15], label: "W1 · Water" },
energy: { position: [-1.5, 4.05, 4.15], label: "E1 · Electricity" },
food: { position: [1.5, 1.45, 4.15], label: "F1 · Food" },
waste: { position: [4.5, 1.45, 4.15], label: "S1 · Waste" }
};
Object.entries(portDefinitions).forEach(([id, definition]) => {
const flow = flowById.get(id);
const group = new THREE.Group();
group.position.set(...definition.position);
scene.add(group);
const collar = new THREE.Mesh(
new THREE.CylinderGeometry(0.36, 0.36, 0.12, 10),
standardMaterial(0xe9eee6)
);
collar.rotation.x = Math.PI / 2;
collar.castShadow = true;
group.add(collar);
const ring = new THREE.Mesh(
new THREE.TorusGeometry(0.34, 0.075, 6, 18),
new THREE.MeshBasicMaterial({ color: flow.color })
);
ring.position.z = 0.08;
group.add(ring);
const markerMaterial = standardMaterial(flow.color, {
emissive: flow.color,
emissiveIntensity: 0.18,
roughness: 0.45
});
const marker = new THREE.Mesh(
id === "energy"
? new THREE.OctahedronGeometry(0.18, 0)
: id === "food"
? new THREE.TetrahedronGeometry(0.2, 0)
: id === "waste"
? new THREE.BoxGeometry(0.23, 0.23, 0.23)
: new THREE.IcosahedronGeometry(0.18, 0),
markerMaterial
);
marker.position.z = 0.16;
marker.userData = { kind: "port", flowId: id };
group.add(marker);
pickables.push(marker);
const labelPosition = new THREE.Vector3(
definition.position[0],
definition.position[1] + 0.16,
definition.position[2] + 0.25
);
const labelElement = addLabel(
definition.label,
labelPosition,
"port-label",
{ interactive: true, dot: true, unit: flow.unit, color: flow.css, flowId: id }
);
labelElement.classList.toggle("active", id === "water");
ports.set(id, { group, marker, material: markerMaterial, labelElement });
});
addLabel("Block 07 · Mixed-use hub", new THREE.Vector3(-1.9, 7.15, -0.2), "building-label");
// -------------------------------------------------------------------------
// Source and destination landmarks
// -------------------------------------------------------------------------
function makeTree(x, z, scale = 1) {
const group = new THREE.Group();
group.position.set(x, 0, z);
group.scale.setScalar(scale);
neighborhood.add(group);
addCylinder(group, 0.16, 0.22, 1.5, 6, 0x785a3d, 0, 0.75, 0);
addCone(group, 0.9, 1.8, 6, 0x4f8057, 0, 2.05, 0);
addCone(group, 0.68, 1.45, 6, 0x5e9462, 0, 2.72, 0);
}
function makeSmallBuilding(x, z, width, depth, height, color, roofColor) {
const group = new THREE.Group();
group.position.set(x, 0, z);
neighborhood.add(group);
addBox(group, width, height, depth, color, 0, height / 2, 0);
addBox(group, width + 0.25, 0.24, depth + 0.25, roofColor, 0, height + 0.12, 0);
const windowCount = Math.max(2, Math.floor(width / 1.5));
for (let i = 0; i < windowCount; i++) {
const wx = -width / 2 + 0.8 + i * ((width - 1.6) / Math.max(1, windowCount - 1));
addBox(group, 0.72, 0.85, 0.08, 0x7399a0, wx, height * 0.58, depth / 2 + 0.045, {
material: { metalness: 0.12, roughness: 0.45 }
});
}
return group;
}
makeSmallBuilding(-19.7, 1.3, 4.2, 5.2, 4.1, 0xded8c9, 0x8d6650);
makeSmallBuilding(-19.2, -5.8, 4.8, 4.4, 5.2, 0xd9dfd0, 0x4d7166);
makeSmallBuilding(20.0, -1.3, 4.5, 5.0, 4.8, 0xe2d6c5, 0x98664d);
makeSmallBuilding(5.8, -12.5, 5.2, 3.2, 3.3, 0xd5d9ca, 0x587b6b);
// Waterworks tower.
const waterworks = new THREE.Group();
waterworks.position.set(-17, 0, -10);
neighborhood.add(waterworks);
[[-0.9, -0.9], [0.9, -0.9], [-0.9, 0.9], [0.9, 0.9]].forEach(([x, z]) => {
addCylinder(waterworks, 0.1, 0.16, 4.1, 6, 0x70817c, x, 2.05, z);
});
addCylinder(waterworks, 1.45, 1.45, 2.4, 8, 0x8fc0c5, 0, 5.25, 0);
addCone(waterworks, 1.62, 1.05, 8, 0x477c7c, 0, 6.97, 0);
addCylinder(waterworks, 0.15, 0.15, 3.1, 6, 0x5e7470, 1.05, 4.6, 0.65);
addCylinder(waterworks, 0.13, 0.13, 1.25, 6, 0x5e7470, 1.05, 3.45, 1.1, {
material: {}
});
const pipe = addCylinder(waterworks, 0.13, 0.13, 1.25, 6, 0x5e7470, 1.05, 3.45, 1.1);
pipe.rotation.x = Math.PI / 2;
addLabel("Waterworks", new THREE.Vector3(-17, 8.0, -10), "node-label");
addNodeHit(new THREE.Vector3(-17, 4.5, -10), [3.4, 8, 3.4], "Waterworks", "Reclaimed water supply");
// Solar commons.
const solar = new THREE.Group();
solar.position.set(-15, 0, 8);
neighborhood.add(solar);
addBox(solar, 7.2, 0.12, 3.8, 0x94a58e, 0, 0.09, 0);
for (let i = 0; i < 4; i++) {
const x = -2.55 + i * 1.7;
addCylinder(solar, 0.08, 0.1, 1.35, 6, 0x657873, x, 0.72, 0);
const panel = addBox(solar, 1.55, 0.12, 2.35, 0x315a6c, x, 1.4, 0, {
material: { metalness: 0.3, roughness: 0.42 }
});
panel.rotation.x = -0.28;
addBox(solar, 0.06, 0.06, 2.45, 0xbfc6b8, x, 1.48, 0);
}
addBox(solar, 1.4, 1.1, 1.2, 0xe3dfce, 3.0, 0.65, -1.4);
addLabel("Solar commons", new THREE.Vector3(-15, 3.0, 8), "node-label");
addNodeHit(new THREE.Vector3(-15, 1.2, 8), [7, 2.4, 3.5], "Solar commons", "Local photovoltaic generation");
// Urban garden.
const garden = new THREE.Group();
garden.position.set(14, 0, -10);
neighborhood.add(garden);
addBox(garden, 8.2, 0.16, 5.5, 0x9f8062, 0, 0.12, 0);
for (let row = -2; row <= 2; row++) {
addBox(garden, 7.1, 0.15, 0.65, 0x735840, 0, 0.25, row * 1.05);
for (let i = 0; i < 8; i++) {
addCone(garden, 0.25, 0.68, 5, row % 2 ? 0x4e8b56 : 0x6ea35f, -3.0 + i * 0.86, 0.62, row * 1.05);
}
}
addBox(garden, 1.8, 1.35, 1.5, 0xe2d9bd, 3.3, 0.75, 2.0);
addLabel("Urban garden", new THREE.Vector3(14, 2.2, -10), "node-label");
addNodeHit(new THREE.Vector3(14, 1.0, -10), [8, 2, 5.5], "Urban garden", "Fresh produce collection");
// Recovery hub.
const recovery = new THREE.Group();
recovery.position.set(16, 0, 8);
neighborhood.add(recovery);
addBox(recovery, 5.7, 3.2, 4.2, 0xddd5c2, 0, 1.65, 0);
addBox(recovery, 6.0, 0.3, 4.5, 0x426a5d, 0, 3.4, 0);
addBox(recovery, 4.8, 0.42, 0.12, 0xe36b59, 0, 2.1, 2.14);
addBox(recovery, 1.45, 2.05, 0.1, 0x526861, -1.1, 1.1, 2.14);
addBox(recovery, 1.45, 2.05, 0.1, 0x526861, 1.1, 1.1, 2.14);
const binColors = [0x3d8c60, 0xd4a53e, 0x4d83a2];
binColors.forEach((color, i) => {
addBox(recovery, 0.75, 1.0, 0.75, color, -3.1 + i * 0.92, 0.55, 2.8);
addBox(recovery, 0.83, 0.12, 0.83, 0x445a54, -3.1 + i * 0.92, 1.1, 2.8);
});
addLabel("Recovery hub", new THREE.Vector3(16, 4.25, 8), "node-label");
addNodeHit(new THREE.Vector3(16, 1.7, 8), [5.8, 3.5, 4.4], "Recovery hub", "Sorted material recovery");
// Street trees and lamps.
[
[-21, 6.7, 0.9], [-21, 8.8, 0.8], [-7.8, 13.7, 0.75],
[0, 13.7, 0.8], [8.5, 13.7, 0.9], [20.8, 4.2, 0.8],
[20.8, -6.0, 0.9], [8.0, -11.5, 0.75], [-5.0, -11.7, 0.8]
].forEach(([x, z, s]) => makeTree(x, z, s));
function makeStreetLamp(x, z, rotation = 0) {
const group = new THREE.Group();
group.position.set(x, 0, z);
group.rotation.y = rotation;
neighborhood.add(group);
addCylinder(group, 0.07, 0.1, 3.4, 6, 0x4f625d, 0, 1.7, 0);
addBox(group, 0.85, 0.12, 0.18, 0x4f625d, 0.35, 3.36, 0);
addBox(group, 0.42, 0.16, 0.3, 0xf0dda4, 0.67, 3.25, 0, {
material: { emissive: 0xe4bc65, emissiveIntensity: 0.22 }
});
}
makeStreetLamp(-6.6, 12.2, Math.PI);
makeStreetLamp(6.5, 12.2, Math.PI);
makeStreetLamp(-12.1, 4.5, -Math.PI / 2);
makeStreetLamp(-12.1, -6.5, -Math.PI / 2);
// Low-poly clouds.
const cloudMaterial = standardMaterial(0xf1f3e9, { roughness: 1 });
[
[-14, 16, -21, 1.0], [8, 18, -25, 1.3], [20, 15, -18, 0.8]
].forEach(([x, y, z, s]) => {
const cloud = new THREE.Group();
cloud.position.set(x, y, z);
cloud.scale.setScalar(s);
scene.add(cloud);
[
[-0.8, 0, 0, 0.8], [0, 0.25, 0, 1.05], [0.95, 0, 0, 0.7],
[0.2, -0.2, 0.2, 0.75]
].forEach(([cx, cy, cz, radius]) => {
const puff = new THREE.Mesh(new THREE.DodecahedronGeometry(radius, 0), cloudMaterial);
puff.position.set(cx, cy, cz);
cloud.add(puff);
});
});
// -------------------------------------------------------------------------
// Animated, unit-specific flow routes
// -------------------------------------------------------------------------
function particleGeometry(flowId) {
switch (flowId) {
case "water": return new THREE.IcosahedronGeometry(0.18, 0);
case "energy": return new THREE.OctahedronGeometry(0.19, 0);
case "food": return new THREE.TetrahedronGeometry(0.22, 0);
case "waste": return new THREE.BoxGeometry(0.25, 0.25, 0.25);
default: return new THREE.IcosahedronGeometry(0.18, 0);
}
}
FLOWS.forEach((flow, flowIndex) => {
const points = flow.path.map((point) => new THREE.Vector3(...point));
const curve = new THREE.CatmullRomCurve3(points, false, "catmullrom", 0.35);
curve.arcLengthDivisions = 180;
const tubeMaterial = new THREE.MeshStandardMaterial({
color: flow.color,
emissive: flow.color,
emissiveIntensity: 0.2,
transparent: true,
opacity: 0.95,
roughness: 0.55,
depthWrite: false
});
const tube = new THREE.Mesh(
new THREE.TubeGeometry(curve, 90, 0.075, 6, false),
tubeMaterial
);
scene.add(tube);
tube.userData = { kind: "flow", flowId: flow.id };
pickables.push(tube);
const particleMaterial = new THREE.MeshBasicMaterial({
color: flow.color,
transparent: true,
opacity: 1
});
const geometry = particleGeometry(flow.id);
const particles = [];
for (let i = 0; i < 9; i++) {
const particle = new THREE.Mesh(geometry, particleMaterial);
particle.userData = { kind: "flow", flowId: flow.id };
scene.add(particle);
particles.push(particle);
pickables.push(particle);
}
const arrowMaterial = new THREE.MeshBasicMaterial({
color: flow.color,
transparent: true,
opacity: 0.95
});
const arrows = [];
[0.28, 0.72].forEach((t) => {
const arrow = new THREE.Mesh(new THREE.ConeGeometry(0.18, 0.5, 5), arrowMaterial);
const point = curve.getPointAt(t);
const tangent = curve.getTangentAt(t).normalize();
arrow.position.copy(point);
arrow.quaternion.setFromUnitVectors(new THREE.Vector3(0, 1, 0), tangent);
scene.add(arrow);
arrows.push(arrow);
});
routes.set(flow.id, {
flow,
curve,
tube,
tubeMaterial,
particles,
particleMaterial,
arrowMaterial,
arrows,
phase: flowIndex * 0.13,
speed: 0.105 + flowIndex * 0.012
});
});
// -------------------------------------------------------------------------
// UI, selection, and simulation
// -------------------------------------------------------------------------
let selectedFlowId = "water";
let currentHour = 10;
let playing = true;
let speed = 1;
let toastTimer = 0;
const cards = [...document.querySelectorAll(".flow-card")];
const timeSlider = document.querySelector("#time-slider");
const timeReadout = document.querySelector("#time-readout");
const playButton = document.querySelector("#play-button");
const speedButton = document.querySelector("#speed-button");
const inspector = document.querySelector("#inspector");
function showToast(message) {
const toast = document.querySelector("#toast");
toast.textContent = message;
toast.classList.add("show");
clearTimeout(toastTimer);
toastTimer = setTimeout(() => toast.classList.remove("show"), 2800);
}
function selectFlow(id) {
if (!flowById.has(id)) return;
selectedFlowId = id;
const selected = flowById.get(id);
cards.forEach((card) => card.classList.toggle("active", card.dataset.flow === id));
routes.forEach((route, routeId) => {
const active = routeId === id;
route.tubeMaterial.opacity = active ? 0.98 : 0.16;
route.tubeMaterial.emissiveIntensity = active ? 0.48 : 0.04;
route.particleMaterial.opacity = active ? 1 : 0.22;
route.arrowMaterial.opacity = active ? 1 : 0.22;
});
ports.forEach((port, portId) => {
const active = portId === id;
port.material.emissiveIntensity = active ? 0.7 : 0.16;
port.labelElement.classList.toggle("active", active);
});
inspector.style.setProperty("--selected-color", selected.css);
document.querySelector("#selected-title").textContent = selected.name;
document.querySelector("#selected-subtitle").textContent = selected.subtitle;
document.querySelector("#selected-unit").textContent = selected.unit;
document.querySelector("#selected-source").textContent = selected.source.name;
document.querySelector("#selected-target").textContent = selected.target.name;
document.querySelector("#integrity-copy").textContent =
`Source ${selected.source.unit} → port ${selected.target.unit}. ` +
`No conversion or mixed-unit total.`;
document.querySelector("#shape-name").textContent = selected.shape;
}
cards.forEach((card) => {
card.addEventListener("click", () => selectFlow(card.dataset.flow));
});
function profile(flow, hour) {
switch (flow.id) {
case "water":
return 0.76 + 0.16 * Math.sin((hour - 5) / 24 * Math.PI * 2);
case "energy":
return 0.43 + 0.57 * Math.max(0, Math.sin((hour - 6) / 12 * Math.PI));
case "food":
return 0.72 + 0.28 * Math.max(0, Math.sin((hour - 6) / 14 * Math.PI));
case "waste":
return 0.64 + 0.36 * Math.max(0, Math.sin((hour - 15) / 12 * Math.PI));
default:
return 1;
}
}
function currentFlowValue(flow) {
return flow.base * profile(flow, currentHour);
}
function formatNumber(value, decimals) {
return value.toLocaleString("en-US", {
minimumFractionDigits: decimals,
maximumFractionDigits: decimals
});
}
function updateReadouts() {
FLOWS.forEach((flow) => {
const value = formatNumber(currentFlowValue(flow), flow.decimals);
document.querySelector(`#value-${flow.id}`).textContent = value;
});
const selected = flowById.get(selectedFlowId);
document.querySelector("#selected-value").textContent =
`${formatNumber(currentFlowValue(selected), selected.decimals)} ${selected.unit} / day`;
const hours = Math.floor(currentHour);
const minutes = Math.floor((currentHour - hours) * 60);
timeReadout.textContent =
`${String(hours).padStart(2, "0")}:${String(minutes).padStart(2, "0")}`;
}
timeSlider.addEventListener("input", () => {
currentHour = Number(timeSlider.value);
updateReadouts();
});
playButton.addEventListener("click", () => {
playing = !playing;
playButton.textContent = playing ? "Ⅱ" : "▶";
playButton.setAttribute("aria-label", playing ? "Pause simulation" : "Play simulation");
});
const speeds = [0.5, 1, 2];
speedButton.addEventListener("click", () => {
speed = speeds[(speeds.indexOf(speed) + 1) % speeds.length];
speedButton.textContent = `${speed}×`;
});
document.addEventListener("keydown", (event) => {
if (event.target instanceof HTMLInputElement) return;
if (event.code === "Space") {
event.preventDefault();
playButton.click();
}
if (["1", "2", "3", "4"].includes(event.key)) {
selectFlow(FLOWS[Number(event.key) - 1].id);
}
if (event.key.toLowerCase() === "r") resetCamera();
});
document.querySelector(".unit-lock").addEventListener("click", () => {
showToast("All four channels are unit-locked. The model intentionally exposes no combined total.");
});
// -------------------------------------------------------------------------
// Direct camera controls — no examples/addons are used.
// -------------------------------------------------------------------------
const cameraState = {
theta: 0.72,
phi: 0.94,
radius: 47,
targetTheta: 0.72,
targetPhi: 0.94,
targetRadius: 47,
target: new THREE.Vector3(0, 1.8, 0)
};
function updateCamera(delta = 1) {
const damping = 1 - Math.exp(-delta * 8);
cameraState.theta += (cameraState.targetTheta - cameraState.theta) * damping;
cameraState.phi += (cameraState.targetPhi - cameraState.phi) * damping;
cameraState.radius += (cameraState.targetRadius - cameraState.radius) * damping;
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);
}
function resetCamera() {
cameraState.targetTheta = 0.72;
cameraState.targetPhi = 0.94;
cameraState.targetRadius = 47;
cameraState.target.set(0, 1.8, 0);
}
document.querySelector("#camera-home").addEventListener("click", resetCamera);
document.querySelector("#camera-top").addEventListener("click", () => {
cameraState.targetTheta = 0;
cameraState.targetPhi = 0.16;
cameraState.targetRadius = 49;
});
document.querySelector("#zoom-in").addEventListener("click", () => {
cameraState.targetRadius = Math.max(25, cameraState.targetRadius - 4);
});
document.querySelector("#zoom-out").addEventListener("click", () => {
cameraState.targetRadius = Math.min(70, cameraState.targetRadius + 4);
});
const raycaster = new THREE.Raycaster();
const pointer = new THREE.Vector2();
let pointerDown = false;
let pointerMoved = 0;
let pointerX = 0;
let pointerY = 0;
const canvas = renderer.domElement;
canvas.addEventListener("pointerdown", (event) => {
pointerDown = true;
pointerMoved = 0;
pointerX = event.clientX;
pointerY = event.clientY;
canvas.setPointerCapture(event.pointerId);
});
canvas.addEventListener("pointermove", (event) => {
if (!pointerDown) return;
const dx = event.clientX - pointerX;
const dy = event.clientY - pointerY;
pointerX = event.clientX;
pointerY = event.clientY;
pointerMoved += Math.abs(dx) + Math.abs(dy);
cameraState.targetTheta -= dx * 0.006;
cameraState.targetPhi = THREE.MathUtils.clamp(
cameraState.targetPhi + dy * 0.005,
0.16,
1.36
);
});
canvas.addEventListener("pointerup", (event) => {
pointerDown = false;
if (pointerMoved < 6) pickScene(event.clientX, event.clientY);
if (canvas.hasPointerCapture(event.pointerId)) {
canvas.releasePointerCapture(event.pointerId);
}
});
canvas.addEventListener("pointercancel", () => {
pointerDown = false;
});
canvas.addEventListener("wheel", (event) => {
event.preventDefault();
cameraState.targetRadius = THREE.MathUtils.clamp(
cameraState.targetRadius + event.deltaY * 0.025,
25,
70
);
}, { passive: false });
canvas.addEventListener("dblclick", resetCamera);
function pickScene(clientX, clientY) {
const rect = canvas.getBoundingClientRect();
pointer.x = ((clientX - rect.left) / rect.width) * 2 - 1;
pointer.y = -((clientY - rect.top) / rect.height) * 2 + 1;
raycaster.setFromCamera(pointer, camera);
const intersections = raycaster.intersectObjects(pickables, false);
if (!intersections.length) return;
const hit = intersections[0].object.userData;
if (hit.kind === "flow" || hit.kind === "port") {
selectFlow(hit.flowId);
} else if (hit.kind === "node") {
showToast(`${hit.name} — ${hit.detail}`);
}
}
// -------------------------------------------------------------------------
// Label projection and animation
// -------------------------------------------------------------------------
function updateLabels() {
const width = window.innerWidth;
const height = window.innerHeight;
labels.forEach((label) => {
const projected = label.world.clone().project(camera);
const inView =
projected.z > -1 &&
projected.z < 1 &&
projected.x > -1.25 &&
projected.x < 1.25 &&
projected.y > -1.25 &&
projected.y < 1.25;
label.element.style.opacity = inView ? label.opacity : 0;
if (!inView) return;
const x = (projected.x * 0.5 + 0.5) * width;
const y = (-projected.y * 0.5 + 0.5) * height;
label.element.style.left = `${x}px`;
label.element.style.top = `${y}px`;
});
}
const clock = new THREE.Clock();
let elapsed = 0;
function animate() {
requestAnimationFrame(animate);
const delta = Math.min(clock.getDelta(), 0.05);
elapsed += delta;
if (playing) {
currentHour = (currentHour + delta * speed * 0.34) % 24;
timeSlider.value = String(currentHour);
updateReadouts();
}
routes.forEach((route) => {
route.phase = (route.phase + delta * speed * route.speed) % 1;
route.particles.forEach((particle, index) => {
const t = (route.phase + index / route.particles.length) % 1;
particle.position.copy(route.curve.getPointAt(t));
const tangent = route.curve.getTangentAt(t).normalize();
particle.quaternion.setFromUnitVectors(new THREE.Vector3(0, 1, 0), tangent);
const pulse = 1 + Math.sin((t + elapsed * 2) * Math.PI * 2) * 0.08;
particle.scale.setScalar(pulse);
});
route.arrows.forEach((arrow) => {
arrow.position.y += Math.sin(elapsed * 2.2 + route.phase * 6) * 0.0006;
});
});
ports.forEach((port, id) => {
const selected = id === selectedFlowId;
const pulse = selected ? 1 + Math.sin(elapsed * 4.2) * 0.1 : 1;
port.marker.scale.setScalar(pulse);
});
haloMaterial.opacity = 0.18 + Math.sin(elapsed * 1.7) * 0.055;
halo.rotation.z = elapsed * 0.025;
updateCamera(delta);
updateLabels();
renderer.render(scene, camera);
}
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
});
selectFlow("water");
updateReadouts();
updateCamera(1);
animate();
requestAnimationFrame(() => {
document.querySelector("#loading").classList.add("hidden");
setTimeout(() => {
document.querySelector("#hint").style.opacity = "0";
}, 6500);
});
</script>
</body>
</html>
```