Prompt specification
Build or customize the Three.js 3D scene "Prism and diffraction spectrum comparison" inside the active workspace.
Engineering Constraints:
1. Runtime & Orbit: Use Three.js r160 with OrbitControls (damping enabled) and ACESFilmicToneMapping.
2. Frame Timing: Drive all motion strictly with clock.getDelta() so animation speed is FPS-independent.
3. Viewport Hygiene: Handle window resize cleanly, cap DPR at Math.min(window.devicePixelRatio, 2), and dispose geometries/materials/renderer on unmount.
Scene Metadata:
Scene: Prism and diffraction spectrum comparison
Category: Science & Physics · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/prism-and-diffraction-spectrum-comparison-x1534
Scene Description Prompt:
Create a standalone stylized scene in one HTML file using Three.js from cdnjs. Place two simplified spectrum displays side by side and label their different mechanisms; avoid implying that all spectral separation arises from the same geometry.
Working Three.js Starter (prism-and-diffraction-spectrum-comparison-x1534):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Two Mechanisms of Spectral Separation</title>
<style>
:root {
color-scheme: dark;
--bg: #07111f;
--panel: rgba(10, 22, 39, 0.82);
--line: rgba(169, 209, 255, 0.18);
--text: #edf7ff;
--muted: #91a8bd;
--cyan: #62e8ff;
--pink: #ff79c6;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: var(--bg);
color: var(--text);
font-family: Inter, ui-sans-serif, system-ui, -apple-system,
BlinkMacSystemFont, "Segoe UI", sans-serif;
}
#scene,
#labels {
position: fixed;
inset: 0;
}
#scene canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
cursor: grab;
}
#scene canvas:active {
cursor: grabbing;
}
#labels {
z-index: 3;
pointer-events: none;
}
.header {
position: fixed;
z-index: 5;
top: 22px;
left: 24px;
max-width: 540px;
pointer-events: none;
}
.eyebrow {
color: var(--cyan);
font-size: 11px;
font-weight: 750;
letter-spacing: 0.2em;
text-transform: uppercase;
}
h1 {
margin: 7px 0 5px;
font-size: clamp(22px, 2.5vw, 36px);
font-weight: 680;
letter-spacing: -0.035em;
line-height: 1.08;
}
.header p {
margin: 0;
color: var(--muted);
font-size: 13px;
line-height: 1.5;
}
.controls {
position: fixed;
z-index: 5;
top: 22px;
right: 24px;
display: flex;
align-items: center;
gap: 10px;
color: var(--muted);
font-size: 11px;
letter-spacing: 0.06em;
}
.controls span {
padding: 8px 11px;
border: 1px solid var(--line);
border-radius: 999px;
background: rgba(7, 17, 31, 0.66);
backdrop-filter: blur(12px);
}
#reset {
border: 1px solid var(--line);
border-radius: 999px;
padding: 8px 12px;
color: var(--text);
background: rgba(7, 17, 31, 0.78);
font: inherit;
font-size: 11px;
cursor: pointer;
transition:
border-color 160ms ease,
background 160ms ease;
}
#reset:hover {
border-color: rgba(98, 232, 255, 0.55);
background: rgba(16, 39, 58, 0.9);
}
.world-label {
position: absolute;
z-index: 4;
transform: translate(-50%, -50%);
padding: 7px 10px;
border: 1px solid rgba(190, 222, 255, 0.18);
border-radius: 7px;
color: #eaf7ff;
background: rgba(5, 14, 27, 0.82);
box-shadow: 0 8px 24px rgba(0, 0, 0, 0.25);
backdrop-filter: blur(8px);
font-size: 10px;
font-weight: 720;
letter-spacing: 0.12em;
line-height: 1;
text-transform: uppercase;
white-space: nowrap;
}
.world-label.title {
padding: 9px 13px;
border-width: 1px;
font-size: 12px;
letter-spacing: 0.16em;
}
.world-label.cyan {
border-color: rgba(98, 232, 255, 0.48);
color: var(--cyan);
}
.world-label.pink {
border-color: rgba(255, 121, 198, 0.48);
color: var(--pink);
}
.cards {
position: fixed;
z-index: 5;
left: 50%;
bottom: 20px;
display: grid;
grid-template-columns: repeat(2, minmax(0, 1fr));
gap: 14px;
width: min(940px, calc(100vw - 32px));
transform: translateX(-50%);
pointer-events: none;
}
.card {
position: relative;
overflow: hidden;
min-height: 112px;
padding: 15px 17px 14px;
border: 1px solid var(--line);
border-radius: 13px;
background: var(--panel);
box-shadow: 0 18px 45px rgba(0, 0, 0, 0.28);
backdrop-filter: blur(16px);
}
.card::before {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 2px;
content: "";
background: var(--accent);
box-shadow: 0 0 18px var(--accent);
}
.card .number {
color: var(--accent);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.17em;
text-transform: uppercase;
}
.card h2 {
margin: 4px 0 6px;
font-size: 16px;
font-weight: 680;
letter-spacing: -0.01em;
}
.card p {
margin: 0;
color: var(--muted);
font-size: 12px;
line-height: 1.48;
}
.card strong {
color: #dcefff;
font-weight: 650;
}
@media (max-width: 760px) {
.header {
top: 15px;
left: 16px;
max-width: calc(100vw - 32px);
}
.header p {
max-width: 320px;
}
.controls {
top: auto;
right: 14px;
bottom: 16px;
}
.controls span {
display: none;
}
.cards {
bottom: 12px;
grid-template-columns: 1fr;
gap: 8px;
width: calc(100vw - 24px);
}
.card {
min-height: 0;
padding: 11px 13px;
}
.card p {
font-size: 11px;
}
}
</style>
</head>
<body>
<div id="scene"></div>
<header class="header">
<div class="eyebrow">Optical systems / comparison 01</div>
<h1>Two ways to separate light</h1>
<p>
Angular diffraction and refractive dispersion are distinct physical
mechanisms—not one universal geometry.
</p>
</header>
<div class="controls">
<span>Drag to orbit · Scroll to zoom</span>
<button id="reset" type="button">Reset view</button>
</div>
<div id="labels" aria-hidden="true">
<div id="label-left-title" class="world-label title cyan">
01 · Diffraction / grating
</div>
<div id="label-left-part" class="world-label">
Parallel grooves
</div>
<div id="label-right-title" class="world-label title pink">
02 · Refraction / prism
</div>
<div id="label-right-part" class="world-label">
Index varies with λ
</div>
</div>
<section class="cards" aria-label="Mechanism comparison">
<article class="card" style="--accent: var(--cyan)">
<div class="number">Mechanism 01</div>
<h2>Diffraction from a grating</h2>
<p>
<strong>Regularly spaced grooves</strong> interfere with one another,
sending different wavelengths into different diffraction angles.
</p>
</article>
<article class="card" style="--accent: var(--pink)">
<div class="number">Mechanism 02</div>
<h2>Refraction through a prism</h2>
<p>
<strong>The glass index depends on wavelength.</strong>
Each color changes direction by a different amount while crossing
the prism interfaces.
</p>
</article>
</section>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// ------------------------------------------------------------
// Renderer and scene
// ------------------------------------------------------------
const container = document.getElementById("scene");
const renderer = new THREE.WebGLRenderer({
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.12;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.domElement.setAttribute(
"aria-label",
"A 3D comparison of spectral separation by diffraction grating and prism"
);
container.appendChild(renderer.domElement);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x07111f);
scene.fog = new THREE.FogExp2(0x07111f, 0.026);
const camera = new THREE.PerspectiveCamera(
38,
window.innerWidth / window.innerHeight,
0.1,
100
);
const cameraState = {
theta: 0,
phi: 1.2,
radius: 16.7,
target: new THREE.Vector3(0, 0.15, 0)
};
function applyCamera() {
camera.position.setFromSphericalCoords(
cameraState.radius,
cameraState.phi,
cameraState.theta
);
camera.position.add(cameraState.target);
camera.lookAt(cameraState.target);
}
applyCamera();
const hemisphere = new THREE.HemisphereLight(0xaedcff, 0x08111d, 2.0);
scene.add(hemisphere);
const keyLight = new THREE.DirectionalLight(0xffffff, 2.8);
keyLight.position.set(3, 9, 11);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -13;
keyLight.shadow.camera.right = 13;
keyLight.shadow.camera.top = 9;
keyLight.shadow.camera.bottom = -7;
keyLight.shadow.camera.near = 1;
keyLight.shadow.camera.far = 35;
scene.add(keyLight);
const cyanLight = new THREE.PointLight(0x42dfff, 28, 15, 2);
cyanLight.position.set(-4.5, 2.3, 4);
scene.add(cyanLight);
const pinkLight = new THREE.PointLight(0xff67bd, 27, 15, 2);
pinkLight.position.set(4.5, 2.1, 4);
scene.add(pinkLight);
// ------------------------------------------------------------
// Shared helpers
// ------------------------------------------------------------
const up = new THREE.Vector3(0, 1, 0);
const photons = [];
function standardMaterial(color, options = {}) {
return new THREE.MeshStandardMaterial({
color,
roughness: 0.56,
metalness: 0.08,
...options
});
}
function roundedRectShape(width, height, radius) {
const x = -width / 2;
const y = -height / 2;
const shape = new THREE.Shape();
shape.moveTo(x + radius, y);
shape.lineTo(x + width - radius, y);
shape.quadraticCurveTo(x + width, y, x + width, y + radius);
shape.lineTo(x + width, y + height - radius);
shape.quadraticCurveTo(
x + width,
y + height,
x + width - radius,
y + height
);
shape.lineTo(x + radius, y + height);
shape.quadraticCurveTo(x, y + height, x, y + height - radius);
shape.lineTo(x, y + radius);
shape.quadraticCurveTo(x, y, x + radius, y);
shape.closePath();
return shape;
}
function makeBeam(parent, start, end, color, radius = 0.018) {
const direction = new THREE.Vector3().subVectors(end, start);
const length = direction.length();
const midpoint = start.clone().add(end).multiplyScalar(0.5);
direction.normalize();
const core = new THREE.Mesh(
new THREE.CylinderGeometry(radius, radius, length, 8),
new THREE.MeshBasicMaterial({ color })
);
core.position.copy(midpoint);
core.quaternion.setFromUnitVectors(up, direction);
parent.add(core);
const glow = new THREE.Mesh(
new THREE.CylinderGeometry(radius * 3.2, radius * 3.2, length, 8),
new THREE.MeshBasicMaterial({
color,
transparent: true,
opacity: 0.12,
depthWrite: false,
blending: THREE.AdditiveBlending
})
);
glow.position.copy(midpoint);
glow.quaternion.copy(core.quaternion);
parent.add(glow);
return core;
}
function addPhoton(parent, start, end, color, speed, offset) {
const photon = new THREE.Mesh(
new THREE.SphereGeometry(0.055, 12, 12),
new THREE.MeshBasicMaterial({ color })
);
parent.add(photon);
photons.push({
mesh: photon,
start: start.clone(),
end: end.clone(),
speed,
offset
});
}
function makeSource(parent, x = -3.15) {
const body = new THREE.Mesh(
new THREE.CylinderGeometry(0.24, 0.31, 0.58, 32),
standardMaterial(0x1b2a3d, {
metalness: 0.7,
roughness: 0.28
})
);
body.rotation.z = -Math.PI / 2;
body.position.set(x, 0, 0.15);
body.castShadow = true;
parent.add(body);
const lens = new THREE.Mesh(
new THREE.TorusGeometry(0.225, 0.035, 10, 40),
standardMaterial(0x7bdfff, {
emissive: 0x1a7892,
emissiveIntensity: 0.8,
metalness: 0.5
})
);
lens.rotation.y = Math.PI / 2;
lens.position.set(x + 0.31, 0, 0.15);
parent.add(lens);
const aperture = new THREE.Mesh(
new THREE.CircleGeometry(0.19, 32),
new THREE.MeshBasicMaterial({
color: 0xeafaff,
transparent: true,
opacity: 0.88
})
);
aperture.rotation.y = Math.PI / 2;
aperture.position.set(x + 0.305, 0, 0.15);
parent.add(aperture);
const stand = new THREE.Mesh(
new THREE.CylinderGeometry(0.055, 0.085, 1.08, 16),
standardMaterial(0x24364c, { metalness: 0.72 })
);
stand.position.set(x - 0.03, -0.78, 0.1);
stand.castShadow = true;
parent.add(stand);
return lens;
}
function makeExhibit(accent) {
const group = new THREE.Group();
const boardShape = roundedRectShape(7.25, 4.0, 0.34);
const board = new THREE.Mesh(
new THREE.ExtrudeGeometry(boardShape, {
depth: 0.12,
bevelEnabled: true,
bevelThickness: 0.035,
bevelSize: 0.035,
bevelSegments: 2,
curveSegments: 10
}),
standardMaterial(0x0c1b2d, {
roughness: 0.82,
metalness: 0.12
})
);
board.position.set(0, 0.55, -0.77);
board.receiveShadow = true;
group.add(board);
const halo = new THREE.Mesh(
new THREE.TorusGeometry(2.3, 0.018, 8, 128),
new THREE.MeshBasicMaterial({
color: accent,
transparent: true,
opacity: 0.38
})
);
halo.position.set(0, 0.22, -0.59);
group.add(halo);
for (let i = 0; i < 12; i++) {
const angle = (i / 12) * Math.PI * 2;
const marker = new THREE.Mesh(
new THREE.SphereGeometry(0.033, 10, 10),
new THREE.MeshBasicMaterial({
color: accent,
transparent: true,
opacity: 0.65
})
);
marker.position.set(
Math.cos(angle) * 2.3,
0.22 + Math.sin(angle) * 2.3,
-0.56
);
group.add(marker);
}
const platform = new THREE.Mesh(
new THREE.BoxGeometry(7.4, 0.28, 2.72),
standardMaterial(0x0a1727, {
roughness: 0.68,
metalness: 0.18
})
);
platform.position.set(0, -1.58, -0.16);
platform.castShadow = true;
platform.receiveShadow = true;
group.add(platform);
const platformEdge = new THREE.Mesh(
new THREE.BoxGeometry(7.08, 0.035, 2.4),
new THREE.MeshBasicMaterial({
color: accent,
transparent: true,
opacity: 0.68
})
);
platformEdge.position.set(0, -1.425, -0.16);
group.add(platformEdge);
return group;
}
// ------------------------------------------------------------
// Ground and atmosphere
// ------------------------------------------------------------
const floor = new THREE.Mesh(
new THREE.PlaneGeometry(80, 80),
standardMaterial(0x071523, {
roughness: 0.95,
metalness: 0.02
})
);
floor.rotation.x = -Math.PI / 2;
floor.position.y = -1.76;
floor.receiveShadow = true;
scene.add(floor);
const grid = new THREE.GridHelper(38, 38, 0x23415c, 0x142a40);
grid.position.y = -1.745;
grid.material.transparent = true;
grid.material.opacity = 0.26;
scene.add(grid);
const starPositions = new Float32Array(150 * 3);
for (let i = 0; i < 150; i++) {
starPositions[i * 3] = (Math.random() - 0.5) * 32;
starPositions[i * 3 + 1] = Math.random() * 9 - 1;
starPositions[i * 3 + 2] = -3 - Math.random() * 12;
}
const starGeometry = new THREE.BufferGeometry();
starGeometry.setAttribute(
"position",
new THREE.BufferAttribute(starPositions, 3)
);
const stars = new THREE.Points(
starGeometry,
new THREE.PointsMaterial({
color: 0x8ecfff,
size: 0.035,
transparent: true,
opacity: 0.55,
depthWrite: false
})
);
scene.add(stars);
// ------------------------------------------------------------
// Left exhibit: diffraction by a ruled grating
// ------------------------------------------------------------
const spectrumColors = [
0xff5668,
0xff9b45,
0xffdf55,
0x64ed91,
0x4fe4ff,
0x588cff,
0xb176ff
];
const left = makeExhibit(0x62e8ff);
left.position.x = -4.08;
scene.add(left);
const leftLens = makeSource(left);
const gratingFrame = new THREE.Mesh(
new THREE.BoxGeometry(0.17, 2.25, 1.16),
standardMaterial(0x203248, {
metalness: 0.72,
roughness: 0.3
})
);
gratingFrame.position.set(-1.04, 0, 0);
gratingFrame.castShadow = true;
left.add(gratingFrame);
const gratingEdge = new THREE.Mesh(
new THREE.BoxGeometry(0.19, 2.31, 1.22),
new THREE.MeshBasicMaterial({
color: 0x62e8ff,
transparent: true,
opacity: 0.24,
wireframe: true
})
);
gratingEdge.position.copy(gratingFrame.position);
left.add(gratingEdge);
// Closely spaced parallel grooves: the defining geometry here.
for (let i = -8; i <= 8; i++) {
const groove = new THREE.Mesh(
new THREE.BoxGeometry(0.13, 0.024, 1.23),
standardMaterial(0xa9d8e8, {
emissive: 0x173947,
emissiveIntensity: 0.35,
metalness: 0.82,
roughness: 0.22
})
);
groove.position.set(-1.02, i * 0.125, 0.54);
left.add(groove);
}
const gratingFoot = new THREE.Mesh(
new THREE.BoxGeometry(0.62, 0.12, 1.45),
standardMaterial(0x14263a, { metalness: 0.55 })
);
gratingFoot.position.set(-1.04, -1.18, 0);
gratingFoot.castShadow = true;
left.add(gratingFoot);
const whiteStartLeft = new THREE.Vector3(-2.80, 0, 0.67);
const gratingHit = new THREE.Vector3(-0.92, 0, 0.67);
makeBeam(left, whiteStartLeft, gratingHit, 0xf3fbff, 0.026);
const detectorFrame = new THREE.Mesh(
new THREE.BoxGeometry(0.16, 2.48, 0.94),
standardMaterial(0x172a3f, {
metalness: 0.65,
roughness: 0.34
})
);
detectorFrame.position.set(2.06, 0, 0.18);
detectorFrame.castShadow = true;
left.add(detectorFrame);
const fanY = [-0.95, -0.68, -0.36, 0, 0.36, 0.68, 0.95];
spectrumColors.forEach((color, index) => {
const end = new THREE.Vector3(1.96, fanY[index], 0.67);
makeBeam(left, gratingHit, end, color, 0.017);
const detectorBand = new THREE.Mesh(
new THREE.BoxGeometry(0.18, 0.25, 0.97),
new THREE.MeshBasicMaterial({ color })
);
detectorBand.position.set(2.07, fanY[index], 0.20);
left.add(detectorBand);
addPhoton(
left,
gratingHit,
end,
color,
0.12 + index * 0.009,
index * 0.17
);
});
// ------------------------------------------------------------
// Right exhibit: wavelength-dependent refraction in a prism
// ------------------------------------------------------------
const right = makeExhibit(0xff79c6);
right.position.x = 4.08;
scene.add(right);
const rightLens = makeSource(right);
const prismShape = new THREE.Shape();
prismShape.moveTo(-0.82, -0.72);
prismShape.lineTo(0.82, -0.72);
prismShape.lineTo(0, 0.92);
prismShape.closePath();
const prismGeometry = new THREE.ExtrudeGeometry(prismShape, {
depth: 0.72,
bevelEnabled: true,
bevelThickness: 0.045,
bevelSize: 0.045,
bevelSegments: 2
});
prismGeometry.translate(0, 0, -0.36);
const prism = new THREE.Mesh(
prismGeometry,
new THREE.MeshPhysicalMaterial({
color: 0xccefff,
emissive: 0x173a48,
emissiveIntensity: 0.18,
transparent: true,
opacity: 0.48,
roughness: 0.16,
metalness: 0.03,
side: THREE.DoubleSide,
depthWrite: false
})
);
prism.position.set(-0.45, 0.08, 0.17);
prism.castShadow = true;
right.add(prism);
const prismEdges = new THREE.LineSegments(
new THREE.EdgesGeometry(prismGeometry, 18),
new THREE.LineBasicMaterial({
color: 0xe5f8ff,
transparent: true,
opacity: 0.85
})
);
prismEdges.position.copy(prism.position);
right.add(prismEdges);
const prismStand = new THREE.Mesh(
new THREE.CylinderGeometry(0.28, 0.38, 0.62, 24),
standardMaterial(0x2a2039, {
metalness: 0.55,
roughness: 0.36
})
);
prismStand.position.set(-0.45, -1.08, 0.15);
prismStand.castShadow = true;
right.add(prismStand);
const whiteStartRight = new THREE.Vector3(-2.80, 0, 0.67);
const prismEntry = new THREE.Vector3(-1.02, 0, 0.67);
const prismExit = new THREE.Vector3(0.22, -0.015, 0.67);
makeBeam(right, whiteStartRight, prismEntry, 0xf3fbff, 0.026);
makeBeam(right, prismEntry, prismExit, 0xf3fbff, 0.021);
// A bent spectrum on an arc makes this exhibit visually distinct
// from the straight diffraction fan.
const deviationAngles = [-4, -9, -14, -19, -24, -29, -34];
const arcRadius = 2.35;
const arcPoints = [];
deviationAngles.forEach((degrees, index) => {
const angle = THREE.MathUtils.degToRad(degrees);
const end = new THREE.Vector3(
prismExit.x + Math.cos(angle) * arcRadius,
prismExit.y + Math.sin(angle) * arcRadius,
0.67
);
arcPoints.push(end);
makeBeam(right, prismExit, end, spectrumColors[index], 0.017);
const colorMarker = new THREE.Mesh(
new THREE.SphereGeometry(0.105, 16, 16),
new THREE.MeshBasicMaterial({ color: spectrumColors[index] })
);
colorMarker.position.copy(end);
right.add(colorMarker);
addPhoton(
right,
prismExit,
end,
spectrumColors[index],
0.115 + index * 0.008,
0.09 + index * 0.15
);
});
// Thin structural arc behind the refracted rays.
for (let i = 0; i < arcPoints.length - 1; i++) {
makeBeam(
right,
arcPoints[i],
arcPoints[i + 1],
0x66788d,
0.032
);
}
// ------------------------------------------------------------
// Screen-space labels, projected from world-space anchors
// ------------------------------------------------------------
const labelAnchors = [];
function addLabelAnchor(parent, id, x, y, z) {
const object = new THREE.Object3D();
object.position.set(x, y, z);
parent.add(object);
labelAnchors.push({
object,
element: document.getElementById(id)
});
}
addLabelAnchor(left, "label-left-title", 0, 2.25, -0.45);
addLabelAnchor(left, "label-left-part", -1.02, 1.43, 0.72);
addLabelAnchor(right, "label-right-title", 0, 2.25, -0.45);
addLabelAnchor(right, "label-right-part", -0.45, 1.43, 0.72);
const projected = new THREE.Vector3();
function updateLabels() {
const width = renderer.domElement.clientWidth;
const height = renderer.domElement.clientHeight;
for (const anchor of labelAnchors) {
anchor.object.getWorldPosition(projected);
projected.project(camera);
const visible = projected.z > -1 && projected.z < 1;
anchor.element.style.display = visible ? "block" : "none";
if (!visible) continue;
const x = (projected.x * 0.5 + 0.5) * width;
const y = (-projected.y * 0.5 + 0.5) * height;
anchor.element.style.left = `${x}px`;
anchor.element.style.top = `${y}px`;
}
}
// ------------------------------------------------------------
// Custom camera controls — no external controls addon
// ------------------------------------------------------------
let dragging = false;
let previousX = 0;
let previousY = 0;
renderer.domElement.addEventListener("pointerdown", event => {
dragging = true;
previousX = event.clientX;
previousY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", event => {
if (!dragging) return;
const deltaX = event.clientX - previousX;
const deltaY = event.clientY - previousY;
previousX = event.clientX;
previousY = event.clientY;
cameraState.theta = THREE.MathUtils.clamp(
cameraState.theta - deltaX * 0.005,
-1.05,
1.05
);
cameraState.phi = THREE.MathUtils.clamp(
cameraState.phi - deltaY * 0.004,
0.76,
1.48
);
applyCamera();
});
function endDrag(event) {
dragging = false;
if (event.pointerId !== undefined &&
renderer.domElement.hasPointerCapture(event.pointerId)) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
}
renderer.domElement.addEventListener("pointerup", endDrag);
renderer.domElement.addEventListener("pointercancel", endDrag);
renderer.domElement.addEventListener(
"wheel",
event => {
event.preventDefault();
cameraState.radius = THREE.MathUtils.clamp(
cameraState.radius + event.deltaY * 0.009,
11,
27
);
applyCamera();
},
{ passive: false }
);
function resetCamera() {
cameraState.theta = 0;
cameraState.phi = 1.2;
cameraState.radius = window.innerWidth / window.innerHeight < 0.8
? 24
: 16.7;
applyCamera();
}
document.getElementById("reset").addEventListener("click", resetCamera);
renderer.domElement.addEventListener("dblclick", resetCamera);
// ------------------------------------------------------------
// Resize and animation
// ------------------------------------------------------------
function onResize() {
const width = window.innerWidth;
const height = window.innerHeight;
const aspect = width / height;
camera.aspect = aspect;
camera.fov = aspect < 0.8 ? 46 : 38;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(width, height);
if (!dragging) {
cameraState.radius = aspect < 0.8
? 24
: Math.max(
16.7,
17.8 / (2 * Math.tan(THREE.MathUtils.degToRad(19)) * aspect)
);
applyCamera();
}
}
window.addEventListener("resize", onResize);
onResize();
const reduceMotion = window.matchMedia(
"(prefers-reduced-motion: reduce)"
).matches;
const clock = new THREE.Clock();
function animate() {
const elapsed = clock.getElapsedTime();
if (!reduceMotion) {
for (const photon of photons) {
const t = (elapsed * photon.speed + photon.offset) % 1;
photon.mesh.position.lerpVectors(
photon.start,
photon.end,
t
);
}
const pulse = 1 + Math.sin(elapsed * 3.2) * 0.06;
leftLens.scale.setScalar(pulse);
rightLens.scale.setScalar(pulse);
stars.rotation.y = elapsed * 0.006;
}
renderer.render(scene, camera);
updateLabels();
requestAnimationFrame(animate);
}
animate();
</script>
</body>
</html>
```