Prompt specification
Build or customize the Three.js 3D scene "Sound around doorway teaching room" 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: Sound around doorway teaching room
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/sound-around-doorway-teaching-room-x1531
Scene Description Prompt:
Make a standalone HTML scene with Three.js from cdnjs that depicts wave diffraction through openings. Compose a small room and doorway with qualitative sound-wave fronts beyond the opening; explain the wavelength-size relationship without claiming realistic acoustic pressure values. Voxel style.
Working Three.js Starter (sound-around-doorway-teaching-room-x1531):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Voxel Diffraction Lab</title>
<style>
:root {
--ink: #eaf4ff;
--muted: #9fb2c7;
--panel: rgba(8, 17, 30, 0.88);
--border: rgba(150, 205, 255, 0.18);
--cyan: #58c8ff;
--amber: #ffb85c;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #07111e;
color: var(--ink);
font-family:
Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
#app,
#app canvas {
position: fixed;
inset: 0;
width: 100%;
height: 100%;
display: block;
}
#app canvas {
touch-action: none;
cursor: grab;
}
#app canvas:active {
cursor: grabbing;
}
#title {
position: fixed;
z-index: 5;
top: 24px;
left: 26px;
max-width: 430px;
pointer-events: none;
text-shadow: 0 2px 18px rgba(0, 0, 0, 0.75);
}
.eyebrow {
margin-bottom: 8px;
color: var(--cyan);
font-size: 11px;
font-weight: 800;
letter-spacing: 0.19em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: clamp(26px, 3vw, 43px);
line-height: 0.98;
letter-spacing: -0.045em;
}
#title p {
max-width: 390px;
margin: 12px 0 0;
color: var(--muted);
font-size: 13px;
line-height: 1.55;
}
#controls {
position: fixed;
z-index: 10;
top: 22px;
right: 22px;
width: min(355px, calc(100vw - 44px));
padding: 19px;
overflow: auto;
border: 1px solid var(--border);
border-radius: 17px;
background: var(--panel);
box-shadow: 0 18px 60px rgba(0, 0, 0, 0.35);
backdrop-filter: blur(14px);
}
.panel-heading {
display: flex;
align-items: center;
justify-content: space-between;
gap: 10px;
margin-bottom: 18px;
}
.panel-heading strong {
font-size: 14px;
letter-spacing: 0.02em;
}
#ratioBadge {
padding: 5px 9px;
border: 1px solid rgba(88, 200, 255, 0.32);
border-radius: 999px;
color: var(--cyan);
background: rgba(88, 200, 255, 0.08);
font-size: 12px;
font-weight: 800;
white-space: nowrap;
}
.control {
margin: 0 0 16px;
}
.control-line {
display: flex;
align-items: baseline;
justify-content: space-between;
gap: 10px;
margin-bottom: 8px;
font-size: 13px;
}
.control-line output {
color: var(--cyan);
font-variant-numeric: tabular-nums;
font-size: 12px;
}
input[type="range"] {
width: 100%;
margin: 0;
accent-color: var(--cyan);
cursor: pointer;
}
.presets {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 7px;
margin: 3px 0 16px;
}
button {
min-height: 42px;
padding: 8px 6px;
border: 1px solid rgba(160, 205, 245, 0.18);
border-radius: 10px;
color: var(--ink);
background: rgba(255, 255, 255, 0.045);
font: inherit;
font-size: 11px;
line-height: 1.25;
cursor: pointer;
transition:
border-color 150ms ease,
background 150ms ease,
transform 150ms ease;
}
button:hover {
border-color: rgba(88, 200, 255, 0.48);
background: rgba(88, 200, 255, 0.09);
}
button:active {
transform: translateY(1px);
}
button.active {
border-color: var(--cyan);
color: #07111e;
background: var(--cyan);
}
#status {
min-height: 77px;
margin: 0 0 16px;
padding: 13px 14px;
border-left: 3px solid var(--amber);
border-radius: 4px 10px 10px 4px;
background: rgba(255, 184, 92, 0.07);
font-size: 13px;
line-height: 1.48;
}
.explanation {
margin: 0;
padding-top: 15px;
border-top: 1px solid var(--border);
color: var(--muted);
font-size: 12px;
line-height: 1.55;
}
.explanation strong {
color: var(--ink);
}
.disclaimer {
margin: 12px 0 0;
color: #7f96ad;
font-size: 10.5px;
line-height: 1.5;
}
#legend {
position: fixed;
z-index: 5;
left: 26px;
bottom: 22px;
display: flex;
flex-wrap: wrap;
gap: 13px;
color: var(--muted);
font-size: 11px;
pointer-events: none;
}
.legend-item {
display: flex;
align-items: center;
gap: 7px;
}
.swatch {
width: 9px;
height: 9px;
border-radius: 2px;
box-shadow: 0 0 12px currentColor;
}
.swatch.incident {
color: var(--cyan);
background: var(--cyan);
}
.swatch.transmitted {
color: #66ffd1;
background: #66ffd1;
}
.swatch.diffracted {
color: var(--amber);
background: var(--amber);
}
#hint {
position: fixed;
z-index: 5;
right: 24px;
bottom: 22px;
color: #8198ae;
font-size: 11px;
pointer-events: none;
}
.tag {
position: fixed;
z-index: 4;
padding: 6px 9px;
border: 1px solid rgba(151, 209, 255, 0.24);
border-radius: 7px;
color: #dcecff;
background: rgba(7, 17, 30, 0.78);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.1em;
text-transform: uppercase;
white-space: nowrap;
pointer-events: none;
transform: translate(-50%, -50%);
opacity: 0;
transition: opacity 100ms linear;
}
.tag::before {
content: "";
display: inline-block;
width: 6px;
height: 6px;
margin-right: 7px;
border-radius: 2px;
background: currentColor;
box-shadow: 0 0 10px currentColor;
}
#sourceTag {
color: var(--cyan);
}
#openingTag {
color: #dcecff;
}
#diffractionTag {
color: var(--amber);
}
@media (max-width: 760px) {
#title {
top: 16px;
left: 16px;
max-width: calc(100vw - 32px);
}
#title p {
display: none;
}
#controls {
top: auto;
right: 12px;
bottom: 12px;
width: calc(100vw - 24px);
max-height: 54vh;
padding: 15px;
}
#legend,
#hint {
display: none;
}
.tag {
font-size: 9px;
}
}
</style>
</head>
<body>
<div id="app"></div>
<header id="title">
<div class="eyebrow">Qualitative wavefront study</div>
<h1>Voxel<br />Diffraction Lab</h1>
<p>
Adjust the wavelength and doorway width. Blue planes approach the room,
while teal fronts continue through the opening and amber arcs reveal
edge spreading.
</p>
</header>
<aside id="controls">
<div class="panel-heading">
<strong>Opening / wavelength</strong>
<span id="ratioBadge">W / λ = 2.00</span>
</div>
<div class="control">
<div class="control-line">
<label for="wavelength">Wavelength λ</label>
<output id="wavelengthValue">0.75 units</output>
</div>
<input
id="wavelength"
type="range"
min="0.45"
max="1.10"
step="0.01"
value="0.75"
/>
</div>
<div class="control">
<div class="control-line">
<label for="opening">Opening width W</label>
<output id="openingValue">1.50 units</output>
</div>
<input
id="opening"
type="range"
min="0.35"
max="4.40"
step="0.05"
value="1.50"
/>
</div>
<div class="presets" aria-label="Opening-to-wavelength presets">
<button type="button" data-ratio="4">Wide<br />W ≈ 4λ</button>
<button type="button" data-ratio="1">Matched<br />W ≈ λ</button>
<button type="button" data-ratio="0.5">Narrow<br />W ≈ ½λ</button>
</div>
<div id="status"></div>
<p class="explanation">
<strong>How to read it:</strong>
A wide opening passes a mostly planar wavefront, with bending concentrated
near its edges. When the opening is comparable to the wavelength, the
edge waves spread into one another. A much narrower opening produces
strong spreading and a weaker transmitted component.
</p>
<p class="disclaimer">
This is a qualitative wavefront sketch. Colors, animation speed, and
relative units are illustrative—not a prediction of acoustic pressure,
sound level, or real-room performance.
</p>
</aside>
<div id="sourceTag" class="tag">Source room</div>
<div id="openingTag" class="tag">Aperture</div>
<div id="diffractionTag" class="tag">Diffraction edge waves</div>
<div id="legend">
<div class="legend-item">
<span class="swatch incident"></span>
Incident plane wave
</div>
<div class="legend-item">
<span class="swatch transmitted"></span>
Transmitted component
</div>
<div class="legend-item">
<span class="swatch diffracted"></span>
Edge diffraction
</div>
</div>
<div id="hint">Drag to orbit · wheel to zoom</div>
<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 app = document.getElementById("app");
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x07111e);
scene.fog = new THREE.Fog(0x07111e, 19, 38);
const camera = new THREE.PerspectiveCamera(
42,
innerWidth / innerHeight,
0.1,
100
);
const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setSize(innerWidth, innerHeight);
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.12;
app.appendChild(renderer.domElement);
const hemisphere = new THREE.HemisphereLight(0xb9ddff, 0x172033, 1.7);
scene.add(hemisphere);
const keyLight = new THREE.DirectionalLight(0xffdfb0, 3.2);
keyLight.position.set(-5, 10, 7);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -12;
keyLight.shadow.camera.right = 12;
keyLight.shadow.camera.top = 12;
keyLight.shadow.camera.bottom = -12;
keyLight.shadow.camera.near = 0.5;
keyLight.shadow.camera.far = 35;
keyLight.shadow.bias = -0.0004;
scene.add(keyLight);
const blueFill = new THREE.PointLight(0x3caeff, 9, 14, 2);
blueFill.position.set(-4, 2.2, 0);
scene.add(blueFill);
const warmFill = new THREE.PointLight(0xff9d48, 7, 13, 2);
warmFill.position.set(4, 2.5, 1.5);
scene.add(warmFill);
const unitBox = new THREE.BoxGeometry(1, 1, 1);
const dummy = new THREE.Object3D();
const identityQuaternion = new THREE.Quaternion();
const materials = {
floor: new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 0.92,
metalness: 0.02
}),
wall: new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 0.86,
metalness: 0.03
}),
frame: new THREE.MeshStandardMaterial({
color: 0xe9a34e,
roughness: 0.62,
metalness: 0.12
}),
cabinet: new THREE.MeshStandardMaterial({
color: 0x23364b,
roughness: 0.58,
metalness: 0.24
}),
dark: new THREE.MeshStandardMaterial({
color: 0x07111d,
roughness: 0.8
}),
sourceBlue: new THREE.MeshStandardMaterial({
color: 0x2b9bd2,
emissive: 0x0b5c8c,
emissiveIntensity: 1.6,
roughness: 0.48
}),
sourceLight: new THREE.MeshStandardMaterial({
color: 0xa9edff,
emissive: 0x42bfff,
emissiveIntensity: 2.4,
roughness: 0.3
})
};
function addBox(parent, size, position, material, castShadow = true) {
const mesh = new THREE.Mesh(unitBox, material);
mesh.scale.set(...size);
mesh.position.set(...position);
mesh.castShadow = castShadow;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function blockTransform(x, y, z, sx, sy, sz) {
return new THREE.Matrix4().compose(
new THREE.Vector3(x, y, z),
identityQuaternion,
new THREE.Vector3(sx, sy, sz)
);
}
function createInstancedBlocks(items, material, parent) {
const mesh = new THREE.InstancedMesh(
unitBox,
material,
items.length
);
items.forEach((item, index) => {
mesh.setMatrixAt(index, item.matrix);
mesh.setColorAt(index, new THREE.Color(item.color));
});
mesh.instanceMatrix.needsUpdate = true;
if (mesh.instanceColor) mesh.instanceColor.needsUpdate = true;
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
// -----------------------------------------------------------------------
// Static voxel floor and cutaway room
// -----------------------------------------------------------------------
const world = new THREE.Group();
scene.add(world);
const floorItems = [];
const floorColors = [0x18283b, 0x1b2d42, 0x203247, 0x1d3044];
for (let ix = 0; ix < 18; ix++) {
for (let iz = 0; iz < 14; iz++) {
const x = -6 + (ix + 0.5) * 0.75;
const z = -5.25 + (iz + 0.5) * 0.75;
const color = floorColors[(ix * 3 + iz * 5) % floorColors.length];
floorItems.push({
matrix: blockTransform(x, -0.14, z, 0.71, 0.24, 0.71),
color
});
}
}
createInstancedBlocks(floorItems, materials.floor, world);
const roomShellItems = [];
const shellColors = [0x415267, 0x465a70, 0x3b4d62, 0x506278];
// Back wall of the source room.
for (let ix = 0; ix < 8; ix++) {
const x = -6 + (ix + 0.5) * 0.75;
for (let iy = 0; iy < 8; iy++) {
const y = (iy + 0.5) * 0.525;
roomShellItems.push({
matrix: blockTransform(
x,
y,
-5.05,
0.72,
0.495,
0.42
),
color: shellColors[(ix + iy) % shellColors.length]
});
}
}
// Left wall of the source room.
for (let iz = 0; iz < 14; iz++) {
const z = -5.25 + (iz + 0.5) * 0.75;
for (let iy = 0; iy < 8; iy++) {
const y = (iy + 0.5) * 0.525;
roomShellItems.push({
matrix: blockTransform(
-6.05,
y,
z,
0.42,
0.495,
0.72
),
color: shellColors[(iz * 2 + iy) % shellColors.length]
});
}
}
createInstancedBlocks(roomShellItems, materials.wall, world);
// Low front lip communicates the cutaway without blocking the view.
const lipItems = [];
for (let ix = 0; ix < 8; ix++) {
lipItems.push({
matrix: blockTransform(
-6 + (ix + 0.5) * 0.75,
0.18,
5.12,
0.71,
0.34,
0.42
),
color: shellColors[ix % shellColors.length]
});
}
createInstancedBlocks(lipItems, materials.wall, world);
// Voxel source speaker facing +X.
const source = new THREE.Group();
world.add(source);
addBox(source, [1.25, 0.22, 1.7], [-4.65, 0.1, 0], materials.dark);
addBox(source, [1.0, 1.65, 1.42], [-4.65, 1.02, 0], materials.cabinet);
addBox(source, [1.12, 0.14, 1.55], [-4.65, 1.91, 0], materials.sourceBlue);
// Stepped square driver.
addBox(source, [0.10, 1.02, 1.02], [-4.09, 1.05, 0], materials.dark);
addBox(source, [0.12, 0.76, 0.76], [-4.02, 1.05, 0], materials.sourceBlue);
addBox(source, [0.14, 0.42, 0.42], [-3.94, 1.05, 0], materials.sourceLight);
addBox(source, [0.13, 0.12, 0.12], [-3.95, 1.64, 0.48], materials.sourceLight);
// -----------------------------------------------------------------------
// Dynamic aperture wall
// -----------------------------------------------------------------------
const divider = new THREE.Group();
world.add(divider);
const apertureHeight = 2.8;
const wallHeight = 4.2;
const wallDepth = 0.48;
let wavelength = 0.75;
let openingWidth = 1.5;
function rebuildDivider() {
for (const child of [...divider.children]) {
if (child.isInstancedMesh) child.dispose();
divider.remove(child);
}
const items = [];
const half = openingWidth / 2;
const cuts = [-5, 5];
for (let value = -4.5; value < 5; value += 0.5) {
cuts.push(value);
}
if (-half > -5 && -half < 5) cuts.push(-half);
if (half > -5 && half < 5) cuts.push(half);
cuts.sort((a, b) => a - b);
function addWallColumn(z0, z1, y0, y1, colorOffset) {
const z = (z0 + z1) / 2;
const dz = z1 - z0;
const height = y1 - y0;
const rows = Math.max(1, Math.round(height / 0.525));
const rowHeight = height / rows;
for (let row = 0; row < rows; row++) {
const y = y0 + (row + 0.5) * rowHeight;
const color = shellColors[
(Math.abs(Math.round(z * 2)) + row + colorOffset) % shellColors.length
];
items.push({
matrix: blockTransform(
0,
y,
z,
wallDepth - 0.025,
rowHeight - 0.025,
dz - 0.025
),
color
});
}
}
for (let i = 0; i < cuts.length - 1; i++) {
const z0 = cuts[i];
const z1 = cuts[i + 1];
const middle = (z0 + z1) / 2;
if (Math.abs(middle - cuts[Math.floor(cuts.length / 2)]) < 0.0001) {
continue;
}
if (middle < -half || middle > half) {
addWallColumn(z0, z1, 0, wallHeight, i);
} else {
addWallColumn(z0, z1, apertureHeight, wallHeight, i + 1);
}
}
createInstancedBlocks(items, materials.wall, divider);
// Amber voxel frame around the opening.
addBox(
divider,
[0.66, apertureHeight + 0.12, 0.16],
[0, apertureHeight / 2, -half - 0.04],
materials.frame
);
addBox(
divider,
[0.66, apertureHeight + 0.12, 0.16],
[0, apertureHeight / 2, half + 0.04],
materials.frame
);
addBox(
divider,
[0.66, 0.16, openingWidth + 0.32],
[0, apertureHeight + 0.04, 0],
materials.frame
);
}
// -----------------------------------------------------------------------
// Qualitative wavefront graphics
// -----------------------------------------------------------------------
const incidentMaterial = new THREE.MeshBasicMaterial({
color: 0x4abaff,
transparent: true,
opacity: 0.34,
depthWrite: false,
blending: THREE.AdditiveBlending
});
const transmittedMaterial = new THREE.MeshBasicMaterial({
color: 0x61ffd1,
transparent: true,
opacity: 0.43,
depthWrite: false,
blending: THREE.AdditiveBlending
});
const diffractionMaterial = new THREE.MeshBasicMaterial({
color: 0xffb45b,
transparent: true,
opacity: 0.72,
depthWrite: false,
blending: THREE.AdditiveBlending
});
const incidentRoot = new THREE.Group();
const transmittedRoot = new THREE.Group();
scene.add(incidentRoot, transmittedRoot);
let incidentFronts = [];
let transmittedFronts = [];
let diffractionMesh = null;
const sourceX = -3.88;
const visualWaveSpeed = 1.55;
function disposeInstancedChildren(root) {
for (const child of [...root.children]) {
if (child.isInstancedMesh) child.dispose();
root.remove(child);
}
}
function makeSheetMesh(zMin, zMax, yMin, yMax, step, material) {
const zCount = Math.max(1, Math.floor((zMax - zMin) / step) + 1);
const yCount = Math.max(1, Math.floor((yMax - yMin) / step) + 1);
const count = zCount * yCount;
const mesh = new THREE.InstancedMesh(unitBox, material, count);
mesh.instanceMatrix.setUsage(THREE.StaticDrawUsage);
mesh.frustumCulled = false;
mesh.renderOrder = 3;
let index = 0;
for (let iy = 0; iy < yCount; iy++) {
for (let iz = 0; iz < zCount; iz++) {
const z = zMin + iz * step;
const y = yMin + iy * step;
dummy.position.set(0, y, z);
dummy.scale.set(0.105, 0.105, 0.105);
dummy.updateMatrix();
mesh.setMatrixAt(index++, dummy.matrix);
}
}
mesh.instanceMatrix.needsUpdate = true;
return mesh;
}
function rebuildIncidentFronts() {
disposeInstancedChildren(incidentRoot);
incidentFronts = [];
const distance = Math.abs(-0.26 - sourceX);
const count = Math.ceil(distance / wavelength) + 1;
for (let i = 0; i < count; i++) {
const sheet = makeSheetMesh(
-4.65,
4.65,
0.32,
4.05,
0.28,
incidentMaterial
);
incidentRoot.add(sheet);
incidentFronts.push(sheet);
}
}
function rebuildTransmittedFronts() {
disposeInstancedChildren(transmittedRoot);
transmittedFronts = [];
const count = Math.ceil(7.4 / wavelength) + 1;
const clearWidth = Math.max(0.25, openingWidth - 0.16);
for (let i = 0; i < count; i++) {
const sheet = makeSheetMesh(
-clearWidth / 2,
clearWidth / 2,
0.23,
apertureHeight - 0.08,
0.24,
transmittedMaterial
);
transmittedRoot.add(sheet);
transmittedFronts.push(sheet);
}
}
function rebuildDiffractionMesh() {
if (diffractionMesh) {
diffractionMesh.dispose();
scene.remove(diffractionMesh);
}
const radiusCount = Math.ceil(7.2 / wavelength) + 1;
const angles = 34;
const count = radiusCount * 2 * angles;
diffractionMesh = new THREE.InstancedMesh(
unitBox,
diffractionMaterial,
count
);
diffractionMesh.instanceMatrix.setUsage(THREE.DynamicDrawUsage);
diffractionMesh.frustumCulled = false;
diffractionMesh.renderOrder = 2;
scene.add(diffractionMesh);
}
function updateDiffraction(phase) {
if (!diffractionMesh) return;
const maxRadius = 7.2;
const radiusCount = Math.ceil(maxRadius / wavelength) + 1;
const angles = 34;
const halfOpening = openingWidth / 2;
let index = 0;
for (let radiusIndex = 0; radiusIndex < radiusCount; radiusIndex++) {
const radius = phase + radiusIndex * wavelength;
if (radius > maxRadius) continue;
for (const edge of [-halfOpening, halfOpening]) {
for (let angleIndex = 0; angleIndex < angles; angleIndex++) {
const angle =
-Math.PI / 2 +
(Math.PI * angleIndex) / (angles - 1);
dummy.position.set(
radius * Math.cos(angle),
0.13,
edge + radius * Math.sin(angle)
);
dummy.scale.set(0.105, 0.10, 0.105);
dummy.updateMatrix();
diffractionMesh.setMatrixAt(index++, dummy.matrix);
}
}
}
diffractionMesh.count = index;
diffractionMesh.instanceMatrix.needsUpdate = true;
}
// -----------------------------------------------------------------------
// UI
// -----------------------------------------------------------------------
const wavelengthInput = document.getElementById("wavelength");
const openingInput = document.getElementById("opening");
const wavelengthValue = document.getElementById("wavelengthValue");
const openingValue = document.getElementById("openingValue");
const ratioBadge = document.getElementById("ratioBadge");
const status = document.getElementById("status");
const presetButtons = [...document.querySelectorAll("[data-ratio]")];
function updateStatus() {
const ratio = openingWidth / wavelength;
ratioBadge.textContent = `W / λ = ${ratio.toFixed(2)}`;
if (ratio >= 3) {
status.innerHTML =
"<strong>Wide aperture.</strong> The transmitted front stays mostly planar. " +
"Diffraction is concentrated near the two edges, with little spreading between them.";
} else if (ratio >= 1.5) {
status.innerHTML =
"<strong>Moderate diffraction.</strong> The opening is wider than one wavelength, " +
"but edge waves still spread into the space beyond the doorway.";
} else if (ratio >= 0.8) {
status.innerHTML =
"<strong>Opening comparable to λ.</strong> The two edge waves overlap strongly, " +
"so the transmitted wave fans outward instead of remaining sharply bounded.";
} else {
status.innerHTML =
"<strong>Narrow aperture.</strong> The opening is smaller than the wavelength. " +
"Strong sideways spreading appears and the directly transmitted component becomes weaker.";
}
transmittedMaterial.opacity = THREE.MathUtils.clamp(
0.24 + ratio * 0.075,
0.24,
0.56
);
diffractionMaterial.opacity = THREE.MathUtils.clamp(
0.9 - ratio * 0.17,
0.32,
0.87
);
for (const button of presetButtons) {
const preset = Number(button.dataset.ratio);
button.classList.toggle(
"active",
Math.abs(ratio - preset) < 0.08
);
}
}
function rebuildWaves() {
rebuildIncidentFronts();
rebuildTransmittedFronts();
rebuildDiffractionMesh();
updateStatus();
}
wavelengthInput.addEventListener("input", () => {
wavelength = Number(wavelengthInput.value);
wavelengthValue.textContent = `${wavelength.toFixed(2)} units`;
rebuildWaves();
});
openingInput.addEventListener("input", () => {
openingWidth = Number(openingInput.value);
openingValue.textContent = `${openingWidth.toFixed(2)} units`;
rebuildDivider();
rebuildTransmittedFronts();
updateStatus();
});
for (const button of presetButtons) {
button.addEventListener("click", () => {
const ratio = Number(button.dataset.ratio);
openingWidth = THREE.MathUtils.clamp(
wavelength * ratio,
Number(openingInput.min),
Number(openingInput.max)
);
openingInput.value = openingWidth.toFixed(2);
openingValue.textContent = `${openingWidth.toFixed(2)} units`;
rebuildDivider();
rebuildTransmittedFronts();
updateStatus();
});
}
rebuildDivider();
rebuildWaves();
// -----------------------------------------------------------------------
// Direct camera controls: no addons
// -----------------------------------------------------------------------
const target = new THREE.Vector3(-0.45, 1.55, 0);
let cameraRadius = 16.8;
let cameraTheta = 0.72;
let cameraPhi = 1.11;
let dragging = false;
let previousX = 0;
let previousY = 0;
function updateCamera() {
const sinPhi = Math.sin(cameraPhi);
camera.position.set(
target.x + cameraRadius * sinPhi * Math.sin(cameraTheta),
target.y + cameraRadius * Math.cos(cameraPhi),
target.z + cameraRadius * sinPhi * Math.cos(cameraTheta)
);
camera.lookAt(target);
}
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;
cameraTheta -= deltaX * 0.006;
cameraPhi = THREE.MathUtils.clamp(
cameraPhi - deltaY * 0.005,
0.36,
1.42
);
updateCamera();
});
function stopDragging(event) {
dragging = false;
if (renderer.domElement.hasPointerCapture(event.pointerId)) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
}
renderer.domElement.addEventListener("pointerup", stopDragging);
renderer.domElement.addEventListener("pointercancel", stopDragging);
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraRadius = THREE.MathUtils.clamp(
cameraRadius * Math.exp(event.deltaY * 0.001),
8,
27
);
updateCamera();
},
{ passive: false }
);
renderer.domElement.addEventListener("contextmenu", (event) => {
event.preventDefault();
});
updateCamera();
// -----------------------------------------------------------------------
// Projected labels
// -----------------------------------------------------------------------
const labels = [
{
element: document.getElementById("sourceTag"),
world: new THREE.Vector3(-4.15, 2.45, 0)
},
{
element: document.getElementById("openingTag"),
world: new THREE.Vector3(0, 3.28, 0)
},
{
element: document.getElementById("diffractionTag"),
world: new THREE.Vector3(4.45, 0.35, 2.65)
}
];
function updateLabels() {
for (const label of labels) {
const projected = label.world.clone().project(camera);
const visible =
projected.z > -1 &&
projected.z < 1 &&
projected.x > -1.1 &&
projected.x < 1.1 &&
projected.y > -1.1 &&
projected.y < 1.1;
label.element.style.opacity = visible ? "1" : "0";
if (visible) {
label.element.style.left =
`${(projected.x * 0.5 + 0.5) * innerWidth}px`;
label.element.style.top =
`${(-projected.y * 0.5 + 0.5) * innerHeight}px`;
}
}
}
// -----------------------------------------------------------------------
// Resize and animation
// -----------------------------------------------------------------------
addEventListener("resize", () => {
camera.aspect = innerWidth / innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(innerWidth, innerHeight);
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
});
const clock = new THREE.Clock();
let elapsed = 0;
renderer.setAnimationLoop(() => {
const delta = Math.min(clock.getDelta(), 0.05);
elapsed += delta;
const phase = (elapsed * visualWaveSpeed) % wavelength;
incidentFronts.forEach((front, index) => {
const x = sourceX + phase + index * wavelength;
front.position.x = x;
front.visible = x < -0.27;
});
transmittedFronts.forEach((front, index) => {
const x = phase + index * wavelength;
front.position.x = x;
front.visible = x < 7.55;
});
updateDiffraction(phase);
updateLabels();
renderer.render(scene, camera);
});
</script>
</body>
</html>
```