Prompt specification
Build or customize the Three.js 3D scene "Pipe narrowing with tracer rings" 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 narrowing with tracer rings
Category: Science & Physics · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/pipe-narrowing-with-tracer-rings-x1453
Scene Description Prompt:
Pipe narrowing with tracer rings. Build a transparent pipe with wide and narrow sections and evenly timed tracer injections; show faster motion in the narrow section under steady incompressible flow. Please build this with Three.js from cdnjs in one self-contained HTML file, using low-poly forms.
Working Three.js Starter (pipe-narrowing-with-tracer-rings-x1453):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Pipe Narrowing — Tracer Rings</title>
<style>
:root {
--bg: #07111f;
--panel: rgba(10, 24, 40, 0.82);
--border: rgba(130, 217, 255, 0.22);
--text: #eaf7ff;
--muted: #91adbf;
--cyan: #62d7ff;
--orange: #ffad42;
}
* {
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;
}
canvas {
display: block;
touch-action: none;
cursor: grab;
}
canvas.dragging {
cursor: grabbing;
}
.panel {
position: fixed;
top: 20px;
left: 20px;
width: min(430px, calc(100vw - 40px));
padding: 20px;
border: 1px solid var(--border);
border-radius: 18px;
background:
linear-gradient(135deg, rgba(98, 215, 255, 0.07), transparent 45%),
var(--panel);
box-shadow: 0 20px 60px rgba(0, 0, 0, 0.28);
backdrop-filter: blur(14px);
pointer-events: none;
user-select: none;
}
.eyebrow {
color: var(--cyan);
font-size: 11px;
font-weight: 800;
letter-spacing: 0.18em;
text-transform: uppercase;
}
h1 {
margin: 8px 0 10px;
font-size: clamp(22px, 3vw, 30px);
line-height: 1.08;
letter-spacing: -0.035em;
}
.description {
margin: 0;
color: var(--muted);
font-size: 13px;
line-height: 1.55;
}
.metric-grid {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 10px;
margin-top: 16px;
}
.metric {
position: relative;
overflow: hidden;
padding: 12px;
border: 1px solid rgba(145, 173, 191, 0.16);
border-radius: 12px;
background: rgba(255, 255, 255, 0.035);
}
.metric::after {
position: absolute;
right: -20px;
bottom: -30px;
width: 90px;
height: 90px;
border-radius: 50%;
background: rgba(98, 215, 255, 0.08);
content: "";
}
.metric.narrow::after {
background: rgba(255, 173, 66, 0.11);
}
.metric-label {
display: block;
color: var(--muted);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.12em;
text-transform: uppercase;
}
.metric strong {
display: block;
margin-top: 6px;
font-size: 24px;
letter-spacing: -0.04em;
}
.metric small {
display: block;
margin-top: 3px;
color: var(--muted);
font-size: 11px;
}
.speed-row {
display: grid;
grid-template-columns: 46px 1fr 50px;
align-items: center;
gap: 8px;
margin-top: 9px;
color: var(--muted);
font-size: 10px;
}
.bar {
height: 5px;
overflow: hidden;
border-radius: 999px;
background: rgba(255, 255, 255, 0.08);
}
.bar i {
display: block;
height: 100%;
border-radius: inherit;
background: var(--cyan);
}
.speed-row.narrow .bar i {
background: var(--orange);
}
.speed-row b {
color: var(--text);
text-align: right;
}
.equation {
display: flex;
align-items: center;
justify-content: space-between;
gap: 14px;
margin-top: 15px;
padding-top: 14px;
border-top: 1px solid rgba(145, 173, 191, 0.15);
}
.equation-main {
font-family: Georgia, "Times New Roman", serif;
font-size: 17px;
white-space: nowrap;
}
.equation-note {
color: var(--muted);
font-size: 10px;
line-height: 1.4;
text-align: right;
}
.legend {
display: flex;
align-items: center;
gap: 9px;
margin-top: 13px;
color: var(--muted);
font-size: 11px;
}
.legend-ring {
width: 17px;
height: 17px;
border: 3px solid var(--orange);
border-radius: 50%;
box-shadow: 0 0 10px rgba(255, 173, 66, 0.35);
}
.world-label {
position: fixed;
z-index: 5;
min-width: 118px;
padding: 9px 11px;
border: 1px solid rgba(98, 215, 255, 0.35);
border-radius: 10px;
background: rgba(7, 17, 31, 0.82);
box-shadow: 0 8px 24px rgba(0, 0, 0, 0.2);
backdrop-filter: blur(8px);
pointer-events: none;
transform: translate(-50%, -50%);
user-select: none;
}
.world-label.narrow-label {
border-color: rgba(255, 173, 66, 0.48);
}
.world-label span {
display: block;
color: var(--cyan);
font-size: 9px;
font-weight: 900;
letter-spacing: 0.14em;
}
.world-label.narrow-label span {
color: var(--orange);
}
.world-label strong {
display: block;
margin-top: 3px;
font-size: 13px;
}
.world-label small {
color: var(--muted);
font-size: 10px;
}
.controls {
position: fixed;
right: 20px;
bottom: 20px;
display: flex;
gap: 8px;
}
button {
padding: 10px 14px;
border: 1px solid rgba(130, 217, 255, 0.28);
border-radius: 10px;
background: rgba(10, 24, 40, 0.86);
color: var(--text);
font: inherit;
font-size: 12px;
font-weight: 700;
cursor: pointer;
backdrop-filter: blur(10px);
transition: border-color 160ms, background 160ms, transform 160ms;
}
button:hover {
border-color: var(--cyan);
background: rgba(23, 49, 72, 0.92);
transform: translateY(-1px);
}
.hint {
position: fixed;
left: 20px;
bottom: 22px;
color: rgba(194, 220, 235, 0.6);
font-size: 11px;
pointer-events: none;
user-select: none;
}
@media (max-width: 700px) {
.panel {
top: 12px;
left: 12px;
width: calc(100vw - 24px);
padding: 15px;
}
.description,
.equation-note,
.legend,
.world-label,
.hint {
display: none;
}
.metric-grid {
margin-top: 12px;
}
.metric strong {
font-size: 20px;
}
.controls {
right: 12px;
bottom: 12px;
}
}
</style>
</head>
<body>
<div id="scene"></div>
<section class="panel">
<div class="eyebrow">Continuity Lab / Steady Incompressible Flow</div>
<h1>When the pipe narrows,<br>the tracer accelerates.</h1>
<p class="description">
Tracer rings are injected at equal time intervals. The pipe area decreases,
so their speed increases and their spacing compresses.
</p>
<div class="metric-grid">
<article class="metric">
<span class="metric-label">Wide section</span>
<strong id="wideArea">—</strong>
<small>cross-sectional area</small>
</article>
<article class="metric narrow">
<span class="metric-label">Narrow section</span>
<strong id="narrowArea">—</strong>
<small>cross-sectional area</small>
</article>
</div>
<div class="speed-row">
<span>WIDE</span>
<div class="bar"><i style="width: 22.9%"></i></div>
<b>1.00×</b>
</div>
<div class="speed-row narrow">
<span>NARROW</span>
<div class="bar"><i style="width: 100%"></i></div>
<b id="speedRatio">—</b>
</div>
<div class="equation">
<div class="equation-main">
A<sub>wide</sub>v<sub>wide</sub> =
A<sub>narrow</sub>v<sub>narrow</sub>
</div>
<div class="equation-note">
Q = Av is constant<br>
Injection interval: <b id="intervalValue">—</b>
</div>
</div>
<div class="legend">
<span class="legend-ring"></span>
<span>Amber ring = one tracer pulse · steady injections every interval</span>
</div>
</section>
<div id="wideLabel" class="world-label">
<span>WIDE</span>
<strong>1.00× speed</strong>
<small id="wideLabelArea">—</small>
</div>
<div id="narrowLabel" class="world-label narrow-label">
<span>NARROW</span>
<strong id="narrowLabelSpeed">—</strong>
<small id="narrowLabelArea">—</small>
</div>
<div class="controls">
<button id="pauseButton" type="button" aria-pressed="false">Pause</button>
<button id="resetButton" type="button">Reset view</button>
</div>
<div class="hint">Drag to orbit · Scroll or pinch to zoom</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// -------------------------------------------------------------------------
// Renderer, camera, scene
// -------------------------------------------------------------------------
const host = 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.12;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
host.appendChild(renderer.domElement);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x07111f);
scene.fog = new THREE.Fog(0x07111f, 19, 38);
const camera = new THREE.PerspectiveCamera(
42,
window.innerWidth / window.innerHeight,
0.1,
100
);
const cameraState = {
theta: 0.14,
phi: 1.22,
radius: window.innerWidth < 700 ? 29 : 17.8,
target: new THREE.Vector3(0, 0.15, 0)
};
function updateCamera() {
const sinPhi = Math.sin(cameraState.phi);
camera.position.set(
cameraState.target.x +
cameraState.radius * sinPhi * Math.cos(cameraState.theta),
cameraState.target.y +
cameraState.radius * Math.cos(cameraState.phi),
cameraState.target.z +
cameraState.radius * sinPhi * Math.sin(cameraState.theta)
);
camera.lookAt(cameraState.target);
camera.updateMatrixWorld();
}
updateCamera();
// -------------------------------------------------------------------------
// Lighting and low-poly environment
// -------------------------------------------------------------------------
const hemisphere = new THREE.HemisphereLight(0xa7e4ff, 0x08111c, 1.7);
scene.add(hemisphere);
const keyLight = new THREE.DirectionalLight(0xffffff, 2.8);
keyLight.position.set(4, 10, 8);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(1024, 1024);
keyLight.shadow.camera.left = -12;
keyLight.shadow.camera.right = 12;
keyLight.shadow.camera.top = 8;
keyLight.shadow.camera.bottom = -8;
scene.add(keyLight);
const rimLight = new THREE.DirectionalLight(0x39c7ff, 2.1);
rimLight.position.set(-8, 4, -7);
scene.add(rimLight);
const warmLight = new THREE.DirectionalLight(0xff9c4b, 1.25);
warmLight.position.set(7, 2, 5);
scene.add(warmLight);
const floorY = -1.65;
const floor = new THREE.Mesh(
new THREE.PlaneGeometry(54, 54),
new THREE.MeshStandardMaterial({
color: 0x091522,
roughness: 0.92,
metalness: 0.08
})
);
floor.rotation.x = -Math.PI / 2;
floor.position.y = floorY;
floor.receiveShadow = true;
scene.add(floor);
const grid = new THREE.GridHelper(42, 42, 0x286985, 0x17384c);
grid.position.y = floorY + 0.012;
grid.material.transparent = true;
grid.material.opacity = 0.28;
scene.add(grid);
const supportMaterial = new THREE.MeshStandardMaterial({
color: 0x243d54,
roughness: 0.65,
metalness: 0.38,
flatShading: true
});
const baseMaterial = new THREE.MeshStandardMaterial({
color: 0x132638,
roughness: 0.75,
metalness: 0.25,
flatShading: true
});
// -------------------------------------------------------------------------
// Pipe geometry and physical flow model
//
// For steady incompressible flow:
//
// Q = A v = constant
// v(x) = baseSpeed * (rWide / r(x))^2
//
// Since A = pi*r^2, narrowing the radius increases speed by the
// inverse area ratio.
// -------------------------------------------------------------------------
const PIPE_Y = 0.55;
const WALL = 0.12;
const FLOW = {
xMin: -7,
xMax: 7,
taperStart: -1.45,
taperEnd: 1.25,
rWide: 1.15,
rNarrow: 0.55,
baseSpeed: 1.15,
injectionInterval: 0.72
};
function radiusAt(x) {
if (x <= FLOW.taperStart) return FLOW.rWide;
if (x >= FLOW.taperEnd) return FLOW.rNarrow;
const u =
(x - FLOW.taperStart) /
(FLOW.taperEnd - FLOW.taperStart);
return THREE.MathUtils.lerp(
FLOW.rWide,
FLOW.rNarrow,
u
);
}
function speedAt(x) {
return (
FLOW.baseSpeed *
Math.pow(FLOW.rWide / radiusAt(x), 2)
);
}
const areaRatio =
Math.pow(FLOW.rWide / FLOW.rNarrow, 2);
const wideArea = Math.PI * FLOW.rWide * FLOW.rWide;
const narrowArea = Math.PI * FLOW.rNarrow * FLOW.rNarrow;
document.getElementById("wideArea").textContent =
wideArea.toFixed(2) + " u²";
document.getElementById("narrowArea").textContent =
narrowArea.toFixed(2) + " u²";
document.getElementById("speedRatio").textContent =
areaRatio.toFixed(2) + "×";
document.getElementById("intervalValue").textContent =
FLOW.injectionInterval.toFixed(2) + " s";
document.getElementById("wideLabelArea").textContent =
"Area " + wideArea.toFixed(2) + " u²";
document.getElementById("narrowLabelArea").textContent =
"Area " + narrowArea.toFixed(2) + " u²";
document.getElementById("narrowLabelSpeed").textContent =
areaRatio.toFixed(2) + "× speed";
const glassMaterial = new THREE.MeshPhongMaterial({
color: 0x5bd4ff,
specular: 0xb8efff,
shininess: 110,
transparent: true,
opacity: 0.16,
side: THREE.DoubleSide,
depthWrite: false,
flatShading: true
});
const fluidMaterial = new THREE.MeshBasicMaterial({
color: 0x188ac4,
transparent: true,
opacity: 0.045,
side: THREE.DoubleSide,
depthWrite: false
});
const ribMaterial = new THREE.MeshBasicMaterial({
color: 0x8be7ff,
transparent: true,
opacity: 0.25,
depthWrite: false
});
const collarMaterial = new THREE.MeshBasicMaterial({
color: 0xb8f1ff,
transparent: true,
opacity: 0.6,
depthWrite: false
});
function addPipeSegment(x1, x2, r1, r2, material, order) {
// CylinderGeometry is aligned along Y, then rotated onto the X axis.
const geometry = new THREE.CylinderGeometry(
r2,
r1,
x2 - x1,
12,
1,
true
);
const mesh = new THREE.Mesh(geometry, material);
mesh.rotation.z = -Math.PI / 2;
mesh.position.set((x1 + x2) / 2, PIPE_Y, 0);
mesh.renderOrder = order;
scene.add(mesh);
return mesh;
}
// Transparent outer pipe.
addPipeSegment(
FLOW.xMin,
FLOW.taperStart,
FLOW.rWide + WALL,
FLOW.rWide + WALL,
glassMaterial,
4
);
addPipeSegment(
FLOW.taperStart,
FLOW.taperEnd,
FLOW.rWide + WALL,
FLOW.rNarrow + WALL,
glassMaterial,
4
);
addPipeSegment(
FLOW.taperEnd,
FLOW.xMax,
FLOW.rNarrow + WALL,
FLOW.rNarrow + WALL,
glassMaterial,
4
);
// Very faint fluid volumes.
addPipeSegment(
FLOW.xMin,
FLOW.taperStart,
FLOW.rWide,
FLOW.rWide,
fluidMaterial,
2
);
addPipeSegment(
FLOW.taperStart,
FLOW.taperEnd,
FLOW.rWide,
FLOW.rNarrow,
fluidMaterial,
2
);
addPipeSegment(
FLOW.taperEnd,
FLOW.xMax,
FLOW.rNarrow,
FLOW.rNarrow,
fluidMaterial,
2
);
function addTorus(x, radius, tube, material, order = 5) {
const mesh = new THREE.Mesh(
new THREE.TorusGeometry(radius, tube, 5, 18),
material
);
// Torus normal is Z; rotate it so its normal follows the pipe's X axis.
mesh.rotation.y = Math.PI / 2;
mesh.position.set(x, PIPE_Y, 0);
mesh.renderOrder = order;
scene.add(mesh);
return mesh;
}
// Low-poly transverse pipe ribs.
for (let x = FLOW.xMin + 0.24; x <= FLOW.xMax - 0.2; x += 0.48) {
addTorus(
x,
radiusAt(x) + WALL * 0.55,
0.018,
ribMaterial,
5
);
}
// Bright collars at openings and reducer transitions.
addTorus(
FLOW.xMin,
FLOW.rWide + WALL * 0.55,
0.085,
collarMaterial
);
addTorus(
FLOW.taperStart,
FLOW.rWide + WALL * 0.55,
0.065,
collarMaterial
);
addTorus(
FLOW.taperEnd,
FLOW.rNarrow + WALL * 0.55,
0.065,
collarMaterial
);
addTorus(
FLOW.xMax,
FLOW.rNarrow + WALL * 0.55,
0.075,
collarMaterial
);
// Longitudinal edge lines emphasize the transparent faceted shell.
const longitudinalMaterial = new THREE.LineBasicMaterial({
color: 0x80dfff,
transparent: true,
opacity: 0.18,
depthWrite: false
});
for (let edge = 0; edge < 8; edge++) {
const angle = (edge / 8) * Math.PI * 2;
const points = [];
for (let i = 0; i <= 100; i++) {
const x = THREE.MathUtils.lerp(
FLOW.xMin,
FLOW.xMax,
i / 100
);
const radius = radiusAt(x) + WALL;
points.push(
new THREE.Vector3(
x,
PIPE_Y + Math.cos(angle) * radius,
Math.sin(angle) * radius
)
);
}
const line = new THREE.Line(
new THREE.BufferGeometry().setFromPoints(points),
longitudinalMaterial
);
line.renderOrder = 5;
scene.add(line);
}
// Pipe supports.
function addSupport(x) {
const outerRadius = radiusAt(x) + WALL;
const pipeBottom = PIPE_Y - outerRadius;
const height = pipeBottom - floorY;
const post = new THREE.Mesh(
new THREE.BoxGeometry(0.23, height, 0.27),
supportMaterial
);
post.position.set(x, floorY + height / 2, 0);
post.castShadow = true;
post.receiveShadow = true;
scene.add(post);
const saddle = new THREE.Mesh(
new THREE.BoxGeometry(0.56, 0.16, 0.72),
supportMaterial
);
saddle.position.set(x, pipeBottom - 0.04, 0);
saddle.castShadow = true;
scene.add(saddle);
const foot = new THREE.Mesh(
new THREE.BoxGeometry(0.88, 0.12, 0.78),
baseMaterial
);
foot.position.set(x, floorY + 0.06, 0);
foot.castShadow = true;
foot.receiveShadow = true;
scene.add(foot);
}
addSupport(-5.55);
addSupport(5.35);
// -------------------------------------------------------------------------
// Travel-time lookup
//
// The tracer's age is converted to position using the integral of dx/v.
// This makes the displayed motion exactly follow v(x) = Q/A(x).
// -------------------------------------------------------------------------
const LUT_SIZE = 1600;
const travelTimes = new Float64Array(LUT_SIZE + 1);
const lutDx = (FLOW.xMax - FLOW.xMin) / LUT_SIZE;
let accumulatedTime = 0;
for (let i = 1; i <= LUT_SIZE; i++) {
const x0 = FLOW.xMin + (i - 1) * lutDx;
const x1 = FLOW.xMin + i * lutDx;
accumulatedTime +=
0.5 *
(1 / speedAt(x0) + 1 / speedAt(x1)) *
lutDx;
travelTimes[i] = accumulatedTime;
}
const totalTravelTime = travelTimes[LUT_SIZE];
function xAtTravelTime(time) {
if (time <= 0) return FLOW.xMin;
if (time >= totalTravelTime) return FLOW.xMax;
let low = 0;
let high = LUT_SIZE;
while (low < high) {
const middle = Math.floor((low + high) / 2);
if (travelTimes[middle] < time) {
low = middle + 1;
} else {
high = middle;
}
}
const index = Math.max(1, low);
const t0 = travelTimes[index - 1];
const t1 = travelTimes[index];
const alpha =
t1 === t0 ? 0 : (time - t0) / (t1 - t0);
return THREE.MathUtils.lerp(
FLOW.xMin + (index - 1) * lutDx,
FLOW.xMin + index * lutDx,
alpha
);
}
// -------------------------------------------------------------------------
// Tracer rings
// -------------------------------------------------------------------------
const tracerGeometry = new THREE.TorusGeometry(
1,
0.065,
5,
18
);
const tracerPool = [];
function createTracer() {
const material = new THREE.MeshStandardMaterial({
color: 0xffb44b,
emissive: 0xff541f,
emissiveIntensity: 1.8,
roughness: 0.32,
metalness: 0.08,
transparent: true,
opacity: 1,
depthWrite: false,
flatShading: true
});
const mesh = new THREE.Mesh(tracerGeometry, material);
mesh.rotation.y = Math.PI / 2;
mesh.renderOrder = 8;
mesh.visible = false;
scene.add(mesh);
return {
mesh,
material,
age: 0
};
}
function spawnTracer(age = 0) {
let tracer = tracerPool.find((item) => !item.mesh.visible);
if (!tracer) {
tracer = createTracer();
tracerPool.push(tracer);
}
tracer.age = age;
tracer.mesh.visible = true;
updateTracerVisual(tracer);
return tracer;
}
function updateTracerVisual(tracer) {
const x = xAtTravelTime(tracer.age);
const localRadius = radiusAt(x);
tracer.mesh.position.set(x, PIPE_Y, 0);
// Local X/Y scale the torus radius. Local Z remains along the pipe,
// preserving a useful ring thickness as the pipe narrows.
tracer.mesh.scale.set(
localRadius * 0.76,
localRadius * 0.76,
1
);
const fadeIn = THREE.MathUtils.smoothstep(
tracer.age,
0,
0.18
);
const fadeOut = 1 - THREE.MathUtils.smoothstep(
tracer.age,
totalTravelTime - 0.28,
totalTravelTime
);
tracer.material.opacity = Math.max(
0,
Math.min(fadeIn, fadeOut)
);
}
// Seed a steady stream so the pipe begins full rather than empty.
for (
let age = 0;
age < totalTravelTime;
age += FLOW.injectionInterval
) {
spawnTracer(age);
}
// Injector pulse at the inlet.
const injectorMaterial = new THREE.MeshBasicMaterial({
color: 0x8cecff,
transparent: true,
opacity: 0.65,
depthWrite: false
});
const injectorRing = addTorus(
FLOW.xMin + 0.055,
FLOW.rWide * 0.91,
0.045,
injectorMaterial,
7
);
const injectorLight = new THREE.PointLight(
0xff7b32,
2,
4,
2
);
injectorLight.position.set(
FLOW.xMin + 0.2,
PIPE_Y,
0.25
);
scene.add(injectorLight);
let injectionPulse = 0;
// -------------------------------------------------------------------------
// World-space HTML labels
// -------------------------------------------------------------------------
const wideLabel = document.getElementById("wideLabel");
const narrowLabel = document.getElementById("narrowLabel");
const wideAnchor = new THREE.Vector3(
-4.2,
PIPE_Y + 1.85,
0
);
const narrowAnchor = new THREE.Vector3(
4.0,
PIPE_Y + 1.15,
0
);
function positionLabel(element, anchor) {
const projected = anchor.clone().project(camera);
const visible =
projected.z > -1 &&
projected.z < 1 &&
Math.abs(projected.x) < 1.25 &&
Math.abs(projected.y) < 1.25;
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";
}
// -------------------------------------------------------------------------
// Direct camera controls: drag to orbit, wheel/pinch to zoom
// -------------------------------------------------------------------------
const pointers = new Map();
let lastPinchDistance = 0;
renderer.domElement.addEventListener("pointerdown", (event) => {
renderer.domElement.setPointerCapture(event.pointerId);
pointers.set(event.pointerId, {
x: event.clientX,
y: event.clientY
});
renderer.domElement.classList.add("dragging");
if (pointers.size === 2) {
const points = [...pointers.values()];
lastPinchDistance = Math.hypot(
points[0].x - points[1].x,
points[0].y - points[1].y
);
}
});
renderer.domElement.addEventListener("pointermove", (event) => {
const previous = pointers.get(event.pointerId);
if (!previous) return;
const current = {
x: event.clientX,
y: event.clientY
};
pointers.set(event.pointerId, current);
if (pointers.size >= 2) {
const points = [...pointers.values()];
const distance = Math.hypot(
points[0].x - points[1].x,
points[0].y - points[1].y
);
if (lastPinchDistance > 0 && distance > 0) {
cameraState.radius *=
lastPinchDistance / distance;
cameraState.radius = THREE.MathUtils.clamp(
cameraState.radius,
8.5,
34
);
}
lastPinchDistance = distance;
} else {
const dx = current.x - previous.x;
const dy = current.y - previous.y;
cameraState.theta -= dx * 0.006;
cameraState.phi = THREE.MathUtils.clamp(
cameraState.phi - dy * 0.005,
0.3,
1.48
);
}
});
function endPointer(event) {
pointers.delete(event.pointerId);
if (pointers.size < 2) {
lastPinchDistance = 0;
}
if (pointers.size === 0) {
renderer.domElement.classList.remove("dragging");
}
}
renderer.domElement.addEventListener("pointerup", endPointer);
renderer.domElement.addEventListener("pointercancel", endPointer);
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraState.radius *= Math.exp(event.deltaY * 0.001);
cameraState.radius = THREE.MathUtils.clamp(
cameraState.radius,
8.5,
34
);
},
{ passive: false }
);
const pauseButton = document.getElementById("pauseButton");
const resetButton = document.getElementById("resetButton");
let paused = false;
pauseButton.addEventListener("click", () => {
paused = !paused;
pauseButton.textContent = paused ? "Resume" : "Pause";
pauseButton.setAttribute("aria-pressed", String(paused));
});
resetButton.addEventListener("click", () => {
cameraState.theta = 0.14;
cameraState.phi = 1.22;
cameraState.radius = window.innerWidth < 700 ? 29 : 17.8;
});
window.addEventListener("keydown", (event) => {
if (event.code === "Space") {
event.preventDefault();
pauseButton.click();
}
if (event.key.toLowerCase() === "r") {
resetButton.click();
}
});
// -------------------------------------------------------------------------
// Animation
// -------------------------------------------------------------------------
const clock = new THREE.Clock();
let simulationTime = 0;
let nextInjection = FLOW.injectionInterval;
function animate() {
const dt = Math.min(clock.getDelta(), 0.05);
if (!paused) {
simulationTime += dt;
for (const tracer of tracerPool) {
if (!tracer.mesh.visible) continue;
tracer.age += dt;
if (tracer.age >= totalTravelTime) {
tracer.mesh.visible = false;
continue;
}
updateTracerVisual(tracer);
}
while (simulationTime + 1e-8 >= nextInjection) {
spawnTracer(0);
injectionPulse = 1;
nextInjection += FLOW.injectionInterval;
}
}
injectionPulse = Math.max(
0,
injectionPulse - dt * 3.5
);
injectorRing.scale.setScalar(1 + injectionPulse * 0.13);
injectorMaterial.opacity = 0.45 + injectionPulse * 0.55;
injectorLight.intensity = 1 + injectionPulse * 7;
updateCamera();
positionLabel(wideLabel, wideAnchor);
positionLabel(narrowLabel, narrowAnchor);
renderer.render(scene, camera);
}
renderer.setAnimationLoop(animate);
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setPixelRatio(
Math.min(window.devicePixelRatio, 2)
);
renderer.setSize(
window.innerWidth,
window.innerHeight
);
});
</script>
</body>
</html>
```