Prompt specification
Build or customize the Three.js 3D scene "Pipe-area continuity playground" 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: Pipe-area continuity playground
Category: Science & Physics · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/pipe-area-continuity-playground-x1456
Scene Description Prompt:
Make a standalone HTML scene with Three.js from cdnjs that depicts a low-poly pipe-continuity playground. A transparent pipe runs between two tanks, with a short middle section whose cross-sectional area can be changed. Hold the chosen volume flow Q fixed and update the mean velocity from Q = A times v. Show equal-volume packets crossing a reference plane and a separate velocity arrow inside the adjustable section. When the section narrows, the mean speed must increase consistently without silently changing Q. Provide a pause button and labels for the displayed units. Keep pressure, turbulence, and pump behavior outside this deliberately limited steady incompressible-flow model. At the same elapsed time, a collection counter at the outlet should show equal delivered volume for wide and narrow settings, despite their different local velocities.
Working Three.js Starter (pipe-area-continuity-playground-x1456):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Pipe Continuity Playground</title>
<style>
:root {
color-scheme: dark;
--panel: rgba(12, 24, 37, 0.84);
--panel-border: rgba(160, 220, 255, 0.20);
--text: #eef8ff;
--muted: #a9c1d2;
--cyan: #59d9ff;
--green: #65e6a6;
--yellow: #ffd166;
--orange: #ff9b52;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #07111d;
color: var(--text);
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
#stage {
position: fixed;
inset: 0;
}
#stage canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
cursor: grab;
}
#stage canvas:active {
cursor: grabbing;
}
.topbar {
position: fixed;
z-index: 10;
top: 18px;
left: 20px;
max-width: 560px;
pointer-events: none;
}
.eyebrow {
color: var(--cyan);
font-size: 11px;
font-weight: 800;
letter-spacing: 0.18em;
text-transform: uppercase;
}
h1 {
margin: 4px 0 3px;
font-size: clamp(22px, 2.5vw, 34px);
line-height: 1.08;
letter-spacing: -0.035em;
}
.subtitle {
margin: 0;
color: var(--muted);
font-size: 13px;
}
.panel {
position: fixed;
z-index: 20;
border: 1px solid var(--panel-border);
border-radius: 16px;
background: var(--panel);
box-shadow: 0 18px 50px rgba(0, 0, 0, 0.28);
backdrop-filter: blur(15px);
-webkit-backdrop-filter: blur(15px);
}
#controls {
top: 112px;
left: 20px;
width: 326px;
padding: 16px;
}
#metrics {
top: 20px;
right: 20px;
width: 360px;
padding: 15px;
}
.section-title {
display: flex;
align-items: center;
justify-content: space-between;
gap: 10px;
margin-bottom: 10px;
font-size: 12px;
font-weight: 800;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.lock {
padding: 3px 7px;
border: 1px solid rgba(101, 230, 166, 0.32);
border-radius: 999px;
color: var(--green);
background: rgba(101, 230, 166, 0.08);
font-size: 9px;
letter-spacing: 0.08em;
}
.q-row {
display: grid;
grid-template-columns: 1fr 74px;
align-items: end;
gap: 10px;
}
label {
display: block;
color: var(--muted);
font-size: 11px;
line-height: 1.35;
}
.unit {
color: #d5e9f6;
font-weight: 650;
}
input[type="number"] {
width: 100%;
margin-top: 5px;
padding: 8px 9px;
border: 1px solid rgba(160, 220, 255, 0.25);
border-radius: 9px;
outline: none;
color: var(--text);
background: rgba(3, 12, 21, 0.68);
font: inherit;
font-size: 14px;
font-weight: 700;
}
input[type="number"]:focus {
border-color: var(--cyan);
}
input[type="range"] {
width: 100%;
margin: 12px 0 4px;
accent-color: var(--cyan);
}
button {
min-height: 36px;
border: 1px solid rgba(160, 220, 255, 0.23);
border-radius: 9px;
color: var(--text);
background: rgba(105, 168, 205, 0.13);
font: inherit;
font-size: 12px;
font-weight: 750;
cursor: pointer;
}
button:hover {
background: rgba(105, 168, 205, 0.23);
}
button.primary {
border-color: rgba(89, 217, 255, 0.48);
background: rgba(89, 217, 255, 0.17);
}
.button-row {
display: grid;
grid-template-columns: 1.1fr 1fr;
gap: 8px;
margin-top: 12px;
}
.hint {
margin: 8px 0 0;
color: var(--muted);
font-size: 10px;
line-height: 1.45;
}
.area-readout {
display: flex;
align-items: baseline;
justify-content: space-between;
margin-top: 8px;
}
.area-readout strong {
color: var(--cyan);
font-size: 20px;
}
.divider {
height: 1px;
margin: 14px 0;
background: rgba(160, 220, 255, 0.13);
}
.scenario-grid {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 9px;
}
.scenario {
padding: 11px;
border: 1px solid rgba(160, 220, 255, 0.15);
border-radius: 12px;
background: rgba(1, 10, 18, 0.36);
}
.scenario.wide {
border-top: 2px solid var(--cyan);
}
.scenario.narrow {
border-top: 2px solid var(--orange);
}
.scenario-name {
margin-bottom: 8px;
color: var(--muted);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.metric {
display: flex;
justify-content: space-between;
gap: 8px;
margin-top: 5px;
color: var(--muted);
font-size: 10px;
}
.metric b {
color: var(--text);
font-size: 11px;
white-space: nowrap;
}
.elapsed-row {
display: flex;
align-items: baseline;
justify-content: space-between;
margin-top: 12px;
padding: 10px;
border-radius: 10px;
background: rgba(89, 217, 255, 0.08);
}
.elapsed-row strong {
color: var(--cyan);
font-size: 22px;
font-variant-numeric: tabular-nums;
}
.match {
margin-top: 9px;
padding: 9px 10px;
border: 1px solid rgba(101, 230, 166, 0.25);
border-radius: 9px;
color: var(--green);
background: rgba(101, 230, 166, 0.07);
text-align: center;
font-size: 11px;
font-weight: 800;
}
.equations {
margin-top: 11px;
color: var(--muted);
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
font-size: 10px;
line-height: 1.6;
}
.model-note {
margin-top: 10px;
padding: 10px;
border-left: 2px solid var(--yellow);
color: #d7e2e9;
background: rgba(255, 209, 102, 0.06);
font-size: 10px;
line-height: 1.5;
}
#labels {
position: fixed;
z-index: 5;
inset: 0;
overflow: hidden;
pointer-events: none;
}
.world-label {
position: absolute;
padding: 5px 8px;
border: 1px solid rgba(160, 220, 255, 0.22);
border-radius: 7px;
color: #e9f7ff;
background: rgba(5, 16, 27, 0.72);
box-shadow: 0 4px 18px rgba(0, 0, 0, 0.2);
font-size: 10px;
font-weight: 750;
line-height: 1.2;
text-align: center;
white-space: nowrap;
transform: translate(-50%, -50%);
}
.world-label.velocity {
border-color: rgba(255, 155, 82, 0.45);
color: #ffd0ad;
}
.world-label.packet {
border-color: rgba(255, 209, 102, 0.38);
color: #ffe4a4;
}
.world-label.outlet {
border-color: rgba(101, 230, 166, 0.36);
color: #b9f5d4;
}
.camera-help {
position: fixed;
z-index: 10;
right: 20px;
bottom: 16px;
padding: 7px 10px;
border: 1px solid rgba(160, 220, 255, 0.15);
border-radius: 999px;
color: var(--muted);
background: rgba(7, 17, 29, 0.68);
font-size: 10px;
pointer-events: none;
}
@media (max-width: 900px) {
#controls {
top: auto;
bottom: 14px;
width: min(326px, calc(100vw - 28px));
max-height: 47vh;
overflow: auto;
}
#metrics {
top: 106px;
right: 14px;
width: min(350px, calc(100vw - 28px));
}
.topbar {
left: 14px;
max-width: calc(100vw - 28px);
}
.subtitle {
display: none;
}
.camera-help {
display: none;
}
}
@media (max-width: 560px) {
#metrics {
top: 92px;
width: calc(100vw - 28px);
}
#controls {
max-height: 42vh;
}
.model-note {
display: none;
}
}
</style>
</head>
<body>
<div id="stage"></div>
<div id="labels"></div>
<header class="topbar">
<div class="eyebrow">Steady incompressible-flow study</div>
<h1>Pipe Continuity Playground</h1>
<p class="subtitle">
Change the middle-section area while Q stays fixed. Compare local velocity,
equal-volume packet crossings, and delivered outlet volume.
</p>
</header>
<section class="panel" id="controls" aria-label="Simulation controls">
<div class="section-title">
<span>Prescribed flow</span>
<span class="lock">Q LOCKED DURING RUN</span>
</div>
<div class="q-row">
<label>
Chosen volume flow Q
<span class="unit">[L/s]</span>
<input
id="qInput"
type="number"
min="0.2"
max="3"
step="0.1"
value="1.00"
aria-label="Volume flow rate in liters per second"
/>
</label>
<button id="applyQ" class="primary" type="button">Set Q<br />+ restart</button>
</div>
<p class="hint">
Changing Q is an explicit reset. Changing the area below never alters Q
or restarts elapsed time.
</p>
<div class="divider"></div>
<div class="section-title">
<span>Adjustable narrow trial</span>
<span class="lock">AREA ONLY</span>
</div>
<label for="areaSlider">
Middle-section cross-sectional area A
<span class="unit">[cm²]</span>
</label>
<input
id="areaSlider"
type="range"
min="10"
max="38.48"
step="0.1"
value="10"
aria-label="Adjustable middle-section area in square centimeters"
/>
<div class="area-readout">
<span class="hint" style="margin: 0">Narrow section</span>
<strong id="areaValue">10.0 cm²</strong>
</div>
<div class="button-row">
<button id="pauseButton" class="primary" type="button">Pause</button>
<button id="resetButton" type="button">Reset time</button>
</div>
<div class="model-note">
Deliberately limited model: steady, incompressible, prescribed-Q flow.
Pressure, turbulence, pump behavior, losses, and transient filling are
not modeled.
</div>
</section>
<section class="panel" id="metrics" aria-label="Comparison metrics">
<div class="section-title">
<span>Same elapsed time</span>
<span id="qBadge" class="lock">Q = 1.00 L/s</span>
</div>
<div class="scenario-grid">
<article class="scenario wide">
<div class="scenario-name">Wide baseline</div>
<div class="metric"><span>Area</span><b id="wideArea">38.48 cm²</b></div>
<div class="metric"><span>Mean v</span><b id="wideVelocity">0.260 m/s</b></div>
<div class="metric"><span>Outlet V</span><b id="wideOutlet">0.00 L</b></div>
<div class="metric"><span>Packets</span><b id="widePackets">0</b></div>
</article>
<article class="scenario narrow">
<div class="scenario-name">Adjustable trial</div>
<div class="metric"><span>Area</span><b id="narrowArea">10.0 cm²</b></div>
<div class="metric"><span>Mean v</span><b id="narrowVelocity">1.000 m/s</b></div>
<div class="metric"><span>Outlet V</span><b id="narrowOutlet">0.00 L</b></div>
<div class="metric"><span>Packets</span><b id="narrowPackets">0</b></div>
</article>
</div>
<div class="elapsed-row">
<span class="hint" style="margin: 0">Elapsed model time</span>
<strong id="elapsed">0.00 s</strong>
</div>
<div id="match" class="match">OUTLET MATCH · ΔV = 0.000 L</div>
<div class="equations">
v = Q / A<br />
f<sub>packet</sub> = Q / V<sub>packet</sub><br />
V<sub>outlet</sub> = Q · t
</div>
<p class="hint">
Yellow markers are abstract fixed-volume packets, each representing
0.25 L. Their crossing frequency is Q/V<sub>packet</sub>, so both rigs
complete the same number of crossings at the same time.
</p>
</section>
<div class="camera-help">Drag to orbit · Wheel to zoom · Space to pause</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// -------------------------------------------------------------------------
// Model constants and state
// -------------------------------------------------------------------------
const SCENE_SCALE_METERS = 0.1; // one scene unit represents 0.1 meters
const FIXED_PIPE_RADIUS = 0.35; // scene units
const WIDE_AREA_CM2 = 100 * Math.PI * FIXED_PIPE_RADIUS * FIXED_PIPE_RADIUS;
const PACKET_VOLUME_L = 0.25;
const state = {
qLps: 1.0,
elapsed: 0,
paused: false
};
const stage = document.querySelector("#stage");
const labelsRoot = document.querySelector("#labels");
// -------------------------------------------------------------------------
// Renderer, camera, lighting
// -------------------------------------------------------------------------
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x07111d);
scene.fog = new THREE.Fog(0x07111d, 24, 43);
const camera = new THREE.PerspectiveCamera(
44,
innerWidth / innerHeight,
0.1,
100
);
const renderer = new THREE.WebGLRenderer({ antialias: true, alpha: false });
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
renderer.setSize(innerWidth, innerHeight);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.12;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
stage.appendChild(renderer.domElement);
scene.add(new THREE.HemisphereLight(0xb9e7ff, 0x172333, 1.65));
const keyLight = new THREE.DirectionalLight(0xffffff, 3.2);
keyLight.position.set(7, 13, 9);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(1024, 1024);
keyLight.shadow.camera.left = -16;
keyLight.shadow.camera.right = 16;
keyLight.shadow.camera.top = 12;
keyLight.shadow.camera.bottom = -12;
keyLight.shadow.camera.near = 1;
keyLight.shadow.camera.far = 35;
scene.add(keyLight);
const rimLight = new THREE.DirectionalLight(0x4cc9ff, 1.8);
rimLight.position.set(-9, 5, -8);
scene.add(rimLight);
const ground = new THREE.Mesh(
new THREE.PlaneGeometry(28, 18),
new THREE.MeshStandardMaterial({
color: 0x0b1b29,
roughness: 0.92,
metalness: 0.05
})
);
ground.rotation.x = -Math.PI / 2;
ground.position.y = -1.56;
ground.receiveShadow = true;
scene.add(ground);
const grid = new THREE.GridHelper(28, 28, 0x31516a, 0x193346);
grid.position.y = -1.545;
grid.material.transparent = true;
grid.material.opacity = 0.55;
scene.add(grid);
// -------------------------------------------------------------------------
// Direct camera controls: no addons or external controls are used
// -------------------------------------------------------------------------
const orbit = {
target: new THREE.Vector3(0, -0.05, 0),
radius: 20.5,
azimuth: 0.58,
polar: 1.03
};
function updateCamera() {
const sinPolar = Math.sin(orbit.polar);
camera.position.set(
orbit.target.x + orbit.radius * sinPolar * Math.sin(orbit.azimuth),
orbit.target.y + orbit.radius * Math.cos(orbit.polar),
orbit.target.z + orbit.radius * sinPolar * Math.cos(orbit.azimuth)
);
camera.lookAt(orbit.target);
camera.updateMatrixWorld();
}
let dragging = false;
let previousPointerX = 0;
let previousPointerY = 0;
renderer.domElement.addEventListener("pointerdown", event => {
if (event.button !== 0) return;
dragging = true;
previousPointerX = event.clientX;
previousPointerY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", event => {
if (!dragging) return;
const dx = event.clientX - previousPointerX;
const dy = event.clientY - previousPointerY;
previousPointerX = event.clientX;
previousPointerY = event.clientY;
orbit.azimuth -= dx * 0.005;
orbit.polar = THREE.MathUtils.clamp(
orbit.polar + dy * 0.005,
0.36,
1.46
);
});
function endDrag(event) {
dragging = false;
if (event.pointerId !== undefined &&
renderer.domElement.hasPointerCapture(event.pointerId)) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
}
renderer.domElement.addEventListener("pointerup", endDrag);
renderer.domElement.addEventListener("pointercancel", endDrag);
renderer.domElement.addEventListener(
"wheel",
event => {
event.preventDefault();
orbit.radius = THREE.MathUtils.clamp(
orbit.radius * Math.exp(event.deltaY * 0.001),
11,
34
);
},
{ passive: false }
);
renderer.domElement.addEventListener("dblclick", () => {
orbit.radius = 20.5;
orbit.azimuth = 0.58;
orbit.polar = 1.03;
});
// -------------------------------------------------------------------------
// Shared materials and geometry helpers
// -------------------------------------------------------------------------
const pipeShellMaterial = new THREE.MeshPhongMaterial({
color: 0xb9e8ff,
transparent: true,
opacity: 0.25,
shininess: 90,
side: THREE.DoubleSide,
depthWrite: false,
flatShading: true
});
const fluidMaterial = new THREE.MeshBasicMaterial({
color: 0x168ec3,
transparent: true,
opacity: 0.12,
side: THREE.DoubleSide,
depthWrite: false
});
const packetGeometry = new THREE.IcosahedronGeometry(0.067, 0);
const packetMaterial = new THREE.MeshStandardMaterial({
color: 0xffd166,
emissive: 0x704000,
emissiveIntensity: 0.7,
roughness: 0.36,
metalness: 0.08
});
function createPipeGeometry(profile, radialSegments = 12, radialOffset = 0) {
const positions = [];
const indices = [];
for (const [x, baseRadius] of profile) {
const radius = baseRadius + radialOffset;
for (let segment = 0; segment <= radialSegments; segment++) {
const angle = (segment / radialSegments) * Math.PI * 2;
positions.push(
x,
Math.sin(angle) * radius,
Math.cos(angle) * radius
);
}
}
const ringSize = radialSegments + 1;
for (let ring = 0; ring < profile.length - 1; ring++) {
for (let segment = 0; segment < radialSegments; segment++) {
const a = ring * ringSize + segment;
const b = a + ringSize;
const c = b + 1;
const d = a + 1;
indices.push(a, b, d);
indices.push(b, c, d);
}
}
const geometry = new THREE.BufferGeometry();
geometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(positions, 3)
);
geometry.setIndex(indices);
geometry.computeVertexNormals();
return geometry;
}
function makeWorldLabel(text, extraClass = "") {
const element = document.createElement("div");
element.className = `world-label ${extraClass}`.trim();
element.textContent = text;
labelsRoot.appendChild(element);
return element;
}
function addTank(group, x) {
const tankGroup = new THREE.Group();
tankGroup.position.x = x;
const shellGeometry = new THREE.CylinderGeometry(1, 1, 2.2, 12, 1, true);
const shell = new THREE.Mesh(
shellGeometry,
new THREE.MeshPhongMaterial({
color: 0xaeddf2,
transparent: true,
opacity: 0.23,
shininess: 75,
side: THREE.DoubleSide,
depthWrite: false
})
);
shell.castShadow = true;
tankGroup.add(shell);
const edges = new THREE.LineSegments(
new THREE.EdgesGeometry(shellGeometry),
new THREE.LineBasicMaterial({
color: 0x78aec8,
transparent: true,
opacity: 0.55
})
);
tankGroup.add(edges);
const water = new THREE.Mesh(
new THREE.CylinderGeometry(0.88, 0.88, 0.72, 12),
new THREE.MeshStandardMaterial({
color: 0x168dc5,
transparent: true,
opacity: 0.56,
roughness: 0.32,
metalness: 0.05
})
);
water.position.y = -0.52;
water.castShadow = true;
tankGroup.add(water);
for (const y of [-1.1, 1.1]) {
const rim = new THREE.Mesh(
new THREE.TorusGeometry(1.0, 0.055, 6, 12),
new THREE.MeshStandardMaterial({
color: 0x9ac6da,
roughness: 0.4,
metalness: 0.45
})
);
rim.rotation.x = Math.PI / 2;
rim.position.y = y;
rim.castShadow = true;
tankGroup.add(rim);
}
for (const xLeg of [-0.62, 0.62]) {
for (const zLeg of [-0.62, 0.62]) {
const leg = new THREE.Mesh(
new THREE.BoxGeometry(0.16, 0.48, 0.16),
new THREE.MeshStandardMaterial({
color: 0x28465a,
roughness: 0.65,
metalness: 0.4
})
);
leg.position.set(xLeg, -1.32, zLeg);
leg.castShadow = true;
tankGroup.add(leg);
}
}
group.add(tankGroup);
}
function addDirectionMarkers(group) {
const markerMaterial = new THREE.MeshStandardMaterial({
color: 0x59d9ff,
emissive: 0x0b5c7d,
emissiveIntensity: 0.6,
roughness: 0.4
});
for (const x of [-5.5, -4.55, 3.8, 4.75]) {
const marker = new THREE.Mesh(
new THREE.ConeGeometry(0.07, 0.22, 6),
markerMaterial
);
marker.rotation.z = -Math.PI / 2;
marker.position.set(x, 0.48, 0);
group.add(marker);
}
}
// -------------------------------------------------------------------------
// Rig construction
// -------------------------------------------------------------------------
function createRig({ z, name, areaCm2, adjustable }) {
const group = new THREE.Group();
group.position.z = z;
scene.add(group);
const radius = Math.sqrt(areaCm2 / (100 * Math.PI));
const areaM2 = areaCm2 * 1e-4;
const velocity = state.qLps / 1000 / areaM2;
const rig = {
group,
name,
adjustable,
areaCm2,
radius,
velocity,
pipeShell: null,
fluidMesh: null,
packets: [],
arrow: null,
labels: {}
};
rig.labels.title = makeWorldLabel(
adjustable
? `ADJUSTABLE · A = ${areaCm2.toFixed(2)} cm²`
: `WIDE BASELINE · A = ${areaCm2.toFixed(2)} cm²`
);
rig.labels.velocity = makeWorldLabel(
`v = ${velocity.toFixed(3)} m/s`,
"velocity"
);
rig.labels.packet = makeWorldLabel(
`REFERENCE PLANE · ${PACKET_VOLUME_L.toFixed(2)} L / packet`,
"packet"
);
rig.labels.outlet = makeWorldLabel("Outlet V = 0.00 L", "outlet");
rebuildPipe(rig);
addTank(group, -7.0);
addTank(group, 7.0);
addDirectionMarkers(group);
// The separate orange arrow represents local mean velocity.
const arrow = new THREE.ArrowHelper(
new THREE.Vector3(1, 0, 0),
new THREE.Vector3(-1.65, -0.09, 0),
1,
0xff9b52,
0.24,
0.14
);
rig.arrow = arrow;
group.add(arrow);
// A translucent measurement plane at x = 0, inside the adjustable section.
const planeGroup = new THREE.Group();
planeGroup.rotation.y = Math.PI / 2;
planeGroup.position.x = 0;
const plane = new THREE.Mesh(
new THREE.PlaneGeometry(1.65, 1.35),
new THREE.MeshBasicMaterial({
color: 0x65e6a6,
transparent: true,
opacity: 0.075,
side: THREE.DoubleSide,
depthWrite: false
})
);
planeGroup.add(plane);
const planeOutline = new THREE.LineSegments(
new THREE.EdgesGeometry(plane.geometry),
new THREE.LineBasicMaterial({
color: 0x65e6a6,
transparent: true,
opacity: 0.6
})
);
planeGroup.add(planeOutline);
group.add(planeGroup);
// Abstract fixed-volume markers. Their phase is shared by both rigs,
// while their local speed and spacing respond to A.
for (let index = 0; index < 9; index++) {
const packet = new THREE.Mesh(packetGeometry, packetMaterial);
packet.castShadow = true;
rig.packets.push(packet);
group.add(packet);
}
updateRigVelocity(rig);
return rig;
}
function rebuildPipe(rig) {
const profile = [
[-6.75, FIXED_PIPE_RADIUS],
[-3.2, FIXED_PIPE_RADIUS],
[-2.0, rig.radius],
[2.0, rig.radius],
[3.2, FIXED_PIPE_RADIUS],
[6.75, FIXED_PIPE_RADIUS]
];
if (rig.pipeShell) {
rig.group.remove(rig.pipeShell);
rig.pipeShell.geometry.dispose();
}
if (rig.fluidMesh) {
rig.group.remove(rig.fluidMesh);
rig.fluidMesh.geometry.dispose();
}
rig.pipeShell = new THREE.Mesh(
createPipeGeometry(profile, 12, 0.025),
pipeShellMaterial
);
rig.pipeShell.renderOrder = 2;
rig.group.add(rig.pipeShell);
rig.fluidMesh = new THREE.Mesh(
createPipeGeometry(profile, 12, 0),
fluidMaterial
);
rig.fluidMesh.scale.y = 0.96;
rig.fluidMesh.scale.z = 0.96;
rig.fluidMesh.renderOrder = 1;
rig.group.add(rig.fluidMesh);
}
function updateRigVelocity(rig) {
rig.velocity =
state.qLps / 1000 / (rig.areaCm2 * 1e-4);
const arrowLength = 0.45 + Math.min(1.8, rig.velocity * 1.2);
rig.arrow.setLength(
arrowLength,
Math.min(0.3, arrowLength * 0.27),
0.14
);
rig.labels.velocity.textContent = `v = ${rig.velocity.toFixed(3)} m/s`;
rig.labels.title.textContent = rig.adjustable
? `ADJUSTABLE · A = ${rig.areaCm2.toFixed(2)} cm²`
: `WIDE BASELINE · A = ${rig.areaCm2.toFixed(2)} cm²`;
}
const wideRig = createRig({
z: 1.8,
name: "Wide baseline",
areaCm2: WIDE_AREA_CM2,
adjustable: false
});
const narrowRig = createRig({
z: -1.8,
name: "Adjustable trial",
areaCm2: 10,
adjustable: true
});
// -------------------------------------------------------------------------
// UI
// -------------------------------------------------------------------------
const qInput = document.querySelector("#qInput");
const applyQButton = document.querySelector("#applyQ");
const areaSlider = document.querySelector("#areaSlider");
const pauseButton = document.querySelector("#pauseButton");
const resetButton = document.querySelector("#resetButton");
const qBadge = document.querySelector("#qBadge");
const areaValue = document.querySelector("#areaValue");
const elapsedElement = document.querySelector("#elapsed");
const wideAreaElement = document.querySelector("#wideArea");
const wideVelocityElement = document.querySelector("#wideVelocity");
const wideOutletElement = document.querySelector("#wideOutlet");
const widePacketsElement = document.querySelector("#widePackets");
const narrowAreaElement = document.querySelector("#narrowArea");
const narrowVelocityElement = document.querySelector("#narrowVelocity");
const narrowOutletElement = document.querySelector("#narrowOutlet");
const narrowPacketsElement = document.querySelector("#narrowPackets");
const matchElement = document.querySelector("#match");
function resetElapsedTime() {
state.elapsed = 0;
}
function applyQ() {
const nextQ = THREE.MathUtils.clamp(
Number.parseFloat(qInput.value),
0.2,
3
);
if (!Number.isFinite(nextQ)) return;
state.qLps = nextQ;
qInput.value = nextQ.toFixed(2);
resetElapsedTime();
updateRigVelocity(wideRig);
updateRigVelocity(narrowRig);
updateMetrics();
}
function togglePause() {
state.paused = !state.paused;
pauseButton.textContent = state.paused ? "Resume" : "Pause";
pauseButton.setAttribute(
"aria-label",
state.paused ? "Resume simulation" : "Pause simulation"
);
}
applyQButton.addEventListener("click", applyQ);
qInput.addEventListener("keydown", event => {
if (event.key === "Enter") applyQ();
});
areaSlider.addEventListener("input", () => {
const nextArea = THREE.MathUtils.clamp(
Number.parseFloat(areaSlider.value),
10,
WIDE_AREA_CM2
);
narrowRig.areaCm2 = nextArea;
narrowRig.radius = Math.sqrt(nextArea / (100 * Math.PI));
// Q and elapsed time deliberately remain unchanged.
rebuildPipe(narrowRig);
updateRigVelocity(narrowRig);
updateMetrics();
});
pauseButton.addEventListener("click", togglePause);
resetButton.addEventListener("click", resetElapsedTime);
window.addEventListener("keydown", event => {
if (
event.code === "Space" &&
event.target !== qInput &&
event.target !== areaSlider
) {
event.preventDefault();
togglePause();
}
});
// -------------------------------------------------------------------------
// Packet and counter update
// -------------------------------------------------------------------------
function updateRigPackets(rig) {
const packetPeriod = PACKET_VOLUME_L / state.qLps;
// Phase is deliberately shared by both rigs. Thus each equal-volume
// packet crosses the reference plane at exactly the same model times.
const phase = ((state.elapsed / packetPeriod) + 0.5) % 1;
// World-space separation between consecutive equal-volume packets:
// spacing = v * (V_packet / Q) = V_packet / A.
const packetSpacing =
(rig.velocity * packetPeriod) / SCENE_SCALE_METERS;
for (let index = 0; index < rig.packets.length; index++) {
const packet = rig.packets[index];
const n = index - 4;
const x = packetSpacing * (phase - n);
if (x >= -2.08 && x <= 2.08) {
packet.visible = true;
packet.position.set(x, 0.075, 0);
// Fade near the ends of the adjustable section.
const edgeFade = Math.min(
1,
(2.08 - Math.abs(x)) / 0.18
);
packet.scale.setScalar(0.78 + 0.22 * edgeFade);
} else {
packet.visible = false;
}
}
}
function updateMetrics() {
const packetPeriod = PACKET_VOLUME_L / state.qLps;
const crossingCount = Math.max(
0,
Math.floor(state.elapsed / packetPeriod + 0.5 - 1e-9)
);
// Both outlets receive the same prescribed volume flow.
const outletVolumeL = state.qLps * state.elapsed;
qBadge.textContent = `Q = ${state.qLps.toFixed(2)} L/s`;
areaValue.textContent = `${narrowRig.areaCm2.toFixed(1)} cm²`;
elapsedElement.textContent = `${state.elapsed.toFixed(2)} s`;
wideAreaElement.textContent = `${wideRig.areaCm2.toFixed(2)} cm²`;
wideVelocityElement.textContent = `${wideRig.velocity.toFixed(3)} m/s`;
wideOutletElement.textContent = `${outletVolumeL.toFixed(2)} L`;
widePacketsElement.textContent = crossingCount.toLocaleString();
narrowAreaElement.textContent = `${narrowRig.areaCm2.toFixed(1)} cm²`;
narrowVelocityElement.textContent = `${narrowRig.velocity.toFixed(3)} m/s`;
narrowOutletElement.textContent = `${outletVolumeL.toFixed(2)} L`;
narrowPacketsElement.textContent = crossingCount.toLocaleString();
const difference = Math.abs(
state.qLps * state.elapsed - state.qLps * state.elapsed
);
matchElement.textContent =
`OUTLET MATCH · ΔV = ${difference.toFixed(3)} L`;
wideRig.labels.outlet.textContent =
`Outlet V = ${outletVolumeL.toFixed(2)} L`;
narrowRig.labels.outlet.textContent =
`Outlet V = ${outletVolumeL.toFixed(2)} L`;
}
// -------------------------------------------------------------------------
// World-space HTML label projection
// -------------------------------------------------------------------------
const projectionVector = new THREE.Vector3();
function positionWorldLabel(element, worldPosition) {
projectionVector.copy(worldPosition).project(camera);
if (
projectionVector.z < -1 ||
projectionVector.z > 1 ||
Math.abs(projectionVector.x) > 1.25 ||
Math.abs(projectionVector.y) > 1.25
) {
element.style.display = "none";
return;
}
element.style.display = "block";
element.style.left =
`${(projectionVector.x * 0.5 + 0.5) * innerWidth}px`;
element.style.top =
`${(-projectionVector.y * 0.5 + 0.5) * innerHeight}px`;
}
function updateLabels() {
for (const rig of [wideRig, narrowRig]) {
const titlePosition = rig.group.localToWorld(
new THREE.Vector3(-6.2, 1.48, 0)
);
const velocityPosition = rig.group.localToWorld(
new THREE.Vector3(-0.55, 0.78, 0)
);
const packetPosition = rig.group.localToWorld(
new THREE.Vector3(0, -1.05, 0)
);
const outletPosition = rig.group.localToWorld(
new THREE.Vector3(7.0, 1.48, 0)
);
positionWorldLabel(rig.labels.title, titlePosition);
positionWorldLabel(rig.labels.velocity, velocityPosition);
positionWorldLabel(rig.labels.packet, packetPosition);
positionWorldLabel(rig.labels.outlet, outletPosition);
}
}
// -------------------------------------------------------------------------
// Resize and animation
// -------------------------------------------------------------------------
function onResize() {
camera.aspect = innerWidth / innerHeight;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
renderer.setSize(innerWidth, innerHeight);
}
window.addEventListener("resize", onResize);
let previousTime = performance.now();
function animate(now) {
const deltaSeconds = Math.min((now - previousTime) / 1000, 0.05);
previousTime = now;
if (!state.paused) {
state.elapsed += deltaSeconds;
}
updateRigPackets(wideRig);
updateRigPackets(narrowRig);
updateMetrics();
updateCamera();
updateLabels();
renderer.render(scene, camera);
requestAnimationFrame(animate);
}
updateMetrics();
updateCamera();
requestAnimationFrame(animate);
</script>
</body>
</html>
```