Prompt specification
Build or customize the Three.js 3D scene "Pipe valve and pressure-wave markers" 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: Pipe valve and pressure-wave markers
Category: Science & Physics · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/pipe-valve-and-pressure-wave-markers-x1493
Scene Description Prompt:
Make a standalone HTML scene with Three.js from cdnjs that depicts a qualitative pressure wave in a pipe. Build a transparent straight pipe ending in a valve with several pressure sensors; show a prescribed traveling disturbance and label it a qualitative transient illustration. Low-poly.
Working Three.js Starter (pipe-valve-and-pressure-wave-markers-x1493):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Qualitative Pipe Pressure Transient</title>
<style>
:root {
color-scheme: dark;
--bg: #071019;
--panel: rgba(10, 20, 31, 0.88);
--border: rgba(157, 220, 238, 0.18);
--text: #e8f4f7;
--muted: #8da7b1;
--cyan: #54c8ff;
--teal: #55dfd0;
--orange: #ff7547;
--blue: #4e9fff;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: var(--bg);
color: var(--text);
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;
}
.panel {
position: fixed;
z-index: 5;
border: 1px solid var(--border);
background: var(--panel);
box-shadow: 0 16px 50px rgba(0, 0, 0, 0.28);
backdrop-filter: blur(14px);
-webkit-backdrop-filter: blur(14px);
}
#title-panel {
top: 22px;
left: 22px;
width: min(420px, calc(100vw - 44px));
padding: 18px 20px;
border-radius: 15px;
pointer-events: none;
}
.eyebrow {
color: var(--teal);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.19em;
text-transform: uppercase;
}
h1 {
margin: 7px 0 9px;
font-size: clamp(20px, 2.6vw, 29px);
line-height: 1.08;
letter-spacing: -0.035em;
}
#qualitative-badge {
display: inline-flex;
align-items: center;
gap: 8px;
padding: 7px 10px;
border: 1px solid rgba(255, 117, 71, 0.42);
border-radius: 999px;
background: rgba(255, 117, 71, 0.1);
color: #ffad8e;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.11em;
text-transform: uppercase;
}
#qualitative-badge::before {
width: 7px;
height: 7px;
border-radius: 50%;
background: var(--orange);
box-shadow: 0 0 12px var(--orange);
content: "";
}
.subtitle {
margin: 11px 0 0;
color: var(--muted);
font-size: 12px;
line-height: 1.5;
}
#instrument-panel {
top: 22px;
right: 22px;
width: min(320px, calc(100vw - 44px));
padding: 16px;
border-radius: 15px;
}
.panel-heading {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 10px;
}
.panel-heading strong {
font-size: 11px;
letter-spacing: 0.12em;
text-transform: uppercase;
}
.panel-heading span {
color: var(--muted);
font-size: 10px;
}
.legend {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 6px;
margin: 12px 0;
}
.legend-item {
padding: 8px 5px;
border: 1px solid rgba(255, 255, 255, 0.07);
border-radius: 9px;
background: rgba(255, 255, 255, 0.025);
color: var(--muted);
font-size: 9px;
text-align: center;
}
.legend-item b {
display: block;
margin-top: 3px;
font-size: 11px;
}
.negative {
color: var(--cyan);
}
.neutral {
color: var(--teal);
}
.positive {
color: var(--orange);
}
#profile {
display: block;
width: 100%;
height: 86px;
margin-top: 5px;
overflow: visible;
}
.equation {
margin-top: 9px;
padding: 9px 10px;
border-left: 2px solid var(--teal);
background: rgba(84, 223, 208, 0.055);
color: #b9d8db;
font-family: "SFMono-Regular", Consolas, monospace;
font-size: 10px;
line-height: 1.55;
}
.control-row {
display: flex;
align-items: center;
gap: 8px;
margin-top: 13px;
}
button {
border: 1px solid rgba(100, 211, 218, 0.28);
border-radius: 8px;
background: rgba(84, 223, 208, 0.1);
color: var(--text);
cursor: pointer;
font: inherit;
font-size: 11px;
font-weight: 700;
padding: 8px 11px;
transition: background 140ms ease, border-color 140ms ease;
}
button:hover {
border-color: rgba(100, 211, 218, 0.55);
background: rgba(84, 223, 208, 0.18);
}
button:active {
transform: translateY(1px);
}
#speed {
min-width: 0;
flex: 1;
accent-color: var(--teal);
}
#speed-value {
min-width: 37px;
color: var(--muted);
font-size: 10px;
text-align: right;
}
#help {
position: fixed;
z-index: 4;
bottom: 18px;
left: 22px;
padding: 9px 12px;
border: 1px solid var(--border);
border-radius: 9px;
background: rgba(7, 16, 25, 0.72);
color: var(--muted);
font-size: 10px;
pointer-events: none;
}
#note {
position: fixed;
z-index: 4;
right: 22px;
bottom: 18px;
max-width: 320px;
padding: 9px 12px;
border: 1px solid var(--border);
border-radius: 9px;
background: rgba(7, 16, 25, 0.72);
color: var(--muted);
font-size: 10px;
line-height: 1.45;
text-align: right;
pointer-events: none;
}
#labels {
position: fixed;
z-index: 3;
inset: 0;
overflow: hidden;
pointer-events: none;
}
.world-label {
position: absolute;
min-width: 76px;
padding: 5px 7px;
border: 1px solid rgba(126, 206, 226, 0.22);
border-radius: 6px;
background: rgba(7, 16, 25, 0.82);
box-shadow: 0 4px 15px rgba(0, 0, 0, 0.2);
font-size: 9px;
line-height: 1.35;
text-align: center;
transform: translate(-50%, -100%);
white-space: nowrap;
}
.world-label .id {
color: #d8edf1;
font-weight: 800;
letter-spacing: 0.08em;
}
.world-label .value {
display: block;
margin-top: 2px;
color: var(--teal);
font-family: "SFMono-Regular", Consolas, monospace;
}
#wave-label {
border-color: rgba(255, 117, 71, 0.35);
background: rgba(35, 15, 12, 0.83);
color: #ffc0a5;
font-size: 8px;
font-weight: 800;
letter-spacing: 0.1em;
}
#valve-label {
border-color: rgba(255, 196, 94, 0.35);
color: #ffd68d;
font-weight: 800;
letter-spacing: 0.1em;
}
@media (max-width: 760px) {
#title-panel {
top: 12px;
left: 12px;
width: calc(100vw - 24px);
padding: 13px 15px;
}
#title-panel .subtitle {
display: none;
}
#instrument-panel {
top: auto;
right: 12px;
bottom: 12px;
width: calc(100vw - 24px);
padding: 12px;
}
#profile {
height: 55px;
}
.equation,
#help,
#note {
display: none;
}
}
</style>
</head>
<body>
<div id="scene"></div>
<div id="labels"></div>
<section id="title-panel" class="panel">
<div class="eyebrow">Fluid systems / transient visualization</div>
<h1>Pressure disturbance in a straight pipe</h1>
<div id="qualitative-badge">Qualitative transient illustration</div>
<p class="subtitle">
A prescribed positive pulse with a trailing negative trough travels toward
the downstream valve. Color and ring spacing indicate relative pressure.
</p>
</section>
<section id="instrument-panel" class="panel">
<div class="panel-heading">
<strong>Pressure profile</strong>
<span>normalized Δp</span>
</div>
<div class="legend">
<div class="legend-item">
<b class="negative">−1.0</b>
pressure fall
</div>
<div class="legend-item">
<b class="neutral">0.0</b>
reference
</div>
<div class="legend-item">
<b class="positive">+1.0</b>
pressure rise
</div>
</div>
<svg id="profile" viewBox="0 0 300 80" aria-label="Current pressure profile">
<line
x1="8"
x2="292"
y1="40"
y2="40"
stroke="rgba(177, 216, 226, 0.2)"
stroke-width="1"
/>
<path
id="profile-area"
d=""
fill="rgba(84, 223, 208, 0.1)"
/>
<path
id="profile-line"
d=""
fill="none"
stroke="#55dfd0"
stroke-width="2.3"
stroke-linecap="round"
stroke-linejoin="round"
/>
<circle id="profile-dot" cx="20" cy="40" r="3.5" fill="#ff7547" />
</svg>
<div class="equation">
p̃(x,t) = exp[−(x−c(t))² / 2σ²]<br />
− 0.38 exp[−(x−c(t)+2)² / 2(1.25)²]
</div>
<div class="control-row">
<button id="pause" type="button">Pause</button>
<button id="replay" type="button">Replay</button>
<input
id="speed"
type="range"
min="0.25"
max="2.5"
step="0.05"
value="1"
aria-label="Animation speed"
/>
<span id="speed-value">1.00×</span>
</div>
</section>
<div id="help">Drag to orbit · Shift-drag to pan · Wheel to zoom · Double-click to reset</div>
<div id="note">
Illustrative disturbance only — not a CFD result, scale model, or engineering prediction.
</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const sceneContainer = document.querySelector("#scene");
const labelsContainer = document.querySelector("#labels");
const profileLine = document.querySelector("#profile-line");
const profileArea = document.querySelector("#profile-area");
const profileDot = document.querySelector("#profile-dot");
const pauseButton = document.querySelector("#pause");
const replayButton = document.querySelector("#replay");
const speedInput = document.querySelector("#speed");
const speedValue = document.querySelector("#speed-value");
const PIPE_START = -4.6;
const PIPE_END = 3.8;
const PIPE_LENGTH = PIPE_END - PIPE_START;
const PIPE_RADIUS = 0.76;
const WAVE_SPEED = 1.65;
const WAVE_SIGMA = 0.62;
let paused = false;
let animationSpeed = 1;
let simulationTime = 0;
// -----------------------------------------------------------------------
// Renderer, scene, and camera
// -----------------------------------------------------------------------
const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.15;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
sceneContainer.appendChild(renderer.domElement);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x071019);
scene.fog = new THREE.Fog(0x071019, 15, 31);
const camera = new THREE.PerspectiveCamera(
42,
window.innerWidth / window.innerHeight,
0.1,
100
);
const controls = {
target: new THREE.Vector3(0.1, 0.05, 0),
spherical: new THREE.Spherical(14.5, 1.23, 0.58),
dragging: false,
panning: false,
previousX: 0,
previousY: 0
};
function resetCamera() {
controls.target.set(0.1, 0.05, 0);
controls.spherical.set(
window.innerWidth / window.innerHeight < 0.8 ? 21 : 14.5,
1.23,
0.58
);
}
function updateCamera() {
controls.spherical.phi = THREE.MathUtils.clamp(
controls.spherical.phi,
0.18,
Math.PI - 0.18
);
controls.spherical.radius = THREE.MathUtils.clamp(
controls.spherical.radius,
6,
30
);
const offset = new THREE.Vector3().setFromSpherical(controls.spherical);
camera.position.copy(controls.target).add(offset);
camera.lookAt(controls.target);
camera.updateMatrixWorld();
}
resetCamera();
updateCamera();
// -----------------------------------------------------------------------
// Lighting and ground
// -----------------------------------------------------------------------
scene.add(new THREE.HemisphereLight(0xa7e5ff, 0x10151c, 2.0));
const keyLight = new THREE.DirectionalLight(0xffffff, 3.2);
keyLight.position.set(2.5, 8, 5.5);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(1024, 1024);
keyLight.shadow.camera.left = -10;
keyLight.shadow.camera.right = 10;
keyLight.shadow.camera.top = 8;
keyLight.shadow.camera.bottom = -8;
keyLight.shadow.bias = -0.0005;
scene.add(keyLight);
const rimLight = new THREE.DirectionalLight(0x4ba9ff, 1.6);
rimLight.position.set(-7, 3, -5);
scene.add(rimLight);
const valveLight = new THREE.PointLight(0xff8b50, 9, 8, 2);
valveLight.position.set(4.35, 2.25, 1.6);
scene.add(valveLight);
const floor = new THREE.Mesh(
new THREE.PlaneGeometry(42, 42),
new THREE.MeshStandardMaterial({
color: 0x0b141d,
roughness: 0.94,
metalness: 0.03
})
);
floor.rotation.x = -Math.PI / 2;
floor.position.y = -1.68;
floor.receiveShadow = true;
scene.add(floor);
const grid = new THREE.GridHelper(34, 34, 0x31505c, 0x19313c);
grid.position.y = -1.665;
grid.material.transparent = true;
grid.material.opacity = 0.28;
scene.add(grid);
// -----------------------------------------------------------------------
// Materials
// -----------------------------------------------------------------------
const pipeMaterial = new THREE.MeshPhysicalMaterial({
color: 0x88d7ee,
roughness: 0.22,
metalness: 0.08,
transparent: true,
opacity: 0.17,
side: THREE.DoubleSide,
depthWrite: false
});
const fluidMaterial = new THREE.MeshStandardMaterial({
color: 0x36bfc0,
emissive: 0x0b5356,
emissiveIntensity: 0.3,
roughness: 0.45,
metalness: 0.04,
transparent: true,
opacity: 0.09,
depthWrite: false
});
const edgeMaterial = new THREE.LineBasicMaterial({
color: 0x8ad7e8,
transparent: true,
opacity: 0.32
});
const metalMaterial = new THREE.MeshStandardMaterial({
color: 0x526575,
roughness: 0.48,
metalness: 0.72,
flatShading: true
});
const darkMetalMaterial = new THREE.MeshStandardMaterial({
color: 0x263541,
roughness: 0.6,
metalness: 0.65,
flatShading: true
});
const brassMaterial = new THREE.MeshStandardMaterial({
color: 0xb98236,
roughness: 0.4,
metalness: 0.68,
flatShading: true
});
const wheelMaterial = new THREE.MeshStandardMaterial({
color: 0xd84d2f,
roughness: 0.48,
metalness: 0.32,
flatShading: true
});
// -----------------------------------------------------------------------
// Transparent straight pipe
// -----------------------------------------------------------------------
const pipeCenter = (PIPE_START + PIPE_END) / 2;
const pipeGeometry = new THREE.CylinderGeometry(
PIPE_RADIUS,
PIPE_RADIUS,
PIPE_LENGTH,
20,
1,
true
);
const pipe = new THREE.Mesh(pipeGeometry, pipeMaterial);
pipe.rotation.z = Math.PI / 2;
pipe.position.x = pipeCenter;
pipe.renderOrder = 3;
scene.add(pipe);
const pipeEdges = new THREE.LineSegments(
new THREE.EdgesGeometry(pipeGeometry),
edgeMaterial
);
pipeEdges.rotation.z = Math.PI / 2;
pipeEdges.position.x = pipeCenter;
pipeEdges.renderOrder = 4;
scene.add(pipeEdges);
const fluid = new THREE.Mesh(
new THREE.CylinderGeometry(0.66, 0.66, PIPE_LENGTH - 0.04, 18),
fluidMaterial
);
fluid.rotation.z = Math.PI / 2;
fluid.position.x = pipeCenter;
fluid.renderOrder = 2;
scene.add(fluid);
function makePipeRing(x, radius = PIPE_RADIUS, tube = 0.025, material = metalMaterial) {
const ring = new THREE.Mesh(
new THREE.TorusGeometry(radius, tube, 6, 20),
material
);
ring.rotation.y = Math.PI / 2;
ring.position.x = x;
ring.castShadow = true;
scene.add(ring);
return ring;
}
[-4.45, -2.3, -0.05, 2.2, 3.65].forEach((x) => {
makePipeRing(x, PIPE_RADIUS + 0.025, 0.045);
});
// Low-poly pipe supports.
function makeSupport(x) {
const support = new THREE.Group();
const foot = new THREE.Mesh(
new THREE.BoxGeometry(0.85, 0.16, 1.15),
darkMetalMaterial
);
foot.position.y = -1.57;
foot.castShadow = true;
foot.receiveShadow = true;
support.add(foot);
const post = new THREE.Mesh(
new THREE.CylinderGeometry(0.13, 0.19, 0.92, 6),
metalMaterial
);
post.position.y = -1.02;
post.castShadow = true;
support.add(post);
const cradle = new THREE.Mesh(
new THREE.TorusGeometry(PIPE_RADIUS + 0.045, 0.055, 6, 20),
metalMaterial
);
cradle.rotation.y = Math.PI / 2;
cradle.castShadow = true;
support.add(cradle);
support.position.x = x;
scene.add(support);
}
makeSupport(-3.05);
makeSupport(1.25);
// -----------------------------------------------------------------------
// Downstream valve assembly
// -----------------------------------------------------------------------
const valve = new THREE.Group();
valve.position.x = 4.35;
scene.add(valve);
const valveBody = new THREE.Mesh(
new THREE.CylinderGeometry(0.54, 0.61, 0.92, 8),
brassMaterial
);
valveBody.rotation.z = Math.PI / 2;
valveBody.castShadow = true;
valveBody.receiveShadow = true;
valve.add(valveBody);
function makeFlange(x) {
const flange = new THREE.Mesh(
new THREE.CylinderGeometry(0.82, 0.82, 0.14, 12),
brassMaterial
);
flange.rotation.z = Math.PI / 2;
flange.position.x = x;
flange.castShadow = true;
valve.add(flange);
for (let i = 0; i < 8; i++) {
const angle = (i / 8) * Math.PI * 2;
const bolt = new THREE.Mesh(
new THREE.CylinderGeometry(0.05, 0.05, 0.18, 6),
darkMetalMaterial
);
bolt.rotation.z = Math.PI / 2;
bolt.position.set(
x + (x < 4.35 ? -0.06 : 0.06),
Math.cos(angle) * 0.64,
Math.sin(angle) * 0.64
);
bolt.castShadow = true;
valve.add(bolt);
}
}
makeFlange(-0.52);
makeFlange(0.52);
const bonnet = new THREE.Mesh(
new THREE.CylinderGeometry(0.32, 0.39, 0.58, 8),
brassMaterial
);
bonnet.position.y = 0.83;
bonnet.castShadow = true;
valve.add(bonnet);
const stem = new THREE.Mesh(
new THREE.CylinderGeometry(0.075, 0.075, 0.85, 8),
metalMaterial
);
stem.position.y = 1.48;
stem.castShadow = true;
valve.add(stem);
const wheel = new THREE.Group();
wheel.position.y = 1.94;
const wheelRing = new THREE.Mesh(
new THREE.TorusGeometry(0.54, 0.075, 6, 16),
wheelMaterial
);
wheelRing.rotation.x = Math.PI / 2;
wheelRing.castShadow = true;
wheel.add(wheelRing);
for (let i = 0; i < 4; i++) {
const spoke = new THREE.Mesh(
new THREE.BoxGeometry(1.02, 0.075, 0.085),
wheelMaterial
);
spoke.rotation.y = (i / 4) * Math.PI;
spoke.castShadow = true;
wheel.add(spoke);
}
const wheelHub = new THREE.Mesh(
new THREE.CylinderGeometry(0.15, 0.15, 0.16, 8),
darkMetalMaterial
);
wheelHub.castShadow = true;
wheel.add(wheelHub);
valve.add(wheel);
const outlet = new THREE.Mesh(
new THREE.CylinderGeometry(0.44, 0.44, 0.6, 10, 1, true),
pipeMaterial
);
outlet.rotation.z = Math.PI / 2;
outlet.position.set(0.82, 0, 0);
outlet.renderOrder = 3;
valve.add(outlet);
// -----------------------------------------------------------------------
// Traveling disturbance visualization
// -----------------------------------------------------------------------
const waveRings = [];
const ringCount = 72;
for (let i = 0; i < ringCount; i++) {
const ring = new THREE.Mesh(
new THREE.TorusGeometry(0.605, 0.039, 5, 16),
new THREE.MeshStandardMaterial({
color: 0x55dfd0,
emissive: 0x55dfd0,
emissiveIntensity: 0.35,
roughness: 0.4,
metalness: 0.05,
transparent: true,
opacity: 0.12,
depthWrite: false
})
);
ring.rotation.y = Math.PI / 2;
ring.renderOrder = 5;
scene.add(ring);
waveRings.push(ring);
}
// Direction arrow beneath the pipe.
const arrowGroup = new THREE.Group();
arrowGroup.position.set(0, -1.02, 0.38);
const arrowShaft = new THREE.Mesh(
new THREE.CylinderGeometry(0.035, 0.035, 6.25, 6),
new THREE.MeshBasicMaterial({ color: 0xff7547 })
);
arrowShaft.rotation.z = -Math.PI / 2;
arrowShaft.position.x = -0.55;
arrowGroup.add(arrowShaft);
const arrowHead = new THREE.Mesh(
new THREE.ConeGeometry(0.13, 0.38, 5),
new THREE.MeshBasicMaterial({ color: 0xff7547 })
);
arrowHead.rotation.z = -Math.PI / 2;
arrowHead.position.x = 2.85;
arrowGroup.add(arrowHead);
scene.add(arrowGroup);
// -----------------------------------------------------------------------
// Pressure sensors and projected labels
// -----------------------------------------------------------------------
const sensorPositions = [-3.45, -1.62, 0.22, 2.32];
const sensors = [];
function createWorldLabel(className = "") {
const element = document.createElement("div");
element.className = `world-label ${className}`.trim();
labelsContainer.appendChild(element);
return element;
}
sensorPositions.forEach((x, index) => {
const group = new THREE.Group();
group.position.x = x;
const mountingRing = new THREE.Mesh(
new THREE.TorusGeometry(PIPE_RADIUS + 0.018, 0.035, 5, 16),
brassMaterial
);
mountingRing.rotation.y = Math.PI / 2;
group.add(mountingRing);
const stem = new THREE.Mesh(
new THREE.CylinderGeometry(0.052, 0.065, 0.38, 6),
metalMaterial
);
stem.position.y = 0.94;
stem.castShadow = true;
group.add(stem);
const housing = new THREE.Mesh(
new THREE.CylinderGeometry(0.18, 0.23, 0.38, 8),
darkMetalMaterial
);
housing.position.y = 1.3;
housing.castShadow = true;
group.add(housing);
const cap = new THREE.Mesh(
new THREE.CylinderGeometry(0.2, 0.2, 0.075, 8),
metalMaterial
);
cap.position.y = 1.52;
cap.castShadow = true;
group.add(cap);
const ledMaterial = new THREE.MeshStandardMaterial({
color: 0x55dfd0,
emissive: 0x55dfd0,
emissiveIntensity: 0.8,
roughness: 0.3,
flatShading: true
});
const led = new THREE.Mesh(
new THREE.IcosahedronGeometry(0.09, 0),
ledMaterial
);
led.position.set(0, 1.58, 0);
group.add(led);
scene.add(group);
const label = createWorldLabel();
label.innerHTML = `
<span class="id">S${index + 1}</span>
<span class="value">Δp +0.00</span>
`;
sensors.push({
x,
group,
led,
ledMaterial,
label,
valueElement: label.querySelector(".value"),
anchor: new THREE.Vector3(x, 1.82, 0)
});
});
const waveLabel = createWorldLabel("wave-label");
waveLabel.textContent = "PRESCRIBED TRAVELING DISTURBANCE";
const valveLabel = createWorldLabel("valve-label");
valveLabel.textContent = "DOWNSTREAM VALVE";
// -----------------------------------------------------------------------
// Prescribed disturbance and visual updates
// -----------------------------------------------------------------------
const neutralColor = new THREE.Color(0x55dfd0);
const positiveColor = new THREE.Color(0xff7547);
const negativeColor = new THREE.Color(0x54c8ff);
const temporaryColor = new THREE.Color();
function pulseCenter() {
return PIPE_START + ((simulationTime * WAVE_SPEED + 2.0) % PIPE_LENGTH);
}
function pressureAt(x, center = pulseCenter()) {
const primary = Math.exp(
-0.5 * Math.pow((x - center) / WAVE_SIGMA, 2)
);
const trailing = -0.38 * Math.exp(
-0.5 * Math.pow((x - center + 2.0) / 1.25, 2)
);
return THREE.MathUtils.clamp(primary + trailing, -1, 1);
}
function colorForPressure(value, target = temporaryColor) {
if (value >= 0) {
return target.copy(neutralColor).lerp(positiveColor, value);
}
return target.copy(neutralColor).lerp(negativeColor, -value);
}
function updateWave() {
const center = pulseCenter();
waveRings.forEach((ring, index) => {
const t = index / (waveRings.length - 1);
const x = THREE.MathUtils.lerp(PIPE_START, PIPE_END, t);
const pressure = pressureAt(x, center);
const color = colorForPressure(pressure);
ring.position.x = x;
const radialScale = 1 + pressure * 0.13;
ring.scale.set(1, radialScale, radialScale);
ring.material.color.copy(color);
ring.material.emissive.copy(color);
ring.material.emissiveIntensity = 0.28 + Math.abs(pressure) * 0.9;
ring.material.opacity = 0.1 + Math.abs(pressure) * 0.82;
});
sensors.forEach((sensor) => {
const pressure = pressureAt(sensor.x, center);
const color = colorForPressure(pressure, new THREE.Color());
sensor.ledMaterial.color.copy(color);
sensor.ledMaterial.emissive.copy(color);
sensor.ledMaterial.emissiveIntensity = 0.65 + Math.abs(pressure) * 1.6;
sensor.led.scale.setScalar(1 + Math.abs(pressure) * 0.35);
sensor.valueElement.textContent =
`Δp ${pressure >= 0 ? "+" : ""}${pressure.toFixed(2)}`;
sensor.valueElement.style.color = `#${color.getHexString()}`;
});
waveLabelAnchor.x = center;
}
const waveLabelAnchor = new THREE.Vector3(0, -0.76, 0.38);
const valveLabelAnchor = new THREE.Vector3(4.35, 2.65, 0);
function updatePressureProfile() {
const center = pulseCenter();
let path = "";
let area = "";
const points = [];
for (let i = 0; i <= 70; i++) {
const x = PIPE_START + (i / 70) * PIPE_LENGTH;
const pressure = pressureAt(x, center);
const px = 8 + (i / 70) * 284;
const py = 40 - pressure * 27;
points.push({ px, py, pressure });
path += `${i === 0 ? "M" : "L"}${px.toFixed(2)},${py.toFixed(2)} `;
}
area = `${path}L292,40 L8,40 Z`;
profileLine.setAttribute("d", path);
profileArea.setAttribute("d", area);
const pulsePoint = points.reduce((nearest, point) => {
return Math.abs(point.px - (8 + ((center - PIPE_START) / PIPE_LENGTH) * 284))
< Math.abs(nearest.px - (8 + ((center - PIPE_START) / PIPE_LENGTH) * 284))
? point
: nearest;
}, points[0]);
profileDot.setAttribute("cx", pulsePoint.px);
profileDot.setAttribute("cy", pulsePoint.py);
profileDot.setAttribute("fill", pulsePoint.pressure >= 0 ? "#ff7547" : "#54c8ff");
}
// -----------------------------------------------------------------------
// DOM label projection
// -----------------------------------------------------------------------
const projected = new THREE.Vector3();
function projectLabel(element, anchor) {
projected.copy(anchor).project(camera);
const visible = projected.z > -1 && projected.z < 1;
element.style.display = visible ? "block" : "none";
if (!visible) return;
const x = (projected.x * 0.5 + 0.5) * window.innerWidth;
const y = (-projected.y * 0.5 + 0.5) * window.innerHeight;
element.style.left = `${x}px`;
element.style.top = `${y}px`;
}
function updateLabels() {
sensors.forEach((sensor) => {
projectLabel(sensor.label, sensor.anchor);
});
projectLabel(waveLabel, waveLabelAnchor);
projectLabel(valveLabel, valveLabelAnchor);
}
// -----------------------------------------------------------------------
// Custom pointer camera controls
// -----------------------------------------------------------------------
const canvas = renderer.domElement;
canvas.addEventListener("contextmenu", (event) => event.preventDefault());
canvas.addEventListener("pointerdown", (event) => {
controls.dragging = true;
controls.panning = event.button === 2 || event.shiftKey;
controls.previousX = event.clientX;
controls.previousY = event.clientY;
canvas.setPointerCapture(event.pointerId);
});
canvas.addEventListener("pointermove", (event) => {
if (!controls.dragging) return;
const dx = event.clientX - controls.previousX;
const dy = event.clientY - controls.previousY;
controls.previousX = event.clientX;
controls.previousY = event.clientY;
if (controls.panning) {
camera.updateMatrixWorld();
const right = new THREE.Vector3().setFromMatrixColumn(camera.matrix, 0);
const up = new THREE.Vector3().setFromMatrixColumn(camera.matrix, 1);
const panScale = controls.spherical.radius * 0.0014;
controls.target
.addScaledVector(right, -dx * panScale)
.addScaledVector(up, dy * panScale);
controls.target.x = THREE.MathUtils.clamp(
controls.target.x,
-8,
8
);
controls.target.y = THREE.MathUtils.clamp(
controls.target.y,
-2.5,
4
);
} else {
controls.spherical.theta -= dx * 0.006;
controls.spherical.phi -= dy * 0.006;
}
updateCamera();
});
function endDrag(event) {
controls.dragging = false;
if (canvas.hasPointerCapture(event.pointerId)) {
canvas.releasePointerCapture(event.pointerId);
}
}
canvas.addEventListener("pointerup", endDrag);
canvas.addEventListener("pointercancel", endDrag);
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
controls.spherical.radius *= Math.exp(event.deltaY * 0.001);
updateCamera();
},
{ passive: false }
);
canvas.addEventListener("dblclick", () => {
resetCamera();
updateCamera();
});
// -----------------------------------------------------------------------
// UI controls
// -----------------------------------------------------------------------
pauseButton.addEventListener("click", () => {
paused = !paused;
pauseButton.textContent = paused ? "Play" : "Pause";
});
replayButton.addEventListener("click", () => {
simulationTime = 0;
paused = false;
pauseButton.textContent = "Pause";
});
speedInput.addEventListener("input", () => {
animationSpeed = Number(speedInput.value);
speedValue.textContent = `${animationSpeed.toFixed(2)}×`;
});
// -----------------------------------------------------------------------
// Resize and animation loop
// -----------------------------------------------------------------------
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
});
const clock = new THREE.Clock();
function animate() {
const delta = Math.min(clock.getDelta(), 0.05);
if (!paused) {
simulationTime += delta * animationSpeed;
}
updateWave();
updatePressureProfile();
updateCamera();
updateLabels();
renderer.render(scene, camera);
}
updateWave();
updatePressureProfile();
renderer.setAnimationLoop(animate);
</script>
</body>
</html>
```