Prompt specification
Build or customize the Three.js 3D scene "Venturi assumption explorer" 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: Venturi assumption explorer
Category: Science & Physics · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/venturi-assumption-explorer-x1516
Scene Description Prompt:
Adjust a low-poly Venturi tube's constriction and head loss; update flow-energy indicators using stated assumptions. Create standalone HTML with Three.js from cdnjs.
Working Three.js Starter (venturi-assumption-explorer-x1516):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Interactive Venturi Tube</title>
<style>
:root {
color-scheme: dark;
--bg: #06111d;
--panel: rgba(9, 24, 39, 0.9);
--panel-border: rgba(133, 211, 255, 0.18);
--text: #e8f6ff;
--muted: #91aabd;
--cyan: #48d7ff;
--cyan-soft: rgba(72, 215, 255, 0.16);
--orange: #ff9854;
--green: #68e6ad;
--shadow: 0 18px 60px rgba(0, 0, 0, 0.35);
}
* {
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;
background:
radial-gradient(circle at 28% 20%, rgba(24, 89, 126, 0.3), transparent 34%),
radial-gradient(circle at 80% 78%, rgba(16, 74, 96, 0.22), transparent 36%),
var(--bg);
color: var(--text);
}
#scene {
position: fixed;
inset: 0;
}
#scene canvas {
display: block;
width: 100%;
height: 100%;
cursor: grab;
touch-action: none;
}
#scene canvas:active {
cursor: grabbing;
}
.brand {
position: fixed;
top: 24px;
left: 28px;
z-index: 4;
max-width: min(520px, calc(100vw - 440px));
pointer-events: none;
text-shadow: 0 2px 18px rgba(0, 0, 0, 0.65);
}
.eyebrow {
color: var(--cyan);
font-size: 0.72rem;
font-weight: 750;
letter-spacing: 0.16em;
text-transform: uppercase;
}
h1 {
margin: 5px 0 7px;
font-size: clamp(1.55rem, 3vw, 2.55rem);
line-height: 1.05;
letter-spacing: -0.035em;
}
.brand p {
margin: 0;
max-width: 470px;
color: var(--muted);
font-size: 0.9rem;
line-height: 1.5;
}
.panel {
position: fixed;
z-index: 5;
top: 18px;
right: 18px;
width: 390px;
max-height: calc(100vh - 36px);
overflow: auto;
padding: 18px;
border: 1px solid var(--panel-border);
border-radius: 20px;
background: var(--panel);
box-shadow: var(--shadow);
backdrop-filter: blur(18px) saturate(130%);
scrollbar-width: thin;
scrollbar-color: rgba(114, 191, 231, 0.35) transparent;
}
.panel-header {
display: flex;
align-items: flex-start;
justify-content: space-between;
gap: 12px;
margin-bottom: 14px;
}
.panel-header h2 {
margin: 0;
font-size: 1rem;
letter-spacing: -0.01em;
}
.panel-header p {
margin: 4px 0 0;
color: var(--muted);
font-size: 0.75rem;
line-height: 1.4;
}
.live {
display: inline-flex;
align-items: center;
gap: 6px;
flex: none;
padding: 5px 8px;
border: 1px solid rgba(104, 230, 173, 0.25);
border-radius: 999px;
color: var(--green);
background: rgba(104, 230, 173, 0.08);
font-size: 0.65rem;
font-weight: 750;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.live::before {
width: 6px;
height: 6px;
border-radius: 50%;
background: currentColor;
box-shadow: 0 0 10px currentColor;
content: "";
}
.control {
margin: 0 0 13px;
}
.control-row {
display: flex;
align-items: baseline;
justify-content: space-between;
gap: 12px;
margin-bottom: 7px;
}
.control label {
font-size: 0.79rem;
font-weight: 680;
}
.control output {
color: var(--cyan);
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
font-size: 0.78rem;
font-weight: 700;
}
.control small {
display: block;
margin-top: 5px;
color: var(--muted);
font-size: 0.69rem;
line-height: 1.35;
}
input[type="range"] {
--pct: 50%;
width: 100%;
height: 5px;
margin: 4px 0;
border-radius: 999px;
outline: none;
background:
linear-gradient(90deg, var(--cyan) 0 var(--pct), rgba(128, 166, 190, 0.22) var(--pct) 100%);
appearance: none;
cursor: pointer;
}
input[type="range"]::-webkit-slider-thumb {
width: 16px;
height: 16px;
border: 2px solid #dff8ff;
border-radius: 50%;
background: var(--cyan);
box-shadow: 0 0 0 4px rgba(72, 215, 255, 0.13);
appearance: none;
}
input[type="range"]::-moz-range-thumb {
width: 14px;
height: 14px;
border: 2px solid #dff8ff;
border-radius: 50%;
background: var(--cyan);
box-shadow: 0 0 0 4px rgba(72, 215, 255, 0.13);
}
.divider {
height: 1px;
margin: 15px 0;
background: rgba(133, 211, 255, 0.11);
}
.metrics {
display: grid;
grid-template-columns: repeat(2, minmax(0, 1fr));
gap: 8px;
}
.metric {
min-height: 76px;
padding: 10px;
border: 1px solid rgba(133, 211, 255, 0.11);
border-radius: 12px;
background: rgba(122, 201, 242, 0.045);
}
.metric-label {
display: block;
margin-bottom: 6px;
color: var(--muted);
font-size: 0.68rem;
line-height: 1.2;
}
.metric-value {
display: block;
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
font-size: 1.02rem;
font-weight: 760;
letter-spacing: -0.04em;
}
.metric-unit {
margin-left: 3px;
color: var(--muted);
font-size: 0.65rem;
font-weight: 500;
}
.metric-note {
display: block;
margin-top: 4px;
color: var(--muted);
font-size: 0.64rem;
}
.section-title {
display: flex;
justify-content: space-between;
align-items: baseline;
margin-bottom: 8px;
font-size: 0.78rem;
font-weight: 720;
}
.section-title span {
color: var(--muted);
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
font-size: 0.68rem;
font-weight: 500;
}
.energy-bar {
display: flex;
width: 100%;
height: 13px;
overflow: hidden;
border: 1px solid rgba(255, 255, 255, 0.08);
border-radius: 999px;
background: rgba(255, 255, 255, 0.05);
}
.energy-segment {
min-width: 0;
transition: width 120ms ease;
}
#kineticSegment {
background: linear-gradient(90deg, #248fc1, var(--cyan));
}
#lossSegment {
background: linear-gradient(90deg, #ff7048, var(--orange));
}
.energy-legend {
display: flex;
justify-content: space-between;
gap: 12px;
margin-top: 7px;
color: var(--muted);
font-size: 0.67rem;
}
.legend-item {
display: flex;
align-items: center;
gap: 6px;
}
.swatch {
width: 7px;
height: 7px;
border-radius: 2px;
}
.swatch.cyan {
background: var(--cyan);
}
.swatch.orange {
background: var(--orange);
}
.assumption-summary {
margin: 0 0 7px;
color: var(--muted);
font-size: 0.69rem;
line-height: 1.45;
}
details {
border: 1px solid rgba(133, 211, 255, 0.11);
border-radius: 12px;
background: rgba(0, 0, 0, 0.1);
}
summary {
padding: 9px 11px;
color: #cfe9f7;
font-size: 0.72rem;
font-weight: 700;
cursor: pointer;
user-select: none;
}
.assumption-body {
padding: 0 11px 10px;
color: var(--muted);
font-size: 0.68rem;
line-height: 1.5;
}
.assumption-body ul {
margin: 0;
padding-left: 17px;
}
.assumption-body li + li {
margin-top: 5px;
}
.formula {
display: block;
margin-top: 7px;
padding: 7px 8px;
overflow-x: auto;
border-radius: 7px;
color: #bdeaff;
background: rgba(72, 215, 255, 0.07);
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
font-size: 0.64rem;
white-space: nowrap;
}
.panel-footer {
display: flex;
align-items: center;
justify-content: space-between;
gap: 10px;
margin-top: 13px;
}
.check {
display: flex;
align-items: center;
gap: 7px;
color: var(--muted);
font-size: 0.7rem;
cursor: pointer;
}
.check input {
width: 14px;
height: 14px;
accent-color: var(--cyan);
}
button {
border: 1px solid rgba(72, 215, 255, 0.25);
border-radius: 9px;
padding: 7px 10px;
color: #dff8ff;
background: rgba(72, 215, 255, 0.09);
font: inherit;
font-size: 0.7rem;
font-weight: 680;
cursor: pointer;
}
button:hover {
background: rgba(72, 215, 255, 0.16);
}
#labelLayer {
position: fixed;
inset: 0;
z-index: 3;
overflow: hidden;
pointer-events: none;
}
.world-label {
position: absolute;
padding: 5px 8px;
border: 1px solid rgba(133, 211, 255, 0.22);
border-radius: 8px;
opacity: 0;
color: #dff8ff;
background: rgba(6, 19, 31, 0.78);
box-shadow: 0 5px 18px rgba(0, 0, 0, 0.25);
backdrop-filter: blur(8px);
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
font-size: 0.65rem;
white-space: nowrap;
transform: translate(-50%, -115%);
transition: opacity 120ms ease;
}
.world-label.loss {
border-color: rgba(255, 152, 84, 0.35);
color: #ffd0b5;
}
.camera-hint {
position: fixed;
z-index: 3;
bottom: 19px;
left: 28px;
display: flex;
align-items: center;
gap: 9px;
color: var(--muted);
font-size: 0.7rem;
pointer-events: none;
}
.camera-hint span {
display: inline-flex;
align-items: center;
gap: 6px;
padding: 6px 9px;
border: 1px solid rgba(133, 211, 255, 0.12);
border-radius: 999px;
background: rgba(7, 19, 30, 0.58);
}
@media (max-width: 760px) {
.brand {
top: 15px;
left: 16px;
max-width: calc(100vw - 32px);
}
.brand p {
display: none;
}
.panel {
top: auto;
right: 10px;
bottom: 10px;
left: 10px;
width: auto;
max-height: 53vh;
padding: 14px;
border-radius: 16px;
}
.camera-hint {
display: none;
}
.world-label {
font-size: 0.58rem;
}
}
</style>
</head>
<body>
<div id="scene" aria-label="Interactive three-dimensional Venturi tube"></div>
<header class="brand">
<div class="eyebrow">Fluid Systems Lab</div>
<h1>Interactive Venturi Tube</h1>
<p>
Adjust the throat diameter and localized loss coefficient to see how the
inferred discharge, velocity, pressure, and energy loss respond.
</p>
</header>
<div id="labelLayer">
<div id="upstreamLabel" class="world-label"></div>
<div id="throatLabel" class="world-label"></div>
<div id="lossLabel" class="world-label loss"></div>
</div>
<aside class="panel" aria-label="Venturi controls and results">
<div class="panel-header">
<div>
<h2>Tube & flow model</h2>
<p>Horizontal, water-filled Venturi meter</p>
</div>
<span class="live">Live</span>
</div>
<div class="control">
<div class="control-row">
<label for="beta">Throat diameter ratio, β = D₂/D₁</label>
<output id="betaOutput" for="beta"></output>
</div>
<input id="beta" type="range" min="0.25" max="0.80" step="0.01" value="0.42">
<small>Upstream diameter is fixed at D₁ = 300 mm.</small>
</div>
<div class="control">
<div class="control-row">
<label for="lossK">Throat head-loss coefficient, K</label>
<output id="lossOutput" for="lossK"></output>
</div>
<input id="lossK" type="range" min="0" max="0.20" step="0.005" value="0.06">
<small>Loss is modeled as hL = K·v₂²/(2g), based on throat velocity.</small>
</div>
<div class="control">
<div class="control-row">
<label for="head">Driving pressure head, H</label>
<output id="headOutput" for="head"></output>
</div>
<input id="head" type="range" min="0.25" max="3.00" step="0.05" value="1.20">
<small>Measured pressure-head difference from the upstream section to the throat.</small>
</div>
<div class="divider"></div>
<div class="metrics" aria-live="polite">
<div class="metric">
<span class="metric-label">Discharge, Q</span>
<span class="metric-value" id="qValue">—</span>
<span class="metric-note" id="qNote"></span>
</div>
<div class="metric">
<span class="metric-label">Upstream velocity, v₁</span>
<span class="metric-value" id="v1Value">—</span>
<span class="metric-note">Based on D₁ = 0.300 m</span>
</div>
<div class="metric">
<span class="metric-label">Throat velocity, v₂</span>
<span class="metric-value" id="v2Value">—</span>
<span class="metric-note" id="d2Note"></span>
</div>
<div class="metric">
<span class="metric-label">Head loss, hL</span>
<span class="metric-value" id="lossValue">—</span>
<span class="metric-note" id="lossShare"></span>
</div>
<div class="metric">
<span class="metric-label">Throat gauge pressure, p₂</span>
<span class="metric-value" id="pressureValue">—</span>
<span class="metric-note" id="pressureNote"></span>
</div>
<div class="metric">
<span class="metric-label">Dissipated hydraulic power</span>
<span class="metric-value" id="powerValue">—</span>
<span class="metric-note" id="downstreamNote"></span>
</div>
</div>
<div class="divider"></div>
<div class="section-title">
<strong>Available-head energy balance</strong>
<span id="energyTotal"></span>
</div>
<div class="energy-bar" role="img" aria-label="Kinetic-head increase and head-loss portions">
<div id="kineticSegment" class="energy-segment"></div>
<div id="lossSegment" class="energy-segment"></div>
</div>
<div class="energy-legend">
<span class="legend-item">
<i class="swatch cyan"></i>
Velocity-head increase <strong id="kineticValue"></strong>
</span>
<span class="legend-item">
<i class="swatch orange"></i>
Dissipation <strong id="lossEnergyValue"></strong>
</span>
</div>
<div class="divider"></div>
<p class="assumption-summary">
Water · horizontal centerline · ρ = 1000 kg/m³ · g = 9.81 m/s² ·
losses represented only at the throat.
</p>
<details open>
<summary>Model assumptions and equations</summary>
<div class="assumption-body">
<ul>
<li>
Water is treated as incompressible with constant density. The upstream
diameter is 0.300 m and the throat diameter is D₂ = βD₁.
</li>
<li>
Section elevations are equal. The input H is the measured pressure-head
difference between the upstream section and throat.
</li>
<li>
A single localized throat loss is assumed:
hL = Kv₂²/(2g). Entrance, friction, diffusion, and contraction losses
are not modeled separately.
</li>
<li>
Continuity gives v₁ = β²v₂. The energy equation gives:
H = (v₂² − v₁²)/(2g) + hL.
</li>
<li>
A fixed upstream gauge pressure of 120 kPa is used only for the
pressure readouts. It does not determine discharge.
</li>
<li>
This is a one-dimensional engineering estimate, not a CFD result.
Real K can vary with Reynolds number, roughness, and geometry.
</li>
</ul>
<span class="formula">
v₂ = √[2gH / (K + 1 − β⁴)], Q = A₂v₂
</span>
</div>
</details>
<div class="panel-footer">
<label class="check">
<input id="flowToggle" type="checkbox" checked>
Animate flow particles
</label>
<button id="resetButton" type="button">Reset model</button>
</div>
</aside>
<div class="camera-hint">
<span>Drag to orbit</span>
<span>Scroll / pinch to zoom</span>
<span>Double-click to reset camera</span>
</div>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const sceneHost = document.getElementById("scene");
// -------------------------------------------------------------------------
// Scene
// -------------------------------------------------------------------------
const scene = new THREE.Scene();
scene.fog = new THREE.FogExp2(0x07111d, 0.026);
const camera = new THREE.PerspectiveCamera(42, 1, 0.1, 100);
const renderer = new THREE.WebGLRenderer({
antialias: true,
alpha: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setClearColor(0x000000, 0);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
sceneHost.appendChild(renderer.domElement);
const hemisphere = new THREE.HemisphereLight(0xbceeff, 0x08111d, 2.1);
scene.add(hemisphere);
const keyLight = new THREE.DirectionalLight(0xe9f7ff, 3.2);
keyLight.position.set(4, 7, 5);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(1024, 1024);
keyLight.shadow.camera.left = -7;
keyLight.shadow.camera.right = 7;
keyLight.shadow.camera.top = 6;
keyLight.shadow.camera.bottom = -6;
scene.add(keyLight);
const rimLight = new THREE.PointLight(0x2dc9ff, 32, 16, 2);
rimLight.position.set(-4, 1.5, -3);
scene.add(rimLight);
const warmLight = new THREE.PointLight(0xff8a50, 16, 10, 2);
warmLight.position.set(2, 1.4, 2.5);
scene.add(warmLight);
const floor = new THREE.Mesh(
new THREE.PlaneGeometry(28, 18),
new THREE.MeshStandardMaterial({
color: 0x071522,
roughness: 0.96,
metalness: 0.05,
transparent: true,
opacity: 0.72
})
);
floor.rotation.x = -Math.PI / 2;
floor.position.y = -1.26;
floor.receiveShadow = true;
scene.add(floor);
const grid = new THREE.GridHelper(24, 24, 0x28516a, 0x132c3d);
grid.position.y = -1.245;
grid.material.transparent = true;
grid.material.opacity = 0.45;
scene.add(grid);
// -------------------------------------------------------------------------
// Fixed model constants
// -------------------------------------------------------------------------
const RHO = 1000;
const G = 9.81;
const D1 = 0.300;
const A1 = Math.PI * D1 * D1 / 4;
const P1_GAUGE = 120000;
const VISUAL_UPSTREAM_RADIUS = 0.62;
const VISUAL_WALL = 0.035;
const VISUAL_FLOW_INSET = 0.055;
const RADIAL_SEGMENTS = 12;
const PARTICLE_COUNT = 170;
const betaInput = document.getElementById("beta");
const lossInput = document.getElementById("lossK");
const headInput = document.getElementById("head");
const flowToggle = document.getElementById("flowToggle");
let currentProfile = [];
let venturiGroup = null;
let lossRing = null;
let flowPoints = null;
let particleData = [];
// -------------------------------------------------------------------------
// Custom orbit camera controls — no external control add-ons
// -------------------------------------------------------------------------
const orbit = {
theta: 0.68,
phi: 1.03,
radius: 9.4,
target: new THREE.Vector3(0, -0.03, 0)
};
const pointers = new Map();
let previousPinchDistance = 0;
function updateCamera() {
const sinPhi = Math.sin(orbit.phi);
camera.position.set(
orbit.radius * sinPhi * Math.cos(orbit.theta),
orbit.radius * Math.cos(orbit.phi),
orbit.radius * sinPhi * Math.sin(orbit.theta)
);
camera.lookAt(orbit.target);
}
function resetCamera() {
orbit.theta = 0.68;
orbit.phi = 1.03;
orbit.radius = 9.4;
updateCamera();
}
function currentPointerDistance() {
const values = [...pointers.values()];
if (values.length < 2) return 0;
return Math.hypot(
values[0].x - values[1].x,
values[0].y - values[1].y
);
}
renderer.domElement.addEventListener("pointerdown", (event) => {
renderer.domElement.setPointerCapture(event.pointerId);
pointers.set(event.pointerId, { x: event.clientX, y: event.clientY });
if (pointers.size === 2) {
previousPinchDistance = currentPointerDistance();
}
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!pointers.has(event.pointerId)) return;
const previous = pointers.get(event.pointerId);
pointers.set(event.pointerId, { x: event.clientX, y: event.clientY });
if (pointers.size === 1) {
const dx = event.clientX - previous.x;
const dy = event.clientY - previous.y;
orbit.theta -= dx * 0.006;
orbit.phi = THREE.MathUtils.clamp(orbit.phi - dy * 0.006, 0.28, 2.65);
} else if (pointers.size === 2) {
const distance = currentPointerDistance();
if (previousPinchDistance > 0 && distance > 0) {
orbit.radius = THREE.MathUtils.clamp(
orbit.radius * (previousPinchDistance / distance),
5.5,
16
);
}
previousPinchDistance = distance;
}
updateCamera();
});
function removePointer(event) {
pointers.delete(event.pointerId);
previousPinchDistance = pointers.size === 2 ? currentPointerDistance() : 0;
}
renderer.domElement.addEventListener("pointerup", removePointer);
renderer.domElement.addEventListener("pointercancel", removePointer);
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
orbit.radius = THREE.MathUtils.clamp(
orbit.radius * Math.exp(event.deltaY * 0.001),
5.5,
16
);
updateCamera();
},
{ passive: false }
);
renderer.domElement.addEventListener("dblclick", resetCamera);
updateCamera();
// -------------------------------------------------------------------------
// Low-poly Venturi geometry
// -------------------------------------------------------------------------
function makeProfile(beta) {
const upstream = VISUAL_UPSTREAM_RADIUS;
const throat = upstream * beta;
return [
{ x: -3.00, r: upstream },
{ x: -2.40, r: upstream },
{ x: -1.65, r: upstream },
{ x: -1.15, r: THREE.MathUtils.lerp(upstream, throat, 0.55) },
{ x: -0.72, r: throat },
{ x: -0.25, r: throat },
{ x: 0.25, r: throat },
{ x: 0.72, r: throat },
{ x: 1.15, r: THREE.MathUtils.lerp(throat, upstream, 0.55) },
{ x: 1.65, r: upstream },
{ x: 2.40, r: upstream },
{ x: 3.00, r: upstream }
];
}
function radiusAt(x, profile = currentProfile) {
if (x <= profile[0].x) return profile[0].r;
if (x >= profile[profile.length - 1].x) return profile[profile.length - 1].r;
for (let i = 0; i < profile.length - 1; i++) {
const a = profile[i];
const b = profile[i + 1];
if (x >= a.x && x <= b.x) {
const t = (x - a.x) / (b.x - a.x);
return THREE.MathUtils.lerp(a.r, b.r, t);
}
}
return VISUAL_UPSTREAM_RADIUS;
}
function createSurfaceGeometry(profile, inset = 0, reverse = false) {
const vertices = [];
const indices = [];
for (const point of profile) {
const radius = Math.max(0.012, point.r - inset);
for (let segment = 0; segment < RADIAL_SEGMENTS; segment++) {
const angle = segment / RADIAL_SEGMENTS * Math.PI * 2;
vertices.push(
point.x,
Math.cos(angle) * radius,
Math.sin(angle) * radius
);
}
}
for (let ring = 0; ring < profile.length - 1; ring++) {
for (let segment = 0; segment < RADIAL_SEGMENTS; segment++) {
const nextSegment = (segment + 1) % RADIAL_SEGMENTS;
const a = ring * RADIAL_SEGMENTS + segment;
const b = ring * RADIAL_SEGMENTS + nextSegment;
const c = (ring + 1) * RADIAL_SEGMENTS + nextSegment;
const d = (ring + 1) * RADIAL_SEGMENTS + segment;
if (reverse) {
indices.push(a, d, b, b, d, c);
} else {
indices.push(a, b, d, b, c, d);
}
}
}
const geometry = new THREE.BufferGeometry();
geometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(vertices, 3)
);
geometry.setIndex(indices);
geometry.computeVertexNormals();
return geometry;
}
function disposeGroup(group) {
if (!group) return;
const geometries = new Set();
const materials = new Set();
group.traverse((object) => {
if (object.geometry) geometries.add(object.geometry);
if (object.material) {
if (Array.isArray(object.material)) {
object.material.forEach((material) => materials.add(material));
} else {
materials.add(object.material);
}
}
});
geometries.forEach((geometry) => geometry.dispose());
materials.forEach((material) => material.dispose());
scene.remove(group);
}
function createTap(x, radius, color) {
const group = new THREE.Group();
const stem = new THREE.Mesh(
new THREE.CylinderGeometry(0.018, 0.018, 0.34, 8),
new THREE.MeshStandardMaterial({
color,
emissive: color,
emissiveIntensity: 0.25,
roughness: 0.45,
metalness: 0.35
})
);
stem.position.set(x, radius + 0.17, 0);
group.add(stem);
const cap = new THREE.Mesh(
new THREE.SphereGeometry(0.038, 8, 6),
new THREE.MeshStandardMaterial({
color,
emissive: color,
emissiveIntensity: 0.4,
roughness: 0.35
})
);
cap.position.set(x, radius + 0.35, 0);
group.add(cap);
return group;
}
function createParticles(profile) {
const positions = new Float32Array(PARTICLE_COUNT * 3);
particleData = [];
for (let i = 0; i < PARTICLE_COUNT; i++) {
const item = {
x: THREE.MathUtils.randFloat(-2.92, 2.92),
angle: Math.random() * Math.PI * 2,
radial: Math.sqrt(Math.random()) * 0.78
};
particleData.push(item);
const radius = Math.max(
0.015,
radiusAt(item.x, profile) - VISUAL_FLOW_INSET
);
positions[i * 3] = item.x;
positions[i * 3 + 1] = Math.cos(item.angle) * radius * item.radial;
positions[i * 3 + 2] = Math.sin(item.angle) * radius * item.radial;
}
const geometry = new THREE.BufferGeometry();
const attribute = new THREE.BufferAttribute(positions, 3);
attribute.setUsage(THREE.DynamicDrawUsage);
geometry.setAttribute("position", attribute);
const material = new THREE.PointsMaterial({
color: 0x9beeff,
size: 0.055,
sizeAttenuation: true,
transparent: true,
opacity: 0.95,
depthWrite: false,
blending: THREE.AdditiveBlending
});
const points = new THREE.Points(geometry, material);
points.renderOrder = 4;
return points;
}
function rebuildVenturi(beta) {
disposeGroup(venturiGroup);
currentProfile = makeProfile(beta);
venturiGroup = new THREE.Group();
const outerMaterial = new THREE.MeshStandardMaterial({
color: 0x3c88aa,
emissive: 0x082737,
emissiveIntensity: 0.45,
metalness: 0.38,
roughness: 0.4,
transparent: true,
opacity: 0.62,
side: THREE.DoubleSide,
flatShading: true
});
const innerMaterial = new THREE.MeshStandardMaterial({
color: 0x0d3851,
metalness: 0.18,
roughness: 0.68,
transparent: true,
opacity: 0.26,
side: THREE.DoubleSide,
flatShading: true
});
const fluidMaterial = new THREE.MeshBasicMaterial({
color: 0x31cfff,
transparent: true,
opacity: 0.105,
side: THREE.DoubleSide,
depthWrite: false
});
const outerMesh = new THREE.Mesh(
createSurfaceGeometry(currentProfile, 0),
outerMaterial
);
outerMesh.renderOrder = 3;
venturiGroup.add(outerMesh);
const innerMesh = new THREE.Mesh(
createSurfaceGeometry(currentProfile, VISUAL_WALL, true),
innerMaterial
);
innerMesh.renderOrder = 2;
venturiGroup.add(innerMesh);
const fluidMesh = new THREE.Mesh(
createSurfaceGeometry(currentProfile, VISUAL_FLOW_INSET),
fluidMaterial
);
fluidMesh.renderOrder = 1;
venturiGroup.add(fluidMesh);
const wireMaterial = new THREE.LineBasicMaterial({
color: 0x9ae8ff,
transparent: true,
opacity: 0.19,
depthWrite: false
});
const wire = new THREE.LineSegments(
new THREE.EdgesGeometry(outerMesh.geometry, 10),
wireMaterial
);
wire.renderOrder = 5;
venturiGroup.add(wire);
const flangeMaterial = new THREE.MeshStandardMaterial({
color: 0x87c9df,
metalness: 0.65,
roughness: 0.32,
flatShading: true,
side: THREE.DoubleSide
});
for (const x of [-3.01, 3.01]) {
const flange = new THREE.Mesh(
new THREE.RingGeometry(
VISUAL_UPSTREAM_RADIUS - VISUAL_WALL,
VISUAL_UPSTREAM_RADIUS + 0.035,
RADIAL_SEGMENTS
),
flangeMaterial
);
flange.rotation.y = Math.PI / 2;
flange.position.x = x;
venturiGroup.add(flange);
}
const throatRadius = VISUAL_UPSTREAM_RADIUS * beta;
lossRing = new THREE.Mesh(
new THREE.TorusGeometry(throatRadius + 0.025, 0.024, 6, 28),
new THREE.MeshStandardMaterial({
color: 0xff9854,
emissive: 0xff6a32,
emissiveIntensity: 1.0,
roughness: 0.36,
metalness: 0.28,
transparent: true,
opacity: 0.78
})
);
lossRing.rotation.y = Math.PI / 2;
lossRing.position.x = 0;
lossRing.renderOrder = 6;
venturiGroup.add(lossRing);
venturiGroup.add(
createTap(-1.78, VISUAL_UPSTREAM_RADIUS, 0x55cfff)
);
venturiGroup.add(
createTap(0, throatRadius, 0xffa05f)
);
const flowArrow = new THREE.ArrowHelper(
new THREE.Vector3(1, 0, 0),
new THREE.Vector3(-1.45, 1.13, 0),
2.8,
0x57d9ff,
0.28,
0.15
);
flowArrow.line.material.transparent = true;
flowArrow.line.material.opacity = 0.65;
flowArrow.cone.material.transparent = true;
flowArrow.cone.material.opacity = 0.8;
venturiGroup.add(flowArrow);
flowPoints = createParticles(currentProfile);
venturiGroup.add(flowPoints);
scene.add(venturiGroup);
}
// -------------------------------------------------------------------------
// Engineering model
// -------------------------------------------------------------------------
function calculateModel() {
const beta = Number(betaInput.value);
const K = Number(lossInput.value);
const H = Number(headInput.value);
const D2 = D1 * beta;
const A2 = Math.PI * D2 * D2 / 4;
// v1 = beta^2 v2, and:
// H = [(1 - beta^4) + K] v2^2 / (2g)
const denominator = K + 1 - Math.pow(beta, 4);
const v2 = Math.sqrt((2 * G * H) / denominator);
const v1 = Math.pow(beta, 2) * v2;
const Q = A2 * v2;
const kineticHead = (v2 * v2 - v1 * v1) / (2 * G);
const lossHead = K * v2 * v2 / (2 * G);
const pressureDrop = RHO * G * H;
const p2 = P1_GAUGE - pressureDrop;
// Ideal diffuser recovery: no diffuser loss; remaining loss is hL.
const p3 = P1_GAUGE - RHO * G * lossHead;
const dissipatedPower = RHO * G * Q * lossHead;
const kineticFraction = kineticHead / H;
const lossFraction = lossHead / H;
return {
beta,
K,
H,
D2,
A2,
v1,
v2,
Q,
kineticHead,
lossHead,
p2,
p3,
dissipatedPower,
kineticFraction,
lossFraction
};
}
function updateRangeFill(input) {
const fraction =
(Number(input.value) - Number(input.min)) /
(Number(input.max) - Number(input.min));
input.style.setProperty("--pct", `${fraction * 100}%`);
}
function updateInterface() {
const model = calculateModel();
document.getElementById("betaOutput").textContent =
model.beta.toFixed(2);
document.getElementById("lossOutput").textContent =
model.K.toFixed(3);
document.getElementById("headOutput").textContent =
`${model.H.toFixed(2)} m`;
document.getElementById("qValue").innerHTML =
`${model.Q.toFixed(3)}<span class="metric-unit">m³/s</span>`;
document.getElementById("qNote").textContent =
`${(model.Q * 1000).toFixed(1)} L/s`;
document.getElementById("v1Value").innerHTML =
`${model.v1.toFixed(2)}<span class="metric-unit">m/s</span>`;
document.getElementById("v2Value").innerHTML =
`${model.v2.toFixed(2)}<span class="metric-unit">m/s</span>`;
document.getElementById("d2Note").textContent =
`D₂ = ${(model.D2 * 1000).toFixed(0)} mm`;
document.getElementById("lossValue").innerHTML =
`${model.lossHead.toFixed(3)}<span class="metric-unit">m</span>`;
document.getElementById("lossShare").textContent =
`${(model.lossFraction * 100).toFixed(1)}% of available head`;
document.getElementById("pressureValue").innerHTML =
`${(model.p2 / 1000).toFixed(1)}<span class="metric-unit">kPa</span>`;
document.getElementById("pressureNote").textContent =
`Upstream p₁ = 120.0 kPa gauge`;
document.getElementById("powerValue").innerHTML =
`${model.dissipatedPower.toFixed(1)}<span class="metric-unit">W</span>`;
document.getElementById("downstreamNote").textContent =
`Estimated p₃ = ${(model.p3 / 1000).toFixed(1)} kPa gauge`;
document.getElementById("energyTotal").textContent =
`H = ${model.H.toFixed(3)} m`;
document.getElementById("kineticSegment").style.width =
`${model.kineticFraction * 100}%`;
document.getElementById("lossSegment").style.width =
`${model.lossFraction * 100}%`;
document.getElementById("kineticValue").textContent =
`${model.kineticHead.toFixed(3)} m`;
document.getElementById("lossEnergyValue").textContent =
`${model.lossHead.toFixed(3)} m`;
document.getElementById("upstreamLabel").textContent =
`p₁ = 120.0 kPa · v₁ = ${model.v1.toFixed(2)} m/s`;
document.getElementById("throatLabel").textContent =
`p₂ = ${(model.p2 / 1000).toFixed(1)} kPa · v₂ = ${model.v2.toFixed(2)} m/s`;
document.getElementById("lossLabel").textContent =
`hL = ${model.lossHead.toFixed(3)} m · K = ${model.K.toFixed(3)}`;
if (lossRing) {
lossRing.material.color.setHex(
model.K < 0.005 ? 0x5cd8ff : 0xff9854
);
lossRing.material.emissive.setHex(
model.K < 0.005 ? 0x164b61 : 0xff6a32
);
}
[betaInput, lossInput, headInput].forEach(updateRangeFill);
}
// -------------------------------------------------------------------------
// Flow animation and projected labels
// -------------------------------------------------------------------------
function updateParticles(delta) {
if (!flowPoints || !flowToggle.checked) return;
const positions = flowPoints.geometry.attributes.position.array;
for (let i = 0; i < particleData.length; i++) {
const particle = particleData[i];
const radius = Math.max(
0.015,
radiusAt(particle.x) - VISUAL_FLOW_INSET
);
const localSpeedFactor = Math.min(
12,
Math.pow(VISUAL_UPSTREAM_RADIUS / radius, 2)
);
particle.x += delta * 0.25 * localSpeedFactor;
if (particle.x > 2.96) particle.x -= 5.92;
const boundedRadius = Math.max(
0.015,
radiusAt(particle.x) - VISUAL_FLOW_INSET
);
positions[i * 3] = particle.x;
positions[i * 3 + 1] =
Math.cos(particle.angle) * boundedRadius * particle.radial;
positions[i * 3 + 2] =
Math.sin(particle.angle) * boundedRadius * particle.radial;
}
flowPoints.geometry.attributes.position.needsUpdate = true;
}
const projected = new THREE.Vector3();
function positionLabel(element, x, y, z) {
projected.set(x, y, z).project(camera);
const visible =
projected.z > -1 &&
projected.z < 1 &&
projected.x > -1.15 &&
projected.x < 1.15 &&
projected.y > -1.15 &&
projected.y < 1.15;
if (!visible) {
element.style.opacity = "0";
return;
}
const rect = renderer.domElement.getBoundingClientRect();
element.style.left =
`${rect.left + (projected.x * 0.5 + 0.5) * rect.width}px`;
element.style.top =
`${rect.top + (-projected.y * 0.5 + 0.5) * rect.height}px`;
element.style.opacity = "0.95";
}
function updateLabels() {
const throatRadius = VISUAL_UPSTREAM_RADIUS * Number(betaInput.value);
positionLabel(
document.getElementById("upstreamLabel"),
-1.78,
VISUAL_UPSTREAM_RADIUS + 0.43,
0
);
positionLabel(
document.getElementById("throatLabel"),
0,
throatRadius + 0.43,
0
);
positionLabel(
document.getElementById("lossLabel"),
0.38,
throatRadius * 0.55,
0.38
);
}
// -------------------------------------------------------------------------
// UI events
// -------------------------------------------------------------------------
betaInput.addEventListener("input", () => {
rebuildVenturi(Number(betaInput.value));
updateInterface();
});
lossInput.addEventListener("input", updateInterface);
headInput.addEventListener("input", updateInterface);
flowToggle.addEventListener("change", () => {
if (flowPoints) flowPoints.visible = flowToggle.checked;
});
document.getElementById("resetButton").addEventListener("click", () => {
betaInput.value = "0.42";
lossInput.value = "0.06";
headInput.value = "1.20";
rebuildVenturi(Number(betaInput.value));
updateInterface();
resetCamera();
});
// -------------------------------------------------------------------------
// Resize and render loop
// -------------------------------------------------------------------------
function resize() {
const width = sceneHost.clientWidth;
const height = sceneHost.clientHeight;
camera.aspect = width / Math.max(height, 1);
camera.updateProjectionMatrix();
renderer.setSize(width, height, false);
}
window.addEventListener("resize", resize);
resize();
const clock = new THREE.Clock();
let elapsed = 0;
function animate() {
requestAnimationFrame(animate);
const delta = Math.min(clock.getDelta(), 0.05);
elapsed += delta;
updateParticles(delta);
if (lossRing) {
const normalizedLoss = Math.min(1, Number(lossInput.value) / 0.20);
const pulse = 0.5 + 0.5 * Math.sin(elapsed * 2.4);
lossRing.material.opacity =
0.18 + normalizedLoss * (0.45 + pulse * 0.18);
}
updateLabels();
renderer.render(scene, camera);
}
rebuildVenturi(Number(betaInput.value));
updateInterface();
animate();
</script>
</body>
</html>
```