Prompt specification
Build or customize the Three.js 3D scene "Bernoulli energy-head comparison" 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: Bernoulli energy-head comparison
Category: Science & Physics · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/bernoulli-energy-head-comparison-x1514
Scene Description Prompt:
Build a self-contained HTML scene with Three.js from cdnjs featuring a stylized solid Bernoulli-head teaching model. Show a pipe narrowing between two selected sections at the same elevation, with a small measurement stand beside each section; each stand has separate columns for elevation head, pressure head, and velocity head, all using the same head units. Under an explicitly steady, incompressible, lossless assumption, changing the flow rate should alter the pressure and velocity terms while preserving their stated total along the streamline. Align a total-head marker across both stands. Make the columns look like explanatory measurements rather than vertical extensions of the pipe itself.
Working Three.js Starter (bernoulli-energy-head-comparison-x1514):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Bernoulli Head Bench</title>
<style>
:root {
color-scheme: light;
--ink: #18343a;
--muted: #61757a;
--paper: #f3f1e8;
--panel: #fbfaf5;
--line: #d8ddd6;
--elevation: #e5ad32;
--pressure: #dd6a63;
--velocity: #3288c9;
--total: #ef8b32;
}
* { box-sizing: border-box; }
html, body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
color: var(--ink);
background: var(--paper);
}
body {
display: grid;
grid-template-columns: minmax(0, 1fr) clamp(340px, 31vw, 430px);
}
#viewport {
position: relative;
min-width: 0;
min-height: 0;
overflow: hidden;
}
#viewport canvas {
display: block;
width: 100%;
height: 100%;
outline: none;
touch-action: none;
}
.scene-hint {
position: absolute;
left: 18px;
bottom: 16px;
z-index: 2;
display: flex;
align-items: center;
gap: 10px;
padding: 9px 12px;
border: 1px solid rgba(24, 52, 58, 0.12);
border-radius: 999px;
background: rgba(251, 250, 245, 0.86);
color: #4d676c;
font-size: 12px;
letter-spacing: 0.02em;
backdrop-filter: blur(8px);
pointer-events: none;
}
.scene-hint button {
pointer-events: auto;
border: 0;
border-radius: 999px;
padding: 5px 9px;
background: #18343a;
color: white;
font: inherit;
font-size: 11px;
cursor: pointer;
}
aside {
position: relative;
z-index: 3;
height: 100vh;
overflow-y: auto;
padding: 28px 28px 30px;
border-left: 1px solid rgba(24, 52, 58, 0.12);
background:
linear-gradient(rgba(24, 52, 58, 0.035) 1px, transparent 1px),
linear-gradient(90deg, rgba(24, 52, 58, 0.035) 1px, transparent 1px),
var(--panel);
background-size: 24px 24px;
box-shadow: -12px 0 35px rgba(27, 53, 55, 0.08);
}
.eyebrow {
margin-bottom: 9px;
color: #668087;
font-size: 11px;
font-weight: 800;
letter-spacing: 0.16em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: clamp(26px, 3vw, 38px);
line-height: 1.03;
letter-spacing: -0.045em;
}
.intro {
margin: 13px 0 19px;
color: var(--muted);
font-size: 14px;
line-height: 1.55;
}
.assumptions {
display: flex;
flex-wrap: wrap;
gap: 7px;
margin-bottom: 20px;
}
.assumptions span {
padding: 6px 9px;
border: 1px solid #cbd7d2;
border-radius: 999px;
background: #eef4ef;
color: #315c58;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.09em;
text-transform: uppercase;
}
.control-card,
.readout-card,
.equation-card {
border: 1px solid rgba(24, 52, 58, 0.12);
border-radius: 17px;
background: rgba(255, 255, 252, 0.86);
box-shadow: 0 8px 22px rgba(28, 56, 58, 0.045);
}
.control-card {
padding: 17px 18px;
}
.control-top {
display: flex;
justify-content: space-between;
align-items: baseline;
gap: 12px;
}
.control-top label {
font-size: 13px;
font-weight: 750;
}
#flow-readout {
color: #1f6068;
font-size: 18px;
font-weight: 800;
font-variant-numeric: tabular-nums;
}
input[type="range"] {
width: 100%;
margin: 16px 0 4px;
accent-color: #277b79;
cursor: pointer;
}
.range-labels {
display: flex;
justify-content: space-between;
color: #819196;
font-size: 10px;
font-variant-numeric: tabular-nums;
}
.readout-card {
margin-top: 14px;
overflow: hidden;
}
.readout-title {
display: flex;
justify-content: space-between;
align-items: center;
gap: 10px;
padding: 12px 15px;
border-bottom: 1px solid var(--line);
background: #f3f6f1;
color: #4b666b;
font-size: 11px;
font-weight: 750;
letter-spacing: 0.04em;
text-transform: uppercase;
}
.unit-note {
color: #75888c;
font-size: 9px;
font-weight: 650;
letter-spacing: 0.04em;
}
table {
width: 100%;
border-collapse: collapse;
font-size: 12px;
font-variant-numeric: tabular-nums;
}
th, td {
padding: 10px 12px;
border-bottom: 1px solid #e5e8e2;
text-align: right;
}
th:first-child, td:first-child {
text-align: left;
}
th {
color: #60777c;
font-size: 10px;
letter-spacing: 0.06em;
text-transform: uppercase;
}
td:first-child {
color: #4a6267;
}
tr:last-child td {
border-bottom: 0;
background: #fff9eb;
font-weight: 800;
}
.term-dot {
display: inline-block;
width: 7px;
height: 7px;
margin-right: 7px;
border-radius: 50%;
vertical-align: 1px;
}
.elevation-dot { background: var(--elevation); }
.pressure-dot { background: var(--pressure); }
.velocity-dot { background: var(--velocity); }
.equation-card {
margin-top: 14px;
padding: 16px;
}
.equation {
margin: 0 0 9px;
color: #1b3f45;
font-family: Georgia, "Times New Roman", serif;
font-size: 19px;
line-height: 1.45;
text-align: center;
}
.equation small {
display: block;
margin-top: 4px;
color: #6c7f83;
font-family: inherit;
font-size: 11px;
}
.continuity {
margin: 11px 0 0;
color: #597076;
font-size: 12px;
line-height: 1.45;
text-align: center;
}
.note {
margin: 15px 2px 0;
color: #6b7d81;
font-size: 11px;
line-height: 1.5;
}
.note strong { color: #39575d; }
@media (max-width: 820px) {
body {
grid-template-columns: 1fr;
grid-template-rows: 58vh 42vh;
}
aside {
height: 42vh;
padding: 18px 20px 28px;
border-top: 1px solid rgba(24, 52, 58, 0.12);
border-left: 0;
}
.scene-hint {
left: 10px;
bottom: 10px;
}
}
</style>
</head>
<body>
<main id="viewport" aria-label="Interactive three-dimensional Bernoulli head model"></main>
<aside aria-label="Bernoulli model controls and measurements">
<div class="eyebrow">Fluid mechanics teaching model</div>
<h1>Bernoulli Head Bench</h1>
<p class="intro">
Compare elevation, pressure, and velocity heads at two equal-elevation
sections of a contracting pipe.
</p>
<div class="assumptions" aria-label="Model assumptions">
<span>Steady</span>
<span>Incompressible</span>
<span>Lossless</span>
<span>One streamline</span>
</div>
<section class="control-card">
<div class="control-top">
<label for="flow">Flow rate, Q</label>
<output id="flow-readout" for="flow">1.00 m³/s</output>
</div>
<input
id="flow"
type="range"
min="0.45"
max="1.60"
step="0.01"
value="1.00"
/>
<div class="range-labels">
<span>0.45 m³/s</span>
<span>1.60 m³/s</span>
</div>
</section>
<section class="readout-card" aria-live="polite">
<div class="readout-title">
<span>Section measurements</span>
<span class="unit-note">All head terms in metres</span>
</div>
<table>
<thead>
<tr>
<th>Quantity</th>
<th>Section 1</th>
<th>Section 2</th>
</tr>
</thead>
<tbody>
<tr>
<td><span class="term-dot elevation-dot"></span>Elevation z (m)</td>
<td id="z1">2.00</td>
<td id="z2">2.00</td>
</tr>
<tr>
<td><span class="term-dot pressure-dot"></span>Pressure p/ρg (m)</td>
<td id="p1">2.94</td>
<td id="p2">2.82</td>
</tr>
<tr>
<td><span class="term-dot velocity-dot"></span>Velocity v²/2g (m)</td>
<td id="v1h">0.06</td>
<td id="v2h">0.18</td>
</tr>
<tr>
<td>Velocity v (m/s)</td>
<td id="v1">1.27</td>
<td id="v2">2.12</td>
</tr>
<tr>
<td>Total head H (m)</td>
<td id="h1">5.00</td>
<td id="h2">5.00</td>
</tr>
</tbody>
</table>
</section>
<section class="equation-card">
<div class="equation">
z + p/ρg + v²/2g = 5.00 m
<small>Bernoulli head, conserved along the streamline</small>
</div>
<div class="continuity">
Continuity: A₁v₁ = A₂v₂ = Q
</div>
</section>
<p class="note">
<strong>Interpretation:</strong> increasing Q raises velocity head and
lowers pressure head, especially in the narrow section. The common
elevation and the aligned total-head marker remain unchanged because
the model is explicitly steady, incompressible, and lossless.
</p>
</aside>
<div class="scene-hint">
<span>Drag to orbit · wheel to zoom</span>
<button id="reset-camera" type="button">Reset view</button>
</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. The pressure terms are gauge pressure heads.
// -------------------------------------------------------------------------
const viewport = document.querySelector("#viewport");
const ELEVATION = 2.00; // m, relative to common datum
const TOTAL_HEAD = 5.00; // m, stated Bernoulli total
const GRAVITY = 9.81; // m/s²
const HEAD_SCALE = 0.50; // world units per metre of head
const DATUM_Y = 0.18; // world height of head zero
const PIPE_Y = DATUM_Y + ELEVATION * HEAD_SCALE;
const D1 = 1.00; // m
const D2 = 0.60; // m
const A1 = Math.PI * D1 * D1 / 4; // m²
const A2 = Math.PI * D2 * D2 / 4; // m²
const SECTION_1_X = -2.60;
const SECTION_2_X = 2.60;
// -------------------------------------------------------------------------
// Renderer, scene, camera, and lighting.
// -------------------------------------------------------------------------
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xf3f1e8);
scene.fog = new THREE.Fog(0xf3f1e8, 18, 32);
const camera = new THREE.PerspectiveCamera(42, 1, 0.1, 80);
const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.05;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
viewport.appendChild(renderer.domElement);
const hemi = new THREE.HemisphereLight(0xffffff, 0x8ca5a0, 2.25);
scene.add(hemi);
const key = new THREE.DirectionalLight(0xfff8e8, 3.1);
key.position.set(4.5, 10, 7.5);
key.castShadow = true;
key.shadow.mapSize.set(2048, 2048);
key.shadow.camera.left = -10;
key.shadow.camera.right = 10;
key.shadow.camera.top = 9;
key.shadow.camera.bottom = -8;
key.shadow.camera.near = 1;
key.shadow.camera.far = 30;
key.shadow.normalBias = 0.035;
scene.add(key);
const fillLight = new THREE.DirectionalLight(0xb9e2ec, 1.1);
fillLight.position.set(-7, 4, -5);
scene.add(fillLight);
// -------------------------------------------------------------------------
// Materials and reusable helpers.
// -------------------------------------------------------------------------
const mat = {
base: new THREE.MeshStandardMaterial({
color: 0xdce3dc, roughness: 0.82, metalness: 0.02
}),
baseTop: new THREE.MeshStandardMaterial({
color: 0xf5f3e9, roughness: 0.88
}),
pipe: new THREE.MeshStandardMaterial({
color: 0x287d76, roughness: 0.34, metalness: 0.18
}),
pipeEdge: new THREE.MeshStandardMaterial({
color: 0xb7ddd3, roughness: 0.48
}),
fluid: new THREE.MeshStandardMaterial({
color: 0x147fae, roughness: 0.27, metalness: 0.06
}),
ring: new THREE.MeshStandardMaterial({
color: 0xf0b33d, roughness: 0.35, metalness: 0.22
}),
stand: new THREE.MeshStandardMaterial({
color: 0xf6f2e7, roughness: 0.72
}),
standEdge: new THREE.MeshStandardMaterial({
color: 0xcdd5ce, roughness: 0.8
}),
dark: new THREE.MeshStandardMaterial({
color: 0x244a50, roughness: 0.45
}),
elevation: new THREE.MeshStandardMaterial({
color: 0xe5ad32, roughness: 0.45
}),
pressure: new THREE.MeshStandardMaterial({
color: 0xdd6a63, roughness: 0.45
}),
velocity: new THREE.MeshStandardMaterial({
color: 0x3288c9, roughness: 0.45
}),
total: new THREE.MeshStandardMaterial({
color: 0xef8b32, roughness: 0.38
})
};
function box(w, h, d, material, x, y, z, parent = scene) {
const mesh = new THREE.Mesh(new THREE.BoxGeometry(w, h, d), material);
mesh.position.set(x, y, z);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function beamBetween(a, b, radius, material, parent = scene) {
const va = new THREE.Vector3(...a);
const vb = new THREE.Vector3(...b);
const direction = new THREE.Vector3().subVectors(vb, va);
const length = direction.length();
const mesh = new THREE.Mesh(
new THREE.CylinderGeometry(radius, radius, length, 12),
material
);
mesh.position.copy(va).add(vb).multiplyScalar(0.5);
mesh.quaternion.setFromUnitVectors(
new THREE.Vector3(0, 1, 0),
direction.normalize()
);
mesh.castShadow = true;
parent.add(mesh);
return mesh;
}
function roundedRect(ctx, x, y, w, h, r) {
ctx.beginPath();
ctx.moveTo(x + r, y);
ctx.lineTo(x + w - r, y);
ctx.quadraticCurveTo(x + w, y, x + w, y + r);
ctx.lineTo(x + w, y + h - r);
ctx.quadraticCurveTo(x + w, y + h, x + w - r, y + h);
ctx.lineTo(x + r, y + h);
ctx.quadraticCurveTo(x, y + h, x, y + h - r);
ctx.lineTo(x, y + r);
ctx.quadraticCurveTo(x, y, x + r, y);
ctx.closePath();
}
function makeTextSprite(
text,
{
width = 1,
height = 0.24,
background = "rgba(255,255,252,0.94)",
foreground = "#18343a",
accent = null,
canvasWidth = 512,
canvasHeight = 128
} = {}
) {
const canvas = document.createElement("canvas");
canvas.width = canvasWidth;
canvas.height = canvasHeight;
const ctx = canvas.getContext("2d");
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.anisotropy = Math.min(8, renderer.capabilities.getMaxAnisotropy());
const material = new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthTest: false,
depthWrite: false
});
const sprite = new THREE.Sprite(material);
sprite.scale.set(width, height, 1);
sprite.renderOrder = 20;
function draw(nextText) {
ctx.clearRect(0, 0, canvas.width, canvas.height);
roundedRect(
ctx,
3, 3,
canvas.width - 6, canvas.height - 6,
Math.min(20, canvas.height * 0.18)
);
ctx.fillStyle = background;
ctx.fill();
if (accent) {
ctx.fillStyle = accent;
ctx.fillRect(15, 14, 8, canvas.height - 28);
}
const length = nextText.length;
const fontSize =
length > 20 ? 31 :
length > 15 ? 37 :
length > 11 ? 43 : 48;
ctx.font = `750 ${fontSize}px ui-sans-serif, system-ui, sans-serif`;
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.fillStyle = foreground;
ctx.fillText(
nextText,
canvas.width / 2,
canvas.height / 2 + 2,
canvas.width * 0.89
);
texture.needsUpdate = true;
}
draw(text);
sprite.userData.setText = draw;
return sprite;
}
// -------------------------------------------------------------------------
// Base and measurement-table setting.
// -------------------------------------------------------------------------
const ground = new THREE.Mesh(
new THREE.PlaneGeometry(80, 80),
new THREE.MeshStandardMaterial({
color: 0xf3f1e8,
roughness: 1
})
);
ground.rotation.x = -Math.PI / 2;
ground.position.y = -0.25;
ground.receiveShadow = true;
scene.add(ground);
const base = box(11.6, 0.20, 6.1, mat.base, 0, -0.12, 0);
const baseTop = box(11.25, 0.035, 5.75, mat.baseTop, 0, 0.005, 0);
const grid = new THREE.GridHelper(12, 24, 0xaebeb8, 0xd3ddd6);
grid.position.y = 0.026;
grid.scale.set(0.92, 0.92, 0.47);
grid.material.transparent = true;
grid.material.opacity = 0.25;
scene.add(grid);
// -------------------------------------------------------------------------
// Cutaway pipe. The missing upper-front quadrant reveals the blue stream.
// -------------------------------------------------------------------------
function createPipeSegment(x1, x2, r1, r2) {
const length = x2 - x1;
const centerX = (x1 + x2) / 2;
// CylinderGeometry is rotated so its axis points along world X.
// thetaStart leaves the upper-front quarter open.
const shellGeometry = new THREE.CylinderGeometry(
r1, r2, length, 64, 1, true,
Math.PI / 2, Math.PI * 1.5
);
shellGeometry.rotateZ(Math.PI / 2);
const shell = new THREE.Mesh(shellGeometry, mat.pipe);
shell.position.set(centerX, PIPE_Y, 0);
shell.castShadow = true;
shell.receiveShadow = true;
scene.add(shell);
const fluidGeometry = new THREE.CylinderGeometry(
r1 - 0.075, r2 - 0.075, length, 48
);
fluidGeometry.rotateZ(Math.PI / 2);
const fluid = new THREE.Mesh(fluidGeometry, mat.fluid);
fluid.position.set(centerX, PIPE_Y, 0);
fluid.castShadow = true;
fluid.receiveShadow = true;
scene.add(fluid);
// Highlight the two longitudinal cut edges.
beamBetween(
[x1, PIPE_Y + r1, 0],
[x2, PIPE_Y + r2, 0],
0.019,
mat.pipeEdge
);
beamBetween(
[x1, PIPE_Y, r1],
[x2, PIPE_Y, r2],
0.019,
mat.pipeEdge
);
}
createPipeSegment(-4.30, SECTION_1_X, 0.52, 0.52);
createPipeSegment(SECTION_1_X, SECTION_2_X, 0.52, 0.30);
createPipeSegment(SECTION_2_X, 4.30, 0.30, 0.30);
function sectionRing(x, radius, label) {
const ring = new THREE.Mesh(
new THREE.TorusGeometry(radius, 0.045, 14, 72),
mat.ring
);
ring.rotation.y = Math.PI / 2;
ring.position.set(x, PIPE_Y, 0);
ring.castShadow = true;
scene.add(ring);
const sprite = makeTextSprite(label, {
width: 1.08,
height: 0.25,
background: "rgba(255,249,226,0.96)",
foreground: "#765414",
accent: "#e5ad32"
});
sprite.position.set(x, PIPE_Y + 0.74, 0.15);
scene.add(sprite);
}
sectionRing(SECTION_1_X, 0.52, "SECTION 1");
sectionRing(SECTION_2_X, 0.30, "SECTION 2");
// Pipe supports.
for (const x of [-3.75, 3.75]) {
box(0.34, 0.77, 0.56, mat.dark, x, 0.39, 0);
box(0.54, 0.10, 0.72, mat.standEdge, x, 0.08, 0);
}
// Flow-direction arrow above the pipe.
const flowArrow = new THREE.Group();
flowArrow.position.set(0, 2.04, -0.64);
scene.add(flowArrow);
beamBetween(
[-3.75, 0, 0], [2.72, 0, 0],
0.037, mat.dark, flowArrow
);
const arrowHead = new THREE.Mesh(
new THREE.ConeGeometry(0.14, 0.38, 24),
mat.dark
);
arrowHead.rotation.z = -Math.PI / 2;
arrowHead.position.set(2.89, 0, 0);
arrowHead.castShadow = true;
flowArrow.add(arrowHead);
const flowSprite = makeTextSprite("FLOW Q = 1.00 m³/s", {
width: 1.85,
height: 0.28,
background: "rgba(238,246,244,0.95)",
foreground: "#24565a",
accent: "#287d76"
});
flowSprite.position.set(0, 2.30, -0.64);
scene.add(flowSprite);
// -------------------------------------------------------------------------
// Measurement stands. These are separate explanatory gauges beside the
// pipe, not extensions of the pipe itself.
// -------------------------------------------------------------------------
const termConfig = {
elevation: {
label: "z (m)",
color: 0xe5ad32,
material: mat.elevation,
accent: "#e5ad32"
},
pressure: {
label: "p/ρg (m)",
color: 0xdd6a63,
material: mat.pressure,
accent: "#dd6a63"
},
velocity: {
label: "v²/2g (m)",
color: 0x3288c9,
material: mat.velocity,
accent: "#3288c9"
}
};
const termOrder = ["elevation", "pressure", "velocity"];
const standData = [];
function createStand(sectionNumber, centerX) {
const z = 1.28;
const offsets = [-0.60, 0, 0.60];
const railHeight = TOTAL_HEAD * HEAD_SCALE;
const railTop = DATUM_Y + railHeight;
box(1.98, 0.12, 0.68, mat.standEdge, centerX, 0.08, z);
box(1.88, 0.035, 0.58, mat.stand, centerX, 0.157, z);
const stand = {
sectionNumber,
centerX,
terms: {}
};
const standLabel = makeTextSprite(
`MEASUREMENT ${sectionNumber}`,
{
width: 1.18,
height: 0.22,
background: "rgba(247,245,235,0.96)",
foreground: "#35585d"
}
);
standLabel.position.set(centerX, 0.13, z + 0.37);
scene.add(standLabel);
termOrder.forEach((termName, index) => {
const config = termConfig[termName];
const x = centerX + offsets[index];
const rail = box(
0.18, railHeight, 0.15,
mat.stand,
x, DATUM_Y + railHeight / 2, z
);
rail.material = new THREE.MeshStandardMaterial({
color: 0xf4f1e7,
roughness: 0.72,
transparent: true,
opacity: 0.90
});
box(0.28, 0.075, 0.31, mat.standEdge, x, DATUM_Y, z);
const fill = box(
0.105, 1, 0.105,
config.material,
x, DATUM_Y, z
);
const cap = box(
0.15, 0.045, 0.15,
config.material,
x, DATUM_Y, z
);
// Common 0.5 m ticks on every gauge.
for (let head = 0.5; head < TOTAL_HEAD; head += 0.5) {
box(
0.068, 0.012, 0.018,
mat.standEdge,
x + 0.118,
DATUM_Y + head * HEAD_SCALE,
z + 0.081
);
}
const header = makeTextSprite(config.label, {
width: 0.92,
height: 0.22,
background: "rgba(255,255,252,0.96)",
foreground: "#29494f",
accent: config.accent
});
header.position.set(x, railTop + 0.22, z);
scene.add(header);
const value = makeTextSprite("0.00 m", {
width: 0.57,
height: 0.16,
background: "rgba(255,255,252,0.97)",
foreground: "#29494f",
accent: config.accent,
canvasWidth: 384,
canvasHeight: 96
});
scene.add(value);
stand.terms[termName] = { fill, cap, value, z };
});
standData.push(stand);
return stand;
}
// Stands are immediately inward of the selected pipe sections so that the
// section rings remain unobstructed.
createStand(1, SECTION_1_X + 0.90);
createStand(2, SECTION_2_X - 0.90);
// -------------------------------------------------------------------------
// Total-head marker: one common horizontal marker across both stands.
// -------------------------------------------------------------------------
const totalY = DATUM_Y + TOTAL_HEAD * HEAD_SCALE;
const totalLineGeometry = new THREE.BufferGeometry().setFromPoints([
new THREE.Vector3(-2.35, totalY, 1.40),
new THREE.Vector3( 2.35, totalY, 1.40)
]);
const totalLine = new THREE.Line(
totalLineGeometry,
new THREE.LineDashedMaterial({
color: 0xef8b32,
dashSize: 0.18,
gapSize: 0.09,
depthTest: false,
transparent: true,
opacity: 0.95
})
);
totalLine.computeLineDistances();
totalLine.renderOrder = 15;
scene.add(totalLine);
for (const stand of standData) {
const marker = new THREE.Mesh(
new THREE.OctahedronGeometry(0.075),
mat.total
);
marker.position.set(stand.centerX, totalY, 1.40);
marker.castShadow = true;
scene.add(marker);
}
const totalLabel = makeTextSprite("TOTAL HEAD H = 5.00 m", {
width: 1.92,
height: 0.28,
background: "rgba(255,244,222,0.97)",
foreground: "#7a4318",
accent: "#ef8b32"
});
totalLabel.position.set(0, totalY + 0.13, 1.43);
scene.add(totalLabel);
// -------------------------------------------------------------------------
// Update measurements.
// -------------------------------------------------------------------------
const flowInput = document.querySelector("#flow");
const flowReadout = document.querySelector("#flow-readout");
const dom = {
z1: document.querySelector("#z1"),
z2: document.querySelector("#z2"),
p1: document.querySelector("#p1"),
p2: document.querySelector("#p2"),
v1h: document.querySelector("#v1h"),
v2h: document.querySelector("#v2h"),
v1: document.querySelector("#v1"),
v2: document.querySelector("#v2"),
h1: document.querySelector("#h1"),
h2: document.querySelector("#h2")
};
function setTermHeight(term, headMetres) {
const worldHeight = Math.max(headMetres * HEAD_SCALE, 0.001);
term.fill.scale.y = worldHeight;
term.fill.position.y = DATUM_Y + worldHeight / 2;
term.cap.position.y = DATUM_Y + worldHeight + 0.018;
term.value.position.set(
term.fill.position.x,
term.cap.position.y + 0.145,
term.z + 0.02
);
term.value.userData.setText(`${headMetres.toFixed(2)} m`);
}
function updateModel() {
const Q = Number(flowInput.value);
// Steady incompressible continuity.
const velocity1 = Q / A1;
const velocity2 = Q / A2;
const velocityHead1 = velocity1 * velocity1 / (2 * GRAVITY);
const velocityHead2 = velocity2 * velocity2 / (2 * GRAVITY);
// For the stated lossless model:
// p/(rho g) + v²/(2g) = H - z.
const pressureVelocityBudget = TOTAL_HEAD - ELEVATION;
const pressureHead1 = pressureVelocityBudget - velocityHead1;
const pressureHead2 = pressureVelocityBudget - velocityHead2;
const total1 = ELEVATION + pressureHead1 + velocityHead1;
const total2 = ELEVATION + pressureHead2 + velocityHead2;
setTermHeight(standData[0].terms.elevation, ELEVATION);
setTermHeight(standData[1].terms.elevation, ELEVATION);
setTermHeight(standData[0].terms.pressure, pressureHead1);
setTermHeight(standData[1].terms.pressure, pressureHead2);
setTermHeight(standData[0].terms.velocity, velocityHead1);
setTermHeight(standData[1].terms.velocity, velocityHead2);
flowReadout.textContent = `${Q.toFixed(2)} m³/s`;
flowSprite.userData.setText(`FLOW Q = ${Q.toFixed(2)} m³/s`);
dom.z1.textContent = ELEVATION.toFixed(2);
dom.z2.textContent = ELEVATION.toFixed(2);
dom.p1.textContent = pressureHead1.toFixed(2);
dom.p2.textContent = pressureHead2.toFixed(2);
dom.v1h.textContent = velocityHead1.toFixed(2);
dom.v2h.textContent = velocityHead2.toFixed(2);
dom.v1.textContent = velocity1.toFixed(2);
dom.v2.textContent = velocity2.toFixed(2);
dom.h1.textContent = total1.toFixed(2);
dom.h2.textContent = total2.toFixed(2);
}
flowInput.addEventListener("input", updateModel);
updateModel();
// -------------------------------------------------------------------------
// Direct camera controls: no OrbitControls or other addons are used.
// -------------------------------------------------------------------------
const target = new THREE.Vector3(0, 1.20, 0);
const defaultCamera = {
theta: 0.49,
phi: 1.045,
radius: 12.25
};
const cameraState = { ...defaultCamera };
let dragging = false;
let previousX = 0;
let previousY = 0;
function updateCamera() {
const sinPhi = Math.sin(cameraState.phi);
camera.position.set(
target.x + cameraState.radius * sinPhi * Math.sin(cameraState.theta),
target.y + cameraState.radius * Math.cos(cameraState.phi),
target.z + cameraState.radius * sinPhi * Math.cos(cameraState.theta)
);
camera.lookAt(target);
}
function resetCamera() {
Object.assign(cameraState, defaultCamera);
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 dx = event.clientX - previousX;
const dy = event.clientY - previousY;
previousX = event.clientX;
previousY = event.clientY;
cameraState.theta -= dx * 0.006;
cameraState.phi = THREE.MathUtils.clamp(
cameraState.phi - dy * 0.005,
0.36,
1.43
);
updateCamera();
});
function stopDragging(event) {
dragging = false;
if (event.pointerId !== undefined &&
renderer.domElement.hasPointerCapture(event.pointerId)) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
}
renderer.domElement.addEventListener("pointerup", stopDragging);
renderer.domElement.addEventListener("pointercancel", stopDragging);
renderer.domElement.addEventListener("wheel", (event) => {
event.preventDefault();
cameraState.radius = THREE.MathUtils.clamp(
cameraState.radius * Math.exp(event.deltaY * 0.001),
7.2,
19
);
updateCamera();
}, { passive: false });
renderer.domElement.addEventListener("dblclick", resetCamera);
document.querySelector("#reset-camera").addEventListener("click", resetCamera);
updateCamera();
// -------------------------------------------------------------------------
// Responsive sizing and render loop.
// -------------------------------------------------------------------------
function resize() {
const width = Math.max(1, viewport.clientWidth);
const height = Math.max(1, viewport.clientHeight);
renderer.setSize(width, height, false);
camera.aspect = width / height;
camera.updateProjectionMatrix();
}
const resizeObserver = new ResizeObserver(resize);
resizeObserver.observe(viewport);
resize();
renderer.setAnimationLoop(() => {
renderer.render(scene, camera);
});
</script>
</body>
</html>
```