Prompt specification
Build or customize the Three.js 3D scene "Optics workshop rainbow station" 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: Optics workshop rainbow station
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/optics-workshop-rainbow-station-x1535
Scene Description Prompt:
Please use Three.js from cdnjs for a voxel scene based on this idea: optics workshop rainbow station. Compose a white-light source, slit, prism, and screen with a schematic spectrum; keep ray paths connected and state that displayed color widths are illustrative. Return one self-contained HTML file.
Technical checks: Build the optical bench, light source, prism, and color display from visibly stepped voxel blocks. Label illustrated beam widths as schematic.
Return one complete HTML document that works with the cdnjs Three.js r160 module inside a sandbox with scripts allowed. Make the requested subject visible at the default camera. Use procedural assets and local button handlers. Do not use native form submission, pointer lock, external assets or runtime requests other than Three.js.
Working Three.js Starter (optics-workshop-rainbow-station-x1535):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Voxel Optics Workshop — Rainbow Station</title>
<link rel="icon" href="data:,">
<style>
:root {
color-scheme: dark;
--panel: rgba(10, 18, 34, 0.92);
--line: rgba(151, 190, 224, 0.24);
--text: #eef7ff;
--muted: #a9bed2;
--accent: #6ee7d2;
}
* { box-sizing: border-box; }
html, body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #08111f;
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}
canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
cursor: grab;
}
canvas.dragging { cursor: grabbing; }
#panel {
position: fixed;
z-index: 20;
top: 14px;
left: 14px;
width: min(350px, calc(100vw - 28px));
padding: 15px;
border: 1px solid var(--line);
border-radius: 14px;
background: var(--panel);
color: var(--text);
box-shadow: 0 16px 48px rgba(0, 0, 0, 0.35);
backdrop-filter: blur(12px);
}
h1 {
margin: 0 0 8px;
font-size: 16px;
line-height: 1.35;
letter-spacing: 0.025em;
}
#objective {
margin: 0 0 12px;
color: var(--muted);
font-size: 11px;
line-height: 1.55;
}
.control-row {
display: flex;
align-items: center;
gap: 6px;
margin: 8px 0;
flex-wrap: wrap;
}
.control-label {
width: 78px;
flex: 0 0 78px;
color: #cfe3f4;
font-size: 10px;
text-transform: uppercase;
letter-spacing: 0.07em;
}
button {
appearance: none;
border: 1px solid #35506c;
border-radius: 7px;
padding: 7px 9px;
background: #15263c;
color: #dbefff;
font: inherit;
font-size: 10px;
cursor: pointer;
transition: background 120ms ease, border-color 120ms ease, transform 120ms ease;
}
button:hover {
background: #203a58;
border-color: #68a4cb;
transform: translateY(-1px);
}
button.active {
background: #225d62;
border-color: var(--accent);
color: #f1fffc;
}
.note {
margin-top: 12px;
padding: 9px 10px;
border-left: 3px solid #f6c453;
border-radius: 5px;
background: rgba(246, 196, 83, 0.08);
color: #f7e8b2;
font-size: 10px;
line-height: 1.5;
}
#geometryStatus {
margin-top: 9px;
color: #83e6c8;
font-size: 10px;
}
#legend {
position: fixed;
z-index: 15;
right: 14px;
bottom: 14px;
display: flex;
gap: 4px;
padding: 8px;
border: 1px solid var(--line);
border-radius: 9px;
background: rgba(8, 17, 31, 0.86);
color: #d9e9f8;
font-size: 9px;
pointer-events: none;
}
.swatch {
width: 23px;
height: 13px;
border-radius: 2px;
box-shadow: inset 0 0 0 1px rgba(255,255,255,0.16);
}
.world-label {
position: fixed;
z-index: 10;
transform: translate(-50%, -50%);
padding: 6px 9px;
border: 1px solid rgba(150, 199, 231, 0.36);
border-radius: 999px;
background: rgba(7, 16, 29, 0.86);
color: #ecf8ff;
font-size: 10px;
line-height: 1;
white-space: nowrap;
pointer-events: none;
box-shadow: 0 5px 16px rgba(0,0,0,0.3);
}
.world-label::before {
content: attr(data-number);
display: inline-grid;
place-items: center;
width: 17px;
height: 17px;
margin-right: 6px;
border-radius: 50%;
background: #6ee7d2;
color: #072128;
font-weight: 800;
}
.world-label.emphasis {
border-color: rgba(246, 196, 83, 0.58);
color: #ffe9a7;
}
#loading {
position: fixed;
inset: 0;
z-index: 50;
display: grid;
place-items: center;
background: #08111f;
color: #b9d8e9;
font-size: 13px;
}
@media (max-width: 700px) {
#panel {
top: 8px;
left: 8px;
width: calc(100vw - 16px);
padding: 11px;
}
#objective { display: none; }
.control-row { margin: 5px 0; }
.control-label { width: 66px; flex-basis: 66px; }
button { padding: 6px 7px; font-size: 9px; }
.note { margin-top: 8px; padding: 7px 8px; }
#legend { right: 8px; bottom: 8px; transform: scale(0.9); transform-origin: bottom right; }
.world-label { font-size: 8px; padding: 5px 7px; }
}
</style>
</head>
<body>
<canvas id="scene" aria-label="Interactive voxel optics workshop showing a white-light source, slit, prism, and spectrum screen"></canvas>
<aside id="panel">
<h1>Voxel Optics Workshop · Rainbow Station</h1>
<p id="objective">
Learning objective: observe a connected white-light path passing through a slit and a voxel prism,
then separating into a schematic spectrum on the screen.
</p>
<div class="control-row">
<span class="control-label">Slit</span>
<button type="button" data-gap="0.14">Narrow</button>
<button type="button" data-gap="0.28" class="active">Medium</button>
<button type="button" data-gap="0.42">Wide</button>
</div>
<div class="control-row">
<span class="control-label">Dispersion</span>
<button type="button" data-dispersion="0.65">Soft</button>
<button type="button" data-dispersion="1" class="active">Standard</button>
<button type="button" data-dispersion="1.35">Wide</button>
</div>
<div class="control-row">
<span class="control-label">Camera</span>
<button type="button" id="overview" class="active">Overview</button>
<button type="button" id="prismView">Prism detail</button>
</div>
<div class="note">
<strong>Schematic model:</strong> the displayed color-band widths and the chromatic separation
are illustrative, not results of a physical ray-tracing simulation. Every source–slit–prism–screen
ray is nevertheless geometrically connected.
</div>
<div id="geometryStatus" aria-live="polite">Checking connected geometry…</div>
</aside>
<div id="legend" aria-label="Visible spectrum colors">
<span class="swatch" style="background:#9b4dff"></span>
<span class="swatch" style="background:#2f6bff"></span>
<span class="swatch" style="background:#28d7e8"></span>
<span class="swatch" style="background:#35d06f"></span>
<span class="swatch" style="background:#ffd21f"></span>
<span class="swatch" style="background:#ff8c1a"></span>
<span class="swatch" style="background:#ff3b30"></span>
</div>
<div id="loading">Building the voxel optics station…</div>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const canvas = document.getElementById("scene");
const statusEl = document.getElementById("geometryStatus");
const renderer = new THREE.WebGLRenderer({
canvas,
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.18;
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x08111f);
scene.fog = new THREE.Fog(0x08111f, 19, 34);
const camera = new THREE.OrthographicCamera(-8, 8, 5, -5, 0.1, 100);
const cameraState = {
target: new THREE.Vector3(-0.55, 1.70, 0),
radius: 17.2,
theta: 0.33,
phi: 1.34,
zoom: 1
};
const hemi = new THREE.HemisphereLight(0xc9edff, 0x182038, 1.75);
scene.add(hemi);
const key = new THREE.DirectionalLight(0xffffff, 3.0);
key.position.set(-3, 9, 8);
scene.add(key);
const rim = new THREE.DirectionalLight(0x6edcff, 1.7);
rim.position.set(7, 4, -8);
scene.add(rim);
const sourceLight = new THREE.PointLight(0xfff6d8, 2.8, 5.0, 2);
sourceLight.position.set(-5.25, 1.55, 0.25);
scene.add(sourceLight);
const prismLight = new THREE.PointLight(0x62d9ff, 1.1, 4.0, 2);
prismLight.position.set(0.15, 1.65, 0.7);
scene.add(prismLight);
const unitBox = new THREE.BoxGeometry(1, 1, 1);
const yAxis = new THREE.Vector3(0, 1, 0);
function material(color, options = {}) {
return new THREE.MeshStandardMaterial({
color,
roughness: 0.72,
metalness: 0.06,
flatShading: true,
...options
});
}
const materials = {
floor: material(0x0b1728),
bench: material(0x273d54),
benchAlt: material(0x304c65),
benchEdge: material(0x6f8ca3),
source: material(0x17646c, { roughness: 0.56 }),
sourceAlt: material(0x25868a, { roughness: 0.52 }),
sourceDark: material(0x103b4c),
sourceWhite: material(0xfffbe8, {
emissive: 0xffeaa5,
emissiveIntensity: 1.7,
roughness: 0.25
}),
orange: material(0xf2a541, { roughness: 0.52 }),
slitDark: material(0x33475a),
prismA: material(0x6fd9ff, {
transparent: true,
opacity: 0.43,
depthWrite: false,
roughness: 0.18,
metalness: 0.08,
emissive: 0x123e58,
emissiveIntensity: 0.42,
side: THREE.DoubleSide
}),
prismB: material(0x8be8ff, {
transparent: true,
opacity: 0.34,
depthWrite: false,
roughness: 0.14,
emissive: 0x164f68,
emissiveIntensity: 0.34,
side: THREE.DoubleSide
}),
screen: material(0x101d31, { roughness: 0.92 }),
screenAlt: material(0x16263d, { roughness: 0.92 }),
frame: material(0x617b91, { roughness: 0.6 })
};
function block(parent, x, y, z, sx, sy, sz, mat) {
const mesh = new THREE.Mesh(unitBox, mat);
mesh.position.set(x, y, z);
mesh.scale.set(sx, sy, sz);
parent.add(mesh);
return mesh;
}
function clearGroup(group) {
while (group.children.length) group.remove(group.children[group.children.length - 1]);
}
const EMIT = new THREE.Vector3(-5.58, 1.55, 0);
const SLIT = new THREE.Vector3(-3.25, 1.55, 0);
const ENTRY = new THREE.Vector3(-1.05, 1.55, 0);
const SCREEN_X = 5.84;
const PRISM_A = new THREE.Vector3(-1.05, 0.20, 0);
const PRISM_B = new THREE.Vector3(-1.05, 2.70, 0);
const PRISM_C = new THREE.Vector3(1.30, 1.45, 0);
const state = {
gap: 0.28,
dispersion: 1,
validated: false
};
const opticalRoot = new THREE.Group();
scene.add(opticalRoot);
const sourceGroup = new THREE.Group();
const slitGroup = new THREE.Group();
const prismGroup = new THREE.Group();
const screenGroup = new THREE.Group();
const spectrumGroup = new THREE.Group();
const rayGroup = new THREE.Group();
opticalRoot.add(sourceGroup, slitGroup, prismGroup, screenGroup, spectrumGroup, rayGroup);
// A tiled optical bench: all large scene props are visibly assembled from blocks.
const benchGroup = new THREE.Group();
scene.add(benchGroup);
for (let x = -7.75; x < 6.75; x += 0.48) {
for (let z = -1.48; z <= 1.48; z += 0.58) {
block(
benchGroup,
x + 0.22,
-0.38,
z,
0.455,
0.30,
0.55,
(Math.round(x * 10) + Math.round(z * 10)) % 3 === 0 ? materials.benchAlt : materials.bench
);
}
}
block(benchGroup, -0.45, -0.63, 0, 14.6, 0.12, 3.2, materials.benchEdge);
const floor = new THREE.Mesh(
new THREE.PlaneGeometry(80, 80),
material(0x091426, { roughness: 1 })
);
floor.rotation.x = -Math.PI / 2;
floor.position.y = -0.72;
scene.add(floor);
const floorGrid = new THREE.GridHelper(40, 40, 0x274158, 0x17293c);
floorGrid.position.y = -0.705;
floorGrid.material.transparent = true;
floorGrid.material.opacity = 0.38;
scene.add(floorGrid);
// Voxel cylindrical source: a stepped circular cross-section made from individual cubes.
function buildSource() {
clearGroup(sourceGroup);
const cell = 0.21;
const sourceY = 1.55;
const sourceX = -7.25;
for (let ix = 0; ix < 8; ix++) {
const x = sourceX + ix * 0.22;
for (let iy = -3; iy <= 3; iy++) {
for (let iz = -3; iz <= 3; iz++) {
const dy = iy * cell;
const dz = iz * cell;
const radius = Math.hypot(dy, dz);
if (radius > 0.69) continue;
const front = ix === 7;
let mat = (ix + iy + iz) % 3 === 0 ? materials.sourceAlt : materials.source;
if (front && radius < 0.46) mat = materials.sourceWhite;
if (front && radius >= 0.46) mat = materials.sourceDark;
block(sourceGroup, x, sourceY + dy, dz, 0.195, 0.195, 0.195, mat);
}
}
}
for (let i = 0; i < 4; i++) {
block(sourceGroup, -6.85 + i * 0.22, 2.39, 0, 0.18, 0.15, 0.18, materials.orange);
}
for (let y = -0.18; y < 0.85; y += 0.20) {
block(sourceGroup, -6.55, y, 0, 0.52, 0.18, 0.52, materials.sourceDark);
}
block(sourceGroup, -6.55, -0.17, 0, 1.15, 0.18, 1.00, materials.sourceAlt);
sourceGroup.userData.output = EMIT.clone();
}
// The slit is rebuilt for every aperture state; the ray always passes through its center.
function buildSlit() {
clearGroup(slitGroup);
const step = 0.15;
const railThickness = 0.135;
const gapZ = 0.17;
for (let zi = -5; zi <= 5; zi++) {
const z = zi * step;
block(
slitGroup,
SLIT.x,
SLIT.y + state.gap / 2 + railThickness / 2,
z,
0.16,
railThickness,
0.135,
materials.orange
);
block(
slitGroup,
SLIT.x,
SLIT.y - state.gap / 2 - railThickness / 2,
z,
0.16,
railThickness,
0.135,
materials.orange
);
}
for (let yi = -6; yi <= 6; yi++) {
const yOffset = yi * step;
if (Math.abs(yOffset) < state.gap / 2 + 0.025) continue;
for (const side of [-1, 1]) {
block(
slitGroup,
SLIT.x,
SLIT.y + yOffset,
side * (gapZ / 2 + railThickness / 2),
0.16,
0.135,
0.135,
materials.slitDark
);
}
}
for (let i = 0; i < 4; i++) {
block(slitGroup, SLIT.x, -0.04 - i * 0.19, 0, 0.34, 0.17, 0.52, materials.slitDark);
}
slitGroup.userData.opening = state.gap;
}
function pointInTriangle(x, y, a, b, c) {
const d1 = (x - b.x) * (a.y - b.y) - (a.x - b.x) * (y - b.y);
const d2 = (x - c.x) * (b.y - c.y) - (b.x - c.x) * (y - c.y);
const d3 = (x - a.x) * (c.y - a.y) - (c.x - a.x) * (y - a.y);
const hasNeg = d1 < 0 || d2 < 0 || d3 < 0;
const hasPos = d1 > 0 || d2 > 0 || d3 > 0;
return !(hasNeg && hasPos);
}
// Triangular prism assembled from separated glass cubes, never a continuous triangular mesh.
function buildPrism() {
clearGroup(prismGroup);
const step = 0.23;
const cube = 0.211;
for (let z = -0.66; z <= 0.661; z += 0.22) {
for (let x = PRISM_A.x + step * 0.5; x < PRISM_C.x; x += step) {
for (let y = PRISM_A.y + step * 0.5; y < PRISM_B.y; y += step) {
if (!pointInTriangle(x, y, PRISM_A, PRISM_B, PRISM_C)) continue;
const mat = ((Math.round(x / step) + Math.round(y / step) + Math.round(z / step)) % 3 === 0)
? materials.prismB
: materials.prismA;
block(prismGroup, x, y, z, cube, cube, cube, mat);
}
}
}
for (let i = 0; i < 3; i++) {
block(prismGroup, 0.05, 0.02 + i * 0.17, 0, 1.10, 0.15, 0.78, materials.slitDark);
}
block(prismGroup, 0.05, -0.16, 0, 2.10, 0.18, 1.30, materials.benchEdge);
}
const bandDefinitions = [
{ name: "Violet", color: 0x9b4dff, count: 3, exitT: 0.82 },
{ name: "Blue", color: 0x2f6bff, count: 5, exitT: 0.70 },
{ name: "Cyan", color: 0x28d7e8, count: 4, exitT: 0.58 },
{ name: "Green", color: 0x35d06f, count: 6, exitT: 0.46 },
{ name: "Yellow", color: 0xffd21f, count: 4, exitT: 0.34 },
{ name: "Orange", color: 0xff8c1a, count: 5, exitT: 0.25 },
{ name: "Red", color: 0xff3b30, count: 4, exitT: 0.16 }
];
const bandStep = 0.085;
let bandCursor = 0.95;
function buildScreen() {
clearGroup(screenGroup);
clearGroup(spectrumGroup);
// Back plate and frame are also grids of voxels.
for (let yi = 0; yi < 17; yi++) {
for (let zi = 0; zi < 13; zi++) {
block(
screenGroup,
6.04,
0.58 + yi * 0.20,
-1.20 + zi * 0.20,
0.13,
0.18,
0.18,
(yi + zi) % 4 === 0 ? materials.screenAlt : materials.screen
);
}
}
for (let zi = 0; zi < 14; zi++) {
const z = -1.30 + zi * 0.20;
block(screenGroup, 5.94, 0.48, z, 0.22, 0.18, 0.18, materials.frame);
block(screenGroup, 5.94, 3.78, z, 0.22, 0.18, 0.18, materials.frame);
}
for (let yi = 0; yi < 18; yi++) {
const y = 0.48 + yi * 0.20;
block(screenGroup, 5.94, y, -1.38, 0.22, 0.18, 0.18, materials.frame);
block(screenGroup, 5.94, y, 1.38, 0.22, 0.18, 0.18, materials.frame);
}
for (const x of [5.35, 6.35]) {
for (let i = 0; i < 4; i++) {
block(screenGroup, x, 0.02 + i * 0.18, 0, 0.25, 0.16, 0.58, materials.slitDark);
}
}
bandCursor = 0.95;
for (const band of bandDefinitions) {
const mat = material(band.color, {
emissive: band.color,
emissiveIntensity: 0.48,
roughness: 0.42,
metalness: 0.02
});
band.baseMin = bandCursor;
band.baseMax = bandCursor + band.count * bandStep;
band.center = (band.baseMin + band.baseMax) / 2;
band.width = band.baseMax - band.baseMin;
for (let row = 0; row < band.count; row++) {
for (let col = 0; col < 9; col++) {
block(
spectrumGroup,
SCREEN_X,
band.baseMin + (row + 0.5) * bandStep,
(col - 4) * 0.082,
0.11,
bandStep * 0.91,
0.072,
mat
);
}
}
bandCursor = band.baseMax;
}
}
const beamMaterialCache = new Map();
function beamMaterial(color) {
if (!beamMaterialCache.has(color)) {
beamMaterialCache.set(color, new THREE.MeshBasicMaterial({
color,
toneMapped: false,
transparent: true,
opacity: 0.98
}));
}
return beamMaterialCache.get(color);
}
function addBeamSegment(parent, from, to, color, width = 0.045) {
const direction = new THREE.Vector3().subVectors(to, from);
const length = direction.length();
const mesh = new THREE.Mesh(unitBox, beamMaterial(color));
mesh.position.copy(from).add(to).multiplyScalar(0.5);
mesh.scale.set(width, length, width);
mesh.quaternion.setFromUnitVectors(yAxis, direction.normalize());
mesh.renderOrder = 3;
parent.add(mesh);
return { mesh, from: from.clone(), to: to.clone() };
}
function addNode(parent, point, color, size = 0.085) {
const mesh = new THREE.Mesh(unitBox, beamMaterial(color));
mesh.position.copy(point);
mesh.scale.set(size, size, size);
mesh.renderOrder = 4;
parent.add(mesh);
return mesh;
}
function prismExit(band) {
return new THREE.Vector3().lerpVectors(PRISM_B, PRISM_C, band.exitT);
}
function spectrumOffset() {
return (state.dispersion - 1) * 0.90;
}
let pathRecords = null;
function buildRays() {
clearGroup(rayGroup);
spectrumGroup.position.y = spectrumOffset();
const records = {
white: [],
colors: []
};
const whiteSourceSegment = addBeamSegment(rayGroup, EMIT, SLIT, 0xfffbe8, 0.060);
const whitePrismSegment = addBeamSegment(
rayGroup,
SLIT,
ENTRY,
0xfffbe8,
0.028 + state.gap * 0.12
);
records.white.push(whiteSourceSegment, whitePrismSegment);
addNode(rayGroup, SLIT, 0xfffbe8, 0.09);
addNode(rayGroup, ENTRY, 0xfffbe8, 0.09);
for (const band of bandDefinitions) {
const exit = prismExit(band);
const endpoint = new THREE.Vector3(
SCREEN_X - 0.06,
band.center + spectrumGroup.position.y,
0
);
const inside = addBeamSegment(rayGroup, ENTRY, exit, band.color, 0.034);
const outside = addBeamSegment(rayGroup, exit, endpoint, band.color, 0.044);
addNode(rayGroup, exit, band.color, 0.075);
addNode(rayGroup, endpoint, band.color, 0.080);
records.colors.push({
band,
inside,
outside,
exit: exit.clone(),
endpoint: endpoint.clone()
});
}
pathRecords = records;
}
function validateOptics() {
if (!pathRecords) return { ok: false, reason: "No ray geometry" };
const whiteConnected =
pathRecords.white[0].to.distanceTo(pathRecords.white[1].from) < 1e-7;
let colorsConnected = true;
let spectrumTargetsMatch = true;
let exitsOnPrismFace = true;
const edge = new THREE.Vector3().subVectors(PRISM_C, PRISM_B);
for (const record of pathRecords.colors) {
colorsConnected &&= record.inside.to.distanceTo(record.outside.from) < 1e-7;
colorsConnected &&= record.inside.from.distanceTo(ENTRY) < 1e-7;
const expected = new THREE.Vector3(
SCREEN_X - 0.06,
record.band.center + spectrumGroup.position.y,
0
);
spectrumTargetsMatch &&= record.endpoint.distanceTo(expected) < 1e-7;
const rel = new THREE.Vector3().subVectors(record.exit, PRISM_B);
exitsOnPrismFace &&= Math.abs(rel.x * edge.y - rel.y * edge.x) < 1e-7;
}
const widths = new Set(bandDefinitions.map(b => Math.round(b.width * 1000)));
const widthsAreIllustrativeAndVariable = widths.size > 1;
const ok =
whiteConnected &&
colorsConnected &&
spectrumTargetsMatch &&
exitsOnPrismFace &&
widthsAreIllustrativeAndVariable &&
slitGroup.userData.opening === state.gap;
state.validated = ok;
statusEl.textContent = ok
? "✓ Connected geometry verified: source → slit → prism → screen"
: "⚠ Geometry validation failed";
statusEl.style.color = ok ? "#83e6c8" : "#ff8f8f";
if (!ok) {
console.error("Optics geometry validation failed", {
whiteConnected,
colorsConnected,
spectrumTargetsMatch,
exitsOnPrismFace,
widthsAreIllustrativeAndVariable
});
}
return {
ok,
whiteConnected,
colorsConnected,
spectrumTargetsMatch,
exitsOnPrismFace,
widthsAreIllustrativeAndVariable
};
}
buildSource();
buildSlit();
buildPrism();
buildScreen();
buildRays();
validateOptics();
document.querySelectorAll("[data-gap]").forEach(button => {
button.addEventListener("click", () => {
state.gap = Number(button.dataset.gap);
document.querySelectorAll("[data-gap]").forEach(b => b.classList.toggle("active", b === button));
buildSlit();
buildRays();
validateOptics();
});
});
document.querySelectorAll("[data-dispersion]").forEach(button => {
button.addEventListener("click", () => {
state.dispersion = Number(button.dataset.dispersion);
document.querySelectorAll("[data-dispersion]").forEach(b => b.classList.toggle("active", b === button));
buildRays();
validateOptics();
});
});
function applyCamera() {
const { target, radius, theta, phi, zoom } = cameraState;
camera.position.set(
target.x + radius * Math.sin(phi) * Math.sin(theta),
target.y + radius * Math.cos(phi),
target.z + radius * Math.sin(phi) * Math.cos(theta)
);
camera.lookAt(target);
camera.zoom = zoom;
camera.updateProjectionMatrix();
}
function setPreset(preset) {
document.querySelectorAll("#overview, #prismView").forEach(b => b.classList.toggle("active", b.id === preset));
if (preset === "overview") {
cameraState.target.set(-0.55, 1.70, 0);
cameraState.radius = 17.2;
cameraState.theta = 0.33;
cameraState.phi = 1.34;
cameraState.zoom = 1;
} else {
cameraState.target.set(0.05, 1.50, 0);
cameraState.radius = 14.0;
cameraState.theta = 0.20;
cameraState.phi = 1.30;
cameraState.zoom = 2.05;
}
applyCamera();
}
document.getElementById("overview").addEventListener("click", () => setPreset("overview"));
document.getElementById("prismView").addEventListener("click", () => setPreset("prismView"));
// Camera controls are implemented locally; no addon modules are loaded.
let dragging = false;
let pointerX = 0;
let pointerY = 0;
canvas.addEventListener("pointerdown", event => {
dragging = true;
pointerX = event.clientX;
pointerY = event.clientY;
canvas.classList.add("dragging");
canvas.setPointerCapture(event.pointerId);
});
canvas.addEventListener("pointermove", event => {
if (!dragging) return;
const dx = event.clientX - pointerX;
const dy = event.clientY - pointerY;
pointerX = event.clientX;
pointerY = event.clientY;
cameraState.theta -= dx * 0.005;
cameraState.phi = THREE.MathUtils.clamp(cameraState.phi + dy * 0.004, 0.38, 1.48);
applyCamera();
});
function endDrag(event) {
dragging = false;
canvas.classList.remove("dragging");
if (event.pointerId !== undefined && canvas.hasPointerCapture(event.pointerId)) {
canvas.releasePointerCapture(event.pointerId);
}
}
canvas.addEventListener("pointerup", endDrag);
canvas.addEventListener("pointercancel", endDrag);
canvas.addEventListener("wheel", event => {
event.preventDefault();
cameraState.zoom = THREE.MathUtils.clamp(
cameraState.zoom * Math.exp(-event.deltaY * 0.001),
0.72,
3.2
);
applyCamera();
}, { passive: false });
function resize() {
const width = window.innerWidth;
const height = window.innerHeight;
const aspect = width / height;
const viewHeight = Math.max(8.8, 16.5 / aspect);
camera.left = -viewHeight * aspect / 2;
camera.right = viewHeight * aspect / 2;
camera.top = viewHeight / 2;
camera.bottom = -viewHeight / 2;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(width, height);
}
window.addEventListener("resize", resize);
resize();
applyCamera();
const labelData = [
{ text: "1 · Voxel white-light source", anchor: new THREE.Vector3(-6.65, 2.78, 0) },
{ text: "2 · Adjustable slit", anchor: new THREE.Vector3(-3.25, 2.62, 0) },
{ text: "3 · Voxel prism", anchor: new THREE.Vector3(0.10, 3.02, 0) },
{ text: "4 · Spectrum screen", anchor: new THREE.Vector3(6.03, 4.08, 0) },
{ text: "Band widths are illustrative", anchor: new THREE.Vector3(6.02, 4.48, 0), emphasis: true }
];
const labels = labelData.map(data => {
const el = document.createElement("div");
el.className = "world-label" + (data.emphasis ? " emphasis" : "");
if (!data.emphasis) el.dataset.number = data.text.charAt(0);
el.textContent = data.emphasis ? data.text : data.text.slice(4);
document.body.appendChild(el);
return { el, anchor: data.anchor };
});
function updateLabels() {
const width = window.innerWidth;
const height = window.innerHeight;
for (const label of labels) {
const p = label.anchor.clone().project(camera);
const visible = p.z > -1 && p.z < 1 && Math.abs(p.x) < 1.15 && Math.abs(p.y) < 1.15;
label.el.style.display = visible ? "block" : "none";
if (!visible) continue;
label.el.style.left = `${(p.x * 0.5 + 0.5) * width}px`;
label.el.style.top = `${(-p.y * 0.5 + 0.5) * height}px`;
}
}
// Expose the actual state and validation routine for inspection and interaction tests.
window.__rainbowStation = {
getState: () => ({
slitOpening: state.gap,
dispersion: state.dispersion,
validated: state.validated,
bands: bandDefinitions.map(b => ({
name: b.name,
color: `#${b.color.toString(16).padStart(6, "0")}`,
width: b.width,
center: b.center + spectrumGroup.position.y
}))
}),
validate: validateOptics
};
function animate(time) {
const pulse = 1 + Math.sin(time * 0.0022) * 0.08;
sourceLight.intensity = 2.8 * pulse;
materials.sourceWhite.emissiveIntensity = 1.7 * pulse;
prismLight.intensity = 1.05 + Math.sin(time * 0.0017 + 1.3) * 0.12;
updateLabels();
renderer.render(scene, camera);
requestAnimationFrame(animate);
}
renderer.render(scene, camera);
document.getElementById("loading").remove();
requestAnimationFrame(animate);
</script>
</body>
</html>
```