Prompt specification
Build or customize the Three.js 3D scene "Shifted Lines Against a Fixed Reference" 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: Shifted Lines Against a Fixed Reference
Category: Space & Earth · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/shifted-lines-against-a-fixed-reference-x3826
Scene Description Prompt:
Create a standalone cutaway scene in one HTML file using Three.js from cdnjs. Animate a synthetic spectrum shifted under a stated Doppler approximation. Show wavelength change and label the approximation's validity rather than equating any red color with recession.
Working Three.js Starter (shifted-lines-against-a-fixed-reference-x3826):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Doppler Shift Cutaway — Synthetic Spectrum</title>
<style>
:root {
color-scheme: dark;
--bg: #070b14;
--panel: rgba(13, 20, 34, 0.88);
--panel-border: rgba(151, 187, 224, 0.18);
--text: #e8f0fb;
--muted: #9aacc3;
--cyan: #77ddff;
--green: #86efac;
--amber: #ffd27a;
--red: #ff8e8e;
}
* { 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;
}
#scene canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
cursor: grab;
}
#scene canvas:active {
cursor: grabbing;
}
.overlay {
position: fixed;
inset: 0;
pointer-events: none;
z-index: 3;
}
.brand {
position: absolute;
top: 24px;
left: 26px;
max-width: 570px;
pointer-events: none;
text-shadow: 0 2px 16px rgba(0, 0, 0, 0.6);
}
.kicker {
display: flex;
align-items: center;
gap: 9px;
margin-bottom: 8px;
color: var(--cyan);
font-size: 11px;
font-weight: 800;
letter-spacing: 0.17em;
text-transform: uppercase;
}
.kicker::before {
width: 7px;
height: 7px;
border-radius: 50%;
background: var(--cyan);
box-shadow: 0 0 16px var(--cyan);
content: "";
}
h1 {
margin: 0;
font-size: clamp(25px, 3.1vw, 43px);
font-weight: 720;
letter-spacing: -0.045em;
line-height: 1.02;
}
.subtitle {
margin-top: 10px;
color: #b9c8da;
font-size: 13px;
line-height: 1.5;
}
.model-badge {
display: inline-flex;
margin-top: 13px;
padding: 6px 9px;
border: 1px solid rgba(119, 221, 255, 0.28);
border-radius: 999px;
background: rgba(14, 34, 49, 0.64);
color: #bdefff;
font: 700 10px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
letter-spacing: 0.09em;
text-transform: uppercase;
}
.panel {
border: 1px solid var(--panel-border);
border-radius: 17px;
background: var(--panel);
box-shadow:
0 18px 55px rgba(0, 0, 0, 0.36),
inset 0 1px rgba(255, 255, 255, 0.035);
backdrop-filter: blur(17px) saturate(120%);
}
.info-panel {
position: absolute;
top: 24px;
right: 24px;
width: min(370px, calc(100vw - 48px));
max-height: calc(100vh - 48px);
padding: 17px;
overflow: auto;
pointer-events: auto;
scrollbar-width: thin;
scrollbar-color: #35475e transparent;
}
.panel-title {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 12px;
color: #d9e7f7;
font-size: 11px;
font-weight: 800;
letter-spacing: 0.13em;
text-transform: uppercase;
}
.panel-title span:last-child {
color: var(--green);
font: 700 10px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
}
.equation {
padding: 13px 14px;
border: 1px solid rgba(119, 221, 255, 0.15);
border-radius: 11px;
background: rgba(6, 14, 24, 0.68);
color: #ecf8ff;
font: 600 18px/1.4 ui-monospace, SFMono-Regular, Menlo, monospace;
text-align: center;
}
.equation small {
display: block;
margin-top: 4px;
color: var(--muted);
font-size: 10px;
font-weight: 500;
}
.validity {
margin-top: 10px;
padding: 10px 11px;
border-left: 3px solid var(--green);
border-radius: 5px;
background: rgba(134, 239, 172, 0.065);
color: #c4d8ca;
font-size: 11px;
line-height: 1.55;
}
.validity strong {
color: #dfffe9;
}
.scope {
margin: 10px 1px 0;
color: var(--muted);
font-size: 11px;
line-height: 1.55;
}
.scope summary {
color: #bdcce0;
cursor: pointer;
font-weight: 700;
}
.scope p {
margin: 8px 0 0;
}
.warning {
margin-top: 11px;
padding: 10px 11px;
border: 1px solid rgba(255, 210, 122, 0.19);
border-radius: 9px;
background: rgba(255, 210, 122, 0.055);
color: #eadbb9;
font-size: 11px;
line-height: 1.5;
}
.warning strong {
color: var(--amber);
}
#spectrumChart {
display: block;
width: 100%;
height: 178px;
margin-top: 13px;
border: 1px solid rgba(146, 178, 215, 0.14);
border-radius: 10px;
background: #07111c;
}
.chart-legend {
display: flex;
flex-wrap: wrap;
gap: 13px;
margin-top: 8px;
color: var(--muted);
font-size: 10px;
}
.chart-legend span {
display: inline-flex;
align-items: center;
gap: 6px;
}
.dash {
width: 19px;
border-top: 2px dashed #a9b7c9;
}
.solid {
width: 19px;
border-top: 3px solid var(--cyan);
}
.control-dock {
position: absolute;
bottom: 22px;
left: 24px;
width: min(860px, calc(100vw - 440px));
min-width: 540px;
padding: 16px 18px 15px;
pointer-events: auto;
}
.dock-grid {
display: grid;
grid-template-columns: minmax(280px, 1fr) minmax(220px, 0.7fr);
gap: 18px;
align-items: center;
}
.control-heading {
display: flex;
align-items: baseline;
justify-content: space-between;
gap: 12px;
margin-bottom: 9px;
}
.control-heading label {
color: #dce8f6;
font-size: 11px;
font-weight: 800;
letter-spacing: 0.11em;
text-transform: uppercase;
}
#velocityReadout {
color: var(--cyan);
font: 750 17px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
}
input[type="range"] {
width: 100%;
height: 5px;
margin: 8px 0 7px;
appearance: none;
border-radius: 999px;
background:
linear-gradient(90deg,
rgba(119, 221, 255, 0.15),
rgba(119, 221, 255, 0.15));
cursor: pointer;
}
input[type="range"]::-webkit-slider-thumb {
width: 18px;
height: 18px;
appearance: none;
border: 3px solid #dff7ff;
border-radius: 50%;
background: #45bde9;
box-shadow: 0 0 0 5px rgba(69, 189, 233, 0.13);
}
input[type="range"]::-moz-range-thumb {
width: 14px;
height: 14px;
border: 3px solid #dff7ff;
border-radius: 50%;
background: #45bde9;
box-shadow: 0 0 0 5px rgba(69, 189, 233, 0.13);
}
.range-ends {
display: flex;
justify-content: space-between;
color: #74869e;
font: 9px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
}
.button-row {
display: flex;
flex-wrap: wrap;
gap: 7px;
margin-top: 12px;
}
button, select {
min-height: 31px;
border: 1px solid rgba(151, 187, 224, 0.2);
border-radius: 8px;
background: rgba(29, 43, 62, 0.8);
color: #dbe8f7;
font: 700 10px/1 system-ui, sans-serif;
cursor: pointer;
transition: border-color 120ms, background 120ms, transform 120ms;
}
button {
padding: 0 11px;
}
button:hover, select:hover {
border-color: rgba(119, 221, 255, 0.52);
background: rgba(39, 63, 87, 0.9);
}
button:active {
transform: translateY(1px);
}
button.active {
border-color: rgba(119, 221, 255, 0.55);
background: rgba(31, 104, 133, 0.55);
color: #e7fbff;
}
select {
width: 100%;
padding: 0 8px;
}
.value-grid {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 8px;
}
.value-cell {
padding: 9px 10px;
border: 1px solid rgba(143, 174, 210, 0.13);
border-radius: 9px;
background: rgba(4, 10, 18, 0.45);
}
.value-cell .label {
display: block;
margin-bottom: 4px;
color: #8193aa;
font-size: 9px;
font-weight: 800;
letter-spacing: 0.09em;
text-transform: uppercase;
}
.value-cell .value {
color: #f0f7ff;
font: 700 15px/1.1 ui-monospace, SFMono-Regular, Menlo, monospace;
}
.direction {
grid-column: 1 / -1;
color: #b8c8da;
font-size: 11px;
line-height: 1.45;
}
.direction strong {
color: var(--cyan);
}
.dock-footer {
display: flex;
justify-content: space-between;
gap: 12px;
margin-top: 12px;
padding-top: 10px;
border-top: 1px solid rgba(149, 178, 212, 0.11);
color: #8294aa;
font-size: 10px;
line-height: 1.4;
}
#sceneLabels {
position: fixed;
inset: 0;
z-index: 2;
pointer-events: none;
}
.scene-label {
position: absolute;
padding: 5px 8px;
border: 1px solid rgba(149, 188, 226, 0.16);
border-radius: 6px;
background: rgba(5, 10, 18, 0.72);
color: #b9cadc;
font: 700 9px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
letter-spacing: 0.06em;
text-transform: uppercase;
white-space: nowrap;
transform: translate(-50%, -50%);
box-shadow: 0 5px 20px rgba(0, 0, 0, 0.24);
}
.scene-label.accent {
border-color: rgba(119, 221, 255, 0.26);
color: #bceeff;
}
.view-hint {
position: absolute;
right: 25px;
bottom: 24px;
padding: 8px 11px;
border: 1px solid rgba(151, 187, 224, 0.13);
border-radius: 999px;
background: rgba(8, 14, 23, 0.62);
color: #8193aa;
font-size: 10px;
pointer-events: none;
}
@media (max-width: 1050px) {
.control-dock {
width: calc(100vw - 48px);
min-width: 0;
}
.view-hint {
display: none;
}
.info-panel {
width: 330px;
}
}
@media (max-width: 720px) {
.brand {
top: 14px;
left: 14px;
right: 14px;
}
.brand .subtitle {
max-width: 360px;
}
.info-panel {
top: auto;
right: 12px;
bottom: 185px;
width: 270px;
max-height: 38vh;
padding: 12px;
}
#spectrumChart {
height: 120px;
}
.control-dock {
right: 12px;
bottom: 12px;
left: 12px;
width: auto;
padding: 13px;
}
.dock-grid {
grid-template-columns: 1fr;
gap: 11px;
}
.value-grid {
grid-template-columns: repeat(3, 1fr);
}
.direction {
grid-column: 1 / -1;
}
.dock-footer {
display: none;
}
.scene-label {
display: none;
}
}
</style>
</head>
<body>
<div id="scene" aria-label="Interactive cutaway of a synthetic spectrum shifted by a radial Doppler approximation"></div>
<div id="sceneLabels"></div>
<div class="overlay">
<header class="brand">
<div class="kicker">Optical bench / synthetic teaching model</div>
<h1>Doppler Shift,<br>Not a “Redness Test”</h1>
<div class="subtitle">
A cutaway spectrometer showing how a synthetic emission spectrum moves
under a first-order radial Doppler approximation.
</div>
<div class="model-badge">Synthetic spectrum · no astronomical inference</div>
</header>
<aside class="info-panel panel">
<div class="panel-title">
<span>Approximation & validity</span>
<span id="validityBadge">LINEAR REGIME</span>
</div>
<div class="equation">
λ<sub>obs</sub> ≈ λ<sub>0</sub>(1 + β)
<small>β = v<sub>r</sub>/c · Δλ ≈ λ<sub>0</sub>v<sub>r</sub>/c</small>
</div>
<div class="validity">
<strong>Valid for |v<sub>r</sub>| ≪ c.</strong>
The omitted relativistic correction is of order β²/2.
At the current setting it is
<span id="errorReadout">—</span>.
</div>
<details class="scope">
<summary>Model scope</summary>
<p>
Purely radial motion in an inertial frame. This excludes gravitational
redshift, cosmological expansion, transverse Doppler effects, and
non-radial motion. The sign convention is positive away from the
detector and negative toward it.
</p>
</details>
<div class="warning">
<strong>Hue encodes wavelength only.</strong>
Red is not, by itself, a recession flag. Direction is inferred here
from the signed radial velocity and the measured sign of Δλ within
this stated model.
</div>
<canvas id="spectrumChart" width="1200" height="600"
aria-label="Rest and observed synthetic spectral lines"></canvas>
<div class="chart-legend">
<span><i class="dash"></i> Rest λ₀</span>
<span><i class="solid"></i> Observed λ</span>
<span>Both lanes use the same wavelength scale</span>
</div>
</aside>
<section class="control-dock panel">
<div class="dock-grid">
<div>
<div class="control-heading">
<label for="velocity">Radial velocity</label>
<div id="velocityReadout">+900 km/s</div>
</div>
<input id="velocity" type="range"
min="-2000" max="2000" step="1" value="900"
aria-label="Radial velocity in kilometers per second">
<div class="range-ends">
<span>−2000 km/s · toward detector</span>
<span>+2000 km/s · away</span>
</div>
<div class="button-row">
<button type="button" data-nudge="-100">−100</button>
<button type="button" data-nudge="0">0 km/s</button>
<button type="button" data-nudge="100">+100</button>
<button type="button" id="playButton">▶ Animate sweep</button>
</div>
</div>
<div>
<div class="control-heading">
<label for="lineSelect">Inspect synthetic line</label>
</div>
<select id="lineSelect" aria-label="Synthetic spectral line">
<option value="450">450 nm</option>
<option value="500">500 nm</option>
<option value="550" selected>550 nm</option>
<option value="600">600 nm</option>
<option value="650">650 nm</option>
</select>
<div class="value-grid" style="margin-top: 9px;">
<div class="value-cell">
<span class="label">Rest λ₀</span>
<span class="value" id="restValue">550.000 nm</span>
</div>
<div class="value-cell">
<span class="label">Observed λ</span>
<span class="value" id="observedValue">550.000 nm</span>
</div>
<div class="value-cell">
<span class="label">Shift Δλ</span>
<span class="value" id="shiftValue">+0.000 nm</span>
</div>
<div class="direction">
<strong id="directionText">Moving away from the detector.</strong><br>
<span id="betaText">β = +0.00300</span>
</div>
</div>
</div>
</div>
<div class="dock-footer">
<span>
Synthetic rest lines: 450, 500, 550, 600, 650 nm.
All lines share the same fractional Doppler shift.
</span>
<span id="resetView">Reset camera</span>
</div>
</section>
<div class="view-hint">Drag to orbit · wheel to zoom · all controls are local</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 C_KMS = 299792.458;
const REST_LINES = [450, 500, 550, 600, 650];
const SPECTRUM_MIN = 448;
const SPECTRUM_MAX = 652;
const sceneHost = document.querySelector("#scene");
const velocityInput = document.querySelector("#velocity");
const velocityReadout = document.querySelector("#velocityReadout");
const lineSelect = document.querySelector("#lineSelect");
const playButton = document.querySelector("#playButton");
const resetView = document.querySelector("#resetView");
const restValue = document.querySelector("#restValue");
const observedValue = document.querySelector("#observedValue");
const shiftValue = document.querySelector("#shiftValue");
const directionText = document.querySelector("#directionText");
const betaText = document.querySelector("#betaText");
const errorReadout = document.querySelector("#errorReadout");
const validityBadge = document.querySelector("#validityBadge");
const chart = document.querySelector("#spectrumChart");
const chartCtx = chart.getContext("2d");
const chartTexture = new THREE.CanvasTexture(chart);
chartTexture.colorSpace = THREE.SRGBColorSpace;
let currentVelocity = Number(velocityInput.value);
let selectedWavelength = Number(lineSelect.value);
let autoplay = false;
let playTime = 0;
let chartDirty = true;
// ---------- Scene and renderer ----------
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x070b14);
scene.fog = new THREE.FogExp2(0x070b14, 0.026);
const renderer = new THREE.WebGLRenderer({
antialias: true,
alpha: false,
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.15;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
sceneHost.appendChild(renderer.domElement);
const camera = new THREE.PerspectiveCamera(
43,
window.innerWidth / window.innerHeight,
0.1,
100
);
const target = new THREE.Vector3(-0.9, 1.2, 0.25);
let cameraTheta = 0.28;
let cameraPhi = 1.07;
let cameraRadius = 15.8;
function updateCamera() {
const sinPhi = Math.sin(cameraPhi);
camera.position.set(
target.x + cameraRadius * sinPhi * Math.sin(cameraTheta),
target.y + cameraRadius * Math.cos(cameraPhi),
target.z + cameraRadius * sinPhi * Math.cos(cameraTheta)
);
camera.lookAt(target);
}
updateCamera();
// Direct camera controls; no external controls addon is used.
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;
cameraTheta -= dx * 0.005;
cameraPhi = THREE.MathUtils.clamp(cameraPhi - dy * 0.005, 0.48, 1.42);
pointerX = event.clientX;
pointerY = event.clientY;
updateCamera();
});
function stopDragging(event) {
dragging = false;
if (event?.pointerId !== undefined) {
if (renderer.domElement.hasPointerCapture(event.pointerId)) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
}
}
renderer.domElement.addEventListener("pointerup", stopDragging);
renderer.domElement.addEventListener("pointercancel", stopDragging);
renderer.domElement.addEventListener("wheel", (event) => {
event.preventDefault();
cameraRadius = THREE.MathUtils.clamp(
cameraRadius + event.deltaY * 0.009,
10.5,
22
);
updateCamera();
}, { passive: false });
resetView.addEventListener("click", () => {
cameraTheta = 0.28;
cameraPhi = 1.07;
cameraRadius = 15.8;
updateCamera();
});
// ---------- Lighting and materials ----------
scene.add(new THREE.HemisphereLight(0x8fb8df, 0x111722, 1.5));
const keyLight = new THREE.DirectionalLight(0xcce7ff, 3.1);
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 = 12;
keyLight.shadow.camera.bottom = -12;
scene.add(keyLight);
const rimLight = new THREE.PointLight(0x477dff, 18, 18, 2);
rimLight.position.set(5, 4, -5);
scene.add(rimLight);
const fillLight = new THREE.PointLight(0x65dfff, 12, 12, 2);
fillLight.position.set(-7, 4, 5);
scene.add(fillLight);
const metal = new THREE.MeshStandardMaterial({
color: 0x34485d,
metalness: 0.78,
roughness: 0.32
});
const darkMetal = new THREE.MeshStandardMaterial({
color: 0x172333,
metalness: 0.72,
roughness: 0.38
});
const edgeMetal = new THREE.MeshStandardMaterial({
color: 0x6c8096,
metalness: 0.82,
roughness: 0.26
});
const benchMaterial = new THREE.MeshStandardMaterial({
color: 0x182334,
metalness: 0.5,
roughness: 0.52
});
function addBox(parent, size, position, material, castShadow = true) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(size.x, size.y, size.z),
material
);
mesh.position.copy(position);
mesh.castShadow = castShadow;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function addCylinder(
parent,
radiusTop,
radiusBottom,
height,
position,
material,
radialSegments = 32
) {
const mesh = new THREE.Mesh(
new THREE.CylinderGeometry(
radiusTop,
radiusBottom,
height,
radialSegments
),
material
);
mesh.position.copy(position);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
// ---------- Bench, floor and optical rail ----------
const floor = new THREE.Mesh(
new THREE.PlaneGeometry(40, 40),
new THREE.MeshStandardMaterial({
color: 0x080d16,
metalness: 0.1,
roughness: 0.92
})
);
floor.rotation.x = -Math.PI / 2;
floor.position.y = -0.03;
floor.receiveShadow = true;
scene.add(floor);
const grid = new THREE.GridHelper(32, 32, 0x29425c, 0x152335);
grid.position.y = 0;
grid.material.transparent = true;
grid.material.opacity = 0.28;
scene.add(grid);
addBox(
scene,
new THREE.Vector3(9.8, 0.34, 8.4),
new THREE.Vector3(-1.25, 0.28, 0.45),
benchMaterial
);
addBox(
scene,
new THREE.Vector3(8.8, 0.12, 0.16),
new THREE.Vector3(-4, 0.52, 0.45),
edgeMetal
);
addBox(
scene,
new THREE.Vector3(8.8, 0.12, 0.16),
new THREE.Vector3(-2.5, 0.52, 0.45),
edgeMetal
);
for (const z of [-3.2, 4.2]) {
addBox(
scene,
new THREE.Vector3(0.42, 0.55, 0.42),
new THREE.Vector3(-5.2, 0.05, z),
darkMetal
);
addBox(
scene,
new THREE.Vector3(0.42, 0.55, 0.42),
new THREE.Vector3(-1.3, 0.05, z),
darkMetal
);
}
// ---------- Cutaway synthetic source ----------
const source = new THREE.Group();
scene.add(source);
// Back and side housing; the camera-facing section is intentionally removed.
addBox(
source,
new THREE.Vector3(2.65, 2.7, 0.16),
new THREE.Vector3(-4, 1.95, 4.92),
metal
);
addBox(
source,
new THREE.Vector3(0.16, 2.7, 2.35),
new THREE.Vector3(-5.32, 1.95, 3.72),
metal
);
// Partial right wall makes the cutaway construction explicit.
addBox(
source,
new THREE.Vector3(0.16, 2.7, 1.05),
new THREE.Vector3(-2.68, 1.95, 4.36),
metal
);
addBox(
source,
new THREE.Vector3(2.65, 0.16, 0.95),
new THREE.Vector3(-4, 3.3, 4.43),
metal
);
addBox(
source,
new THREE.Vector3(2.8, 0.18, 2.5),
new THREE.Vector3(-4, 0.64, 3.75),
darkMetal
);
// Neutral-white emitter: its light is not colored to imply motion.
const emitterMaterial = new THREE.MeshStandardMaterial({
color: 0xf5fbff,
emissive: 0xdff5ff,
emissiveIntensity: 3.2,
metalness: 0.08,
roughness: 0.18
});
const emitter = addCylinder(
source,
0.11,
0.11,
1.3,
new THREE.Vector3(-4, 1.76, 4.05),
emitterMaterial,
24
);
const emitterGlow = new THREE.Mesh(
new THREE.SphereGeometry(0.24, 24, 16),
new THREE.MeshBasicMaterial({
color: 0xe9faff,
transparent: true,
opacity: 0.34,
blending: THREE.AdditiveBlending,
depthWrite: false
})
);
emitterGlow.position.set(-4, 1.76, 4.05);
source.add(emitterGlow);
for (const y of [1.13, 2.39]) {
const ring = new THREE.Mesh(
new THREE.TorusGeometry(0.25, 0.045, 12, 40),
edgeMetal
);
ring.rotation.x = Math.PI / 2;
ring.position.set(-4, y, 4.05);
source.add(ring);
}
const sourceLight = new THREE.PointLight(0xe9f8ff, 7, 5, 2);
sourceLight.position.set(-4, 1.76, 3.8);
source.add(sourceLight);
// Radial-motion arrow. Positive velocity points away from the detector.
const motionArrow = new THREE.ArrowHelper(
new THREE.Vector3(0, 0, 1),
new THREE.Vector3(-4, 3.48, 3.62),
1.25,
0x77ddff,
0.27,
0.16
);
scene.add(motionArrow);
// ---------- Slit, collimator and grating ----------
const slit = new THREE.Group();
scene.add(slit);
addBox(
slit,
new THREE.Vector3(1.03, 2.25, 0.12),
new THREE.Vector3(-4.56, 1.7, 2.62),
metal
);
addBox(
slit,
new THREE.Vector3(1.03, 2.25, 0.12),
new THREE.Vector3(-3.44, 1.7, 2.62),
metal
);
// Narrow bright edges define the slit aperture.
const apertureMaterial = new THREE.MeshBasicMaterial({ color: 0x9eeeff });
addBox(
slit,
new THREE.Vector3(0.025, 2.25, 0.135),
new THREE.Vector3(-4.035, 1.7, 2.62),
apertureMaterial,
false
);
addBox(
slit,
new THREE.Vector3(0.025, 2.25, 0.135),
new THREE.Vector3(-3.965, 1.7, 2.62),
apertureMaterial,
false
);
const lensMaterial = new THREE.MeshPhongMaterial({
color: 0x8adfff,
transparent: true,
opacity: 0.28,
shininess: 110,
side: THREE.DoubleSide,
depthWrite: false
});
const lens = addCylinder(
scene,
0.68,
0.68,
0.14,
new THREE.Vector3(-4, 1.7, 1.45),
lensMaterial,
48
);
lens.rotation.x = Math.PI / 2;
const lensRing = new THREE.Mesh(
new THREE.TorusGeometry(0.72, 0.065, 14, 56),
edgeMetal
);
lensRing.position.set(-4, 1.7, 1.45);
scene.add(lensRing);
const gratingGroup = new THREE.Group();
gratingGroup.position.set(-4, 1.7, 0);
scene.add(gratingGroup);
addBox(
gratingGroup,
new THREE.Vector3(1.85, 1.8, 0.09),
new THREE.Vector3(0, 0, 0),
darkMetal
);
const grooveMaterial = new THREE.MeshStandardMaterial({
color: 0x9bc2db,
metalness: 0.9,
roughness: 0.22
});
for (let i = 0; i < 22; i++) {
addBox(
gratingGroup,
new THREE.Vector3(0.018, 1.58, 0.025),
new THREE.Vector3(-0.82 + i * 0.078, 0, -0.065),
grooveMaterial,
false
);
}
const gratingFrame = new THREE.Mesh(
new THREE.TorusGeometry(1.02, 0.045, 12, 4),
edgeMetal
);
gratingFrame.rotation.z = Math.PI / 4;
gratingFrame.scale.set(1.05, 1.03, 1);
gratingGroup.add(gratingFrame);
// ---------- Detector screen ----------
const screenGroup = new THREE.Group();
screenGroup.position.set(-4, 1.7, -3.55);
scene.add(screenGroup);
addBox(
screenGroup,
new THREE.Vector3(3.75, 2.03, 0.16),
new THREE.Vector3(0, 0, 0),
darkMetal
);
const screenMaterial = new THREE.MeshBasicMaterial({
map: chartTexture,
toneMapped: false,
side: THREE.DoubleSide
});
const screen = new THREE.Mesh(
new THREE.PlaneGeometry(3.55, 1.78),
screenMaterial
);
screen.position.z = 0.087;
screenGroup.add(screen);
// Detector feet.
for (const x of [-1.45, 1.45]) {
addBox(
screenGroup,
new THREE.Vector3(0.14, 0.78, 0.14),
new THREE.Vector3(x, -1.38, 0),
edgeMetal
);
}
// Incoming white beam and a faint beam volume.
const incomingGeometry = new THREE.BufferGeometry().setFromPoints([
new THREE.Vector3(-4, 1.7, 3.65),
new THREE.Vector3(-4, 1.7, 0)
]);
const incomingLine = new THREE.Line(
incomingGeometry,
new THREE.LineBasicMaterial({
color: 0xe5f8ff,
transparent: true,
opacity: 0.32
})
);
scene.add(incomingLine);
const beamVolume = addCylinder(
scene,
0.035,
0.035,
3.65,
new THREE.Vector3(-4, 1.7, 1.83),
new THREE.MeshBasicMaterial({
color: 0xbdeeff,
transparent: true,
opacity: 0.045,
depthWrite: false
}),
16
);
beamVolume.rotation.x = Math.PI / 2;
const rayGroup = new THREE.Group();
scene.add(rayGroup);
const observedPoints = new Map();
const restPoints = new Map();
// ---------- Photons ----------
const photonGeometry = new THREE.SphereGeometry(0.035, 8, 8);
const photons = [];
for (let i = 0; i < 30; i++) {
const material = new THREE.MeshBasicMaterial({
color: 0xffffff,
transparent: true,
opacity: 0.9
});
const photon = new THREE.Mesh(photonGeometry, material);
scene.add(photon);
photons.push({
mesh: photon,
lineIndex: i % REST_LINES.length,
phase: i / 30
});
}
// ---------- Projected HTML labels ----------
const labels = [];
function addSceneLabel(text, anchor, offsetX = 0, offsetY = 0, accent = false) {
const element = document.createElement("div");
element.className = "scene-label" + (accent ? " accent" : "");
element.textContent = text;
document.querySelector("#sceneLabels").appendChild(element);
labels.push({
element,
anchor: anchor.clone(),
offsetX,
offsetY
});
}
addSceneLabel(
"Cutaway synthetic lamp",
new THREE.Vector3(-4.85, 3.05, 4.15),
-45, -12,
true
);
addSceneLabel(
"Entrance slit",
new THREE.Vector3(-4, 2.82, 2.62),
52, -6
);
addSceneLabel(
"Collimating lens",
new THREE.Vector3(-4, 2.45, 1.45),
-70, -5
);
addSceneLabel(
"Diffraction grating",
new THREE.Vector3(-4, 2.62, 0),
64, -4,
true
);
addSceneLabel(
"Detector / wavelength display",
new THREE.Vector3(-4, 2.75, -3.55),
84, -6,
true
);
const projected = new THREE.Vector3();
function updateLabels() {
const width = window.innerWidth;
const height = window.innerHeight;
for (const label of labels) {
projected.copy(label.anchor).project(camera);
const visible =
projected.z > -1 &&
projected.z < 1 &&
Math.abs(projected.x) < 1.25 &&
Math.abs(projected.y) < 1.25;
label.element.style.display = visible ? "block" : "none";
if (!visible) continue;
const x = (projected.x * 0.5 + 0.5) * width + label.offsetX;
const y = (-projected.y * 0.5 + 0.5) * height + label.offsetY;
label.element.style.left = `${x}px`;
label.element.style.top = `${y}px`;
}
}
// ---------- Wavelength and model calculations ----------
function wavelengthColor(nm) {
let r = 0;
let g = 0;
let b = 0;
if (nm >= 380 && nm < 440) {
r = -(nm - 440) / 60;
b = 1;
} else if (nm >= 440 && nm < 490) {
g = (nm - 440) / 50;
b = 1;
} else if (nm >= 490 && nm < 510) {
g = 1;
b = -(nm - 510) / 20;
} else if (nm >= 510 && nm < 580) {
r = (nm - 510) / 70;
g = 1;
} else if (nm >= 580 && nm < 645) {
r = 1;
g = -(nm - 645) / 65;
} else if (nm >= 645 && nm <= 780) {
r = 1;
}
let factor = 1;
if (nm >= 380 && nm < 420) factor = 0.3 + 0.7 * (nm - 380) / 40;
if (nm > 700 && nm <= 780) factor = 0.3 + 0.7 * (780 - nm) / 80;
const gamma = 0.85;
r = Math.round(255 * Math.pow(Math.max(0, r) * factor, gamma));
g = Math.round(255 * Math.pow(Math.max(0, g) * factor, gamma));
b = Math.round(255 * Math.pow(Math.max(0, b) * factor, gamma));
return `rgb(${r}, ${g}, ${b})`;
}
function betaFromVelocity(velocityKms) {
return velocityKms / C_KMS;
}
function observedWavelength(restNm, velocityKms) {
const beta = betaFromVelocity(velocityKms);
return restNm * (1 + beta);
}
function exactSRWavelength(restNm, velocityKms) {
const beta = betaFromVelocity(velocityKms);
return restNm * Math.sqrt((1 + beta) / (1 - beta));
}
function formatSigned(value, digits = 3) {
const sign = value > 0 ? "+" : value < 0 ? "−" : "";
return `${sign}${Math.abs(value).toFixed(digits)}`;
}
function screenXForWavelength(nm) {
const normalized =
(nm - SPECTRUM_MIN) / (SPECTRUM_MAX - SPECTRUM_MIN);
return -4 + (normalized - 0.5) * 3.55;
}
// ---------- Spectrum chart ----------
function drawSpectrum(velocityKms) {
const ctx = chartCtx;
const width = chart.width;
const height = chart.height;
const plotLeft = 118;
const plotRight = 1160;
const plotWidth = plotRight - plotLeft;
const upperTop = 126;
const upperBottom = 255;
const lowerTop = 355;
const lowerBottom = 485;
const axisY = 535;
const mapX = (nm) =>
plotLeft + ((nm - SPECTRUM_MIN) /
(SPECTRUM_MAX - SPECTRUM_MIN)) * plotWidth;
ctx.clearRect(0, 0, width, height);
const background = ctx.createLinearGradient(0, 0, width, height);
background.addColorStop(0, "#091624");
background.addColorStop(1, "#050b13");
ctx.fillStyle = background;
ctx.fillRect(0, 0, width, height);
ctx.fillStyle = "#dcecff";
ctx.font = "700 27px system-ui, sans-serif";
ctx.fillText("SYNTHETIC SPECTRUM · SAME WAVELENGTH SCALE", 42, 46);
ctx.fillStyle = "#91a6bd";
ctx.font = "19px system-ui, sans-serif";
ctx.fillText(
`First-order radial approximation: Δλ ≈ λ₀ vᵣ/c`,
42,
77
);
// Plot frames.
ctx.strokeStyle = "rgba(148, 177, 211, 0.20)";
ctx.lineWidth = 1.5;
ctx.strokeRect(plotLeft, upperTop, plotWidth, upperBottom - upperTop);
ctx.strokeRect(plotLeft, lowerTop, plotWidth, lowerBottom - lowerTop);
// Wavelength grid.
for (let nm = 450; nm <= 650; nm += 10) {
const x = mapX(nm);
ctx.strokeStyle = "rgba(135, 166, 200, 0.075)";
ctx.lineWidth = 1;
ctx.beginPath();
ctx.moveTo(x, upperTop);
ctx.lineTo(x, lowerBottom);
ctx.stroke();
ctx.fillStyle = "#8296ad";
ctx.font = "17px ui-monospace, monospace";
ctx.textAlign = "center";
ctx.fillText(String(nm), x, axisY + 28);
}
ctx.textAlign = "left";
ctx.fillStyle = "#a9bbcf";
ctx.font = "700 18px system-ui, sans-serif";
ctx.fillText("REST λ₀", 28, 190);
ctx.fillText("OBSERVED λ", 17, 420);
ctx.fillStyle = "#7f93aa";
ctx.font = "15px system-ui, sans-serif";
ctx.fillText("wavelength / nm", plotLeft, axisY - 14);
// Rest lines and observed lines.
for (const rest of REST_LINES) {
const observed = observedWavelength(rest, velocityKms);
const restX = mapX(rest);
const observedX = mapX(observed);
const isSelected = rest === selectedWavelength;
const color = wavelengthColor(observed);
// Rest marker.
ctx.save();
ctx.setLineDash([10, 8]);
ctx.strokeStyle = isSelected ? "#dce8f4" : "#91a0b2";
ctx.lineWidth = isSelected ? 4 : 2.5;
ctx.beginPath();
ctx.moveTo(restX, upperTop + 8);
ctx.lineTo(restX, upperBottom - 8);
ctx.stroke();
ctx.restore();
// Observed line.
ctx.save();
ctx.shadowColor = color;
ctx.shadowBlur = isSelected ? 18 : 9;
ctx.strokeStyle = color;
ctx.lineWidth = isSelected ? 8 : 4;
ctx.beginPath();
ctx.moveTo(observedX, lowerTop + 8);
ctx.lineTo(observedX, lowerBottom - 8);
ctx.stroke();
ctx.restore();
// The selected line gets a direct displacement connector.
if (isSelected) {
ctx.save();
ctx.strokeStyle = "#d9f4ff";
ctx.fillStyle = "#d9f4ff";
ctx.lineWidth = 2.5;
ctx.beginPath();
ctx.moveTo(restX, upperBottom + 5);
ctx.lineTo(observedX, lowerTop - 14);
ctx.stroke();
const angle = Math.atan2(
lowerTop - 14 - (upperBottom + 5),
observedX - restX
);
ctx.translate(observedX, lowerTop - 14);
ctx.rotate(angle);
ctx.beginPath();
ctx.moveTo(0, 0);
ctx.lineTo(-11, -6);
ctx.lineTo(-11, 6);
ctx.closePath();
ctx.fill();
ctx.restore();
const delta = observed - rest;
ctx.fillStyle = "#e8f5ff";
ctx.font = "700 17px ui-monospace, monospace";
ctx.textAlign = "center";
ctx.fillText(
`Δλ = ${formatSigned(delta, 3)} nm`,
(restX + observedX) / 2,
325
);
}
}
ctx.textAlign = "left";
ctx.fillStyle = "#93a8bf";
ctx.font = "15px system-ui, sans-serif";
ctx.fillText(
"Hue = wavelength only; direction comes from signed vᵣ, not from redness.",
42,
586
);
chartTexture.needsUpdate = true;
}
// ---------- Optical rays ----------
function clearRayGroup() {
while (rayGroup.children.length) {
const child = rayGroup.children[0];
rayGroup.remove(child);
if (child.geometry) child.geometry.dispose();
if (child.material) child.material.dispose();
}
}
function rebuildRays(velocityKms) {
clearRayGroup();
observedPoints.clear();
restPoints.clear();
const gratingPoint = new THREE.Vector3(-4, 1.7, -0.08);
for (const rest of REST_LINES) {
const observed = observedWavelength(rest, velocityKms);
const restPoint = new THREE.Vector3(
screenXForWavelength(rest),
1.7,
-3.45
);
const observedPoint = new THREE.Vector3(
screenXForWavelength(observed),
1.7,
-3.45
);
restPoints.set(rest, restPoint);
observedPoints.set(rest, observedPoint);
// Ghost ray to the unshifted position.
const restGeometry = new THREE.BufferGeometry().setFromPoints([
gratingPoint,
restPoint
]);
const restRay = new THREE.Line(
restGeometry,
new THREE.LineDashedMaterial({
color: 0x9aabbe,
transparent: true,
opacity: 0.24,
dashSize: 0.075,
gapSize: 0.055
})
);
restRay.computeLineDistances();
rayGroup.add(restRay);
// Colored observed ray.
const rayGeometry = new THREE.BufferGeometry().setFromPoints([
gratingPoint,
observedPoint
]);
const rayMaterial = new THREE.LineBasicMaterial({
color: new THREE.Color(wavelengthColor(observed)),
transparent: true,
opacity: rest === selectedWavelength ? 0.92 : 0.52,
blending: THREE.AdditiveBlending,
depthWrite: false
});
const observedRay = new THREE.Line(rayGeometry, rayMaterial);
rayGroup.add(observedRay);
const marker = new THREE.Mesh(
new THREE.SphereGeometry(
rest === selectedWavelength ? 0.052 : 0.035,
12,
10
),
new THREE.MeshBasicMaterial({
color: new THREE.Color(wavelengthColor(observed)),
transparent: true,
opacity: 0.95
})
);
marker.position.copy(observedPoint);
rayGroup.add(marker);
}
}
// ---------- UI updates ----------
function updateUI(velocityKms) {
const beta = betaFromVelocity(velocityKms);
const observed = observedWavelength(selectedWavelength, velocityKms);
const exact = exactSRWavelength(selectedWavelength, velocityKms);
const exactDifference = exact - observed;
const relativeOmitted = Math.abs(exactDifference / observed);
velocityReadout.textContent =
`${velocityKms > 0 ? "+" : velocityKms < 0 ? "−" : ""}` +
`${Math.abs(velocityKms).toFixed(0)} km/s`;
restValue.textContent = `${selectedWavelength.toFixed(3)} nm`;
observedValue.textContent = `${observed.toFixed(3)} nm`;
shiftValue.textContent = `${formatSigned(observed - selectedWavelength, 3)} nm`;
betaText.textContent =
`β = ${formatSigned(beta, 5)} · |vᵣ|/c = ${Math.abs(beta).toFixed(5)}`;
if (velocityKms > 0) {
directionText.textContent = "Moving away from the detector.";
} else if (velocityKms < 0) {
directionText.textContent = "Moving toward the detector.";
} else {
directionText.textContent = "No radial motion; the spectrum is unshifted.";
}
errorReadout.textContent =
`${(relativeOmitted * 100).toFixed(5)}% ` +
`(${Math.abs(exactDifference).toFixed(5)} nm at ${selectedWavelength} nm)`;
validityBadge.textContent =
Math.abs(beta) < 0.01 ? "LINEAR REGIME" : "CHECK APPROXIMATION";
validityBadge.style.color = Math.abs(beta) < 0.01 ? "#86efac" : "#ffd27a";
}
function applyVelocity(value, updateInput = true) {
currentVelocity = THREE.MathUtils.clamp(Number(value), -2000, 2000);
if (updateInput) velocityInput.value = String(currentVelocity);
updateUI(currentVelocity);
chartDirty = true;
}
velocityInput.addEventListener("input", () => {
if (autoplay) toggleAutoplay();
applyVelocity(velocityInput.value);
});
lineSelect.addEventListener("change", () => {
selectedWavelength = Number(lineSelect.value);
updateUI(currentVelocity);
chartDirty = true;
});
document.querySelectorAll("[data-nudge]").forEach((button) => {
button.addEventListener("click", () => {
if (autoplay) toggleAutoplay();
const nudge = Number(button.dataset.nudge);
applyVelocity(
THREE.MathUtils.clamp(currentVelocity + nudge, -2000, 2000)
);
});
});
function toggleAutoplay() {
autoplay = !autoplay;
playButton.classList.toggle("active", autoplay);
playButton.textContent = autoplay ? "❚❚ Pause sweep" : "▶ Animate sweep";
}
playButton.addEventListener("click", toggleAutoplay);
// ---------- Resize ----------
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
});
// ---------- Animation loop ----------
const clock = new THREE.Clock();
let lastChartTime = -Infinity;
function animate() {
requestAnimationFrame(animate);
const elapsed = clock.getElapsedTime();
const delta = Math.min(clock.getDelta(), 0.05);
if (autoplay) {
playTime += delta;
const velocity = Math.sin(playTime * 0.65) * 1500;
applyVelocity(velocity);
}
// Avoid redrawing the detailed chart more than about 30 times per second.
if (chartDirty && elapsed - lastChartTime > 0.032) {
drawSpectrum(currentVelocity);
rebuildRays(currentVelocity);
chartDirty = false;
lastChartTime = elapsed;
}
// Neutral source pulse; this is not a velocity color cue.
const pulse = 2.7 + Math.sin(elapsed * 2.3) * 0.65;
emitterMaterial.emissiveIntensity = pulse;
sourceLight.intensity = 6 + Math.sin(elapsed * 2.3) * 1.2;
// Update the motion arrow.
const arrowVisible = Math.abs(currentVelocity) > 1;
motionArrow.visible = arrowVisible;
if (arrowVisible) {
const sign = Math.sign(currentVelocity);
motionArrow.setDirection(new THREE.Vector3(0, 0, sign));
motionArrow.setLength(
0.45 + Math.abs(currentVelocity) / 2000 * 1.0,
0.27,
0.16
);
}
// Photons travel from the neutral source to the grating, then follow
// the wavelength-dependent diffracted rays to the detector.
for (const photon of photons) {
const rest = REST_LINES[photon.lineIndex];
const observed = observedWavelength(rest, currentVelocity);
const color = wavelengthColor(observed);
const endpoint = observedPoints.get(rest);
const phase = (photon.phase + elapsed * 0.19) % 1;
if (!endpoint) {
photon.mesh.visible = false;
continue;
}
photon.mesh.visible = true;
if (phase < 0.62) {
const t = phase / 0.62;
photon.mesh.position.set(
-4,
1.7,
THREE.MathUtils.lerp(3.62, -0.08, t)
);
photon.mesh.material.color.set(0xeaf8ff);
} else {
const t = (phase - 0.62) / 0.38;
photon.mesh.position.set(
THREE.MathUtils.lerp(-4, endpoint.x, t),
1.7,
THREE.MathUtils.lerp(-0.08, endpoint.z, t)
);
photon.mesh.material.color.set(color);
}
const fade = Math.sin(phase * Math.PI);
photon.mesh.material.opacity = 0.25 + fade * 0.7;
photon.mesh.scale.setScalar(0.75 + fade * 0.65);
}
updateLabels();
renderer.render(scene, camera);
}
// Initial state.
applyVelocity(currentVelocity);
drawSpectrum(currentVelocity);
rebuildRays(currentVelocity);
animate();
</script>
</body>
</html>
```