Prompt specification
Build or customize the Three.js 3D scene "Two Pipe Networks With Distinct Endpoints" 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: Two Pipe Networks With Distinct Endpoints
Category: Everyday & Industrial · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/two-pipe-networks-with-distinct-endpoints-x3993
Scene Description Prompt:
Create a single self-contained HTML file using Three.js from cdnjs that shows separate water supply and drainage networks. Model two color-and-symbol-coded pipe networks with separate sources and destinations. Prevent accidental connections and distinguish the streams through labels as well as color. A 3D cutaway view.
Working Three.js Starter (two-pipe-networks-with-distinct-endpoints-x3993):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Isolated Water Supply and Drainage Networks</title>
<style>
:root {
--bg: #07111f;
--panel: rgba(10, 20, 34, 0.82);
--panel-strong: rgba(7, 16, 29, 0.94);
--line: rgba(151, 190, 218, 0.2);
--text: #edf8ff;
--muted: #8fa9bc;
--supply: #28bfff;
--supply-soft: rgba(40, 191, 255, 0.14);
--drain: #ff9d42;
--drain-soft: rgba(255, 157, 66, 0.14);
--safe: #71e6bd;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: var(--bg);
color: var(--text);
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
button,
input {
font: inherit;
}
#viewport,
#labels {
position: fixed;
inset: 0;
}
#viewport canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
cursor: grab;
outline: none;
}
#viewport canvas:active {
cursor: grabbing;
}
#labels {
pointer-events: none;
overflow: hidden;
z-index: 3;
}
.ui {
position: fixed;
inset: 0;
pointer-events: none;
z-index: 5;
}
.panel {
background: var(--panel);
border: 1px solid var(--line);
border-radius: 16px;
box-shadow:
0 18px 60px rgba(0, 0, 0, 0.28),
inset 0 1px 0 rgba(255, 255, 255, 0.035);
backdrop-filter: blur(14px);
-webkit-backdrop-filter: blur(14px);
}
.header {
position: absolute;
top: 24px;
left: 26px;
max-width: 440px;
text-shadow: 0 2px 15px rgba(0, 0, 0, 0.5);
}
.eyebrow {
display: flex;
align-items: center;
gap: 9px;
margin-bottom: 9px;
color: #9fc0d4;
font-size: 10px;
font-weight: 750;
letter-spacing: 0.2em;
text-transform: uppercase;
}
.eyebrow::before {
content: "";
width: 24px;
height: 2px;
background: var(--safe);
box-shadow: 0 0 12px rgba(113, 230, 189, 0.55);
}
h1 {
margin: 0;
font-size: clamp(27px, 3.3vw, 48px);
line-height: 1.02;
letter-spacing: -0.045em;
font-weight: 720;
}
.header p {
max-width: 390px;
margin: 12px 0 0;
color: var(--muted);
font-size: 13px;
line-height: 1.55;
}
.legend {
position: absolute;
left: 26px;
bottom: 24px;
width: 286px;
padding: 17px;
pointer-events: auto;
}
.panel-title {
margin-bottom: 13px;
color: #a9c0d1;
font-size: 10px;
font-weight: 750;
letter-spacing: 0.17em;
text-transform: uppercase;
}
.legend-row {
display: grid;
grid-template-columns: 36px 1fr;
align-items: center;
gap: 11px;
padding: 10px 0;
}
.legend-row + .legend-row {
border-top: 1px solid rgba(151, 190, 218, 0.11);
}
.legend-symbol {
display: grid;
width: 34px;
height: 34px;
place-items: center;
border-radius: 50%;
font-weight: 800;
}
.legend-symbol svg {
width: 20px;
height: 20px;
}
.legend-symbol.supply {
color: var(--supply);
background: var(--supply-soft);
border: 1px solid rgba(40, 191, 255, 0.42);
box-shadow: 0 0 20px rgba(40, 191, 255, 0.13);
}
.legend-symbol.drain {
color: var(--drain);
background: var(--drain-soft);
border: 1px solid rgba(255, 157, 66, 0.42);
box-shadow: 0 0 20px rgba(255, 157, 66, 0.13);
}
.legend-copy strong {
display: block;
margin-bottom: 3px;
font-size: 12px;
letter-spacing: 0.03em;
}
.legend-copy span {
display: block;
color: var(--muted);
font-size: 10px;
line-height: 1.4;
}
.safe-note {
display: flex;
align-items: flex-start;
gap: 9px;
margin-top: 10px;
padding: 10px;
color: #bdebd8;
background: rgba(113, 230, 189, 0.07);
border: 1px solid rgba(113, 230, 189, 0.2);
border-radius: 10px;
font-size: 10px;
line-height: 1.45;
}
.safe-note svg {
flex: 0 0 auto;
width: 16px;
height: 16px;
margin-top: 1px;
}
.controls {
position: absolute;
top: 24px;
right: 26px;
width: 272px;
padding: 14px;
pointer-events: auto;
}
.segmented {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 5px;
padding: 4px;
background: rgba(0, 0, 0, 0.24);
border: 1px solid rgba(151, 190, 218, 0.13);
border-radius: 11px;
}
.segmented button {
min-height: 34px;
padding: 7px 5px;
color: #89a5b8;
background: transparent;
border: 0;
border-radius: 8px;
cursor: pointer;
font-size: 10px;
font-weight: 750;
letter-spacing: 0.06em;
text-transform: uppercase;
transition: 160ms ease;
}
.segmented button:hover {
color: white;
background: rgba(255, 255, 255, 0.05);
}
.segmented button.active {
color: white;
background: rgba(71, 123, 158, 0.25);
box-shadow: inset 0 0 0 1px rgba(142, 197, 230, 0.17);
}
.control-line {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
margin-top: 12px;
color: #a8bdcc;
font-size: 11px;
}
.control-line + .control-line {
padding-top: 11px;
border-top: 1px solid rgba(151, 190, 218, 0.1);
}
.switch {
position: relative;
display: inline-flex;
align-items: center;
gap: 8px;
cursor: pointer;
user-select: none;
}
.switch input {
position: absolute;
opacity: 0;
pointer-events: none;
}
.switch-track {
position: relative;
width: 31px;
height: 17px;
background: #243746;
border-radius: 20px;
transition: 160ms ease;
}
.switch-track::after {
content: "";
position: absolute;
top: 3px;
left: 3px;
width: 11px;
height: 11px;
background: #a8bbc8;
border-radius: 50%;
transition: 160ms ease;
}
.switch input:checked + .switch-track {
background: rgba(40, 191, 255, 0.45);
}
.switch input:checked + .switch-track::after {
left: 17px;
background: #e9faff;
}
.reset-button {
padding: 7px 10px;
color: #bdd0dd;
background: rgba(255, 255, 255, 0.045);
border: 1px solid rgba(151, 190, 218, 0.15);
border-radius: 8px;
cursor: pointer;
font-size: 10px;
font-weight: 700;
transition: 160ms ease;
}
.reset-button:hover {
color: white;
border-color: rgba(151, 190, 218, 0.34);
background: rgba(255, 255, 255, 0.08);
}
.metrics {
position: absolute;
right: 26px;
bottom: 24px;
width: 272px;
padding: 15px 16px;
}
.metric {
display: grid;
grid-template-columns: 9px 1fr auto;
align-items: center;
gap: 9px;
color: #a9becc;
font-size: 10px;
}
.metric + .metric {
margin-top: 9px;
}
.metric strong {
color: #edf8ff;
font-size: 10px;
font-weight: 700;
}
.metric .value {
color: #7894a8;
font-variant-numeric: tabular-nums;
}
.dot {
width: 7px;
height: 7px;
border-radius: 50%;
}
.dot.supply {
background: var(--supply);
box-shadow: 0 0 9px rgba(40, 191, 255, 0.8);
}
.dot.drain {
background: var(--drain);
box-shadow: 0 0 9px rgba(255, 157, 66, 0.8);
}
.separation-metric {
display: flex;
justify-content: space-between;
gap: 10px;
margin-top: 13px;
padding-top: 11px;
color: #83aa9b;
border-top: 1px solid rgba(113, 230, 189, 0.15);
font-size: 9px;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.separation-metric strong {
color: var(--safe);
letter-spacing: 0;
}
.hint {
position: absolute;
left: 50%;
bottom: 23px;
transform: translateX(-50%);
color: rgba(164, 189, 206, 0.65);
font-size: 10px;
letter-spacing: 0.04em;
white-space: nowrap;
}
.net-label {
position: absolute;
top: 0;
left: 0;
display: flex;
align-items: center;
gap: 9px;
min-width: 178px;
padding: 8px 10px 8px 8px;
background: rgba(7, 16, 28, 0.9);
border: 1px solid rgba(40, 191, 255, 0.38);
border-radius: 9px;
box-shadow: 0 10px 30px rgba(0, 0, 0, 0.32);
opacity: 0;
visibility: hidden;
transition: opacity 120ms ease;
will-change: transform;
}
.net-label.drain {
border-color: rgba(255, 157, 66, 0.38);
}
.label-icon {
display: grid;
flex: 0 0 auto;
width: 28px;
height: 28px;
place-items: center;
color: var(--supply);
background: var(--supply-soft);
border: 1px solid rgba(40, 191, 255, 0.35);
border-radius: 50%;
}
.drain .label-icon {
color: var(--drain);
background: var(--drain-soft);
border-color: rgba(255, 157, 66, 0.35);
}
.label-icon svg {
width: 15px;
height: 15px;
}
.label-copy {
min-width: 0;
}
.label-role {
display: block;
margin-bottom: 2px;
color: var(--supply);
font-size: 8px;
font-weight: 800;
letter-spacing: 0.16em;
text-transform: uppercase;
}
.drain .label-role {
color: var(--drain);
}
.label-title {
display: block;
overflow: hidden;
color: #eaf6fc;
font-size: 10px;
font-weight: 700;
line-height: 1.25;
text-overflow: ellipsis;
white-space: nowrap;
}
.label-sub {
display: block;
margin-top: 2px;
overflow: hidden;
color: #829bae;
font-size: 8px;
line-height: 1.2;
text-overflow: ellipsis;
white-space: nowrap;
}
.isolation-label {
position: absolute;
top: 0;
left: 0;
padding: 6px 9px;
color: #bdebd8;
background: rgba(7, 22, 25, 0.92);
border: 1px solid rgba(113, 230, 189, 0.38);
border-radius: 6px;
box-shadow: 0 8px 24px rgba(0, 0, 0, 0.28);
font-size: 8px;
font-weight: 800;
letter-spacing: 0.09em;
text-transform: uppercase;
opacity: 0;
visibility: hidden;
transform: translate(-50%, -50%);
white-space: nowrap;
}
#loading {
position: fixed;
inset: 0;
z-index: 20;
display: grid;
place-items: center;
color: #a8c2d3;
background: #07111f;
font-size: 11px;
font-weight: 700;
letter-spacing: 0.16em;
text-transform: uppercase;
transition: opacity 350ms ease;
}
#loading.ready {
opacity: 0;
pointer-events: none;
}
@media (max-width: 900px) {
.header {
max-width: 320px;
}
.header p {
display: none;
}
.legend {
width: 235px;
padding: 13px;
}
.legend-copy span,
.safe-note {
display: none;
}
.metrics,
.hint {
display: none;
}
}
@media (max-width: 620px) {
.header {
top: 15px;
left: 15px;
}
h1 {
max-width: 250px;
font-size: 25px;
}
.eyebrow {
font-size: 8px;
}
.controls {
top: auto;
right: 12px;
bottom: 12px;
width: 215px;
padding: 10px;
}
.legend {
display: none;
}
.control-line {
margin-top: 9px;
}
.control-line + .control-line {
padding-top: 8px;
}
.net-label {
min-width: 135px;
padding: 6px;
}
.label-icon {
width: 23px;
height: 23px;
}
.label-sub {
display: none;
}
}
</style>
</head>
<body>
<div id="viewport"></div>
<div id="labels"></div>
<div class="ui">
<header class="header">
<div class="eyebrow">Civil infrastructure / Cutaway 01</div>
<h1>Two networks.<br>No shared paths.</h1>
<p>
Pressurized water supply occupies the shallow service tier.
Gravity drainage remains isolated in the deeper invert.
</p>
</header>
<section class="legend panel" aria-label="Network legend">
<div class="panel-title">Network key</div>
<div class="legend-row">
<div class="legend-symbol supply">
<svg viewBox="0 0 16 16" aria-hidden="true">
<path
d="M8 3v10M3 8h10"
fill="none"
stroke="currentColor"
stroke-width="2.4"
stroke-linecap="round"
/>
</svg>
</div>
<div class="legend-copy">
<strong>WATER SUPPLY · +</strong>
<span>Pressurized · shallow tier · thin pipe</span>
</div>
</div>
<div class="legend-row">
<div class="legend-symbol drain">
<svg viewBox="0 0 16 16" aria-hidden="true">
<path
d="M8 1.2S2.8 7.1 2.8 10.2a5.2 5.2 0 0 0 10.4 0C13.2 7.1 8 1.2 8 1.2Z"
fill="currentColor"
/>
</svg>
</div>
<div class="legend-copy">
<strong>DRAINAGE · DROPLET</strong>
<span>Gravity collection · deep tier · thick pipe</span>
</div>
</div>
<div class="safe-note">
<svg viewBox="0 0 16 16" aria-hidden="true">
<path
d="M8 1.3 13.4 3v4.2c0 3.4-2.2 6.2-5.4 7.5-3.2-1.3-5.4-4.1-5.4-7.5V3L8 1.3Z"
fill="none"
stroke="currentColor"
stroke-width="1.4"
/>
<path
d="m5.5 8 1.6 1.6 3.5-3.7"
fill="none"
stroke="currentColor"
stroke-width="1.5"
stroke-linecap="round"
stroke-linejoin="round"
/>
</svg>
<span>
Separate sources, destinations and nodes. A physical vertical clearance
prevents accidental cross-connections.
</span>
</div>
</section>
<section class="controls panel" aria-label="Scene controls">
<div class="segmented" role="group" aria-label="Isolate network">
<button class="active" data-mode="all" type="button">Combined</button>
<button data-mode="supply" type="button">Water</button>
<button data-mode="drainage" type="button">Drain</button>
</div>
<div class="control-line">
<label class="switch">
<input id="flow-toggle" type="checkbox" checked>
<span class="switch-track"></span>
<span>Animated flow</span>
</label>
<button id="reset-button" class="reset-button" type="button">
Reset view
</button>
</div>
<div class="control-line">
<label class="switch">
<input id="labels-toggle" type="checkbox" checked>
<span class="switch-track"></span>
<span>Scene labels</span>
</label>
<span style="font-size:9px;color:#668296">Drag: orbit<br>Right-drag: pan</span>
</div>
</section>
<section class="metrics panel" aria-label="Network measurements">
<div class="metric">
<span class="dot supply"></span>
<strong>Supply</strong>
<span class="value">1 source → 2 destinations</span>
</div>
<div class="metric">
<span class="dot drain"></span>
<strong>Drainage</strong>
<span class="value">2 sources → 1 destination</span>
</div>
<div class="separation-metric">
<span>Separation</span>
<strong>0.71 m clear gap</strong>
</div>
</section>
<div class="hint">Scroll to zoom · Shift-drag to pan · Double-click to reset</div>
</div>
<div id="loading">Building utility networks…</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const viewport = document.getElementById("viewport");
const labelLayer = document.getElementById("labels");
const loading = document.getElementById("loading");
const COLORS = {
supply: 0x28bfff,
supplyLight: 0xa9eaff,
supplyDark: 0x075b80,
drain: 0xff9d42,
drainLight: 0xffd09a,
drainDark: 0x8a4319,
safe: 0x71e6bd
};
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x07111f);
scene.fog = new THREE.FogExp2(0x07111f, 0.017);
const camera = new THREE.PerspectiveCamera(
42,
window.innerWidth / window.innerHeight,
0.1,
150
);
const renderer = new THREE.WebGLRenderer({
antialias: true,
powerPreference: "high-performance"
});
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.12;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
viewport.appendChild(renderer.domElement);
const hemisphere = new THREE.HemisphereLight(0xb9e8ff, 0x17202b, 2.1);
scene.add(hemisphere);
const sun = new THREE.DirectionalLight(0xffffff, 3.6);
sun.position.set(9, 17, 12);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -18;
sun.shadow.camera.right = 18;
sun.shadow.camera.top = 15;
sun.shadow.camera.bottom = -15;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 55;
sun.shadow.bias = -0.0003;
scene.add(sun);
const rimLight = new THREE.DirectionalLight(0x4ea8ff, 1.8);
rimLight.position.set(-13, 8, -10);
scene.add(rimLight);
const supplyLight = new THREE.PointLight(COLORS.supply, 24, 17, 2);
supplyLight.position.set(-2, 2.8, -3.5);
scene.add(supplyLight);
const drainLight = new THREE.PointLight(COLORS.drain, 20, 14, 2);
drainLight.position.set(6, 0.1, 1.6);
scene.add(drainLight);
const terrainGroup = new THREE.Group();
const supplyGroup = new THREE.Group();
const drainageGroup = new THREE.Group();
const sharedStructures = new THREE.Group();
const flowParticles = [];
const labels = [];
scene.add(
terrainGroup,
supplyGroup,
drainageGroup,
sharedStructures
);
function standardMaterial(color, options = {}) {
return new THREE.MeshStandardMaterial({
color,
roughness: 0.72,
metalness: 0.08,
...options
});
}
function addEdges(mesh, color = 0x4e7188, opacity = 0.36) {
const edge = new THREE.LineSegments(
new THREE.EdgesGeometry(mesh.geometry, 30),
new THREE.LineBasicMaterial({
color,
transparent: true,
opacity
})
);
edge.position.copy(mesh.position);
edge.rotation.copy(mesh.rotation);
edge.scale.copy(mesh.scale);
mesh.parent.add(edge);
return edge;
}
function box(
width,
height,
depth,
position,
material,
parent = terrainGroup,
castShadow = true,
receiveShadow = true
) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(width, height, depth),
material
);
mesh.position.set(...position);
mesh.castShadow = castShadow;
mesh.receiveShadow = receiveShadow;
parent.add(mesh);
return mesh;
}
function cylinderBetween(start, end, radius, material, parent) {
const a = new THREE.Vector3(...start);
const b = new THREE.Vector3(...end);
const direction = b.clone().sub(a);
const length = direction.length();
const mesh = new THREE.Mesh(
new THREE.CylinderGeometry(radius, radius, length, 16),
material
);
mesh.position.copy(a).add(b).multiplyScalar(0.5);
mesh.quaternion.setFromUnitVectors(
new THREE.Vector3(0, 1, 0),
direction.normalize()
);
mesh.castShadow = true;
parent.add(mesh);
return mesh;
}
// ---------------------------------------------------------------------
// Cutaway terrain: L-shaped surface, exposed rear/left soil strata,
// open excavation in front, and a technical base grid.
// ---------------------------------------------------------------------
const grid = new THREE.GridHelper(34, 34, 0x1d526b, 0x102c3b);
grid.position.y = -2.39;
grid.material.transparent = true;
grid.material.opacity = 0.42;
scene.add(grid);
const baseMaterial = standardMaterial(0x24333a, {
roughness: 0.86,
metalness: 0.04
});
const base = box(
24,
0.36,
15.5,
[0, -2.18, 0],
baseMaterial
);
addEdges(base, 0x51788d, 0.55);
const soilLayers = [
{ y: -1.72, color: 0x49362f },
{ y: -1.16, color: 0x604637 },
{ y: -0.60, color: 0x73533b },
{ y: -0.23, color: 0x3d4937 }
];
for (const layer of soilLayers) {
const material = standardMaterial(layer.color, {
roughness: 0.96,
metalness: 0
});
box(
24,
0.56,
0.48,
[0, layer.y, -7.18],
material
);
box(
0.48,
0.56,
14.72,
[-11.72, layer.y, 0],
material
);
}
const surfaceMaterial = standardMaterial(0x344b3c, {
roughness: 0.95,
metalness: 0
});
const backSurface = box(
24,
0.18,
3.48,
[0, -0.04, -5.45],
surfaceMaterial
);
addEdges(backSurface, 0x73916b, 0.4);
const leftSurface = box(
5.05,
0.18,
14.72,
[-9.19, -0.04, 0],
surfaceMaterial
);
addEdges(leftSurface, 0x73916b, 0.4);
const sectionEdgeMaterial = new THREE.MeshBasicMaterial({
color: 0xb88c59,
transparent: true,
opacity: 0.45
});
box(
16.5,
0.035,
0.08,
[2.72, -0.105, -3.68],
sectionEdgeMaterial,
terrainGroup,
false,
false
);
box(
0.08,
0.035,
10.65,
[-6.63, -0.105, 1.65],
sectionEdgeMaterial,
terrainGroup,
false,
false
);
const pitMaterial = standardMaterial(0x253039, {
roughness: 0.9,
metalness: 0.02
});
box(
16.35,
0.06,
10.55,
[2.72, -1.97, 1.65],
pitMaterial,
terrainGroup,
false,
true
);
// ---------------------------------------------------------------------
// Buildings and facilities.
// ---------------------------------------------------------------------
const concreteMaterial = standardMaterial(0x7e939e, {
roughness: 0.78,
metalness: 0.12
});
const buildingMaterial = standardMaterial(0xb6c6ce, {
roughness: 0.55,
metalness: 0.12
});
const darkBuildingMaterial = standardMaterial(0x344651, {
roughness: 0.68,
metalness: 0.16
});
const windowMaterial = new THREE.MeshStandardMaterial({
color: 0x76c8eb,
emissive: 0x176d92,
emissiveIntensity: 0.75,
roughness: 0.25,
metalness: 0.18
});
// Civic Tower: shared structure and a supply destination.
const civic = new THREE.Group();
civic.position.set(4, 0, -4.9);
sharedStructures.add(civic);
box(3.05, 0.13, 3.05, [0, 0.075, 0], concreteMaterial, civic);
const civicBody = box(
2.5,
3.1,
2.4,
[0, 1.65, 0],
buildingMaterial,
civic
);
addEdges(civicBody, 0xe2f4ff, 0.42);
box(2.72, 0.16, 2.62, [0, 3.28, 0], darkBuildingMaterial, civic);
box(1.2, 0.22, 0.85, [-0.35, 3.48, -0.2], darkBuildingMaterial, civic);
for (const y of [0.95, 1.55, 2.15, 2.7]) {
for (const x of [-0.78, -0.26, 0.26, 0.78]) {
box(
0.34,
0.37,
0.04,
[x, y, 1.215],
windowMaterial,
civic,
false,
false
);
}
}
for (const y of [1.05, 1.8, 2.55]) {
for (const z of [-0.72, 0, 0.72]) {
box(
0.04,
0.38,
0.48,
[1.265, y, z],
windowMaterial,
civic,
false,
false
);
}
}
// Clinic: shared structure and a second supply destination.
const clinic = new THREE.Group();
clinic.position.set(7, 0, 2.3);
sharedStructures.add(clinic);
box(3.55, 0.13, 3.05, [0, 0.075, 0], concreteMaterial, clinic);
const clinicBody = box(
2.8,
1.85,
2.4,
[0, 1.02, 0],
buildingMaterial,
clinic
);
addEdges(clinicBody, 0xe2f4ff, 0.4);
box(3.05, 0.16, 2.62, [0, 2.02, 0], darkBuildingMaterial, clinic);
const medicalRed = new THREE.MeshStandardMaterial({
color: 0xff5d64,
emissive: 0x6d1018,
emissiveIntensity: 0.45,
roughness: 0.45
});
box(0.32, 1.05, 0.08, [0, 1.13, 1.23], medicalRed, clinic, false, false);
box(1.05, 0.32, 0.08, [0, 1.13, 1.235], medicalRed, clinic, false, false);
for (const x of [-0.9, -0.45, 0, 0.45, 0.9]) {
box(
0.28,
0.43,
0.04,
[x, 0.72, 1.215],
windowMaterial,
clinic,
false,
false
);
}
// Potable-water source.
const waterFacility = new THREE.Group();
waterFacility.position.set(-9, 0, -5.2);
supplyGroup.add(waterFacility);
const tankBase = new THREE.Mesh(
new THREE.CylinderGeometry(1.55, 1.62, 0.22, 48),
concreteMaterial
);
tankBase.position.y = 0.15;
tankBase.castShadow = true;
tankBase.receiveShadow = true;
waterFacility.add(tankBase);
const tankShellMaterial = new THREE.MeshPhysicalMaterial({
color: 0xa6e4f7,
transparent: true,
opacity: 0.2,
roughness: 0.16,
metalness: 0.08,
side: THREE.DoubleSide,
depthWrite: false
});
const tankShell = new THREE.Mesh(
new THREE.CylinderGeometry(1.42, 1.42, 1.45, 48, 1, true),
tankShellMaterial
);
tankShell.position.y = 0.98;
waterFacility.add(tankShell);
const waterMaterial = new THREE.MeshStandardMaterial({
color: 0x138fc5,
emissive: 0x07577c,
emissiveIntensity: 0.62,
roughness: 0.24,
metalness: 0.05,
transparent: true,
opacity: 0.84
});
const tankWater = new THREE.Mesh(
new THREE.CylinderGeometry(1.28, 1.28, 0.9, 48),
waterMaterial
);
tankWater.position.y = 0.88;
waterFacility.add(tankWater);
const tankRing = new THREE.Mesh(
new THREE.TorusGeometry(1.43, 0.08, 10, 48),
standardMaterial(0x9fc4d1, {
roughness: 0.32,
metalness: 0.45
})
);
tankRing.rotation.x = Math.PI / 2;
tankRing.position.y = 1.7;
waterFacility.add(tankRing);
const supplyOutletMaterial = standardMaterial(COLORS.supply, {
roughness: 0.28,
metalness: 0.28,
emissive: COLORS.supplyDark,
emissiveIntensity: 0.52
});
cylinderBetween(
[-8.25, 0.28, -5.2],
[-7.5, 0.28, -5.2],
0.17,
supplyOutletMaterial,
supplyGroup
);
// Wastewater destination.
const wastewaterFacility = new THREE.Group();
wastewaterFacility.position.set(8.8, 0, 5.75);
drainageGroup.add(wastewaterFacility);
box(4.1, 0.13, 3.45, [0, 0.075, 0], concreteMaterial, wastewaterFacility);
const clarifierWater = new THREE.MeshStandardMaterial({
color: 0x6a472d,
emissive: 0x38200f,
emissiveIntensity: 0.35,
roughness: 0.35,
metalness: 0.1
});
const clarifierRim = standardMaterial(0x788993, {
roughness: 0.42,
metalness: 0.35
});
for (const x of [-1.0, 0.95]) {
const tank = new THREE.Mesh(
new THREE.CylinderGeometry(0.82, 0.86, 0.58, 40),
clarifierRim
);
tank.position.set(x, 0.43, 0.05);
tank.castShadow = true;
wastewaterFacility.add(tank);
const water = new THREE.Mesh(
new THREE.CylinderGeometry(0.72, 0.72, 0.08, 40),
clarifierWater
);
water.position.set(x, 0.73, 0.05);
wastewaterFacility.add(water);
const ring = new THREE.Mesh(
new THREE.TorusGeometry(0.83, 0.055, 8, 36),
standardMaterial(COLORS.drain, {
emissive: COLORS.drainDark,
emissiveIntensity: 0.45,
roughness: 0.35,
metalness: 0.25
})
);
ring.rotation.x = Math.PI / 2;
ring.position.set(x, 0.72, 0.05);
wastewaterFacility.add(ring);
const bridge = box(
1.65,
0.08,
0.18,
[x, 0.8, 0.05],
clarifierRim,
wastewaterFacility
);
bridge.rotation.y = x < 0 ? 0.55 : -0.55;
}
box(
1.35,
0.9,
1.05,
[0, 0.56, -1.0],
darkBuildingMaterial,
wastewaterFacility
);
box(
1.55,
0.13,
1.2,
[0, 1.08, -1.0],
standardMaterial(COLORS.drain, {
roughness: 0.5,
metalness: 0.15
}),
wastewaterFacility
);
// ---------------------------------------------------------------------
// Pipe materials and construction helpers.
// ---------------------------------------------------------------------
const supplyPipeMaterial = new THREE.MeshStandardMaterial({
color: COLORS.supply,
roughness: 0.26,
metalness: 0.32,
emissive: COLORS.supplyDark,
emissiveIntensity: 0.62
});
const drainPipeMaterial = new THREE.MeshStandardMaterial({
color: COLORS.drain,
roughness: 0.34,
metalness: 0.23,
emissive: COLORS.drainDark,
emissiveIntensity: 0.52
});
const supplyCouplingMaterial = standardMaterial(0x0b7196, {
roughness: 0.28,
metalness: 0.42
});
const drainCouplingMaterial = standardMaterial(0xa65722, {
roughness: 0.4,
metalness: 0.34
});
const upAxis = new THREE.Vector3(0, 1, 0);
function addPipe(curve, radius, material, parent, segments = 90) {
const geometry = new THREE.TubeGeometry(
curve,
segments,
radius,
12,
false
);
const mesh = new THREE.Mesh(geometry, material);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function addNode(position, color, radius, parent) {
const material = new THREE.MeshStandardMaterial({
color,
emissive: color,
emissiveIntensity: 0.24,
roughness: 0.28,
metalness: 0.3
});
const node = new THREE.Mesh(
new THREE.SphereGeometry(radius, 20, 14),
material
);
node.position.set(...position);
node.castShadow = true;
parent.add(node);
return node;
}
function addCouplings(
curve,
radius,
material,
parent,
spacing = 3.4
) {
const count = Math.max(1, Math.floor(curve.getLength() / spacing));
for (let i = 1; i < count; i++) {
const t = i / count;
const position = curve.getPointAt(t);
const tangent = curve.getTangentAt(t).normalize();
const coupling = new THREE.Mesh(
new THREE.CylinderGeometry(
radius * 1.34,
radius * 1.34,
0.18,
14
),
material
);
coupling.position.copy(position);
coupling.quaternion.setFromUnitVectors(upAxis, tangent);
coupling.castShadow = true;
parent.add(coupling);
}
}
function addArrows(curve, color, parent, radius = 0.28) {
const arrowMaterial = new THREE.MeshStandardMaterial({
color,
emissive: color,
emissiveIntensity: 0.35,
roughness: 0.34,
metalness: 0.12
});
const count = Math.max(2, Math.floor(curve.getLength() / 3.1));
for (let i = 1; i <= count; i++) {
const t = i / (count + 1);
const position = curve.getPointAt(t);
const tangent = curve.getTangentAt(t).normalize();
const arrow = new THREE.Mesh(
new THREE.ConeGeometry(radius, radius * 2.7, 12),
arrowMaterial
);
arrow.position.copy(position);
arrow.quaternion.setFromUnitVectors(upAxis, tangent);
arrow.castShadow = true;
parent.add(arrow);
}
}
function makeIconTexture(type) {
const canvas = document.createElement("canvas");
canvas.width = 128;
canvas.height = 128;
const ctx = canvas.getContext("2d");
const color = type === "supply" ? "#28bfff" : "#ff9d42";
ctx.clearRect(0, 0, 128, 128);
ctx.beginPath();
ctx.arc(64, 64, 51, 0, Math.PI * 2);
ctx.fillStyle = type === "supply"
? "rgba(13, 91, 128, 0.94)"
: "rgba(126, 61, 21, 0.94)";
ctx.fill();
ctx.strokeStyle = color;
ctx.lineWidth = 7;
ctx.stroke();
ctx.fillStyle = "#f4fbff";
ctx.strokeStyle = "#f4fbff";
ctx.lineCap = "round";
ctx.lineJoin = "round";
if (type === "supply") {
ctx.lineWidth = 12;
ctx.beginPath();
ctx.moveTo(64, 34);
ctx.lineTo(64, 94);
ctx.moveTo(34, 64);
ctx.lineTo(94, 64);
ctx.stroke();
} else {
ctx.beginPath();
ctx.moveTo(64, 27);
ctx.bezierCurveTo(55, 43, 37, 60, 37, 76);
ctx.bezierCurveTo(37, 95, 49, 104, 64, 104);
ctx.bezierCurveTo(79, 104, 91, 95, 91, 76);
ctx.bezierCurveTo(91, 60, 73, 43, 64, 27);
ctx.closePath();
ctx.fill();
}
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.needsUpdate = true;
return texture;
}
const supplyIconTexture = makeIconTexture("supply");
const drainIconTexture = makeIconTexture("drain");
function addSymbol(position, type, scale = 0.58) {
const sprite = new THREE.Sprite(
new THREE.SpriteMaterial({
map: type === "supply"
? supplyIconTexture
: drainIconTexture,
transparent: true,
depthTest: false,
depthWrite: false
})
);
sprite.position.set(...position);
sprite.scale.set(scale, scale, scale);
sprite.renderOrder = 20;
(type === "supply" ? supplyGroup : drainageGroup).add(sprite);
return sprite;
}
function addFlowParticles(curve, type, count, speed) {
const material = new THREE.MeshBasicMaterial({
color: type === "supply" ? COLORS.supplyLight : COLORS.drainLight
});
const radius = type === "supply" ? 0.105 : 0.13;
for (let i = 0; i < count; i++) {
const particle = new THREE.Mesh(
new THREE.SphereGeometry(radius, 10, 8),
material
);
(type === "supply" ? supplyGroup : drainageGroup).add(particle);
flowParticles.push({
mesh: particle,
curve,
offset: i / count,
speed,
lift: type === "supply" ? 0.21 : 0.27
});
}
}
// ---------------------------------------------------------------------
// Water supply network:
// Water Works -> Civic Tower -> Clinic.
// ---------------------------------------------------------------------
const supplyMain = new THREE.CatmullRomCurve3(
[
new THREE.Vector3(-7.52, 0.28, -5.2),
new THREE.Vector3(-5.2, 0.28, -5.2),
new THREE.Vector3(-1.3, 0.28, -5.2),
new THREE.Vector3(1.6, 0.28, -4.9),
new THREE.Vector3(4.0, 0.28, -3.7)
],
false,
"catmullrom",
0.08
);
const supplyBranch = new THREE.CatmullRomCurve3(
[
new THREE.Vector3(4.0, 0.28, -3.7),
new THREE.Vector3(4.0, 0.28, -1.7),
new THREE.Vector3(4.2, 0.28, 0.2),
new THREE.Vector3(5.6, 0.28, 1.1),
new THREE.Vector3(7.0, 0.28, 1.1)
],
false,
"catmullrom",
0.08
);
addPipe(supplyMain, 0.17, supplyPipeMaterial, supplyGroup);
addPipe(supplyBranch, 0.17, supplyPipeMaterial, supplyGroup);
addCouplings(
supplyMain,
0.17,
supplyCouplingMaterial,
supplyGroup,
3.1
);
addCouplings(
supplyBranch,
0.17,
supplyCouplingMaterial,
supplyGroup,
3.1
);
addArrows(supplyMain, COLORS.supplyLight, supplyGroup, 0.24);
addArrows(supplyBranch, COLORS.supplyLight, supplyGroup, 0.24);
addNode([-7.52, 0.28, -5.2], COLORS.supply, 0.25, supplyGroup);
addNode([4.0, 0.28, -3.7], COLORS.supply, 0.25, supplyGroup);
addNode([7.0, 0.28, 1.1], COLORS.supply, 0.25, supplyGroup);
addSymbol([-7.5, 0.82, -5.2], "supply", 0.62);
addSymbol(
supplyMain.getPointAt(0.47).toArray().map((v, i) =>
i === 1 ? v + 0.54 : v
),
"supply",
0.53
);
addSymbol([4.0, 0.82, -3.7], "supply", 0.56);
addSymbol([7.0, 0.82, 1.1], "supply", 0.56);
addFlowParticles(supplyMain, "supply", 7, 0.075);
addFlowParticles(supplyBranch, "supply", 5, 0.085);
// ---------------------------------------------------------------------
// Drainage network:
// Civic drain + Clinic drain -> East Wastewater Treatment Plant.
// ---------------------------------------------------------------------
const drainMain = new THREE.CatmullRomCurve3(
[
new THREE.Vector3(4.0, -0.85, -4.9),
new THREE.Vector3(4.0, -0.85, -2.8),
new THREE.Vector3(5.9, -0.85, -1.7),
new THREE.Vector3(6.0, -0.85, 1.0),
new THREE.Vector3(6.0, -0.85, 3.8),
new THREE.Vector3(8.8, -0.85, 4.72)
],
false,
"catmullrom",
0.08
);
const drainBranch = new THREE.CatmullRomCurve3(
[
new THREE.Vector3(7.0, -0.85, 2.3),
new THREE.Vector3(6.5, -0.85, 2.3),
new THREE.Vector3(6.0, -0.85, 2.3)
],
false,
"catmullrom",
0.08
);
addPipe(drainMain, 0.22, drainPipeMaterial, drainageGroup);
addPipe(drainBranch, 0.22, drainPipeMaterial, drainageGroup);
addCouplings(
drainMain,
0.22,
drainCouplingMaterial,
drainageGroup,
3.4
);
addCouplings(
drainBranch,
0.22,
drainCouplingMaterial,
drainageGroup,
2.8
);
addArrows(drainMain, COLORS.drainLight, drainageGroup, 0.29);
addArrows(drainBranch, COLORS.drainLight, drainageGroup, 0.29);
addNode([4.0, -0.85, -4.9], COLORS.drain, 0.29, drainageGroup);
addNode([7.0, -0.85, 2.3], COLORS.drain, 0.29, drainageGroup);
addNode([6.0, -0.85, 2.3], COLORS.drain, 0.29, drainageGroup);
addNode([8.8, -0.85, 4.72], COLORS.drain, 0.29, drainageGroup);
cylinderBetween(
[4.0, -0.85, -4.9],
[4.0, 0.12, -4.9],
0.22,
drainPipeMaterial,
drainageGroup
);
cylinderBetween(
[7.0, -0.85, 2.3],
[7.0, 0.12, 2.3],
0.22,
drainPipeMaterial,
drainageGroup
);
cylinderBetween(
[8.8, -0.85, 4.72],
[8.8, 0.18, 4.72],
0.22,
drainPipeMaterial,
drainageGroup
);
addSymbol([4.0, -0.42, -4.9], "drain", 0.56);
addSymbol(
drainMain.getPointAt(0.48).toArray().map((v, i) =>
i === 1 ? v + 0.57 : v
),
"drain",
0.55
);
addSymbol([7.0, -0.42, 2.3], "drain", 0.56);
addSymbol([8.8, -0.42, 4.72], "drain", 0.56);
addFlowParticles(drainMain, "drain", 8, 0.065);
addFlowParticles(drainBranch, "drain", 3, 0.075);
// Visible separation at the point where the routes cross in plan.
const separationGeometry = new THREE.BufferGeometry().setFromPoints([
new THREE.Vector3(6.0, -0.62, 1.04),
new THREE.Vector3(6.0, 0.07, 1.04)
]);
const separationLine = new THREE.Line(
separationGeometry,
new THREE.LineDashedMaterial({
color: COLORS.safe,
dashSize: 0.12,
gapSize: 0.09,
transparent: true,
opacity: 0.9,
depthTest: false
})
);
separationLine.computeLineDistances();
separationLine.renderOrder = 25;
scene.add(separationLine);
// ---------------------------------------------------------------------
// Projected HTML labels.
// ---------------------------------------------------------------------
function iconSvg(type) {
if (type === "supply") {
return `
<svg viewBox="0 0 16 16" aria-hidden="true">
<path d="M8 3v10M3 8h10"
fill="none" stroke="currentColor"
stroke-width="2.4" stroke-linecap="round"/>
</svg>`;
}
return `
<svg viewBox="0 0 16 16" aria-hidden="true">
<path d="M8 1.2S2.8 7.1 2.8 10.2a5.2 5.2 0 0 0 10.4 0C13.2 7.1 8 1.2 8 1.2Z"
fill="currentColor"/>
</svg>`;
}
function addLabel({
type,
role,
title,
subtitle,
position,
offset = [0, 0],
network = type
}) {
const element = document.createElement("div");
element.className = `net-label ${type}`;
element.innerHTML = `
<span class="label-icon">${iconSvg(type)}</span>
<span class="label-copy">
<span class="label-role">${role}</span>
<span class="label-title">${title}</span>
<span class="label-sub">${subtitle}</span>
</span>
`;
labelLayer.appendChild(element);
labels.push({
element,
position: new THREE.Vector3(...position),
offset,
network
});
}
addLabel({
type: "supply",
role: "Supply source",
title: "North Water Works",
subtitle: "Potable storage · pressure origin",
position: [-9.0, 2.28, -5.2],
offset: [-45, -14]
});
addLabel({
type: "supply",
role: "Supply destination",
title: "Civic Tower",
subtitle: "Pressurized domestic connection",
position: [4.0, 3.85, -4.9],
offset: [-20, -30]
});
addLabel({
type: "supply",
role: "Supply destination",
title: "Community Clinic",
subtitle: "Pressurized domestic connection",
position: [7.0, 2.55, 2.3],
offset: [52, -28]
});
addLabel({
type: "drain",
role: "Drain source",
title: "Civic Tower Drain",
subtitle: "Gravity collection · invert −0.85",
position: [4.0, -1.13, -4.9],
offset: [-42, 28]
});
addLabel({
type: "drain",
role: "Drain source",
title: "Clinic Drain",
subtitle: "Gravity collection · invert −0.85",
position: [7.0, -1.12, 2.3],
offset: [48, 27]
});
addLabel({
type: "drain",
role: "Drain destination",
title: "East WWTP",
subtitle: "Wastewater treatment · gravity terminal",
position: [8.8, 1.55, 5.75],
offset: [62, -6]
});
const isolationLabel = document.createElement("div");
isolationLabel.className = "isolation-label";
isolationLabel.textContent = "0.71 m clearance · no connection";
labelLayer.appendChild(isolationLabel);
const isolationAnchor = new THREE.Vector3(6.0, -0.27, 1.04);
// ---------------------------------------------------------------------
// Custom camera controls. No examples or addons are imported.
// ---------------------------------------------------------------------
const orbit = {
target: new THREE.Vector3(0, -0.45, -0.55),
theta: 0.72,
phi: 1.02,
radius: 30,
goal: null
};
const cameraOffset = new THREE.Vector3();
const cameraDirection = new THREE.Vector3();
const cameraRight = new THREE.Vector3();
const cameraUp = new THREE.Vector3();
const worldUp = new THREE.Vector3(0, 1, 0);
function updateCamera() {
cameraOffset.setFromSphericalCoords(
orbit.radius,
orbit.phi,
orbit.theta
);
camera.position.copy(orbit.target).add(cameraOffset);
camera.lookAt(orbit.target);
camera.updateMatrixWorld();
}
function setCameraGoal(target, radius, theta, phi) {
orbit.goal = {
target: new THREE.Vector3(...target),
radius,
theta,
phi
};
}
function resetCamera(mode = currentMode) {
if (mode === "supply") {
setCameraGoal([0.1, -0.2, -2.25], 23.5, 0.76, 1.06);
} else if (mode === "drainage") {
setCameraGoal([4.0, -0.55, 1.2], 21.5, 0.72, 1.08);
} else {
setCameraGoal([0, -0.45, -0.55], 30, 0.72, 1.02);
}
}
const canvas = renderer.domElement;
const activePointers = new Map();
let dragMode = null;
let lastPointer = { x: 0, y: 0 };
let lastPinchDistance = 0;
let lastPinchCenter = null;
canvas.addEventListener("contextmenu", (event) => {
event.preventDefault();
});
canvas.addEventListener("pointerdown", (event) => {
canvas.setPointerCapture(event.pointerId);
orbit.goal = null;
activePointers.set(event.pointerId, {
x: event.clientX,
y: event.clientY
});
if (activePointers.size === 2) {
const points = [...activePointers.values()];
lastPinchDistance = Math.hypot(
points[0].x - points[1].x,
points[0].y - points[1].y
);
lastPinchCenter = {
x: (points[0].x + points[1].x) / 2,
y: (points[0].y + points[1].y) / 2
};
dragMode = "pinch";
} else {
dragMode =
event.button === 2 || event.shiftKey
? "pan"
: "orbit";
}
lastPointer = {
x: event.clientX,
y: event.clientY
};
});
canvas.addEventListener("pointermove", (event) => {
if (!activePointers.has(event.pointerId)) return;
const previous = activePointers.get(event.pointerId);
previous.x = event.clientX;
previous.y = event.clientY;
if (dragMode === "pinch" && activePointers.size >= 2) {
const points = [...activePointers.values()];
const distance = Math.hypot(
points[0].x - points[1].x,
points[0].y - points[1].y
);
const center = {
x: (points[0].x + points[1].x) / 2,
y: (points[0].y + points[1].y) / 2
};
if (lastPinchDistance > 0) {
orbit.radius = THREE.MathUtils.clamp(
orbit.radius * (lastPinchDistance / Math.max(distance, 1)),
12,
46
);
}
if (lastPinchCenter) {
panCamera(
center.x - lastPinchCenter.x,
center.y - lastPinchCenter.y
);
}
lastPinchDistance = distance;
lastPinchCenter = center;
return;
}
const dx = event.clientX - lastPointer.x;
const dy = event.clientY - lastPointer.y;
lastPointer = {
x: event.clientX,
y: event.clientY
};
if (dragMode === "orbit") {
orbit.theta -= dx * 0.006;
orbit.phi = THREE.MathUtils.clamp(
orbit.phi - dy * 0.006,
0.22,
1.46
);
} else if (dragMode === "pan") {
panCamera(dx, dy);
}
});
function endPointer(event) {
activePointers.delete(event.pointerId);
if (activePointers.size === 0) {
dragMode = null;
lastPinchDistance = 0;
lastPinchCenter = null;
} else if (activePointers.size === 1) {
const remaining = [...activePointers.values()][0];
lastPointer = { x: remaining.x, y: remaining.y };
dragMode = "orbit";
}
}
canvas.addEventListener("pointerup", endPointer);
canvas.addEventListener("pointercancel", endPointer);
function panCamera(dx, dy) {
camera.getWorldDirection(cameraDirection);
cameraRight.crossVectors(cameraDirection, worldUp).normalize();
cameraUp.crossVectors(cameraRight, cameraDirection).normalize();
const scale = orbit.radius * 0.00165;
orbit.target
.addScaledVector(cameraRight, -dx * scale)
.addScaledVector(cameraUp, dy * scale);
orbit.target.x = THREE.MathUtils.clamp(orbit.target.x, -12, 12);
orbit.target.y = THREE.MathUtils.clamp(orbit.target.y, -3, 4);
orbit.target.z = THREE.MathUtils.clamp(orbit.target.z, -10, 10);
}
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
orbit.goal = null;
orbit.radius = THREE.MathUtils.clamp(
orbit.radius * Math.exp(event.deltaY * 0.001),
12,
46
);
},
{ passive: false }
);
canvas.addEventListener("dblclick", () => {
resetCamera(currentMode);
});
// ---------------------------------------------------------------------
// Network isolation and UI.
// ---------------------------------------------------------------------
let currentMode = "all";
let flowEnabled = true;
let labelsEnabled = true;
function setMode(mode, moveCamera = true) {
currentMode = mode;
supplyGroup.visible = mode !== "drainage";
drainageGroup.visible = mode !== "supply";
separationLine.visible = mode === "all";
document.querySelectorAll("[data-mode]").forEach((button) => {
button.classList.toggle("active", button.dataset.mode === mode);
});
if (moveCamera) resetCamera(mode);
}
document.querySelectorAll("[data-mode]").forEach((button) => {
button.addEventListener("click", () => {
setMode(button.dataset.mode, true);
});
});
document.getElementById("reset-button").addEventListener("click", () => {
resetCamera(currentMode);
});
document.getElementById("flow-toggle").addEventListener("change", (event) => {
flowEnabled = event.target.checked;
flowParticles.forEach(({ mesh }) => {
mesh.visible = flowEnabled;
});
});
document.getElementById("labels-toggle").addEventListener("change", (event) => {
labelsEnabled = event.target.checked;
labelLayer.style.display = labelsEnabled ? "block" : "none";
});
// ---------------------------------------------------------------------
// Animation and projected label placement.
// ---------------------------------------------------------------------
const clock = new THREE.Clock();
const projected = new THREE.Vector3();
let firstFrame = true;
function updateLabels() {
if (!labelsEnabled) return;
const width = window.innerWidth;
const height = window.innerHeight;
for (const label of labels) {
const visible =
label.network === "all" ||
(currentMode === "all") ||
label.network === currentMode;
projected.copy(label.position).project(camera);
const onScreen =
projected.z > -1 &&
projected.z < 1 &&
projected.x > -1.2 &&
projected.x < 1.2 &&
projected.y > -1.2 &&
projected.y < 1.2;
if (!visible || !onScreen) {
label.element.style.opacity = "0";
label.element.style.visibility = "hidden";
continue;
}
const x = (projected.x * 0.5 + 0.5) * width + label.offset[0];
const y = (-projected.y * 0.5 + 0.5) * height + label.offset[1];
label.element.style.visibility = "visible";
label.element.style.opacity = "1";
label.element.style.transform =
`translate3d(${x}px, ${y}px, 0) translate(-50%, -50%)`;
}
projected.copy(isolationAnchor).project(camera);
const isolationVisible =
currentMode === "all" &&
projected.z > -1 &&
projected.z < 1;
if (isolationVisible) {
const x = (projected.x * 0.5 + 0.5) * width;
const y = (-projected.y * 0.5 + 0.5) * height - 22;
isolationLabel.style.visibility = "visible";
isolationLabel.style.opacity = "1";
isolationLabel.style.transform =
`translate3d(${x}px, ${y}px, 0) translate(-50%, -50%)`;
} else {
isolationLabel.style.visibility = "hidden";
isolationLabel.style.opacity = "0";
}
}
function animate() {
const delta = Math.min(clock.getDelta(), 0.05);
const elapsed = clock.elapsedTime;
if (orbit.goal) {
const alpha = 1 - Math.exp(-delta * 5.2);
orbit.target.lerp(orbit.goal.target, alpha);
orbit.radius = THREE.MathUtils.lerp(
orbit.radius,
orbit.goal.radius,
alpha
);
orbit.theta = THREE.MathUtils.lerp(
orbit.theta,
orbit.goal.theta,
alpha
);
orbit.phi = THREE.MathUtils.lerp(
orbit.phi,
orbit.goal.phi,
alpha
);
if (
orbit.target.distanceTo(orbit.goal.target) < 0.01 &&
Math.abs(orbit.radius - orbit.goal.radius) < 0.01 &&
Math.abs(orbit.theta - orbit.goal.theta) < 0.001 &&
Math.abs(orbit.phi - orbit.goal.phi) < 0.001
) {
orbit.goal = null;
}
}
updateCamera();
if (flowEnabled) {
for (const particle of flowParticles) {
const t = (particle.offset + elapsed * particle.speed) % 1;
const position = particle.curve.getPointAt(t);
position.y += particle.lift;
particle.mesh.position.copy(position);
}
}
renderer.render(scene, camera);
updateLabels();
if (firstFrame) {
firstFrame = false;
loading.classList.add("ready");
setTimeout(() => loading.remove(), 400);
}
}
renderer.setAnimationLoop(animate);
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
});
updateCamera();
</script>
</body>
</html>
```