Prompt specification
Build or customize the Three.js 3D scene "Pulse reflector distance rig" 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: Pulse reflector distance rig
Category: Science & Physics · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/pulse-reflector-distance-rig-x1541
Scene Description Prompt:
Show a low-poly pulse traveling to a flat reflector and returning, with time and twice-separation distance displayed. Three.js from cdnjs, one HTML file.
Working Three.js Starter (pulse-reflector-distance-rig-x1541):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Low-Poly Pulse Reflector</title>
<style>
:root {
--bg: #060c16;
--panel: rgba(9, 19, 32, 0.82);
--border: rgba(126, 198, 232, 0.22);
--text: #e5f4ff;
--muted: #8da9bd;
--cyan: #55ddff;
--amber: #ffae4c;
}
* { 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;
width: 100%;
height: 100%;
touch-action: none;
}
#hud {
position: fixed;
inset: 0;
pointer-events: none;
}
.brand {
position: absolute;
top: 22px;
left: 24px;
max-width: 360px;
}
.eyebrow {
color: var(--cyan);
font-size: 11px;
font-weight: 800;
letter-spacing: 0.22em;
text-transform: uppercase;
}
.brand h1 {
margin: 6px 0 5px;
font-size: clamp(22px, 3vw, 38px);
line-height: 1;
letter-spacing: -0.035em;
}
.brand p {
margin: 0;
color: var(--muted);
font-size: 13px;
line-height: 1.5;
}
.metrics {
position: absolute;
top: 22px;
right: 24px;
display: flex;
gap: 10px;
}
.metric {
min-width: 145px;
padding: 13px 15px;
border: 1px solid var(--border);
border-radius: 12px;
background: var(--panel);
backdrop-filter: blur(14px);
box-shadow: 0 10px 35px rgba(0, 0, 0, 0.2);
}
.metric span {
display: block;
margin-bottom: 4px;
color: var(--muted);
font-size: 10px;
font-weight: 700;
letter-spacing: 0.12em;
text-transform: uppercase;
}
.metric strong {
font-size: 24px;
font-variant-numeric: tabular-nums;
letter-spacing: -0.04em;
}
.metric.accent strong {
color: var(--cyan);
}
.phase {
position: absolute;
top: 28px;
left: 50%;
transform: translateX(-50%);
padding: 8px 13px;
border: 1px solid rgba(85, 221, 255, 0.35);
border-radius: 999px;
background: rgba(6, 20, 31, 0.75);
color: var(--cyan);
font-size: 11px;
font-weight: 800;
letter-spacing: 0.16em;
text-transform: uppercase;
transition: color 150ms, border-color 150ms;
}
.phase.returning {
color: var(--amber);
border-color: rgba(255, 174, 76, 0.48);
}
.equation {
position: absolute;
left: 24px;
bottom: 24px;
width: min(350px, calc(100vw - 48px));
padding: 16px 18px;
border: 1px solid var(--border);
border-radius: 14px;
background: var(--panel);
backdrop-filter: blur(14px);
}
.equation .formula {
font-family: Georgia, "Times New Roman", serif;
font-size: 25px;
letter-spacing: 0.01em;
}
.equation .formula span {
color: var(--cyan);
}
.equation p {
margin: 7px 0 0;
color: var(--muted);
font-size: 11px;
line-height: 1.45;
}
.progress-track {
height: 3px;
margin-top: 13px;
overflow: hidden;
border-radius: 99px;
background: rgba(255, 255, 255, 0.08);
}
#progress {
width: 0;
height: 100%;
background: linear-gradient(90deg, var(--cyan), var(--amber));
transition: width 50ms linear;
}
.controls {
position: absolute;
right: 24px;
bottom: 24px;
width: 300px;
padding: 16px;
border: 1px solid var(--border);
border-radius: 14px;
background: var(--panel);
backdrop-filter: blur(14px);
pointer-events: auto;
}
.controls-title {
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: 14px;
font-size: 11px;
font-weight: 800;
letter-spacing: 0.14em;
text-transform: uppercase;
}
.controls-title small {
color: var(--muted);
font-size: 9px;
font-weight: 600;
letter-spacing: 0.04em;
text-transform: none;
}
.control {
margin: 13px 0;
}
.control label {
display: flex;
justify-content: space-between;
margin-bottom: 7px;
color: var(--muted);
font-size: 11px;
}
.control output {
color: var(--text);
font-weight: 800;
font-variant-numeric: tabular-nums;
}
input[type="range"] {
width: 100%;
height: 4px;
margin: 0;
border-radius: 99px;
accent-color: var(--cyan);
cursor: pointer;
}
.buttons {
display: flex;
gap: 8px;
margin-top: 15px;
}
button {
flex: 1;
padding: 9px 12px;
border: 1px solid rgba(85, 221, 255, 0.35);
border-radius: 8px;
background: rgba(85, 221, 255, 0.09);
color: var(--text);
font: inherit;
font-size: 12px;
font-weight: 800;
cursor: pointer;
}
button:hover {
background: rgba(85, 221, 255, 0.16);
}
button.secondary {
border-color: rgba(141, 169, 189, 0.25);
background: rgba(255, 255, 255, 0.04);
}
.hint {
margin-top: 11px;
color: #668296;
font-size: 10px;
text-align: center;
}
.world-label {
position: absolute;
transform: translate(-50%, -50%);
padding: 5px 8px;
border: 1px solid rgba(85, 221, 255, 0.25);
border-radius: 6px;
background: rgba(5, 13, 23, 0.78);
color: #bcefff;
font-size: 9px;
font-weight: 800;
letter-spacing: 0.13em;
text-transform: uppercase;
white-space: nowrap;
box-shadow: 0 5px 18px rgba(0, 0, 0, 0.25);
}
.world-label.reflector {
color: #ffd39e;
border-color: rgba(255, 174, 76, 0.3);
}
@media (max-width: 800px) {
.brand p { display: none; }
.brand h1 { font-size: 23px; }
.metrics { top: 95px; right: 14px; }
.metric { min-width: 125px; padding: 10px 12px; }
.metric strong { font-size: 20px; }
.phase { top: 68px; }
.equation { left: 14px; bottom: 14px; width: 250px; }
.controls { right: 14px; bottom: 14px; width: 250px; }
.formula { font-size: 21px; }
}
@media (max-width: 580px) {
.brand { left: 14px; top: 15px; }
.brand h1 { font-size: 20px; }
.metrics {
top: 78px;
left: 14px;
right: 14px;
}
.metric { flex: 1; min-width: 0; }
.metric span { font-size: 8px; }
.metric strong { font-size: 17px; }
.phase { top: 58px; }
.equation { display: none; }
.controls { left: 14px; right: 14px; width: auto; }
}
</style>
</head>
<body>
<div id="scene"></div>
<div id="hud">
<header class="brand">
<div class="eyebrow">Wave laboratory / 01</div>
<h1>Pulse → Flat Reflector</h1>
<p>A faceted pulse crosses the gap, reflects, and returns to its source.</p>
</header>
<div class="phase" id="phase">Outbound</div>
<section class="metrics">
<div class="metric">
<span>Elapsed time</span>
<strong id="time">0.00 s</strong>
</div>
<div class="metric accent">
<span>Twice separation · 2d</span>
<strong id="doubleDistance">12.00 m</strong>
</div>
<div class="metric">
<span>Separation · d</span>
<strong id="separationMetric">6.00 m</strong>
</div>
</section>
<section class="equation">
<div class="formula">
t<sub>round trip</sub> =
<span>2d / c</span>
= <span id="equationResult">2.00 s</span>
</div>
<p>
d is the source-to-reflector separation. The out-and-back distance
is 2d, while the pulse color indicates direction.
</p>
<div class="progress-track"><div id="progress"></div></div>
</section>
<section class="controls">
<div class="controls-title">
Simulation
<small>Space: play · R: reset</small>
</div>
<div class="control">
<label for="separation">
Separation d
<output id="separationOutput">6.0 m</output>
</label>
<input id="separation" type="range" min="2" max="10" step="0.5" value="6">
</div>
<div class="control">
<label for="speed">
Pulse speed c
<output id="speedOutput">6.0 m/s</output>
</label>
<input id="speed" type="range" min="1" max="15" step="0.5" value="6">
</div>
<div class="buttons">
<button id="play" type="button">Pause</button>
<button id="reset" class="secondary" type="button">Reset round trip</button>
</div>
<div class="hint">Drag to orbit · Scroll to zoom</div>
</section>
<div id="sourceLabel" class="world-label">Source</div>
<div id="pulseLabel" class="world-label">Pulse · Outbound</div>
<div id="reflectorLabel" class="world-label reflector">Flat reflector</div>
</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const $ = id => document.getElementById(id);
const COLORS = {
cyan: 0x55ddff,
amber: 0xffae4c,
bg: 0x060c16,
muted: 0x31516a
};
// ------------------------------------------------------------
// Renderer, scene, and camera
// ------------------------------------------------------------
const scene = new THREE.Scene();
scene.background = new THREE.Color(COLORS.bg);
scene.fog = new THREE.Fog(COLORS.bg, 11, 30);
const camera = new THREE.PerspectiveCamera(
43,
innerWidth / innerHeight,
0.1,
100
);
const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
renderer.setSize(innerWidth, innerHeight);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.12;
$("scene").appendChild(renderer.domElement);
scene.add(new THREE.HemisphereLight(0x9ddfff, 0x07101b, 1.7));
const keyLight = new THREE.DirectionalLight(0xffffff, 2.2);
keyLight.position.set(4, 8, 7);
scene.add(keyLight);
const rimLight = new THREE.DirectionalLight(0x4bcfff, 1.1);
rimLight.position.set(-7, 2, -5);
scene.add(rimLight);
// Custom camera orbit state.
const cameraTarget = new THREE.Vector3(0, -0.05, 0);
let cameraAzimuth = 0.48;
let cameraPolar = 1.25;
let cameraRadius = 11;
function updateCamera() {
const sinPolar = Math.sin(cameraPolar);
camera.position.set(
cameraTarget.x + cameraRadius * sinPolar * Math.sin(cameraAzimuth),
cameraTarget.y + cameraRadius * Math.cos(cameraPolar),
cameraTarget.z + cameraRadius * sinPolar * Math.cos(cameraAzimuth)
);
camera.lookAt(cameraTarget);
}
// Direct orbit controls, implemented without addons.
let dragging = false;
let pointerX = 0;
let pointerY = 0;
renderer.domElement.addEventListener("pointerdown", event => {
dragging = true;
pointerX = event.clientX;
pointerY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", event => {
if (!dragging) return;
const dx = event.clientX - pointerX;
const dy = event.clientY - pointerY;
pointerX = event.clientX;
pointerY = event.clientY;
cameraAzimuth -= dx * 0.006;
cameraPolar = THREE.MathUtils.clamp(
cameraPolar - dy * 0.005,
0.48,
1.46
);
updateCamera();
});
function endDrag(event) {
dragging = false;
if (renderer.domElement.hasPointerCapture(event.pointerId)) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
}
renderer.domElement.addEventListener("pointerup", endDrag);
renderer.domElement.addEventListener("pointercancel", endDrag);
renderer.domElement.addEventListener("wheel", event => {
event.preventDefault();
cameraRadius = THREE.MathUtils.clamp(
cameraRadius * Math.exp(event.deltaY * 0.001),
5.5,
25
);
updateCamera();
}, { passive: false });
// ------------------------------------------------------------
// Ground and scale ruler
// ------------------------------------------------------------
const ground = new THREE.Mesh(
new THREE.PlaneGeometry(30, 20),
new THREE.MeshStandardMaterial({
color: 0x07111c,
roughness: 0.93,
metalness: 0.05
})
);
ground.rotation.x = -Math.PI / 2;
ground.position.y = -1.55;
scene.add(ground);
const grid = new THREE.GridHelper(20, 20, 0x28516b, 0x142b3d);
grid.position.y = -1.535;
grid.material.transparent = true;
grid.material.opacity = 0.34;
scene.add(grid);
let ruler = null;
function rebuildRuler(distance) {
if (ruler) {
scene.remove(ruler);
ruler.geometry.dispose();
ruler.material.dispose();
}
const vertices = [];
const z = -2.18;
vertices.push(-distance / 2, -1.515, z, distance / 2, -1.515, z);
for (let x = -distance / 2; x <= distance / 2 + 0.001; x += 1) {
const major = Math.abs(x - Math.round(x)) < 0.001;
const tick = major ? 0.25 : 0.14;
vertices.push(x, -1.515, z, x, -1.515, z - tick);
}
const geometry = new THREE.BufferGeometry();
geometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(vertices, 3)
);
ruler = new THREE.LineSegments(
geometry,
new THREE.LineBasicMaterial({
color: 0x537b95,
transparent: true,
opacity: 0.65
})
);
scene.add(ruler);
}
// ------------------------------------------------------------
// Low-poly emitter
// ------------------------------------------------------------
const sourceGroup = new THREE.Group();
scene.add(sourceGroup);
const sourceMaterial = new THREE.MeshStandardMaterial({
color: 0x1c3447,
emissive: 0x071522,
emissiveIntensity: 0.6,
roughness: 0.42,
metalness: 0.62,
flatShading: true
});
const sourceBody = new THREE.Mesh(
new THREE.CylinderGeometry(0.24, 0.48, 0.72, 8, 1, false),
sourceMaterial
);
sourceBody.rotation.z = -Math.PI / 2;
sourceBody.position.x = -0.36;
sourceGroup.add(sourceBody);
const sourceAperture = new THREE.Mesh(
new THREE.CircleGeometry(0.235, 8),
new THREE.MeshBasicMaterial({
color: COLORS.cyan,
side: THREE.DoubleSide
})
);
sourceAperture.rotation.y = Math.PI / 2;
sourceAperture.position.x = 0.008;
sourceGroup.add(sourceAperture);
const sourceRingPoints = [];
for (let i = 0; i < 8; i++) {
const angle = i / 8 * Math.PI * 2;
sourceRingPoints.push(
new THREE.Vector3(
0.018,
Math.cos(angle) * 0.29,
Math.sin(angle) * 0.29
)
);
}
const sourceRing = new THREE.LineLoop(
new THREE.BufferGeometry().setFromPoints(sourceRingPoints),
new THREE.LineBasicMaterial({ color: COLORS.cyan })
);
sourceGroup.add(sourceRing);
const sourceStand = new THREE.Mesh(
new THREE.BoxGeometry(0.32, 0.52, 0.48),
sourceMaterial
);
sourceStand.position.set(-0.35, -1.27, 0);
sourceGroup.add(sourceStand);
const sourceLight = new THREE.PointLight(COLORS.cyan, 3, 4.5);
sourceLight.position.set(0.3, 0, 0);
sourceGroup.add(sourceLight);
// ------------------------------------------------------------
// Flat reflector
// ------------------------------------------------------------
const reflectorGroup = new THREE.Group();
scene.add(reflectorGroup);
const reflectorMaterial = new THREE.MeshStandardMaterial({
color: 0x253b4e,
emissive: 0x07131d,
emissiveIntensity: 0.8,
metalness: 0.75,
roughness: 0.28,
side: THREE.DoubleSide,
flatShading: true
});
const reflector = new THREE.Mesh(
new THREE.PlaneGeometry(3.8, 3.2),
reflectorMaterial
);
reflector.rotation.y = Math.PI / 2;
reflectorGroup.add(reflector);
const framePoints = [
new THREE.Vector3(0.025, -1.6, -1.9),
new THREE.Vector3(0.025, 1.6, -1.9),
new THREE.Vector3(0.025, 1.6, 1.9),
new THREE.Vector3(0.025, -1.6, 1.9)
];
const reflectorFrame = new THREE.LineLoop(
new THREE.BufferGeometry().setFromPoints(framePoints),
new THREE.LineBasicMaterial({
color: 0xffc77b,
transparent: true,
opacity: 0.9
})
);
reflectorGroup.add(reflectorFrame);
// Grid embedded just in front of the flat plane.
const reflectorGridVertices = [];
for (let z = -1.9; z <= 1.91; z += 0.475) {
reflectorGridVertices.push(
0.012, -1.6, z,
0.012, 1.6, z
);
}
for (let y = -1.6; y <= 1.61; y += 0.4) {
reflectorGridVertices.push(
0.012, y, -1.9,
0.012, y, 1.9
);
}
const reflectorGridGeometry = new THREE.BufferGeometry();
reflectorGridGeometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(reflectorGridVertices, 3)
);
const reflectorGrid = new THREE.LineSegments(
reflectorGridGeometry,
new THREE.LineBasicMaterial({
color: 0x6b91a8,
transparent: true,
opacity: 0.24
})
);
reflectorGroup.add(reflectorGrid);
const reflectorFeet = new THREE.Group();
for (const z of [-1.45, 1.45]) {
const foot = new THREE.Mesh(
new THREE.BoxGeometry(0.45, 0.32, 0.18),
sourceMaterial
);
foot.position.set(0, -1.39, z);
reflectorFeet.add(foot);
}
reflectorGroup.add(reflectorFeet);
const reflectorLight = new THREE.PointLight(COLORS.amber, 0, 5);
reflectorLight.position.set(-0.3, 0, 0);
reflectorGroup.add(reflectorLight);
// Expanding impact ring.
const impactRing = new THREE.Mesh(
new THREE.RingGeometry(0.23, 0.29, 8),
new THREE.MeshBasicMaterial({
color: COLORS.amber,
transparent: true,
opacity: 0,
side: THREE.DoubleSide,
depthWrite: false
})
);
impactRing.rotation.y = Math.PI / 2;
impactRing.position.x = -0.025;
impactRing.visible = false;
reflectorGroup.add(impactRing);
// Low-poly impact fragments.
const sparkMaterial = new THREE.MeshBasicMaterial({
color: COLORS.amber,
transparent: true,
opacity: 0
});
const sparks = [];
for (let i = 0; i < 8; i++) {
const spark = new THREE.Mesh(
new THREE.TetrahedronGeometry(0.075),
sparkMaterial
);
spark.userData.angle = i / 8 * Math.PI * 2;
spark.visible = false;
reflectorGroup.add(spark);
sparks.push(spark);
}
// ------------------------------------------------------------
// Faceted traveling pulse
// ------------------------------------------------------------
const pulseRoot = new THREE.Group();
scene.add(pulseRoot);
const pulseGeometry = new THREE.OctahedronGeometry(1, 0);
const pulseMaterial = new THREE.MeshStandardMaterial({
color: COLORS.cyan,
emissive: COLORS.cyan,
emissiveIntensity: 1.7,
roughness: 0.26,
metalness: 0.2,
flatShading: true,
transparent: true,
opacity: 0.68
});
const pulseShell = new THREE.Mesh(pulseGeometry, pulseMaterial);
pulseShell.scale.set(0.34, 0.6, 0.6);
pulseRoot.add(pulseShell);
const pulseEdges = new THREE.LineSegments(
new THREE.EdgesGeometry(pulseGeometry),
new THREE.LineBasicMaterial({
color: COLORS.cyan,
transparent: true,
opacity: 0.95
})
);
pulseEdges.scale.copy(pulseShell.scale);
pulseRoot.add(pulseEdges);
const pulseCoreMaterial = new THREE.MeshBasicMaterial({
color: 0xd9f8ff
});
const pulseCore = new THREE.Mesh(
new THREE.TetrahedronGeometry(0.24),
pulseCoreMaterial
);
pulseCore.position.x = 0.1;
pulseRoot.add(pulseCore);
const pulseRingPoints = [];
for (let i = 0; i < 12; i++) {
const angle = i / 12 * Math.PI * 2;
pulseRingPoints.push(
new THREE.Vector3(
-0.025,
Math.cos(angle) * 0.63,
Math.sin(angle) * 0.63
)
);
}
const pulseRingMaterial = new THREE.LineBasicMaterial({
color: COLORS.cyan,
transparent: true,
opacity: 0.75
});
const pulseRing = new THREE.LineLoop(
new THREE.BufferGeometry().setFromPoints(pulseRingPoints),
pulseRingMaterial
);
pulseRoot.add(pulseRing);
const pulseLight = new THREE.PointLight(COLORS.cyan, 3.2, 4.5);
pulseRoot.add(pulseLight);
// ------------------------------------------------------------
// Guide and moving trail
// ------------------------------------------------------------
const guideGeometry = new THREE.BufferGeometry().setFromPoints([
new THREE.Vector3(-3, 0, 0),
new THREE.Vector3(3, 0, 0)
]);
const guideLine = new THREE.Line(
guideGeometry,
new THREE.LineDashedMaterial({
color: 0x31536a,
dashSize: 0.16,
gapSize: 0.13,
transparent: true,
opacity: 0.55
})
);
guideLine.computeLineDistances();
scene.add(guideLine);
const trailPositions = new Float32Array([
-3, 0, 0,
3, 0, 0
]);
const trailGeometry = new THREE.BufferGeometry();
trailGeometry.setAttribute(
"position",
new THREE.BufferAttribute(trailPositions, 3)
);
const trailMaterial = new THREE.LineBasicMaterial({
color: COLORS.cyan,
transparent: true,
opacity: 0.92
});
const trailLine = new THREE.Line(trailGeometry, trailMaterial);
scene.add(trailLine);
// ------------------------------------------------------------
// Simulation state and layout
// ------------------------------------------------------------
let separation = 6;
let speed = 6;
let elapsed = 0;
let playing = true;
function configureLayout() {
const half = separation / 2;
sourceGroup.position.x = -half;
reflectorGroup.position.x = half;
guideGeometry.setFromPoints([
new THREE.Vector3(-half, 0, 0),
new THREE.Vector3(half, 0, 0)
]);
guideLine.computeLineDistances();
rebuildRuler(separation);
cameraRadius = Math.max(8, separation * 1.42 + 2);
updateCamera();
}
function updateTrail(fromX, toX) {
const attribute = trailGeometry.getAttribute("position");
attribute.setX(0, fromX);
attribute.setX(1, toX);
attribute.needsUpdate = true;
trailGeometry.computeBoundingSphere();
}
// ------------------------------------------------------------
// Readouts and controls
// ------------------------------------------------------------
$("separation").addEventListener("input", event => {
separation = Number(event.target.value);
elapsed = 0;
$("separationOutput").textContent = `${separation.toFixed(1)} m`;
$("separationMetric").textContent = `${separation.toFixed(2)} m`;
$("doubleDistance").textContent = `${(2 * separation).toFixed(2)} m`;
$("equationResult").textContent = `${(2 * separation / speed).toFixed(2)} s`;
configureLayout();
});
$("speed").addEventListener("input", event => {
speed = Number(event.target.value);
$("speedOutput").textContent = `${speed.toFixed(1)} m/s`;
$("equationResult").textContent = `${(2 * separation / speed).toFixed(2)} s`;
});
function togglePlayback() {
playing = !playing;
$("play").textContent = playing ? "Pause" : "Play";
}
function resetRoundTrip() {
elapsed = 0;
}
$("play").addEventListener("click", togglePlayback);
$("reset").addEventListener("click", resetRoundTrip);
addEventListener("keydown", event => {
if (event.code === "Space") {
event.preventDefault();
togglePlayback();
}
if (event.key.toLowerCase() === "r") {
resetRoundTrip();
}
});
// ------------------------------------------------------------
// Projected world labels
// ------------------------------------------------------------
const projected = new THREE.Vector3();
function placeLabel(element, x, y, z) {
projected.set(x, y, z).project(camera);
const visible = projected.z > -1 && projected.z < 1;
element.style.display = visible ? "block" : "none";
if (!visible) return;
element.style.left = `${(projected.x * 0.5 + 0.5) * innerWidth}px`;
element.style.top = `${(-projected.y * 0.5 + 0.5) * innerHeight}px`;
}
// ------------------------------------------------------------
// Animation
// ------------------------------------------------------------
let previousTime = performance.now();
function animate(now) {
const delta = Math.min((now - previousTime) / 1000, 0.05);
previousTime = now;
if (playing) elapsed += delta;
const outboundDuration = separation / speed;
const roundTripDuration = outboundDuration * 2;
const localTime = elapsed % roundTripDuration;
const returning = localTime > outboundDuration;
const x = returning
? separation / 2 -
((localTime - outboundDuration) / outboundDuration) * separation
: -separation / 2 +
(localTime / outboundDuration) * separation;
const activeColor = returning ? COLORS.amber : COLORS.cyan;
pulseRoot.position.set(x, 0, 0);
pulseRoot.rotation.y = elapsed * 0.48;
pulseRoot.rotation.z = elapsed * 0.21;
const pulseBreath = 1 + Math.sin(elapsed * 8) * 0.035;
pulseShell.scale.set(
0.34 * pulseBreath,
0.6 * pulseBreath,
0.6 * pulseBreath
);
pulseEdges.scale.copy(pulseShell.scale);
pulseMaterial.color.setHex(activeColor);
pulseMaterial.emissive.setHex(activeColor);
pulseEdges.material.color.setHex(activeColor);
pulseRingMaterial.color.setHex(activeColor);
pulseLight.color.setHex(activeColor);
pulseCoreMaterial.color.setHex(returning ? 0xffe0ad : 0xd9f8ff);
trailMaterial.color.setHex(activeColor);
if (returning) {
updateTrail(separation / 2, x);
} else {
updateTrail(-separation / 2, x);
}
// Impact animation on the reflector.
if (returning) {
const impactProgress =
(localTime - outboundDuration) / outboundDuration;
impactRing.visible = true;
impactRing.scale.setScalar(1 + impactProgress * 3.6);
impactRing.material.opacity = (1 - impactProgress) * 0.82;
reflectorLight.intensity = (1 - impactProgress) * 5;
sparkMaterial.opacity = (1 - impactProgress) * 0.95;
for (const spark of sparks) {
spark.visible = true;
const radius = 0.25 + impactProgress * 1.15;
const angle = spark.userData.angle;
spark.position.set(
-0.04 - impactProgress * 0.3,
Math.cos(angle) * radius,
Math.sin(angle) * radius
);
spark.rotation.x = elapsed * 4 + angle;
spark.rotation.y = elapsed * 5;
}
} else {
impactRing.visible = false;
impactRing.material.opacity = 0;
reflectorLight.intensity = 0;
sparkMaterial.opacity = 0;
for (const spark of sparks) {
spark.visible = false;
}
}
// Readouts.
$("time").textContent = `${localTime.toFixed(2)} s`;
$("progress").style.width = `${localTime / roundTripDuration * 100}%`;
const phaseElement = $("phase");
phaseElement.textContent = returning ? "Returning" : "Outbound";
phaseElement.classList.toggle("returning", returning);
$("pulseLabel").textContent = returning
? "Pulse · Returning"
: "Pulse · Outbound";
$("pulseLabel").style.color = returning ? "#ffd39e" : "#bcefff";
$("pulseLabel").style.borderColor = returning
? "rgba(255, 174, 76, 0.35)"
: "rgba(85, 221, 255, 0.28)";
placeLabel($("sourceLabel"), -separation / 2, 0.9, 0);
placeLabel($("pulseLabel"), x, 0.95, 0);
placeLabel($("reflectorLabel"), separation / 2, 1.05, 0);
renderer.render(scene, camera);
requestAnimationFrame(animate);
}
// ------------------------------------------------------------
// Resize and initialization
// ------------------------------------------------------------
addEventListener("resize", () => {
camera.aspect = innerWidth / innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(innerWidth, innerHeight);
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
});
$("separationMetric").textContent = `${separation.toFixed(2)} m`;
$("doubleDistance").textContent = `${(2 * separation).toFixed(2)} m`;
$("equationResult").textContent = `${(2 * separation / speed).toFixed(2)} s`;
configureLayout();
updateCamera();
requestAnimationFrame(animate);
</script>
</body>
</html>
```