Prompt specification
Build or customize the Three.js 3D scene "Single-slit wavefront demonstrator" 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: Single-slit wavefront demonstrator
Category: Science & Physics · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/single-slit-wavefront-demonstrator-x1529
Scene Description Prompt:
Build a low-poly single-slit diffraction model with adjustable aperture and wavelength, labeling the far-field intensity approximation. Three.js from cdnjs, one HTML file.
Working Three.js Starter (single-slit-wavefront-demonstrator-x1529):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Single-Slit Far-Field Diffraction</title>
<style>
:root {
color-scheme: dark;
--bg: #07111f;
--panel: rgba(10, 23, 39, 0.82);
--border: rgba(142, 203, 255, 0.17);
--text: #eaf5ff;
--muted: #91a9bd;
--cyan: #54d9ff;
--amber: #ffb75b;
--cyan-dark: #153e52;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background:
radial-gradient(circle at 30% 15%, #17304a 0%, transparent 42%),
radial-gradient(circle at 80% 80%, #10253c 0%, transparent 38%),
var(--bg);
color: var(--text);
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
#scene {
position: fixed;
inset: 0;
}
#scene canvas {
display: block;
width: 100%;
height: 100%;
cursor: grab;
touch-action: none;
}
#scene canvas:active {
cursor: grabbing;
}
.hud {
position: fixed;
inset: 0;
pointer-events: none;
}
.panel {
border: 1px solid var(--border);
background: var(--panel);
box-shadow:
0 18px 50px rgba(0, 0, 0, 0.25),
inset 0 1px 0 rgba(255, 255, 255, 0.035);
backdrop-filter: blur(14px);
-webkit-backdrop-filter: blur(14px);
}
.brand {
position: absolute;
top: 22px;
left: 22px;
width: min(390px, calc(100vw - 44px));
padding: 17px 19px;
border-radius: 15px;
}
.eyebrow {
margin-bottom: 8px;
color: var(--cyan);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.19em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: clamp(21px, 3vw, 30px);
font-weight: 760;
letter-spacing: -0.035em;
line-height: 1.05;
}
.subtitle {
margin: 8px 0 0;
color: var(--muted);
font-size: 12px;
line-height: 1.5;
}
.controls {
position: absolute;
top: 22px;
right: 22px;
width: 320px;
padding: 18px;
border-radius: 15px;
pointer-events: auto;
}
.control-section + .control-section {
margin-top: 19px;
padding-top: 17px;
border-top: 1px solid rgba(255, 255, 255, 0.075);
}
.control-heading {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
margin-bottom: 10px;
font-size: 12px;
font-weight: 700;
}
.control-heading output {
min-width: 74px;
color: var(--cyan);
text-align: right;
font-variant-numeric: tabular-nums;
}
.control-note {
margin: 7px 0 0;
color: var(--muted);
font-size: 10px;
line-height: 1.45;
}
input[type="range"] {
width: 100%;
height: 5px;
margin: 8px 0 4px;
appearance: none;
-webkit-appearance: none;
border-radius: 999px;
outline: none;
background: linear-gradient(
to right,
var(--cyan) 0%,
var(--cyan) var(--pct, 50%),
rgba(255, 255, 255, 0.12) var(--pct, 50%),
rgba(255, 255, 255, 0.12) 100%
);
cursor: pointer;
}
input[type="range"]::-webkit-slider-thumb {
width: 17px;
height: 17px;
appearance: none;
-webkit-appearance: none;
border: 3px solid #0a1727;
border-radius: 50%;
background: var(--cyan);
box-shadow: 0 0 0 1px rgba(84, 217, 255, 0.45);
}
input[type="range"]::-moz-range-thumb {
width: 12px;
height: 12px;
border: 3px solid #0a1727;
border-radius: 50%;
background: var(--cyan);
box-shadow: 0 0 0 1px rgba(84, 217, 255, 0.45);
}
.range-labels {
display: flex;
justify-content: space-between;
color: #70879b;
font-size: 9px;
font-variant-numeric: tabular-nums;
}
.segmented {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 5px;
padding: 4px;
border-radius: 10px;
background: rgba(255, 255, 255, 0.045);
}
button {
border: 1px solid transparent;
border-radius: 8px;
padding: 9px 10px;
background: transparent;
color: var(--muted);
font: inherit;
font-size: 11px;
font-weight: 700;
cursor: pointer;
transition:
color 150ms ease,
background 150ms ease,
border-color 150ms ease;
}
button:hover {
color: var(--text);
background: rgba(255, 255, 255, 0.055);
}
button.active {
border-color: rgba(84, 217, 255, 0.24);
background: rgba(84, 217, 255, 0.12);
color: var(--cyan);
}
.button-row {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 7px;
margin-top: 10px;
}
.button-row button {
border-color: rgba(255, 255, 255, 0.08);
background: rgba(255, 255, 255, 0.035);
}
.formula-card {
position: absolute;
bottom: 22px;
left: 22px;
width: min(570px, calc(100vw - 44px));
padding: 18px 20px;
border-radius: 15px;
}
.formula-title {
display: flex;
align-items: center;
justify-content: space-between;
gap: 15px;
margin-bottom: 10px;
}
.formula-title .eyebrow {
margin: 0;
}
.scale-badge {
flex: 0 0 auto;
padding: 4px 8px;
border: 1px solid rgba(255, 183, 91, 0.25);
border-radius: 999px;
background: rgba(255, 183, 91, 0.09);
color: var(--amber);
font-size: 9px;
font-weight: 800;
letter-spacing: 0.08em;
text-transform: uppercase;
}
.formula {
display: flex;
align-items: center;
gap: 8px;
min-height: 45px;
color: #f4fbff;
font-family: Georgia, "Times New Roman", serif;
font-size: clamp(18px, 2.4vw, 25px);
white-space: nowrap;
}
.frac {
display: inline-flex;
flex-direction: column;
align-items: stretch;
text-align: center;
vertical-align: middle;
}
.frac .num {
padding: 0 7px 3px;
border-bottom: 1px solid currentColor;
}
.frac .den {
padding: 3px 7px 0;
}
.formula sup {
margin-left: -4px;
font-size: 0.64em;
}
.formula-note {
margin: 8px 0 13px;
color: var(--muted);
font-size: 10px;
line-height: 1.45;
}
.readouts {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 8px;
}
.readout {
padding: 9px 11px;
border: 1px solid rgba(255, 255, 255, 0.065);
border-radius: 10px;
background: rgba(255, 255, 255, 0.026);
}
.readout span {
display: block;
margin-bottom: 3px;
color: #7990a5;
font-size: 8px;
font-weight: 800;
letter-spacing: 0.13em;
text-transform: uppercase;
}
.readout strong {
display: block;
color: #f5fbff;
font-size: 14px;
font-variant-numeric: tabular-nums;
}
.readout small {
display: block;
margin-top: 2px;
color: var(--muted);
font-size: 9px;
}
.help {
position: absolute;
right: 22px;
bottom: 22px;
padding: 9px 12px;
border-radius: 10px;
color: #8198ad;
font-size: 9px;
font-weight: 700;
letter-spacing: 0.08em;
text-transform: uppercase;
}
@media (max-width: 820px) {
.brand {
top: 12px;
left: 12px;
}
.controls {
top: 12px;
right: 12px;
width: 280px;
}
.formula-card {
bottom: 12px;
left: 12px;
width: calc(100vw - 24px);
}
.help {
display: none;
}
}
@media (max-width: 620px) {
.brand {
right: 12px;
width: auto;
}
.brand .subtitle {
display: none;
}
.controls {
top: 100px;
left: 12px;
right: 12px;
width: auto;
padding: 14px;
}
.control-section + .control-section {
margin-top: 13px;
padding-top: 12px;
}
.formula-card {
padding: 14px;
}
.formula {
font-size: 17px;
overflow-x: auto;
padding-bottom: 3px;
}
.formula-note {
display: none;
}
.readouts {
margin-top: 12px;
}
}
</style>
</head>
<body>
<div id="scene"></div>
<div class="hud">
<header class="brand panel">
<div class="eyebrow">Optical Wave Laboratory</div>
<h1>Single-Slit Diffraction</h1>
<p class="subtitle">
An interactive far-field model of monochromatic light passing through
an adjustable aperture.
</p>
</header>
<aside class="controls panel">
<section class="control-section">
<div class="control-heading">
<label for="wavelength">Wavelength λ</label>
<output id="wavelengthValue">550 nm</output>
</div>
<input
id="wavelength"
type="range"
min="400"
max="700"
step="5"
value="550"
/>
<div class="range-labels">
<span>400 nm · violet</span>
<span>700 nm · red</span>
</div>
</section>
<section class="control-section">
<div class="control-heading">
<label for="aperture">Slit aperture a</label>
<output id="apertureValue">1.50 mm</output>
</div>
<input
id="aperture"
type="range"
min="0.5"
max="5"
step="0.1"
value="1.5"
/>
<div class="range-labels">
<span>0.5 mm</span>
<span>5.0 mm</span>
</div>
<p class="control-note">
The rendered slit is enlarged for visibility. The diffraction
calculation uses the selected physical dimensions.
</p>
</section>
<section class="control-section">
<div class="control-heading">Intensity display</div>
<div class="segmented">
<button id="linearButton" class="active" type="button">
Linear
</button>
<button id="logButton" type="button">Log₁₀</button>
</div>
<div class="button-row">
<button id="animationButton" type="button">Pause Wave</button>
<button id="resetButton" type="button">Reset View</button>
</div>
</section>
</aside>
<section class="formula-card panel">
<div class="formula-title">
<div class="eyebrow">
Far-Field Intensity Approximation · Fraunhofer Diffraction
</div>
<div id="scaleBadge" class="scale-badge">Linear scale</div>
</div>
<div class="formula" aria-label="I theta equals I zero times sine squared over beta squared">
<span>I(θ) ≈ I<sub>0</sub></span>
<span class="frac">
<span class="num">sin(πa sinθ / λ)</span>
<span class="den">πa sinθ / λ</span>
</span>
<sup>2</sup>
</div>
<p class="formula-note">
Ideal narrow slit, normalized so I(0) / I₀ = 1. The far-field
detector displays intensity versus observation angle θ.
</p>
<div class="readouts">
<div class="readout">
<span>First minimum</span>
<strong id="firstMinimum">0.0210°</strong>
<small>θ₁ = asin(λ / a)</small>
</div>
<div class="readout">
<span>Central lobe width</span>
<strong id="centralWidth">0.0420°</strong>
<small>Full angular width ≈ 2θ₁</small>
</div>
</div>
</section>
<div class="help panel">Drag to orbit · Scroll to zoom</div>
</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const container = document.querySelector("#scene");
const state = {
wavelengthNm: 550,
slitMm: 1.5,
scale: "linear",
animate: true
};
const wavelengthInput = document.querySelector("#wavelength");
const apertureInput = document.querySelector("#aperture");
const wavelengthValue = document.querySelector("#wavelengthValue");
const apertureValue = document.querySelector("#apertureValue");
const firstMinimum = document.querySelector("#firstMinimum");
const centralWidth = document.querySelector("#centralWidth");
const linearButton = document.querySelector("#linearButton");
const logButton = document.querySelector("#logButton");
const animationButton = document.querySelector("#animationButton");
const resetButton = document.querySelector("#resetButton");
const scaleBadge = document.querySelector("#scaleBadge");
// -------------------------------------------------------------------------
// Renderer and scene
// -------------------------------------------------------------------------
const renderer = new THREE.WebGLRenderer({
antialias: true,
alpha: false,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.12;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
container.appendChild(renderer.domElement);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x07111f);
scene.fog = new THREE.Fog(0x07111f, 32, 62);
const camera = new THREE.PerspectiveCamera(
43,
window.innerWidth / window.innerHeight,
0.1,
120
);
const cameraTarget = new THREE.Vector3(0, 2.7, 1.7);
const defaultCamera = {
yaw: 0.92,
pitch: 0.43,
distance: 31
};
const cameraState = { ...defaultCamera };
const hemisphere = new THREE.HemisphereLight(0xbfe6ff, 0x172033, 1.6);
scene.add(hemisphere);
const keyLight = new THREE.DirectionalLight(0xd9f2ff, 2.7);
keyLight.position.set(-10, 17, 8);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(1024, 1024);
keyLight.shadow.camera.left = -22;
keyLight.shadow.camera.right = 22;
keyLight.shadow.camera.top = 18;
keyLight.shadow.camera.bottom = -12;
keyLight.shadow.camera.near = 1;
keyLight.shadow.camera.far = 55;
scene.add(keyLight);
const warmLight = new THREE.DirectionalLight(0xffb45c, 1.2);
warmLight.position.set(12, 5, -8);
scene.add(warmLight);
const groundMaterial = new THREE.MeshStandardMaterial({
color: 0x0b1929,
roughness: 0.93,
metalness: 0.08
});
const ground = new THREE.Mesh(
new THREE.PlaneGeometry(60, 60),
groundMaterial
);
ground.rotation.x = -Math.PI / 2;
ground.position.y = -0.045;
ground.receiveShadow = true;
scene.add(ground);
const grid = new THREE.GridHelper(50, 50, 0x25455d, 0x162a3d);
grid.position.y = -0.025;
grid.material.transparent = true;
grid.material.opacity = 0.27;
scene.add(grid);
// -------------------------------------------------------------------------
// Label sprites
// -------------------------------------------------------------------------
function roundedRect(ctx, x, y, width, height, radius) {
const r = Math.min(radius, width / 2, height / 2);
ctx.beginPath();
ctx.moveTo(x + r, y);
ctx.arcTo(x + width, y, x + width, y + height, r);
ctx.arcTo(x + width, y + height, x, y + height, r);
ctx.arcTo(x, y + height, x, y, r);
ctx.arcTo(x, y, x + width, y, r);
ctx.closePath();
}
function createLabel(text, options = {}) {
const canvas = document.createElement("canvas");
canvas.width = 1024;
canvas.height = 192;
const ctx = canvas.getContext("2d");
if (options.background !== false) {
roundedRect(
ctx,
9,
9,
canvas.width - 18,
canvas.height - 18,
28
);
ctx.fillStyle = options.background || "rgba(7, 17, 31, 0.82)";
ctx.fill();
ctx.lineWidth = 4;
ctx.strokeStyle = options.border || "rgba(120, 205, 255, 0.38)";
ctx.stroke();
}
const fontSize = options.fontSize || 54;
ctx.font = `700 ${fontSize}px system-ui, sans-serif`;
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.fillStyle = options.color || "#eaf5ff";
ctx.fillText(text, canvas.width / 2, canvas.height / 2 + 1);
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.minFilter = THREE.LinearFilter;
const material = new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthTest: false,
depthWrite: false,
toneMapped: false
});
const sprite = new THREE.Sprite(material);
sprite.renderOrder = 20;
return sprite;
}
function placeLabel(text, position, scale, options = {}) {
const label = createLabel(text, options);
label.position.copy(position);
label.scale.set(scale[0], scale[1], 1);
scene.add(label);
return label;
}
placeLabel(
"MONOCHROMATIC PLANE WAVE",
new THREE.Vector3(0, 4.45, -8.8),
[4.65, 0.87],
{ fontSize: 45 }
);
placeLabel(
"ADJUSTABLE SINGLE SLIT",
new THREE.Vector3(0, 6.25, 0),
[4.25, 0.82],
{ fontSize: 48 }
);
placeLabel(
"FAR-FIELD INTENSITY · I / I₀",
new THREE.Vector3(0, 8.15, 12.55),
[7.8, 0.92],
{
fontSize: 48,
border: "rgba(255, 183, 91, 0.38)",
background: "rgba(7, 17, 31, 0.88)"
}
);
placeLabel(
"−0.12°",
new THREE.Vector3(-7.15, 0.72, 12.58),
[1.45, 0.34],
{ background: false, color: "#7890a5", fontSize: 50 }
);
placeLabel(
"0",
new THREE.Vector3(0, 0.72, 12.58),
[0.6, 0.34],
{ background: false, color: "#7890a5", fontSize: 56 }
);
placeLabel(
"+0.12°",
new THREE.Vector3(7.15, 0.72, 12.58),
[1.45, 0.34],
{ background: false, color: "#7890a5", fontSize: 50 }
);
placeLabel(
"I / I₀",
new THREE.Vector3(-7.85, 6.75, 12.58),
[1.35, 0.36],
{ background: false, color: "#7890a5", fontSize: 52 }
);
// -------------------------------------------------------------------------
// Low-poly source and incident beam
// -------------------------------------------------------------------------
const sourceMaterial = new THREE.MeshStandardMaterial({
color: 0x203950,
roughness: 0.55,
metalness: 0.6,
flatShading: true
});
const sourceAccentMaterial = new THREE.MeshStandardMaterial({
color: 0xffb45c,
emissive: 0xff8b28,
emissiveIntensity: 1.5,
roughness: 0.36,
metalness: 0.45,
flatShading: true
});
const sourceHousing = new THREE.Mesh(
new THREE.CylinderGeometry(1.15, 1.15, 1.35, 12),
sourceMaterial
);
sourceHousing.rotation.x = Math.PI / 2;
sourceHousing.position.set(0, 2.7, -8.75);
sourceHousing.castShadow = true;
scene.add(sourceHousing);
const sourceNose = new THREE.Mesh(
new THREE.CylinderGeometry(0.82, 1.15, 1.25, 12),
sourceAccentMaterial
);
sourceNose.rotation.x = Math.PI / 2;
sourceNose.position.set(0, 2.7, -7.52);
sourceNose.castShadow = true;
scene.add(sourceNose);
const emitter = new THREE.Mesh(
new THREE.CircleGeometry(0.67, 16),
new THREE.MeshBasicMaterial({ color: 0xffe5b6 })
);
emitter.position.set(0, 2.7, -6.87);
scene.add(emitter);
const pulseRings = [];
for (let i = 0; i < 3; i++) {
const ring = new THREE.Mesh(
new THREE.TorusGeometry(0.82 + i * 0.11, 0.055, 6, 24),
new THREE.MeshBasicMaterial({
color: 0x65dcff,
transparent: true,
opacity: 0.75 - i * 0.16,
blending: THREE.AdditiveBlending,
depthWrite: false
})
);
ring.position.set(0, 2.7, -7.03 + i * 0.22);
scene.add(ring);
pulseRings.push(ring);
}
const beamMaterial = new THREE.MeshBasicMaterial({
color: 0x49d8ff,
transparent: true,
opacity: 0.07,
side: THREE.DoubleSide,
depthWrite: false,
blending: THREE.AdditiveBlending
});
const incidentBeam = new THREE.Mesh(
new THREE.ConeGeometry(3.45, 6.88, 4, 1, true),
beamMaterial
);
incidentBeam.rotation.x = Math.PI / 2;
incidentBeam.position.set(0, 2.7, -3.44);
scene.add(incidentBeam);
function createGlowTexture() {
const canvas = document.createElement("canvas");
canvas.width = 128;
canvas.height = 128;
const ctx = canvas.getContext("2d");
const gradient = ctx.createRadialGradient(
64,
64,
0,
64,
64,
64
);
gradient.addColorStop(0, "rgba(255,255,255,1)");
gradient.addColorStop(0.2, "rgba(255,210,130,0.85)");
gradient.addColorStop(0.55, "rgba(255,145,45,0.22)");
gradient.addColorStop(1, "rgba(255,145,45,0)");
ctx.fillStyle = gradient;
ctx.fillRect(0, 0, 128, 128);
return new THREE.CanvasTexture(canvas);
}
const sourceGlow = new THREE.Sprite(
new THREE.SpriteMaterial({
map: createGlowTexture(),
transparent: true,
opacity: 0.75,
blending: THREE.AdditiveBlending,
depthWrite: false,
toneMapped: false
})
);
sourceGlow.position.set(0, 2.7, -6.82);
sourceGlow.scale.set(2.5, 2.5, 1);
scene.add(sourceGlow);
// -------------------------------------------------------------------------
// Adjustable slit barrier
// -------------------------------------------------------------------------
const barrierMaterial = new THREE.MeshStandardMaterial({
color: 0x243b53,
roughness: 0.7,
metalness: 0.4,
flatShading: true
});
const barrierEdgeMaterial = new THREE.LineBasicMaterial({
color: 0x6f98b6,
transparent: true,
opacity: 0.55
});
function createBarrierPanel() {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(1, 1, 1),
barrierMaterial
);
mesh.castShadow = true;
mesh.receiveShadow = true;
const edges = new THREE.LineSegments(
new THREE.EdgesGeometry(mesh.geometry),
barrierEdgeMaterial
);
edges.scale.setScalar(1.003);
mesh.add(edges);
scene.add(mesh);
return mesh;
}
const barrierLeft = createBarrierPanel();
const barrierRight = createBarrierPanel();
const barrierTop = createBarrierPanel();
const barrierBottom = createBarrierPanel();
const slitEdgeMaterial = new THREE.MeshStandardMaterial({
color: 0xffb45c,
emissive: 0xff7b20,
emissiveIntensity: 1.5,
roughness: 0.4,
metalness: 0.25,
flatShading: true
});
const slitEdgeLeft = new THREE.Mesh(
new THREE.BoxGeometry(1, 1, 1),
slitEdgeMaterial
);
const slitEdgeRight = new THREE.Mesh(
new THREE.BoxGeometry(1, 1, 1),
slitEdgeMaterial
);
scene.add(slitEdgeLeft, slitEdgeRight);
const barrierOuterHalf = 6.2;
const barrierHeight = 5.4;
const slitCenterY = 2.7;
const slitVisualHeight = 1.65;
let visualGap = 1;
function updateBarrier() {
const t = (state.slitMm - 0.5) / 4.5;
visualGap = THREE.MathUtils.lerp(0.55, 3.25, t);
const gapHalf = visualGap / 2;
const sideWidth = barrierOuterHalf - gapHalf;
const sideX = (barrierOuterHalf + gapHalf) / 2;
barrierLeft.scale.set(sideWidth, barrierHeight, 0.55);
barrierLeft.position.set(-sideX, barrierHeight / 2, 0);
barrierRight.scale.set(sideWidth, barrierHeight, 0.55);
barrierRight.position.set(sideX, barrierHeight / 2, 0);
const bridgeHeight =
(barrierHeight - slitVisualHeight) / 2;
barrierTop.scale.set(visualGap, bridgeHeight, 0.55);
barrierTop.position.set(
0,
slitCenterY + slitVisualHeight / 2 + bridgeHeight / 2,
0
);
barrierBottom.scale.set(visualGap, bridgeHeight, 0.55);
barrierBottom.position.set(
0,
slitCenterY - slitVisualHeight / 2 - bridgeHeight / 2,
0
);
slitEdgeLeft.scale.set(0.065, slitVisualHeight, 0.61);
slitEdgeLeft.position.set(-gapHalf, slitCenterY, 0);
slitEdgeRight.scale.set(0.065, slitVisualHeight, 0.61);
slitEdgeRight.position.set(gapHalf, slitCenterY, 0);
}
// -------------------------------------------------------------------------
// Far-field detector
// -------------------------------------------------------------------------
const screenMaterial = new THREE.MeshStandardMaterial({
color: 0x0a1727,
roughness: 0.8,
metalness: 0.25,
flatShading: true
});
const frameMaterial = new THREE.MeshStandardMaterial({
color: 0x29465d,
roughness: 0.6,
metalness: 0.48,
flatShading: true
});
const screen = new THREE.Mesh(
new THREE.BoxGeometry(16.5, 8.35, 0.32),
screenMaterial
);
screen.position.set(0, 4.25, 12.25);
screen.receiveShadow = true;
scene.add(screen);
function createBox(material, scale, position) {
const box = new THREE.Mesh(
new THREE.BoxGeometry(1, 1, 1),
material
);
box.scale.set(...scale);
box.position.set(...position);
scene.add(box);
return box;
}
createBox(frameMaterial, [16.8, 0.2, 0.48], [0, 8.45, 12.27]);
createBox(frameMaterial, [16.8, 0.2, 0.48], [0, 0.05, 12.27]);
createBox(frameMaterial, [0.2, 8.6, 0.48], [-8.3, 4.25, 12.27]);
createBox(frameMaterial, [0.2, 8.6, 0.48], [8.3, 4.25, 12.27]);
const graphHalfWidth = 7.15;
const graphBaseY = 1.28;
const graphHeight = 5.45;
const thetaMax = THREE.MathUtils.degToRad(0.12);
const axisMaterial = new THREE.MeshBasicMaterial({
color: 0x6b879d,
transparent: true,
opacity: 0.65
});
createBox(
axisMaterial,
[graphHalfWidth * 2, 0.035, 0.04],
[0, graphBaseY, 12.47]
);
createBox(
axisMaterial,
[0.035, graphHeight + 0.35, 0.04],
[-graphHalfWidth, graphBaseY + graphHeight / 2, 12.47]
);
for (let i = -4; i <= 4; i++) {
const x = (i / 4) * graphHalfWidth;
createBox(
axisMaterial,
[0.025, 0.11, 0.04],
[x, graphBaseY - 0.07, 12.47]
);
}
const barCount = 160;
const barGeometry = new THREE.BoxGeometry(1, 1, 1);
const bars = [];
for (let i = 0; i < barCount; i++) {
const material = new THREE.MeshBasicMaterial({
color: 0x54d9ff,
transparent: true,
opacity: 0.9
});
const bar = new THREE.Mesh(barGeometry, material);
bar.position.z = 12.49;
scene.add(bar);
bars.push(bar);
}
const graphPositions = new Float32Array((barCount + 1) * 3);
const graphGeometry = new THREE.BufferGeometry();
graphGeometry.setAttribute(
"position",
new THREE.BufferAttribute(graphPositions, 3)
);
const graphLine = new THREE.Line(
graphGeometry,
new THREE.LineBasicMaterial({
color: 0xfff0cf,
transparent: true,
opacity: 0.92
})
);
graphLine.position.z = 12.56;
scene.add(graphLine);
const zeroMarkerMaterial = new THREE.MeshBasicMaterial({
color: 0xffb45c,
transparent: true,
opacity: 0.48,
depthWrite: false
});
const zeroMarkerLeft = new THREE.Mesh(
new THREE.BoxGeometry(1, 1, 1),
zeroMarkerMaterial
);
const zeroMarkerRight = new THREE.Mesh(
new THREE.BoxGeometry(1, 1, 1),
zeroMarkerMaterial
);
zeroMarkerLeft.scale.set(0.027, graphHeight, 0.035);
zeroMarkerRight.scale.set(0.027, graphHeight, 0.035);
zeroMarkerLeft.position.y = graphBaseY + graphHeight / 2;
zeroMarkerRight.position.y = graphBaseY + graphHeight / 2;
zeroMarkerLeft.position.z = 12.54;
zeroMarkerRight.position.z = 12.54;
scene.add(zeroMarkerLeft, zeroMarkerRight);
// Diffracted ray lines, including the first-minimum directions.
const rayMeshes = [];
for (let i = 0; i < 7; i++) {
const geometry = new THREE.BufferGeometry();
geometry.setAttribute(
"position",
new THREE.BufferAttribute(new Float32Array(6), 3)
);
const line = new THREE.Line(
geometry,
new THREE.LineBasicMaterial({
color: 0x54d9ff,
transparent: true,
opacity: 0.18
})
);
line.frustumCulled = false;
scene.add(line);
rayMeshes.push(line);
}
// -------------------------------------------------------------------------
// Physical diffraction calculation
// -------------------------------------------------------------------------
function wavelengthMeters() {
return state.wavelengthNm * 1e-9;
}
function apertureMeters() {
return state.slitMm * 1e-3;
}
function firstNullAngle() {
return Math.asin(wavelengthMeters() / apertureMeters());
}
function rawIntensity(theta) {
const beta =
Math.PI *
(apertureMeters() / wavelengthMeters()) *
Math.sin(theta);
if (Math.abs(beta) < 1e-10) {
return 1;
}
const value = Math.sin(beta) / beta;
return value * value;
}
function displayedIntensity(intensity) {
if (state.scale === "linear") {
return intensity;
}
const floor = 1e-4;
return THREE.MathUtils.clamp(
(Math.log10(Math.max(intensity, floor)) + 4) / 4,
0,
1
);
}
const cyan = new THREE.Color(0x35cfff);
const amber = new THREE.Color(0xffa84d);
const hot = new THREE.Color(0xfff1c7);
const tempColor = new THREE.Color();
function intensityColor(value) {
if (value < 0.48) {
return tempColor.copy(cyan).lerp(amber, value / 0.48);
}
return tempColor
.copy(amber)
.lerp(hot, (value - 0.48) / 0.52);
}
function updateGraph() {
const firstNull = firstNullAngle();
const firstNullX =
(firstNull / thetaMax) * graphHalfWidth;
zeroMarkerLeft.position.x = -firstNullX;
zeroMarkerRight.position.x = firstNullX;
const barWidth = (graphHalfWidth * 2 / barCount) * 0.82;
for (let i = 0; i < barCount; i++) {
const theta =
-thetaMax + ((i + 0.5) / barCount) * thetaMax * 2;
const intensity = rawIntensity(theta);
const value = displayedIntensity(intensity);
const height = Math.max(value * graphHeight, 0.012);
const bar = bars[i];
bar.position.x =
-graphHalfWidth + ((i + 0.5) / barCount) * graphHalfWidth * 2;
bar.position.y = graphBaseY + height / 2;
bar.scale.set(barWidth, height, 0.09);
bar.material.color.copy(intensityColor(value));
bar.material.opacity = 0.86 + value * 0.12;
}
for (let i = 0; i <= barCount; i++) {
const theta =
-thetaMax + (i / barCount) * thetaMax * 2;
const intensity = rawIntensity(theta);
const value = displayedIntensity(intensity);
const x =
-graphHalfWidth + (i / barCount) * graphHalfWidth * 2;
const y = graphBaseY + value * graphHeight;
const index = i * 3;
graphPositions[index] = x;
graphPositions[index + 1] = y;
graphPositions[index + 2] = 0;
}
graphGeometry.attributes.position.needsUpdate = true;
graphGeometry.computeBoundingSphere();
}
function updateRays() {
const firstNull = firstNullAngle();
const angles = [
-thetaMax * 0.86,
-thetaMax * 0.52,
-firstNull,
0,
firstNull,
thetaMax * 0.52,
thetaMax * 0.86
];
rayMeshes.forEach((line, index) => {
const theta = angles[index];
const value = displayedIntensity(rawIntensity(theta));
const screenX = (theta / thetaMax) * graphHalfWidth;
const targetY = graphBaseY + value * graphHeight;
const isZeroRay = index === 2 || index === 4;
const isCentralRay = index === 3;
const startX = isZeroRay
? Math.sign(theta) * visualGap * 0.42
: 0;
const positions = line.geometry.attributes.position.array;
positions[0] = startX;
positions[1] = slitCenterY;
positions[2] = 0.48;
positions[3] = screenX;
positions[4] = targetY;
positions[5] = 12.59;
line.geometry.attributes.position.needsUpdate = true;
line.geometry.computeBoundingSphere();
if (isCentralRay) {
line.material.color.set(0xfff1d4);
line.material.opacity = 0.48;
} else if (isZeroRay) {
line.material.color.set(0xffa84d);
line.material.opacity = 0.36;
} else {
line.material.color.set(0x54d9ff);
line.material.opacity = 0.12;
}
});
}
// -------------------------------------------------------------------------
// UI synchronization
// -------------------------------------------------------------------------
function updateRangeFill(input) {
const min = Number(input.min);
const max = Number(input.max);
const value = Number(input.value);
const percent = ((value - min) / (max - min)) * 100;
input.style.setProperty("--pct", `${percent}%`);
}
function updateUI() {
wavelengthValue.textContent = `${state.wavelengthNm.toFixed(0)} nm`;
apertureValue.textContent = `${state.slitMm.toFixed(2)} mm`;
const nullAngle = firstNullAngle();
const nullDegrees = THREE.MathUtils.radToDeg(nullAngle);
firstMinimum.textContent = `${nullDegrees.toFixed(4)}°`;
centralWidth.textContent = `${(nullDegrees * 2).toFixed(4)}°`;
updateRangeFill(wavelengthInput);
updateRangeFill(apertureInput);
}
function updateEverything() {
updateUI();
updateBarrier();
updateGraph();
updateRays();
}
wavelengthInput.addEventListener("input", () => {
state.wavelengthNm = Number(wavelengthInput.value);
updateEverything();
});
apertureInput.addEventListener("input", () => {
state.slitMm = Number(apertureInput.value);
updateEverything();
});
linearButton.addEventListener("click", () => {
state.scale = "linear";
linearButton.classList.add("active");
logButton.classList.remove("active");
scaleBadge.textContent = "Linear scale";
updateGraph();
updateRays();
});
logButton.addEventListener("click", () => {
state.scale = "log";
logButton.classList.add("active");
linearButton.classList.remove("active");
scaleBadge.textContent = "Log₁₀ display";
updateGraph();
updateRays();
});
animationButton.addEventListener("click", () => {
state.animate = !state.animate;
animationButton.textContent = state.animate ? "Pause Wave" : "Play Wave";
});
resetButton.addEventListener("click", () => {
Object.assign(cameraState, defaultCamera);
});
// -------------------------------------------------------------------------
// Direct camera controls: orbit, pan-free drag, wheel zoom, double-click reset
// -------------------------------------------------------------------------
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 dx = event.clientX - previousX;
const dy = event.clientY - previousY;
cameraState.yaw -= dx * 0.006;
cameraState.pitch = THREE.MathUtils.clamp(
cameraState.pitch + dy * 0.0045,
0.08,
1.22
);
previousX = event.clientX;
previousY = event.clientY;
});
function endDrag(event) {
dragging = false;
if (renderer.domElement.hasPointerCapture(event.pointerId)) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
}
renderer.domElement.addEventListener("pointerup", endDrag);
renderer.domElement.addEventListener("pointercancel", endDrag);
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraState.distance = THREE.MathUtils.clamp(
cameraState.distance + event.deltaY * 0.025,
18,
50
);
},
{ passive: false }
);
renderer.domElement.addEventListener("dblclick", () => {
Object.assign(cameraState, defaultCamera);
});
function updateCamera() {
const horizontalDistance =
cameraState.distance * Math.cos(cameraState.pitch);
camera.position.set(
cameraTarget.x +
Math.sin(cameraState.yaw) * horizontalDistance,
cameraTarget.y +
Math.sin(cameraState.pitch) * cameraState.distance,
cameraTarget.z +
Math.cos(cameraState.yaw) * horizontalDistance
);
camera.lookAt(cameraTarget);
}
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
});
// -------------------------------------------------------------------------
// Animation
// -------------------------------------------------------------------------
const reducedMotion = window.matchMedia(
"(prefers-reduced-motion: reduce)"
).matches;
if (reducedMotion) {
state.animate = false;
animationButton.textContent = "Play Wave";
}
renderer.setAnimationLoop((time) => {
const t = time * 0.001;
if (state.animate) {
pulseRings.forEach((ring, index) => {
const pulse = 1 + 0.055 * Math.sin(t * 3.1 - index * 0.75);
ring.scale.setScalar(pulse);
ring.material.opacity =
0.32 + 0.32 * (0.5 + 0.5 * Math.sin(t * 3.1 - index * 0.75));
});
beamMaterial.opacity = 0.06 + 0.015 * Math.sin(t * 2.3);
sourceGlow.material.opacity =
0.62 + 0.14 * Math.sin(t * 3.4);
sourceAccentMaterial.emissiveIntensity =
1.35 + 0.25 * Math.sin(t * 2.1);
slitEdgeMaterial.emissiveIntensity =
1.3 + 0.25 * Math.sin(t * 2.1 + 1.1);
}
updateCamera();
renderer.render(scene, camera);
});
updateEverything();
updateCamera();
</script>
</body>
</html>
```