Prompt specification
Build or customize the Three.js 3D scene "Reading alcove reflection demo" 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: Reading alcove reflection demo
Category: Architecture & Cities · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/reading-alcove-reflection-demo-x0162
Scene Description Prompt:
Create a single self-contained HTML file using Three.js from cdnjs that shows room acoustics. Create a tiny alcove with interchangeable panels and a pulsing sound marker; show fewer reflected paths when absorbing surfaces are selected in this qualitative model. Low-poly.
Working Three.js Starter (reading-alcove-reflection-demo-x0162):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Alcove — Room Acoustics</title>
<style>
:root {
--paper: #ece8de;
--ink: #173238;
--muted: #6f7d79;
--line: rgba(23, 50, 56, 0.14);
--accent: #f16d4d;
--teal: #287c78;
--gold: #d7a343;
--panel: rgba(249, 247, 240, 0.88);
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: var(--paper);
color: var(--ink);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
button,
select,
input {
font: inherit;
}
#scene {
position: fixed;
inset: 0;
width: 100%;
height: 100%;
display: block;
touch-action: none;
outline: none;
}
.brand {
position: fixed;
top: 25px;
left: 28px;
z-index: 5;
pointer-events: none;
user-select: none;
}
.brand-kicker {
display: flex;
align-items: center;
gap: 9px;
margin-bottom: 6px;
color: var(--muted);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.19em;
text-transform: uppercase;
}
.brand-kicker::before {
content: "";
width: 19px;
height: 2px;
background: var(--accent);
}
.brand h1 {
margin: 0;
font-size: clamp(24px, 3vw, 40px);
font-weight: 780;
letter-spacing: -0.055em;
line-height: 0.95;
}
.brand p {
max-width: 270px;
margin: 9px 0 0;
color: var(--muted);
font-size: 12px;
line-height: 1.45;
}
.sidebar {
position: fixed;
top: 22px;
right: 22px;
bottom: 22px;
z-index: 7;
width: 326px;
padding: 22px;
overflow: auto;
border: 1px solid rgba(255, 255, 255, 0.75);
border-radius: 18px;
background: var(--panel);
box-shadow:
0 18px 50px rgba(28, 45, 46, 0.12),
inset 0 0 0 1px rgba(23, 50, 56, 0.04);
backdrop-filter: blur(16px);
}
.sidebar-head {
display: flex;
align-items: flex-start;
justify-content: space-between;
gap: 12px;
margin-bottom: 20px;
}
.eyebrow {
margin: 0 0 5px;
color: var(--muted);
font-size: 9px;
font-weight: 800;
letter-spacing: 0.17em;
text-transform: uppercase;
}
.sidebar h2 {
margin: 0;
font-size: 20px;
font-weight: 760;
letter-spacing: -0.035em;
}
.room-tag {
flex: 0 0 auto;
padding: 6px 8px;
border-radius: 7px;
background: var(--ink);
color: #f8f4e8;
font-size: 9px;
font-weight: 800;
letter-spacing: 0.12em;
}
.intro {
margin: 0 0 20px;
color: var(--muted);
font-size: 12px;
line-height: 1.55;
}
.section-title {
display: flex;
align-items: center;
justify-content: space-between;
margin: 19px 0 10px;
padding-top: 16px;
border-top: 1px solid var(--line);
color: var(--muted);
font-size: 9px;
font-weight: 800;
letter-spacing: 0.16em;
text-transform: uppercase;
}
.surface-card {
margin-bottom: 9px;
padding: 11px;
border: 1px solid var(--line);
border-radius: 11px;
background: rgba(255, 255, 255, 0.38);
transition: border-color 160ms ease, background 160ms ease;
}
.surface-card:hover {
border-color: rgba(23, 50, 56, 0.28);
background: rgba(255, 255, 255, 0.65);
}
.surface-meta {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 8px;
}
.surface-name {
display: flex;
align-items: center;
gap: 8px;
font-size: 11px;
font-weight: 760;
}
.surface-dot {
width: 8px;
height: 8px;
border-radius: 50%;
background: var(--teal);
box-shadow: 0 0 0 3px rgba(40, 124, 120, 0.1);
}
.absorption {
color: var(--muted);
font-size: 9px;
font-weight: 750;
letter-spacing: 0.08em;
}
.select-wrap {
position: relative;
}
.select-wrap::after {
content: "";
position: absolute;
top: 50%;
right: 11px;
width: 6px;
height: 6px;
border-right: 1.5px solid var(--ink);
border-bottom: 1.5px solid var(--ink);
transform: translateY(-65%) rotate(45deg);
pointer-events: none;
}
select {
width: 100%;
height: 35px;
padding: 0 31px 0 10px;
appearance: none;
border: 1px solid var(--line);
border-radius: 8px;
outline: none;
background: rgba(255, 255, 255, 0.72);
color: var(--ink);
font-size: 11px;
font-weight: 650;
cursor: pointer;
}
select:focus {
border-color: var(--teal);
box-shadow: 0 0 0 3px rgba(40, 124, 120, 0.1);
}
.preset-row {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 8px;
margin-top: 11px;
}
button {
min-height: 36px;
padding: 0 12px;
border: 1px solid var(--line);
border-radius: 9px;
background: transparent;
color: var(--ink);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.07em;
text-transform: uppercase;
cursor: pointer;
transition: transform 140ms ease, background 140ms ease, color 140ms ease;
}
button:hover {
transform: translateY(-1px);
background: rgba(255, 255, 255, 0.8);
}
button.primary {
border-color: var(--ink);
background: var(--ink);
color: #fffaf0;
}
button.primary:hover {
background: #24464b;
}
.switch-row {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
padding: 11px 0;
border-top: 1px solid var(--line);
border-bottom: 1px solid var(--line);
cursor: pointer;
}
.switch-copy strong {
display: block;
margin-bottom: 3px;
font-size: 11px;
}
.switch-copy span {
color: var(--muted);
font-size: 9px;
}
.switch {
position: relative;
flex: 0 0 auto;
width: 37px;
height: 21px;
}
.switch input {
position: absolute;
opacity: 0;
pointer-events: none;
}
.switch-track {
position: absolute;
inset: 0;
border-radius: 20px;
background: #c8c9c3;
transition: background 160ms ease;
}
.switch-track::after {
content: "";
position: absolute;
top: 3px;
left: 3px;
width: 15px;
height: 15px;
border-radius: 50%;
background: white;
box-shadow: 0 1px 4px rgba(0, 0, 0, 0.22);
transition: transform 160ms ease;
}
.switch input:checked + .switch-track {
background: var(--teal);
}
.switch input:checked + .switch-track::after {
transform: translateX(16px);
}
.model-note {
margin: 15px 0 0;
color: var(--muted);
font-size: 9px;
line-height: 1.5;
}
.stats {
position: fixed;
left: 28px;
bottom: 27px;
z-index: 5;
display: grid;
grid-template-columns: repeat(3, minmax(84px, auto));
gap: 0;
overflow: hidden;
border: 1px solid rgba(255, 255, 255, 0.72);
border-radius: 13px;
background: rgba(249, 247, 240, 0.83);
box-shadow: 0 10px 30px rgba(28, 45, 46, 0.08);
backdrop-filter: blur(12px);
pointer-events: none;
}
.stat {
min-width: 105px;
padding: 12px 15px;
border-right: 1px solid var(--line);
}
.stat:last-child {
border-right: 0;
}
.stat-label {
margin-bottom: 4px;
color: var(--muted);
font-size: 8px;
font-weight: 800;
letter-spacing: 0.13em;
text-transform: uppercase;
}
.stat-value {
font-size: 21px;
font-weight: 780;
letter-spacing: -0.04em;
}
.stat-value small {
color: var(--muted);
font-size: 10px;
font-weight: 700;
letter-spacing: 0;
}
.controls-hint {
position: fixed;
left: 50%;
bottom: 29px;
z-index: 4;
transform: translateX(-50%);
color: var(--muted);
font-size: 9px;
font-weight: 750;
letter-spacing: 0.1em;
text-transform: uppercase;
white-space: nowrap;
pointer-events: none;
}
.controls-hint span {
margin: 0 7px;
color: var(--accent);
}
.source-label,
.listener-label {
position: fixed;
z-index: 4;
transform: translate(-50%, -100%);
pointer-events: none;
user-select: none;
white-space: nowrap;
}
.source-label {
padding: 6px 8px;
border-radius: 6px;
background: var(--accent);
color: white;
font-size: 8px;
font-weight: 850;
letter-spacing: 0.11em;
text-transform: uppercase;
box-shadow: 0 5px 15px rgba(241, 109, 77, 0.24);
}
.source-label::after {
content: "";
position: absolute;
top: 100%;
left: 50%;
width: 1px;
height: 15px;
background: var(--accent);
}
.listener-label {
top: -100%;
color: var(--ink);
font-size: 8px;
font-weight: 850;
letter-spacing: 0.11em;
text-transform: uppercase;
}
@media (max-width: 900px) {
.brand p,
.controls-hint {
display: none;
}
.sidebar {
top: auto;
left: 14px;
right: 14px;
bottom: 14px;
width: auto;
max-height: 44vh;
padding: 16px;
}
.sidebar .intro,
.model-note {
display: none;
}
.stats {
left: 14px;
bottom: calc(44vh + 27px);
}
.stat {
min-width: 82px;
padding: 9px 11px;
}
.stat-value {
font-size: 17px;
}
.surface-card {
margin-bottom: 7px;
}
}
@media (max-width: 560px) {
.brand {
top: 16px;
left: 16px;
}
.brand h1 {
font-size: 25px;
}
.stats {
display: none;
}
.sidebar {
max-height: 48vh;
}
}
</style>
</head>
<body>
<canvas id="scene" aria-label="Interactive 3D acoustic alcove"></canvas>
<header class="brand">
<div class="brand-kicker">Qualitative acoustics lab</div>
<h1>Alcove / 04</h1>
<p>A tiny room for comparing surface treatments and early reflected paths.</p>
</header>
<aside class="sidebar">
<div class="sidebar-head">
<div>
<p class="eyebrow">Surface library</p>
<h2>Interchangeable panels</h2>
</div>
<div class="room-tag">8 × 5 M</div>
</div>
<p class="intro">
Choose a finish for each surface. More absorptive finishes remove energy from
reflected paths, so fewer and shorter paths remain visible.
</p>
<div class="section-title">
<span>Room surfaces</span>
<span>Absorption</span>
</div>
<div class="surface-card">
<div class="surface-meta">
<div class="surface-name">
<span class="surface-dot"></span>
Rear wall
</div>
<span class="absorption" id="abs-back">06%</span>
</div>
<div class="select-wrap">
<select id="surface-back" aria-label="Rear wall treatment">
<option value="bare">Bare plaster</option>
<option value="felt">Acoustic felt</option>
<option value="foam">Foam wedges</option>
<option value="diffuser">Wood diffuser</option>
</select>
</div>
</div>
<div class="surface-card">
<div class="surface-meta">
<div class="surface-name">
<span class="surface-dot"></span>
Left wall
</div>
<span class="absorption" id="abs-left">06%</span>
</div>
<div class="select-wrap">
<select id="surface-left" aria-label="Left wall treatment">
<option value="bare">Bare plaster</option>
<option value="felt">Acoustic felt</option>
<option value="foam">Foam wedges</option>
<option value="diffuser">Wood diffuser</option>
</select>
</div>
</div>
<div class="surface-card">
<div class="surface-meta">
<div class="surface-name">
<span class="surface-dot"></span>
Floor
</div>
<span class="absorption" id="abs-floor">06%</span>
</div>
<div class="select-wrap">
<select id="surface-floor" aria-label="Floor treatment">
<option value="bare">Bare floor</option>
<option value="felt">Acoustic felt</option>
<option value="foam">Foam wedges</option>
<option value="diffuser">Wood diffuser</option>
</select>
</div>
</div>
<div class="preset-row">
<button class="primary" id="absorb-all" type="button">Absorb all</button>
<button id="reset-surfaces" type="button">Bare room</button>
</div>
<div class="section-title">
<span>Visualization</span>
<span>Live</span>
</div>
<label class="switch-row" for="show-paths">
<span class="switch-copy">
<strong>Show reflected paths</strong>
<span>Color indicates the first surface struck</span>
</span>
<span class="switch">
<input id="show-paths" type="checkbox" checked>
<span class="switch-track"></span>
</span>
</label>
<div class="preset-row">
<button id="reset-view" type="button">Reset camera</button>
</div>
<p class="model-note">
Educational model only. Absorption values and ray behavior are illustrative,
not a substitute for an acoustic solver or measured material data.
</p>
</aside>
<section class="stats" aria-live="polite">
<div class="stat">
<div class="stat-label">Visible paths</div>
<div class="stat-value" id="path-count">—</div>
</div>
<div class="stat">
<div class="stat-label">Reflected segments</div>
<div class="stat-value" id="segment-count">—</div>
</div>
<div class="stat">
<div class="stat-label">Room absorption</div>
<div class="stat-value"><span id="room-absorption">6</span><small>%</small></div>
</div>
</section>
<div class="controls-hint">
Drag to orbit <span>•</span> Scroll to zoom <span>•</span> Click a panel to cycle
</div>
<div class="source-label" id="source-label">Source · 440 Hz</div>
<div class="listener-label" id="listener-label">Listener</div>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const canvas = document.querySelector("#scene");
const renderer = new THREE.WebGLRenderer({
canvas,
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.05;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xece8de);
scene.fog = new THREE.Fog(0xece8de, 18, 35);
const camera = new THREE.PerspectiveCamera(
36,
window.innerWidth / window.innerHeight,
0.1,
80
);
const controls = {
target: new THREE.Vector3(0, 1.55, -0.15),
theta: 0.62,
phi: 1.04,
radius: 11.7
};
function updateCamera() {
const sinPhi = Math.sin(controls.phi);
camera.position.set(
controls.target.x + controls.radius * sinPhi * Math.sin(controls.theta),
controls.target.y + controls.radius * Math.cos(controls.phi),
controls.target.z + controls.radius * sinPhi * Math.cos(controls.theta)
);
camera.lookAt(controls.target);
}
updateCamera();
const hemisphere = new THREE.HemisphereLight(0xfff9e9, 0x71817b, 2.25);
scene.add(hemisphere);
const sun = new THREE.DirectionalLight(0xfff4dc, 3.1);
sun.position.set(-5, 10, 7);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -8;
sun.shadow.camera.right = 8;
sun.shadow.camera.top = 8;
sun.shadow.camera.bottom = -8;
sun.shadow.normalBias = 0.035;
scene.add(sun);
const fill = new THREE.DirectionalLight(0x9ec6d0, 1.25);
fill.position.set(7, 4, -5);
scene.add(fill);
const room = new THREE.Group();
const panelGroups = {
back: new THREE.Group(),
left: new THREE.Group(),
floor: new THREE.Group()
};
const pathsGroup = new THREE.Group();
const interactiveMeshes = [];
scene.add(room, panelGroups.back, panelGroups.left, panelGroups.floor, pathsGroup);
function standard(color, extra = {}) {
return new THREE.MeshStandardMaterial({
color,
roughness: 0.84,
metalness: 0,
flatShading: true,
...extra
});
}
function box(parent, size, position, material, rotation = null) {
const mesh = new THREE.Mesh(new THREE.BoxGeometry(...size), material);
mesh.position.set(...position);
if (rotation) mesh.rotation.set(...rotation);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
// Architectural shell.
const plinthMaterial = standard(0x24464a);
box(room, [8.55, 0.22, 5.85], [0, -0.25, 0], plinthMaterial);
const floorMaterial = standard(0xc8bba7);
box(room, [7.8, 0.18, 5.3], [0, -0.09, 0], floorMaterial);
const wallMaterial = standard(0xddd5c6);
const backWall = box(room, [7.8, 4.8, 0.18], [0, 2.4, -2.65], wallMaterial);
const leftWall = box(room, [0.18, 4.8, 5.3], [-3.9, 2.4, 0], wallMaterial);
const frameMaterial = standard(0x466267);
box(room, [8.0, 0.14, 0.14], [0, 4.77, 2.58], frameMaterial);
box(room, [0.14, 4.8, 0.14], [-3.89, 2.4, 2.58], frameMaterial);
box(room, [0.14, 4.8, 0.14], [3.89, 2.4, 2.58], frameMaterial);
// Subtle floor grid.
const gridVertices = [];
for (let x = -3.6; x <= 3.61; x += 0.6) {
gridVertices.push(x, 0.012, -2.48, x, 0.012, 2.48);
}
for (let z = -2.4; z <= 2.41; z += 0.6) {
gridVertices.push(-3.78, 0.012, z, 3.78, 0.012, z);
}
const gridGeometry = new THREE.BufferGeometry();
gridGeometry.setAttribute(
"position",
new THREE.Float32BufferAttribute(gridVertices, 3)
);
const grid = new THREE.LineSegments(
gridGeometry,
new THREE.LineBasicMaterial({
color: 0x6c7771,
transparent: true,
opacity: 0.13
})
);
room.add(grid);
const ground = new THREE.Mesh(
new THREE.PlaneGeometry(60, 60),
standard(0xece8de, { roughness: 1 })
);
ground.rotation.x = -Math.PI / 2;
ground.position.y = -0.37;
ground.receiveShadow = true;
scene.add(ground);
// Invisible interaction planes.
function addPicker(surface, size, position, rotation) {
const material = new THREE.MeshBasicMaterial({
transparent: true,
opacity: 0,
depthWrite: false,
side: THREE.DoubleSide
});
const mesh = new THREE.Mesh(new THREE.PlaneGeometry(...size), material);
mesh.position.set(...position);
mesh.rotation.set(...rotation);
mesh.userData.surface = surface;
scene.add(mesh);
interactiveMeshes.push(mesh);
}
addPicker("back", [7.55, 4.55], [0, 2.35, -2.54], [0, 0, 0]);
addPicker("left", [5.05, 4.55], [-3.79, 2.35, 0], [0, Math.PI / 2, 0]);
addPicker("floor", [7.55, 5.05], [0, 0.025, 0], [-Math.PI / 2, 0, 0]);
const treatments = {
bare: {
label: "Bare plaster",
absorption: 0.06,
color: 0xc7bba8
},
felt: {
label: "Acoustic felt",
absorption: 0.58,
color: 0x287c78
},
foam: {
label: "Foam wedges",
absorption: 0.82,
color: 0xf16d4d
},
diffuser: {
label: "Wood diffuser",
absorption: 0.22,
color: 0xc18a4b
}
};
const state = {
back: "bare",
left: "bare",
floor: "bare"
};
function clearGroup(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 addFoam(parent, surface) {
const geometry = new THREE.ConeGeometry(0.17, 0.25, 4, 1, false);
const material = standard(treatments.foam.color, {
roughness: 0.98,
side: THREE.DoubleSide
});
const dummy = new THREE.Object3D();
const transforms = [];
if (surface === "back") {
geometry.rotateX(Math.PI / 2);
for (let ix = 0; ix < 9; ix++) {
for (let iy = 0; iy < 7; iy++) {
transforms.push([
-3.28 + ix * 0.82,
0.48 + iy * 0.6,
-2.35
]);
}
}
} else if (surface === "left") {
geometry.rotateZ(-Math.PI / 2);
for (let iz = 0; iz < 9; iz++) {
for (let iy = 0; iy < 7; iy++) {
transforms.push([
-3.63,
0.48 + iy * 0.6,
-2.28 + iz * 0.57
]);
}
}
} else {
for (let ix = 0; ix < 9; ix++) {
for (let iz = 0; iz < 7; iz++) {
transforms.push([
-3.28 + ix * 0.82,
0.13,
-2.28 + iz * 0.76
]);
}
}
}
const mesh = new THREE.InstancedMesh(geometry, material, transforms.length);
transforms.forEach((position, index) => {
dummy.position.set(...position);
dummy.updateMatrix();
mesh.setMatrixAt(index, dummy.matrix);
});
mesh.instanceMatrix.needsUpdate = true;
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
}
function addFelt(parent, surface) {
const material = standard(treatments.felt.color, {
roughness: 1,
side: THREE.DoubleSide
});
if (surface === "back") {
const xs = [-2.82, -0.94, 0.94, 2.82];
const ys = [0.72, 2.22, 3.72];
xs.forEach((x) => {
ys.forEach((y) => {
box(parent, [1.68, 1.28, 0.1], [x, y, -2.49], material);
});
});
} else if (surface === "left") {
const zs = [-2.1, -0.7, 0.7, 2.1];
const ys = [0.72, 2.22, 3.72];
zs.forEach((z) => {
ys.forEach((y) => {
box(parent, [0.1, 1.28, 1.18], [-3.73, y, z], material);
});
});
} else {
const xs = [-2.85, -0.95, 0.95, 2.85];
const zs = [-1.75, 0, 1.75];
xs.forEach((x) => {
zs.forEach((z) => {
box(parent, [1.68, 0.07, 1.48], [x, 0.035, z], material);
});
});
}
}
function addDiffuser(parent, surface) {
const woodA = standard(0xc28b4b, { roughness: 0.7 });
const woodB = standard(0x9f693d, { roughness: 0.76 });
if (surface === "back") {
for (let i = 0; i < 15; i++) {
const x = -3.48 + i * 0.495;
box(
parent,
[0.14, 4.25, 0.2],
[x, 2.37, -2.43],
i % 3 === 0 ? woodB : woodA
);
}
} else if (surface === "left") {
for (let i = 0; i < 15; i++) {
const z = -2.4 + i * 0.343;
box(
parent,
[0.2, 4.25, 0.14],
[-3.64, 2.37, z],
i % 3 === 0 ? woodB : woodA
);
}
} else {
for (let i = 0; i < 16; i++) {
const z = -2.38 + i * 0.317;
box(
parent,
[7.35, 0.075, 0.16],
[0, 0.04, z],
i % 3 === 0 ? woodB : woodA
);
}
}
}
function rebuildPanel(surface) {
const group = panelGroups[surface];
clearGroup(group);
const type = state[surface];
if (type === "felt") addFelt(group, surface);
if (type === "foam") addFoam(group, surface);
if (type === "diffuser") addDiffuser(group, surface);
}
// Source marker.
const sourcePosition = new THREE.Vector3(0, 1.2, 1.35);
const sourceGroup = new THREE.Group();
sourceGroup.position.copy(sourcePosition);
scene.add(sourceGroup);
const sourceCore = new THREE.Mesh(
new THREE.IcosahedronGeometry(0.15, 1),
standard(0xf16d4d, {
emissive: 0xf16d4d,
emissiveIntensity: 1.3,
roughness: 0.55
})
);
sourceCore.castShadow = true;
sourceGroup.add(sourceCore);
const pulseRings = [];
for (let i = 0; i < 3; i++) {
const material = new THREE.MeshBasicMaterial({
color: 0xf16d4d,
transparent: true,
opacity: 0.4,
depthWrite: false
});
const ring = new THREE.Mesh(
new THREE.TorusGeometry(0.23 + i * 0.025, 0.012, 4, 28),
material
);
ring.rotation.x = Math.PI / 2;
ring.userData.phase = i * 0.8;
pulseRings.push(ring);
sourceGroup.add(ring);
}
// Listener / receiver.
const listenerPosition = new THREE.Vector3(0.65, 1.08, 0.55);
const listenerGroup = new THREE.Group();
listenerGroup.position.copy(listenerPosition);
scene.add(listenerGroup);
const listenerMaterial = standard(0x173238);
const head = new THREE.Mesh(
new THREE.IcosahedronGeometry(0.16, 1),
listenerMaterial
);
head.castShadow = true;
listenerGroup.add(head);
const stem = new THREE.Mesh(
new THREE.CylinderGeometry(0.025, 0.025, 0.95, 6),
listenerMaterial
);
stem.position.y = -0.55;
listenerGroup.add(stem);
const base = new THREE.Mesh(
new THREE.CylinderGeometry(0.18, 0.22, 0.07, 8),
listenerMaterial
);
base.position.y = -1.02;
listenerGroup.add(base);
const listenerRing = new THREE.Mesh(
new THREE.TorusGeometry(0.3, 0.012, 4, 32),
new THREE.MeshBasicMaterial({ color: 0xd7a343 })
);
listenerRing.rotation.x = Math.PI / 2;
listenerRing.position.y = -1.01;
listenerGroup.add(listenerRing);
// Direct sound path.
const directGeometry = new THREE.BufferGeometry().setFromPoints([
sourcePosition,
listenerPosition
]);
const directMaterial = new THREE.LineDashedMaterial({
color: 0x2b8a78,
dashSize: 0.13,
gapSize: 0.08,
transparent: true,
opacity: 0.85,
depthWrite: false
});
const directPath = new THREE.Line(directGeometry, directMaterial);
directPath.computeLineDistances();
pathsGroup.add(directPath);
const bounds = {
xMin: -3.78,
xMax: 3.78,
yMin: 0.08,
yMax: 4.72,
zMin: -2.52,
zMax: 2.6
};
const boundaryPlanes = [
{ axis: "z", value: bounds.zMin, surface: "back" },
{ axis: "x", value: bounds.xMin, surface: "left" },
{ axis: "x", value: bounds.xMax, surface: "right" },
{ axis: "y", value: bounds.yMin, surface: "floor" },
{ axis: "y", value: bounds.yMax, surface: "ceiling" },
{ axis: "z", value: bounds.zMax, surface: "front" }
];
function nearestIntersection(origin, direction) {
let nearest = null;
boundaryPlanes.forEach((plane) => {
const axis = plane.axis;
const component = direction[axis];
if (Math.abs(component) < 1e-7) return;
const distance = (plane.value - origin[axis]) / component;
if (distance <= 0.0001) return;
const point = origin.clone().addScaledVector(direction, distance);
const epsilon = 0.001;
let valid = false;
if (axis === "x") {
valid =
point.y >= bounds.yMin - epsilon &&
point.y <= bounds.yMax + epsilon &&
point.z >= bounds.zMin - epsilon &&
point.z <= bounds.zMax + epsilon;
} else if (axis === "y") {
valid =
point.x >= bounds.xMin - epsilon &&
point.x <= bounds.xMax + epsilon &&
point.z >= bounds.zMin - epsilon &&
point.z <= bounds.zMax + epsilon;
} else {
valid =
point.x >= bounds.xMin - epsilon &&
point.x <= bounds.xMax + epsilon &&
point.y >= bounds.yMin - epsilon &&
point.y <= bounds.yMax + epsilon;
}
if (valid && (!nearest || distance < nearest.distance)) {
nearest = {
distance,
point,
surface: plane.surface,
axis
};
}
});
return nearest;
}
function reflectivity(surface) {
if (surface in state) {
return 1 - treatments[state[surface]].absorption;
}
return 0.94;
}
const visibilityThreshold = 0.27;
function traceRay(target) {
const direction = target.clone().sub(sourcePosition).normalize();
let origin = sourcePosition.clone();
let energy = 1;
let firstSurface = null;
let firstReflectionEnergy = 1;
const points = [origin.clone()];
const energies = [];
for (let bounce = 0; bounce < 4; bounce++) {
const hit = nearestIntersection(origin, direction);
if (!hit) break;
points.push(hit.point.clone());
energies.push(energy);
if (hit.surface === "front") {
break;
}
if (!firstSurface) {
firstSurface = hit.surface;
firstReflectionEnergy = energy * reflectivity(hit.surface);
if (firstReflectionEnergy < visibilityThreshold) {
return null;
}
}
const reflectedEnergy = energy * reflectivity(hit.surface);
if (reflectedEnergy < visibilityThreshold) break;
direction[hit.axis] *= -1;
origin = hit.point.clone().addScaledVector(direction, 0.002);
energy = reflectedEnergy;
}
return {
points,
energies,
firstSurface,
reflectedSegments: Math.max(0, points.length - 2)
};
}
const rayTargets = [
// Rear wall.
new THREE.Vector3(-3.2, 0.9, bounds.zMin),
new THREE.Vector3(-1.8, 2.2, bounds.zMin),
new THREE.Vector3(-0.4, 3.6, bounds.zMin),
new THREE.Vector3(1.0, 1.2, bounds.zMin),
new THREE.Vector3(2.6, 2.8, bounds.zMin),
new THREE.Vector3(3.2, 3.9, bounds.zMin),
// Left wall.
new THREE.Vector3(bounds.xMin, 0.8, -2.2),
new THREE.Vector3(bounds.xMin, 2.1, -1.2),
new THREE.Vector3(bounds.xMin, 3.6, -0.2),
new THREE.Vector3(bounds.xMin, 1.3, 0.9),
new THREE.Vector3(bounds.xMin, 2.8, 1.8),
new THREE.Vector3(bounds.xMin, 0.7, 2.4),
// Right conceptual wall.
new THREE.Vector3(bounds.xMax, 0.9, -1.8),
new THREE.Vector3(bounds.xMax, 2.4, -0.6),
new THREE.Vector3(bounds.xMax, 1.1, 1.1),
new THREE.Vector3(bounds.xMax, 3.2, 1.9),
// Floor.
new THREE.Vector3(-3.0, bounds.yMin, -2.1),
new THREE.Vector3(-2.3, bounds.yMin, -1.1),
new THREE.Vector3(-1.5, bounds.yMin, 0.1),
new THREE.Vector3(-0.8, bounds.yMin, 1.5),
new THREE.Vector3(-0.2, bounds.yMin, -1.8),
new THREE.Vector3(0.5, bounds.yMin, 0.5),
new THREE.Vector3(1.0, bounds.yMin, 2.1),
new THREE.Vector3(1.5, bounds.yMin, -1.4),
new THREE.Vector3(2.1, bounds.yMin, 0.1),
new THREE.Vector3(2.5, bounds.yMin, 1.8),
new THREE.Vector3(3.0, bounds.yMin, -0.4),
new THREE.Vector3(3.3, bounds.yMin, 1.1),
// Ceiling.
new THREE.Vector3(-2.6, bounds.yMax, -1.4),
new THREE.Vector3(-1.0, bounds.yMax, 0.3),
new THREE.Vector3(0.9, bounds.yMax, -1.0),
new THREE.Vector3(2.6, bounds.yMax, 0.8)
];
const pathColors = {
back: 0xf16d4d,
left: 0x287c78,
floor: 0xd7a343,
right: 0x5f84b4,
ceiling: 0x8c7ba8
};
const animatedPathMaterials = [];
function rebuildPaths() {
animatedPathMaterials.length = 0;
clearGroup(pathsGroup);
// Re-add the always-visible direct path.
const directGeometry = new THREE.BufferGeometry().setFromPoints([
sourcePosition,
listenerPosition
]);
const directMaterial = new THREE.LineDashedMaterial({
color: 0x2b8a78,
dashSize: 0.13,
gapSize: 0.08,
transparent: true,
opacity: 0.82,
depthWrite: false
});
const direct = new THREE.Line(directGeometry, directMaterial);
direct.computeLineDistances();
pathsGroup.add(direct);
let visiblePaths = 0;
let reflectedSegments = 0;
rayTargets.forEach((target, pathIndex) => {
const trace = traceRay(target);
if (!trace) return;
visiblePaths++;
reflectedSegments += trace.reflectedSegments;
const color = new THREE.Color(
pathColors[trace.firstSurface] || pathColors.back
);
for (let i = 0; i < trace.points.length - 1; i++) {
const geometry = new THREE.BufferGeometry().setFromPoints([
trace.points[i],
trace.points[i + 1]
]);
const opacity = Math.min(
0.78,
0.12 + trace.energies[i] * 0.66
);
const material = new THREE.LineBasicMaterial({
color,
transparent: true,
opacity,
depthWrite: false
});
material.userData.baseOpacity = opacity;
material.userData.phase = pathIndex * 0.47 + i;
const line = new THREE.Line(geometry, material);
pathsGroup.add(line);
animatedPathMaterials.push(material);
}
// Small low-poly markers identify reflection points.
for (let i = 1; i < trace.points.length; i++) {
const marker = new THREE.Mesh(
new THREE.OctahedronGeometry(0.052, 0),
new THREE.MeshBasicMaterial({
color,
transparent: true,
opacity: 0.86
})
);
marker.position.copy(trace.points[i]);
marker.rotation.set(pathIndex * 0.2, i, pathIndex * 0.13);
pathsGroup.add(marker);
}
});
document.querySelector("#path-count").textContent = visiblePaths;
document.querySelector("#segment-count").textContent = reflectedSegments;
updateAbsorptionReadout();
}
function updateAbsorptionReadout() {
const average =
(state.back + state.left + state.floor)
.split("")
.length; // Keeps the expression visibly finite before calculation below.
void average;
const values = Object.values(state).map(
(type) => treatments[type].absorption
);
const roomAbsorption = Math.round(
(values.reduce((sum, value) => sum + value, 0) / values.length) * 100
);
document.querySelector("#room-absorption").textContent = roomAbsorption;
["back", "left", "floor"].forEach((surface) => {
document.querySelector(`#abs-${surface}`).textContent =
Math.round(treatments[state[surface]].absorption * 100)
.toString()
.padStart(2, "0") + "%";
});
}
function syncControls() {
["back", "left", "floor"].forEach((surface) => {
document.querySelector(`#surface-${surface}`).value = state[surface];
});
updateAbsorptionReadout();
}
function setTreatment(surface, treatment) {
state[surface] = treatment;
rebuildPanel(surface);
rebuildPaths();
syncControls();
}
["back", "left", "floor"].forEach((surface) => {
document
.querySelector(`#surface-${surface}`)
.addEventListener("change", (event) => {
setTreatment(surface, event.target.value);
});
});
document.querySelector("#absorb-all").addEventListener("click", () => {
["back", "left", "floor"].forEach((surface) => {
state[surface] = "foam";
rebuildPanel(surface);
});
rebuildPaths();
syncControls();
});
document.querySelector("#reset-surfaces").addEventListener("click", () => {
["back", "left", "floor"].forEach((surface) => {
state[surface] = "bare";
rebuildPanel(surface);
});
rebuildPaths();
syncControls();
});
document.querySelector("#show-paths").addEventListener("change", (event) => {
pathsGroup.visible = event.target.checked;
});
document.querySelector("#reset-view").addEventListener("click", () => {
controls.target.set(0, 1.55, -0.15);
controls.theta = 0.62;
controls.phi = 1.04;
controls.radius = 11.7;
updateCamera();
});
// Custom camera controls; no external controls module is used.
const raycaster = new THREE.Raycaster();
const pointer = new THREE.Vector2();
const pointerState = {
down: false,
moved: false,
button: 0,
x: 0,
y: 0,
startX: 0,
startY: 0
};
function setPointerFromEvent(event) {
const rect = canvas.getBoundingClientRect();
pointer.x = ((event.clientX - rect.left) / rect.width) * 2 - 1;
pointer.y = -((event.clientY - rect.top) / rect.height) * 2 + 1;
}
function pickSurface(event) {
setPointerFromEvent(event);
raycaster.setFromCamera(pointer, camera);
const hits = raycaster.intersectObjects(interactiveMeshes, false);
return hits.length ? hits[0].object.userData.surface : null;
}
canvas.addEventListener("pointerdown", (event) => {
pointerState.down = true;
pointerState.moved = false;
pointerState.button = event.button;
pointerState.x = event.clientX;
pointerState.y = event.clientY;
pointerState.startX = event.clientX;
pointerState.startY = event.clientY;
canvas.setPointerCapture(event.pointerId);
});
canvas.addEventListener("pointermove", (event) => {
if (pointerState.down) {
const dx = event.clientX - pointerState.x;
const dy = event.clientY - pointerState.y;
if (
Math.abs(event.clientX - pointerState.startX) +
Math.abs(event.clientY - pointerState.startY) >
4
) {
pointerState.moved = true;
}
if (pointerState.button === 2) {
const right = new THREE.Vector3();
camera.getWorldDirection(right);
right.cross(camera.up).normalize();
const up = new THREE.Vector3(0, 1, 0);
const scale = controls.radius * 0.0015;
controls.target.addScaledVector(right, -dx * scale);
controls.target.addScaledVector(up, dy * scale);
controls.target.y = THREE.MathUtils.clamp(
controls.target.y,
0.5,
3.2
);
} else {
controls.theta -= dx * 0.005;
controls.phi = THREE.MathUtils.clamp(
controls.phi + dy * 0.005,
0.35,
1.43
);
}
pointerState.x = event.clientX;
pointerState.y = event.clientY;
updateCamera();
canvas.style.cursor = "grabbing";
} else {
const surface = pickSurface(event);
canvas.style.cursor = surface ? "pointer" : "grab";
}
});
canvas.addEventListener("pointerup", (event) => {
if (!pointerState.moved) {
const surface = pickSurface(event);
if (surface) {
const order = ["bare", "felt", "foam", "diffuser"];
const next =
order[(order.indexOf(state[surface]) + 1) % order.length];
setTreatment(surface, next);
}
}
pointerState.down = false;
canvas.style.cursor = "grab";
if (canvas.hasPointerCapture(event.pointerId)) {
canvas.releasePointerCapture(event.pointerId);
}
});
canvas.addEventListener("pointercancel", () => {
pointerState.down = false;
canvas.style.cursor = "grab";
});
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
controls.radius = THREE.MathUtils.clamp(
controls.radius * Math.exp(event.deltaY * 0.001),
7.2,
17.5
);
updateCamera();
},
{ passive: false }
);
canvas.addEventListener("contextmenu", (event) => {
event.preventDefault();
});
canvas.addEventListener("dblclick", () => {
controls.target.set(0, 1.55, -0.15);
controls.theta = 0.62;
controls.phi = 1.04;
controls.radius = 11.7;
updateCamera();
});
function projectLabel(object, element, offsetY = 0) {
const position = new THREE.Vector3();
object.getWorldPosition(position);
position.project(camera);
const visible = position.z > -1 && position.z < 1;
element.style.display = visible ? "block" : "none";
const x = (position.x * 0.5 + 0.5) * window.innerWidth;
const y = (-position.y * 0.5 + 0.5) * window.innerHeight + offsetY;
element.style.left = `${x}px`;
element.style.top = `${y}px`;
}
function resize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
}
window.addEventListener("resize", resize);
// Initial state.
rebuildPaths();
syncControls();
const clock = new THREE.Clock();
renderer.setAnimationLoop(() => {
const time = clock.getElapsedTime();
pulseRings.forEach((ring) => {
const phase = (time * 0.72 + ring.userData.phase) % 2.4;
const scale = 1 + phase * 1.25;
ring.scale.setScalar(scale);
ring.material.opacity = Math.max(0, 0.4 * (1 - phase / 2.4));
ring.rotation.z = time * 0.18;
});
const coreScale = 1 + Math.sin(time * 4.2) * 0.055;
sourceCore.scale.setScalar(coreScale);
sourceCore.material.emissiveIntensity = 1.15 + Math.sin(time * 4.2) * 0.25;
animatedPathMaterials.forEach((material) => {
const shimmer =
0.88 +
0.12 *
Math.sin(time * 1.8 + material.userData.phase);
material.opacity = material.userData.baseOpacity * shimmer;
});
listenerRing.rotation.z = time * 0.12;
projectLabel(
sourceGroup,
document.querySelector("#source-label"),
-13
);
projectLabel(
listenerGroup,
document.querySelector("#listener-label"),
-22
);
renderer.render(scene, camera);
});
</script>
</body>
</html>
```