Prompt specification
Build or customize the Three.js 3D scene "Parallel and angled wall reflections" 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: Parallel and angled wall reflections
Category: Architecture & Cities · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/parallel-and-angled-wall-reflections-x2162
Scene Description Prompt:
Make one HTML file using Three.js from cdnjs for this cutaway scene: Compare sound-ray paths between parallel walls and a slightly angled pair. Keep source position identical and show the repeated reflection pattern as a geometric approximation.
Working Three.js Starter (parallel-and-angled-wall-reflections-x2162):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Acoustic Ray Geometry — Parallel vs. Angled Walls</title>
<style>
:root {
color-scheme: dark;
--bg: #071019;
--panel: rgba(12, 24, 34, 0.78);
--line: rgba(143, 213, 235, 0.2);
--text: #e9f7fb;
--muted: #8ca8b5;
--cyan: #65e7ff;
--amber: #ffb663;
--orange: #ff7448;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background:
radial-gradient(circle at 18% 16%, rgba(42, 105, 130, 0.2), transparent 34%),
radial-gradient(circle at 82% 30%, rgba(125, 79, 42, 0.16), transparent 31%),
linear-gradient(155deg, #0a1620 0%, #050b11 68%, #071018 100%);
color: var(--text);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
#stage,
#stage canvas {
position: fixed;
inset: 0;
width: 100%;
height: 100%;
}
#stage canvas {
display: block;
touch-action: none;
cursor: grab;
}
#stage canvas:active {
cursor: grabbing;
}
.vignette {
position: fixed;
inset: 0;
pointer-events: none;
background:
linear-gradient(to bottom, rgba(2, 7, 11, 0.4), transparent 22%, transparent 72%, rgba(2, 7, 11, 0.62)),
radial-gradient(ellipse at center, transparent 44%, rgba(0, 0, 0, 0.36) 100%);
z-index: 2;
}
.ui {
position: fixed;
inset: 0;
z-index: 3;
pointer-events: none;
}
header {
position: absolute;
top: 0;
left: 0;
right: 0;
min-height: 86px;
padding: 20px 28px;
display: flex;
align-items: flex-start;
justify-content: space-between;
gap: 24px;
border-bottom: 1px solid rgba(132, 196, 219, 0.12);
background: linear-gradient(to bottom, rgba(5, 12, 18, 0.78), rgba(5, 12, 18, 0.08));
}
.eyebrow {
margin: 0 0 6px;
color: var(--cyan);
font-size: 10px;
font-weight: 750;
letter-spacing: 0.24em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: clamp(20px, 2.2vw, 31px);
line-height: 1.05;
font-weight: 630;
letter-spacing: -0.035em;
}
.subtitle {
margin-top: 7px;
color: var(--muted);
font-size: 12px;
letter-spacing: 0.015em;
}
.controls {
pointer-events: auto;
display: flex;
align-items: center;
justify-content: flex-end;
flex-wrap: wrap;
gap: 8px;
max-width: 650px;
}
button,
.angle-control {
border: 1px solid rgba(153, 213, 232, 0.18);
background: rgba(10, 23, 32, 0.76);
color: #dcecf1;
border-radius: 7px;
min-height: 36px;
font: inherit;
font-size: 11px;
letter-spacing: 0.035em;
}
button {
padding: 0 13px;
cursor: pointer;
transition: border-color 160ms ease, background 160ms ease, color 160ms ease;
}
button:hover {
border-color: rgba(101, 231, 255, 0.55);
background: rgba(18, 41, 53, 0.88);
color: #fff;
}
.angle-control {
display: flex;
align-items: center;
gap: 10px;
padding: 0 12px;
color: var(--muted);
}
.angle-control output {
min-width: 36px;
color: var(--text);
font-variant-numeric: tabular-nums;
text-align: right;
}
input[type="range"] {
width: 100px;
accent-color: var(--cyan);
cursor: pointer;
}
.panel-label {
position: absolute;
top: 108px;
display: flex;
align-items: center;
gap: 10px;
transform: translateX(-50%);
padding: 9px 13px 9px 9px;
border: 1px solid rgba(156, 214, 232, 0.16);
border-radius: 9px;
background: rgba(8, 19, 27, 0.68);
backdrop-filter: blur(12px);
box-shadow: 0 12px 40px rgba(0, 0, 0, 0.16);
}
.panel-label.left {
left: 25%;
}
.panel-label.right {
left: 75%;
}
.index {
display: grid;
place-items: center;
width: 31px;
height: 31px;
border-radius: 6px;
color: #061018;
font-size: 11px;
font-weight: 800;
}
.parallel .index {
background: var(--cyan);
}
.angled .index {
background: var(--amber);
}
.panel-label strong {
display: block;
font-size: 12px;
font-weight: 670;
letter-spacing: 0.02em;
}
.panel-label small {
display: block;
margin-top: 3px;
color: var(--muted);
font-size: 10px;
}
.comparison-card {
position: absolute;
bottom: 22px;
width: min(330px, 30vw);
padding: 13px 15px 14px;
border: 1px solid rgba(156, 214, 232, 0.15);
border-radius: 10px;
background: linear-gradient(145deg, rgba(12, 27, 38, 0.88), rgba(7, 17, 25, 0.78));
backdrop-filter: blur(14px);
box-shadow: 0 18px 50px rgba(0, 0, 0, 0.22);
}
.comparison-card.left {
left: 25%;
transform: translateX(-50%);
}
.comparison-card.right {
left: 75%;
transform: translateX(-50%);
}
.card-topline {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
margin-bottom: 7px;
}
.card-topline span {
color: var(--muted);
font-size: 9px;
font-weight: 750;
letter-spacing: 0.18em;
text-transform: uppercase;
}
.contacts {
color: #edfaff;
font-size: 11px;
font-variant-numeric: tabular-nums;
}
.comparison-card h2 {
margin: 0;
font-size: 14px;
font-weight: 650;
}
.comparison-card p {
margin: 5px 0 0;
color: #91a9b4;
font-size: 10.5px;
line-height: 1.5;
}
.source-note {
position: absolute;
left: 50%;
bottom: 28px;
transform: translateX(-50%);
color: rgba(183, 210, 220, 0.7);
font-size: 9.5px;
letter-spacing: 0.06em;
text-align: center;
white-space: nowrap;
}
.source-note b {
color: var(--orange);
font-weight: 650;
}
.camera-hint {
position: absolute;
right: 28px;
bottom: 30px;
color: rgba(160, 187, 199, 0.55);
font-size: 9px;
letter-spacing: 0.08em;
text-transform: uppercase;
}
@media (max-width: 980px) {
header {
padding: 16px 18px;
}
.controls {
max-width: 440px;
}
.comparison-card {
width: 29vw;
}
.source-note,
.camera-hint {
display: none;
}
}
@media (max-width: 720px) {
header {
align-items: flex-start;
flex-direction: column;
gap: 13px;
}
.controls {
width: 100%;
justify-content: flex-start;
}
.panel-label {
top: 147px;
padding: 7px;
}
.panel-label small {
display: none;
}
.comparison-card {
bottom: 12px;
width: 44vw;
padding: 10px;
}
.comparison-card p {
display: none;
}
.subtitle {
display: none;
}
}
</style>
</head>
<body>
<div id="stage"></div>
<div class="vignette"></div>
<div class="ui">
<header>
<div>
<p class="eyebrow">Acoustic geometry / cutaway study</p>
<h1>Parallel walls vs. a splayed pair</h1>
<div class="subtitle">
Identical source coordinate, specular horizontal-plane reflections,
visualized as repeated geometric ray paths.
</div>
</div>
<div class="controls">
<label class="angle-control" for="angle">
Half-angle
<input id="angle" type="range" min="28" max="52" step="1" value="42" />
<output id="angleValue">42°</output>
</label>
<button id="viewButton" type="button">Plan view</button>
<button id="resetButton" type="button">Reset</button>
<button id="pauseButton" type="button">Pause rays</button>
</div>
</header>
<div class="panel-label left parallel">
<div class="index">01</div>
<div>
<strong>Parallel walls</strong>
<small>constant reflection interval</small>
</div>
</div>
<div class="panel-label right angled">
<div class="index">02</div>
<div>
<strong>Slightly angled walls</strong>
<small>successive intervals shorten</small>
</div>
</div>
<div class="comparison-card left">
<div class="card-topline">
<span>Parallel</span>
<div class="contacts"><b id="parallelContacts">—</b> contacts</div>
</div>
<h2>Even geometric beat</h2>
<p>
Parallel boundaries preserve the distance between successive wall contacts,
producing a regular sequence of reflected segments.
</p>
</div>
<div class="comparison-card right">
<div class="card-topline">
<span>Angled</span>
<div class="contacts"><b id="angledContacts">—</b> contacts</div>
</div>
<h2>Compressing geometric beat</h2>
<p>
The converging boundaries shorten later intervals, so repeated contacts
become denser toward the far end of the cutaway.
</p>
</div>
<div class="source-note">
SAME LOCAL SOURCE <b>S = (0.00, 0.72, 4.20)</b>
· SPECULAR RAY GEOMETRY · NO DIFFRACTION OR SCATTERING
</div>
<div class="camera-hint">Drag to orbit · wheel 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";
// ---------------------------------------------------------------------
// Constants and geometry
// ---------------------------------------------------------------------
const stage = document.querySelector("#stage");
const angleInput = document.querySelector("#angle");
const angleValue = document.querySelector("#angleValue");
const viewButton = document.querySelector("#viewButton");
const resetButton = document.querySelector("#resetButton");
const pauseButton = document.querySelector("#pauseButton");
const parallelContacts = document.querySelector("#parallelContacts");
const angledContacts = document.querySelector("#angledContacts");
const FAR_Z = -5.55;
const NEAR_Z = 4.80;
const SOURCE_Y = 0.72;
const SOURCE = new THREE.Vector3(0, SOURCE_Y, 4.20);
const NEAR_HALF_WIDTH = 2.43;
const FAR_HALF_WIDTH = 2.05;
const PARALLEL_HALF_WIDTH = 2.43;
const COLORS = {
cyan: 0x65e7ff,
amber: 0xffb663,
orange: 0xff7448,
ray: 0xa8f9ff,
raySecondary: 0x54d5e8,
wall: 0x1b3441,
floor: 0x0d1a24
};
const scene = new THREE.Scene();
scene.fog = new THREE.FogExp2(0x071019, 0.014);
const camera = new THREE.PerspectiveCamera(
42,
innerWidth / innerHeight,
0.1,
160
);
const renderer = new THREE.WebGLRenderer({
antialias: true,
alpha: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
renderer.setSize(innerWidth, innerHeight);
renderer.setClearColor(0x071019, 0);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.08;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
stage.appendChild(renderer.domElement);
const hemisphere = new THREE.HemisphereLight(0x9bdcf1, 0x10121b, 1.35);
scene.add(hemisphere);
const keyLight = new THREE.DirectionalLight(0xd7f3ff, 2.2);
keyLight.position.set(-9, 15, 10);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(1024, 1024);
keyLight.shadow.camera.left = -16;
keyLight.shadow.camera.right = 16;
keyLight.shadow.camera.top = 13;
keyLight.shadow.camera.bottom = -13;
keyLight.shadow.camera.near = 1;
keyLight.shadow.camera.far = 45;
keyLight.shadow.bias = -0.0005;
const keyTarget = new THREE.Object3D();
keyTarget.position.set(0, 0, -0.4);
scene.add(keyTarget);
keyLight.target = keyTarget;
const fillLight = new THREE.DirectionalLight(0xffb56f, 0.55);
fillLight.position.set(12, 8, -5);
scene.add(fillLight);
const ground = new THREE.Mesh(
new THREE.PlaneGeometry(90, 90),
new THREE.MeshStandardMaterial({
color: 0x060c12,
roughness: 0.95,
metalness: 0.05
})
);
ground.rotation.x = -Math.PI / 2;
ground.position.y = -0.25;
ground.receiveShadow = true;
scene.add(ground);
// ---------------------------------------------------------------------
// Direct camera controls — no addons
// ---------------------------------------------------------------------
const cameraTarget = new THREE.Vector3(0, 0.35, -0.35);
const cameraState = {
theta: 0.015,
phi: 0.78,
radius: 30
};
let planView = false;
function recommendedRadius() {
const aspect = innerWidth / innerHeight;
return 29.5 * Math.max(1, 1.08 / aspect);
}
function updateCamera() {
const { theta, phi, radius } = cameraState;
const sinPhi = Math.sin(phi);
camera.position.set(
cameraTarget.x + radius * sinPhi * Math.sin(theta),
cameraTarget.y + radius * Math.cos(phi),
cameraTarget.z + radius * sinPhi * Math.cos(theta)
);
camera.lookAt(cameraTarget);
}
function resetCamera() {
planView = false;
cameraState.theta = 0.015;
cameraState.phi = 0.78;
cameraState.radius = recommendedRadius();
viewButton.textContent = "Plan view";
updateCamera();
}
function toggleView() {
planView = !planView;
cameraState.theta = 0;
cameraState.radius = recommendedRadius() * (planView ? 1.04 : 1);
if (planView) {
cameraState.phi = 0.035;
viewButton.textContent = "Oblique view";
} else {
cameraState.phi = 0.78;
viewButton.textContent = "Plan view";
}
updateCamera();
}
let dragging = false;
let pointerX = 0;
let pointerY = 0;
renderer.domElement.addEventListener("pointerdown", (event) => {
dragging = true;
pointerX = event.clientX;
pointerY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.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.0055;
cameraState.phi = THREE.MathUtils.clamp(
cameraState.phi - dy * 0.0045,
0.025,
1.42
);
if (cameraState.phi > 0.08) {
planView = false;
viewButton.textContent = "Plan view";
}
updateCamera();
});
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.radius = THREE.MathUtils.clamp(
cameraState.radius * Math.exp(event.deltaY * 0.001),
12,
72
);
updateCamera();
},
{ passive: false }
);
renderer.domElement.addEventListener("dblclick", resetCamera);
viewButton.addEventListener("click", toggleView);
resetButton.addEventListener("click", resetCamera);
// ---------------------------------------------------------------------
// Architectural helpers
// ---------------------------------------------------------------------
function wallX(wall, z) {
return wall.a + wall.b * z;
}
function corridorHalfWidth(kind, z) {
if (kind === "parallel") return PARALLEL_HALF_WIDTH;
const t = (z - FAR_Z) / (NEAR_Z - FAR_Z);
return THREE.MathUtils.lerp(FAR_HALF_WIDTH, NEAR_HALF_WIDTH, t);
}
function makeWallSegment(group, wall, z1, z2, accent) {
const x1 = wallX(wall, z1);
const x2 = wallX(wall, z2);
const length = Math.hypot(x2 - x1, z2 - z1);
const direction = new THREE.Vector3(x2 - x1, 0, z2 - z1).normalize();
const rotation = new THREE.Quaternion().setFromUnitVectors(
new THREE.Vector3(0, 0, 1),
direction
);
const center = new THREE.Vector3(
(x1 + x2) * 0.5,
0,
(z1 + z2) * 0.5
);
const base = new THREE.Mesh(
new THREE.BoxGeometry(0.14, 0.32, length),
new THREE.MeshStandardMaterial({
color: COLORS.wall,
roughness: 0.72,
metalness: 0.18
})
);
base.position.set(center.x, 0.16, center.z);
base.quaternion.copy(rotation);
base.castShadow = true;
base.receiveShadow = true;
group.add(base);
const upper = new THREE.Mesh(
new THREE.BoxGeometry(0.095, 1.12, length),
new THREE.MeshStandardMaterial({
color: accent,
transparent: true,
opacity: 0.105,
roughness: 0.38,
metalness: 0.08,
depthWrite: false,
side: THREE.DoubleSide
})
);
upper.position.set(center.x, 0.88, center.z);
upper.quaternion.copy(rotation);
group.add(upper);
const cap = new THREE.Mesh(
new THREE.BoxGeometry(0.105, 0.022, length),
new THREE.MeshBasicMaterial({
color: accent,
transparent: true,
opacity: 0.52
})
);
cap.position.set(center.x, 1.45, center.z);
cap.quaternion.copy(rotation);
group.add(cap);
}
function makeBackWall(group, kind, accent) {
const halfWidth = corridorHalfWidth(kind, FAR_Z);
const base = new THREE.Mesh(
new THREE.BoxGeometry(halfWidth * 2, 0.32, 0.15),
new THREE.MeshStandardMaterial({
color: COLORS.wall,
roughness: 0.72,
metalness: 0.18
})
);
base.position.set(0, 0.16, FAR_Z);
base.receiveShadow = true;
group.add(base);
const upper = new THREE.Mesh(
new THREE.BoxGeometry(halfWidth * 2, 1.12, 0.10),
new THREE.MeshStandardMaterial({
color: accent,
transparent: true,
opacity: 0.09,
depthWrite: false,
side: THREE.DoubleSide
})
);
upper.position.set(0, 0.88, FAR_Z);
group.add(upper);
const cap = new THREE.Mesh(
new THREE.BoxGeometry(halfWidth * 2, 0.022, 0.11),
new THREE.MeshBasicMaterial({
color: accent,
transparent: true,
opacity: 0.5
})
);
cap.position.set(0, 1.45, FAR_Z);
group.add(cap);
}
function makeGrid(group, kind, accent) {
const positions = [];
// Longitudinal lines.
for (let x = -2.5; x <= 2.5001; x += 0.5) {
const absX = Math.abs(x);
let zStart = NEAR_Z;
if (kind === "angled" && absX > FAR_HALF_WIDTH) {
const t =
(absX - FAR_HALF_WIDTH) /
(NEAR_HALF_WIDTH - FAR_HALF_WIDTH);
zStart = THREE.MathUtils.lerp(FAR_Z, NEAR_Z, t);
}
if (kind === "parallel" && absX > PARALLEL_HALF_WIDTH) {
continue;
}
positions.push(x, 0.002, zStart, x, 0.002, FAR_Z);
}
// Transverse lines.
for (let z = FAR_Z; z <= NEAR_Z + 0.001; z += 0.65) {
const half = corridorHalfWidth(kind, z);
positions.push(-half, 0.002, z, half, 0.002, z);
}
const geometry = new THREE.BufferGeometry();
geometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(positions, 3)
);
const grid = new THREE.LineSegments(
geometry,
new THREE.LineBasicMaterial({
color: accent,
transparent: true,
opacity: 0.12,
depthWrite: false
})
);
group.add(grid);
}
function makeSourceBadge() {
const canvas = document.createElement("canvas");
canvas.width = 128;
canvas.height = 64;
const context = canvas.getContext("2d");
context.clearRect(0, 0, 128, 64);
context.fillStyle = "rgba(255,116,72,0.95)";
context.font = "700 35px system-ui";
context.textAlign = "center";
context.textBaseline = "middle";
context.fillText("S", 64, 34);
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
const sprite = new THREE.Sprite(
new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthTest: false
})
);
sprite.scale.set(0.42, 0.21, 1);
return sprite;
}
function makeSource(group) {
const pedestal = new THREE.Mesh(
new THREE.CylinderGeometry(0.19, 0.25, 0.22, 24),
new THREE.MeshStandardMaterial({
color: 0x1c2d38,
roughness: 0.55,
metalness: 0.5
})
);
pedestal.position.set(SOURCE.x, 0.08, SOURCE.z);
pedestal.castShadow = true;
pedestal.receiveShadow = true;
group.add(pedestal);
const stem = new THREE.Mesh(
new THREE.CylinderGeometry(0.025, 0.035, 0.49, 12),
new THREE.MeshStandardMaterial({
color: 0x61717a,
roughness: 0.4,
metalness: 0.75
})
);
stem.position.set(SOURCE.x, 0.42, SOURCE.z);
stem.castShadow = true;
group.add(stem);
const source = new THREE.Mesh(
new THREE.SphereGeometry(0.125, 24, 18),
new THREE.MeshStandardMaterial({
color: COLORS.orange,
emissive: 0xff4f25,
emissiveIntensity: 1.7,
roughness: 0.24,
metalness: 0.1
})
);
source.position.copy(SOURCE);
source.castShadow = true;
group.add(source);
const light = new THREE.PointLight(0xff7146, 2.1, 4.2, 2);
light.position.copy(SOURCE);
group.add(light);
const ringMaterial = new THREE.MeshBasicMaterial({
color: COLORS.orange,
transparent: true,
opacity: 0.55,
depthWrite: false
});
const ring = new THREE.Mesh(
new THREE.TorusGeometry(0.25, 0.012, 8, 64),
ringMaterial
);
ring.rotation.x = Math.PI / 2;
ring.position.set(SOURCE.x, 0.012, SOURCE.z);
group.add(ring);
const badge = makeSourceBadge();
badge.position.set(SOURCE.x + 0.28, 1.02, SOURCE.z);
group.add(badge);
return { ring, ringMaterial, light };
}
function makePanel(kind, worldX) {
const group = new THREE.Group();
group.position.x = worldX;
scene.add(group);
const accent = kind === "parallel" ? COLORS.cyan : COLORS.amber;
const floor = new THREE.Mesh(
new THREE.BoxGeometry(5.75, 0.18, 10.95),
new THREE.MeshStandardMaterial({
color: COLORS.floor,
roughness: 0.79,
metalness: 0.2
})
);
floor.position.set(0, -0.12, (NEAR_Z + FAR_Z) * 0.5);
floor.receiveShadow = true;
group.add(floor);
const floorEdges = new THREE.LineSegments(
new THREE.EdgesGeometry(floor.geometry),
new THREE.LineBasicMaterial({
color: accent,
transparent: true,
opacity: 0.24
})
);
floorEdges.position.copy(floor.position);
group.add(floorEdges);
makeGrid(group, kind, accent);
const walls = makeWallDefinitions(kind);
makeWallSegment(group, walls.left, NEAR_Z, FAR_Z, accent);
makeWallSegment(group, walls.right, NEAR_Z, FAR_Z, accent);
makeBackWall(group, kind, accent);
const frontWidth = corridorHalfWidth(kind, NEAR_Z) * 2;
const frontEdge = new THREE.Mesh(
new THREE.BoxGeometry(frontWidth, 0.025, 0.04),
new THREE.MeshBasicMaterial({
color: accent,
transparent: true,
opacity: 0.7
})
);
frontEdge.position.set(0, 0.012, NEAR_Z + 0.025);
group.add(frontEdge);
const receiver = new THREE.Mesh(
new THREE.BoxGeometry(corridorHalfWidth(kind, FAR_Z) * 2, 0.022, 0.035),
new THREE.MeshBasicMaterial({
color: accent,
transparent: true,
opacity: 0.72
})
);
receiver.position.set(0, SOURCE_Y, FAR_Z + 0.085);
group.add(receiver);
const source = makeSource(group);
const rayRoot = new THREE.Group();
rayRoot.renderOrder = 3;
group.add(rayRoot);
return {
kind,
group,
walls,
rayRoot,
source,
paths: []
};
}
// ---------------------------------------------------------------------
// Wall definitions and specular ray tracing
// ---------------------------------------------------------------------
function makeWallDefinitions(kind) {
if (kind === "parallel") {
return {
left: {
a: -PARALLEL_HALF_WIDTH,
b: 0,
normal: new THREE.Vector3(-1, 0, 0)
},
right: {
a: PARALLEL_HALF_WIDTH,
b: 0,
normal: new THREE.Vector3(1, 0, 0)
}
};
}
// Each wall tilts approximately 2.1 degrees. The corridor narrows
// from the source end toward the far termination plane.
const b = (NEAR_HALF_WIDTH - FAR_HALF_WIDTH) / (NEAR_Z - FAR_Z);
const a = NEAR_HALF_WIDTH - b * NEAR_Z;
return {
left: {
a: -a,
b: -b,
normal: new THREE.Vector3(-1, 0, b).normalize()
},
right: {
a,
b,
normal: new THREE.Vector3(1, 0, -b).normalize()
}
};
}
function intersectWall(position, direction, wall) {
const denominator = direction.x - wall.b * direction.z;
if (Math.abs(denominator) < 1e-9) return null;
const t =
(wall.a + wall.b * position.z - position.x) / denominator;
if (t <= 1e-5) return null;
const z = position.z + direction.z * t;
if (z < FAR_Z - 1e-5 || z > NEAR_Z + 1e-5) {
return null;
}
return {
t,
z,
x: wallX(wall, z)
};
}
function traceRay(start, initialDirection, walls, maxBounces = 18) {
const points = [start.clone()];
const hits = [];
let position = start.clone();
let direction = initialDirection.clone().normalize();
for (let bounce = 0; bounce < maxBounces; bounce++) {
const leftHit = intersectWall(position, direction, walls.left);
const rightHit = intersectWall(position, direction, walls.right);
const candidates = [];
if (leftHit) candidates.push({ hit: leftHit, wall: walls.left });
if (rightHit) candidates.push({ hit: rightHit, wall: walls.right });
candidates.sort((a, b) => a.hit.t - b.hit.t);
const farT =
direction.z < -1e-8
? (FAR_Z - position.z) / direction.z
: Infinity;
const nextWall = candidates[0];
if (!nextWall || nextWall.hit.t > farT) {
if (Number.isFinite(farT) && farT > 0) {
const end = position.clone().addScaledVector(direction, farT);
end.y = SOURCE_Y;
points.push(end);
}
break;
}
const contact = new THREE.Vector3(
nextWall.hit.x,
SOURCE_Y,
nextWall.hit.z
);
points.push(contact);
hits.push({
point: contact.clone(),
wall: nextWall.wall
});
position.copy(contact);
direction.reflect(nextWall.wall.normal).normalize();
}
const cumulative = [0];
let length = 0;
for (let i = 1; i < points.length; i++) {
length += points[i].distanceTo(points[i - 1]);
cumulative.push(length);
}
return {
points,
cumulative,
hits,
length
};
}
function disposeGroup(group) {
const geometries = new Set();
const materials = new Set();
group.traverse((object) => {
if (object.geometry) geometries.add(object.geometry);
if (object.material) {
const list = Array.isArray(object.material)
? object.material
: [object.material];
list.forEach((material) => materials.add(material));
}
});
geometries.forEach((geometry) => geometry.dispose());
materials.forEach((material) => material.dispose());
group.clear();
}
function addRaySegment(parent, a, b, material, radius) {
const direction = b.clone().sub(a);
const length = direction.length();
if (length < 1e-5) return;
const mesh = new THREE.Mesh(
new THREE.CylinderGeometry(radius, radius, length, 7, 1, false),
material
);
mesh.position.copy(a).add(b).multiplyScalar(0.5);
mesh.quaternion.setFromUnitVectors(
new THREE.Vector3(0, 1, 0),
direction.normalize()
);
mesh.renderOrder = 4;
parent.add(mesh);
}
function addBounceMarker(parent, hit, material) {
const marker = new THREE.Mesh(
new THREE.CircleGeometry(0.055, 16),
material
);
marker.position
.copy(hit.point)
.addScaledVector(hit.wall.normal, 0.013);
marker.quaternion.setFromUnitVectors(
new THREE.Vector3(0, 0, 1),
hit.wall.normal
);
marker.renderOrder = 5;
parent.add(marker);
}
function buildRays(panel) {
disposeGroup(panel.rayRoot);
panel.paths = [];
const halfAngle = Number(angleInput.value);
const factors = [-1, -0.78, -0.56, -0.34, 0.34, 0.56, 0.78, 1];
factors.forEach((factor, index) => {
const angle = THREE.MathUtils.degToRad(halfAngle * factor);
const direction = new THREE.Vector3(
Math.sin(angle),
0,
-Math.cos(angle)
);
const path = traceRay(SOURCE, direction, panel.walls);
const principal = Math.abs(Math.abs(factor) - 1) < 0.01;
const strong = Math.abs(factor) >= 0.55;
const color = principal ? COLORS.ray : COLORS.raySecondary;
const opacity = principal ? 0.96 : strong ? 0.58 : 0.24;
const radius = principal ? 0.021 : strong ? 0.014 : 0.009;
const material = new THREE.MeshBasicMaterial({
color,
transparent: true,
opacity,
blending: THREE.AdditiveBlending,
depthWrite: false
});
const bounceMaterial = new THREE.MeshBasicMaterial({
color: principal ? 0xe8feff : 0x9eeaf2,
transparent: true,
opacity: principal ? 0.95 : 0.5,
side: THREE.DoubleSide,
blending: THREE.AdditiveBlending,
depthWrite: false
});
for (let i = 1; i < path.points.length; i++) {
addRaySegment(
panel.rayRoot,
path.points[i - 1],
path.points[i],
material,
radius
);
}
path.hits.forEach((hit) => {
addBounceMarker(panel.rayRoot, hit, bounceMaterial);
});
const pulse = new THREE.Mesh(
new THREE.SphereGeometry(principal ? 0.072 : 0.052, 12, 9),
new THREE.MeshBasicMaterial({
color: principal ? 0xffffff : 0xa8f9ff,
transparent: true,
opacity: principal ? 1 : 0.72,
blending: THREE.AdditiveBlending,
depthWrite: false
})
);
pulse.renderOrder = 6;
panel.rayRoot.add(pulse);
path.pulse = pulse;
path.offset = index * 0.83;
path.principal = principal;
panel.paths.push(path);
});
}
function pointAlongPath(path, distance) {
const d = ((distance % path.length) + path.length) % path.length;
for (let i = 1; i < path.points.length; i++) {
if (d <= path.cumulative[i]) {
const segmentLength =
path.cumulative[i] - path.cumulative[i - 1];
const t =
(d - path.cumulative[i - 1]) / Math.max(segmentLength, 1e-6);
return path.points[i - 1].clone().lerp(path.points[i], t);
}
}
return path.points[path.points.length - 1].clone();
}
function updateContactCounts() {
const parallel = parallelPanel.paths[0];
const angled = angledPanel.paths[0];
parallelContacts.textContent = parallel.hits.length;
angledContacts.textContent = angled.hits.length;
}
const parallelPanel = makePanel("parallel", -6.7);
const angledPanel = makePanel("angled", 6.7);
buildRays(parallelPanel);
buildRays(angledPanel);
updateContactCounts();
angleInput.addEventListener("input", () => {
angleValue.textContent = `${angleInput.value}°`;
buildRays(parallelPanel);
buildRays(angledPanel);
updateContactCounts();
});
// ---------------------------------------------------------------------
// Animation
// ---------------------------------------------------------------------
const clock = new THREE.Clock();
let elapsed = 0;
let paused = false;
pauseButton.addEventListener("click", () => {
paused = !paused;
pauseButton.textContent = paused ? "Resume rays" : "Pause rays";
});
function animate() {
requestAnimationFrame(animate);
const delta = Math.min(clock.getDelta(), 0.05);
if (!paused) elapsed += delta;
[parallelPanel, angledPanel].forEach((panel, panelIndex) => {
const phase = (elapsed * 0.72 + panelIndex * 0.21) % 1;
const scale = 1 + phase * 1.75;
panel.source.ring.scale.setScalar(scale);
panel.source.ringMaterial.opacity = (1 - phase) * 0.5;
panel.source.light.intensity = 1.8 + (1 - phase) * 0.9;
panel.paths.forEach((path) => {
const travel = elapsed * 3.65 + path.offset;
path.pulse.position.copy(pointAlongPath(path, travel));
const pulsePhase = (travel / path.length) % 1;
const pulseScale =
0.88 + Math.sin(pulsePhase * Math.PI) * 0.32;
path.pulse.scale.setScalar(pulseScale);
});
});
renderer.render(scene, camera);
}
// ---------------------------------------------------------------------
// Resize
// ---------------------------------------------------------------------
function onResize() {
camera.aspect = innerWidth / innerHeight;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
renderer.setSize(innerWidth, innerHeight);
if (!dragging) {
cameraState.radius = recommendedRadius() * (planView ? 1.04 : 1);
updateCamera();
}
}
window.addEventListener("resize", onResize);
resetCamera();
animate();
</script>
</body>
</html>
```