Prompt specification
Build or customize the Three.js 3D scene "Home library rearrangement" 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: Home library rearrangement
Category: Architecture & Cities · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/home-library-rearrangement-x0231
Scene Description Prompt:
Make a standalone HTML scene with Three.js from cdnjs that depicts shelf supports and brackets. Build a reading wall with shelves, books, plants, and desk; move heavy book groups onto visibly well-supported lower spans while preserving clear access to the workspace. Stylized solid forms.
Working Three.js Starter (home-library-rearrangement-x0231):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Supported Reading Wall</title>
<style>
:root {
--ink: #22363b;
--muted: #627276;
--paper: #f3eee4;
--accent: #df6c43;
--teal: #2e6d70;
--line: rgba(34, 54, 59, 0.14);
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #dcd5c8;
color: var(--ink);
font-family: Inter, ui-sans-serif, system-ui, -apple-system,
BlinkMacSystemFont, "Segoe UI", sans-serif;
}
#scene {
position: fixed;
inset: 0;
}
canvas {
display: block;
width: 100%;
height: 100%;
outline: none;
touch-action: none;
cursor: grab;
}
canvas:active {
cursor: grabbing;
}
.hud {
position: fixed;
inset: 0;
z-index: 2;
pointer-events: none;
}
.title-card {
position: absolute;
top: 28px;
left: 30px;
max-width: 430px;
padding: 18px 20px 17px;
border: 1px solid rgba(255, 255, 255, 0.55);
border-radius: 18px;
background: rgba(247, 242, 233, 0.86);
box-shadow: 0 16px 45px rgba(51, 59, 55, 0.12);
backdrop-filter: blur(14px);
}
.eyebrow {
margin-bottom: 7px;
color: var(--accent);
font-size: 11px;
font-weight: 800;
letter-spacing: 0.18em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: clamp(25px, 3vw, 39px);
line-height: 1.02;
letter-spacing: -0.045em;
}
.subtitle {
max-width: 360px;
margin: 11px 0 0;
color: var(--muted);
font-size: 13px;
line-height: 1.5;
}
.legend {
position: absolute;
left: 30px;
bottom: 27px;
display: flex;
flex-wrap: wrap;
gap: 9px;
max-width: min(660px, calc(100vw - 60px));
padding: 11px 13px;
border: 1px solid rgba(255, 255, 255, 0.5);
border-radius: 15px;
background: rgba(247, 242, 233, 0.86);
box-shadow: 0 14px 38px rgba(51, 59, 55, 0.1);
backdrop-filter: blur(14px);
}
.legend-item {
display: flex;
align-items: center;
gap: 8px;
padding: 4px 7px;
color: #4c6064;
font-size: 11px;
font-weight: 650;
letter-spacing: 0.01em;
}
.swatch {
width: 10px;
height: 10px;
flex: 0 0 auto;
border-radius: 3px;
}
.swatch.bracket {
background: var(--accent);
}
.swatch.heavy {
background: #334e68;
}
.swatch.clear {
background: var(--teal);
}
.controls {
position: absolute;
right: 30px;
bottom: 27px;
display: flex;
align-items: center;
gap: 12px;
pointer-events: auto;
}
.hint {
padding: 10px 13px;
border: 1px solid rgba(255, 255, 255, 0.5);
border-radius: 13px;
background: rgba(247, 242, 233, 0.84);
color: var(--muted);
font-size: 11px;
backdrop-filter: blur(12px);
}
button {
height: 42px;
padding: 0 16px;
border: 0;
border-radius: 13px;
background: var(--ink);
color: #fffaf0;
font: inherit;
font-size: 12px;
font-weight: 750;
cursor: pointer;
box-shadow: 0 10px 24px rgba(34, 54, 59, 0.18);
transition: transform 150ms ease, background 150ms ease;
}
button:hover {
transform: translateY(-2px);
background: #31555a;
}
button:focus-visible,
canvas:focus-visible {
outline: 3px solid rgba(223, 108, 67, 0.55);
outline-offset: 3px;
}
#annotations {
position: absolute;
inset: 0;
overflow: hidden;
pointer-events: none;
}
.annotation {
position: absolute;
width: max-content;
max-width: 220px;
padding: 9px 11px;
border-left: 3px solid var(--accent);
border-radius: 0 10px 10px 0;
background: rgba(248, 244, 236, 0.92);
box-shadow: 0 10px 28px rgba(40, 50, 48, 0.13);
opacity: 0;
transform: translate(-50%, -115%);
transition: opacity 160ms ease;
backdrop-filter: blur(10px);
}
.annotation.visible {
opacity: 1;
}
.annotation.workspace {
border-left-color: var(--teal);
}
.annotation strong {
display: block;
margin-bottom: 2px;
font-size: 10px;
font-weight: 850;
letter-spacing: 0.1em;
text-transform: uppercase;
}
.annotation span {
display: block;
color: var(--muted);
font-size: 10px;
line-height: 1.35;
}
@media (max-width: 760px) {
.title-card {
top: 15px;
left: 15px;
right: 15px;
max-width: none;
padding: 14px 16px;
}
.subtitle {
display: none;
}
.legend {
left: 15px;
right: 15px;
bottom: 68px;
max-width: none;
gap: 4px;
padding: 8px;
}
.legend-item {
padding: 3px 4px;
font-size: 9px;
}
.controls {
right: 15px;
bottom: 15px;
}
.hint {
display: none;
}
.annotation {
display: none;
}
}
</style>
</head>
<body>
<div id="scene"></div>
<div class="hud">
<header class="title-card">
<div class="eyebrow">Shelf Logic / Structural Study</div>
<h1>The Supported<br>Reading Wall</h1>
<p class="subtitle">
Heavy volumes sit on low, bracketed spans. Lighter books rise above,
while the central desk remains open and easy to reach.
</p>
</header>
<div class="legend" aria-label="Scene legend">
<div class="legend-item">
<span class="swatch bracket"></span>
Triangular support brackets
</div>
<div class="legend-item">
<span class="swatch heavy"></span>
Heavy books kept low
</div>
<div class="legend-item">
<span class="swatch clear"></span>
Open workspace
</div>
</div>
<div class="controls">
<div class="hint">Drag to orbit · Scroll to zoom</div>
<button id="reset" type="button">Reset view</button>
</div>
<div id="annotations">
<div class="annotation" id="heavy-label">
<strong>Heavy book group</strong>
<span>Placed within a visibly bracketed lower span.</span>
</div>
<div class="annotation workspace" id="workspace-label">
<strong>Clear work zone</strong>
<span>No floor-level storage or overhanging book stacks.</span>
</div>
</div>
</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// ------------------------------------------------------------
// Renderer and scene
// ------------------------------------------------------------
const sceneContainer = document.querySelector("#scene");
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;
sceneContainer.appendChild(renderer.domElement);
renderer.domElement.tabIndex = 0;
renderer.domElement.setAttribute(
"aria-label",
"Interactive three-dimensional reading wall with shelves, brackets, books, plants, and a desk"
);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xdcd5c8);
scene.fog = new THREE.Fog(0xdcd5c8, 17, 29);
const camera = new THREE.PerspectiveCamera(
38,
window.innerWidth / window.innerHeight,
0.1,
60
);
const hemisphere = new THREE.HemisphereLight(0xfff8ea, 0x66706f, 2.25);
scene.add(hemisphere);
const sun = new THREE.DirectionalLight(0xfff3dd, 3.3);
sun.position.set(-4.5, 9, 7);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -9;
sun.shadow.camera.right = 9;
sun.shadow.camera.top = 10;
sun.shadow.camera.bottom = -3;
sun.shadow.camera.near = 0.5;
sun.shadow.camera.far = 25;
sun.shadow.normalBias = 0.025;
scene.add(sun);
const fill = new THREE.DirectionalLight(0xa9cbd0, 1.1);
fill.position.set(7, 5, 4);
scene.add(fill);
// ------------------------------------------------------------
// Materials
// ------------------------------------------------------------
const material = (color, options = {}) =>
new THREE.MeshStandardMaterial({
color,
roughness: 0.82,
metalness: 0,
...options
});
const mats = {
wall: material(0xe9e2d6, { roughness: 0.96 }),
floor: material(0xb6ad9f, { roughness: 0.96 }),
baseboard: material(0xeee8dd),
shelf: material(0xb97b51, { roughness: 0.7 }),
shelfEdge: material(0x915b3c, { roughness: 0.75 }),
post: material(0x30464a, { roughness: 0.55, metalness: 0.18 }),
bracket: material(0xdf6c43, { roughness: 0.48, metalness: 0.18 }),
desk: material(0xd2a16d, { roughness: 0.66 }),
deskDark: material(0x8f6545),
teal: material(0x2e6d70, { roughness: 0.66 }),
rug: material(0xb8c3b4, { roughness: 1 }),
cream: material(0xf1e8d8),
ink: material(0x28373b),
soil: material(0x493a2d),
leaf: material(0x4e8066, { roughness: 0.88 }),
leafLight: material(0x71966e, { roughness: 0.88 }),
pot: material(0xc86e4d, { roughness: 0.9 }),
potLight: material(0xd6b091, { roughness: 0.9 })
};
const panelMats = [
material(0xd6dfd8),
material(0xe5ded2),
material(0xd8e1e1),
material(0xe2d4c7)
];
const bookMats = [
material(0xcf5c4d),
material(0xdc9d4a),
material(0x3d7774),
material(0x3e5d85),
material(0x7b5672),
material(0xd0bc98),
material(0x72845a)
];
const heavyBookMats = [
material(0x2e455e),
material(0x394c54),
material(0x653f4d),
material(0x284f51),
material(0x514638)
];
// ------------------------------------------------------------
// Geometry helpers
// ------------------------------------------------------------
function box(width, height, depth, mat, x, y, z, parent = scene) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(width, height, depth),
mat
);
mesh.position.set(x, y, z);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function cylinder(radiusTop, radiusBottom, height, mat, x, y, z, parent = scene) {
const mesh = new THREE.Mesh(
new THREE.CylinderGeometry(radiusTop, radiusBottom, height, 20),
mat
);
mesh.position.set(x, y, z);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function rod(start, end, radius, mat, parent = scene) {
const direction = new THREE.Vector3().subVectors(end, start);
const midpoint = new THREE.Vector3()
.addVectors(start, end)
.multiplyScalar(0.5);
const mesh = new THREE.Mesh(
new THREE.CylinderGeometry(radius, radius, direction.length(), 12),
mat
);
mesh.position.copy(midpoint);
mesh.quaternion.setFromUnitVectors(
new THREE.Vector3(0, 1, 0),
direction.normalize()
);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
let seed = 73;
function random() {
seed = (seed * 1664525 + 1013904223) >>> 0;
return seed / 4294967296;
}
function randomBetween(min, max) {
return min + (max - min) * random();
}
// ------------------------------------------------------------
// Room
// ------------------------------------------------------------
box(14, 0.22, 8.4, mats.floor, 0, -0.12, 0.35);
box(13.4, 6.75, 0.24, mats.wall, 0, 3.375, -2.5);
box(13.3, 0.2, 0.16, mats.baseboard, 0, 0.1, -2.34);
// Quiet wall panels visually separate the structural bays.
const panelBays = [
{ left: -5.35, right: -2.72 },
{ left: -2.72, right: -0.55 },
{ left: -0.55, right: 2.85 },
{ left: 2.85, right: 5.35 }
];
panelBays.forEach((bay, index) => {
const width = bay.right - bay.left;
box(
width - 0.07,
6.12,
0.035,
panelMats[index],
(bay.left + bay.right) / 2,
3.08,
-2.365
);
});
// ------------------------------------------------------------
// Shelving frame and visible supports
// ------------------------------------------------------------
const standardX = [-5.35, -2.72, -0.55, 2.85, 5.35];
const upperLevels = [1.55, 2.68, 3.81, 4.94];
const lowerLevel = 0.52;
const shelfCenterZ = -1.76;
function addStandard(x) {
box(0.095, 5.65, 0.14, mats.post, x, 2.86, -2.16);
box(0.21, 0.075, 0.22, mats.post, x, 0.075, -2.16);
cylinder(0.065, 0.065, 0.04, mats.bracket, x, 5.69, -2.16);
}
function addBracket(x, levelY, drop = 0.48) {
// Horizontal arm under the shelf.
box(
0.13,
0.105,
0.72,
mats.bracket,
x,
levelY - 0.13,
-1.77
);
// Diagonal triangular brace.
rod(
new THREE.Vector3(x, levelY - drop, -2.11),
new THREE.Vector3(x, levelY - 0.18, -1.40),
0.043,
mats.bracket
);
// Visible triangular foot plate.
box(
0.20,
0.075,
0.19,
mats.bracket,
x,
levelY - drop,
-2.07
);
}
standardX.forEach(addStandard);
function addShelf(left, right, levelY) {
const width = right - left;
const center = (left + right) / 2;
box(width, 0.13, 0.72, mats.shelf, center, levelY, shelfCenterZ);
box(width, 0.105, 0.055, mats.shelfEdge, center, levelY, -1.375);
standardX.forEach((x) => {
if (x >= left - 0.05 && x <= right + 0.05) {
addBracket(x, levelY, levelY === lowerLevel ? 0.39 : 0.52);
}
});
}
// Continuous upper shelves.
upperLevels.forEach((level) => {
addShelf(-5.5, 5.5, level);
});
// Split lower shelves leave the desk bay open and accessible.
addShelf(-5.5, -0.55, lowerLevel);
addShelf(2.85, 5.5, lowerLevel);
// Small shelf-index plaques make the structural bays legible.
standardX.forEach((x) => {
box(0.15, 0.16, 0.035, mats.bracket, x, 0.25, -2.07);
});
// ------------------------------------------------------------
// Books
// ------------------------------------------------------------
function addBookGroup(
centerX,
shelfY,
count,
{
minHeight = 0.4,
maxHeight = 0.67,
minThickness = 0.11,
maxThickness = 0.19,
heavy = false,
z = -1.58
} = {}
) {
const available = heavy ? heavyBookMats : bookMats;
const widths = [];
let totalWidth = 0;
for (let i = 0; i < count; i++) {
const thickness = randomBetween(minThickness, maxThickness);
widths.push(thickness);
totalWidth += thickness + 0.025;
}
let cursor = centerX - totalWidth / 2;
widths.forEach((thickness, index) => {
const height = randomBetween(minHeight, maxHeight);
const mat = available[index % available.length];
const x = cursor + thickness / 2;
const bottom = shelfY + 0.075;
const centerY = bottom + height / 2;
box(thickness, height, 0.39, mat, x, centerY, z);
// Spine label and bands.
const labelMat = heavy ? mats.cream : mats.ink;
const bandMat = heavy ? mats.bracket : mats.cream;
box(
thickness * 0.62,
0.025,
0.025,
bandMat,
x,
bottom + height * 0.17,
z + 0.207
);
box(
thickness * 0.62,
0.025,
0.025,
bandMat,
x,
bottom + height * 0.83,
z + 0.207
);
box(
thickness * 0.47,
Math.min(0.13, height * 0.2),
0.025,
labelMat,
x,
bottom + height * 0.53,
z + 0.208
);
cursor += thickness + 0.025;
});
return totalWidth;
}
function addBookends(centerX, shelfY, totalWidth) {
const left = centerX - totalWidth / 2 - 0.055;
const right = centerX + totalWidth / 2 + 0.055;
box(0.055, 0.42, 0.48, mats.bracket, left, shelfY + 0.29, -1.57);
box(0.055, 0.42, 0.48, mats.bracket, right, shelfY + 0.29, -1.57);
box(
totalWidth + 0.11,
0.045,
0.48,
mats.bracket,
centerX,
shelfY + 0.095,
-1.57
);
}
function addHorizontalStack(centerX, shelfY, count = 3) {
let y = shelfY + 0.09;
for (let i = 0; i < count; i++) {
const width = randomBetween(0.52, 0.73);
const depth = randomBetween(0.31, 0.39);
const height = randomBetween(0.08, 0.12);
const mat = bookMats[(i + 2) % bookMats.length];
const book = box(
width,
height,
depth,
mat,
centerX + randomBetween(-0.05, 0.05),
y + height / 2,
-1.58
);
book.rotation.y = randomBetween(-0.08, 0.08);
y += height;
}
}
// Heavy books occupy the two braced lower outer bays.
const leftHeavyWidth = addBookGroup(-4.12, lowerLevel, 9, {
minHeight: 0.64,
maxHeight: 0.79,
minThickness: 0.19,
maxThickness: 0.27,
heavy: true
});
addBookends(-4.12, lowerLevel, leftHeavyWidth);
const rightHeavyWidth = addBookGroup(4.15, lowerLevel, 8, {
minHeight: 0.62,
maxHeight: 0.78,
minThickness: 0.18,
maxThickness: 0.26,
heavy: true
});
addBookends(4.15, lowerLevel, rightHeavyWidth);
// Lighter books are distributed away from the central work zone.
addBookGroup(-4.23, 1.55, 7, {
minHeight: 0.43,
maxHeight: 0.66
});
addBookGroup(4.12, 1.55, 6, {
minHeight: 0.45,
maxHeight: 0.64
});
addBookGroup(-3.36, 2.68, 7, {
minHeight: 0.42,
maxHeight: 0.62
});
addBookGroup(4.03, 2.68, 6, {
minHeight: 0.40,
maxHeight: 0.60
});
addBookGroup(-4.22, 3.81, 6, {
minHeight: 0.38,
maxHeight: 0.58
});
addBookGroup(3.63, 3.81, 5, {
minHeight: 0.4,
maxHeight: 0.61
});
addBookGroup(-4.65, 4.94, 5, {
minHeight: 0.34,
maxHeight: 0.52
});
addHorizontalStack(0.55, 4.94, 3);
addHorizontalStack(3.05, 4.94, 4);
// ------------------------------------------------------------
// Plants
// ------------------------------------------------------------
function addPlant(x, y, z, scale = 1, lightPot = false) {
const group = new THREE.Group();
group.position.set(x, y, z);
group.scale.setScalar(scale);
scene.add(group);
const potMat = lightPot ? mats.potLight : mats.pot;
cylinder(
0.24,
0.17,
0.34,
potMat,
0,
0.17,
0,
group
);
cylinder(0.225, 0.225, 0.025, mats.soil, 0, 0.34, 0, group);
const stemTop = new THREE.Vector3(0.02, 0.83, 0);
rod(
new THREE.Vector3(0, 0.32, 0),
stemTop,
0.022,
mats.leaf,
group
);
const leafGeometry = new THREE.SphereGeometry(1, 14, 10);
const leaves = [
[-0.18, 0.52, 0.03, -0.52, 0.8],
[0.20, 0.61, 0.02, 0.55, 0.9],
[-0.15, 0.74, -0.01, -0.48, 0.72],
[0.17, 0.82, 0.00, 0.48, 0.75],
[-0.04, 0.95, 0.02, -0.05, 0.62]
];
leaves.forEach((leaf, index) => {
const mesh = new THREE.Mesh(
leafGeometry,
index % 2 ? mats.leafLight : mats.leaf
);
mesh.position.set(leaf[0], leaf[1], leaf[2]);
mesh.scale.set(0.13, leaf[4], 0.065);
mesh.rotation.z = leaf[3];
mesh.rotation.y = index * 0.72;
mesh.castShadow = true;
mesh.receiveShadow = true;
group.add(mesh);
});
return group;
}
addPlant(-5.0, 2.755, -1.66, 0.78, true);
addPlant(-1.32, 4.995, -1.66, 0.94);
addPlant(4.68, 3.885, -1.66, 0.86, true);
addPlant(-0.85, 1.625, -1.67, 0.66);
addPlant(2.48, 1.625, -1.67, 0.62, true);
// ------------------------------------------------------------
// Desk and chair
// ------------------------------------------------------------
const deskCenterX = 1.15;
const deskTopY = 0.86;
// Soft rug under the work area.
box(3.85, 0.018, 2.55, mats.rug, deskCenterX, 0.012, -0.15);
box(3.2, 0.14, 1.43, mats.desk, deskCenterX, deskTopY, -1.08);
box(3.03, 0.065, 0.055, mats.deskDark, deskCenterX, 0.82, -0.365);
// Slender side legs preserve knee space.
box(0.13, 0.72, 1.22, mats.teal, -0.34, 0.43, -1.13);
box(0.13, 0.72, 1.22, mats.teal, 2.64, 0.43, -1.13);
// Low rear brace.
box(2.78, 0.10, 0.10, mats.teal, deskCenterX, 0.26, -1.69);
// Chair.
const chair = new THREE.Group();
chair.position.set(1.13, 0, 0.15);
scene.add(chair);
box(0.78, 0.12, 0.69, mats.teal, 0, 0.49, 0, chair);
box(0.72, 0.56, 0.12, mats.teal, 0, 0.84, 0.34, chair);
const chairBack = chair.children[1];
chairBack.rotation.x = -0.08;
[-0.29, 0.29].forEach((x) => {
[-0.24, 0.24].forEach((z) => {
rod(
new THREE.Vector3(x, 0.43, z),
new THREE.Vector3(x * 1.18, 0.04, z * 1.22),
0.035,
mats.post,
chair
);
});
});
// A notebook and pen leave most of the desktop open.
box(0.58, 0.035, 0.40, mats.cream, 0.30, 0.947, -0.70);
box(0.022, 0.04, 0.40, mats.ink, 0.03, 0.953, -0.70);
rod(
new THREE.Vector3(0.48, 0.977, -0.60),
new THREE.Vector3(0.72, 0.977, -0.82),
0.018,
mats.bracket
);
// Desktop task lamp.
const lamp = new THREE.Group();
lamp.position.set(2.35, 0.94, -1.44);
scene.add(lamp);
cylinder(0.16, 0.18, 0.055, mats.post, 0, 0.03, 0, lamp);
rod(
new THREE.Vector3(0, 0.06, 0),
new THREE.Vector3(-0.08, 0.43, 0.02),
0.026,
mats.post,
lamp
);
rod(
new THREE.Vector3(-0.08, 0.43, 0.02),
new THREE.Vector3(-0.36, 0.36, 0.17),
0.025,
mats.post,
lamp
);
const shade = new THREE.Mesh(
new THREE.CylinderGeometry(0.07, 0.18, 0.22, 20),
mats.bracket
);
shade.position.set(-0.38, 0.30, 0.18);
shade.castShadow = true;
lamp.add(shade);
const bulb = new THREE.PointLight(0xffd59a, 1.4, 2.2);
bulb.position.set(-0.38, 0.19, 0.18);
lamp.add(bulb);
// Small plant at the desk edge.
addPlant(2.47, 0.94, -1.70, 0.42, true);
// ------------------------------------------------------------
// Custom orbit camera controls
// ------------------------------------------------------------
const orbit = {
target: new THREE.Vector3(0, 2.35, -0.45),
theta: 0.08,
phi: 1.13,
radius: 12.1,
desiredTheta: 0.08,
desiredPhi: 1.13,
desiredRadius: 12.1
};
function updateCamera(delta = 1) {
const damping = 1 - Math.pow(0.0008, delta);
orbit.theta +=
(orbit.desiredTheta - orbit.theta) * damping;
orbit.phi +=
(orbit.desiredPhi - orbit.phi) * damping;
orbit.radius +=
(orbit.desiredRadius - orbit.radius) * damping;
const sinPhi = Math.sin(orbit.phi);
camera.position.set(
orbit.target.x + orbit.radius * sinPhi * Math.sin(orbit.theta),
orbit.target.y + orbit.radius * Math.cos(orbit.phi),
orbit.target.z + orbit.radius * sinPhi * Math.cos(orbit.theta)
);
camera.lookAt(orbit.target);
}
let dragging = false;
let pointerX = 0;
let pointerY = 0;
renderer.domElement.addEventListener("pointerdown", (event) => {
dragging = true;
pointerX = event.clientX;
pointerY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!dragging) return;
const dx = event.clientX - pointerX;
const dy = event.clientY - pointerY;
pointerX = event.clientX;
pointerY = event.clientY;
orbit.desiredTheta = THREE.MathUtils.clamp(
orbit.desiredTheta - dx * 0.005,
-0.68,
0.68
);
orbit.desiredPhi = THREE.MathUtils.clamp(
orbit.desiredPhi - dy * 0.004,
0.78,
1.43
);
});
function endDrag(event) {
dragging = false;
if (
event.pointerId !== undefined &&
renderer.domElement.hasPointerCapture(event.pointerId)
) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
}
renderer.domElement.addEventListener("pointerup", endDrag);
renderer.domElement.addEventListener("pointercancel", endDrag);
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
orbit.desiredRadius = THREE.MathUtils.clamp(
orbit.desiredRadius * Math.exp(event.deltaY * 0.001),
8.3,
18.5
);
},
{ passive: false }
);
function resetView() {
orbit.desiredTheta = 0.08;
orbit.desiredPhi = 1.13;
orbit.desiredRadius = window.innerWidth < 700 ? 14.2 : 12.1;
}
document.querySelector("#reset").addEventListener("click", resetView);
renderer.domElement.addEventListener("dblclick", resetView);
renderer.domElement.addEventListener("keydown", (event) => {
const step = 0.08;
if (event.key === "ArrowLeft") {
orbit.desiredTheta = THREE.MathUtils.clamp(
orbit.desiredTheta - step,
-0.68,
0.68
);
}
if (event.key === "ArrowRight") {
orbit.desiredTheta = THREE.MathUtils.clamp(
orbit.desiredTheta + step,
-0.68,
0.68
);
}
if (event.key === "ArrowUp") {
orbit.desiredPhi = THREE.MathUtils.clamp(
orbit.desiredPhi - step,
0.78,
1.43
);
}
if (event.key === "ArrowDown") {
orbit.desiredPhi = THREE.MathUtils.clamp(
orbit.desiredPhi + step,
0.78,
1.43
);
}
if (event.key === "+" || event.key === "=") {
orbit.desiredRadius = Math.max(8.3, orbit.desiredRadius - 0.6);
}
if (event.key === "-") {
orbit.desiredRadius = Math.min(18.5, orbit.desiredRadius + 0.6);
}
});
// ------------------------------------------------------------
// Projected annotations
// ------------------------------------------------------------
const annotations = [
{
element: document.querySelector("#heavy-label"),
point: new THREE.Vector3(-4.15, 1.27, -1.15)
},
{
element: document.querySelector("#workspace-label"),
point: new THREE.Vector3(1.13, 1.18, -0.38)
}
];
function updateAnnotations() {
annotations.forEach((annotation) => {
const projected = annotation.point.clone().project(camera);
const visible =
projected.z > -1 &&
projected.z < 1 &&
Math.abs(projected.x) < 1.08 &&
Math.abs(projected.y) < 1.08;
annotation.element.classList.toggle("visible", visible);
if (!visible) return;
const x = (projected.x * 0.5 + 0.5) * window.innerWidth;
const y = (-projected.y * 0.5 + 0.5) * window.innerHeight;
annotation.element.style.left = `${x}px`;
annotation.element.style.top = `${y}px`;
});
}
// ------------------------------------------------------------
// Resize and animation
// ------------------------------------------------------------
function resize() {
const width = window.innerWidth;
const height = window.innerHeight;
const aspect = width / height;
camera.aspect = aspect;
camera.fov = aspect < 0.8 ? 58 : aspect < 1.2 ? 47 : 38;
camera.updateProjectionMatrix();
renderer.setSize(width, height);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
}
window.addEventListener("resize", resize);
resize();
if (window.innerWidth < 700) {
orbit.radius = 14.2;
orbit.desiredRadius = 14.2;
orbit.phi = 1.17;
orbit.desiredPhi = 1.17;
}
const clock = new THREE.Clock();
function animate() {
requestAnimationFrame(animate);
const delta = Math.min(clock.getDelta(), 0.05);
updateCamera(delta);
updateAnnotations();
renderer.render(scene, camera);
}
updateCamera(1);
animate();
</script>
</body>
</html>
```