Prompt specification
Build or customize the Three.js 3D scene "Composition Shift or Motion Shift Puzzle" 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: Composition Shift or Motion Shift Puzzle
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/composition-shift-or-motion-shift-puzzle-x3828
Scene Description Prompt:
Compare voxel synthetic spectra with independently changing line identities and common wavelength shifts; reveal ambiguous composition-versus-motion explanations. One HTML file using Three.js from cdnjs.
Working Three.js Starter (composition-shift-or-motion-shift-puzzle-x3828):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Spectral Voxel Ambiguity Lab</title>
<style>
:root {
--bg: #07101d;
--panel: #0c1727;
--panel-2: #101e31;
--line: #20324a;
--text: #e8f0fa;
--muted: #8fa2b9;
--observed: #f6c85f;
--composition: #ff6b6b;
--motion: #56cfe1;
--accent: #8ea6ff;
}
* { 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;
}
button, select, input { font: inherit; }
button:focus-visible,
select:focus-visible,
input:focus-visible,
canvas:focus-visible {
outline: 2px solid var(--accent);
outline-offset: 2px;
}
.app {
height: 100vh;
display: flex;
flex-direction: column;
}
header {
height: 74px;
flex: 0 0 74px;
display: flex;
align-items: center;
justify-content: space-between;
gap: 24px;
padding: 0 24px;
border-bottom: 1px solid var(--line);
background: rgba(8, 17, 30, 0.96);
z-index: 5;
}
.brand {
display: flex;
align-items: center;
gap: 13px;
min-width: 0;
}
.mark {
width: 38px;
height: 38px;
flex: 0 0 38px;
border: 1px solid #536a91;
border-radius: 10px;
background:
linear-gradient(90deg, transparent 44%, #6682b5 45%, #6682b5 55%, transparent 56%),
linear-gradient(0deg, transparent 44%, #6682b5 45%, #6682b5 55%, transparent 56%),
#14233a;
box-shadow: inset 0 0 16px rgba(104, 139, 200, 0.16);
}
h1 {
margin: 0;
font-size: 17px;
line-height: 1.25;
letter-spacing: 0.01em;
font-weight: 680;
}
.subtitle {
margin-top: 4px;
color: var(--muted);
font-size: 12px;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
.header-meta {
display: flex;
gap: 8px;
align-items: center;
flex: 0 0 auto;
}
.chip {
border: 1px solid #2b405d;
border-radius: 999px;
padding: 7px 10px;
color: #aebed1;
background: #0d1a2c;
font-size: 10px;
font-weight: 750;
letter-spacing: 0.09em;
text-transform: uppercase;
white-space: nowrap;
}
.chip.live {
color: #79e4c0;
border-color: #225348;
background: rgba(25, 78, 65, 0.25);
}
main {
flex: 1;
min-height: 0;
display: flex;
}
aside {
width: 340px;
flex: 0 0 340px;
overflow-y: auto;
padding: 18px;
border-right: 1px solid var(--line);
background: #091422;
scrollbar-width: thin;
scrollbar-color: #2a405f transparent;
}
.section {
padding: 0 0 18px;
margin: 0 0 18px;
border-bottom: 1px solid var(--line);
}
.section:last-child { border-bottom: 0; }
.section-title {
display: flex;
align-items: center;
justify-content: space-between;
gap: 10px;
margin-bottom: 11px;
}
.section-title h2 {
margin: 0;
font-size: 11px;
color: #b9c7d8;
letter-spacing: 0.11em;
text-transform: uppercase;
font-weight: 800;
}
.section-title span {
color: #6f829a;
font-size: 10px;
}
.preset-grid {
display: grid;
grid-template-columns: 1fr;
gap: 7px;
}
.preset {
width: 100%;
padding: 10px 11px;
text-align: left;
color: #c5d1df;
background: #0f1c2d;
border: 1px solid #21344e;
border-radius: 9px;
cursor: pointer;
transition: 140ms ease;
}
.preset:hover {
border-color: #3b5377;
background: #132238;
}
.preset.active {
color: #f4f8ff;
border-color: #7188be;
background: linear-gradient(90deg, rgba(91, 119, 178, 0.23), rgba(25, 42, 68, 0.66));
box-shadow: inset 3px 0 #91a8ff;
}
.preset strong {
display: block;
font-size: 12px;
margin-bottom: 3px;
}
.preset small {
display: block;
color: #8295ad;
font-size: 10px;
line-height: 1.4;
}
.case-description {
margin-top: 10px;
padding: 10px 11px;
border-radius: 8px;
color: #9db0c7;
background: rgba(20, 36, 58, 0.62);
border: 1px solid #1e324c;
font-size: 11px;
line-height: 1.45;
}
.control-label {
display: flex;
align-items: baseline;
justify-content: space-between;
gap: 12px;
margin-bottom: 9px;
color: #cbd6e4;
font-size: 12px;
}
.control-label output {
color: #8fb7ff;
font-variant-numeric: tabular-nums;
font-size: 12px;
font-weight: 700;
}
input[type="range"] {
width: 100%;
height: 4px;
margin: 7px 0 9px;
appearance: none;
border-radius: 99px;
background: linear-gradient(90deg, #283d5b, #7188be, #283d5b);
}
input[type="range"]::-webkit-slider-thumb {
appearance: none;
width: 16px;
height: 16px;
border-radius: 50%;
background: #b7c9ff;
border: 3px solid #526b9e;
box-shadow: 0 0 0 4px rgba(119, 147, 213, 0.1);
cursor: grab;
}
input[type="range"]::-moz-range-thumb {
width: 12px;
height: 12px;
border-radius: 50%;
background: #b7c9ff;
border: 3px solid #526b9e;
cursor: grab;
}
.range-scale {
display: flex;
justify-content: space-between;
color: #63778e;
font-size: 9px;
}
.identity-list {
display: grid;
gap: 7px;
}
.identity-row {
display: grid;
grid-template-columns: 10px 78px 1fr;
align-items: center;
gap: 8px;
}
.identity-dot {
width: 7px;
height: 7px;
border-radius: 50%;
box-shadow: 0 0 10px currentColor;
}
.identity-name {
color: #91a4bb;
font-size: 10px;
white-space: nowrap;
}
select {
width: 100%;
min-width: 0;
height: 32px;
padding: 0 28px 0 9px;
color: #dce6f2;
background: #101e31;
border: 1px solid #263b57;
border-radius: 7px;
cursor: pointer;
}
select:hover { border-color: #3c5479; }
.button-row {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 7px;
margin-top: 10px;
}
.button {
min-height: 34px;
padding: 7px 9px;
border: 1px solid #2b405e;
border-radius: 8px;
color: #b9c9dc;
background: #101e31;
cursor: pointer;
font-size: 10px;
font-weight: 650;
}
.button:hover {
color: #fff;
border-color: #4e6790;
background: #16263d;
}
.note {
color: #7e92a9;
font-size: 10px;
line-height: 1.55;
}
.note strong { color: #aebed0; }
.workspace {
flex: 1;
min-width: 0;
min-height: 0;
display: flex;
flex-direction: column;
}
#viewport {
position: relative;
flex: 1;
min-height: 300px;
overflow: hidden;
background:
radial-gradient(circle at 48% 42%, rgba(40, 67, 101, 0.17), transparent 52%),
#07111e;
}
#viewport > canvas {
position: absolute;
inset: 0;
width: 100%;
height: 100%;
cursor: grab;
touch-action: none;
}
#viewport > canvas.dragging { cursor: grabbing; }
.scene-label {
position: absolute;
left: 18px;
top: 16px;
z-index: 2;
pointer-events: none;
}
.scene-label h2 {
margin: 0 0 4px;
font-size: 12px;
font-weight: 750;
letter-spacing: 0.02em;
}
.scene-label p {
margin: 0;
color: #70849d;
font-size: 10px;
}
.scene-legend {
position: absolute;
left: 18px;
bottom: 17px;
z-index: 2;
display: flex;
gap: 13px;
align-items: center;
padding: 8px 10px;
border: 1px solid rgba(45, 67, 96, 0.75);
border-radius: 8px;
background: rgba(8, 18, 31, 0.78);
backdrop-filter: blur(8px);
pointer-events: none;
}
.legend-item {
display: flex;
align-items: center;
gap: 6px;
color: #9cafc3;
font-size: 9px;
white-space: nowrap;
}
.swatch {
width: 9px;
height: 9px;
border-radius: 2px;
box-shadow: 0 0 9px currentColor;
}
.analysis {
position: absolute;
right: 16px;
top: 16px;
z-index: 2;
width: 280px;
padding: 13px;
border: 1px solid rgba(46, 68, 97, 0.85);
border-radius: 11px;
background: rgba(9, 20, 34, 0.88);
backdrop-filter: blur(12px);
box-shadow: 0 14px 38px rgba(0, 0, 0, 0.2);
pointer-events: none;
}
.analysis-head {
display: flex;
justify-content: space-between;
align-items: center;
gap: 10px;
margin-bottom: 10px;
}
.analysis-head span {
color: #8fa3bb;
font-size: 9px;
letter-spacing: 0.11em;
text-transform: uppercase;
font-weight: 800;
}
.ambiguity-value {
color: var(--observed);
font-size: 21px;
font-weight: 780;
font-variant-numeric: tabular-nums;
}
.metric-grid {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 7px;
}
.metric {
padding: 9px;
border: 1px solid #20344e;
border-radius: 8px;
background: rgba(19, 35, 56, 0.75);
}
.metric .name {
color: #8499b0;
font-size: 8px;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.metric .value {
margin-top: 4px;
font-size: 18px;
font-weight: 740;
font-variant-numeric: tabular-nums;
}
.metric.composition .value { color: var(--composition); }
.metric.motion .value { color: var(--motion); }
.metric .detail {
margin-top: 3px;
color: #71859e;
font-size: 9px;
}
.verdict {
margin-top: 9px;
padding: 9px 10px;
border-radius: 8px;
border: 1px solid #725f35;
background: rgba(111, 86, 31, 0.16);
color: #f0d483;
font-size: 10px;
line-height: 1.42;
}
.verdict.composition {
color: #ff9d9d;
border-color: #703c43;
background: rgba(107, 45, 54, 0.18);
}
.verdict.motion {
color: #88e5ef;
border-color: #2e6470;
background: rgba(35, 93, 106, 0.17);
}
.verdict.neither {
color: #c5b5f7;
border-color: #574c7e;
background: rgba(78, 63, 121, 0.17);
}
.view-actions {
position: absolute;
right: 16px;
bottom: 16px;
z-index: 3;
display: flex;
gap: 6px;
}
.view-actions button {
width: 34px;
height: 34px;
display: grid;
place-items: center;
border: 1px solid #2b405f;
border-radius: 8px;
color: #a5b8cd;
background: rgba(12, 27, 45, 0.88);
cursor: pointer;
}
.view-actions button:hover {
color: #fff;
border-color: #4c658d;
}
.tooltip {
position: absolute;
z-index: 8;
display: none;
min-width: 170px;
max-width: 240px;
padding: 9px 11px;
border: 1px solid #385174;
border-radius: 8px;
color: #dce7f4;
background: rgba(7, 16, 28, 0.95);
box-shadow: 0 10px 30px rgba(0, 0, 0, 0.3);
pointer-events: none;
font-size: 10px;
line-height: 1.5;
}
.tooltip strong {
display: block;
margin-bottom: 3px;
color: #fff;
font-size: 11px;
}
.chart-panel {
flex: 0 0 232px;
min-height: 232px;
border-top: 1px solid var(--line);
background: #091422;
padding: 11px 15px 8px;
}
.chart-header {
height: 28px;
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
}
.chart-title {
display: flex;
align-items: baseline;
gap: 10px;
}
.chart-title strong {
font-size: 11px;
color: #c5d2e1;
}
#chartReadout {
color: #7890aa;
font-size: 10px;
font-variant-numeric: tabular-nums;
}
.chart-legend {
display: flex;
gap: 12px;
align-items: center;
}
#spectrumChart {
display: block;
width: 100%;
height: 185px;
overflow: visible;
}
@media (max-width: 950px) {
html, body { overflow: auto; }
.app { height: auto; min-height: 100vh; }
header {
position: sticky;
top: 0;
}
main { flex-direction: column; }
aside {
width: 100%;
flex: 0 0 auto;
max-height: none;
border-right: 0;
border-bottom: 1px solid var(--line);
}
.preset-grid { grid-template-columns: repeat(3, 1fr); }
.workspace { min-height: 820px; }
#viewport { min-height: 570px; }
}
@media (max-width: 650px) {
header { padding: 0 13px; }
.header-meta .chip:first-child { display: none; }
.subtitle { max-width: 230px; }
.preset-grid { grid-template-columns: 1fr; }
.analysis { width: 245px; right: 10px; top: 10px; }
.scene-legend { left: 10px; bottom: 10px; gap: 8px; }
.chart-legend { display: none; }
}
</style>
</head>
<body>
<div class="app">
<header>
<div class="brand">
<div class="mark" aria-hidden="true"></div>
<div>
<h1>Spectral Voxel Ambiguity Lab</h1>
<div class="subtitle">Independent line identities versus one common wavelength shift</div>
</div>
</div>
<div class="header-meta">
<span class="chip">Three.js / Core</span>
<span class="chip live" id="caseChip">Shared shift</span>
</div>
</header>
<main>
<aside>
<section class="section">
<div class="section-title">
<h2>Observed scenario</h2>
<span>fixed synthetic cube</span>
</div>
<div class="preset-grid" id="presets">
<button class="preset active" data-case="motion">
<strong>1 · Shared wavelength shift</strong>
<small>Every reference line is displaced together.</small>
</button>
<button class="preset" data-case="identity">
<strong>2 · Independent identity changes</strong>
<small>Two line identities are replaced independently.</small>
</button>
<button class="preset" data-case="mixed">
<strong>3 · Mixed mechanisms</strong>
<small>Identity changes plus a shared displacement.</small>
</button>
</div>
<div class="case-description" id="caseDescription"></div>
</section>
<section class="section">
<div class="section-title">
<h2>Motion hypothesis</h2>
<span>one shift for all lines</span>
</div>
<label class="control-label" for="shiftSlider">
<span>Common radial velocity</span>
<output id="shiftOutput">+72.0 km/s</output>
</label>
<input id="shiftSlider" type="range" min="-120" max="120" step="0.5" value="72" />
<div class="range-scale">
<span>−120 km/s · blue</span>
<span>0</span>
<span>+120 km/s · red</span>
</div>
<div class="note" id="shiftDetail" style="margin-top:8px"></div>
</section>
<section class="section">
<div class="section-title">
<h2>Composition hypothesis</h2>
<span>each row changes alone</span>
</div>
<div class="identity-list" id="identityList"></div>
<div class="button-row">
<button class="button" id="fitComposition">Fit identities</button>
<button class="button" id="fitMotion">Fit common shift</button>
<button class="button" id="resetIdentities">Reset identities</button>
<button class="button" id="resetAll">Reset lab</button>
</div>
</section>
<section class="section">
<div class="section-title">
<h2>How to read the test</h2>
<span>degeneracy probe</span>
</div>
<p class="note">
<strong>Composition model:</strong> each observed feature moves to the canonical
wavelength of its assigned identity. Changes are independent.
</p>
<p class="note">
<strong>Motion model:</strong> the original reference spectrum is moved by one
velocity, preserving every relative wavelength.
</p>
<p class="note">
This is a toy line library. “Mimic” entries are synthetic lines placed near
reference features so that an identity substitution can imitate a Doppler shift.
</p>
</section>
</aside>
<section class="workspace">
<div id="viewport">
<div class="scene-label">
<h2>Spectral voxel volume</h2>
<p>Drag to orbit · wheel to zoom · hover a voxel for details</p>
</div>
<div class="scene-legend">
<span class="legend-item"><i class="swatch" style="background:#f6c85f;color:#f6c85f"></i>Observed</span>
<span class="legend-item"><i class="swatch" style="background:#ff6b6b;color:#ff6b6b"></i>Composition</span>
<span class="legend-item"><i class="swatch" style="background:#56cfe1;color:#56cfe1"></i>Motion</span>
</div>
<div class="analysis">
<div class="analysis-head">
<span>Ambiguity score</span>
<div class="ambiguity-value" id="ambiguityValue">0%</div>
</div>
<div class="metric-grid">
<div class="metric composition">
<div class="name">Composition RMS</div>
<div class="value" id="compositionScore">0.000</div>
<div class="detail" id="compositionDetail">0 / 6 changed</div>
</div>
<div class="metric motion">
<div class="name">Motion RMS</div>
<div class="value" id="motionScore">0.000</div>
<div class="detail" id="motionDetail">+72.0 km/s</div>
</div>
</div>
<div class="verdict" id="verdict" aria-live="polite"></div>
</div>
<div class="view-actions">
<button id="resetView" title="Reset camera" aria-label="Reset camera">⌂</button>
</div>
<div class="tooltip" id="voxelTooltip"></div>
</div>
<div class="chart-panel">
<div class="chart-header">
<div class="chart-title">
<strong>Synthetic spectrum comparison</strong>
<span id="chartReadout">Move across the spectrum to inspect</span>
</div>
<div class="chart-legend">
<span class="legend-item"><i class="swatch" style="background:#f6c85f;color:#f6c85f"></i>Observed</span>
<span class="legend-item"><i class="swatch" style="background:#ff6b6b;color:#ff6b6b"></i>Composition</span>
<span class="legend-item"><i class="swatch" style="background:#56cfe1;color:#56cfe1"></i>Motion</span>
</div>
</div>
<svg id="spectrumChart" role="img" aria-label="Observed, composition, and motion synthetic spectra"></svg>
</div>
</section>
</main>
</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 WAVELENGTH_MIN = 380;
const WAVELENGTH_MAX = 610;
const SAMPLE_STEP = 0.2;
const SIGMA = 0.42;
const FIT_THRESHOLD = 0.006;
const COLORS = {
observed: "#f6c85f",
composition: "#ff6b6b",
motion: "#56cfe1"
};
const LINE_DEFS = {
fe: { label: "Fe I", center: 438.35, factor: 1.00, color: "#ff9f43" },
fem: { label: "Fe I mimic", center: 438.45, factor: 1.00, color: "#ffb45f" },
cr: { label: "Cr I", center: 438.22, factor: 0.88, color: "#ff6f68" },
cak: { label: "Ca II K", center: 393.37, factor: 1.00, color: "#54c7ec" },
cakm:{ label: "Ca K mimic", center: 393.46, factor: 1.00, color: "#75d8f4" },
zn: { label: "Zn I", center: 393.29, factor: 0.75, color: "#6ed7c8" },
cah: { label: "Ca II H", center: 396.85, factor: 1.00, color: "#72a8ff" },
cahm:{ label: "Ca H mimic", center: 396.94, factor: 1.00, color: "#8ab7ff" },
ti: { label: "Ti II", center: 397.15, factor: 0.90, color: "#e58cff" },
mg: { label: "Mg I", center: 518.36, factor: 1.00, color: "#65e3a3" },
mgm: { label: "Mg I mimic", center: 518.48, factor: 1.00, color: "#85efb8" },
al: { label: "Al I", center: 518.20, factor: 0.86, color: "#65b9ed" },
na: { label: "Na I D", center: 589.00, factor: 1.00, color: "#bd8cff" },
nam: { label: "Na D mimic", center: 589.14, factor: 1.00, color: "#cea8ff" },
he: { label: "He I", center: 588.90, factor: 0.92, color: "#aebbd0" },
hb: { label: "Balmer β", center: 486.13, factor: 1.00, color: "#f6d76f" },
hbm: { label: "Balmer mimic", center: 486.24, factor: 1.00, color: "#ffe89b" },
heb: { label: "He I · β band", center: 486.00, factor: 0.78, color: "#8f9eb4" }
};
const GROUPS = [
{ name: "Fe band", base: "fe", options: ["fe", "fem", "cr"] },
{ name: "Ca K band", base: "cak", options: ["cak", "cakm", "zn"] },
{ name: "Ca H band", base: "cah", options: ["cah", "cahm", "ti"] },
{ name: "Mg band", base: "mg", options: ["mg", "mgm", "al"] },
{ name: "Na D band", base: "na", options: ["na", "nam", "he"] },
{ name: "β band", base: "hb", options: ["hb", "hbm", "heb"] }
];
const AMPLITUDES = [1.00, 0.72, 0.48, 0.95, 0.58, 0.40];
const REFERENCE = GROUPS.map((g, i) => ({
...LINE_DEFS[g.base],
key: g.base,
slot: g.name,
amp: AMPLITUDES[i]
}));
const MIMIC_MAP = GROUPS.map(g => g.options[1]);
const ALTERNATE_MAP = [
GROUPS[0].base,
GROUPS[1].base,
"ti",
GROUPS[3].base,
"he",
GROUPS[5].base
];
function shiftLines(lines, velocity) {
return lines.map(line => ({
...line,
center: line.center + line.center * velocity / C_KMS
}));
}
const CASES = {
motion: {
label: "Shared shift",
description: "The observed cube is the reference spectrum displaced by +72 km/s. Its composition remains unchanged.",
observed: shiftLines(REFERENCE, 72),
selected: [...MIMIC_MAP],
shift: 72
},
identity: {
label: "Identity swap",
description: "The Ca H and Na D identities are replaced independently, with no common wavelength displacement.",
observed: GROUPS.map((g, i) => ({
...LINE_DEFS[ALTERNATE_MAP[i]],
key: ALTERNATE_MAP[i],
slot: g.name,
amp: AMPLITUDES[i]
})),
selected: [...ALTERNATE_MAP],
shift: 0
},
mixed: {
label: "Mixed mechanisms",
description: "Two identities are replaced independently and then every observed line is displaced by +72 km/s.",
observed: shiftLines(GROUPS.map((g, i) => ({
...LINE_DEFS[ALTERNATE_MAP[i]],
key: ALTERNATE_MAP[i],
slot: g.name,
amp: AMPLITUDES[i]
})), 72),
selected: [...ALTERNATE_MAP],
shift: 72
}
};
const state = {
caseName: "motion",
selected: [...CASES.motion.selected],
shift: CASES.motion.shift
};
function selectedLines() {
return GROUPS.map((g, i) => ({
...LINE_DEFS[state.selected[i]],
key: state.selected[i],
slot: g.name,
amp: AMPLITUDES[i]
}));
}
function sampleSpectrum(lines) {
const count = Math.floor((WAVELENGTH_MAX - WAVELENGTH_MIN) / SAMPLE_STEP) + 1;
const values = new Float32Array(count);
for (let i = 0; i < count; i++) {
const wavelength = WAVELENGTH_MIN + i * SAMPLE_STEP;
let value = 0.015;
for (const line of lines) {
const d = wavelength - line.center;
value += line.amp * line.factor * Math.exp(-0.5 * d * d / (SIGMA * SIGMA));
}
values[i] = value;
}
return values;
}
function rms(a, b) {
let sum = 0;
for (let i = 0; i < a.length; i++) {
const d = a[i] - b[i];
sum += d * d;
}
return Math.sqrt(sum / a.length);
}
const viewport = document.getElementById("viewport");
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x07111e);
scene.fog = new THREE.FogExp2(0x07111e, 0.032);
const camera = new THREE.PerspectiveCamera(43, 1, 0.1, 100);
const renderer = new THREE.WebGLRenderer({ antialias: true, alpha: false });
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.12;
renderer.domElement.tabIndex = 0;
renderer.domElement.setAttribute("aria-label", "Interactive 3D spectral voxel comparison");
viewport.prepend(renderer.domElement);
const ambient = new THREE.HemisphereLight(0xa8c7ee, 0x101827, 2.0);
scene.add(ambient);
const keyLight = new THREE.DirectionalLight(0xdde9ff, 2.2);
keyLight.position.set(7, 11, 8);
scene.add(keyLight);
const rimLight = new THREE.DirectionalLight(0x557cff, 1.15);
rimLight.position.set(-8, 5, -6);
scene.add(rimLight);
const floor = new THREE.Mesh(
new THREE.PlaneGeometry(12, 9),
new THREE.MeshStandardMaterial({ color: 0x091321, roughness: 0.96, metalness: 0.02 })
);
floor.rotation.x = -Math.PI / 2;
floor.position.y = -0.055;
scene.add(floor);
const grid = new THREE.GridHelper(12, 24, 0x355174, 0x182a40);
grid.position.y = -0.035;
grid.material.transparent = true;
grid.material.opacity = 0.34;
scene.add(grid);
const axisMaterial = new THREE.LineBasicMaterial({
color: 0x5d789f,
transparent: true,
opacity: 0.48
});
function addAxisLine(points) {
const geometry = new THREE.BufferGeometry().setFromPoints(points);
scene.add(new THREE.Line(geometry, axisMaterial));
}
addAxisLine([
new THREE.Vector3(-5, 0.015, 3.55),
new THREE.Vector3(5, 0.015, 3.55)
]);
addAxisLine([
new THREE.Vector3(5, 0.015, -3.7),
new THREE.Vector3(5, 0.015, 3.7)
]);
const cameraState = {
theta: 0.72,
phi: 1.03,
radius: 14.6,
target: new THREE.Vector3(0, 1.15, 0)
};
function updateCamera() {
const sinPhi = Math.sin(cameraState.phi);
camera.position.set(
cameraState.target.x + cameraState.radius * sinPhi * Math.sin(cameraState.theta),
cameraState.target.y + cameraState.radius * Math.cos(cameraState.phi),
cameraState.target.z + cameraState.radius * sinPhi * Math.cos(cameraState.theta)
);
camera.lookAt(cameraState.target);
}
function resetCamera() {
cameraState.theta = 0.72;
cameraState.phi = 1.03;
cameraState.radius = 14.6;
cameraState.target.set(0, 1.15, 0);
updateCamera();
}
function wavelengthToX(wavelength) {
return -5 + 12 * (wavelength - WAVELENGTH_MIN) / (WAVELENGTH_MAX - WAVELENGTH_MIN);
}
function makeTextSprite(text, color = "#dce8f7", scale = 1.7) {
const canvas = document.createElement("canvas");
canvas.width = 512;
canvas.height = 96;
const ctx = canvas.getContext("2d");
ctx.fillStyle = "rgba(8, 18, 31, 0.84)";
ctx.strokeStyle = "rgba(89, 117, 159, 0.75)";
ctx.lineWidth = 3;
ctx.beginPath();
ctx.roundRect(3, 3, 506, 90, 16);
ctx.fill();
ctx.stroke();
ctx.fillStyle = color;
ctx.font = "600 34px system-ui, sans-serif";
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.fillText(text, 256, 49);
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.minFilter = THREE.LinearFilter;
const sprite = new THREE.Sprite(new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthTest: false,
depthWrite: false
}));
sprite.scale.set(scale, scale * 0.1875, 1);
sprite.renderOrder = 20;
return sprite;
}
let modelGroup = new THREE.Group();
scene.add(modelGroup);
function disposeGroup(group) {
group.traverse(object => {
if (object.geometry) object.geometry.dispose();
if (object.material) {
const materials = Array.isArray(object.material) ? object.material : [object.material];
for (const material of materials) {
if (material.map) material.map.dispose();
material.dispose();
}
}
});
scene.remove(group);
}
function buildLayer(lines, z, color, role) {
const positions = [];
const entries = [];
for (const line of lines) {
const x = wavelengthToX(line.center);
const levels = Math.max(1, Math.round(line.amp * line.factor * 13));
for (let level = 0; level < levels; level++) {
positions.push(new THREE.Vector3(x, 0.12 + level * 0.215, z));
entries.push({
role,
line: line.label,
slot: line.slot,
wavelength: line.center,
level,
levels
});
}
}
const geometry = new THREE.BoxGeometry(0.145, 0.145, 0.145);
const material = new THREE.MeshStandardMaterial({
color: 0xffffff,
emissive: new THREE.Color(color),
emissiveIntensity: 0.34,
roughness: 0.42,
metalness: 0.12,
transparent: true,
opacity: 0.92
});
const mesh = new THREE.InstancedMesh(geometry, material, positions.length);
const matrix = new THREE.Matrix4();
const instanceColor = new THREE.Color(color);
positions.forEach((position, index) => {
matrix.makeTranslation(position.x, position.y, position.z);
mesh.setMatrixAt(index, matrix);
mesh.setColorAt(index, instanceColor);
});
mesh.instanceMatrix.needsUpdate = true;
if (mesh.instanceColor) mesh.instanceColor.needsUpdate = true;
if (mesh.computeBoundingSphere) mesh.computeBoundingSphere();
mesh.userData.entries = entries;
modelGroup.add(mesh);
const stemPoints = [];
for (const line of lines) {
const x = wavelengthToX(line.center);
const top = 0.12 + Math.max(1, Math.round(line.amp * line.factor * 13)) * 0.215;
stemPoints.push(new THREE.Vector3(x, 0.03, z));
stemPoints.push(new THREE.Vector3(x, top, z));
}
const stemGeometry = new THREE.BufferGeometry().setFromPoints(stemPoints);
const stemMaterial = new THREE.LineBasicMaterial({
color,
transparent: true,
opacity: 0.28
});
modelGroup.add(new THREE.LineSegments(stemGeometry, stemMaterial));
return positions.length;
}
function rebuildScene(observed, composition, motion) {
disposeGroup(modelGroup);
modelGroup = new THREE.Group();
scene.add(modelGroup);
buildLayer(observed, 2.55, COLORS.observed, "Observed");
buildLayer(composition, 0, COLORS.composition, "Composition");
buildLayer(motion, -2.55, COLORS.motion, "Motion");
const layerLabels = [
["OBSERVED", COLORS.observed, 2.55],
["COMPOSITION", COLORS.composition, 0],
["MOTION", COLORS.motion, -2.55]
];
for (const [text, color, z] of layerLabels) {
const sprite = makeTextSprite(text, color, 1.58);
sprite.position.set(-5.18, 0.22, z);
modelGroup.add(sprite);
}
for (let wavelength = 400; wavelength <= 600; wavelength += 50) {
const sprite = makeTextSprite(`${wavelength} nm`, "#8fa4bd", 0.77);
sprite.position.set(wavelengthToX(wavelength), 0.04, 3.72);
modelGroup.add(sprite);
}
const intensityLabel = makeTextSprite("INTENSITY", "#70869f", 1.2);
intensityLabel.position.set(5.12, 1.7, 3.55);
modelGroup.add(intensityLabel);
const wavelengthLabel = makeTextSprite("WAVELENGTH", "#70869f", 1.25);
wavelengthLabel.position.set(0, 0.02, 4.12);
modelGroup.add(wavelengthLabel);
}
const identityList = document.getElementById("identityList");
const identitySelects = [];
GROUPS.forEach((group, index) => {
const row = document.createElement("div");
row.className = "identity-row";
const dot = document.createElement("i");
dot.className = "identity-dot";
const name = document.createElement("div");
name.className = "identity-name";
name.textContent = group.name;
const select = document.createElement("select");
select.className = "identity-select";
select.setAttribute("aria-label", `${group.name} line identity`);
for (const key of group.options) {
const option = document.createElement("option");
option.value = key;
option.textContent = `${LINE_DEFS[key].label} · ${LINE_DEFS[key].center.toFixed(2)}`;
select.appendChild(option);
}
select.addEventListener("change", () => {
state.selected[index] = select.value;
updateAll();
});
row.append(dot, name, select);
identityList.appendChild(row);
identitySelects.push(select);
});
function updateIdentityControls() {
identitySelects.forEach((select, index) => {
const key = state.selected[index];
select.value = key;
const dot = select.parentElement.querySelector(".identity-dot");
dot.style.background = LINE_DEFS[key].color;
dot.style.color = LINE_DEFS[key].color;
});
}
function formatSigned(value, digits = 1) {
return `${value >= 0 ? "+" : ""}${value.toFixed(digits)}`;
}
function updateAnalysis(observedLines, compositionLines, motionLines, observedSpec, compSpec, motionSpec) {
const compRMS = rms(observedSpec, compSpec);
const motionRMS = rms(observedSpec, motionSpec);
const gap = Math.abs(compRMS - motionRMS);
const best = Math.min(compRMS, motionRMS);
const ambiguity = Math.round(
100 *
Math.exp(-gap / 0.015) *
Math.exp(-best / 0.03)
);
const changed = state.selected.filter((key, i) => key !== GROUPS[i].base).length;
document.getElementById("ambiguityValue").textContent = `${ambiguity}%`;
document.getElementById("compositionScore").textContent = compRMS.toFixed(4);
document.getElementById("motionScore").textContent = motionRMS.toFixed(4);
document.getElementById("compositionDetail").textContent = `${changed} / 6 non-reference`;
document.getElementById("motionDetail").textContent = `${formatSigned(state.shift)} km/s`;
const verdict = document.getElementById("verdict");
verdict.className = "verdict";
if (compRMS < FIT_THRESHOLD && motionRMS < FIT_THRESHOLD) {
verdict.textContent = "Ambiguous: both explanations fit the observed voxel spectrum.";
} else if (compRMS < FIT_THRESHOLD && motionRMS >= FIT_THRESHOLD) {
verdict.classList.add("composition");
verdict.textContent = "Composition favored: the independent identity map reproduces the line centers.";
} else if (motionRMS < FIT_THRESHOLD && compRMS >= FIT_THRESHOLD) {
verdict.classList.add("motion");
verdict.textContent = "Motion favored: one common shift reproduces the line centers.";
} else {
verdict.classList.add("neither");
verdict.textContent = "Neither single mechanism is sufficient; identity changes and a common shift may both be needed.";
}
}
const chart = document.getElementById("spectrumChart");
chart.userData = {};
const CHART = { width: 1000, height: 185, left: 43, right: 14, top: 12, bottom: 28 };
let chartScale = { maxY: 1, y: () => 0 };
function chartX(wavelength) {
return CHART.left +
(CHART.width - CHART.left - CHART.right) *
(wavelength - WAVELENGTH_MIN) / (WAVELENGTH_MAX - WAVELENGTH_MIN);
}
function chartY(value) {
return CHART.top +
(CHART.height - CHART.top - CHART.bottom) *
(1 - value / chartScale.maxY);
}
function pathFor(values) {
let path = "";
for (let i = 0; i < values.length; i++) {
const wavelength = WAVELENGTH_MIN + i * SAMPLE_STEP;
path += `${i === 0 ? "M" : "L"}${chartX(wavelength).toFixed(2)},${chartY(values[i]).toFixed(2)} `;
}
return path.trim();
}
function renderChart(observedSpec, compSpec, motionSpec) {
let maxY = 0;
for (const value of observedSpec) maxY = Math.max(maxY, value);
chartScale.maxY = maxY * 1.16;
const observedPath = pathFor(observedSpec);
const compPath = pathFor(compSpec);
const motionPath = pathFor(motionSpec);
const baseY = chartY(0);
const lastX = chartX(WAVELENGTH_MAX);
const firstX = chartX(WAVELENGTH_MIN);
let grid = "";
for (let i = 0; i <= 4; i++) {
const value = chartScale.maxY * i / 4;
const y = chartY(value);
grid += `
<line x1="${CHART.left}" x2="${CHART.width - CHART.right}" y1="${y}" y2="${y}"
stroke="#1d3048" stroke-width="1"/>
<text x="${CHART.left - 8}" y="${y + 3}" text-anchor="end"
fill="#617791" font-size="9">${value.toFixed(1)}</text>`;
}
let ticks = "";
for (let wavelength = 400; wavelength <= 600; wavelength += 50) {
const x = chartX(wavelength);
ticks += `
<line x1="${x}" x2="${x}" y1="${baseY}" y2="${baseY + 4}" stroke="#49617f"/>
<text x="${x}" y="${CHART.height - 8}" text-anchor="middle"
fill="#71869e" font-size="9">${wavelength}</text>`;
}
chart.setAttribute("viewBox", `0 0 ${CHART.width} ${CHART.height}`);
chart.innerHTML = `
<defs>
<linearGradient id="observedFill" x1="0" y1="0" x2="0" y2="1">
<stop offset="0" stop-color="#f6c85f" stop-opacity="0.22"/>
<stop offset="1" stop-color="#f6c85f" stop-opacity="0"/>
</linearGradient>
</defs>
${grid}
<path d="${observedPath} L${lastX},${baseY} L${firstX},${baseY} Z" fill="url(#observedFill)"/>
<path d="${motionPath}" fill="none" stroke="#56cfe1" stroke-width="1.45" opacity="0.9"/>
<path d="${compPath}" fill="none" stroke="#ff6b6b" stroke-width="1.55" opacity="0.92"/>
<path d="${observedPath}" fill="none" stroke="#f6c85f" stroke-width="2.3"/>
${ticks}
<text x="${(CHART.left + CHART.width - CHART.right) / 2}" y="${CHART.height - 8}"
text-anchor="middle" fill="#627890" font-size="9">WAVELENGTH (nm)</text>
<g id="chartCursor" visibility="hidden">
<line id="cursorLine" x1="0" x2="0" y1="${CHART.top}" y2="${baseY}"
stroke="#9fb4ce" stroke-width="1" stroke-dasharray="3 3"/>
<circle id="cursorObserved" r="3.5" fill="#f6c85f"/>
<circle id="cursorComposition" r="3.5" fill="#ff6b6b"/>
<circle id="cursorMotion" r="3.5" fill="#56cfe1"/>
</g>`;
}
chart.addEventListener("pointermove", event => {
const rect = chart.getBoundingClientRect();
const x = (event.clientX - rect.left) / rect.width * CHART.width;
const wavelength = WAVELENGTH_MIN +
(x - CHART.left) / (CHART.width - CHART.left - CHART.right) *
(WAVELENGTH_MAX - WAVELENGTH_MIN);
if (wavelength < WAVELENGTH_MIN || wavelength > WAVELENGTH_MAX) return;
const index = Math.max(0, Math.min(
Math.round((wavelength - WAVELENGTH_MIN) / SAMPLE_STEP),
Math.floor((WAVELENGTH_MAX - WAVELENGTH_MIN) / SAMPLE_STEP)
));
const cursor = chart.querySelector("#chartCursor");
const line = chart.querySelector("#cursorLine");
const observedCircle = chart.querySelector("#cursorObserved");
const compositionCircle = chart.querySelector("#cursorComposition");
const motionCircle = chart.querySelector("#cursorMotion");
const px = chartX(WAVELENGTH_MIN + index * SAMPLE_STEP);
cursor.setAttribute("visibility", "visible");
line.setAttribute("x1", px);
line.setAttribute("x2", px);
const values = chart.userData.values;
observedCircle.setAttribute("cx", px);
observedCircle.setAttribute("cy", chartY(values.observed[index]));
compositionCircle.setAttribute("cx", px);
compositionCircle.setAttribute("cy", chartY(values.composition[index]));
motionCircle.setAttribute("cx", px);
motionCircle.setAttribute("cy", chartY(values.motion[index]));
document.getElementById("chartReadout").textContent =
`${(WAVELENGTH_MIN + index * SAMPLE_STEP).toFixed(1)} nm · ` +
`obs ${values.observed[index].toFixed(3)} · comp ${values.composition[index].toFixed(3)} · ` +
`motion ${values.motion[index].toFixed(3)}`;
});
chart.addEventListener("pointerleave", () => {
const cursor = chart.querySelector("#chartCursor");
if (cursor) cursor.setAttribute("visibility", "hidden");
document.getElementById("chartReadout").textContent = "Move across the spectrum to inspect";
});
function updateShiftControl() {
document.getElementById("shiftSlider").value = state.shift;
document.getElementById("shiftOutput").textContent = `${formatSigned(state.shift)} km/s`;
const deltaAt500 = 500 * state.shift / C_KMS;
document.getElementById("shiftDetail").textContent =
`Equivalent to ${formatSigned(deltaAt500, 3)} nm at 500 nm; all relative line spacing is preserved.`;
}
function updateAll() {
const currentCase = CASES[state.caseName];
const observedLines = currentCase.observed;
const compositionLines = selectedLines();
const motionLines = shiftLines(REFERENCE, state.shift);
const observedSpec = sampleSpectrum(observedLines);
const compSpec = sampleSpectrum(compositionLines);
const motionSpec = sampleSpectrum(motionLines);
updateIdentityControls();
updateShiftControl();
updateAnalysis(observedLines, compositionLines, motionLines, observedSpec, compSpec, motionSpec);
renderChart(observedSpec, compSpec, motionSpec);
chart.userData.values = {
observed: observedSpec,
composition: compSpec,
motion: motionSpec
};
rebuildScene(observedLines, compositionLines, motionLines);
}
document.querySelectorAll(".preset").forEach(button => {
button.addEventListener("click", () => {
const caseName = button.dataset.case;
state.caseName = caseName;
state.selected = [...CASES[caseName].selected];
state.shift = CASES[caseName].shift;
document.querySelectorAll(".preset").forEach(item => {
item.classList.toggle("active", item === button);
});
const currentCase = CASES[caseName];
document.getElementById("caseChip").textContent = currentCase.label;
document.getElementById("caseDescription").textContent = currentCase.description;
updateAll();
});
});
document.getElementById("shiftSlider").addEventListener("input", event => {
state.shift = Number(event.target.value);
updateAll();
});
document.getElementById("fitComposition").addEventListener("click", () => {
const observed = CASES[state.caseName].observed;
state.selected = GROUPS.map((group, index) => {
let bestKey = group.options[0];
let bestDistance = Infinity;
for (const key of group.options) {
const distance = Math.abs(LINE_DEFS[key].center - observed[index].center);
if (distance < bestDistance) {
bestDistance = distance;
bestKey = key;
}
}
return bestKey;
});
updateAll();
});
document.getElementById("fitMotion").addEventListener("click", () => {
const observedSpec = sampleSpectrum(CASES[state.caseName].observed);
let bestShift = 0;
let bestRMS = Infinity;
for (let shift = -120; shift <= 120; shift += 0.5) {
const candidate = sampleSpectrum(shiftLines(REFERENCE, shift));
const error = rms(observedSpec, candidate);
if (error < bestRMS) {
bestRMS = error;
bestShift = shift;
}
}
state.shift = bestShift;
updateAll();
});
document.getElementById("resetIdentities").addEventListener("click", () => {
state.selected = GROUPS.map(group => group.base);
updateAll();
});
document.getElementById("resetAll").addEventListener("click", () => {
const caseName = state.caseName;
state.selected = [...CASES[caseName].selected];
state.shift = CASES[caseName].shift;
resetCamera();
updateAll();
});
document.getElementById("resetView").addEventListener("click", resetCamera);
const raycaster = new THREE.Raycaster();
const pointer = new THREE.Vector2();
const tooltip = document.getElementById("voxelTooltip");
let dragging = false;
let movedWhileDragging = false;
let lastPointerX = 0;
let lastPointerY = 0;
function updateTooltip(event) {
if (dragging) {
tooltip.style.display = "none";
return;
}
const rect = renderer.domElement.getBoundingClientRect();
pointer.x = ((event.clientX - rect.left) / rect.width) * 2 - 1;
pointer.y = -((event.clientY - rect.top) / rect.height) * 2 + 1;
raycaster.setFromCamera(pointer, camera);
const hits = raycaster.intersectObjects(modelGroup.children, true);
const hit = hits.find(item => item.object.isInstancedMesh);
if (!hit) {
tooltip.style.display = "none";
return;
}
const entry = hit.object.userData.entries[hit.instanceId];
if (!entry) {
tooltip.style.display = "none";
return;
}
tooltip.innerHTML = `
<strong>${entry.role} voxel</strong>
${entry.line} · ${entry.slot}<br>
λ = ${entry.wavelength.toFixed(3)} nm<br>
Intensity voxel ${entry.level + 1} / ${entry.levels}`;
const viewportRect = viewport.getBoundingClientRect();
let left = event.clientX - viewportRect.left + 14;
let top = event.clientY - viewportRect.top + 14;
left = Math.min(left, viewport.clientWidth - 245);
top = Math.min(top, viewport.clientHeight - 100);
tooltip.style.left = `${left}px`;
tooltip.style.top = `${top}px`;
tooltip.style.display = "block";
}
renderer.domElement.addEventListener("pointerdown", event => {
if (event.button !== 0) return;
dragging = true;
movedWhileDragging = false;
lastPointerX = event.clientX;
lastPointerY = event.clientY;
renderer.domElement.classList.add("dragging");
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", event => {
if (dragging) {
const dx = event.clientX - lastPointerX;
const dy = event.clientY - lastPointerY;
if (Math.abs(dx) + Math.abs(dy) > 2) movedWhileDragging = true;
cameraState.theta -= dx * 0.005;
cameraState.phi = THREE.MathUtils.clamp(cameraState.phi - dy * 0.005, 0.28, 1.46);
lastPointerX = event.clientX;
lastPointerY = event.clientY;
updateCamera();
tooltip.style.display = "none";
} else {
updateTooltip(event);
}
});
renderer.domElement.addEventListener("pointerup", event => {
dragging = false;
renderer.domElement.classList.remove("dragging");
if (renderer.domElement.hasPointerCapture(event.pointerId)) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
});
renderer.domElement.addEventListener("pointercancel", () => {
dragging = false;
renderer.domElement.classList.remove("dragging");
});
renderer.domElement.addEventListener("pointerleave", () => {
if (!dragging) tooltip.style.display = "none";
});
renderer.domElement.addEventListener("wheel", event => {
event.preventDefault();
cameraState.radius = THREE.MathUtils.clamp(
cameraState.radius * Math.exp(event.deltaY * 0.001),
7.5,
27
);
updateCamera();
}, { passive: false });
renderer.domElement.addEventListener("dblclick", resetCamera);
renderer.domElement.addEventListener("keydown", event => {
const step = 0.09;
if (event.key === "ArrowLeft") cameraState.theta += step;
else if (event.key === "ArrowRight") cameraState.theta -= step;
else if (event.key === "ArrowUp") cameraState.phi = Math.max(0.28, cameraState.phi - step);
else if (event.key === "ArrowDown") cameraState.phi = Math.min(1.46, cameraState.phi + step);
else if (event.key === "+" || event.key === "=") cameraState.radius = Math.max(7.5, cameraState.radius - 0.7);
else if (event.key === "-") cameraState.radius = Math.min(27, cameraState.radius + 0.7);
else return;
event.preventDefault();
updateCamera();
});
function resize() {
const width = viewport.clientWidth;
const height = viewport.clientHeight;
if (!width || !height) return;
renderer.setSize(width, height, false);
camera.aspect = width / height;
camera.updateProjectionMatrix();
}
new ResizeObserver(resize).observe(viewport);
resize();
resetCamera();
document.getElementById("caseDescription").textContent = CASES.motion.description;
updateAll();
function animate() {
requestAnimationFrame(animate);
updateCamera();
renderer.render(scene, camera);
}
animate();
</script>
</body>
</html>
```