Prompt specification
Build or customize the Three.js 3D scene "Library shelf reach ranges" 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 shelf reach ranges
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/library-shelf-reach-ranges-x0465
Scene Description Prompt:
Please make a voxel Three.js scene in one standalone HTML file using cdnjs. Display a small shelf with books at several heights and two visitor reach envelopes; highlight the overlap region where both can access the same objects.
Working Three.js Starter (library-shelf-reach-ranges-x0465):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Voxel Shared Reach</title>
<style>
:root {
--ink: #24363a;
--paper: #f4f0e6;
--visitor-a: #169f95;
--visitor-b: #e9685b;
--overlap: #f3c843;
--panel: rgba(250, 247, 238, 0.9);
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #e9e5dc;
color: var(--ink);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
#stage {
position: fixed;
inset: 0;
}
canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
cursor: grab;
}
canvas:active {
cursor: grabbing;
}
.hud {
position: fixed;
z-index: 5;
top: 20px;
left: 20px;
width: min(350px, calc(100vw - 40px));
padding: 16px 18px;
border: 1px solid rgba(36, 54, 58, 0.12);
border-radius: 14px;
background: var(--panel);
box-shadow: 0 12px 35px rgba(47, 48, 42, 0.09);
backdrop-filter: blur(12px);
pointer-events: none;
}
.eyebrow {
margin-bottom: 5px;
color: #617073;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.18em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: clamp(22px, 3vw, 31px);
line-height: 1.02;
letter-spacing: -0.04em;
}
.subtitle {
margin: 9px 0 13px;
color: #5c696b;
font-size: 13px;
line-height: 1.45;
}
.legend {
display: flex;
flex-wrap: wrap;
gap: 8px 13px;
padding-top: 12px;
border-top: 1px solid rgba(36, 54, 58, 0.1);
font-size: 11px;
font-weight: 700;
}
.legend-item {
display: inline-flex;
align-items: center;
gap: 6px;
}
.swatch {
width: 11px;
height: 11px;
border-radius: 3px;
box-shadow: inset 0 0 0 1px rgba(0, 0, 0, 0.12);
}
.swatch.a {
background: var(--visitor-a);
}
.swatch.b {
background: var(--visitor-b);
}
.swatch.overlap {
background: var(--overlap);
}
.stats {
position: fixed;
z-index: 5;
top: 20px;
right: 20px;
padding: 11px 14px;
border: 1px solid rgba(36, 54, 58, 0.12);
border-radius: 12px;
background: var(--panel);
box-shadow: 0 10px 28px rgba(47, 48, 42, 0.08);
text-align: right;
pointer-events: none;
}
.stats strong {
display: block;
font-size: 18px;
letter-spacing: -0.03em;
}
.stats span {
color: #667375;
font-size: 10px;
font-weight: 800;
letter-spacing: 0.1em;
text-transform: uppercase;
}
.hint {
position: fixed;
z-index: 5;
bottom: 18px;
left: 20px;
padding: 9px 12px;
border-radius: 10px;
background: rgba(250, 247, 238, 0.82);
color: #647174;
font-size: 11px;
font-weight: 650;
pointer-events: none;
}
.view-controls {
position: fixed;
z-index: 8;
right: 20px;
bottom: 18px;
display: flex;
gap: 6px;
}
.view-controls button {
width: 38px;
height: 36px;
border: 1px solid rgba(36, 54, 58, 0.16);
border-radius: 9px;
background: var(--panel);
color: var(--ink);
font: 800 15px/1 ui-sans-serif, system-ui, sans-serif;
box-shadow: 0 8px 20px rgba(47, 48, 42, 0.08);
cursor: pointer;
}
.view-controls button:hover {
background: white;
transform: translateY(-1px);
}
.view-controls button:last-child {
width: auto;
padding: 0 12px;
font-size: 11px;
}
.scene-label {
position: fixed;
z-index: 4;
display: flex;
align-items: center;
gap: 7px;
padding: 6px 9px;
border: 1px solid rgba(36, 54, 58, 0.12);
border-radius: 8px;
background: rgba(250, 247, 238, 0.93);
box-shadow: 0 6px 18px rgba(39, 48, 47, 0.1);
color: var(--ink);
font-size: 9px;
font-weight: 850;
letter-spacing: 0.08em;
line-height: 1.1;
text-transform: uppercase;
white-space: nowrap;
transform: translate(-50%, -100%);
pointer-events: none;
}
.scene-label::after {
position: absolute;
left: 50%;
bottom: -6px;
width: 2px;
height: 6px;
background: currentColor;
content: "";
opacity: 0.5;
}
.scene-label b {
display: grid;
width: 20px;
height: 20px;
place-items: center;
border-radius: 5px;
color: white;
background: currentColor;
font-size: 11px;
}
.scene-label b::first-letter {
color: white;
}
.label-a {
color: var(--visitor-a);
}
.label-b {
color: var(--visitor-b);
}
.label-overlap {
color: #8a6810;
border-color: rgba(177, 131, 13, 0.25);
background: rgba(255, 244, 199, 0.96);
text-align: center;
}
.label-shelf {
color: #596669;
}
@media (max-width: 700px) {
.hud {
top: 12px;
left: 12px;
width: min(300px, calc(100vw - 24px));
padding: 12px 14px;
}
.subtitle {
display: none;
}
.stats {
top: auto;
right: 12px;
bottom: 62px;
}
.hint {
display: none;
}
.view-controls {
right: 12px;
bottom: 12px;
}
}
</style>
</head>
<body>
<div id="stage" aria-label="Interactive voxel shelf and visitor reach scene"></div>
<section class="hud">
<div class="eyebrow">Spatial access study · Voxel scene</div>
<h1>Shared Reach</h1>
<p class="subtitle">
Two visitors approach the same shelf. Their voxelized reach envelopes
intersect in the gold region, revealing books accessible to both.
</p>
<div class="legend">
<div class="legend-item">
<span class="swatch a"></span>
Visitor A reach
</div>
<div class="legend-item">
<span class="swatch b"></span>
Visitor B reach
</div>
<div class="legend-item">
<span class="swatch overlap"></span>
Shared overlap
</div>
</div>
</section>
<div class="stats">
<strong id="objectCount">—</strong>
<span>books · shared highlighted</span>
</div>
<div class="hint">Drag to orbit · Scroll to zoom · Double-click to reset</div>
<div class="view-controls">
<button id="zoomOut" aria-label="Zoom out">−</button>
<button id="zoomIn" aria-label="Zoom in">+</button>
<button id="resetView">Reset</button>
</div>
<div id="labelA" class="scene-label label-a">
<b>A</b><span>Visitor · reach 3.45 m</span>
</div>
<div id="labelB" class="scene-label label-b">
<b>B</b><span>Visitor · reach 3.45 m</span>
</div>
<div id="labelOverlap" class="scene-label label-overlap">
<span>Shared zone · both can reach</span>
</div>
<div id="labelShelf" class="scene-label label-shelf">
<span>Voxel shelf · four levels</span>
</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// -------------------------------------------------------------------------
// Renderer and camera
// -------------------------------------------------------------------------
const stage = document.getElementById("stage");
const renderer = new THREE.WebGLRenderer({
antialias: true,
alpha: false,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.08;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
stage.appendChild(renderer.domElement);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xe9e5dc);
scene.fog = new THREE.Fog(0xe9e5dc, 15, 30);
const camera = new THREE.OrthographicCamera(-7, 7, 5, -5, 0.1, 50);
const cameraTarget = new THREE.Vector3(0, 1.65, 0.25);
const defaultView = {
yaw: 0.46,
pitch: 0.87
};
let yaw = defaultView.yaw;
let pitch = defaultView.pitch;
const cameraRadius = 11;
function updateCamera() {
const horizontal = Math.sin(pitch) * cameraRadius;
camera.position.set(
cameraTarget.x + horizontal * Math.sin(yaw),
cameraTarget.y + Math.cos(pitch) * cameraRadius,
cameraTarget.z + horizontal * Math.cos(yaw)
);
camera.lookAt(cameraTarget);
}
updateCamera();
// -------------------------------------------------------------------------
// Lighting
// -------------------------------------------------------------------------
scene.add(new THREE.HemisphereLight(0xfffbef, 0x796c5d, 1.8));
const keyLight = new THREE.DirectionalLight(0xfff4d9, 3.2);
keyLight.position.set(5, 10, 7);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -9;
keyLight.shadow.camera.right = 9;
keyLight.shadow.camera.top = 9;
keyLight.shadow.camera.bottom = -9;
keyLight.shadow.camera.near = 1;
keyLight.shadow.camera.far = 25;
keyLight.shadow.bias = -0.00035;
scene.add(keyLight);
const fillLight = new THREE.DirectionalLight(0xb8d8dc, 1.1);
fillLight.position.set(-7, 5, -4);
scene.add(fillLight);
// -------------------------------------------------------------------------
// Shared geometry and material helpers
// -------------------------------------------------------------------------
const unitBox = new THREE.BoxGeometry(1, 1, 1);
const materialCache = new Map();
function getMaterial(color) {
if (!materialCache.has(color)) {
materialCache.set(
color,
new THREE.MeshStandardMaterial({
color,
roughness: 0.78,
metalness: 0.02
})
);
}
return materialCache.get(color);
}
function addBox(
parent,
size,
position,
material,
castShadow = true,
receiveShadow = true
) {
const mesh = new THREE.Mesh(unitBox, material);
mesh.scale.set(size.x, size.y, size.z);
mesh.position.copy(position);
mesh.castShadow = castShadow;
mesh.receiveShadow = receiveShadow;
parent.add(mesh);
return mesh;
}
const v = (x, y, z) => new THREE.Vector3(x, y, z);
// -------------------------------------------------------------------------
// Checkerboard voxel floor
// -------------------------------------------------------------------------
const floorMaterial = new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 0.94
});
const floorColumns = 16;
const floorRows = 13;
const floorTiles = floorColumns * floorRows;
const floor = new THREE.InstancedMesh(
unitBox,
floorMaterial,
floorTiles
);
const dummy = new THREE.Object3D();
const floorColors = [
new THREE.Color(0xe3ded2),
new THREE.Color(0xeee9de),
new THREE.Color(0xd9d7ca)
];
let floorIndex = 0;
for (let row = 0; row < floorRows; row++) {
for (let column = 0; column < floorColumns; column++) {
dummy.position.set(
(column - (floorColumns - 1) / 2) * 0.82,
-0.09,
(row - (floorRows - 1) / 2) * 0.82 - 0.25
);
dummy.scale.set(0.79, 0.14, 0.79);
dummy.updateMatrix();
floor.setMatrixAt(floorIndex, dummy.matrix);
const variation = (column + row) % 3;
floor.setColorAt(floorIndex, floorColors[variation]);
floorIndex++;
}
}
floor.instanceMatrix.needsUpdate = true;
if (floor.instanceColor) floor.instanceColor.needsUpdate = true;
floor.receiveShadow = true;
scene.add(floor);
// -------------------------------------------------------------------------
// Voxel shelf
// -------------------------------------------------------------------------
const woodDark = getMaterial(0x79503b);
const wood = getMaterial(0xa86e43);
const woodLight = getMaterial(0xc58b56);
const woodBack = getMaterial(0x6e4938);
addBox(
scene,
v(4.95, 3.9, 0.14),
v(0, 2.18, -1.08),
woodBack
);
addBox(
scene,
v(0.24, 4.25, 1.08),
v(-2.52, 2.08, -0.55),
woodDark
);
addBox(
scene,
v(0.24, 4.25, 1.08),
v(2.52, 2.08, -0.55),
woodDark
);
addBox(
scene,
v(5.28, 0.24, 1.08),
v(0, 4.18, -0.55),
woodLight
);
const shelfLevels = [0.42, 1.36, 2.30, 3.24];
for (const level of shelfLevels) {
addBox(
scene,
v(4.84, 0.16, 1.08),
v(0, level, -0.55),
wood
);
}
// -------------------------------------------------------------------------
// Books and shared-object highlighting
// -------------------------------------------------------------------------
const bookPalette = [
0x557b9a,
0xd2705f,
0x669574,
0x866d98,
0xc49a49,
0x4d8e99,
0xb76372,
0x7d8461
];
const paperMaterial = getMaterial(0xf1ead8);
const sharedBookMaterial = new THREE.MeshStandardMaterial({
color: 0xf4c94f,
roughness: 0.62,
emissive: 0x8f6408,
emissiveIntensity: 0.11
});
const sharedTagMaterial = new THREE.MeshBasicMaterial({
color: 0x1c9c94
});
const envelopeDefinitions = [
{
center: v(-2.35, 1.95, 1.25),
radius: v(3.45, 3.25, 2.8)
},
{
center: v(2.35, 1.95, 1.25),
radius: v(3.45, 3.25, 2.8)
}
];
function insideEnvelope(point, envelope) {
const dx = (point.x - envelope.center.x) / envelope.radius.x;
const dy = (point.y - envelope.center.y) / envelope.radius.y;
const dz = (point.z - envelope.center.z) / envelope.radius.z;
return dx * dx + dy * dy + dz * dz <= 1 && point.y >= 0;
}
const leftSlots = [
{ x: -2.08, width: 0.27 },
{ x: -1.73, width: 0.32 },
{ x: -1.39, width: 0.25 },
{ x: -1.02, width: 0.34 }
];
const rightSlots = [
{ x: 0.38, width: 0.28 },
{ x: 0.72, width: 0.31 },
{ x: 1.08, width: 0.26 },
{ x: 1.42, width: 0.34 },
{ x: 1.82, width: 0.29 },
{ x: 2.13, width: 0.24 }
];
let bookCount = 0;
let sharedCount = 0;
let colorIndex = 0;
function seededRandom(index) {
const x = Math.sin(index * 127.1 + 311.7) * 43758.5453;
return x - Math.floor(x);
}
function addBook(slot, level, bookIndex) {
const isShared = Math.abs(slot.x) < 0.001;
const height = isShared
? 0.70 + seededRandom(bookIndex) * 0.06
: 0.56 + seededRandom(bookIndex) * 0.20;
const depth = isShared
? 0.38
: 0.27 + seededRandom(bookIndex + 9) * 0.10;
const bookZ = 0.015;
const bookY = level + 0.08 + height / 2;
const bookPosition = v(slot.x, bookY, bookZ);
const shared =
insideEnvelope(bookPosition, envelopeDefinitions[0]) &&
insideEnvelope(bookPosition, envelopeDefinitions[1]);
const material = isShared
? sharedBookMaterial
: getMaterial(bookPalette[colorIndex++ % bookPalette.length]);
addBox(
scene,
v(slot.width, height, depth),
bookPosition,
material
);
// Small voxel spine label.
addBox(
scene,
v(slot.width * 0.64, 0.065, 0.026),
v(slot.x, bookY + height * 0.08, bookZ + depth / 2 + 0.014),
paperMaterial,
false
);
if (shared) {
// Dark voxel outline makes the dual-access books explicit.
const outlineGeometry = new THREE.EdgesGeometry(
new THREE.BoxGeometry(
slot.width + 0.055,
height + 0.055,
depth + 0.055
)
);
const outline = new THREE.LineSegments(
outlineGeometry,
new THREE.LineBasicMaterial({
color: 0x126f70,
transparent: true,
opacity: 0.95
})
);
outline.position.copy(bookPosition);
scene.add(outline);
// Cyan badge on the shelf-facing spine.
addBox(
scene,
v(0.095, 0.095, 0.032),
v(slot.x, bookY - height * 0.23, bookZ + depth / 2 + 0.018),
sharedTagMaterial,
false
);
sharedCount++;
}
bookCount++;
}
for (let levelIndex = 0; levelIndex < shelfLevels.length; levelIndex++) {
const level = shelfLevels[levelIndex];
let bookIndex = levelIndex * 100;
for (const slot of leftSlots) {
addBook(slot, level, bookIndex++);
}
addBook({ x: 0, width: 0.32 }, level, bookIndex++);
for (const slot of rightSlots) {
addBook(slot, level, bookIndex++);
}
}
document.getElementById("objectCount").textContent =
`${bookCount} total · ${sharedCount} shared`;
// -------------------------------------------------------------------------
// Voxel visitors
// -------------------------------------------------------------------------
const skinMaterial = getMaterial(0xd7a77f);
const darkClothing = getMaterial(0x35454a);
const hairMaterial = getMaterial(0x443a34);
const shoeMaterial = getMaterial(0x263236);
function createVisitor(x, jacketColor, innerSide) {
const group = new THREE.Group();
group.position.set(x, 0, 2.08);
scene.add(group);
const jacket = getMaterial(jacketColor);
// Ground marker.
const markerMaterial = new THREE.MeshBasicMaterial({
color: jacketColor,
transparent: true,
opacity: 0.75
});
addBox(group, v(0.9, 0.025, 0.045), v(0, 0.015, 0.43), markerMaterial, false);
addBox(group, v(0.9, 0.025, 0.045), v(0, 0.015, -0.43), markerMaterial, false);
addBox(group, v(0.045, 0.025, 0.9), v(0.43, 0.015, 0), markerMaterial, false);
addBox(group, v(0.045, 0.025, 0.9), v(-0.43, 0.015, 0), markerMaterial, false);
// Legs and shoes.
addBox(group, v(0.24, 0.68, 0.30), v(-0.17, 0.43, 0), darkClothing);
addBox(group, v(0.24, 0.68, 0.30), v(0.17, 0.43, 0), darkClothing);
addBox(group, v(0.29, 0.16, 0.43), v(-0.17, 0.09, 0.05), shoeMaterial);
addBox(group, v(0.29, 0.16, 0.43), v(0.17, 0.09, 0.05), shoeMaterial);
// Torso, neck, head and hair.
addBox(group, v(0.67, 0.76, 0.39), v(0, 1.13, 0), jacket);
addBox(group, v(0.19, 0.18, 0.19), v(0, 1.60, 0), skinMaterial);
addBox(group, v(0.47, 0.47, 0.45), v(0, 1.86, -0.01), skinMaterial);
addBox(group, v(0.50, 0.14, 0.48), v(0, 2.10, 0.015), hairMaterial);
// Small nose points toward the shelf.
addBox(group, v(0.10, 0.10, 0.10), v(0, 1.84, -0.28), skinMaterial);
// Back patch, visible from the default camera.
addBox(group, v(0.24, 0.24, 0.035), v(0, 1.19, 0.215), paperMaterial, false);
// The inward arm reaches into the shared space.
addBox(
group,
v(0.20, 0.22, 0.78),
v(0.34 * innerSide, 1.29, -0.30),
jacket
);
addBox(
group,
v(0.20, 0.20, 0.22),
v(0.38 * innerSide, 1.27, -0.73),
skinMaterial
);
// Outward arm.
addBox(
group,
v(0.20, 0.55, 0.24),
v(-0.40 * innerSide, 1.14, 0),
jacket
);
addBox(
group,
v(0.19, 0.19, 0.22),
v(-0.40 * innerSide, 0.82, -0.01),
skinMaterial
);
return group;
}
createVisitor(-3.08, 0x169f95, 1);
createVisitor(3.08, 0xe9685b, -1);
// -------------------------------------------------------------------------
// Voxelized reach envelopes
// -------------------------------------------------------------------------
const voxelStep = 0.34;
function pointKey(ix, iy, iz) {
return `${ix},${iy},${iz}`;
}
function buildEnvelopeSet(envelope) {
const points = [];
const keys = new Set();
for (let ix = -21; ix <= 21; ix++) {
for (let iy = -9; iy <= 10; iy++) {
for (let iz = -9; iz <= 9; iz++) {
const x = (ix + 0.5) * voxelStep;
const y = (iy + 0.5) * voxelStep;
const z = (iz + 0.5) * voxelStep;
if (!insideEnvelope(v(x, y, z), envelope)) continue;
const point = { x, y, z, ix, iy, iz };
points.push(point);
keys.add(pointKey(ix, iy, iz));
}
}
}
return { points, keys };
}
const envelopeA = buildEnvelopeSet(envelopeDefinitions[0]);
const envelopeB = buildEnvelopeSet(envelopeDefinitions[1]);
function isBoundary(keys, point) {
const neighbors = [
[1, 0, 0], [-1, 0, 0],
[0, 1, 0], [0, -1, 0],
[0, 0, 1], [0, 0, -1]
];
for (const [dx, dy, dz] of neighbors) {
if (
!keys.has(
pointKey(
point.ix + dx,
point.iy + dy,
point.iz + dz
)
)
) {
return true;
}
}
return false;
}
function createVoxelInstances(
points,
color,
opacity,
size,
renderOrder
) {
const geometry = new THREE.BoxGeometry(size, size, size);
const material = new THREE.MeshBasicMaterial({
color,
transparent: true,
opacity,
depthWrite: false,
toneMapped: false
});
const mesh = new THREE.InstancedMesh(
geometry,
material,
points.length
);
const instanceDummy = new THREE.Object3D();
points.forEach((point, index) => {
instanceDummy.position.set(point.x, point.y, point.z);
instanceDummy.updateMatrix();
mesh.setMatrixAt(index, instanceDummy.matrix);
});
mesh.instanceMatrix.needsUpdate = true;
mesh.renderOrder = renderOrder;
mesh.frustumCulled = false;
scene.add(mesh);
return { mesh, material };
}
const shellA = envelopeA.points.filter((point) =>
isBoundary(envelopeA.keys, point)
);
const shellB = envelopeB.points.filter((point) =>
isBoundary(envelopeB.keys, point)
);
createVoxelInstances(
shellA,
0x169f95,
0.17,
voxelStep * 0.91,
2
);
createVoxelInstances(
shellB,
0xe9685b,
0.17,
voxelStep * 0.91,
2
);
// Exact intersection of the two continuous ellipsoids, sampled on the
// same voxel lattice. This is the highlighted shared-access volume.
const overlapPoints = envelopeA.points.filter((point) =>
envelopeB.keys.has(pointKey(point.ix, point.iy, point.iz))
);
const overlapKeys = new Set(
overlapPoints.map((point) =>
pointKey(point.ix, point.iy, point.iz)
)
);
const overlapFill = overlapPoints.filter((point) =>
((point.ix + point.iy + point.iz) & 1) === 0
);
const overlapShell = overlapPoints.filter((point) =>
isBoundary(overlapKeys, point)
);
createVoxelInstances(
overlapFill,
0xf3c843,
0.10,
voxelStep * 0.86,
3
);
const overlapHighlight = createVoxelInstances(
overlapShell,
0xffcf36,
0.42,
voxelStep * 0.94,
4
);
// -------------------------------------------------------------------------
// HTML scene labels projected from Three.js world coordinates
// -------------------------------------------------------------------------
const labels = [
{
element: document.getElementById("labelA"),
position: v(-3.08, 2.46, 2.08)
},
{
element: document.getElementById("labelB"),
position: v(3.08, 2.46, 2.08)
},
{
element: document.getElementById("labelOverlap"),
position: v(0, 4.72, 0.05)
},
{
element: document.getElementById("labelShelf"),
position: v(-2.42, 4.30, -0.58)
}
];
function updateLabels() {
const width = window.innerWidth;
const height = window.innerHeight;
for (const label of labels) {
const projected = label.position.clone().project(camera);
const visible =
projected.z >= -1 &&
projected.z <= 1 &&
Math.abs(projected.x) < 1.25 &&
Math.abs(projected.y) < 1.25;
label.element.style.display = visible ? "flex" : "none";
label.element.style.left =
`${(projected.x * 0.5 + 0.5) * width}px`;
label.element.style.top =
`${(-projected.y * 0.5 + 0.5) * height}px`;
}
}
// -------------------------------------------------------------------------
// Custom camera controls: no external controls module
// -------------------------------------------------------------------------
let dragging = false;
let previousX = 0;
let previousY = 0;
let activePointerId = null;
const canvas = renderer.domElement;
canvas.addEventListener("pointerdown", (event) => {
dragging = true;
activePointerId = event.pointerId;
previousX = event.clientX;
previousY = event.clientY;
canvas.setPointerCapture(event.pointerId);
});
canvas.addEventListener("pointermove", (event) => {
if (!dragging || event.pointerId !== activePointerId) return;
const deltaX = event.clientX - previousX;
const deltaY = event.clientY - previousY;
previousX = event.clientX;
previousY = event.clientY;
yaw -= deltaX * 0.006;
pitch = THREE.MathUtils.clamp(
pitch + deltaY * 0.005,
0.45,
1.16
);
updateCamera();
});
function stopDragging(event) {
if (event && event.pointerId !== activePointerId) return;
dragging = false;
activePointerId = null;
}
canvas.addEventListener("pointerup", stopDragging);
canvas.addEventListener("pointercancel", stopDragging);
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
camera.zoom = THREE.MathUtils.clamp(
camera.zoom * Math.exp(-event.deltaY * 0.001),
0.72,
2.15
);
camera.updateProjectionMatrix();
},
{ passive: false }
);
function resetView() {
yaw = defaultView.yaw;
pitch = defaultView.pitch;
camera.zoom = 1;
camera.updateProjectionMatrix();
updateCamera();
}
canvas.addEventListener("dblclick", resetView);
document.getElementById("zoomIn").addEventListener("click", () => {
camera.zoom = THREE.MathUtils.clamp(camera.zoom * 1.12, 0.72, 2.15);
camera.updateProjectionMatrix();
});
document.getElementById("zoomOut").addEventListener("click", () => {
camera.zoom = THREE.MathUtils.clamp(camera.zoom / 1.12, 0.72, 2.15);
camera.updateProjectionMatrix();
});
document.getElementById("resetView").addEventListener("click", resetView);
// -------------------------------------------------------------------------
// Responsive viewport
// -------------------------------------------------------------------------
function resize() {
const width = window.innerWidth;
const height = window.innerHeight;
const aspect = width / height;
// Keep the entire scene visible on narrow screens.
const viewHeight = Math.max(8.4, 8.6 / aspect);
camera.left = (-viewHeight * aspect) / 2;
camera.right = (viewHeight * aspect) / 2;
camera.top = viewHeight / 2;
camera.bottom = -viewHeight / 2;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(width, height);
}
window.addEventListener("resize", resize);
resize();
// -------------------------------------------------------------------------
// Render loop
// -------------------------------------------------------------------------
const clock = new THREE.Clock();
renderer.setAnimationLoop(() => {
const elapsed = clock.getElapsedTime();
// Gentle pulse emphasizes the shared zone without moving the geometry.
overlapHighlight.material.opacity =
0.38 + Math.sin(elapsed * 2.1) * 0.075;
renderer.render(scene, camera);
updateLabels();
});
</script>
</body>
</html>
```