Prompt specification
Build or customize the Three.js 3D scene "Slow and abrupt valve closure 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: Slow and abrupt valve closure comparison
Category: Science & Physics · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/slow-and-abrupt-valve-closure-comparison-x1494
Scene Description Prompt:
Make a stylized Three.js scene in a self-contained HTML file using cdnjs: slow and abrupt valve closure comparison. Arrange identical pipes with different closure durations; illustrate differing pressure-pulse amplitudes using a stated toy relation rather than asserting universal real-world values.
Working Three.js Starter (slow-and-abrupt-valve-closure-comparison-x1494):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Slow vs. Abrupt Valve Closure</title>
<style>
:root {
color-scheme: dark;
--cyan: #67e8f9;
--amber: #ffb45f;
--pink: #ff5fa2;
--panel: rgba(8, 18, 30, 0.78);
--border: rgba(157, 220, 255, 0.18);
--muted: #91a7bb;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #07101b;
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
#scene {
position: fixed;
inset: 0;
}
#scene canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
outline: none;
}
.glass {
background: var(--panel);
border: 1px solid var(--border);
box-shadow: 0 18px 55px rgba(0, 0, 0, 0.28);
backdrop-filter: blur(16px);
-webkit-backdrop-filter: blur(16px);
}
header {
position: fixed;
z-index: 5;
top: 22px;
left: 22px;
max-width: 520px;
padding: 17px 20px 16px;
border-radius: 18px;
pointer-events: none;
}
.eyebrow {
display: flex;
align-items: center;
gap: 9px;
margin-bottom: 8px;
color: var(--cyan);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.18em;
text-transform: uppercase;
}
.eyebrow::before {
width: 7px;
height: 7px;
border-radius: 50%;
background: var(--cyan);
box-shadow: 0 0 14px var(--cyan);
content: "";
}
h1 {
margin: 0;
color: #f1f8ff;
font-size: clamp(22px, 2.5vw, 34px);
font-weight: 780;
letter-spacing: -0.045em;
line-height: 1.05;
}
header p {
margin: 9px 0 0;
color: #a9bdce;
font-size: 13px;
line-height: 1.45;
}
.controls {
position: fixed;
z-index: 7;
top: 22px;
right: 22px;
display: flex;
gap: 8px;
padding: 8px;
border-radius: 15px;
}
button {
appearance: none;
border: 1px solid rgba(174, 224, 255, 0.17);
border-radius: 10px;
padding: 9px 12px;
background: rgba(255, 255, 255, 0.045);
color: #d9edfa;
font: inherit;
font-size: 12px;
font-weight: 650;
cursor: pointer;
transition: background 150ms ease, border-color 150ms ease, transform 150ms ease;
}
button:hover {
border-color: rgba(174, 224, 255, 0.38);
background: rgba(255, 255, 255, 0.1);
transform: translateY(-1px);
}
button:focus-visible {
outline: 2px solid var(--cyan);
outline-offset: 2px;
}
.status {
position: fixed;
z-index: 5;
top: 24px;
left: 50%;
display: flex;
align-items: center;
gap: 9px;
padding: 9px 13px;
border-radius: 999px;
color: #c8dbea;
font-size: 11px;
font-weight: 700;
letter-spacing: 0.06em;
pointer-events: none;
transform: translateX(-50%);
}
.status-dot {
width: 7px;
height: 7px;
border-radius: 50%;
background: var(--cyan);
box-shadow: 0 0 12px var(--cyan);
}
.model-card {
position: fixed;
z-index: 6;
bottom: 22px;
left: 50%;
width: min(650px, calc(100vw - 32px));
padding: 15px 18px 14px;
border-radius: 18px;
pointer-events: none;
transform: translateX(-50%);
}
.model-heading {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
margin-bottom: 7px;
color: var(--pink);
font-size: 10px;
font-weight: 850;
letter-spacing: 0.16em;
text-transform: uppercase;
}
.model-heading span:last-child {
color: var(--muted);
font-size: 9px;
letter-spacing: 0.08em;
}
.equation {
color: #f4f9ff;
font-family: Georgia, "Times New Roman", serif;
font-size: clamp(17px, 2vw, 23px);
line-height: 1.35;
}
.equation sub,
.equation sup {
font-size: 0.62em;
}
.values {
display: flex;
flex-wrap: wrap;
gap: 9px;
margin: 8px 0 9px;
}
.value {
display: inline-flex;
align-items: center;
gap: 7px;
padding: 5px 8px;
border: 1px solid rgba(255, 255, 255, 0.08);
border-radius: 8px;
background: rgba(255, 255, 255, 0.035);
color: #c7d9e8;
font-size: 11px;
}
.value::before {
width: 6px;
height: 6px;
border-radius: 50%;
background: var(--dot);
box-shadow: 0 0 9px var(--dot);
content: "";
}
.disclaimer {
margin: 0;
color: #8299ad;
font-size: 10px;
line-height: 1.45;
}
.hint {
position: fixed;
z-index: 5;
right: 22px;
bottom: 22px;
padding: 9px 12px;
border-radius: 12px;
color: #8097aa;
font-size: 10px;
line-height: 1.45;
pointer-events: none;
}
#loading {
position: fixed;
z-index: 20;
inset: 0;
display: grid;
place-items: center;
background: #07101b;
color: #bcefff;
font-size: 13px;
letter-spacing: 0.12em;
text-transform: uppercase;
}
@media (max-width: 900px) {
header {
top: 14px;
left: 14px;
max-width: calc(100vw - 28px);
padding: 13px 15px;
}
header p {
display: none;
}
.controls {
top: 104px;
right: 14px;
}
.status {
top: 106px;
left: 14px;
transform: none;
}
.model-card {
bottom: 14px;
}
.hint {
display: none;
}
}
@media (max-width: 560px) {
.controls {
left: 14px;
right: auto;
}
.status {
top: 153px;
}
.model-heading span:last-child,
.disclaimer {
display: none;
}
.model-card {
padding: 12px 14px;
}
}
</style>
</head>
<body>
<div id="scene"></div>
<header class="glass">
<div class="eyebrow">Interactive fluid-system visualization</div>
<h1>Slow vs. Abrupt Valve Closure</h1>
<p>
Two identical pipe rigs, one closure duration. Compare the pressure-pulse
shapes on the same arbitrary-unit scale.
</p>
</header>
<div class="controls glass" aria-label="Scene controls">
<button id="pauseButton" type="button">Pause</button>
<button id="restartButton" type="button">Restart</button>
<button id="cameraButton" type="button">Reset view</button>
</div>
<div class="status glass" aria-live="polite">
<span class="status-dot"></span>
<span id="statusText">Valve open</span>
</div>
<section class="model-card glass" aria-label="Toy pressure relationship">
<div class="model-heading">
<span>Stated toy relationship</span>
<span>Arbitrary visual units · shared scale</span>
</div>
<div class="equation">
Δp<sub>toy</sub> =
50 a.u. · (1.0 s / T<sub>close</sub>)<sup>1/2</sup>
</div>
<div class="values">
<span class="value" style="--dot: var(--cyan)">
Slow, 3.20 s → 28.0 a.u.
</span>
<span class="value" style="--dot: var(--amber)">
Abrupt, 0.25 s → 100.0 a.u.
</span>
</div>
<p class="disclaimer">
The constant, exponent, and “a.u.” are presentation choices for this toy
model—not a prediction of real pressure. Actual amplitudes depend on fluid,
geometry, operating conditions, and boundary conditions.
</p>
</section>
<div class="hint glass">
Drag to orbit · scroll or pinch to zoom<br>
Pressure traces share a 0–110 a.u. scale.
</div>
<div id="loading">Loading Three.js scene…</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// ---------------------------------------------------------------------
// Configuration and illustrative toy model
// ---------------------------------------------------------------------
const TOY = Object.freeze({
constant: 50,
referenceTime: 1.0,
exponent: 0.5,
maxPressure: 110
});
const CYCLE = 7.5;
const CLOSURE_START = 1.0;
const REOPEN_START = 6.55;
const REOPEN_DURATION = 0.75;
const PIPE_Y = 0.38;
const VALVE_X = 0.95;
const SLOW_DURATION = 3.2;
const ABRUPT_DURATION = 0.25;
const slowAmplitude =
TOY.constant *
Math.pow(TOY.referenceTime / SLOW_DURATION, TOY.exponent);
const abruptAmplitude =
TOY.constant *
Math.pow(TOY.referenceTime / ABRUPT_DURATION, TOY.exponent);
const clamp = (v, min, max) => Math.max(min, Math.min(max, v));
const smoothstep = (v) => {
v = clamp(v, 0, 1);
return v * v * (3 - 2 * v);
};
// ---------------------------------------------------------------------
// Renderer, scene, camera, and lights
// ---------------------------------------------------------------------
const container = document.getElementById("scene");
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.1;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
container.appendChild(renderer.domElement);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x07101b);
scene.fog = new THREE.Fog(0x07101b, 18, 40);
const camera = new THREE.PerspectiveCamera(
42,
window.innerWidth / window.innerHeight,
0.1,
100
);
const cameraTarget = new THREE.Vector3(0, -0.12, 0);
let cameraYaw = 0;
let cameraPitch = 0.25;
let cameraDistance = 17.5;
const hemisphere = new THREE.HemisphereLight(0xb9e9ff, 0x07101a, 1.65);
scene.add(hemisphere);
const keyLight = new THREE.DirectionalLight(0xe7f7ff, 2.5);
keyLight.position.set(4, 11, 8);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -14;
keyLight.shadow.camera.right = 14;
keyLight.shadow.camera.top = 10;
keyLight.shadow.camera.bottom = -10;
keyLight.shadow.camera.near = 1;
keyLight.shadow.camera.far = 35;
scene.add(keyLight);
const cyanLight = new THREE.PointLight(0x42dfff, 38, 20, 2);
cyanLight.position.set(-7, 3, 5);
scene.add(cyanLight);
const pinkLight = new THREE.PointLight(0xff4f9a, 32, 18, 2);
pinkLight.position.set(7, 1, 4);
scene.add(pinkLight);
const floor = new THREE.Mesh(
new THREE.PlaneGeometry(60, 40),
new THREE.MeshStandardMaterial({
color: 0x08131f,
roughness: 0.92,
metalness: 0.04
})
);
floor.rotation.x = -Math.PI / 2;
floor.position.y = -1.82;
floor.receiveShadow = true;
scene.add(floor);
const grid = new THREE.GridHelper(36, 36, 0x23506a, 0x153044);
grid.position.y = -1.8;
grid.material.transparent = true;
grid.material.opacity = 0.28;
scene.add(grid);
// Distant particles provide depth without distracting from the rigs.
const starPositions = [];
for (let i = 0; i < 180; i++) {
const angle = i * 2.399963;
const radius = 13 + (i % 17) * 0.65;
starPositions.push(
Math.cos(angle) * radius,
-1 + (i % 13) * 0.42,
-4 - (i % 19) * 0.65
);
}
const starGeometry = new THREE.BufferGeometry();
starGeometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(starPositions, 3)
);
const stars = new THREE.Points(
starGeometry,
new THREE.PointsMaterial({
color: 0x8edcf5,
size: 0.025,
transparent: true,
opacity: 0.45,
depthWrite: false
})
);
scene.add(stars);
// ---------------------------------------------------------------------
// Reusable materials
// ---------------------------------------------------------------------
const pipeShellMaterial = new THREE.MeshPhysicalMaterial({
color: 0xb9f5ff,
transparent: true,
opacity: 0.24,
roughness: 0.16,
metalness: 0.12,
side: THREE.DoubleSide,
depthWrite: false
});
const fluidMaterial = new THREE.MeshStandardMaterial({
color: 0x13a8bd,
emissive: 0x063d4b,
emissiveIntensity: 1.15,
transparent: true,
opacity: 0.48,
roughness: 0.28,
metalness: 0.08,
depthWrite: false
});
const metalMaterial = new THREE.MeshStandardMaterial({
color: 0x526b7e,
roughness: 0.3,
metalness: 0.76
});
const darkMetalMaterial = new THREE.MeshStandardMaterial({
color: 0x152838,
roughness: 0.38,
metalness: 0.68
});
const baseMaterial = new THREE.MeshStandardMaterial({
color: 0x0d2031,
roughness: 0.58,
metalness: 0.38
});
const flowMaterial = new THREE.MeshStandardMaterial({
color: 0xb9fbff,
emissive: 0x35d9ee,
emissiveIntensity: 2.1,
roughness: 0.2
});
const pressureMaterial = new THREE.MeshStandardMaterial({
color: 0xff72ad,
emissive: 0xff267f,
emissiveIntensity: 2.4,
transparent: true,
opacity: 0.9,
depthWrite: false,
blending: THREE.AdditiveBlending
});
// ---------------------------------------------------------------------
// Canvas sprite labels
// ---------------------------------------------------------------------
function roundedRect(ctx, x, y, width, height, radius) {
const r = Math.min(radius, width / 2, height / 2);
ctx.beginPath();
ctx.moveTo(x + r, y);
ctx.lineTo(x + width - r, y);
ctx.quadraticCurveTo(x + width, y, x + width, y + r);
ctx.lineTo(x + width, y + height - r);
ctx.quadraticCurveTo(
x + width,
y + height,
x + width - r,
y + height
);
ctx.lineTo(x + r, y + height);
ctx.quadraticCurveTo(x, y + height, x, y + height - r);
ctx.lineTo(x, y + r);
ctx.quadraticCurveTo(x, y, x + r, y);
ctx.closePath();
}
function makeRigBadge(title, detail, accent) {
const canvas = document.createElement("canvas");
canvas.width = 900;
canvas.height = 220;
const ctx = canvas.getContext("2d");
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.shadowColor = "rgba(0,0,0,0.4)";
ctx.shadowBlur = 26;
ctx.shadowOffsetY = 12;
const gradient = ctx.createLinearGradient(0, 0, canvas.width, 0);
gradient.addColorStop(0, "rgba(12,28,43,0.94)");
gradient.addColorStop(1, "rgba(8,18,30,0.80)");
roundedRect(ctx, 8, 8, 884, 204, 30);
ctx.fillStyle = gradient;
ctx.fill();
ctx.shadowColor = "transparent";
ctx.fillStyle = accent;
roundedRect(ctx, 25, 25, 12, 170, 6);
ctx.fill();
ctx.textBaseline = "alphabetic";
ctx.fillStyle = "#f2f9ff";
ctx.font = "800 62px system-ui, sans-serif";
ctx.fillText(title, 60, 91);
ctx.fillStyle = accent;
ctx.font = "650 31px system-ui, sans-serif";
ctx.fillText(detail, 61, 154);
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
const sprite = new THREE.Sprite(
new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthTest: false,
depthWrite: false
})
);
sprite.scale.set(3.75, 0.92, 1);
sprite.renderOrder = 20;
return sprite;
}
function makeTextSprite(
text,
{
width = 1.4,
height = 0.3,
color = "#b9d3e4",
background = null,
fontSize = 44,
align = "center"
} = {}
) {
const canvas = document.createElement("canvas");
canvas.width = 640;
canvas.height = 128;
const ctx = canvas.getContext("2d");
ctx.clearRect(0, 0, canvas.width, canvas.height);
if (background) {
ctx.fillStyle = background;
roundedRect(ctx, 4, 4, 632, 120, 22);
ctx.fill();
}
ctx.font = `700 ${fontSize}px system-ui, sans-serif`;
ctx.textAlign = align;
ctx.textBaseline = "middle";
ctx.fillStyle = color;
const x = align === "left" ? 28 : canvas.width / 2;
ctx.fillText(text, x, canvas.height / 2 + 2);
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
const sprite = new THREE.Sprite(
new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthTest: false,
depthWrite: false
})
);
sprite.scale.set(width, height, 1);
sprite.renderOrder = 20;
return sprite;
}
// ---------------------------------------------------------------------
// Geometry helpers
// ---------------------------------------------------------------------
function horizontalCylinder(radius, length, material, radialSegments = 48) {
const mesh = new THREE.Mesh(
new THREE.CylinderGeometry(
radius,
radius,
length,
radialSegments,
1,
false
),
material
);
mesh.rotation.z = Math.PI / 2;
return mesh;
}
function torusAtX(x, radius, tube, material) {
const mesh = new THREE.Mesh(
new THREE.TorusGeometry(radius, tube, 12, 56),
material
);
mesh.rotation.y = Math.PI / 2;
mesh.position.x = x;
return mesh;
}
function addEdges(mesh, color = 0x6ca8c5, opacity = 0.22) {
const edges = new THREE.LineSegments(
new THREE.EdgesGeometry(mesh.geometry),
new THREE.LineBasicMaterial({
color,
transparent: true,
opacity
})
);
edges.position.copy(mesh.position);
edges.rotation.copy(mesh.rotation);
edges.scale.copy(mesh.scale);
mesh.parent?.add(edges);
return edges;
}
function createHalo() {
const points = [];
for (let i = 0; i < 96; i++) {
const angle = (i / 96) * Math.PI * 2;
points.push(
new THREE.Vector3(
Math.cos(angle) * 3.05,
0.25 + Math.sin(angle) * 1.85,
-1.55
)
);
}
const halo = new THREE.LineLoop(
new THREE.BufferGeometry().setFromPoints(points),
new THREE.LineBasicMaterial({
color: 0x4ccff2,
transparent: true,
opacity: 0.11
})
);
return halo;
}
// ---------------------------------------------------------------------
// Pressure chart
// ---------------------------------------------------------------------
const CHART = Object.freeze({
samples: 140,
minX: -2.35,
maxX: 2.35,
baseline: -1.28,
height: 0.98,
z: 1.08
});
function createChart() {
const group = new THREE.Group();
const panel = new THREE.Mesh(
new THREE.PlaneGeometry(5.08, 1.4),
new THREE.MeshBasicMaterial({
color: 0x06131e,
transparent: true,
opacity: 0.82,
side: THREE.DoubleSide,
depthWrite: false
})
);
panel.position.set(0, -0.71, CHART.z - 0.03);
panel.renderOrder = 5;
group.add(panel);
const border = new THREE.LineSegments(
new THREE.EdgesGeometry(panel.geometry),
new THREE.LineBasicMaterial({
color: 0x5aa3c4,
transparent: true,
opacity: 0.3,
depthTest: false
})
);
border.position.copy(panel.position);
border.renderOrder = 7;
group.add(border);
const gridVertices = [];
for (let i = 0; i <= 4; i++) {
const ratio = i / 4;
const y = CHART.baseline + ratio * CHART.height;
gridVertices.push(
CHART.minX, y, CHART.z,
CHART.maxX, y, CHART.z
);
}
for (let i = 0; i <= 8; i++) {
const x = THREE.MathUtils.lerp(CHART.minX, CHART.maxX, i / 8);
gridVertices.push(
x, CHART.baseline, CHART.z,
x, CHART.baseline + CHART.height, CHART.z
);
}
const gridLines = new THREE.LineSegments(
new THREE.BufferGeometry().setAttribute(
"position",
new THREE.Float32BufferAttribute(gridVertices, 3)
),
new THREE.LineBasicMaterial({
color: 0x4e829e,
transparent: true,
opacity: 0.17,
depthTest: false
})
);
gridLines.renderOrder = 7;
group.add(gridLines);
const positions = new Float32Array(CHART.samples * 3);
const geometry = new THREE.BufferGeometry();
geometry.setAttribute(
"position",
new THREE.BufferAttribute(positions, 3).setUsage(THREE.DynamicDrawUsage)
);
const trace = new THREE.Line(
geometry,
new THREE.LineBasicMaterial({
color: 0xff64a8,
transparent: true,
opacity: 0.96,
depthTest: false
})
);
trace.frustumCulled = false;
trace.renderOrder = 10;
group.add(trace);
const glow = new THREE.Line(
geometry,
new THREE.LineBasicMaterial({
color: 0xff2d8a,
transparent: true,
opacity: 0.22,
blending: THREE.AdditiveBlending,
depthTest: false
})
);
glow.frustumCulled = false;
glow.renderOrder = 9;
group.add(glow);
const markers = [];
const markerGeometry = new THREE.SphereGeometry(0.035, 10, 8);
for (let i = 0; i < 9; i++) {
const marker = new THREE.Mesh(markerGeometry, pressureMaterial);
marker.renderOrder = 11;
marker.material = marker.material.clone();
group.add(marker);
markers.push(marker);
}
const scaleLabel = makeTextSprite("0–110 a.u. • SAME SCALE", {
width: 2.65,
height: 0.25,
color: "#8ba9bd",
fontSize: 38
});
scaleLabel.position.set(0.18, -0.18, CHART.z + 0.08);
group.add(scaleLabel);
[0, 55, 110].forEach((value) => {
const y = CHART.baseline + (value / TOY.maxPressure) * CHART.height;
const label = makeTextSprite(String(value), {
width: 0.31,
height: 0.2,
color: "#6f8da2",
fontSize: 38
});
label.position.set(-2.57, y, CHART.z + 0.07);
group.add(label);
});
const timeLabel = makeTextSprite("TIME →", {
width: 0.64,
height: 0.18,
color: "#587387",
fontSize: 34
});
timeLabel.position.set(2.08, -1.47, CHART.z + 0.07);
group.add(timeLabel);
return { group, geometry, positions, trace, glow, markers };
}
function createPeakLine(amplitude) {
const y =
CHART.baseline +
(amplitude / TOY.maxPressure) * CHART.height;
const line = new THREE.Line(
new THREE.BufferGeometry().setFromPoints([
new THREE.Vector3(CHART.minX, y, CHART.z + 0.025),
new THREE.Vector3(CHART.maxX, y, CHART.z + 0.025)
]),
new THREE.LineDashedMaterial({
color: 0xffd5e7,
transparent: true,
opacity: 0.3,
dashSize: 0.09,
gapSize: 0.07,
depthTest: false
})
);
line.computeLineDistances();
line.renderOrder = 8;
return line;
}
// ---------------------------------------------------------------------
// Live pressure gauge
// ---------------------------------------------------------------------
function gaugeAngle(pressure) {
const ratio = clamp(pressure / TOY.maxPressure, 0, 1);
return THREE.MathUtils.degToRad(120 - ratio * 240);
}
function gaugeDirection(angle, radius) {
return new THREE.Vector3(
-Math.sin(angle) * radius,
Math.cos(angle) * radius,
0
);
}
function createGauge(peakAmplitude) {
const group = new THREE.Group();
const face = new THREE.Mesh(
new THREE.CircleGeometry(0.39, 48),
new THREE.MeshBasicMaterial({
color: 0x071521,
transparent: true,
opacity: 0.92,
side: THREE.DoubleSide
})
);
face.renderOrder = 12;
group.add(face);
const rim = new THREE.Mesh(
new THREE.TorusGeometry(0.39, 0.025, 10, 64),
new THREE.MeshBasicMaterial({
color: 0x7ab8d2,
transparent: true,
opacity: 0.55
})
);
rim.position.z = 0.012;
rim.renderOrder = 13;
group.add(rim);
const arcPoints = [];
for (let i = 0; i <= 48; i++) {
const ratio = i / 48;
const angle = THREE.MathUtils.degToRad(120 - ratio * 240);
arcPoints.push(gaugeDirection(angle, 0.31).setZ(0.025));
}
const arc = new THREE.Line(
new THREE.BufferGeometry().setFromPoints(arcPoints),
new THREE.LineBasicMaterial({
color: 0xff72ad,
transparent: true,
opacity: 0.82
})
);
arc.renderOrder = 13;
group.add(arc);
const tickVertices = [];
for (let i = 0; i <= 10; i++) {
const angle = gaugeAngle((i / 10) * TOY.maxPressure);
const inner = gaugeDirection(angle, 0.27);
const outer = gaugeDirection(angle, 0.33);
tickVertices.push(
inner.x, inner.y, 0.03,
outer.x, outer.y, 0.03
);
}
const ticks = new THREE.LineSegments(
new THREE.BufferGeometry().setAttribute(
"position",
new THREE.Float32BufferAttribute(tickVertices, 3)
),
new THREE.LineBasicMaterial({
color: 0xa6c4d5,
transparent: true,
opacity: 0.55
})
);
ticks.renderOrder = 13;
group.add(ticks);
const needleGeometry = new THREE.BoxGeometry(0.034, 0.29, 0.025);
needleGeometry.translate(0, 0.145, 0);
const needle = new THREE.Mesh(
needleGeometry,
new THREE.MeshBasicMaterial({
color: 0xf4fbff,
transparent: true,
opacity: 0.94
})
);
needle.position.z = 0.045;
needle.renderOrder = 14;
group.add(needle);
const peakAngle = gaugeAngle(peakAmplitude);
const peakDirection = gaugeDirection(peakAngle, 0.32);
const peakNeedle = new THREE.Line(
new THREE.BufferGeometry().setFromPoints([
new THREE.Vector3(0, 0, 0.055),
peakDirection.clone().setZ(0.055)
]),
new THREE.LineBasicMaterial({
color: 0xffc76a,
transparent: true,
opacity: 0.9
})
);
peakNeedle.renderOrder = 14;
group.add(peakNeedle);
const label = makeTextSprite("LIVE Δp", {
width: 0.7,
height: 0.2,
color: "#8eafc3",
fontSize: 38
});
label.position.set(0, -0.53, 0.04);
group.add(label);
return { group, needle };
}
// ---------------------------------------------------------------------
// Pipe rig
// ---------------------------------------------------------------------
function createRig({
title,
duration,
amplitude,
accent,
accentCss
}) {
const group = new THREE.Group();
group.add(createHalo());
// Pedestal.
const base = new THREE.Mesh(
new THREE.BoxGeometry(6.05, 0.25, 2.62),
baseMaterial
);
base.position.y = -1.64;
base.receiveShadow = true;
base.castShadow = true;
group.add(base);
const baseEdges = addEdges(base, 0x6ca8c5, 0.2);
baseEdges.position.y = -1.64;
const accentMaterial = new THREE.MeshStandardMaterial({
color: accent,
emissive: accent,
emissiveIntensity: 0.35,
roughness: 0.28,
metalness: 0.55
});
const accentStrip = new THREE.Mesh(
new THREE.BoxGeometry(5.96, 0.035, 2.56),
accentMaterial
);
accentStrip.position.y = -1.49;
group.add(accentStrip);
// Transparent pipe and inner fluid.
const shell = horizontalCylinder(0.52, 5.3, pipeShellMaterial);
shell.position.y = PIPE_Y;
shell.renderOrder = 3;
group.add(shell);
const fluid = horizontalCylinder(0.325, 5.08, fluidMaterial);
fluid.position.y = PIPE_Y;
fluid.renderOrder = 2;
group.add(fluid);
// Pipe end flanges.
[-2.64, 2.64].forEach((x) => {
const flange = torusAtX(x, 0.535, 0.075, metalMaterial);
flange.position.y = PIPE_Y;
flange.castShadow = true;
group.add(flange);
const innerFlange = torusAtX(x, 0.37, 0.026, accentMaterial);
innerFlange.position.y = PIPE_Y;
group.add(innerFlange);
});
// Supports.
[-1.7, 1.7].forEach((x) => {
const support = new THREE.Mesh(
new THREE.BoxGeometry(0.18, 1.36, 0.24),
darkMetalMaterial
);
support.position.set(x, -0.8, 0);
support.castShadow = true;
group.add(support);
const foot = new THREE.Mesh(
new THREE.BoxGeometry(0.55, 0.08, 0.55),
metalMaterial
);
foot.position.set(x, -1.43, 0);
group.add(foot);
});
// Valve body and flanges.
const valveBody = horizontalCylinder(
0.615,
0.48,
new THREE.MeshStandardMaterial({
color: 0x1c3648,
roughness: 0.3,
metalness: 0.72,
transparent: true,
opacity: 0.94
})
);
valveBody.position.set(VALVE_X, PIPE_Y, 0);
valveBody.castShadow = true;
group.add(valveBody);
[VALVE_X - 0.26, VALVE_X + 0.26].forEach((x) => {
const flange = torusAtX(x, 0.625, 0.072, metalMaterial);
flange.position.y = PIPE_Y;
group.add(flange);
});
// Sliding gate.
const gate = new THREE.Mesh(
new THREE.BoxGeometry(0.085, 0.68, 0.88),
accentMaterial
);
gate.position.set(VALVE_X, 0.78, 0);
gate.castShadow = true;
group.add(gate);
// Valve stem and handwheel.
const stem = new THREE.Mesh(
new THREE.CylinderGeometry(0.052, 0.052, 0.67, 16),
metalMaterial
);
stem.position.set(VALVE_X, 0.93, 0);
group.add(stem);
const wheel = new THREE.Group();
wheel.position.set(VALVE_X, 1.28, 0);
const wheelRing = new THREE.Mesh(
new THREE.TorusGeometry(0.31, 0.042, 10, 48),
accentMaterial
);
wheel.add(wheelRing);
for (let i = 0; i < 4; i++) {
const spoke = new THREE.Mesh(
new THREE.BoxGeometry(0.56, 0.032, 0.032),
metalMaterial
);
spoke.rotation.z = (i * Math.PI) / 4;
wheel.add(spoke);
}
group.add(wheel);
// Flow particles inside the upstream pipe.
const flow = [];
for (let i = 0; i < 11; i++) {
const particle = new THREE.Mesh(
new THREE.SphereGeometry(0.067, 12, 10),
flowMaterial
);
particle.renderOrder = 4;
group.add(particle);
flow.push(particle);
}
// Shock rings around the valve.
const pulseRings = [];
for (let i = 0; i < 4; i++) {
const material = new THREE.MeshBasicMaterial({
color: 0xff5fa2,
transparent: true,
opacity: 0,
blending: THREE.AdditiveBlending,
depthTest: false,
depthWrite: false
});
const ring = new THREE.Mesh(
new THREE.TorusGeometry(0.39, 0.035, 8, 52),
material
);
ring.rotation.y = Math.PI / 2;
ring.position.y = PIPE_Y;
ring.renderOrder = 8;
group.add(ring);
pulseRings.push({ mesh: ring, material });
}
// Pressure chart.
const chart = createChart();
group.add(chart.group);
const peakLine = createPeakLine(amplitude);
chart.group.add(peakLine);
// Live gauge.
const gauge = createGauge(amplitude);
gauge.group.position.set(2.58, 0.63, 0.84);
group.add(gauge.group);
// Floating title and values.
const badge = makeRigBadge(
title,
`Tclose = ${duration.toFixed(2)} s | peak = ${amplitude.toFixed(1)} a.u.`,
accentCss
);
badge.position.set(0, 2.04, 0.18);
group.add(badge);
return {
group,
duration,
amplitude,
gate,
wheel,
flow,
flowPhase: 0,
pulseRings,
chart,
gauge
};
}
const slowRig = createRig({
title: "SLOW CLOSURE",
duration: SLOW_DURATION,
amplitude: slowAmplitude,
accent: 0x67e8f9,
accentCss: "#67e8f9"
});
const abruptRig = createRig({
title: "ABRUPT CLOSURE",
duration: ABRUPT_DURATION,
amplitude: abruptAmplitude,
accent: 0xffb45f,
accentCss: "#ffb45f"
});
scene.add(slowRig.group, abruptRig.group);
// ---------------------------------------------------------------------
// Responsive layout
// ---------------------------------------------------------------------
let stackedLayout = false;
function layoutScene() {
stackedLayout = window.innerWidth / window.innerHeight < 1.25;
if (stackedLayout) {
slowRig.group.position.set(0, 1.85, 0);
abruptRig.group.position.set(0, -1.85, 0);
cameraTarget.set(0, 0, 0);
} else {
slowRig.group.position.set(-5.05, 0, 0);
abruptRig.group.position.set(5.05, 0, 0);
cameraTarget.set(0, -0.12, 0);
}
}
function updateCamera() {
const horizontal = cameraDistance * Math.cos(cameraPitch);
camera.position.set(
cameraTarget.x + Math.sin(cameraYaw) * horizontal,
cameraTarget.y + Math.sin(cameraPitch) * cameraDistance,
cameraTarget.z + Math.cos(cameraYaw) * horizontal
);
camera.lookAt(cameraTarget);
}
function resetCamera() {
cameraYaw = 0;
cameraPitch = stackedLayout ? 0.19 : 0.25;
cameraDistance = stackedLayout ? 21 : 17.5;
updateCamera();
}
layoutScene();
resetCamera();
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
const wasStacked = stackedLayout;
layoutScene();
if (wasStacked !== stackedLayout) {
resetCamera();
}
});
// ---------------------------------------------------------------------
// Custom camera controls: orbit, wheel zoom, and touch pinch zoom
// ---------------------------------------------------------------------
const pointers = new Map();
function pointerGesture() {
const values = [...pointers.values()];
if (!values.length) return null;
let midpointX = 0;
let midpointY = 0;
for (const pointer of values) {
midpointX += pointer.x;
midpointY += pointer.y;
}
midpointX /= values.length;
midpointY /= values.length;
let distance = 0;
if (values.length >= 2) {
distance = Math.hypot(
values[0].x - values[1].x,
values[0].y - values[1].y
);
}
return { midpointX, midpointY, distance };
}
renderer.domElement.addEventListener("pointerdown", (event) => {
renderer.domElement.setPointerCapture(event.pointerId);
pointers.set(event.pointerId, { x: event.clientX, y: event.clientY });
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!pointers.has(event.pointerId)) return;
const before = pointerGesture();
pointers.set(event.pointerId, { x: event.clientX, y: event.clientY });
const after = pointerGesture();
if (!before || !after) return;
if (after.distance > 0 && before.distance > 0) {
cameraDistance = clamp(
cameraDistance * (before.distance / after.distance),
10,
29
);
}
cameraYaw -= (after.midpointX - before.midpointX) * 0.005;
cameraPitch = clamp(
cameraPitch + (after.midpointY - before.midpointY) * 0.0035,
0.03,
0.72
);
});
function removePointer(event) {
pointers.delete(event.pointerId);
}
renderer.domElement.addEventListener("pointerup", removePointer);
renderer.domElement.addEventListener("pointercancel", removePointer);
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraDistance = clamp(
cameraDistance * Math.exp(event.deltaY * 0.001),
10,
29
);
},
{ passive: false }
);
// ---------------------------------------------------------------------
// Time model
// ---------------------------------------------------------------------
let elapsed = 0;
let paused = false;
let previousTime = performance.now();
function pressureAt(rig, time) {
if (time < 0) return 0;
const cycleIndex = Math.floor(time / CYCLE);
const local = time - cycleIndex * CYCLE;
const eventAge = local - CLOSURE_START;
if (eventAge < 0) return 0;
if (eventAge < rig.duration) {
return rig.amplitude * smoothstep(eventAge / rig.duration);
}
return rig.amplitude * Math.exp(-(eventAge - rig.duration) / 1.15);
}
function closureAt(rig, localTime) {
const eventAge = localTime - CLOSURE_START;
if (eventAge < 0) return 0;
if (eventAge < rig.duration) {
return smoothstep(eventAge / rig.duration);
}
if (localTime < REOPEN_START) return 1;
return (
1 -
smoothstep(
(localTime - REOPEN_START) / REOPEN_DURATION
)
);
}
function updateChart(rig, currentTime) {
const chart = rig.chart;
const windowDuration = 5;
const startTime = currentTime - windowDuration;
for (let i = 0; i < CHART.samples; i++) {
const ratio = i / (CHART.samples - 1);
const sampleTime = startTime + ratio * windowDuration;
const pressure = pressureAt(rig, sampleTime);
const index = i * 3;
chart.positions[index] = THREE.MathUtils.lerp(
CHART.minX,
CHART.maxX,
ratio
);
chart.positions[index + 1] =
CHART.baseline +
(pressure / TOY.maxPressure) * CHART.height;
chart.positions[index + 2] = CHART.z + 0.045;
}
chart.geometry.attributes.position.needsUpdate = true;
chart.markers.forEach((marker, markerIndex) => {
const index = Math.round(
(markerIndex / (chart.markers.length - 1)) *
(CHART.samples - 1)
);
const offset = index * 3;
marker.position.set(
chart.positions[offset],
chart.positions[offset + 1],
CHART.z + 0.07
);
});
}
function updateRig(rig, currentTime, deltaTime) {
const local = currentTime % CYCLE;
const closure = closureAt(rig, local);
const pressure = pressureAt(rig, currentTime);
rig.gate.position.y = THREE.MathUtils.lerp(0.78, 0.39, closure);
rig.wheel.rotation.z = -closure * Math.PI * 2.4;
if (!paused) {
rig.flowPhase += deltaTime * 0.95 * (1 - closure * 0.92);
}
rig.flow.forEach((particle, index) => {
let x =
-2.38 +
((rig.flowPhase + index * 0.47) % 4.55);
if (x > VALVE_X - 0.2) {
x =
VALVE_X -
0.2 -
(x - (VALVE_X - 0.2)) * (0.06 + closure * 0.08);
}
particle.position.set(
x,
PIPE_Y + Math.sin(currentTime * 2.1 + index) * 0.09,
Math.sin(currentTime * 1.7 + index * 1.8) * 0.13
);
particle.scale.setScalar(
0.8 + Math.sin(currentTime * 3 + index) * 0.12
);
});
const eventAge = local - CLOSURE_START;
const strength = rig.amplitude > 0
? pressure / rig.amplitude
: 0;
rig.pulseRings.forEach((ring, index) => {
const age = eventAge - index * 0.16;
if (age >= 0 && age < 2.4) {
const direction = index % 2 === 0 ? 1 : -1;
const distance = 0.14 + age * 1.36;
ring.mesh.position.set(
VALVE_X + direction * distance,
PIPE_Y,
0
);
ring.mesh.scale.setScalar(1 + strength * 0.28);
ring.material.opacity =
(1 - age / 2.4) * (0.12 + strength * 0.78);
} else {
ring.material.opacity = 0;
}
});
rig.gauge.needle.rotation.z = gaugeAngle(pressure);
updateChart(rig, currentTime);
}
// ---------------------------------------------------------------------
// UI
// ---------------------------------------------------------------------
const pauseButton = document.getElementById("pauseButton");
const restartButton = document.getElementById("restartButton");
const cameraButton = document.getElementById("cameraButton");
const statusText = document.getElementById("statusText");
pauseButton.addEventListener("click", () => {
paused = !paused;
pauseButton.textContent = paused ? "Play" : "Pause";
});
restartButton.addEventListener("click", () => {
elapsed = 0;
slowRig.flowPhase = 0;
abruptRig.flowPhase = 0;
});
cameraButton.addEventListener("click", resetCamera);
function updateStatus(local) {
let text;
if (local < CLOSURE_START) {
text = `Valve open · closure in ${(CLOSURE_START - local).toFixed(1)} s`;
} else if (local < CLOSURE_START + slowRig.duration) {
const percent = Math.round(
((local - CLOSURE_START) / slowRig.duration) * 100
);
text = `Closing · slow gate ${percent}%`;
} else if (local < REOPEN_START) {
text = "Closed · pressure pulse visible";
} else {
text = "Reopening";
}
statusText.textContent = text;
}
// ---------------------------------------------------------------------
// Animation loop
// ---------------------------------------------------------------------
function animate(now) {
requestAnimationFrame(animate);
const deltaTime = Math.min((now - previousTime) / 1000, 0.05);
previousTime = now;
if (!paused) {
elapsed += deltaTime;
}
updateRig(slowRig, elapsed, deltaTime);
updateRig(abruptRig, elapsed, deltaTime);
updateStatus(elapsed % CYCLE);
updateCamera();
renderer.render(scene, camera);
}
document.getElementById("loading")?.remove();
requestAnimationFrame(animate);
</script>
</body>
</html>
```