Prompt specification
Build or customize the Three.js 3D scene "Connected-vessel pressure counterfactual" 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-vessel pressure counterfactual
Category: Science & Physics · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/connected-vessel-pressure-counterfactual-x1452
Scene Description Prompt:
Build a self-contained HTML scene with Three.js from cdnjs featuring liquid levels in communicating vessels. Let users change external pressure over one vessel in a simplified U-tube; calculate the resulting level difference and visibly state the equal-density static-fluid assumption. Low-poly.
Working Three.js Starter (connected-vessel-pressure-counterfactual-x1452):
```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 — Pressure Lab</title>
<style>
:root {
color-scheme: dark;
--bg: #07141e;
--panel: rgba(10, 26, 37, 0.88);
--panel-border: rgba(151, 220, 235, 0.16);
--text: #e8f8fb;
--muted: #91adb8;
--cyan: #62d9ed;
--mint: #53e0c1;
--orange: #ffb45d;
--blue: #67bfff;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background:
radial-gradient(circle at 48% 36%, #153242 0%, #0b1c29 42%, #061019 100%);
color: var(--text);
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
button,
input {
font: inherit;
}
#stage {
position: fixed;
inset: 0;
}
canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
cursor: grab;
}
canvas:active {
cursor: grabbing;
}
.vignette {
position: absolute;
inset: 0;
pointer-events: none;
background:
linear-gradient(90deg, rgba(3, 10, 16, 0.28), transparent 28%),
radial-gradient(circle at center, transparent 50%, rgba(2, 8, 13, 0.42));
}
header {
position: fixed;
top: 28px;
left: 32px;
z-index: 4;
max-width: 430px;
pointer-events: none;
}
.eyebrow {
margin-bottom: 8px;
color: var(--mint);
font-size: 0.69rem;
font-weight: 800;
letter-spacing: 0.19em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: clamp(1.65rem, 3vw, 2.65rem);
line-height: 0.98;
letter-spacing: -0.045em;
}
header p {
margin: 10px 0 0;
color: var(--muted);
font-size: 0.9rem;
line-height: 1.5;
}
.panel {
position: fixed;
z-index: 10;
top: 22px;
right: 22px;
width: min(380px, calc(100vw - 44px));
max-height: calc(100vh - 44px);
overflow: auto;
padding: 20px;
border: 1px solid var(--panel-border);
border-radius: 18px;
background: var(--panel);
box-shadow:
0 20px 70px rgba(0, 0, 0, 0.38),
inset 0 1px 0 rgba(255, 255, 255, 0.035);
backdrop-filter: blur(18px);
scrollbar-width: thin;
scrollbar-color: #33525e transparent;
}
.section-head {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
margin-bottom: 15px;
}
.section-head h2 {
margin: 0;
font-size: 0.95rem;
letter-spacing: -0.01em;
}
.solved {
display: inline-flex;
align-items: center;
gap: 6px;
color: var(--mint);
font-size: 0.64rem;
font-weight: 800;
letter-spacing: 0.12em;
text-transform: uppercase;
white-space: nowrap;
}
.solved::before {
width: 6px;
height: 6px;
border-radius: 50%;
background: currentColor;
box-shadow: 0 0 12px currentColor;
content: "";
}
.pressure-card {
padding: 15px;
border: 1px solid rgba(135, 212, 230, 0.12);
border-radius: 13px;
background:
linear-gradient(135deg, rgba(83, 224, 193, 0.075), rgba(98, 217, 237, 0.025));
}
.pressure-top {
display: flex;
align-items: flex-start;
justify-content: space-between;
gap: 12px;
margin-bottom: 17px;
}
.pressure-number {
font-variant-numeric: tabular-nums;
font-size: 2.15rem;
font-weight: 760;
line-height: 0.95;
letter-spacing: -0.055em;
}
.pressure-number small {
display: block;
margin-top: 6px;
color: var(--muted);
font-size: 0.67rem;
font-weight: 700;
letter-spacing: 0.12em;
text-transform: uppercase;
}
.relation {
padding: 5px 8px;
border: 1px solid rgba(83, 224, 193, 0.25);
border-radius: 999px;
background: rgba(83, 224, 193, 0.08);
color: var(--mint);
font-size: 0.64rem;
font-weight: 800;
white-space: nowrap;
}
.relation.hot {
border-color: rgba(255, 180, 93, 0.3);
background: rgba(255, 180, 93, 0.09);
color: var(--orange);
}
.relation.cold {
border-color: rgba(103, 191, 255, 0.3);
background: rgba(103, 191, 255, 0.09);
color: var(--blue);
}
input[type="range"] {
--fill: 40%;
--accent: var(--mint);
width: 100%;
height: 5px;
margin: 4px 0 10px;
border-radius: 99px;
outline: none;
appearance: none;
background: linear-gradient(
to right,
var(--accent) 0%,
var(--accent) var(--fill),
rgba(255, 255, 255, 0.12) var(--fill),
rgba(255, 255, 255, 0.12) 100%
);
cursor: pointer;
}
input[type="range"]::-webkit-slider-thumb {
width: 17px;
height: 17px;
border: 3px solid #dffaff;
border-radius: 50%;
appearance: none;
background: var(--accent);
box-shadow: 0 0 0 4px rgba(255, 255, 255, 0.05);
}
input[type="range"]::-moz-range-thumb {
width: 12px;
height: 12px;
border: 3px solid #dffaff;
border-radius: 50%;
background: var(--accent);
box-shadow: 0 0 0 4px rgba(255, 255, 255, 0.05);
}
.range-labels {
display: flex;
justify-content: space-between;
color: #708e9a;
font-size: 0.62rem;
font-weight: 700;
}
.reference {
margin: 11px 0 0;
color: var(--muted);
font-size: 0.7rem;
line-height: 1.45;
}
.reference strong {
color: #c7e8f0;
}
.presets {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 7px;
margin-top: 13px;
}
.presets button {
padding: 8px 5px;
border: 1px solid rgba(255, 255, 255, 0.09);
border-radius: 8px;
background: rgba(255, 255, 255, 0.035);
color: #b9d0d8;
font-size: 0.66rem;
font-weight: 700;
cursor: pointer;
transition: 140ms ease;
}
.presets button:hover,
.presets button.active {
border-color: rgba(83, 224, 193, 0.45);
background: rgba(83, 224, 193, 0.1);
color: #e9fff9;
}
.results-title {
margin: 19px 0 9px;
color: #a8c2cc;
font-size: 0.67rem;
font-weight: 800;
letter-spacing: 0.13em;
text-transform: uppercase;
}
.result-grid {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 7px;
}
.result {
min-width: 0;
padding: 10px 8px;
border: 1px solid rgba(255, 255, 255, 0.07);
border-radius: 9px;
background: rgba(255, 255, 255, 0.028);
}
.result span {
display: block;
margin-bottom: 5px;
color: #78949f;
font-size: 0.58rem;
font-weight: 800;
letter-spacing: 0.09em;
text-transform: uppercase;
}
.result strong {
display: block;
overflow: hidden;
color: #e8fbfd;
font-size: 0.92rem;
font-variant-numeric: tabular-nums;
text-overflow: ellipsis;
white-space: nowrap;
}
.direction {
min-height: 18px;
margin: 9px 1px 0;
color: var(--muted);
font-size: 0.72rem;
}
.formula {
margin-top: 13px;
padding: 12px;
border-left: 2px solid var(--mint);
border-radius: 0 8px 8px 0;
background: rgba(83, 224, 193, 0.055);
color: #c7dfe5;
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
font-size: 0.68rem;
line-height: 1.6;
}
.formula b {
color: var(--mint);
font-weight: 700;
}
.calculation {
display: block;
margin-top: 5px;
color: #8eacb6;
font-size: 0.62rem;
}
.assumption {
margin-top: 14px;
padding: 12px;
border: 1px solid rgba(255, 180, 93, 0.18);
border-radius: 10px;
background: rgba(255, 180, 93, 0.055);
color: #c8d7dc;
font-size: 0.7rem;
line-height: 1.5;
}
.assumption strong {
display: block;
margin-bottom: 3px;
color: var(--orange);
font-size: 0.64rem;
letter-spacing: 0.09em;
text-transform: uppercase;
}
.world-label {
position: fixed;
z-index: 3;
min-width: 128px;
padding: 7px 10px;
border: 1px solid rgba(113, 222, 236, 0.24);
border-radius: 9px;
background: rgba(6, 20, 29, 0.78);
box-shadow: 0 7px 22px rgba(0, 0, 0, 0.24);
color: #dffaff;
pointer-events: none;
transform: translate(-50%, -50%);
backdrop-filter: blur(9px);
}
.world-label span {
display: block;
color: #8eb4bf;
font-size: 0.55rem;
font-weight: 800;
letter-spacing: 0.1em;
text-transform: uppercase;
white-space: nowrap;
}
.world-label strong {
display: block;
margin-top: 3px;
font-size: 0.78rem;
font-variant-numeric: tabular-nums;
white-space: nowrap;
}
#rightTag {
border-color: rgba(83, 224, 193, 0.3);
}
.hint {
position: fixed;
z-index: 5;
left: 32px;
bottom: 27px;
display: flex;
align-items: center;
gap: 11px;
color: #76949f;
font-size: 0.68rem;
pointer-events: none;
}
.hint span {
display: inline-flex;
align-items: center;
gap: 6px;
}
.key {
padding: 3px 6px;
border: 1px solid rgba(255, 255, 255, 0.12);
border-radius: 5px;
background: rgba(255, 255, 255, 0.04);
color: #b7d2da;
font-size: 0.58rem;
}
#resetCamera {
position: fixed;
z-index: 5;
right: 420px;
bottom: 27px;
padding: 7px 11px;
border: 1px solid rgba(255, 255, 255, 0.11);
border-radius: 8px;
background: rgba(8, 23, 32, 0.76);
color: #a9c3cc;
font-size: 0.65rem;
font-weight: 700;
cursor: pointer;
}
#resetCamera:hover {
border-color: rgba(83, 224, 193, 0.4);
color: #e6fff9;
}
@media (max-width: 760px) {
header {
top: 17px;
left: 18px;
max-width: calc(100vw - 36px);
}
header p {
display: none;
}
.panel {
top: auto;
right: 12px;
bottom: 12px;
left: 12px;
width: auto;
max-height: 49vh;
padding: 15px;
border-radius: 14px;
}
.world-label {
display: none;
}
.hint {
left: 17px;
bottom: calc(49vh + 27px);
}
.hint .desktop-only {
display: none;
}
#resetCamera {
right: 17px;
bottom: calc(49vh + 27px);
}
}
</style>
</head>
<body>
<main id="stage">
<canvas id="scene"></canvas>
<div class="vignette"></div>
<header>
<div class="eyebrow">Hydrostatics / Interactive Lab</div>
<h1>Communicating Vessels</h1>
<p>
Apply pressure to one side of a connected U-tube and observe the
hydrostatic level difference.
</p>
</header>
<div id="leftTag" class="world-label">
<span>Left · Atmosphere</span>
<strong id="leftTagValue">2.550 m</strong>
</div>
<div id="rightTag" class="world-label">
<span id="rightTagTitle">Right · 101.3 kPa</span>
<strong id="rightTagValue">2.550 m</strong>
</div>
<aside class="panel" aria-label="Hydrostatic controls and results">
<div class="section-head">
<h2>External pressure</h2>
<div class="solved">Static equilibrium</div>
</div>
<section class="pressure-card">
<div class="pressure-top">
<div class="pressure-number">
<span id="pressureValue">101.3</span>
<small>kPa absolute · right vessel</small>
</div>
<span id="pressureRelation" class="relation">Balanced</span>
</div>
<label for="pressure" class="sr-only">Right-side absolute pressure</label>
<input
id="pressure"
type="range"
min="90"
max="115"
step="0.1"
value="101.3"
aria-label="Right-side absolute pressure in kilopascals"
/>
<div class="range-labels">
<span>90 kPa</span>
<span>101.3 kPa</span>
<span>115 kPa</span>
</div>
<p class="reference">
Reference pressure:
<strong>101.3 kPa atmospheric</strong> above the left vessel.
</p>
<div class="presets">
<button type="button" data-pressure="94">Sub-atmospheric</button>
<button type="button" data-pressure="101.3" class="active">Atmosphere</button>
<button type="button" data-pressure="105.3">+4 kPa load</button>
</div>
</section>
<div class="results-title">Calculated liquid levels</div>
<section class="result-grid" aria-live="polite">
<div class="result">
<span>Left</span>
<strong id="leftLevel">2.550 m</strong>
</div>
<div class="result">
<span>Right</span>
<strong id="rightLevel">2.550 m</strong>
</div>
<div class="result">
<span>Difference</span>
<strong id="levelDifference">0.000 m</strong>
</div>
</section>
<div id="direction" class="direction">The two surfaces are level.</div>
<div class="formula">
<b>P<sub>R</sub> + ρgh<sub>R</sub> = P<sub>L</sub> + ρgh<sub>L</sub></b>
<b>Δh = (P<sub>R</sub> − P<sub>L</sub>) / ρg</b>
<span id="calculation" class="calculation">
Δh = (101.3 − 101.3) × 1000 / (1000 × 9.81) = 0.000 m
</span>
</div>
<div class="assumption">
<strong>Equal-density static-fluid assumption</strong>
Both legs contain the same incompressible liquid at rest, with equal
density ρ = 1000 kg/m³. Gas density is neglected. The legs have equal
cross-sectional area, so their surface elevations sum to a constant.
</div>
</aside>
<div class="hint">
<span><span class="key">Drag</span> Orbit</span>
<span class="desktop-only"><span class="key">Wheel</span> Zoom</span>
</div>
<button id="resetCamera" type="button">Reset view</button>
</main>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// -------------------------------------------------------------------------
// Physical model
// -------------------------------------------------------------------------
const ATMOSPHERE_KPA = 101.3;
const LIQUID_DENSITY = 1000; // kg/m^3
const GRAVITY = 9.81; // m/s^2
const INITIAL_LEVEL = 2.55; // m, model surface elevation
const LEVEL_BOTTOM = 0.92; // visual bottom of each vertical liquid leg
const TUBE_TOP = 4.20;
// -------------------------------------------------------------------------
// Renderer and scene
// -------------------------------------------------------------------------
const canvas = document.getElementById("scene");
const renderer = new THREE.WebGLRenderer({
canvas,
antialias: true,
alpha: true
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight, false);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.08;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.setClearColor(0x07141e, 0);
const scene = new THREE.Scene();
scene.fog = new THREE.FogExp2(0x07141e, 0.026);
const camera = new THREE.PerspectiveCamera(
42,
window.innerWidth / window.innerHeight,
0.1,
100
);
// Direct, addon-free camera controls.
const cameraTarget = new THREE.Vector3(0, 2.08, 0);
const cameraState = {
yaw: 0.62,
pitch: 0.30,
distance: 9.1
};
function resetCamera() {
cameraState.yaw = 0.62;
cameraState.pitch = 0.30;
cameraState.distance = 9.1;
}
function updateCamera() {
const cp = Math.cos(cameraState.pitch);
camera.position.set(
cameraTarget.x + cameraState.distance * Math.sin(cameraState.yaw) * cp,
cameraTarget.y + cameraState.distance * Math.sin(cameraState.pitch),
cameraTarget.z + cameraState.distance * Math.cos(cameraState.yaw) * cp
);
camera.lookAt(cameraTarget);
camera.updateMatrixWorld();
}
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 dx = event.clientX - previousX;
const dy = event.clientY - previousY;
previousX = event.clientX;
previousY = event.clientY;
cameraState.yaw -= dx * 0.006;
cameraState.pitch = THREE.MathUtils.clamp(
cameraState.pitch + dy * 0.0045,
-0.12,
1.08
);
});
function endDrag(event) {
dragging = false;
if (canvas.hasPointerCapture(event.pointerId)) {
canvas.releasePointerCapture(event.pointerId);
}
}
canvas.addEventListener("pointerup", endDrag);
canvas.addEventListener("pointercancel", endDrag);
canvas.addEventListener("dblclick", resetCamera);
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraState.distance = THREE.MathUtils.clamp(
cameraState.distance + event.deltaY * 0.007,
6.1,
14
);
},
{ passive: false }
);
document.getElementById("resetCamera").addEventListener("click", resetCamera);
// -------------------------------------------------------------------------
// Lighting
// -------------------------------------------------------------------------
scene.add(new THREE.HemisphereLight(0xb9efff, 0x101722, 2.0));
const keyLight = new THREE.DirectionalLight(0xffffff, 3.2);
keyLight.position.set(4.5, 8, 5.5);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(1024, 1024);
keyLight.shadow.camera.left = -6;
keyLight.shadow.camera.right = 6;
keyLight.shadow.camera.top = 7;
keyLight.shadow.camera.bottom = -3;
keyLight.shadow.bias = -0.0004;
scene.add(keyLight);
const rimLight = new THREE.PointLight(0x38d9ef, 11, 11, 2);
rimLight.position.set(-3.4, 4.5, 2.8);
scene.add(rimLight);
const warmLight = new THREE.PointLight(0xff9f43, 8, 10, 2);
warmLight.position.set(3.6, 4.2, -2.8);
scene.add(warmLight);
// -------------------------------------------------------------------------
// Low-poly environment
// -------------------------------------------------------------------------
const ground = new THREE.Mesh(
new THREE.CircleGeometry(7.5, 32),
new THREE.MeshStandardMaterial({
color: 0x081923,
roughness: 0.94,
metalness: 0.04
})
);
ground.rotation.x = -Math.PI / 2;
ground.position.y = -0.015;
ground.receiveShadow = true;
scene.add(ground);
const platform = new THREE.Mesh(
new THREE.CylinderGeometry(3.25, 3.48, 0.22, 16),
new THREE.MeshStandardMaterial({
color: 0x102a37,
roughness: 0.72,
metalness: 0.12,
flatShading: true
})
);
platform.position.y = 0.10;
platform.receiveShadow = true;
platform.castShadow = true;
scene.add(platform);
const platformInset = new THREE.Mesh(
new THREE.CylinderGeometry(2.85, 2.92, 0.035, 16),
new THREE.MeshStandardMaterial({
color: 0x173846,
roughness: 0.78,
metalness: 0.08,
flatShading: true
})
);
platformInset.position.y = 0.225;
platformInset.receiveShadow = true;
scene.add(platformInset);
const grid = new THREE.GridHelper(12, 24, 0x245263, 0x173744);
grid.position.y = 0.235;
grid.material.transparent = true;
grid.material.opacity = 0.18;
scene.add(grid);
// -------------------------------------------------------------------------
// U-tube geometry
// -------------------------------------------------------------------------
const glassMaterial = new THREE.MeshStandardMaterial({
color: 0xc3efff,
roughness: 0.16,
metalness: 0.08,
transparent: true,
opacity: 0.24,
side: THREE.DoubleSide,
depthWrite: false
});
const tubePath = new THREE.CatmullRomCurve3(
[
new THREE.Vector3(-1.25, TUBE_TOP, 0),
new THREE.Vector3(-1.25, 0.95, 0),
new THREE.Vector3(-1.14, 0.64, 0),
new THREE.Vector3(-0.72, 0.49, 0),
new THREE.Vector3(0.72, 0.49, 0),
new THREE.Vector3(1.14, 0.64, 0),
new THREE.Vector3(1.25, 0.95, 0),
new THREE.Vector3(1.25, TUBE_TOP, 0)
],
false,
"catmullrom",
0.35
);
const glassTube = new THREE.Mesh(
new THREE.TubeGeometry(tubePath, 56, 0.34, 8, false),
glassMaterial
);
glassTube.renderOrder = 3;
scene.add(glassTube);
const liquidMaterial = new THREE.MeshStandardMaterial({
color: 0x2bcbb8,
roughness: 0.24,
metalness: 0.04,
flatShading: true
});
const surfaceMaterial = new THREE.MeshStandardMaterial({
color: 0x74f5dd,
roughness: 0.16,
metalness: 0.02,
emissive: 0x0a4a42,
emissiveIntensity: 0.2,
flatShading: true
});
const lowerLiquidPath = new THREE.CatmullRomCurve3(
[
new THREE.Vector3(-1.25, 0.94, 0),
new THREE.Vector3(-1.15, 0.63, 0),
new THREE.Vector3(-0.72, 0.50, 0),
new THREE.Vector3(0.72, 0.50, 0),
new THREE.Vector3(1.15, 0.63, 0),
new THREE.Vector3(1.25, 0.94, 0)
],
false,
"catmullrom",
0.35
);
const lowerLiquid = new THREE.Mesh(
new THREE.TubeGeometry(lowerLiquidPath, 40, 0.255, 8, false),
liquidMaterial
);
lowerLiquid.castShadow = true;
lowerLiquid.receiveShadow = true;
scene.add(lowerLiquid);
const verticalGeometry = new THREE.CylinderGeometry(
0.265,
0.265,
1,
12,
1,
false
);
const surfaceGeometry = new THREE.CylinderGeometry(
0.267,
0.267,
0.035,
12
);
function createLiquidLeg(x) {
const column = new THREE.Mesh(verticalGeometry, liquidMaterial);
column.castShadow = true;
column.receiveShadow = true;
const surface = new THREE.Mesh(surfaceGeometry, surfaceMaterial);
surface.castShadow = true;
column.position.x = surface.position.x = x;
scene.add(column, surface);
return { column, surface };
}
const leftLeg = createLiquidLeg(-1.25), rightLeg = createLiquidLeg(1.25);
// Top rims emphasize that both vessels are open to their gas regions.
const rimMaterial = new THREE.MeshStandardMaterial({
color: 0x8edce7,
roughness: 0.25,
metalness: 0.24,
flatShading: true
});
for (const x of [-1.25, 1.25]) {
const rim = new THREE.Mesh(
new THREE.TorusGeometry(0.34, 0.047, 6, 16),
rimMaterial
);
rim.rotation.x = Math.PI / 2;
rim.position.set(x, TUBE_TOP + 0.02, 0);
rim.castShadow = true;
scene.add(rim);
}
// Small measurement ticks.
const tickMaterial = new THREE.MeshStandardMaterial({
color: 0x7eaab6,
roughness: 0.6,
metalness: 0.12
});
for (let y = 1.0; y <= 4.0; y += 0.5) {
for (const side of [-1, 1]) {
const tick = new THREE.Mesh(
new THREE.BoxGeometry(0.17, 0.025, 0.035),
tickMaterial
);
tick.position.set(side * 1.58, y, 0);
scene.add(tick);
}
}
// -------------------------------------------------------------------------
// Gas regions above the liquid surfaces
// -------------------------------------------------------------------------
const gasGeometry = new THREE.CylinderGeometry(
0.315,
0.315,
1,
12,
1,
true
);
const leftGasMaterial = new THREE.MeshBasicMaterial({
color: 0x56cfff,
transparent: true,
opacity: 0.065,
side: THREE.DoubleSide,
depthWrite: false
});
const rightGasMaterial = new THREE.MeshBasicMaterial({
color: 0x53e0c1,
transparent: true,
opacity: 0.065,
side: THREE.DoubleSide,
depthWrite: false
});
const leftGas = new THREE.Mesh(gasGeometry, leftGasMaterial);
const rightGas = new THREE.Mesh(gasGeometry, rightGasMaterial);
leftGas.renderOrder = 1;
rightGas.renderOrder = 1;
leftGas.position.x = -1.25; rightGas.position.x = 1.25;
scene.add(leftGas, rightGas);
// -------------------------------------------------------------------------
// Applied-pressure arrows
// -------------------------------------------------------------------------
const pressureArrows = [];
const arrowGroup = new THREE.Group();
scene.add(arrowGroup);
for (const xOffset of [-0.18, 0, 0.18]) {
const arrow = new THREE.ArrowHelper(
new THREE.Vector3(0, -1, 0),
new THREE.Vector3(1.25 + xOffset, 4.78, 0),
0.40,
0xffb45d,
0.13,
0.085
);
arrow.line.material.transparent = true;
arrow.cone.material.transparent = true;
arrowGroup.add(arrow);
pressureArrows.push(arrow);
}
// -------------------------------------------------------------------------
// UI references
// -------------------------------------------------------------------------
const pressureInput = document.getElementById("pressure");
const pressureValue = document.getElementById("pressureValue");
const pressureRelation = document.getElementById("pressureRelation");
const leftLevelOutput = document.getElementById("leftLevel");
const rightLevelOutput = document.getElementById("rightLevel");
const levelDifferenceOutput = document.getElementById("levelDifference");
const directionOutput = document.getElementById("direction");
const calculationOutput = document.getElementById("calculation");
const leftTagValue = document.getElementById("leftTagValue");
const rightTagValue = document.getElementById("rightTagValue");
const rightTagTitle = document.getElementById("rightTagTitle");
const presetButtons = [...document.querySelectorAll("[data-pressure]")];
let currentLeftLevel = INITIAL_LEVEL;
let currentRightLevel = INITIAL_LEVEL;
function setLegLevel(leg, level) {
const height = Math.max(0.02, level - LEVEL_BOTTOM);
leg.column.scale.y = height;
leg.column.position.y = LEVEL_BOTTOM + height / 2;
leg.surface.position.y = level;
}
function setGasColumn(mesh, lowerY, upperY) {
const height = Math.max(0.02, upperY - lowerY);
mesh.scale.y = height;
mesh.position.y = lowerY + height / 2;
}
function updatePressure() {
const rightPressureKPa = Number(pressureInput.value);
const pressureDeltaKPa = rightPressureKPa - ATMOSPHERE_KPA;
const pressureDeltaPa = pressureDeltaKPa * 1000;
// At a common horizontal level in the connected liquid:
// P_L + rho*g*h_L = P_R + rho*g*h_R
// Therefore h_L - h_R = (P_R - P_L)/(rho*g).
const signedDifference =
pressureDeltaPa / (LIQUID_DENSITY * GRAVITY);
// Equal-bore legs and conserved volume imply:
// h_L + h_R = 2 * INITIAL_LEVEL.
currentLeftLevel = INITIAL_LEVEL + signedDifference / 2;
currentRightLevel = INITIAL_LEVEL - signedDifference / 2;
setLegLevel(leftLeg, currentLeftLevel);
setLegLevel(rightLeg, currentRightLevel);
setGasColumn(leftGas, currentLeftLevel + 0.025, TUBE_TOP - 0.03);
setGasColumn(rightGas, currentRightLevel + 0.025, TUBE_TOP - 0.03);
// Pressure slider fill and label.
const percentage =
((rightPressureKPa - Number(pressureInput.min)) /
(Number(pressureInput.max) - Number(pressureInput.min))) *
100;
pressureInput.style.setProperty("--fill", `${percentage}%`);
pressureValue.textContent = rightPressureKPa.toFixed(1);
if (Math.abs(pressureDeltaKPa) < 0.05) {
pressureRelation.textContent = "Balanced";
pressureRelation.className = "relation";
pressureInput.style.setProperty("--accent", "#53e0c1");
} else if (pressureDeltaKPa > 0) {
pressureRelation.textContent = `+${pressureDeltaKPa.toFixed(1)} kPa`;
pressureRelation.className = "relation hot";
pressureInput.style.setProperty("--accent", "#ffb45d");
} else {
pressureRelation.textContent = `${pressureDeltaKPa.toFixed(1)} kPa`;
pressureRelation.className = "relation cold";
pressureInput.style.setProperty("--accent", "#67bfff");
}
// Results.
const absoluteDifference = Math.abs(signedDifference);
leftLevelOutput.textContent = `${currentLeftLevel.toFixed(3)} m`;
rightLevelOutput.textContent = `${currentRightLevel.toFixed(3)} m`;
levelDifferenceOutput.textContent = `${absoluteDifference.toFixed(3)} m`;
leftTagValue.textContent = `${currentLeftLevel.toFixed(3)} m`;
rightTagValue.textContent = `${currentRightLevel.toFixed(3)} m`;
rightTagTitle.textContent = `Right · ${rightPressureKPa.toFixed(1)} kPa`;
if (absoluteDifference < 0.0005) {
directionOutput.textContent = "The two surfaces are level.";
} else if (signedDifference > 0) {
directionOutput.textContent =
"The left surface is higher because the right side is under greater pressure.";
} else {
directionOutput.textContent =
"The right surface is higher because the right side is under lower pressure.";
}
calculationOutput.textContent =
`|Δh| = |(${rightPressureKPa.toFixed(1)} − 101.3) × 1000 / ` +
`(1000 × 9.81)| = ${absoluteDifference.toFixed(3)} m`;
// Right gas chamber and arrows.
const magnitude = Math.min(1, Math.abs(pressureDeltaKPa) / 15);
const arrowColor =
pressureDeltaKPa > 0.05
? 0xffb45d
: pressureDeltaKPa < -0.05
? 0x67bfff
: 0x53e0c1;
rightGasMaterial.color.setHex(arrowColor);
rightGasMaterial.opacity = 0.065 + magnitude * 0.13;
pressureArrows.forEach((arrow, index) => {
const visible = Math.abs(pressureDeltaKPa) > 0.05;
arrow.visible = visible;
if (!visible) return;
const direction =
pressureDeltaKPa > 0
? new THREE.Vector3(0, -1, 0)
: new THREE.Vector3(0, 1, 0);
const originY =
pressureDeltaKPa > 0 ? 4.78 : 4.20;
const length = 0.32 + magnitude * 0.24;
const x = 1.25 + [-0.18, 0, 0.18][index];
arrow.position.set(x, originY, 0);
arrow.setDirection(direction);
arrow.setLength(length, 0.13, 0.085);
arrow.setColor(arrowColor);
arrow.line.material.opacity = 0.50 + magnitude * 0.45;
arrow.cone.material.opacity = 0.65 + magnitude * 0.35;
});
// Active preset state.
presetButtons.forEach((button) => {
const value = Number(button.dataset.pressure);
button.classList.toggle(
"active",
Math.abs(value - rightPressureKPa) < 0.05
);
});
}
pressureInput.addEventListener("input", updatePressure);
presetButtons.forEach((button) => {
button.addEventListener("click", () => {
pressureInput.value = button.dataset.pressure;
updatePressure();
});
});
updatePressure();
// -------------------------------------------------------------------------
// Projected HTML 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.opacity = visible ? "1" : "0";
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`;
}
// -------------------------------------------------------------------------
// Resize and render loop
// -------------------------------------------------------------------------
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight, false);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
});
function animate() {
requestAnimationFrame(animate);
updateCamera();
placeLabel(
document.getElementById("leftTag"),
-1.25,
currentLeftLevel + 0.48,
0
);
placeLabel(
document.getElementById("rightTag"),
1.25,
currentRightLevel + 0.48,
0
);
renderer.render(scene, camera);
}
animate();
</script>
</body>
</html>
```