Prompt specification
Build or customize the Three.js 3D scene "Connected tube level demonstrator" 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: Connected tube level demonstrator
Category: Science & Physics · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/connected-tube-level-demonstrator-x1449
Scene Description Prompt:
Please create one self-contained HTML file with Three.js from cdnjs showing liquid levels in communicating vessels. Model two transparent vessels of different widths joined at the bottom; show one continuous liquid with equal surface heights under matching external pressure. Low-poly.
Working Three.js Starter (connected-tube-level-demonstrator-x1449):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Communicating Vessels</title>
<style>
:root {
--ink: #173247;
--muted: #62798b;
--accent: #079bc5;
--accent-light: #b9f3ff;
--panel: rgba(255, 255, 255, 0.84);
--border: rgba(29, 83, 108, 0.13);
--shadow: 0 18px 50px rgba(30, 71, 94, 0.14);
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
color: var(--ink);
background: #eef3f7;
font-family: Inter, ui-sans-serif, system-ui, -apple-system,
BlinkMacSystemFont, "Segoe UI", sans-serif;
}
#stage {
position: fixed;
inset: 0;
}
canvas {
display: block;
width: 100%;
height: 100%;
cursor: grab;
touch-action: none;
}
canvas:active {
cursor: grabbing;
}
.hero {
position: fixed;
top: 28px;
left: 30px;
z-index: 5;
max-width: 390px;
pointer-events: none;
}
.eyebrow {
display: flex;
align-items: center;
gap: 9px;
margin-bottom: 8px;
color: var(--accent);
font-size: 11px;
font-weight: 800;
letter-spacing: 0.17em;
text-transform: uppercase;
}
.eyebrow::before {
width: 24px;
height: 2px;
content: "";
background: var(--accent);
border-radius: 999px;
}
h1 {
margin: 0;
font-size: clamp(27px, 3.1vw, 44px);
font-weight: 760;
letter-spacing: -0.045em;
line-height: 1;
}
.hero p {
margin: 12px 0 0;
color: var(--muted);
font-size: 14px;
line-height: 1.55;
}
.status {
position: fixed;
top: 28px;
right: 30px;
z-index: 6;
display: flex;
align-items: center;
gap: 10px;
padding: 10px 14px;
border: 1px solid var(--border);
border-radius: 999px;
background: var(--panel);
box-shadow: 0 8px 25px rgba(30, 71, 94, 0.08);
backdrop-filter: blur(14px);
-webkit-backdrop-filter: blur(14px);
font-size: 12px;
font-weight: 750;
}
.status-dot {
width: 9px;
height: 9px;
border-radius: 50%;
background: #16b88b;
box-shadow: 0 0 0 5px rgba(22, 184, 139, 0.12);
transition: background 180ms ease, box-shadow 180ms ease;
}
.status-dot.mismatch {
background: #ef9a3d;
box-shadow: 0 0 0 5px rgba(239, 154, 61, 0.13);
}
.panel {
position: fixed;
z-index: 7;
left: 30px;
bottom: 28px;
width: min(370px, calc(100vw - 60px));
padding: 19px;
border: 1px solid var(--border);
border-radius: 20px;
background: var(--panel);
box-shadow: var(--shadow);
backdrop-filter: blur(18px);
-webkit-backdrop-filter: blur(18px);
}
.panel-heading {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
margin-bottom: 16px;
}
.panel-heading strong {
font-size: 14px;
}
.match-badge {
padding: 5px 8px;
border-radius: 999px;
color: #087a62;
background: rgba(22, 184, 139, 0.11);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.05em;
text-transform: uppercase;
}
.match-badge.mismatch {
color: #9a591b;
background: rgba(239, 154, 61, 0.13);
}
.pressure-row {
margin-top: 14px;
}
.pressure-label {
display: flex;
align-items: baseline;
justify-content: space-between;
margin-bottom: 7px;
font-size: 12px;
font-weight: 700;
}
.pressure-label output {
color: var(--accent);
font-variant-numeric: tabular-nums;
}
input[type="range"] {
width: 100%;
height: 5px;
margin: 0;
appearance: none;
-webkit-appearance: none;
border-radius: 999px;
outline: none;
background: linear-gradient(
90deg,
var(--accent) 0%,
var(--accent) var(--fill, 50%),
#dce7ed var(--fill, 50%),
#dce7ed 100%
);
cursor: pointer;
}
input[type="range"]::-webkit-slider-thumb {
width: 17px;
height: 17px;
appearance: none;
-webkit-appearance: none;
border: 3px solid white;
border-radius: 50%;
background: var(--accent);
box-shadow: 0 2px 8px rgba(4, 112, 151, 0.32);
}
input[type="range"]::-moz-range-thumb {
width: 12px;
height: 12px;
border: 3px solid white;
border-radius: 50%;
background: var(--accent);
box-shadow: 0 2px 8px rgba(4, 112, 151, 0.32);
}
.equation {
margin-top: 17px;
padding: 12px 13px;
border: 1px solid rgba(7, 155, 197, 0.12);
border-radius: 12px;
color: #32566b;
background: rgba(7, 155, 197, 0.055);
font-family: Georgia, "Times New Roman", serif;
font-size: 14px;
text-align: center;
}
.measurements {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 8px;
margin-top: 10px;
}
.measurement {
padding: 9px 6px;
border-radius: 10px;
background: rgba(30, 71, 94, 0.045);
text-align: center;
}
.measurement span {
display: block;
color: var(--muted);
font-size: 9px;
font-weight: 800;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.measurement strong {
display: block;
margin-top: 3px;
font-size: 13px;
font-variant-numeric: tabular-nums;
}
.panel-footer {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
margin-top: 14px;
}
.volume-note {
color: var(--muted);
font-size: 10px;
line-height: 1.4;
}
button {
flex: 0 0 auto;
padding: 9px 12px;
border: 0;
border-radius: 10px;
color: white;
background: var(--ink);
font: inherit;
font-size: 11px;
font-weight: 750;
cursor: pointer;
transition: transform 150ms ease, background 150ms ease;
}
button:hover {
background: #24506a;
transform: translateY(-1px);
}
button:active {
transform: translateY(0);
}
.legend {
position: fixed;
right: 30px;
bottom: 28px;
z-index: 5;
display: grid;
gap: 9px;
padding: 13px 15px;
border: 1px solid var(--border);
border-radius: 14px;
background: rgba(255, 255, 255, 0.72);
backdrop-filter: blur(12px);
-webkit-backdrop-filter: blur(12px);
pointer-events: none;
}
.legend-item {
display: flex;
align-items: center;
gap: 9px;
color: var(--muted);
font-size: 10px;
font-weight: 650;
}
.swatch {
width: 18px;
height: 5px;
border-radius: 999px;
background: #0ba7d1;
}
.swatch.glass {
height: 10px;
border: 1px solid #78a8b8;
background: rgba(255, 255, 255, 0.25);
}
.swatch.datum {
height: 0;
border-top: 2px dashed #45b8ce;
background: transparent;
}
.hint {
position: fixed;
z-index: 4;
left: 50%;
bottom: 20px;
transform: translateX(-50%);
color: rgba(23, 50, 71, 0.55);
font-size: 10px;
font-weight: 650;
letter-spacing: 0.04em;
pointer-events: none;
}
.scene-label {
position: fixed;
z-index: 3;
pointer-events: none;
transform: translate(-50%, -50%);
}
.vessel-label {
min-width: 125px;
padding: 9px 11px;
border: 1px solid rgba(29, 83, 108, 0.12);
border-radius: 12px;
background: rgba(255, 255, 255, 0.83);
box-shadow: 0 8px 24px rgba(30, 71, 94, 0.1);
backdrop-filter: blur(10px);
-webkit-backdrop-filter: blur(10px);
}
.vessel-label strong {
display: block;
font-size: 11px;
}
.vessel-label span {
display: block;
margin-top: 2px;
color: var(--muted);
font-size: 9px;
font-weight: 700;
}
.surface-tag {
padding: 5px 8px;
border: 1px solid rgba(3, 141, 176, 0.18);
border-radius: 999px;
color: #086e8b;
background: rgba(220, 249, 255, 0.9);
box-shadow: 0 4px 12px rgba(3, 141, 176, 0.12);
font-size: 9px;
font-weight: 800;
font-variant-numeric: tabular-nums;
white-space: nowrap;
}
.datum-label {
padding: 5px 8px;
border-radius: 8px;
color: #397286;
background: rgba(255, 255, 255, 0.72);
font-size: 9px;
font-weight: 750;
white-space: nowrap;
}
@media (max-width: 760px) {
.hero {
top: 18px;
left: 18px;
}
.hero p {
display: none;
}
.status {
top: 18px;
right: 18px;
padding: 9px 11px;
}
.panel {
left: 14px;
bottom: 14px;
width: calc(100vw - 28px);
padding: 15px;
}
.legend,
.hint {
display: none;
}
.measurements {
grid-template-columns: repeat(3, 1fr);
}
.vessel-label {
min-width: 105px;
padding: 7px 9px;
}
}
</style>
</head>
<body>
<div id="stage"></div>
<header class="hero">
<div class="eyebrow">Hydrostatics / 01</div>
<h1>Communicating<br>Vessels</h1>
<p>Different widths, one connected liquid. Matching external pressure produces equal free-surface heights.</p>
</header>
<div class="status">
<span id="statusDot" class="status-dot"></span>
<span id="statusText">Equal surface heights</span>
</div>
<aside class="panel">
<div class="panel-heading">
<strong>External air pressure</strong>
<span id="matchBadge" class="match-badge">Matched</span>
</div>
<div class="pressure-row">
<label class="pressure-label" for="pressureA">
<span>Vessel A · wide</span>
<output id="pressureValueA">101.3 kPa</output>
</label>
<input id="pressureA" type="range" min="98" max="104.5" step="0.1" value="101.3">
</div>
<div class="pressure-row">
<label class="pressure-label" for="pressureB">
<span>Vessel B · narrow</span>
<output id="pressureValueB">101.3 kPa</output>
</label>
<input id="pressureB" type="range" min="98" max="104.5" step="0.1" value="101.3">
</div>
<div class="equation">
P<sub>A</sub> + ρg h<sub>A</sub>
=
P<sub>B</sub> + ρg h<sub>B</sub>
</div>
<div class="measurements">
<div class="measurement">
<span>Δ pressure</span>
<strong id="deltaPressure">0.0 kPa</strong>
</div>
<div class="measurement">
<span>Height difference</span>
<strong id="heightDifference">0.00 m</strong>
</div>
<div class="measurement">
<span>State</span>
<strong id="stateValue">Balanced</strong>
</div>
</div>
<div class="panel-footer">
<div class="volume-note">
Liquid volume is conserved while pressure<br>
changes the distribution between vessels.
</div>
<button id="matchButton" type="button">Match pressures</button>
</div>
</aside>
<div class="legend">
<div class="legend-item"><span class="swatch"></span> Continuous liquid</div>
<div class="legend-item"><span class="swatch glass"></span> Transparent vessel</div>
<div class="legend-item"><span class="swatch datum"></span> Equal-level datum</div>
</div>
<div class="hint">Drag to orbit · Scroll to zoom · Double-click to reset view</div>
<div id="labelA" class="scene-label vessel-label">
<strong>VESSEL A · WIDE</strong>
<span id="scenePressureA">Pₐ = 101.3 kPa</span>
</div>
<div id="labelB" class="scene-label vessel-label">
<strong>VESSEL B · NARROW</strong>
<span id="scenePressureB">Pᵦ = 101.3 kPa</span>
</div>
<div id="surfaceA" class="scene-label surface-tag">hₐ = 3.57 m</div>
<div id="surfaceB" class="scene-label surface-tag">hᵦ = 3.57 m</div>
<div id="datumLabel" class="scene-label datum-label">equal-level datum</div>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// ---------------------------------------------------------------------
// Scene setup
// ---------------------------------------------------------------------
const stage = document.getElementById("stage");
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xf1f5f8);
scene.fog = new THREE.Fog(0xf1f5f8, 15, 29);
const camera = new THREE.PerspectiveCamera(
35,
window.innerWidth / window.innerHeight,
0.1,
100
);
const renderer = new THREE.WebGLRenderer({
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.08;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
stage.appendChild(renderer.domElement);
const hemisphere = new THREE.HemisphereLight(0xffffff, 0x8fa7b7, 2.2);
scene.add(hemisphere);
const keyLight = new THREE.DirectionalLight(0xffffff, 3.2);
keyLight.position.set(-5, 9, 7);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(1024, 1024);
keyLight.shadow.camera.left = -7;
keyLight.shadow.camera.right = 7;
keyLight.shadow.camera.top = 9;
keyLight.shadow.camera.bottom = -2;
keyLight.shadow.bias = -0.0004;
scene.add(keyLight);
const fillLight = new THREE.DirectionalLight(0x8edfff, 1.2);
fillLight.position.set(7, 5, -5);
scene.add(fillLight);
const ground = new THREE.Mesh(
new THREE.PlaneGeometry(40, 40),
new THREE.MeshStandardMaterial({
color: 0xe8eef3,
roughness: 0.96,
metalness: 0
})
);
ground.rotation.x = -Math.PI / 2;
ground.position.y = 0.08;
ground.receiveShadow = true;
scene.add(ground);
const grid = new THREE.GridHelper(30, 30, 0xa9c1cf, 0xb9cbd6);
grid.position.y = 0.09;
grid.material.transparent = true;
grid.material.opacity = 0.13;
scene.add(grid);
// ---------------------------------------------------------------------
// Physical model
//
// One scene unit represents one meter.
// The two vertical liquid columns and the horizontal connecting pipe
// use the same material and overlap to form one continuous body.
// ---------------------------------------------------------------------
const BASE_Y = 0.48;
const VESSEL_TOP = 5.35;
const INITIAL_LEVEL = 4.05;
const INITIAL_HEIGHT = INITIAL_LEVEL - BASE_Y;
const xA = -1.85;
const xB = 1.35;
const radiusA = 1.20;
const radiusB = 0.72;
const liquidRadiusA = 1.11;
const liquidRadiusB = 0.63;
const pipeOuterRadius = 0.30;
const pipeLiquidRadius = 0.205;
const density = 1000;
const gravity = 9.81;
const areaA = Math.PI * liquidRadiusA * liquidRadiusA;
const areaB = Math.PI * liquidRadiusB * liquidRadiusB;
let pressureA = 101.3;
let pressureB = 101.3;
let currentHeightA = INITIAL_HEIGHT;
let currentHeightB = INITIAL_HEIGHT;
let targetHeightA = INITIAL_HEIGHT;
let targetHeightB = INITIAL_HEIGHT;
const glassMaterial = new THREE.MeshPhongMaterial({
color: 0xb9e5ee,
transparent: true,
opacity: 0.15,
shininess: 110,
specular: 0xb9f5ff,
side: THREE.DoubleSide,
depthWrite: false
});
const edgeMaterial = new THREE.LineBasicMaterial({
color: 0x6798a8,
transparent: true,
opacity: 0.30
});
const rimMaterial = new THREE.MeshStandardMaterial({
color: 0x8db8c5,
roughness: 0.34,
metalness: 0.18,
transparent: true,
opacity: 0.82
});
const liquidMaterial = new THREE.MeshPhysicalMaterial({
color: 0x079fc9,
roughness: 0.24,
metalness: 0.02,
clearcoat: 0.85,
clearcoatRoughness: 0.16,
transparent: true,
opacity: 0.88,
depthWrite: true
});
const surfaceMaterial = new THREE.MeshStandardMaterial({
color: 0x8aeaff,
emissive: 0x087a9d,
emissiveIntensity: 0.28,
roughness: 0.18,
metalness: 0.04,
side: THREE.DoubleSide
});
const arrowMaterial = new THREE.MeshStandardMaterial({
color: 0x31566c,
roughness: 0.52,
metalness: 0.05
});
function createVessel(x, radius, liquidRadius) {
const group = new THREE.Group();
group.position.x = x;
const height = VESSEL_TOP - BASE_Y;
const shellGeometry = new THREE.CylinderGeometry(
radius,
radius,
height,
12,
1,
true
);
const shell = new THREE.Mesh(shellGeometry, glassMaterial);
shell.position.y = BASE_Y + height / 2;
shell.renderOrder = 5;
group.add(shell);
const edges = new THREE.LineSegments(
new THREE.EdgesGeometry(shellGeometry, 14),
edgeMaterial
);
edges.position.copy(shell.position);
edges.renderOrder = 6;
group.add(edges);
const topRim = new THREE.Mesh(
new THREE.TorusGeometry(radius, 0.036, 5, 12),
rimMaterial
);
topRim.rotation.x = Math.PI / 2;
topRim.position.y = VESSEL_TOP;
group.add(topRim);
const bottomRim = new THREE.Mesh(
new THREE.TorusGeometry(radius, 0.032, 5, 12),
rimMaterial
);
bottomRim.rotation.x = Math.PI / 2;
bottomRim.position.y = BASE_Y;
group.add(bottomRim);
const liquidGeometry = new THREE.CylinderGeometry(
liquidRadius,
liquidRadius,
1,
16,
1,
false
);
const liquid = new THREE.Mesh(liquidGeometry, liquidMaterial);
liquid.scale.y = INITIAL_HEIGHT;
liquid.position.y = BASE_Y + INITIAL_HEIGHT / 2;
liquid.castShadow = true;
liquid.receiveShadow = true;
liquid.renderOrder = 2;
group.add(liquid);
const surface = new THREE.Mesh(
new THREE.CircleGeometry(liquidRadius, 16),
surfaceMaterial
);
surface.rotation.x = -Math.PI / 2;
surface.position.y = INITIAL_LEVEL + 0.006;
surface.renderOrder = 3;
group.add(surface);
const surfaceRim = new THREE.Mesh(
new THREE.TorusGeometry(liquidRadius, 0.022, 4, 16),
surfaceMaterial
);
surfaceRim.rotation.x = Math.PI / 2;
surfaceRim.position.y = INITIAL_LEVEL + 0.012;
surfaceRim.renderOrder = 4;
group.add(surfaceRim);
const arrow = new THREE.Group();
const shaft = new THREE.Mesh(
new THREE.CylinderGeometry(0.035, 0.035, 0.50, 8),
arrowMaterial
);
shaft.position.y = 0.52;
arrow.add(shaft);
const head = new THREE.Mesh(
new THREE.ConeGeometry(0.13, 0.26, 8),
arrowMaterial
);
head.rotation.z = Math.PI;
head.position.y = 0.12;
arrow.add(head);
arrow.position.set(0, INITIAL_LEVEL, 0);
group.add(arrow);
scene.add(group);
return { group, liquid, surface, surfaceRim, arrow };
}
const vesselA = createVessel(xA, radiusA, liquidRadiusA);
const vesselB = createVessel(xB, radiusB, liquidRadiusB);
// The horizontal connector is part of the same continuous liquid.
const pipeLength = Math.abs(xB - xA);
const pipeShell = new THREE.Mesh(
new THREE.CylinderGeometry(
pipeOuterRadius,
pipeOuterRadius,
pipeLength,
12,
1,
true
),
glassMaterial
);
pipeShell.rotation.z = Math.PI / 2;
pipeShell.position.set((xA + xB) / 2, BASE_Y, 0);
pipeShell.renderOrder = 5;
scene.add(pipeShell);
const pipeEdges = new THREE.LineSegments(
new THREE.EdgesGeometry(pipeShell.geometry, 14),
edgeMaterial
);
pipeEdges.rotation.copy(pipeShell.rotation);
pipeEdges.position.copy(pipeShell.position);
pipeEdges.renderOrder = 6;
scene.add(pipeEdges);
const pipeLiquid = new THREE.Mesh(
new THREE.CylinderGeometry(
pipeLiquidRadius,
pipeLiquidRadius,
pipeLength,
12,
1,
false
),
liquidMaterial
);
pipeLiquid.rotation.z = Math.PI / 2;
pipeLiquid.position.set((xA + xB) / 2, BASE_Y, 0);
pipeLiquid.renderOrder = 2;
scene.add(pipeLiquid);
// Dashed equal-level reference line.
const datumGeometry = new THREE.BufferGeometry().setFromPoints([
new THREE.Vector3(xA - radiusA - 0.45, INITIAL_LEVEL, 0.05),
new THREE.Vector3(xB + radiusB + 0.45, INITIAL_LEVEL, 0.05)
]);
const datumLine = new THREE.Line(
datumGeometry,
new THREE.LineDashedMaterial({
color: 0x49bdd2,
dashSize: 0.14,
gapSize: 0.10,
transparent: true,
opacity: 0.55,
depthTest: false
})
);
datumLine.computeLineDistances();
datumLine.renderOrder = 7;
scene.add(datumLine);
// ---------------------------------------------------------------------
// Pressure model
// ---------------------------------------------------------------------
const pressureInputA = document.getElementById("pressureA");
const pressureInputB = document.getElementById("pressureB");
function updateRangeFill(input) {
const percent =
((Number(input.value) - Number(input.min)) /
(Number(input.max) - Number(input.min))) *
100;
input.style.setProperty("--fill", `${percent}%`);
}
function calculateTargetHeights() {
// At the common bottom:
// pressureA + rho*g*hA = pressureB + rho*g*hB
// Therefore hA - hB = (pressureB - pressureA)/(rho*g).
const heightDifference =
((pressureB - pressureA) * 1000) / (density * gravity);
// Conservation of liquid volume:
// areaA*hA + areaB*hB = constant.
const areaSum = areaA + areaB;
targetHeightA =
INITIAL_HEIGHT +
(areaB / areaSum) * heightDifference;
targetHeightB =
INITIAL_HEIGHT -
(areaA / areaSum) * heightDifference;
const minHeight = 0.18;
const maxHeight = VESSEL_TOP - BASE_Y - 0.16;
targetHeightA = THREE.MathUtils.clamp(targetHeightA, minHeight, maxHeight);
targetHeightB = THREE.MathUtils.clamp(targetHeightB, minHeight, maxHeight);
}
function readPressures() {
pressureA = Number(pressureInputA.value);
pressureB = Number(pressureInputB.value);
calculateTargetHeights();
updateRangeFill(pressureInputA);
updateRangeFill(pressureInputB);
document.getElementById("pressureValueA").textContent =
`${pressureA.toFixed(1)} kPa`;
document.getElementById("pressureValueB").textContent =
`${pressureB.toFixed(1)} kPa`;
document.getElementById("scenePressureA").textContent =
`Pₐ = ${pressureA.toFixed(1)} kPa`;
document.getElementById("scenePressureB").textContent =
`Pᵦ = ${pressureB.toFixed(1)} kPa`;
const matched = Math.abs(pressureA - pressureB) < 0.05;
document.getElementById("matchBadge").textContent =
matched ? "Matched" : "Different";
document.getElementById("matchBadge").classList.toggle(
"mismatch",
!matched
);
document.getElementById("statusDot").classList.toggle(
"mismatch",
!matched
);
document.getElementById("statusText").textContent = matched
? "Equal surface heights"
: "Pressure difference detected";
document.getElementById("deltaPressure").textContent =
`${(pressureA - pressureB).toFixed(1)} kPa`;
document.getElementById("stateValue").textContent = matched
? "Balanced"
: "Adjusting";
}
pressureInputA.addEventListener("input", readPressures);
pressureInputB.addEventListener("input", readPressures);
document.getElementById("matchButton").addEventListener("click", () => {
pressureInputA.value = "101.3";
pressureInputB.value = "101.3";
readPressures();
});
// ---------------------------------------------------------------------
// Direct camera controls: no external control modules are used.
// ---------------------------------------------------------------------
const cameraTarget = new THREE.Vector3(0, 2.55, 0);
let cameraRadius = 11.4;
let cameraTheta = 0.47;
let cameraPhi = 1.10;
let dragging = false;
let previousX = 0;
let previousY = 0;
function updateCamera() {
camera.position.set(
cameraTarget.x +
cameraRadius *
Math.sin(cameraPhi) *
Math.sin(cameraTheta),
cameraTarget.y + cameraRadius * Math.cos(cameraPhi),
cameraTarget.z +
cameraRadius *
Math.sin(cameraPhi) *
Math.cos(cameraTheta)
);
camera.lookAt(cameraTarget);
camera.updateMatrixWorld();
}
function resetCamera() {
cameraRadius = 11.4;
cameraTheta = 0.47;
cameraPhi = 1.10;
updateCamera();
}
renderer.domElement.addEventListener("pointerdown", (event) => {
dragging = true;
previousX = event.clientX;
previousY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!dragging) return;
const deltaX = event.clientX - previousX;
const deltaY = event.clientY - previousY;
previousX = event.clientX;
previousY = event.clientY;
cameraTheta -= deltaX * 0.006;
cameraPhi = THREE.MathUtils.clamp(
cameraPhi - deltaY * 0.005,
0.68,
1.46
);
updateCamera();
});
function endDrag(event) {
dragging = false;
if (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();
cameraRadius = THREE.MathUtils.clamp(
cameraRadius + event.deltaY * 0.008,
8.0,
17.0
);
updateCamera();
},
{ passive: false }
);
renderer.domElement.addEventListener("dblclick", resetCamera);
// ---------------------------------------------------------------------
// Screen-space labels
// ---------------------------------------------------------------------
const projected = new THREE.Vector3();
function placeLabel(element, x, y, z) {
projected.set(x, y, z).project(camera);
const visible = projected.z > -1 && projected.z < 1;
element.style.display = visible ? "block" : "none";
if (!visible) return;
const screenX = (projected.x * 0.5 + 0.5) * window.innerWidth;
const screenY = (-projected.y * 0.5 + 0.5) * window.innerHeight;
element.style.left = `${screenX}px`;
element.style.top = `${screenY}px`;
}
const labelA = document.getElementById("labelA");
const labelB = document.getElementById("labelB");
const surfaceLabelA = document.getElementById("surfaceA");
const surfaceLabelB = document.getElementById("surfaceB");
const datumLabel = document.getElementById("datumLabel");
// ---------------------------------------------------------------------
// Animation
// ---------------------------------------------------------------------
const clock = new THREE.Clock();
function animate() {
const delta = Math.min(clock.getDelta(), 0.05);
const smoothing = 1 - Math.exp(-delta * 6.5);
currentHeightA +=
(targetHeightA - currentHeightA) * smoothing;
currentHeightB +=
(targetHeightB - currentHeightB) * smoothing;
const levelA = BASE_Y + currentHeightA;
const levelB = BASE_Y + currentHeightB;
vesselA.liquid.scale.y = currentHeightA;
vesselA.liquid.position.y = BASE_Y + currentHeightA / 2;
vesselB.liquid.scale.y = currentHeightB;
vesselB.liquid.position.y = BASE_Y + currentHeightB / 2;
vesselA.surface.position.y = levelA + 0.006;
vesselA.surfaceRim.position.y = levelA + 0.012;
vesselA.arrow.position.y = levelA;
vesselB.surface.position.y = levelB + 0.006;
vesselB.surfaceRim.position.y = levelB + 0.012;
vesselB.arrow.position.y = levelB;
// Arrow length subtly communicates relative pressure.
vesselA.arrow.scale.y = 0.78 + ((pressureA - 98) / 6.5) * 0.34;
vesselB.arrow.scale.y = 0.78 + ((pressureB - 98) / 6.5) * 0.34;
surfaceLabelA.textContent = `hₐ = ${currentHeightA.toFixed(2)} m`;
surfaceLabelB.textContent = `hᵦ = ${currentHeightB.toFixed(2)} m`;
const difference = Math.abs(currentHeightA - currentHeightB);
document.getElementById("heightDifference").textContent =
`${difference.toFixed(2)} m`;
datumLabel.textContent =
difference < 0.015
? "equal-level datum"
: "reference datum";
placeLabel(labelA, xA, 5.95, 0);
placeLabel(labelB, xB, 5.95, 0);
placeLabel(
surfaceLabelA,
xA + radiusA + 0.34,
levelA,
0
);
placeLabel(
surfaceLabelB,
xB + radiusB + 0.34,
levelB,
0
);
placeLabel(
datumLabel,
(xA + xB) / 2,
INITIAL_LEVEL + 0.16,
0.15
);
renderer.render(scene, camera);
}
function resize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
}
window.addEventListener("resize", resize);
updateRangeFill(pressureInputA);
updateRangeFill(pressureInputB);
readPressures();
resetCamera();
renderer.setAnimationLoop(animate);
</script>
</body>
</html>
```