Prompt specification
Build or customize the Three.js 3D scene "Parallel pipes carrying equal flow" 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: Parallel pipes carrying equal flow
Category: Science & Physics · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/parallel-pipes-carrying-equal-flow-x1454
Scene Description Prompt:
Build a self-contained HTML scene with Three.js from cdnjs featuring flow continuity through pipes and junctions. Place two pipes of different areas side by side with equal volumetric flow; display their different mean speeds and preserve the same counted volume per second. Stylized solid forms.
Working Three.js Starter (parallel-pipes-carrying-equal-flow-x1454):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Flow Continuity Lab</title>
<style>
:root {
--bg: #07101f;
--panel: rgba(12, 22, 42, 0.82);
--line: rgba(160, 190, 230, 0.18);
--text: #eef5ff;
--muted: #91a4c2;
--small: #ff7b5c;
--large: #38a3ff;
--flow: #ffd166;
--purple: #9b7cff;
--good: #66e6b5;
}
* {
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;
}
#scene {
position: fixed;
inset: 0;
width: 100%;
height: 100%;
display: block;
touch-action: none;
cursor: grab;
}
#scene:active {
cursor: grabbing;
}
#vignette {
position: fixed;
inset: 0;
pointer-events: none;
background:
radial-gradient(circle at 52% 45%, transparent 25%, rgba(1, 6, 16, 0.44) 100%),
linear-gradient(180deg, rgba(3, 8, 18, 0.08), rgba(3, 8, 18, 0.32));
z-index: 1;
}
.panel {
position: fixed;
z-index: 5;
border: 1px solid var(--line);
background: var(--panel);
box-shadow:
0 18px 50px rgba(0, 0, 0, 0.24),
inset 0 1px 0 rgba(255, 255, 255, 0.04);
backdrop-filter: blur(18px);
-webkit-backdrop-filter: blur(18px);
}
#intro {
top: 22px;
left: 22px;
width: min(390px, calc(100vw - 44px));
padding: 20px 22px 19px;
border-radius: 18px;
}
.eyebrow {
display: flex;
align-items: center;
gap: 9px;
color: var(--good);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.18em;
text-transform: uppercase;
}
.live-dot {
width: 7px;
height: 7px;
border-radius: 50%;
background: var(--good);
box-shadow: 0 0 15px var(--good);
}
h1 {
margin: 11px 0 8px;
font-size: clamp(25px, 3vw, 38px);
line-height: 0.98;
letter-spacing: -0.045em;
}
#intro p {
margin: 0;
color: var(--muted);
font-size: 13px;
line-height: 1.55;
}
.equation {
display: flex;
align-items: center;
gap: 11px;
margin-top: 15px;
padding-top: 13px;
border-top: 1px solid var(--line);
color: var(--muted);
font-size: 12px;
}
.equation strong {
color: var(--text);
font-size: 16px;
letter-spacing: 0.02em;
}
.equation .equals {
color: var(--flow);
font-size: 18px;
font-weight: 300;
}
#balance {
top: 22px;
right: 22px;
width: 330px;
padding: 17px 18px 16px;
border-radius: 18px;
}
.balance-head {
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: 13px;
}
.balance-head span:first-child {
color: var(--muted);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.15em;
text-transform: uppercase;
}
.balance-status {
padding: 4px 7px;
border: 1px solid rgba(102, 230, 181, 0.24);
border-radius: 999px;
color: var(--good);
background: rgba(102, 230, 181, 0.08);
font-size: 9px;
font-weight: 800;
letter-spacing: 0.12em;
}
.balance-grid {
display: grid;
grid-template-columns: 1fr auto 1fr;
align-items: center;
gap: 10px;
}
.balance-node {
padding: 10px 11px;
border: 1px solid var(--line);
border-radius: 11px;
background: rgba(255, 255, 255, 0.025);
}
.balance-node small {
display: block;
margin-bottom: 4px;
color: var(--muted);
font-size: 9px;
font-weight: 800;
letter-spacing: 0.12em;
}
.balance-node strong {
font-size: 17px;
font-variant-numeric: tabular-nums;
}
.balance-node strong em {
color: var(--flow);
font-style: normal;
}
.split-arrow {
color: var(--flow);
font-size: 23px;
text-align: center;
}
#balanceFooter {
display: flex;
justify-content: space-between;
margin-top: 11px;
color: var(--muted);
font-size: 10px;
}
#balanceFooter strong {
color: var(--good);
font-variant-numeric: tabular-nums;
}
#metrics {
position: fixed;
z-index: 5;
left: 22px;
bottom: 22px;
display: flex;
gap: 12px;
pointer-events: none;
}
.metric {
width: 278px;
padding: 16px 17px 15px;
border: 1px solid var(--line);
border-radius: 17px;
background: rgba(12, 22, 42, 0.86);
box-shadow: 0 18px 50px rgba(0, 0, 0, 0.25);
backdrop-filter: blur(18px);
-webkit-backdrop-filter: blur(18px);
}
.metric.small {
border-top: 2px solid var(--small);
}
.metric.large {
border-top: 2px solid var(--large);
}
.metric-title {
display: flex;
justify-content: space-between;
align-items: center;
color: var(--muted);
font-size: 9px;
font-weight: 800;
letter-spacing: 0.14em;
text-transform: uppercase;
}
.metric-title b {
color: var(--small);
}
.metric.large .metric-title b {
color: var(--large);
}
.metric-values {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 13px;
margin-top: 13px;
}
.metric-values small {
display: block;
margin-bottom: 3px;
color: var(--muted);
font-size: 9px;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.metric-values strong {
font-size: 20px;
font-variant-numeric: tabular-nums;
}
.metric-values span {
color: var(--muted);
font-size: 10px;
}
.count-row {
display: flex;
align-items: end;
justify-content: space-between;
margin-top: 13px;
padding-top: 11px;
border-top: 1px solid var(--line);
}
.count-row small {
color: var(--muted);
font-size: 9px;
line-height: 1.45;
}
.count-row strong {
color: var(--flow);
font-size: 16px;
font-variant-numeric: tabular-nums;
}
.packet-row {
display: flex;
justify-content: space-between;
margin-top: 8px;
color: var(--muted);
font-size: 9px;
font-variant-numeric: tabular-nums;
}
.packet-row b {
color: var(--text);
font-weight: 700;
}
#controls {
position: fixed;
z-index: 6;
right: 22px;
bottom: 22px;
display: flex;
align-items: center;
gap: 8px;
padding: 8px;
border: 1px solid var(--line);
border-radius: 14px;
background: rgba(12, 22, 42, 0.86);
backdrop-filter: blur(18px);
-webkit-backdrop-filter: blur(18px);
}
button {
border: 1px solid var(--line);
border-radius: 9px;
padding: 8px 11px;
color: var(--text);
background: rgba(255, 255, 255, 0.045);
font: inherit;
font-size: 10px;
font-weight: 700;
cursor: pointer;
transition: background 150ms ease, transform 150ms ease;
}
button:hover {
background: rgba(255, 255, 255, 0.1);
}
button:active {
transform: translateY(1px);
}
.control-hint {
padding: 0 7px 0 5px;
color: var(--muted);
font-size: 9px;
white-space: nowrap;
}
.tag {
position: fixed;
z-index: 4;
transform: translate(-50%, -50%);
padding: 7px 10px;
border: 1px solid var(--line);
border-radius: 9px;
background: rgba(7, 16, 31, 0.78);
box-shadow: 0 8px 24px rgba(0, 0, 0, 0.22);
backdrop-filter: blur(10px);
pointer-events: none;
white-space: nowrap;
font-size: 9px;
font-weight: 800;
letter-spacing: 0.09em;
text-transform: uppercase;
}
.tag::before {
content: "";
display: inline-block;
width: 7px;
height: 7px;
margin-right: 7px;
border-radius: 50%;
background: var(--tag-color);
box-shadow: 0 0 10px var(--tag-color);
}
#tagSmall {
--tag-color: var(--small);
}
#tagLarge {
--tag-color: var(--large);
}
#loading {
position: fixed;
z-index: 20;
inset: 0;
display: grid;
place-items: center;
color: var(--muted);
background: var(--bg);
font-size: 11px;
letter-spacing: 0.14em;
text-transform: uppercase;
transition: opacity 350ms ease;
}
#loading.hidden {
opacity: 0;
pointer-events: none;
}
@media (max-width: 900px) {
#balance {
top: auto;
right: 14px;
bottom: 76px;
width: 285px;
}
#intro {
top: 14px;
left: 14px;
}
#metrics {
left: 14px;
bottom: 14px;
}
.metric {
width: 235px;
}
#controls {
right: 14px;
bottom: 14px;
}
.control-hint {
display: none;
}
}
@media (max-width: 650px) {
#intro {
width: calc(100vw - 28px);
padding: 15px 17px;
}
#intro p {
font-size: 11px;
}
.equation {
margin-top: 10px;
padding-top: 10px;
}
#balance {
display: none;
}
#metrics {
right: 14px;
gap: 7px;
}
.metric {
width: calc(50vw - 17px);
min-width: 0;
padding: 12px;
}
.metric-values {
gap: 5px;
}
.metric-values strong {
font-size: 16px;
}
.metric-title {
font-size: 8px;
}
.count-row {
align-items: start;
flex-direction: column;
gap: 5px;
}
#controls {
left: 14px;
right: auto;
}
}
</style>
</head>
<body>
<canvas id="scene"></canvas>
<div id="vignette"></div>
<header id="intro" class="panel">
<div class="eyebrow">
<span class="live-dot"></span>
Live conservation experiment
</div>
<h1>Flow Continuity<br />Through Junctions</h1>
<p>
One inlet divides equally into two pipes of different area.
The larger pipe moves more slowly, while both discharge the same
counted volume every second.
</p>
<div class="equation">
<strong>Q = A · v̄</strong>
<span class="equals">=</span>
<span>area × mean speed</span>
</div>
</header>
<section id="balance" class="panel">
<div class="balance-head">
<span>Junction conservation</span>
<span class="balance-status">BALANCED</span>
</div>
<div class="balance-grid">
<div class="balance-node">
<small>INLET</small>
<strong><em>8.00</em> u³/s</strong>
</div>
<div class="split-arrow">→</div>
<div class="balance-node">
<small>1:1 SPLIT</small>
<strong><em>4.00</em> + 4.00</strong>
</div>
</div>
<div id="balanceFooter">
<span>Measured branch difference</span>
<strong id="balanceValue">|ΔQ| = 0.00 u³/s</strong>
</div>
</section>
<section id="metrics">
<article class="metric small">
<div class="metric-title">
<b>01 / Small bore</b>
<span>A = 1.50 u²</span>
</div>
<div class="metric-values">
<div>
<small>Mean speed</small>
<strong>2.67</strong> <span>u/s</span>
</div>
<div>
<small>Target flow</small>
<strong>4.00</strong> <span>u³/s</span>
</div>
</div>
<div class="count-row">
<small>Counted volume<br />over the last 1.00 s</small>
<strong id="smallRate">— u³/s</strong>
</div>
<div class="packet-row">
<span><b id="smallPackets">warming up</b> packets/s</span>
<span>0.50 u³ each</span>
</div>
<div class="packet-row">
<span>Total packets counted</span>
<span><b id="smallTotal">0</b></span>
</div>
</article>
<article class="metric large">
<div class="metric-title">
<b>02 / Large bore</b>
<span>A = 3.75 u²</span>
</div>
<div class="metric-values">
<div>
<small>Mean speed</small>
<strong>1.07</strong> <span>u/s</span>
</div>
<div>
<small>Target flow</small>
<strong>4.00</strong> <span>u³/s</span>
</div>
</div>
<div class="count-row">
<small>Counted volume<br />over the last 1.00 s</small>
<strong id="largeRate">— u³/s</strong>
</div>
<div class="packet-row">
<span><b id="largePackets">warming up</b> packets/s</span>
<span>0.50 u³ each</span>
</div>
<div class="packet-row">
<span>Total packets counted</span>
<span><b id="largeTotal">0</b></span>
</div>
</article>
</section>
<div id="tagSmall" class="tag">Small bore · faster flow</div>
<div id="tagLarge" class="tag">Large bore · slower flow</div>
<div id="controls">
<button id="pauseButton" type="button">Pause flow</button>
<button id="resetButton" type="button">Reset view</button>
<span class="control-hint">Drag to orbit · scroll to zoom</span>
</div>
<div id="loading">Building flow system…</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// ------------------------------------------------------------
// Constants and simulation model
// ------------------------------------------------------------
const PACKET_VOLUME = 0.5;
const BRANCH_RATE = 4.0;
const TOTAL_RATE = 8.0;
const PACKET_RATE = BRANCH_RATE / PACKET_VOLUME;
const SMALL_AREA = 1.5;
const LARGE_AREA = 3.75;
const INLET_AREA = 4.0;
const SMALL_SPEED = BRANCH_RATE / SMALL_AREA;
const LARGE_SPEED = BRANCH_RATE / LARGE_AREA;
const INLET_SPEED = TOTAL_RATE / INLET_AREA;
const SMALL_RADIUS = Math.sqrt(SMALL_AREA / Math.PI);
const LARGE_RADIUS = Math.sqrt(LARGE_AREA / Math.PI);
const INLET_RADIUS = Math.sqrt(INLET_AREA / Math.PI);
// Chosen so each branch counts exactly eight packets per second:
// countRate = particleCount * speed / pathLength.
const PATH_LENGTH = 22 / 3;
const HUB = new THREE.Vector3(-2.65, 0, 0);
const ELBOW_X = -1.35;
const BRANCH_SEPARATION = 1.25;
const CONNECTOR_LENGTH = Math.hypot(
ELBOW_X - HUB.x,
BRANCH_SEPARATION
);
const STRAIGHT_LENGTH = PATH_LENGTH - CONNECTOR_LENGTH;
const OUTLET_X = ELBOW_X + STRAIGHT_LENGTH;
const SMALL_Y = BRANCH_SEPARATION;
const LARGE_Y = -BRANCH_SEPARATION;
const SMALL_ELBOW = new THREE.Vector3(ELBOW_X, SMALL_Y, 0);
const LARGE_ELBOW = new THREE.Vector3(ELBOW_X, LARGE_Y, 0);
const SMALL_END = new THREE.Vector3(OUTLET_X, SMALL_Y, 0);
const LARGE_END = new THREE.Vector3(OUTLET_X, LARGE_Y, 0);
const INLET_START = new THREE.Vector3(-7, 0, 0);
const INLET_END = new THREE.Vector3(-3, 0, 0);
const INLET_LENGTH = INLET_END.x - INLET_START.x;
const colors = {
background: 0x07101f,
inlet: 0x9b7cff,
small: 0xff7b5c,
large: 0x38a3ff,
particle: 0xffd166,
hub: 0x5de3d0
};
// ------------------------------------------------------------
// Renderer, scene, camera, and lighting
// ------------------------------------------------------------
const canvas = document.querySelector("#scene");
const renderer = new THREE.WebGLRenderer({
canvas,
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.12;
const scene = new THREE.Scene();
scene.background = new THREE.Color(colors.background);
scene.fog = new THREE.FogExp2(colors.background, 0.035);
const camera = new THREE.PerspectiveCamera(
43,
window.innerWidth / window.innerHeight,
0.1,
80
);
const cameraTarget = new THREE.Vector3(-0.15, -0.05, 0);
let cameraYaw = 0.57;
let cameraPitch = 0.34;
let cameraDistance = 15.5;
function updateCamera() {
const horizontal = cameraDistance * Math.cos(cameraPitch);
camera.position.set(
cameraTarget.x + horizontal * Math.sin(cameraYaw),
cameraTarget.y + cameraDistance * Math.sin(cameraPitch),
cameraTarget.z + horizontal * Math.cos(cameraYaw)
);
camera.lookAt(cameraTarget);
}
updateCamera();
scene.add(new THREE.HemisphereLight(0xb9d8ff, 0x182038, 2.0));
const keyLight = new THREE.DirectionalLight(0xffffff, 3.2);
keyLight.position.set(4, 9, 7);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -10;
keyLight.shadow.camera.right = 10;
keyLight.shadow.camera.top = 9;
keyLight.shadow.camera.bottom = -9;
keyLight.shadow.camera.near = 1;
keyLight.shadow.camera.far = 28;
keyLight.shadow.normalBias = 0.025;
scene.add(keyLight);
const rimLight = new THREE.DirectionalLight(0x5b8cff, 2.0);
rimLight.position.set(-7, 3, -5);
scene.add(rimLight);
const warmLight = new THREE.PointLight(0xffa35c, 35, 12, 2);
warmLight.position.set(4, 3, 4);
scene.add(warmLight);
// ------------------------------------------------------------
// Ground and background grid
// ------------------------------------------------------------
const ground = new THREE.Mesh(
new THREE.PlaneGeometry(34, 22),
new THREE.MeshStandardMaterial({
color: 0x0d1729,
roughness: 0.93,
metalness: 0.03
})
);
ground.rotation.x = -Math.PI / 2;
ground.position.y = -2.84;
ground.receiveShadow = true;
scene.add(ground);
const grid = new THREE.GridHelper(
30,
30,
0x334765,
0x1b2a43
);
grid.position.y = -2.825;
grid.material.transparent = true;
grid.material.opacity = 0.42;
scene.add(grid);
// ------------------------------------------------------------
// Stylized half-pipe geometry
// ------------------------------------------------------------
function createHalfPipeGeometry(radius, length, thickness = 0.13) {
const positions = [];
const indices = [];
const segments = 30;
const innerRadius = radius - thickness;
const halfLength = length / 2;
function addQuad(a, b, c, d, flip = false) {
const base = positions.length / 3;
positions.push(...a, ...b, ...c, ...d);
if (flip) {
indices.push(
base, base + 2, base + 1,
base, base + 3, base + 2
);
} else {
indices.push(
base, base + 1, base + 2,
base, base + 2, base + 3
);
}
}
for (let i = 0; i < segments; i++) {
const t0 = (i / segments) * Math.PI;
const t1 = ((i + 1) / segments) * Math.PI;
const outer0 = [
-halfLength,
-radius * Math.sin(t0),
radius * Math.cos(t0)
];
const outer1 = [
halfLength,
-radius * Math.sin(t0),
radius * Math.cos(t0)
];
const outer2 = [
halfLength,
-radius * Math.sin(t1),
radius * Math.cos(t1)
];
const outer3 = [
-halfLength,
-radius * Math.sin(t1),
radius * Math.cos(t1)
];
const inner0 = [
-halfLength,
-innerRadius * Math.sin(t0),
innerRadius * Math.cos(t0)
];
const inner1 = [
halfLength,
-innerRadius * Math.sin(t0),
innerRadius * Math.cos(t0)
];
const inner2 = [
halfLength,
-innerRadius * Math.sin(t1),
innerRadius * Math.cos(t1)
];
const inner3 = [
-halfLength,
-innerRadius * Math.sin(t1),
innerRadius * Math.cos(t1)
];
addQuad(outer0, outer1, outer2, outer3);
addQuad(inner0, inner3, inner2, inner1, true);
// End caps.
addQuad(
outer0, outer3, inner3, inner0,
true
);
addQuad(
outer1, inner1, inner2, outer2,
true
);
}
// Cut edges at the open top of each pipe.
addQuad(
[-halfLength, 0, radius],
[halfLength, 0, radius],
[halfLength, 0, innerRadius],
[-halfLength, 0, innerRadius]
);
addQuad(
[-halfLength, 0, -radius],
[-halfLength, 0, -innerRadius],
[halfLength, 0, -innerRadius],
[halfLength, 0, -radius]
);
const geometry = new THREE.BufferGeometry();
geometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(positions, 3)
);
geometry.setIndex(indices);
geometry.computeVertexNormals();
return geometry;
}
function addPipe({
xStart,
length,
y,
radius,
color,
rings = 4
}) {
const group = new THREE.Group();
const xCenter = xStart + length / 2;
const accent = new THREE.Color(color);
const lowerMaterial = new THREE.MeshStandardMaterial({
color: accent.clone().multiplyScalar(0.58),
roughness: 0.46,
metalness: 0.12,
side: THREE.DoubleSide
});
const lowerShell = new THREE.Mesh(
createHalfPipeGeometry(radius, length),
lowerMaterial
);
lowerShell.position.set(xCenter, y, 0);
lowerShell.castShadow = true;
lowerShell.receiveShadow = true;
group.add(lowerShell);
const shellMaterial = new THREE.MeshPhysicalMaterial({
color: accent,
roughness: 0.25,
metalness: 0.08,
transparent: true,
opacity: 0.14,
depthWrite: false,
side: THREE.DoubleSide
});
const transparentShell = new THREE.Mesh(
new THREE.CylinderGeometry(
radius,
radius,
length,
48,
1,
true
),
shellMaterial
);
transparentShell.rotation.z = Math.PI / 2;
transparentShell.position.set(xCenter, y, 0);
transparentShell.renderOrder = 3;
group.add(transparentShell);
const collarMaterial = new THREE.MeshStandardMaterial({
color: accent.clone().multiplyScalar(1.08),
roughness: 0.32,
metalness: 0.28
});
function addRing(x, tube, majorOffset = 0.025) {
const ring = new THREE.Mesh(
new THREE.TorusGeometry(radius + majorOffset, tube, 10, 44),
collarMaterial
);
ring.rotation.y = Math.PI / 2;
ring.position.set(x, y, 0);
ring.castShadow = true;
group.add(ring);
}
addRing(xStart, 0.115);
addRing(xStart + length, 0.115);
for (let i = 1; i < rings; i++) {
addRing(
xStart + (length * i) / rings,
0.037,
0.012
);
}
scene.add(group);
return group;
}
function addConnector(start, end, radius, color) {
const direction = new THREE.Vector3().subVectors(end, start);
const length = direction.length();
const midpoint = new THREE.Vector3()
.addVectors(start, end)
.multiplyScalar(0.5);
const material = new THREE.MeshPhysicalMaterial({
color,
roughness: 0.25,
metalness: 0.08,
transparent: true,
opacity: 0.26,
depthWrite: false,
side: THREE.DoubleSide
});
const connector = new THREE.Mesh(
new THREE.CylinderGeometry(
radius,
radius,
length,
36,
1,
true
),
material
);
connector.position.copy(midpoint);
connector.quaternion.setFromUnitVectors(
new THREE.Vector3(0, 1, 0),
direction.clone().normalize()
);
connector.renderOrder = 3;
scene.add(connector);
return connector;
}
// Inlet and two unequal branches.
addPipe({
xStart: INLET_START.x,
length: INLET_LENGTH,
y: 0,
radius: INLET_RADIUS,
color: colors.inlet,
rings: 5
});
addPipe({
xStart: ELBOW_X,
length: STRAIGHT_LENGTH,
y: SMALL_Y,
radius: SMALL_RADIUS,
color: colors.small,
rings: 4
});
addPipe({
xStart: ELBOW_X,
length: STRAIGHT_LENGTH,
y: LARGE_Y,
radius: LARGE_RADIUS,
color: colors.large,
rings: 4
});
addConnector(
HUB,
SMALL_ELBOW,
SMALL_RADIUS,
colors.small
);
addConnector(
HUB,
LARGE_ELBOW,
LARGE_RADIUS,
colors.large
);
// ------------------------------------------------------------
// Faceted junction chamber
// ------------------------------------------------------------
const junctionGeometry = new THREE.IcosahedronGeometry(1.34, 2);
const junction = new THREE.Mesh(
junctionGeometry,
new THREE.MeshPhysicalMaterial({
color: colors.hub,
roughness: 0.18,
metalness: 0.12,
transparent: true,
opacity: 0.22,
depthWrite: false,
side: THREE.DoubleSide
})
);
junction.position.copy(HUB);
junction.renderOrder = 4;
scene.add(junction);
const junctionEdges = new THREE.LineSegments(
new THREE.EdgesGeometry(junctionGeometry, 18),
new THREE.LineBasicMaterial({
color: colors.hub,
transparent: true,
opacity: 0.62
})
);
junctionEdges.position.copy(HUB);
junctionEdges.renderOrder = 5;
scene.add(junctionEdges);
const junctionCore = new THREE.Mesh(
new THREE.IcosahedronGeometry(0.48, 1),
new THREE.MeshStandardMaterial({
color: 0x9effe8,
emissive: 0x24bfa8,
emissiveIntensity: 1.2,
roughness: 0.25
})
);
junctionCore.position.copy(HUB);
junctionCore.castShadow = true;
scene.add(junctionCore);
// ------------------------------------------------------------
// Direction arrows and outlet indicators
// ------------------------------------------------------------
function addFlowArrow(x, y, radius, color) {
const arrow = new THREE.Mesh(
new THREE.ConeGeometry(0.16, 0.43, 14),
new THREE.MeshBasicMaterial({
color,
transparent: true,
opacity: 0.88
})
);
arrow.rotation.z = -Math.PI / 2;
arrow.position.set(x, y, radius + 0.18);
scene.add(arrow);
}
addFlowArrow(-5.3, 0, INLET_RADIUS, colors.inlet);
for (const x of [-0.25, 1.55, 3.35]) {
addFlowArrow(x, SMALL_Y, SMALL_RADIUS, colors.small);
addFlowArrow(x, LARGE_Y, LARGE_RADIUS, colors.large);
}
const pulseMaterial = new THREE.MeshBasicMaterial({
color: colors.particle,
transparent: true,
opacity: 0.9
});
const smallPulse = new THREE.Mesh(
new THREE.SphereGeometry(0.13, 16, 12),
pulseMaterial
);
smallPulse.position.set(OUTLET_X + 0.28, SMALL_Y, 0);
scene.add(smallPulse);
const largePulse = new THREE.Mesh(
new THREE.SphereGeometry(0.13, 16, 12),
pulseMaterial.clone()
);
largePulse.position.set(OUTLET_X + 0.28, LARGE_Y, 0);
scene.add(largePulse);
// ------------------------------------------------------------
// Tracer packet system
// ------------------------------------------------------------
const particleGeometry = new THREE.SphereGeometry(0.065, 12, 8);
const particleMaterial = new THREE.MeshStandardMaterial({
color: colors.particle,
emissive: 0xff9e32,
emissiveIntensity: 0.75,
roughness: 0.28,
metalness: 0.05
});
const dummy = new THREE.Object3D();
function makeFlow(definition) {
const mesh = new THREE.InstancedMesh(
particleGeometry,
particleMaterial,
definition.count
);
mesh.instanceMatrix.setUsage(THREE.DynamicDrawUsage);
mesh.frustumCulled = false;
scene.add(mesh);
const data = [];
for (let i = 0; i < definition.count; i++) {
const normalized = (i * 0.61803398875) % 1;
const z =
(normalized * 2 - 1) *
definition.radius *
0.58;
data.push({
offset: (i / definition.count) * definition.length,
z,
wraps: 0,
position: new THREE.Vector3()
});
}
return {
definition,
mesh,
data,
eventTimes: [],
totalPackets: 0
};
}
function inletPath(distance, output) {
const t = distance / INLET_LENGTH;
output.lerpVectors(INLET_START, INLET_END, t);
return output;
}
function branchPath(elbow, end, distance, output) {
if (distance <= CONNECTOR_LENGTH) {
const t = distance / CONNECTOR_LENGTH;
output.lerpVectors(HUB, elbow, t);
} else {
const t =
(distance - CONNECTOR_LENGTH) /
(PATH_LENGTH - CONNECTOR_LENGTH);
output.lerpVectors(elbow, end, t);
}
return output;
}
const inletFlow = makeFlow({
count: 32,
speed: INLET_SPEED,
length: INLET_LENGTH,
radius: INLET_RADIUS,
path: inletPath,
countsOutput: false
});
const smallFlow = makeFlow({
count: 22,
speed: SMALL_SPEED,
length: PATH_LENGTH,
radius: SMALL_RADIUS,
path: (distance, output) =>
branchPath(SMALL_ELBOW, SMALL_END, distance, output),
countsOutput: true
});
const largeFlow = makeFlow({
count: 55,
speed: LARGE_SPEED,
length: PATH_LENGTH,
radius: LARGE_RADIUS,
path: (distance, output) =>
branchPath(LARGE_ELBOW, LARGE_END, distance, output),
countsOutput: true
});
function updateFlow(flow, simulationTime) {
const definition = flow.definition;
flow.data.forEach((item, index) => {
const rawDistance =
item.offset + definition.speed * simulationTime;
const wrapIndex = Math.floor(
rawDistance / definition.length
);
if (wrapIndex > item.wraps) {
const newEvents = wrapIndex - item.wraps;
item.wraps = wrapIndex;
if (definition.countsOutput) {
flow.totalPackets += newEvents;
for (let i = 0; i < newEvents; i++) {
flow.eventTimes.push(simulationTime);
}
}
}
const distance =
rawDistance - wrapIndex * definition.length;
definition.path(distance, item.position);
item.position.z += item.z;
dummy.position.copy(item.position);
dummy.rotation.set(0, 0, 0);
dummy.scale.set(2.35, 1, 1);
dummy.updateMatrix();
flow.mesh.setMatrixAt(index, dummy.matrix);
});
flow.mesh.instanceMatrix.needsUpdate = true;
while (
flow.eventTimes.length &&
flow.eventTimes[0] < simulationTime - 2
) {
flow.eventTimes.shift();
}
}
// ------------------------------------------------------------
// Direct camera controls — no external controls addon
// ------------------------------------------------------------
let dragging = false;
let previousX = 0;
let previousY = 0;
canvas.addEventListener("pointerdown", (event) => {
dragging = true;
previousX = event.clientX;
previousY = event.clientY;
canvas.setPointerCapture(event.pointerId);
});
canvas.addEventListener("pointermove", (event) => {
if (!dragging) return;
const deltaX = event.clientX - previousX;
const deltaY = event.clientY - previousY;
previousX = event.clientX;
previousY = event.clientY;
cameraYaw -= deltaX * 0.006;
cameraPitch = THREE.MathUtils.clamp(
cameraPitch + deltaY * 0.005,
0.08,
1.12
);
updateCamera();
});
function endDrag(event) {
dragging = false;
if (event.pointerId !== undefined) {
if (canvas.hasPointerCapture(event.pointerId)) {
canvas.releasePointerCapture(event.pointerId);
}
}
}
canvas.addEventListener("pointerup", endDrag);
canvas.addEventListener("pointercancel", endDrag);
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraDistance = THREE.MathUtils.clamp(
cameraDistance + event.deltaY * 0.012,
8,
24
);
updateCamera();
},
{ passive: false }
);
function resetView() {
cameraYaw = 0.57;
cameraPitch = 0.34;
cameraDistance = 15.5;
updateCamera();
}
canvas.addEventListener("dblclick", resetView);
document.querySelector("#resetButton").addEventListener(
"click",
resetView
);
let paused = false;
const pauseButton = document.querySelector("#pauseButton");
pauseButton.addEventListener("click", () => {
paused = !paused;
pauseButton.textContent = paused
? "Resume flow"
: "Pause flow";
});
// ------------------------------------------------------------
// HUD updates
// ------------------------------------------------------------
const smallRateElement = document.querySelector("#smallRate");
const largeRateElement = document.querySelector("#largeRate");
const smallPacketsElement = document.querySelector("#smallPackets");
const largePacketsElement = document.querySelector("#largePackets");
const smallTotalElement = document.querySelector("#smallTotal");
const largeTotalElement = document.querySelector("#largeTotal");
const balanceValueElement = document.querySelector("#balanceValue");
function packetsInLastSecond(flow, time) {
let count = 0;
for (const eventTime of flow.eventTimes) {
if (eventTime > time - 1 && eventTime <= time) {
count++;
}
}
return count;
}
function updateHUD(time) {
const ready = time >= 1;
const smallPackets = packetsInLastSecond(smallFlow, time);
const largePackets = packetsInLastSecond(largeFlow, time);
const smallMeasured = smallPackets * PACKET_VOLUME;
const largeMeasured = largePackets * PACKET_VOLUME;
smallRateElement.textContent = ready
? `${smallMeasured.toFixed(2)} u³/s`
: "— u³/s";
largeRateElement.textContent = ready
? `${largeMeasured.toFixed(2)} u³/s`
: "— u³/s";
smallPacketsElement.textContent = ready
? `${smallPackets}`
: "warming up";
largePacketsElement.textContent = ready
? `${largePackets}`
: "warming up";
smallTotalElement.textContent = smallFlow.totalPackets.toLocaleString();
largeTotalElement.textContent = largeFlow.totalPackets.toLocaleString();
const difference = Math.abs(smallMeasured - largeMeasured);
balanceValueElement.textContent = ready
? `|ΔQ| = ${difference.toFixed(2)} u³/s`
: "Measuring branch outputs…";
}
const smallTag = document.querySelector("#tagSmall");
const largeTag = document.querySelector("#tagLarge");
const smallTagPosition = new THREE.Vector3(
1.75,
SMALL_Y + SMALL_RADIUS + 0.52,
0
);
const largeTagPosition = new THREE.Vector3(
1.75,
LARGE_Y - LARGE_RADIUS - 0.48,
0
);
const projected = new THREE.Vector3();
function positionTag(element, worldPosition) {
projected.copy(worldPosition).project(camera);
const visible = projected.z > -1 && projected.z < 1;
element.style.left =
`${(projected.x * 0.5 + 0.5) * window.innerWidth}px`;
element.style.top =
`${(-projected.y * 0.5 + 0.5) * window.innerHeight}px`;
element.style.opacity = visible ? "1" : "0";
}
// ------------------------------------------------------------
// Resize and animation loop
// ------------------------------------------------------------
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setPixelRatio(
Math.min(window.devicePixelRatio, 2)
);
renderer.setSize(
window.innerWidth,
window.innerHeight
);
});
let simulationTime = 0;
let previousFrameTime = performance.now();
let previousHUDTime = -1;
function animate(now) {
requestAnimationFrame(animate);
const delta = Math.min(
(now - previousFrameTime) / 1000,
0.05
);
previousFrameTime = now;
if (!paused) {
simulationTime += delta;
}
updateFlow(inletFlow, simulationTime);
updateFlow(smallFlow, simulationTime);
updateFlow(largeFlow, simulationTime);
junctionCore.rotation.x = simulationTime * 0.45;
junctionCore.rotation.y = simulationTime * 0.72;
junctionEdges.rotation.y = simulationTime * 0.08;
const pulsePhase = (simulationTime % 0.125) / 0.125;
const pulseScale =
0.72 + Math.exp(-pulsePhase * 8) * 1.25;
smallPulse.scale.setScalar(pulseScale);
largePulse.scale.setScalar(pulseScale);
smallPulse.material.opacity = 0.42 + pulseScale * 0.25;
largePulse.material.opacity = 0.42 + pulseScale * 0.25;
if (simulationTime - previousHUDTime > 0.08) {
updateHUD(simulationTime);
previousHUDTime = simulationTime;
}
positionTag(smallTag, smallTagPosition);
positionTag(largeTag, largeTagPosition);
renderer.render(scene, camera);
}
requestAnimationFrame(animate);
requestAnimationFrame(() => {
document.querySelector("#loading").classList.add("hidden");
});
</script>
</body>
</html>
```