Prompt specification
Build or customize the Three.js 3D scene "Library quiet-zone planning table" 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: Library quiet-zone planning table
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/library-quiet-zone-planning-table-x0164
Scene Description Prompt:
Make a standalone HTML scene with Three.js from cdnjs that depicts room acoustics. Build a library model with conversation and reading zones; users place soft dividers and observe a simplified sound-propagation overlay while keeping routes and sightlines legible. Voxel style.
Working Three.js Starter (library-quiet-zone-planning-table-x0164):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Quiet Library — Room Acoustics Lab</title>
<style>
:root {
--ink: #27312e;
--muted: #69736f;
--paper: #f7f4ec;
--panel: rgba(250, 248, 241, 0.94);
--line: rgba(44, 61, 55, 0.13);
--teal: #20a897;
--orange: #eb8b5b;
--yellow: #e8b949;
--violet: #765bd3;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #e9e7df;
color: var(--ink);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
button,
input {
font: inherit;
}
#viewport {
position: fixed;
inset: 0;
background:
radial-gradient(circle at 50% 36%, rgba(255, 255, 255, 0.72), transparent 44%),
#e9e7df;
}
#viewport canvas {
position: absolute;
inset: 0;
display: block;
width: 100%;
height: 100%;
touch-action: none;
outline: none;
}
.brand {
position: fixed;
z-index: 5;
top: 24px;
left: 26px;
pointer-events: none;
max-width: 390px;
}
.eyebrow {
display: flex;
align-items: center;
gap: 8px;
margin-bottom: 7px;
color: #52605b;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.16em;
text-transform: uppercase;
}
.eyebrow::before {
content: "";
width: 9px;
height: 9px;
border: 2px solid var(--teal);
transform: rotate(45deg);
}
h1 {
margin: 0;
font-family: Georgia, "Times New Roman", serif;
font-size: clamp(30px, 3.4vw, 48px);
font-weight: 500;
letter-spacing: -0.045em;
line-height: 0.95;
}
.brand p {
margin: 9px 0 0;
color: var(--muted);
font-size: 13px;
line-height: 1.45;
}
.panel {
position: fixed;
z-index: 6;
top: 20px;
right: 20px;
width: 318px;
max-height: calc(100vh - 40px);
overflow: auto;
padding: 18px;
border: 1px solid rgba(255, 255, 255, 0.78);
border-radius: 20px;
background: var(--panel);
box-shadow:
0 22px 55px rgba(57, 63, 58, 0.13),
inset 0 0 0 1px var(--line);
backdrop-filter: blur(18px);
-webkit-backdrop-filter: blur(18px);
}
.panel-head {
display: flex;
align-items: flex-start;
justify-content: space-between;
gap: 12px;
margin-bottom: 16px;
}
.panel-head strong {
display: block;
font-size: 14px;
letter-spacing: -0.01em;
}
.panel-head small {
display: block;
margin-top: 4px;
color: var(--muted);
font-size: 11px;
}
.count {
min-width: 29px;
height: 29px;
display: grid;
place-items: center;
border-radius: 9px;
background: #e4e9e3;
color: #52605b;
font-size: 12px;
font-weight: 800;
}
.section {
padding: 15px 0;
border-top: 1px solid var(--line);
}
.section:first-of-type {
border-top: 0;
padding-top: 0;
}
.section-title {
margin: 0 0 12px;
color: #7a837f;
font-size: 9px;
font-weight: 800;
letter-spacing: 0.16em;
text-transform: uppercase;
}
.toggle {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
min-height: 35px;
cursor: pointer;
font-size: 12px;
font-weight: 650;
}
.toggle-label {
display: flex;
align-items: center;
gap: 9px;
}
.swatch {
width: 10px;
height: 10px;
flex: 0 0 auto;
border-radius: 3px;
box-shadow: 0 0 0 1px rgba(0, 0, 0, 0.07);
}
.switch {
position: relative;
width: 34px;
height: 19px;
flex: 0 0 auto;
}
.switch input {
position: absolute;
opacity: 0;
pointer-events: none;
}
.switch span {
position: absolute;
inset: 0;
border-radius: 99px;
background: #cdd1cc;
transition: 160ms ease;
}
.switch span::after {
content: "";
position: absolute;
top: 3px;
left: 3px;
width: 13px;
height: 13px;
border-radius: 50%;
background: white;
box-shadow: 0 1px 4px rgba(0, 0, 0, 0.2);
transition: 160ms ease;
}
.switch input:checked + span {
background: var(--teal);
}
.switch input:checked + span::after {
transform: translateX(15px);
}
.control {
margin-top: 13px;
}
.control label {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 7px;
color: #49544f;
font-size: 11px;
font-weight: 650;
}
.control output {
color: #7b8580;
font-variant-numeric: tabular-nums;
font-weight: 700;
}
input[type="range"] {
width: 100%;
height: 4px;
margin: 0;
border-radius: 10px;
accent-color: var(--teal);
cursor: pointer;
}
.buttons {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 8px;
}
button {
min-height: 36px;
border: 1px solid var(--line);
border-radius: 10px;
background: rgba(255, 255, 255, 0.72);
color: #3f4c46;
font-size: 11px;
font-weight: 750;
cursor: pointer;
transition:
transform 120ms ease,
background 120ms ease,
border-color 120ms ease;
}
button:hover {
transform: translateY(-1px);
border-color: rgba(32, 168, 151, 0.35);
background: white;
}
button:active {
transform: translateY(0);
}
button.primary {
grid-column: 1 / -1;
border-color: transparent;
background: var(--ink);
color: white;
}
button.primary.active {
background: var(--teal);
}
button.danger {
color: #a1554a;
}
.hint {
margin: 10px 0 0;
color: #77817c;
font-size: 10px;
line-height: 1.55;
}
.readings {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 8px;
}
.reading {
padding: 10px;
border: 1px solid var(--line);
border-radius: 11px;
background: rgba(255, 255, 255, 0.5);
}
.reading span {
display: block;
color: #7d8681;
font-size: 9px;
font-weight: 700;
letter-spacing: 0.06em;
text-transform: uppercase;
}
.reading strong {
display: block;
margin-top: 3px;
font-size: 20px;
font-weight: 650;
letter-spacing: -0.04em;
}
.legend {
position: fixed;
z-index: 5;
left: 25px;
bottom: 24px;
display: flex;
align-items: center;
gap: 16px;
padding: 10px 13px;
border: 1px solid rgba(255, 255, 255, 0.65);
border-radius: 12px;
background: rgba(249, 247, 240, 0.82);
box-shadow: 0 10px 30px rgba(54, 63, 57, 0.08);
backdrop-filter: blur(12px);
pointer-events: none;
}
.legend-item {
display: flex;
align-items: center;
gap: 7px;
color: #5c6862;
font-size: 10px;
font-weight: 700;
}
.legend-mark {
width: 12px;
height: 7px;
border-radius: 2px;
}
.legend-mark.sound {
background: linear-gradient(90deg, #21ad9a, #ed8a5a);
}
.legend-mark.route {
background: var(--yellow);
}
.legend-mark.sight {
background: var(--violet);
}
.legend-mark.divider {
background: #75a99f;
border-radius: 1px;
}
.camera-hint {
position: fixed;
z-index: 5;
right: 24px;
bottom: 24px;
color: #79827e;
font-size: 10px;
font-weight: 650;
pointer-events: none;
}
#world-labels {
position: absolute;
z-index: 3;
inset: 0;
overflow: hidden;
pointer-events: none;
}
.world-label {
position: absolute;
left: 0;
top: 0;
padding: 6px 9px;
border: 1px solid rgba(255, 255, 255, 0.8);
border-radius: 8px;
background: rgba(251, 249, 242, 0.86);
box-shadow: 0 5px 15px rgba(53, 60, 56, 0.09);
color: #3e4b45;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.04em;
white-space: nowrap;
transform: translate(-50%, -50%);
will-change: transform;
}
.world-label.conversation {
border-bottom: 3px solid var(--teal);
}
.world-label.reading {
border-bottom: 3px solid var(--orange);
}
.world-label.route {
color: #8a6c1e;
border-bottom: 3px solid var(--yellow);
}
#toast {
position: fixed;
z-index: 10;
left: 50%;
bottom: 28px;
padding: 9px 13px;
border-radius: 10px;
background: rgba(39, 49, 46, 0.92);
color: white;
font-size: 11px;
font-weight: 650;
opacity: 0;
transform: translate(-50%, 10px);
transition: 180ms ease;
pointer-events: none;
}
#toast.visible {
opacity: 1;
transform: translate(-50%, 0);
}
@media (max-width: 760px) {
.brand {
top: 15px;
left: 17px;
}
.brand p {
display: none;
}
.panel {
top: auto;
right: 10px;
bottom: 10px;
left: 10px;
width: auto;
max-height: 43vh;
border-radius: 16px;
padding: 14px;
}
.legend {
left: 12px;
bottom: calc(43vh + 20px);
gap: 9px;
padding: 8px 9px;
}
.legend-item {
font-size: 9px;
}
.camera-hint {
display: none;
}
.world-label {
font-size: 9px;
}
}
</style>
</head>
<body>
<main id="viewport" aria-label="Interactive three-dimensional library acoustics model">
<div id="world-labels">
<div id="label-conversation" class="world-label conversation">CONVERSATION</div>
<div id="label-reading" class="world-label reading">READING</div>
<div id="label-route" class="world-label route">MAIN ROUTE</div>
</div>
</main>
<header class="brand">
<div class="eyebrow">Fieldnote / Acoustics Lab</div>
<h1>Quiet library</h1>
<p>Place soft dividers, tune the voices, and inspect how sound, routes, and sightlines share the room.</p>
</header>
<aside class="panel" aria-label="Acoustics controls">
<div class="panel-head">
<div>
<strong>Room conditions</strong>
<small>Simplified, source-based propagation model</small>
</div>
<div id="divider-count" class="count">0</div>
</div>
<section class="section">
<h2 class="section-title">Visible layers</h2>
<label class="toggle">
<span class="toggle-label">
<span class="swatch" style="background: linear-gradient(135deg, #21ad9a, #ed8a5a)"></span>
Sound field
</span>
<span class="switch">
<input id="sound-toggle" type="checkbox" checked />
<span></span>
</span>
</label>
<label class="toggle">
<span class="toggle-label">
<span class="swatch" style="background: var(--yellow)"></span>
Circulation route
</span>
<span class="switch">
<input id="route-toggle" type="checkbox" checked />
<span></span>
</span>
</label>
<label class="toggle">
<span class="toggle-label">
<span class="swatch" style="background: var(--violet)"></span>
Sightlines
</span>
<span class="switch">
<input id="sight-toggle" type="checkbox" checked />
<span></span>
</span>
</label>
</section>
<section class="section">
<h2 class="section-title">Source levels</h2>
<div class="control">
<label for="conversation-level">
Conversation
<output id="conversation-value">64 dB</output>
</label>
<input id="conversation-level" type="range" min="42" max="78" value="64" />
</div>
<div class="control">
<label for="reading-level">
Reading
<output id="reading-value">57 dB</output>
</label>
<input id="reading-level" type="range" min="38" max="72" value="57" />
</div>
<div class="control">
<label for="absorption-level">
Divider absorption
<output id="absorption-value">10 dB</output>
</label>
<input id="absorption-level" type="range" min="3" max="20" value="10" />
</div>
</section>
<section class="section">
<h2 class="section-title">Soft dividers</h2>
<div class="buttons">
<button id="place-divider" class="primary" type="button">+ Place divider</button>
<button id="rotate-divider" type="button">Rotate 90°</button>
<button id="undo-divider" type="button">Undo</button>
<button id="clear-dividers" class="danger" type="button">Clear all</button>
</div>
<p class="hint">Click the floor to place. Press R while placing to rotate. Divider absorption attenuates direct sound and stops sightlines.</p>
</section>
<section class="section">
<h2 class="section-title">Listening posts</h2>
<div class="readings">
<div class="reading">
<span>Chat side</span>
<strong id="chat-reading">— dB</strong>
</div>
<div class="reading">
<span>Quiet side</span>
<strong id="quiet-reading">— dB</strong>
</div>
</div>
</section>
</aside>
<div class="legend" aria-hidden="true">
<div class="legend-item"><span class="legend-mark sound"></span>Sound</div>
<div class="legend-item"><span class="legend-mark route"></span>Route</div>
<div class="legend-item"><span class="legend-mark sight"></span>Sightline</div>
<div class="legend-item"><span class="legend-mark divider"></span>Divider</div>
</div>
<div class="camera-hint">Drag to orbit · Shift/right-drag to pan · Scroll to zoom</div>
<div id="toast" role="status"></div>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// -------------------------------------------------------------------------
// Scene setup
// -------------------------------------------------------------------------
const viewport = document.getElementById("viewport");
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xe9e7df);
scene.fog = new THREE.Fog(0xe9e7df, 38, 66);
const renderer = new THREE.WebGLRenderer({ antialias: true, alpha: false });
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.08;
renderer.domElement.setAttribute("aria-label", "Orbitable library model");
viewport.prepend(renderer.domElement);
const camera = new THREE.OrthographicCamera(-15, 15, 10, -10, 0.1, 120);
const cameraTarget = new THREE.Vector3(0, 0.45, 0);
const cameraHome = {
position: new THREE.Vector3(20, 18, 23),
target: new THREE.Vector3(0, 0.45, 0),
zoom: 0.92
};
camera.position.copy(cameraHome.position);
camera.zoom = cameraHome.zoom;
camera.lookAt(cameraTarget);
const hemi = new THREE.HemisphereLight(0xfff8e9, 0x87968d, 2.25);
scene.add(hemi);
const sun = new THREE.DirectionalLight(0xfff4dc, 3.15);
sun.position.set(-10, 21, 14);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -19;
sun.shadow.camera.right = 19;
sun.shadow.camera.top = 17;
sun.shadow.camera.bottom = -17;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 55;
sun.shadow.normalBias = 0.035;
scene.add(sun);
const fill = new THREE.DirectionalLight(0xc9e6e3, 0.8);
fill.position.set(13, 8, -10);
scene.add(fill);
const ROOM = { width: 22, depth: 16 };
const floorPlane = new THREE.Plane(new THREE.Vector3(0, 1, 0), 0);
const raycaster = new THREE.Raycaster();
const pointerNDC = new THREE.Vector2();
function standardMaterial(color, roughness = 0.78, metalness = 0) {
return new THREE.MeshStandardMaterial({
color,
roughness,
metalness
});
}
const materials = {
floor: standardMaterial(0xd8d3c6),
floorEdge: standardMaterial(0xb8b4aa),
wall: standardMaterial(0xe7e2d7),
wallTrim: standardMaterial(0xc3bcae),
wood: standardMaterial(0x725b43),
woodLight: standardMaterial(0xa78968),
darkWood: standardMaterial(0x4e453b),
cream: standardMaterial(0xf3ecdc),
tealRug: standardMaterial(0xb9d8cf),
orangeRug: standardMaterial(0xedc8ae),
teal: standardMaterial(0x2c9e8e),
orange: standardMaterial(0xe58b5d),
route: new THREE.MeshBasicMaterial({ color: 0xe7b846 }),
routeDark: new THREE.MeshBasicMaterial({ color: 0xc99729 }),
dividerFrame: standardMaterial(0x53665f),
pot: standardMaterial(0xb66f4f),
leaves: standardMaterial(0x658e69),
leavesLight: standardMaterial(0x83a77c)
};
function addBox(parent, w, h, d, x, y, z, material, castShadow = true) {
const mesh = new THREE.Mesh(new THREE.BoxGeometry(w, h, d), material);
mesh.position.set(x, y, z);
mesh.castShadow = castShadow;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
// -------------------------------------------------------------------------
// Room shell and floor
// -------------------------------------------------------------------------
addBox(scene, ROOM.width, 0.35, ROOM.depth, 0, -0.175, 0, materials.floor);
addBox(scene, ROOM.width + 0.18, 0.16, ROOM.depth + 0.18, 0, -0.39, 0, materials.floorEdge);
addBox(scene, ROOM.width, 3.05, 0.28, 0, 1.525, -7.86, materials.wall);
addBox(scene, 0.28, 3.05, ROOM.depth, -10.86, 1.525, 0, materials.wall);
addBox(scene, ROOM.width, 0.13, 0.42, 0, 3.05, -7.83, materials.wallTrim);
addBox(scene, 0.42, 0.13, ROOM.depth, -10.83, 3.05, 0, materials.wallTrim);
// Voxel window panels.
const glass = standardMaterial(0xa9cfca, 0.35);
const windowFrame = standardMaterial(0x887966);
[-7, 0, 7].forEach((x) => {
addBox(scene, 3.15, 1.25, 0.07, x, 1.72, -7.68, glass, false);
addBox(scene, 3.38, 0.09, 0.12, x, 1.06, -7.63, windowFrame);
addBox(scene, 3.38, 0.09, 0.12, x, 2.38, -7.63, windowFrame);
addBox(scene, 0.09, 1.4, 0.12, x - 1.66, 1.72, -7.63, windowFrame);
addBox(scene, 0.09, 1.4, 0.12, x + 1.66, 1.72, -7.63, windowFrame);
addBox(scene, 0.08, 1.25, 0.1, x, 1.72, -7.61, windowFrame);
});
// Fine floor grid.
const gridPositions = [];
for (let x = -11; x <= 11; x += 1) {
gridPositions.push(x, 0.012, -8, x, 0.012, 8);
}
for (let z = -8; z <= 8; z += 1) {
gridPositions.push(-11, 0.012, z, 11, 0.012, z);
}
const gridGeometry = new THREE.BufferGeometry();
gridGeometry.setAttribute("position", new THREE.Float32BufferAttribute(gridPositions, 3));
const floorGrid = new THREE.LineSegments(
gridGeometry,
new THREE.LineBasicMaterial({
color: 0x8e938b,
transparent: true,
opacity: 0.12
})
);
scene.add(floorGrid);
// -------------------------------------------------------------------------
// Library furniture
// -------------------------------------------------------------------------
let randomSeed = 7219;
function random() {
randomSeed = (randomSeed * 1664525 + 1013904223) >>> 0;
return randomSeed / 4294967296;
}
const bookColors = [
0x5d8c85,
0xd58d67,
0x7186a7,
0xd1b25d,
0x856b8f,
0x91a477,
0xc87c72,
0x5c6d7c
];
function makeBookshelf(x, z, length, rotation = 0) {
const group = new THREE.Group();
const height = 2.72;
const depth = 0.55;
addBox(group, 0.15, height, depth, -length / 2, height / 2, 0, materials.darkWood);
addBox(group, 0.15, height, depth, length / 2, height / 2, 0, materials.darkWood);
addBox(group, length, height, 0.08, 0, height / 2, -0.23, materials.wood);
[0.18, 0.93, 1.68, 2.43].forEach((shelfY) => {
addBox(group, length, 0.1, depth, 0, shelfY, 0, materials.woodLight);
let cursor = -length / 2 + 0.18;
while (cursor < length / 2 - 0.22) {
const bookWidth = 0.13 + random() * 0.12;
const bookHeight = 0.36 + random() * 0.28;
const bookDepth = 0.31 + random() * 0.08;
const color = bookColors[Math.floor(random() * bookColors.length)];
addBox(
group,
bookWidth,
bookHeight,
bookDepth,
cursor + bookWidth / 2,
shelfY + 0.06 + bookHeight / 2,
0.03,
standardMaterial(color),
false
);
cursor += bookWidth + 0.025 + random() * 0.035;
}
});
group.position.set(x, 0, z);
group.rotation.y = rotation;
scene.add(group);
}
makeBookshelf(-6.2, -6.85, 4.75);
makeBookshelf(-0.2, -6.85, 4.75);
makeBookshelf(6.1, -6.85, 4.55);
makeBookshelf(-9.75, 1.65, 6.1, Math.PI / 2);
function makeTable(x, z, width, depth, topMaterial) {
const group = new THREE.Group();
addBox(group, width, 0.18, depth, 0, 0.98, 0, topMaterial);
const legPositions = [
[-width / 2 + 0.25, -depth / 2 + 0.24],
[width / 2 - 0.25, -depth / 2 + 0.24],
[-width / 2 + 0.25, depth / 2 - 0.24],
[width / 2 - 0.25, depth / 2 - 0.24]
];
legPositions.forEach(([lx, lz]) => {
addBox(group, 0.18, 0.9, 0.18, lx, 0.45, lz, materials.wood);
});
group.position.set(x, 0, z);
scene.add(group);
return group;
}
function makeChair(x, z, rotation, fabricMaterial) {
const group = new THREE.Group();
addBox(group, 0.72, 0.2, 0.72, 0, 0.58, 0, fabricMaterial);
addBox(group, 0.72, 0.72, 0.16, 0, 0.98, -0.31, fabricMaterial);
addBox(group, 0.16, 0.55, 0.16, -0.25, 0.28, -0.25, materials.wood);
addBox(group, 0.16, 0.55, 0.16, 0.25, 0.28, -0.25, materials.wood);
addBox(group, 0.16, 0.55, 0.16, -0.25, 0.28, 0.25, materials.wood);
addBox(group, 0.16, 0.55, 0.16, 0.25, 0.28, 0.25, materials.wood);
group.position.set(x, 0, z);
group.rotation.y = rotation;
scene.add(group);
}
function makePlant(x, z, scale = 1) {
const group = new THREE.Group();
addBox(group, 0.72, 0.55, 0.72, 0, 0.275, 0, materials.pot);
addBox(group, 0.13, 1.15, 0.13, 0, 0.95, 0, materials.wood);
const leaves = [
[0, 1.55, 0, 0.72, 0.72, 0.64],
[-0.28, 1.35, 0.05, 0.6, 0.48, 0.58],
[0.3, 1.25, -0.02, 0.56, 0.54, 0.55],
[0.05, 1.82, -0.02, 0.58, 0.55, 0.52]
];
leaves.forEach((leaf, i) => {
addBox(
group,
leaf[3],
leaf[4],
leaf[5],
leaf[0],
leaf[1],
leaf[2],
i % 2 ? materials.leaves : materials.leavesLight
);
});
group.position.set(x, 0, z);
group.scale.setScalar(scale);
scene.add(group);
}
function makeVoxelLamp(x, z, color) {
const group = new THREE.Group();
addBox(group, 0.12, 2.2, 0.12, 0, 1.1, 0, materials.darkWood);
addBox(group, 0.72, 0.12, 0.72, 0, 0.08, 0, materials.darkWood);
addBox(group, 0.8, 0.48, 0.8, 0, 2.28, 0, standardMaterial(color));
addBox(group, 0.62, 0.12, 0.62, 0, 2.0, 0, standardMaterial(0xfff0bd, 0.4));
group.position.set(x, 0, z);
scene.add(group);
}
// Conversation zone.
addBox(scene, 6.5, 0.065, 5.2, -5, 0.035, 0.25, materials.tealRug, false);
makeTable(-5, 0.4, 2.65, 1.45, materials.woodLight);
makeChair(-6.85, 0.4, Math.PI / 2, materials.teal);
makeChair(-3.15, 0.4, -Math.PI / 2, materials.teal);
makeChair(-5, -1.1, 0, materials.teal);
makeChair(-5, 1.9, Math.PI, materials.teal);
addBox(scene, 0.7, 0.08, 0.46, -5.45, 1.12, 0.38, materials.cream);
addBox(scene, 0.48, 0.1, 0.42, -4.55, 1.12, 0.3, materials.orange);
makeVoxelLamp(-8.15, -1.6, 0xdf9c68);
// Reading zone.
addBox(scene, 5.8, 0.065, 5.7, 5.25, 0.035, -1.25, materials.orangeRug, false);
makeTable(5.25, -1.3, 2.8, 1.45, materials.woodLight);
makeChair(3.35, -1.3, Math.PI / 2, materials.orange);
makeChair(7.15, -1.3, -Math.PI / 2, materials.orange);
makeChair(5.25, -3.35, 0, materials.orange);
makeChair(5.25, 0.75, Math.PI, materials.orange);
addBox(scene, 0.78, 0.09, 0.52, 4.65, 1.12, -1.3, materials.cream);
addBox(scene, 0.62, 0.11, 0.47, 5.65, 1.12, -1.25, materials.cream);
addBox(scene, 0.55, 0.1, 0.42, 5.2, 1.18, -1.28, materials.teal);
makeVoxelLamp(8.2, -4.35, 0xd99b6d);
makePlant(-9.45, -6.25, 1.05);
makePlant(9.25, 5.85, 1.2);
makePlant(-9.55, 6.2, 0.9);
// Small voxel books on a low bench.
addBox(scene, 2.25, 0.55, 0.75, -9.65, 0.275, 5.45, materials.woodLight);
[0, 1, 2, 3, 4].forEach((i) => {
addBox(
scene,
0.22,
0.38 + (i % 2) * 0.08,
0.48,
-10.45 + i * 0.39,
0.75,
5.45,
standardMaterial(bookColors[(i + 2) % bookColors.length])
);
});
// -------------------------------------------------------------------------
// Acoustic sources
// -------------------------------------------------------------------------
const sources = [
{
name: "Conversation",
position: new THREE.Vector3(-5, 0, 0.4),
color: new THREE.Color(0x20aa98),
level: 64
},
{
name: "Reading",
position: new THREE.Vector3(5.25, 0, -1.3),
color: new THREE.Color(0xeb8b5b),
level: 57
}
];
function makeSourceMarker(source) {
const group = new THREE.Group();
const coreMaterial = new THREE.MeshStandardMaterial({
color: source.color,
emissive: source.color,
emissiveIntensity: 0.75,
roughness: 0.35
});
addBox(group, 0.38, 0.38, 0.38, 0, 1.47, 0, coreMaterial, false);
const ringPoints = [
new THREE.Vector3(-0.52, 1.22, -0.52),
new THREE.Vector3(0.52, 1.22, -0.52),
new THREE.Vector3(0.52, 1.22, 0.52),
new THREE.Vector3(-0.52, 1.22, 0.52)
];
const ring = new THREE.LineLoop(
new THREE.BufferGeometry().setFromPoints(ringPoints),
new THREE.LineBasicMaterial({
color: source.color,
transparent: true,
opacity: 0.85
})
);
group.add(ring);
group.position.set(source.position.x, 0, source.position.z);
scene.add(group);
}
sources.forEach(makeSourceMarker);
// Listening-post floor markers.
const listenerPositions = [
new THREE.Vector3(-1.15, 0, 2.25),
new THREE.Vector3(1.15, 0, -2.9)
];
listenerPositions.forEach((position) => {
const points = [
new THREE.Vector3(position.x - 0.28, 0.12, position.z - 0.28),
new THREE.Vector3(position.x + 0.28, 0.12, position.z - 0.28),
new THREE.Vector3(position.x + 0.28, 0.12, position.z + 0.28),
new THREE.Vector3(position.x - 0.28, 0.12, position.z + 0.28)
];
scene.add(
new THREE.LineLoop(
new THREE.BufferGeometry().setFromPoints(points),
new THREE.LineBasicMaterial({ color: 0xffffff, transparent: true, opacity: 0.85 })
)
);
});
// -------------------------------------------------------------------------
// Soft divider geometry and placement
// -------------------------------------------------------------------------
const DIVIDER = {
halfWidth: 1.65,
halfDepth: 0.18,
height: 2.2
};
const dividers = [];
let placementMode = false;
let dividerAngle = Math.PI / 2;
let ghostDivider = null;
const dividerTileGeometry = new THREE.BoxGeometry(0.51, 0.51, 0.2);
function makeDivider(angle, isGhost = false) {
const group = new THREE.Group();
const fabricMaterial = new THREE.MeshStandardMaterial({
color: 0x70aa9f,
roughness: 0.95,
transparent: isGhost,
opacity: isGhost ? 0.3 : 1,
depthWrite: !isGhost
});
for (let row = 0; row < 3; row++) {
for (let col = 0; col < 6; col++) {
const tile = new THREE.Mesh(dividerTileGeometry, fabricMaterial);
tile.position.set(
-1.36 + col * 0.545,
0.55 + row * 0.575,
0
);
tile.castShadow = !isGhost;
tile.receiveShadow = true;
group.add(tile);
}
}
addBox(group, 0.1, 2.35, 0.32, -1.72, 1.16, 0, materials.dividerFrame, !isGhost);
addBox(group, 0.1, 2.35, 0.32, 1.72, 1.16, 0, materials.dividerFrame, !isGhost);
addBox(group, 0.55, 0.13, 0.85, -1.72, 0.1, 0, materials.dividerFrame, !isGhost);
addBox(group, 0.55, 0.13, 0.85, 1.72, 0.1, 0, materials.dividerFrame, !isGhost);
addBox(group, 3.48, 0.1, 0.3, 0, 2.34, 0, materials.dividerFrame, !isGhost);
group.rotation.y = angle;
group.userData.fabricMaterial = fabricMaterial;
group.userData.isDivider = true;
if (isGhost) {
group.visible = false;
scene.add(group);
}
return group;
}
ghostDivider = makeDivider(dividerAngle, true);
function absorption() {
return Number(document.getElementById("absorption-level").value);
}
function updateDividerMaterials() {
const amount = absorption();
const t = THREE.MathUtils.clamp(amount / 20, 0, 1);
const target = new THREE.Color(0x75aaa0).lerp(new THREE.Color(0xc5d8d2), 1 - t);
dividers.forEach((divider) => {
divider.group.userData.fabricMaterial.color.copy(target);
});
ghostDivider.userData.fabricMaterial.color.copy(target);
}
function setPlacement(enabled) {
placementMode = enabled;
const button = document.getElementById("place-divider");
button.classList.toggle("active", enabled);
button.textContent = enabled ? "● Click floor to place" : "+ Place divider";
ghostDivider.visible = enabled;
renderer.domElement.style.cursor = enabled ? "crosshair" : "grab";
if (enabled) {
showToast("Click the floor to place a divider");
}
}
function addDivider(position) {
const group = makeDivider(dividerAngle);
group.position.set(position.x, 0, position.z);
scene.add(group);
dividers.push({
group,
angle: dividerAngle,
position: group.position
});
updateAll();
showToast("Divider placed");
}
function removeLastDivider() {
if (!dividers.length) {
showToast("No dividers to remove");
return;
}
const divider = dividers.pop();
scene.remove(divider.group);
updateAll();
showToast("Divider removed");
}
function clearDividers() {
while (dividers.length) {
scene.remove(dividers.pop().group);
}
updateAll();
showToast("All dividers cleared");
}
// Convert a world point into divider-local X/Z coordinates.
function worldToDividerLocal(point, divider) {
const dx = point.x - divider.position.x;
const dz = point.z - divider.position.z;
const c = Math.cos(divider.angle);
const s = Math.sin(divider.angle);
return {
x: dx * c - dz * s,
z: dx * s + dz * c
};
}
// Segment/AABB clipping in divider-local coordinates.
function segmentDividerIntersection(start, end, divider) {
const a = worldToDividerLocal(start, divider);
const b = worldToDividerLocal(end, divider);
const dx = b.x - a.x;
const dz = b.z - a.z;
let tEnter = 0;
let tExit = 1;
const axes = [
{ p: a.x, d: dx, min: -DIVIDER.halfWidth, max: DIVIDER.halfWidth },
{ p: a.z, d: dz, min: -DIVIDER.halfDepth, max: DIVIDER.halfDepth }
];
for (const axis of axes) {
if (Math.abs(axis.d) < 1e-8) {
if (axis.p < axis.min || axis.p > axis.max) return null;
continue;
}
let t1 = (axis.min - axis.p) / axis.d;
let t2 = (axis.max - axis.p) / axis.d;
if (t1 > t2) [t1, t2] = [t2, t1];
tEnter = Math.max(tEnter, t1);
tExit = Math.min(tExit, t2);
if (tEnter > tExit) return null;
}
if (tExit < 0 || tEnter > 1) return null;
return {
enter: THREE.MathUtils.clamp(tEnter, 0, 1),
exit: THREE.MathUtils.clamp(tExit, 0, 1)
};
}
// -------------------------------------------------------------------------
// Voxel sound-propagation field
// -------------------------------------------------------------------------
const CELL = 0.5;
const GRID_X = 44;
const GRID_Z = 32;
const soundGeometry = new THREE.BoxGeometry(0.46, 0.07, 0.46);
const soundMaterial = new THREE.MeshBasicMaterial({
transparent: true,
opacity: 0.3,
depthWrite: false,
toneMapped: false
});
const soundMesh = new THREE.InstancedMesh(soundGeometry, soundMaterial, GRID_X * GRID_Z);
soundMesh.instanceMatrix.setUsage(THREE.DynamicDrawUsage);
soundMesh.frustumCulled = false;
soundMesh.renderOrder = 1;
scene.add(soundMesh);
const dummy = new THREE.Object3D();
const cellPositions = [];
for (let z = 0; z < GRID_Z; z++) {
for (let x = 0; x < GRID_X; x++) {
cellPositions.push(
new THREE.Vector3(
-10.75 + x * CELL,
0,
-7.75 + z * CELL
)
);
}
}
function sourceDbAt(source, point) {
const distance = source.position.distanceTo(point);
let db = source.level - 17 * Math.log10(Math.max(1, distance));
for (const divider of dividers) {
if (segmentDividerIntersection(source.position, point, divider)) {
db -= absorption();
}
}
return db;
}
function updateSoundField() {
let index = 0;
for (const point of cellPositions) {
let totalEnergy = 0;
let colorEnergy = 0;
const weightedColor = new THREE.Color();
for (const source of sources) {
const db = sourceDbAt(source, point);
const energy = Math.pow(10, db / 10);
totalEnergy += energy;
colorEnergy += energy;
weightedColor.r += source.color.r * energy;
weightedColor.g += source.color.g * energy;
weightedColor.b += source.color.b * energy;
}
const totalDb = 10 * Math.log10(Math.max(1e-8, totalEnergy));
const strength = THREE.MathUtils.clamp((totalDb - 36) / 27, 0, 1);
dummy.position.set(point.x, 0.085 + strength * 0.035, point.z);
dummy.rotation.set(0, 0, 0);
dummy.scale.set(1, 0.35 + strength * 1.25, 1);
dummy.updateMatrix();
soundMesh.setMatrixAt(index, dummy.matrix);
if (colorEnergy > 0) {
weightedColor.multiplyScalar(1 / colorEnergy);
}
weightedColor.multiplyScalar(0.48 + strength * 0.52);
soundMesh.setColorAt(index, weightedColor);
index++;
}
soundMesh.instanceMatrix.needsUpdate = true;
if (soundMesh.instanceColor) soundMesh.instanceColor.needsUpdate = true;
}
// -------------------------------------------------------------------------
// Circulation route
// -------------------------------------------------------------------------
const routeGroup = new THREE.Group();
scene.add(routeGroup);
const routeSegmentGeometry = new THREE.BoxGeometry(1, 0.045, 0.64);
const routeShaftGeometry = new THREE.BoxGeometry(0.55, 0.045, 0.13);
const routeHeadGeometry = new THREE.BoxGeometry(0.35, 0.045, 0.13);
function addRouteSegment(a, b) {
const dx = b.x - a.x;
const dz = b.z - a.z;
const length = Math.hypot(dx, dz);
if (length < 0.01) return;
const segment = new THREE.Mesh(routeSegmentGeometry, materials.route);
segment.position.set((a.x + b.x) / 2, 0.125, (a.z + b.z) / 2);
segment.rotation.y = -Math.atan2(dz, dx);
segment.scale.x = length;
segment.receiveShadow = true;
routeGroup.add(segment);
}
function addRouteArrow(x, z, rotation) {
const arrow = new THREE.Group();
const shaft = new THREE.Mesh(routeShaftGeometry, materials.routeDark);
shaft.position.x = -0.12;
arrow.add(shaft);
const headA = new THREE.Mesh(routeHeadGeometry, materials.routeDark);
headA.position.set(0.22, 0, 0.13);
headA.rotation.y = -0.62;
arrow.add(headA);
const headB = new THREE.Mesh(routeHeadGeometry, materials.routeDark);
headB.position.set(0.22, 0, -0.13);
headB.rotation.y = 0.62;
arrow.add(headB);
arrow.position.set(x, 0.16, z);
arrow.rotation.y = rotation;
routeGroup.add(arrow);
}
function dividerBounds(divider) {
const c = Math.abs(Math.cos(divider.angle));
const s = Math.abs(Math.sin(divider.angle));
return {
x: c * DIVIDER.halfWidth + s * DIVIDER.halfDepth,
z: s * DIVIDER.halfWidth + c * DIVIDER.halfDepth
};
}
function updateRoute() {
routeGroup.clear();
const points = [{ x: 0, z: 7.25 }];
const obstacles = dividers
.map((divider) => ({ divider, bounds: dividerBounds(divider) }))
.filter(({ divider, bounds }) => {
const overlapsCorridor = Math.abs(divider.position.x) < 1.05 + bounds.x;
const withinLength = divider.position.z > -6.2 && divider.position.z < 7;
return overlapsCorridor && withinLength;
})
.sort((a, b) => b.divider.position.z - a.divider.position.z);
for (const { divider, bounds } of obstacles) {
const side = divider.position.x >= 0 ? 1 : -1;
const bypassX = divider.position.x + side * (bounds.x + 0.9);
const beforeZ = divider.position.z + bounds.z + 0.62;
const afterZ = divider.position.z - bounds.z - 0.62;
points.push({ x: bypassX, z: beforeZ });
points.push({ x: bypassX, z: afterZ });
points.push({ x: 0, z: afterZ });
}
points.push({ x: 0, z: -6.55 });
for (let i = 0; i < points.length - 1; i++) {
const a = points[i];
const b = points[i + 1];
addRouteSegment(a, b);
const length = Math.hypot(b.x - a.x, b.z - a.z);
if (length > 1.4) {
const rotations = length > 3 ? [0.28, 0.72] : [0.5];
rotations.forEach((t) => {
addRouteArrow(
a.x + (b.x - a.x) * t,
a.z + (b.z - a.z) * t,
-Math.atan2(b.z - a.z, b.x - a.x)
);
});
}
}
// Branches remain visually separate from the main circulation spine.
addRouteSegment({ x: 0, z: 1.05 }, { x: -3.55, z: 1.05 });
addRouteSegment({ x: 0, z: -1.3 }, { x: 3.45, z: -1.3 });
}
// -------------------------------------------------------------------------
// Sightlines
// -------------------------------------------------------------------------
const sightGroup = new THREE.Group();
scene.add(sightGroup);
const sightMaterial = new THREE.LineDashedMaterial({
color: 0x765bd3,
dashSize: 0.22,
gapSize: 0.13,
transparent: true,
opacity: 0.9,
depthTest: false
});
const sightEndpointMaterial = new THREE.MeshBasicMaterial({
color: 0x765bd3,
depthTest: false
});
const sightPairs = [
[new THREE.Vector3(-6.85, 1.05, 0.4), new THREE.Vector3(-5, 1.05, 0.4)],
[new THREE.Vector3(-3.15, 1.05, 0.4), new THREE.Vector3(-5, 1.05, 0.4)],
[new THREE.Vector3(-5, 1.05, -1.1), new THREE.Vector3(-5, 1.05, 0.4)],
[new THREE.Vector3(-5, 1.05, 1.9), new THREE.Vector3(-5, 1.05, 0.4)],
[new THREE.Vector3(3.35, 1.05, -1.3), new THREE.Vector3(5.25, 1.05, -1.3)],
[new THREE.Vector3(7.15, 1.05, -1.3), new THREE.Vector3(5.25, 1.05, -1.3)],
[new THREE.Vector3(5.25, 1.05, -3.35), new THREE.Vector3(5.25, 1.05, -1.3)],
[new THREE.Vector3(5.25, 1.05, 0.75), new THREE.Vector3(5.25, 1.05, -1.3)]
];
function updateSightlines() {
sightGroup.children.forEach((child) => {
if (child.geometry) child.geometry.dispose();
});
sightGroup.clear();
for (const [start, target] of sightPairs) {
let end = target.clone();
let earliest = Infinity;
for (const divider of dividers) {
const hit = segmentDividerIntersection(start, target, divider);
if (hit && hit.enter < earliest) {
earliest = hit.enter;
}
}
if (earliest !== Infinity) {
end = start.clone().lerp(target, Math.max(0.02, earliest - 0.025));
}
const line = new THREE.Line(
new THREE.BufferGeometry().setFromPoints([start, end]),
sightMaterial
);
line.computeLineDistances();
line.renderOrder = 12;
sightGroup.add(line);
const startMark = new THREE.Mesh(
new THREE.BoxGeometry(0.11, 0.11, 0.11),
sightEndpointMaterial
);
startMark.position.copy(start);
startMark.renderOrder = 13;
sightGroup.add(startMark);
const endMark = new THREE.Mesh(
new THREE.BoxGeometry(0.11, 0.11, 0.11),
sightEndpointMaterial
);
endMark.position.copy(end);
endMark.renderOrder = 13;
sightGroup.add(endMark);
}
}
// -------------------------------------------------------------------------
// Readings and combined updates
// -------------------------------------------------------------------------
function combinedDbAt(point) {
let energy = 0;
for (const source of sources) {
energy += Math.pow(10, sourceDbAt(source, point) / 10);
}
return 10 * Math.log10(Math.max(1e-8, energy));
}
function updateReadings() {
document.getElementById("chat-reading").textContent =
`${combinedDbAt(listenerPositions[0]).toFixed(0)} dB`;
document.getElementById("quiet-reading").textContent =
`${combinedDbAt(listenerPositions[1]).toFixed(0)} dB`;
}
function updateAll() {
updateDividerMaterials();
updateSoundField();
updateRoute();
updateSightlines();
updateReadings();
document.getElementById("divider-count").textContent = dividers.length;
}
// -------------------------------------------------------------------------
// UI
// -------------------------------------------------------------------------
const conversationSlider = document.getElementById("conversation-level");
const readingSlider = document.getElementById("reading-level");
const absorptionSlider = document.getElementById("absorption-level");
conversationSlider.addEventListener("input", () => {
sources[0].level = Number(conversationSlider.value);
document.getElementById("conversation-value").textContent =
`${conversationSlider.value} dB`;
updateAll();
});
readingSlider.addEventListener("input", () => {
sources[1].level = Number(readingSlider.value);
document.getElementById("reading-value").textContent =
`${readingSlider.value} dB`;
updateAll();
});
absorptionSlider.addEventListener("input", () => {
document.getElementById("absorption-value").textContent =
`${absorptionSlider.value} dB`;
updateAll();
});
document.getElementById("sound-toggle").addEventListener("change", (event) => {
soundMesh.visible = event.target.checked;
});
document.getElementById("route-toggle").addEventListener("change", (event) => {
routeGroup.visible = event.target.checked;
});
document.getElementById("sight-toggle").addEventListener("change", (event) => {
sightGroup.visible = event.target.checked;
});
document.getElementById("place-divider").addEventListener("click", () => {
setPlacement(!placementMode);
});
document.getElementById("rotate-divider").addEventListener("click", () => {
dividerAngle += Math.PI / 2;
ghostDivider.rotation.y = dividerAngle;
if (!placementMode) setPlacement(true);
showToast("Divider rotated 90°");
});
document.getElementById("undo-divider").addEventListener("click", removeLastDivider);
document.getElementById("clear-dividers").addEventListener("click", clearDividers);
let toastTimer;
function showToast(message) {
const toast = document.getElementById("toast");
toast.textContent = message;
toast.classList.add("visible");
clearTimeout(toastTimer);
toastTimer = setTimeout(() => toast.classList.remove("visible"), 1500);
}
// -------------------------------------------------------------------------
// Direct camera controls: orbit, pan, zoom, and reset
// -------------------------------------------------------------------------
let pointerDown = null;
function updatePointerNDC(event) {
const rect = renderer.domElement.getBoundingClientRect();
pointerNDC.x = ((event.clientX - rect.left) / rect.width) * 2 - 1;
pointerNDC.y = -((event.clientY - rect.top) / rect.height) * 2 + 1;
}
function groundPointFromEvent(event) {
updatePointerNDC(event);
raycaster.setFromCamera(pointerNDC, camera);
const point = new THREE.Vector3();
if (!raycaster.ray.intersectPlane(floorPlane, point)) return null;
point.x = THREE.MathUtils.clamp(
Math.round(point.x * 2) / 2,
-9.6,
9.6
);
point.z = THREE.MathUtils.clamp(
Math.round(point.z * 2) / 2,
-7.2,
7.2
);
point.y = 0;
return point;
}
function orbitCamera(dx, dy) {
const offset = camera.position.clone().sub(cameraTarget);
const spherical = new THREE.Spherical().setFromVector3(offset);
spherical.theta -= dx * 0.007;
spherical.phi = THREE.MathUtils.clamp(spherical.phi - dy * 0.006, 0.34, 1.28);
camera.position.copy(cameraTarget).add(offset.setFromSpherical(spherical));
camera.lookAt(cameraTarget);
}
function panCamera(dx, dy) {
const viewDirection = camera.getWorldDirection(new THREE.Vector3());
const right = new THREE.Vector3().crossVectors(viewDirection, camera.up).normalize();
const cameraUp = new THREE.Vector3().crossVectors(right, viewDirection).normalize();
const frustumHeight = (camera.top - camera.bottom) / camera.zoom;
const unitsPerPixel = frustumHeight / renderer.domElement.clientHeight;
cameraTarget.addScaledVector(right, -dx * unitsPerPixel);
cameraTarget.addScaledVector(cameraUp, dy * unitsPerPixel);
cameraTarget.x = THREE.MathUtils.clamp(cameraTarget.x, -7, 7);
cameraTarget.z = THREE.MathUtils.clamp(cameraTarget.z, -6, 6);
cameraTarget.y = 0.45;
camera.position.addScaledVector(right, -dx * unitsPerPixel);
camera.position.addScaledVector(cameraUp, dy * unitsPerPixel);
camera.lookAt(cameraTarget);
}
renderer.domElement.addEventListener("pointerdown", (event) => {
if (event.button !== 0 && event.button !== 2) return;
pointerDown = {
x: event.clientX,
y: event.clientY,
button: event.button,
moved: false
};
renderer.domElement.setPointerCapture(event.pointerId);
renderer.domElement.style.cursor = placementMode ? "crosshair" : "grabbing";
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (placementMode) {
const point = groundPointFromEvent(event);
if (point) {
ghostDivider.position.set(point.x, 0, point.z);
ghostDivider.visible = true;
}
}
if (!pointerDown) return;
const dx = event.clientX - pointerDown.x;
const dy = event.clientY - pointerDown.y;
const distance = Math.hypot(dx, dy);
if (distance > 4) pointerDown.moved = true;
if (!pointerDown.moved) return;
if (event.shiftKey || pointerDown.button === 2) {
panCamera(dx, dy);
} else {
orbitCamera(dx, dy);
}
pointerDown.x = event.clientX;
pointerDown.y = event.clientY;
});
renderer.domElement.addEventListener("pointerup", (event) => {
if (!pointerDown) return;
if (placementMode && !pointerDown.moved && pointerDown.button === 0) {
const point = groundPointFromEvent(event);
if (point) addDivider(point);
}
pointerDown = null;
renderer.domElement.style.cursor = placementMode ? "crosshair" : "grab";
});
renderer.domElement.addEventListener("pointercancel", () => {
pointerDown = null;
});
renderer.domElement.addEventListener("pointerleave", () => {
if (!pointerDown) ghostDivider.visible = false;
});
renderer.domElement.addEventListener("contextmenu", (event) => {
event.preventDefault();
});
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
camera.zoom = THREE.MathUtils.clamp(
camera.zoom * Math.exp(-event.deltaY * 0.001),
0.55,
2.3
);
camera.updateProjectionMatrix();
},
{ passive: false }
);
function resetCamera() {
camera.position.copy(cameraHome.position);
cameraTarget.copy(cameraHome.target);
camera.zoom = cameraHome.zoom;
camera.lookAt(cameraTarget);
camera.updateProjectionMatrix();
showToast("Camera reset");
}
window.addEventListener("keydown", (event) => {
const tag = document.activeElement?.tagName;
if (tag === "INPUT" || tag === "TEXTAREA") return;
if (event.key.toLowerCase() === "r") {
dividerAngle += Math.PI / 2;
ghostDivider.rotation.y = dividerAngle;
if (!placementMode) setPlacement(true);
showToast("Divider rotated 90°");
}
if (event.key === "Escape") {
setPlacement(false);
}
if (event.key === "Backspace" || event.key === "Delete") {
event.preventDefault();
removeLastDivider();
}
if (event.key.toLowerCase() === "c") {
resetCamera();
}
});
// Double-click resets the camera.
renderer.domElement.addEventListener("dblclick", resetCamera);
// -------------------------------------------------------------------------
// Projected labels
// -------------------------------------------------------------------------
const labelItems = [
{
element: document.getElementById("label-conversation"),
position: new THREE.Vector3(-5, 3.05, 0.4)
},
{
element: document.getElementById("label-reading"),
position: new THREE.Vector3(5.25, 3.05, -1.3)
},
{
element: document.getElementById("label-route"),
position: new THREE.Vector3(0, 0.45, 4.5)
}
];
function updateLabels() {
const width = viewport.clientWidth;
const height = viewport.clientHeight;
for (const item of labelItems) {
const projected = item.position.clone().project(camera);
const visible = projected.z > -1 && projected.z < 1;
item.element.style.display = visible ? "block" : "none";
if (!visible) continue;
const x = (projected.x * 0.5 + 0.5) * width;
const y = (-projected.y * 0.5 + 0.5) * height;
item.element.style.transform = `translate(-50%, -50%) translate(${x}px, ${y}px)`;
}
}
// -------------------------------------------------------------------------
// Resize and render loop
// -------------------------------------------------------------------------
function resize() {
const width = window.innerWidth;
const height = window.innerHeight;
const aspect = width / height;
const viewSize = 19.5;
camera.left = (-viewSize * aspect) / 2;
camera.right = (viewSize * aspect) / 2;
camera.top = viewSize / 2;
camera.bottom = -viewSize / 2;
camera.updateProjectionMatrix();
renderer.setSize(width, height);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
}
window.addEventListener("resize", resize);
resize();
updateAll();
renderer.domElement.style.cursor = "grab";
function animate() {
requestAnimationFrame(animate);
updateLabels();
renderer.render(scene, camera);
}
animate();
</script>
</body>
</html>
```