Prompt specification
Build or customize the Three.js 3D scene "Moving observer versus moving source 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: Moving observer versus moving source comparison
Category: Science & Physics · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/moving-observer-versus-moving-source-comparison-x1538
Scene Description Prompt:
Compare stylized Doppler scenes with a moving source or moving observer in a stationary medium. Count wave arrivals. Three.js from cdnjs, standalone HTML.
Working Three.js Starter (moving-observer-versus-moving-source-comparison-x1538):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Doppler Arrival Lab</title>
<style>
:root {
color-scheme: dark;
--bg: #050b12;
--panel: rgba(8, 18, 29, 0.72);
--line: rgba(130, 194, 219, 0.18);
--text: #eaf7ff;
--muted: #89a9bb;
--source: #ffad5c;
--observer: #52e5ff;
}
* { box-sizing: border-box; }
html, body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: var(--bg);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
color: var(--text);
}
#scene {
position: fixed;
inset: 0;
display: block;
width: 100%;
height: 100%;
touch-action: none;
cursor: grab;
}
#scene:active { cursor: grabbing; }
#topbar {
position: fixed;
z-index: 20;
top: 16px;
left: 20px;
right: 20px;
display: flex;
align-items: center;
justify-content: space-between;
gap: 20px;
pointer-events: none;
}
.brand {
display: flex;
align-items: center;
gap: 11px;
min-width: 0;
}
.brand-mark {
width: 11px;
height: 34px;
border-radius: 10px;
background:
linear-gradient(to top, var(--source) 0 48%, transparent 48% 52%,
var(--observer) 52% 100%);
box-shadow: 0 0 22px rgba(82, 229, 255, 0.28);
}
.brand b {
display: block;
font-size: 14px;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.brand small {
display: block;
margin-top: 3px;
color: var(--muted);
font-size: 11px;
}
.controls {
display: flex;
align-items: center;
gap: 8px;
padding: 7px;
border: 1px solid var(--line);
border-radius: 13px;
background: rgba(5, 13, 22, 0.82);
backdrop-filter: blur(15px);
pointer-events: auto;
}
button {
border: 1px solid rgba(132, 194, 218, 0.2);
border-radius: 8px;
padding: 7px 10px;
background: rgba(19, 39, 53, 0.8);
color: var(--text);
font: inherit;
font-size: 12px;
cursor: pointer;
}
button:hover {
border-color: rgba(82, 229, 255, 0.55);
background: rgba(29, 57, 73, 0.9);
}
.speed {
display: flex;
align-items: center;
gap: 7px;
padding: 0 5px 0 9px;
color: var(--muted);
font-size: 11px;
}
.speed input {
width: 80px;
accent-color: var(--observer);
}
.speed output {
min-width: 31px;
color: var(--text);
font-variant-numeric: tabular-nums;
}
#overlay {
position: fixed;
inset: 0;
z-index: 5;
display: grid;
grid-template-columns: 1fr 1fr;
pointer-events: none;
}
.panel {
position: relative;
min-width: 0;
min-height: 0;
border-right: 1px solid var(--line);
background:
linear-gradient(180deg, rgba(4, 11, 18, 0.38), transparent 28%),
linear-gradient(0deg, rgba(4, 11, 18, 0.35), transparent 38%);
}
.panel:last-child { border-right: 0; }
.panel-head {
position: absolute;
top: 86px;
left: 22px;
right: 22px;
display: flex;
align-items: flex-start;
gap: 12px;
}
.scene-no {
display: grid;
place-items: center;
flex: 0 0 34px;
height: 34px;
border: 1px solid rgba(255, 173, 92, 0.4);
border-radius: 10px;
color: var(--source);
background: rgba(255, 173, 92, 0.07);
font-size: 13px;
font-weight: 750;
}
.panel h2 {
margin: 0;
font-size: clamp(18px, 2.2vw, 27px);
line-height: 1.05;
letter-spacing: -0.03em;
}
.panel p {
margin: 7px 0 0;
color: var(--muted);
font-size: 11px;
}
.motion-pill {
margin-left: auto;
padding: 6px 9px;
border: 1px solid rgba(82, 229, 255, 0.25);
border-radius: 999px;
color: var(--observer);
background: rgba(82, 229, 255, 0.06);
font-size: 9px;
font-weight: 750;
letter-spacing: 0.1em;
text-transform: uppercase;
}
.stats {
position: absolute;
left: 22px;
right: 22px;
bottom: 22px;
display: grid;
grid-template-columns: minmax(110px, 0.9fr) 1.5fr;
gap: 14px;
align-items: end;
padding: 15px 16px;
border: 1px solid var(--line);
border-radius: 16px;
background: var(--panel);
backdrop-filter: blur(15px);
box-shadow: 0 16px 45px rgba(0, 0, 0, 0.22);
}
.arrival-label {
display: block;
margin-bottom: 5px;
color: var(--muted);
font-size: 9px;
font-weight: 750;
letter-spacing: 0.12em;
}
#count-source, #count-observer {
display: block;
color: var(--observer);
font-size: clamp(32px, 4vw, 48px);
line-height: 0.95;
font-weight: 800;
font-variant-numeric: tabular-nums;
text-shadow: 0 0 22px rgba(82, 229, 255, 0.24);
}
.mini-stats {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 8px;
}
.mini-stat {
min-width: 0;
padding-left: 9px;
border-left: 1px solid var(--line);
}
.mini-stat span {
display: block;
margin-bottom: 6px;
color: var(--muted);
font-size: 8px;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.mini-stat b {
display: block;
overflow: hidden;
font-size: 11px;
white-space: nowrap;
font-variant-numeric: tabular-nums;
}
.approach { color: #78ffbf; }
.recede { color: #ff8d8d; }
@media (max-width: 760px) {
#topbar {
top: 8px;
left: 10px;
right: 10px;
}
.brand small { display: none; }
.brand b { font-size: 11px; }
.brand-mark { height: 28px; }
.controls {
gap: 5px;
padding: 5px;
}
button {
padding: 6px 8px;
font-size: 10px;
}
.speed {
padding-left: 5px;
font-size: 0;
}
.speed input { width: 58px; }
#overlay {
grid-template-columns: 1fr;
grid-template-rows: 1fr 1fr;
}
.panel {
border-right: 0;
border-bottom: 1px solid var(--line);
}
.panel:last-child { border-bottom: 0; }
.panel-head {
top: 60px;
left: 14px;
right: 14px;
}
.panel h2 { font-size: 18px; }
.panel p { font-size: 9px; }
.stats {
left: 12px;
right: 12px;
bottom: 12px;
padding: 11px;
grid-template-columns: 90px 1fr;
gap: 8px;
}
.mini-stat { padding-left: 6px; }
.mini-stat span { font-size: 7px; }
.mini-stat b { font-size: 9px; }
}
</style>
</head>
<body>
<canvas id="scene" aria-label="Interactive Doppler wave simulation"></canvas>
<div id="topbar">
<div class="brand">
<div class="brand-mark"></div>
<div>
<b>Doppler Arrival Lab</b>
<small>Drag to orbit · wheel to zoom · double-click resets view</small>
</div>
</div>
<div class="controls">
<button id="pause" type="button">Pause</button>
<button id="reset" type="button">Reset</button>
<label class="speed">
Speed
<input id="speed" type="range" min="0.35" max="1.6" step="0.05" value="1">
<output id="speed-value">1.0×</output>
</label>
</div>
</div>
<div id="overlay">
<section class="panel" id="panel-source">
<div class="panel-head">
<div class="scene-no">A</div>
<div>
<h2>Moving source</h2>
<p>Stationary medium · fixed observer</p>
</div>
<div class="motion-pill">Source moves</div>
</div>
<div class="stats" id="stats-source">
<div>
<span class="arrival-label">Wave arrivals</span>
<strong id="count-source">0</strong>
</div>
<div class="mini-stats">
<div class="mini-stat">
<span>Closing speed</span>
<b id="closing-source">+0.00 u/s</b>
</div>
<div class="mini-stat">
<span>Radial shift</span>
<b id="shift-source">×1.00</b>
</div>
<div class="mini-stat">
<span>Last arrival</span>
<b id="last-source">—</b>
</div>
</div>
</div>
</section>
<section class="panel" id="panel-observer">
<div class="panel-head">
<div class="scene-no">B</div>
<div>
<h2>Moving observer</h2>
<p>Stationary medium · fixed source</p>
</div>
<div class="motion-pill">Observer moves</div>
</div>
<div class="stats" id="stats-observer">
<div>
<span class="arrival-label">Wave arrivals</span>
<strong id="count-observer">0</strong>
</div>
<div class="mini-stats">
<div class="mini-stat">
<span>Closing speed</span>
<b id="closing-observer">+0.00 u/s</b>
</div>
<div class="mini-stat">
<span>Radial shift</span>
<b id="shift-observer">×1.00</b>
</div>
<div class="mini-stat">
<span>Last arrival</span>
<b id="last-observer">—</b>
</div>
</div>
</div>
</section>
</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const canvas = document.querySelector("#scene");
const renderer = new THREE.WebGLRenderer({
canvas,
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio || 1, 2));
renderer.setSize(window.innerWidth, window.innerHeight, false);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.12;
const WAVE_SPEED = 6.5;
const EMISSION_PERIOD = 1.0;
const MAX_WAVE_AGE = 4.8;
const PATH_RADIUS = 2.5;
const ANGULAR_SPEED = 0.6;
const SOURCE_COLOR = 0xffad5c;
const OBSERVER_COLOR = 0x52e5ff;
const WAVE_COLOR = 0xffb86b;
const ringGeometry = new THREE.RingGeometry(0.992, 1.008, 128);
const clamp = THREE.MathUtils.clamp;
function makeLabel(text, color) {
const labelCanvas = document.createElement("canvas");
labelCanvas.width = 512;
labelCanvas.height = 128;
const ctx = labelCanvas.getContext("2d");
ctx.clearRect(0, 0, 512, 128);
ctx.fillStyle = "rgba(4, 12, 20, 0.84)";
ctx.strokeStyle = color;
ctx.lineWidth = 3;
const x = 8;
const y = 18;
const w = 496;
const h = 86;
const r = 20;
ctx.beginPath();
ctx.moveTo(x + r, y);
ctx.lineTo(x + w - r, y);
ctx.quadraticCurveTo(x + w, y, x + w, y + r);
ctx.lineTo(x + w, y + h - r);
ctx.quadraticCurveTo(x + w, y + h, x + w - r, y + h);
ctx.lineTo(x + r, y + h);
ctx.quadraticCurveTo(x, y + h, x, y + h - r);
ctx.lineTo(x, y + r);
ctx.quadraticCurveTo(x, y, x + r, y);
ctx.closePath();
ctx.fill();
ctx.stroke();
ctx.fillStyle = color;
ctx.font = "700 31px system-ui, sans-serif";
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.fillText(text, 256, 63);
const texture = new THREE.CanvasTexture(labelCanvas);
texture.colorSpace = THREE.SRGBColorSpace;
const sprite = new THREE.Sprite(new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthTest: false,
depthWrite: false
}));
sprite.scale.set(2.75, 0.69, 1);
sprite.position.y = 1.08;
sprite.renderOrder = 20;
return sprite;
}
function makeSource(labelText) {
const group = new THREE.Group();
const material = new THREE.MeshStandardMaterial({
color: SOURCE_COLOR,
emissive: SOURCE_COLOR,
emissiveIntensity: 1.6,
roughness: 0.28,
metalness: 0.15
});
const core = new THREE.Mesh(
new THREE.IcosahedronGeometry(0.32, 2),
material
);
core.position.y = 0.34;
group.add(core);
const halo = new THREE.Mesh(
new THREE.TorusGeometry(0.48, 0.035, 10, 64),
new THREE.MeshBasicMaterial({
color: SOURCE_COLOR,
transparent: true,
opacity: 0.55,
depthWrite: false
})
);
halo.rotation.x = Math.PI / 2;
halo.position.y = 0.09;
group.add(halo);
group.add(makeLabel(labelText, "#ffbd7c"));
return { group, core, halo };
}
function makeObserver(labelText) {
const group = new THREE.Group();
const body = new THREE.Mesh(
new THREE.CylinderGeometry(0.34, 0.42, 0.28, 48),
new THREE.MeshStandardMaterial({
color: 0x167e99,
emissive: 0x0b5265,
emissiveIntensity: 1.15,
roughness: 0.35,
metalness: 0.2
})
);
body.position.y = 0.16;
group.add(body);
const eyeRing = new THREE.Mesh(
new THREE.TorusGeometry(0.48, 0.055, 12, 64),
new THREE.MeshStandardMaterial({
color: OBSERVER_COLOR,
emissive: OBSERVER_COLOR,
emissiveIntensity: 1.7,
roughness: 0.25
})
);
eyeRing.rotation.x = Math.PI / 2;
eyeRing.position.y = 0.33;
group.add(eyeRing);
const pupil = new THREE.Mesh(
new THREE.SphereGeometry(0.15, 24, 16),
new THREE.MeshBasicMaterial({ color: 0xc8fbff })
);
pupil.position.y = 0.37;
group.add(pupil);
const flashRing = new THREE.Mesh(
new THREE.TorusGeometry(0.61, 0.025, 10, 64),
new THREE.MeshBasicMaterial({
color: OBSERVER_COLOR,
transparent: true,
opacity: 0,
depthWrite: false,
blending: THREE.AdditiveBlending
})
);
flashRing.rotation.x = Math.PI / 2;
flashRing.position.y = 0.36;
group.add(flashRing);
group.add(makeLabel(labelText, "#83efff"));
return { group, flashRing };
}
function makePath(pathPosition, color) {
const points = [];
for (let i = 0; i <= 100; i++) {
const a = i / 100 * Math.PI * 2;
const p = pathPosition(a);
points.push(new THREE.Vector3(p.x, 0.025, p.z));
}
const geometry = new THREE.BufferGeometry().setFromPoints(points);
const material = new THREE.LineDashedMaterial({
color,
transparent: true,
opacity: 0.42,
dashSize: 0.24,
gapSize: 0.16
});
const line = new THREE.Line(geometry, material);
line.computeLineDistances();
return line;
}
function addStaticMedium(scene) {
const floor = new THREE.Mesh(
new THREE.PlaneGeometry(24, 18),
new THREE.MeshStandardMaterial({
color: 0x07131d,
roughness: 0.95,
metalness: 0.02
})
);
floor.rotation.x = -Math.PI / 2;
floor.position.y = -0.12;
scene.add(floor);
const grid = new THREE.GridHelper(22, 22, 0x28596c, 0x153542);
grid.position.y = -0.095;
grid.material.transparent = true;
grid.material.opacity = 0.62;
scene.add(grid);
const positions = [];
for (let i = 0; i < 170; i++) {
positions.push(
(Math.random() - 0.5) * 21,
-0.015 + Math.random() * 0.035,
(Math.random() - 0.5) * 15
);
}
const dustGeometry = new THREE.BufferGeometry();
dustGeometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(positions, 3)
);
const dust = new THREE.Points(
dustGeometry,
new THREE.PointsMaterial({
color: 0x4c8da1,
size: 0.035,
transparent: true,
opacity: 0.55,
depthWrite: false
})
);
scene.add(dust);
}
function addDirectionArrow(group, color) {
const arrow = new THREE.ArrowHelper(
new THREE.Vector3(1, 0, 0),
new THREE.Vector3(0, 0.25, 0),
1.25,
color,
0.28,
0.14
);
group.add(arrow);
return arrow;
}
function createScenario(config) {
const scene = new THREE.Scene();
scene.background = new THREE.Color(config.background);
scene.fog = new THREE.Fog(config.background, 19, 38);
const camera = new THREE.PerspectiveCamera(
42,
window.innerWidth / window.innerHeight,
0.1,
100
);
scene.add(new THREE.HemisphereLight(0xa4e4ff, 0x06101a, 1.7));
const key = new THREE.DirectionalLight(0xffffff, 2.1);
key.position.set(5, 10, 7);
scene.add(key);
const rim = new THREE.DirectionalLight(0x52e5ff, 1.15);
rim.position.set(-7, 5, -4);
scene.add(rim);
addStaticMedium(scene);
scene.add(makePath(config.pathPosition, config.movingColor));
const source = makeSource(config.sourceLabel);
const observer = makeObserver(config.observerLabel);
scene.add(source.group, observer.group);
const sourceArrow = config.moving === "source"
? addDirectionArrow(source.group, SOURCE_COLOR)
: null;
const observerArrow = config.moving === "observer"
? addDirectionArrow(observer.group, OBSERVER_COLOR)
: null;
const scenario = {
...config,
scene,
camera,
source,
observer,
sourceArrow,
observerArrow,
waves: [],
time: 0,
nextEmission: EMISSION_PERIOD,
count: 0,
lastArrival: -1,
flash: 0,
closingSpeed: 0
};
emitWave(scenario, config.sourcePosition(0));
updateScenario(scenario, 0);
return scenario;
}
const sourceMovingPosition = a => ({
x: -4 - PATH_RADIUS * Math.cos(a),
z: -PATH_RADIUS * Math.sin(a)
});
const sourceMovingVelocity = a => ({
x: PATH_RADIUS * Math.sin(a) * ANGULAR_SPEED,
z: -PATH_RADIUS * Math.cos(a) * ANGULAR_SPEED
});
const observerMovingPosition = a => ({
x: 4 + PATH_RADIUS * Math.cos(a),
z: PATH_RADIUS * Math.sin(a)
});
const observerMovingVelocity = a => ({
x: -PATH_RADIUS * Math.sin(a) * ANGULAR_SPEED,
z: PATH_RADIUS * Math.cos(a) * ANGULAR_SPEED
});
const scenarios = [
createScenario({
id: "source",
panel: document.querySelector("#panel-source"),
countElement: document.querySelector("#count-source"),
closingElement: document.querySelector("#closing-source"),
shiftElement: document.querySelector("#shift-source"),
lastElement: document.querySelector("#last-source"),
statsElement: document.querySelector("#stats-source"),
moving: "source",
background: 0x07131d,
target: new THREE.Vector3(-3.25, 0, 0),
movingColor: SOURCE_COLOR,
sourceLabel: "MOVING SOURCE",
observerLabel: "FIXED OBSERVER",
pathPosition: sourceMovingPosition,
sourcePosition: sourceMovingPosition,
observerPosition: () => ({ x: 0, z: 0 }),
sourceVelocity: sourceMovingVelocity,
observerVelocity: () => ({ x: 0, z: 0 })
}),
createScenario({
id: "observer",
panel: document.querySelector("#panel-observer"),
countElement: document.querySelector("#count-observer"),
closingElement: document.querySelector("#closing-observer"),
shiftElement: document.querySelector("#shift-observer"),
lastElement: document.querySelector("#last-observer"),
statsElement: document.querySelector("#stats-observer"),
moving: "observer",
background: 0x07131d,
target: new THREE.Vector3(3.25, 0, 0),
movingColor: OBSERVER_COLOR,
sourceLabel: "FIXED SOURCE",
observerLabel: "MOVING OBSERVER",
pathPosition: observerMovingPosition,
sourcePosition: () => ({ x: 0, z: 0 }),
observerPosition: observerMovingPosition,
sourceVelocity: () => ({ x: 0, z: 0 }),
observerVelocity: observerMovingVelocity
})
];
function emitWave(scenario, sourcePosition) {
const material = new THREE.MeshBasicMaterial({
color: WAVE_COLOR,
transparent: true,
opacity: 0.55,
side: THREE.DoubleSide,
depthWrite: false,
blending: THREE.AdditiveBlending
});
const mesh = new THREE.Mesh(ringGeometry, material);
mesh.rotation.x = -Math.PI / 2;
mesh.position.set(sourcePosition.x, 0.025, sourcePosition.z);
mesh.renderOrder = 3;
scenario.scene.add(mesh);
scenario.waves.push({
mesh,
centerX: sourcePosition.x,
centerZ: sourcePosition.z,
emittedAt: scenario.time,
counted: false
});
}
function registerArrival(scenario) {
scenario.count += 1;
scenario.lastArrival = scenario.time;
scenario.flash = 1;
scenario.countElement.textContent = scenario.count;
if (scenario.countElement.animate) {
scenario.countElement.animate([
{ transform: "scale(1)", color: "#52e5ff" },
{ transform: "scale(1.22)", color: "#ffffff", offset: 0.35 },
{ transform: "scale(1)", color: "#52e5ff" }
], {
duration: 460,
easing: "ease-out"
});
}
if (scenario.statsElement.animate) {
scenario.statsElement.animate([
{ borderColor: "rgba(82,229,255,0.18)" },
{ borderColor: "rgba(82,229,255,0.9)" },
{ borderColor: "rgba(82,229,255,0.18)" }
], {
duration: 520,
easing: "ease-out"
});
}
}
function updateScenario(scenario, dt) {
scenario.time += dt;
const angle = scenario.time * ANGULAR_SPEED;
const sourcePosition = scenario.sourcePosition(angle);
const observerPosition = scenario.observerPosition(angle);
const sourceVelocity = scenario.sourceVelocity(angle);
const observerVelocity = scenario.observerVelocity(angle);
scenario.source.group.position.set(sourcePosition.x, 0, sourcePosition.z);
scenario.observer.group.position.set(observerPosition.x, 0, observerPosition.z);
if (scenario.sourceArrow) {
const direction = new THREE.Vector3(
sourceVelocity.x,
0,
sourceVelocity.z
).normalize();
scenario.sourceArrow.setDirection(direction);
}
if (scenario.observerArrow) {
const direction = new THREE.Vector3(
observerVelocity.x,
0,
observerVelocity.z
).normalize();
scenario.observerArrow.setDirection(direction);
}
const relativeX = scenario.moving === "source"
? sourcePosition.x - observerPosition.x
: observerPosition.x - sourcePosition.x;
const relativeZ = scenario.moving === "source"
? sourcePosition.z - observerPosition.z
: observerPosition.z - sourcePosition.z;
const movingVelocity = scenario.moving === "source"
? sourceVelocity
: observerVelocity;
const distance = Math.hypot(relativeX, relativeZ);
const radialVelocity = (
relativeX * movingVelocity.x +
relativeZ * movingVelocity.z
) / Math.max(distance, 0.0001);
// Positive closing speed means the source and observer are approaching.
scenario.closingSpeed = -radialVelocity;
while (scenario.time >= scenario.nextEmission) {
emitWave(scenario, sourcePosition);
scenario.nextEmission += EMISSION_PERIOD;
}
for (let i = scenario.waves.length - 1; i >= 0; i--) {
const wave = scenario.waves[i];
const age = scenario.time - wave.emittedAt;
const radius = WAVE_SPEED * age;
const observerToWaveX = observerPosition.x - wave.centerX;
const observerToWaveZ = observerPosition.z - wave.centerZ;
const currentDistance = Math.hypot(observerToWaveX, observerToWaveZ);
// The expanding wavefront intersects the observer when radius = distance.
if (!wave.counted && radius >= currentDistance) {
wave.counted = true;
registerArrival(scenario);
}
if (age > MAX_WAVE_AGE) {
scenario.scene.remove(wave.mesh);
wave.mesh.material.dispose();
scenario.waves.splice(i, 1);
continue;
}
wave.mesh.scale.set(radius, radius, radius);
const fade = 1 - age / MAX_WAVE_AGE;
wave.mesh.material.opacity = 0.12 + 0.43 * fade;
}
scenario.flash *= Math.exp(-dt * 4.5);
scenario.observer.flashRing.material.opacity = scenario.flash * 0.85;
scenario.observer.flashRing.scale.setScalar(1 + scenario.flash * 0.8);
const pulse = 1 + Math.sin(scenario.time * 4) * 0.055;
scenario.source.halo.scale.setScalar(pulse);
scenario.source.halo.material.opacity = 0.43 + Math.sin(scenario.time * 4) * 0.12;
const closing = scenario.closingSpeed;
const signedClosing = `${closing >= 0 ? "+" : "−"}${Math.abs(closing).toFixed(2)} u/s`;
scenario.closingElement.textContent = signedClosing;
scenario.closingElement.classList.toggle("approach", closing > 0.025);
scenario.closingElement.classList.toggle("recede", closing < -0.025);
const radialShift = scenario.moving === "source"
? WAVE_SPEED / (WAVE_SPEED - closing)
: (WAVE_SPEED + closing) / WAVE_SPEED;
scenario.shiftElement.textContent = `×${radialShift.toFixed(2)}`;
scenario.lastElement.textContent = scenario.lastArrival < 0
? "—"
: `${scenario.lastArrival.toFixed(1)} s`;
}
let paused = false;
let playbackSpeed = 1;
const pauseButton = document.querySelector("#pause");
const resetButton = document.querySelector("#reset");
const speedInput = document.querySelector("#speed");
const speedOutput = document.querySelector("#speed-value");
pauseButton.addEventListener("click", () => {
paused = !paused;
pauseButton.textContent = paused ? "Resume" : "Pause";
});
speedInput.addEventListener("input", () => {
playbackSpeed = Number(speedInput.value);
speedOutput.textContent = `${playbackSpeed.toFixed(1)}×`;
});
function resetSimulation() {
for (const scenario of scenarios) {
for (const wave of scenario.waves) {
scenario.scene.remove(wave.mesh);
wave.mesh.material.dispose();
}
scenario.waves.length = 0;
scenario.time = 0;
scenario.nextEmission = EMISSION_PERIOD;
scenario.count = 0;
scenario.lastArrival = -1;
scenario.flash = 0;
scenario.countElement.textContent = "0";
scenario.lastElement.textContent = "—";
emitWave(scenario, scenario.sourcePosition(0));
updateScenario(scenario, 0);
}
}
resetButton.addEventListener("click", resetSimulation);
// Direct camera controls; no external controls module is used.
const view = {
yaw: 0,
phi: 0.88,
radius: 18
};
let dragging = false;
let previousPointerX = 0;
let previousPointerY = 0;
function resetView() {
view.yaw = 0;
view.phi = 0.88;
view.radius = 18;
}
canvas.addEventListener("pointerdown", event => {
if (event.button !== 0) return;
dragging = true;
previousPointerX = event.clientX;
previousPointerY = event.clientY;
canvas.setPointerCapture(event.pointerId);
});
canvas.addEventListener("pointermove", event => {
if (!dragging) return;
const dx = event.clientX - previousPointerX;
const dy = event.clientY - previousPointerY;
previousPointerX = event.clientX;
previousPointerY = event.clientY;
view.yaw -= dx * 0.005;
view.phi = clamp(view.phi + dy * 0.004, 0.35, 1.35);
});
function endDrag(event) {
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();
view.radius = clamp(view.radius * Math.exp(event.deltaY * 0.001), 12, 30);
}, { passive: false });
canvas.addEventListener("dblclick", resetView);
function updateCamera(camera, target) {
const sinPhi = Math.sin(view.phi);
const cosPhi = Math.cos(view.phi);
camera.position.set(
target.x + view.radius * sinPhi * Math.sin(view.yaw),
view.radius * cosPhi,
target.z + view.radius * sinPhi * Math.cos(view.yaw)
);
camera.lookAt(target);
}
function resize() {
renderer.setSize(window.innerWidth, window.innerHeight, false);
}
window.addEventListener("resize", resize);
resize();
let previousTime = performance.now();
function animate(now) {
const rawDt = Math.min((now - previousTime) / 1000, 0.05);
previousTime = now;
if (!paused) {
const simulationDt = rawDt * playbackSpeed;
for (const scenario of scenarios) {
updateScenario(scenario, simulationDt);
}
}
renderer.setScissorTest(false);
renderer.setClearColor(0x050b12, 1);
renderer.clear();
renderer.setScissorTest(true);
for (const scenario of scenarios) {
const rect = scenario.panel.getBoundingClientRect();
const bottom = window.innerHeight - rect.bottom;
renderer.setViewport(rect.left, bottom, rect.width, rect.height);
renderer.setScissor(rect.left, bottom, rect.width, rect.height);
scenario.camera.aspect = rect.width / Math.max(rect.height, 1);
scenario.camera.updateProjectionMatrix();
updateCamera(scenario.camera, scenario.target);
renderer.render(scenario.scene, scenario.camera);
}
renderer.setScissorTest(false);
requestAnimationFrame(animate);
}
requestAnimationFrame(animate);
</script>
</body>
</html>
```