Prompt specification
Build or customize the Three.js 3D scene "Book exchange shelf labels" 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: Book exchange shelf labels
Category: Architecture & Cities · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/book-exchange-shelf-labels-x0489
Scene Description Prompt:
Model a small outdoor book cabinet with distinct return and browse shelves, raised book spines, and a weather cover; keep its circulation labels easy to read. Use Three.js from cdnjs in a single HTML file, with stylized solid shapes.
Working Three.js Starter (book-exchange-shelf-labels-x0489):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Outdoor Community Book Cabinet</title>
<style>
:root {
--ink: #153c3a;
--cream: #fff8e8;
--orange: #e96f45;
--teal: #247d75;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #dcebe8;
color: var(--ink);
font-family: Inter, ui-sans-serif, system-ui, -apple-system,
BlinkMacSystemFont, "Segoe UI", sans-serif;
}
#scene {
position: fixed;
inset: 0;
display: block;
touch-action: none;
cursor: grab;
}
#scene.dragging {
cursor: grabbing;
}
.hud {
position: fixed;
z-index: 10;
top: 22px;
left: 22px;
width: min(340px, calc(100vw - 44px));
padding: 18px 20px 16px;
border: 1px solid rgba(21, 60, 58, 0.12);
border-radius: 18px;
background: rgba(255, 248, 232, 0.91);
box-shadow: 0 16px 42px rgba(27, 67, 61, 0.15);
backdrop-filter: blur(10px);
user-select: none;
}
.eyebrow {
margin-bottom: 6px;
color: var(--orange);
font-size: 11px;
font-weight: 850;
letter-spacing: 0.17em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: clamp(22px, 3vw, 30px);
line-height: 1.05;
letter-spacing: -0.035em;
}
.subtitle {
margin: 9px 0 15px;
font-size: 13px;
line-height: 1.45;
color: #4c6864;
}
.legend {
display: flex;
flex-wrap: wrap;
gap: 8px;
margin-bottom: 15px;
}
.key {
display: inline-flex;
align-items: center;
gap: 7px;
padding: 7px 9px;
border-radius: 999px;
background: #e5eee7;
font-size: 11px;
font-weight: 800;
letter-spacing: 0.04em;
text-transform: uppercase;
}
.dot {
width: 9px;
height: 9px;
border-radius: 50%;
background: var(--teal);
}
.dot.return {
background: var(--orange);
}
.actions {
display: flex;
gap: 8px;
}
button {
appearance: none;
border: 0;
border-radius: 10px;
padding: 9px 12px;
background: var(--ink);
color: var(--cream);
font: inherit;
font-size: 12px;
font-weight: 800;
cursor: pointer;
transition: transform 120ms ease, background 120ms ease;
}
button:hover {
background: var(--teal);
transform: translateY(-1px);
}
button.secondary {
background: #d9e6df;
color: var(--ink);
}
.help {
position: fixed;
z-index: 10;
right: 20px;
bottom: 18px;
padding: 9px 13px;
border-radius: 999px;
background: rgba(21, 60, 58, 0.86);
color: var(--cream);
font-size: 11px;
font-weight: 700;
letter-spacing: 0.04em;
box-shadow: 0 8px 24px rgba(21, 60, 58, 0.18);
pointer-events: none;
}
#loading {
position: fixed;
z-index: 100;
inset: 0;
display: grid;
place-items: center;
background: #dcebe8;
color: var(--ink);
font-weight: 850;
letter-spacing: 0.08em;
text-transform: uppercase;
}
#loading.hidden {
display: none;
}
@media (max-width: 640px) {
.hud {
top: 12px;
left: 12px;
width: calc(100vw - 24px);
padding: 14px 16px;
}
.subtitle {
margin-bottom: 10px;
}
.legend {
margin-bottom: 10px;
}
.help {
right: 10px;
bottom: 10px;
left: 10px;
text-align: center;
}
}
</style>
</head>
<body>
<canvas id="scene" aria-label="Interactive 3D outdoor book cabinet"></canvas>
<section class="hud">
<div class="eyebrow">Community Exchange</div>
<h1>Outdoor Book Cabinet</h1>
<p class="subtitle">
A weather-ready little library with separate return and browse zones.
</p>
<div class="legend" aria-hidden="true">
<span class="key"><span class="dot return"></span>Return slot</span>
<span class="key"><span class="dot"></span>Browse shelves</span>
</div>
<div class="actions">
<button id="reset" type="button">Reset view</button>
<button id="front" class="secondary" type="button">Front view</button>
</div>
</section>
<div class="help">Drag to orbit · Wheel or pinch to zoom · Shift-drag to pan</div>
<div id="loading">Building the book cabinet…</div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// ------------------------------------------------------------
// Renderer, camera, and scene
// ------------------------------------------------------------
const canvas = document.querySelector("#scene");
const loading = document.querySelector("#loading");
const renderer = new THREE.WebGLRenderer({
canvas,
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.08;
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xdcebe8);
scene.fog = new THREE.Fog(0xdcebe8, 17, 34);
const camera = new THREE.PerspectiveCamera(
38,
window.innerWidth / window.innerHeight,
0.1,
80
);
const controls = {
target: new THREE.Vector3(0, 2.15, 0),
theta: 0.34,
phi: 1.12,
radius: 11.5,
minRadius: 7,
maxRadius: 18
};
function updateCamera() {
const sinPhi = Math.sin(controls.phi);
camera.position.set(
controls.target.x + controls.radius * sinPhi * Math.sin(controls.theta),
controls.target.y + controls.radius * Math.cos(controls.phi),
controls.target.z + controls.radius * sinPhi * Math.cos(controls.theta)
);
camera.lookAt(controls.target);
}
updateCamera();
const hemisphere = new THREE.HemisphereLight(0xf6fff9, 0x789078, 2.15);
scene.add(hemisphere);
const sun = new THREE.DirectionalLight(0xfff1d4, 3.3);
sun.position.set(-6, 11, 8);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -12;
sun.shadow.camera.right = 12;
sun.shadow.camera.top = 12;
sun.shadow.camera.bottom = -10;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 35;
sun.shadow.bias = -0.0004;
scene.add(sun);
const fill = new THREE.DirectionalLight(0xb8d9dc, 0.8);
fill.position.set(8, 5, -6);
scene.add(fill);
// ------------------------------------------------------------
// Reusable geometry and material helpers
// ------------------------------------------------------------
const materials = {
wood: new THREE.MeshStandardMaterial({
color: 0xb97742,
roughness: 0.78
}),
woodLight: new THREE.MeshStandardMaterial({
color: 0xd9a566,
roughness: 0.72
}),
woodDark: new THREE.MeshStandardMaterial({
color: 0x70462f,
roughness: 0.85
}),
teal: new THREE.MeshStandardMaterial({
color: 0x247d75,
roughness: 0.56,
metalness: 0.08
}),
tealDark: new THREE.MeshStandardMaterial({
color: 0x185753,
roughness: 0.65
}),
cream: new THREE.MeshStandardMaterial({
color: 0xfff3d5,
roughness: 0.9
}),
charcoal: new THREE.MeshStandardMaterial({
color: 0x17302f,
roughness: 0.84
}),
concrete: new THREE.MeshStandardMaterial({
color: 0xc7c5b8,
roughness: 0.95
}),
grass: new THREE.MeshStandardMaterial({
color: 0x83aa72,
roughness: 1
}),
path: new THREE.MeshStandardMaterial({
color: 0xd8c9a7,
roughness: 1
}),
leaf: new THREE.MeshStandardMaterial({
color: 0x4f8661,
roughness: 1
}),
leafLight: new THREE.MeshStandardMaterial({
color: 0x75a66d,
roughness: 1
}),
trunk: new THREE.MeshStandardMaterial({
color: 0x76503a,
roughness: 1
})
};
function box(
parent,
size,
position,
material,
castShadow = true,
receiveShadow = true
) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(size[0], size[1], size[2]),
material
);
mesh.position.set(position[0], position[1], position[2]);
mesh.castShadow = castShadow;
mesh.receiveShadow = receiveShadow;
parent.add(mesh);
return mesh;
}
function cylinderBetween(
parent,
start,
end,
radius,
material,
radialSegments = 12
) {
const a = new THREE.Vector3(...start);
const b = new THREE.Vector3(...end);
const direction = b.clone().sub(a);
const length = direction.length();
const mesh = new THREE.Mesh(
new THREE.CylinderGeometry(radius, radius, length, radialSegments),
material
);
mesh.position.copy(a).add(b).multiplyScalar(0.5);
mesh.quaternion.setFromUnitVectors(
new THREE.Vector3(0, 1, 0),
direction.normalize()
);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
// ------------------------------------------------------------
// High-contrast canvas signs
// ------------------------------------------------------------
function roundedRect(ctx, x, y, width, height, radius) {
const r = Math.min(radius, width / 2, height / 2);
ctx.beginPath();
ctx.moveTo(x + r, y);
ctx.arcTo(x + width, y, x + width, y + height, r);
ctx.arcTo(x + width, y + height, x, y + height, r);
ctx.arcTo(x, y + height, x, y, r);
ctx.arcTo(x, y, x + width, y, r);
ctx.closePath();
}
function createLabelTexture(title, subtitle, background, foreground, accent) {
const labelCanvas = document.createElement("canvas");
labelCanvas.width = 1024;
labelCanvas.height = 256;
const ctx = labelCanvas.getContext("2d");
ctx.clearRect(0, 0, 1024, 256);
roundedRect(ctx, 6, 6, 1012, 244, 28);
ctx.fillStyle = background;
ctx.fill();
roundedRect(ctx, 7, 7, 1010, 242, 27);
ctx.strokeStyle = accent;
ctx.lineWidth = 7;
ctx.stroke();
ctx.fillStyle = accent;
ctx.fillRect(28, 30, 14, 196);
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.fillStyle = foreground;
let titleSize = subtitle ? 70 : 82;
ctx.font = `900 ${titleSize}px system-ui, sans-serif`;
while (ctx.measureText(title).width > 900 && titleSize > 36) {
titleSize -= 2;
ctx.font = `900 ${titleSize}px system-ui, sans-serif`;
}
ctx.fillText(title, 525, subtitle ? 99 : 128);
if (subtitle) {
ctx.font = "700 30px system-ui, sans-serif";
ctx.fillStyle = accent;
ctx.fillText(subtitle, 525, 178);
}
const texture = new THREE.CanvasTexture(labelCanvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.anisotropy = renderer.capabilities.getMaxAnisotropy();
return texture;
}
function addSign(
parent,
title,
subtitle,
position,
size,
background = "#153c3a",
foreground = "#fff8e8",
accent = "#e96f45"
) {
const group = new THREE.Group();
group.position.set(position[0], position[1], position[2]);
box(
group,
[size[0], size[1], 0.11],
[0, 0, 0],
materials.charcoal,
true,
false
);
const texture = createLabelTexture(
title,
subtitle,
background,
foreground,
accent
);
const face = new THREE.Mesh(
new THREE.PlaneGeometry(size[0] - 0.035, size[1] - 0.035),
new THREE.MeshBasicMaterial({
map: texture,
toneMapped: false
})
);
face.position.z = 0.057;
face.renderOrder = 5;
group.add(face);
parent.add(group);
return group;
}
// ------------------------------------------------------------
// Books with solid covers, pages, and raised spine details
// ------------------------------------------------------------
const bookPalette = [
0xe96f45,
0x247d75,
0xe5b743,
0x455f91,
0x8c4e68,
0xcb5148,
0x3e7c70,
0x6f7651
];
function createBook(thickness, height, depth, color, titleMark = true) {
const group = new THREE.Group();
const coverMaterial = new THREE.MeshStandardMaterial({
color,
roughness: 0.64
});
const pagesMaterial = new THREE.MeshStandardMaterial({
color: 0xf1e4c5,
roughness: 0.95
});
// Page block.
box(
group,
[thickness * 0.72, height * 0.91, depth * 0.86],
[0, height * 0.49, -0.015],
pagesMaterial
);
// Two rigid covers.
box(
group,
[thickness * 0.09, height, depth],
[-thickness * 0.44, height * 0.5, 0],
coverMaterial
);
box(
group,
[thickness * 0.09, height, depth],
[thickness * 0.44, height * 0.5, 0],
coverMaterial
);
// Raised front spine, slightly proud of the page block.
box(
group,
[thickness * 0.94, height * 0.98, 0.075],
[0, height * 0.5, depth * 0.45],
coverMaterial
);
// Raised spine bands.
box(
group,
[thickness * 0.82, 0.035, 0.028],
[0, height * 0.19, depth * 0.45 + 0.05],
materials.cream
);
box(
group,
[thickness * 0.82, 0.035, 0.028],
[0, height * 0.79, depth * 0.45 + 0.05],
materials.cream
);
// Small raised title patch and top tab.
if (titleMark) {
box(
group,
[thickness * 0.62, height * 0.12, 0.025],
[0, height * 0.57, depth * 0.45 + 0.05],
materials.cream
);
box(
group,
[thickness * 0.34, 0.045, 0.035],
[0, height * 0.88, depth * 0.45 + 0.06],
coverMaterial
);
}
return group;
}
// ------------------------------------------------------------
// Outdoor setting
// ------------------------------------------------------------
const environment = new THREE.Group();
scene.add(environment);
box(
environment,
[40, 0.12, 40],
[0, -0.09, 0],
materials.grass,
false,
true
);
box(
environment,
[8.2, 0.08, 11.5],
[0, 0.01, 6.0],
materials.path,
false,
true
);
box(
environment,
[7.25, 0.24, 2.35],
[0, 0.12, 0.04],
materials.concrete,
false,
true
);
// Stylized tree.
const tree = new THREE.Group();
tree.position.set(5.8, 0, -3.8);
environment.add(tree);
cylinderBetween(
tree,
[0, 0, 0],
[0.1, 3.0, 0],
0.18,
materials.trunk,
10
);
const treeCrown = new THREE.Mesh(
new THREE.IcosahedronGeometry(1.55, 1),
materials.leaf
);
treeCrown.position.set(0.1, 3.25, 0);
treeCrown.scale.set(1.1, 1.25, 0.9);
treeCrown.castShadow = true;
tree.add(treeCrown);
const treeCrown2 = new THREE.Mesh(
new THREE.IcosahedronGeometry(1.1, 1),
materials.leafLight
);
treeCrown2.position.set(-0.8, 2.75, 0.25);
treeCrown2.castShadow = true;
tree.add(treeCrown2);
// Low shrubs.
function addShrub(x, z, scale = 1) {
const shrub = new THREE.Group();
shrub.position.set(x, 0, z);
shrub.scale.setScalar(scale);
environment.add(shrub);
[
[-0.55, 0.48, 0.05, 0.72],
[0.2, 0.65, 0, 0.88],
[0.75, 0.42, 0.1, 0.66]
].forEach(([px, py, pz, radius], index) => {
const crown = new THREE.Mesh(
new THREE.IcosahedronGeometry(radius, 1),
index === 1 ? materials.leafLight : materials.leaf
);
crown.position.set(px, py, pz);
crown.castShadow = true;
crown.receiveShadow = true;
shrub.add(crown);
});
}
addShrub(-4.5, -2.7, 1.1);
addShrub(-3.4, -3.5, 0.8);
addShrub(3.8, -4.2, 0.9);
// Small flowers near the path.
const flowerStemMaterial = new THREE.MeshStandardMaterial({
color: 0x477f4e,
roughness: 1
});
const flowerColors = [0xf1bf48, 0xe96f45, 0xf3eee0];
[
[-3.6, 2.0], [-3.2, 3.1], [3.5, 2.3], [3.8, 3.4],
[-2.8, 4.2], [2.9, 4.5], [4.0, 1.7], [-4.0, 4.0]
].forEach(([x, z], index) => {
const flower = new THREE.Group();
flower.position.set(x, 0, z);
environment.add(flower);
cylinderBetween(
flower,
[0, 0, 0],
[0, 0.42, 0],
0.018,
flowerStemMaterial,
6
);
const bloom = new THREE.Mesh(
new THREE.IcosahedronGeometry(0.105, 1),
new THREE.MeshStandardMaterial({
color: flowerColors[index % flowerColors.length],
roughness: 0.8
})
);
bloom.position.y = 0.46;
bloom.castShadow = true;
flower.add(bloom);
});
// ------------------------------------------------------------
// Main cabinet
// ------------------------------------------------------------
const cabinet = new THREE.Group();
cabinet.position.z = 0;
scene.add(cabinet);
// Back and structural frame.
box(
cabinet,
[5.72, 3.88, 0.14],
[0, 2.12, -0.62],
materials.woodDark
);
box(
cabinet,
[0.17, 3.88, 1.28],
[-2.86, 2.12, 0],
materials.wood
);
box(
cabinet,
[0.17, 3.88, 1.28],
[2.86, 2.12, 0],
materials.wood
);
box(
cabinet,
[5.86, 0.22, 1.34],
[0, 0.44, 0],
materials.woodDark
);
box(
cabinet,
[5.86, 0.22, 1.34],
[0, 4.05, 0],
materials.woodDark
);
// Divider between return and browse areas.
box(
cabinet,
[0.16, 3.48, 1.26],
[-1.0, 2.25, 0],
materials.wood
);
// Accent inner trim around the browse opening.
box(
cabinet,
[3.72, 0.12, 0.12],
[0.9, 3.98, 0.65],
materials.woodLight
);
// ------------------------------------------------------------
// Return zone: recessed chute, horizontal slot, and tray
// ------------------------------------------------------------
const returnZone = new THREE.Group();
cabinet.add(returnZone);
box(
returnZone,
[1.56, 2.68, 0.13],
[-1.93, 2.02, 0.18],
materials.charcoal
);
// Recessed return opening.
box(
returnZone,
[1.36, 0.18, 0.07],
[-1.93, 2.49, 0.58],
materials.charcoal,
false,
false
);
box(
returnZone,
[1.49, 0.13, 0.18],
[-1.93, 2.66, 0.60],
materials.woodLight
);
box(
returnZone,
[1.49, 0.13, 0.18],
[-1.93, 2.32, 0.60],
materials.woodLight
);
// Projecting return tray.
box(
returnZone,
[1.61, 0.13, 1.02],
[-1.93, 1.02, 0.43],
materials.woodLight
);
box(
returnZone,
[1.61, 0.23, 0.10],
[-1.93, 1.13, 0.91],
materials.wood
);
// Book currently being returned.
const returnBook = createBook(0.25, 0.84, 0.54, 0xe96f45);
returnBook.position.set(-2.17, 1.10, 0.70);
returnBook.rotation.z = -0.29;
returnBook.rotation.y = -0.08;
returnZone.add(returnBook);
addSign(
returnZone,
"RETURN BOOKS",
"SLIDE ONE AT A TIME",
[-1.93, 3.26, 0.69],
[1.66, 0.48],
"#153c3a",
"#fff8e8",
"#f18a56"
);
// ------------------------------------------------------------
// Browse zone: three solid shelves with dimensional books
// ------------------------------------------------------------
const shelfLevels = [1.00, 2.05, 3.10];
const shelfLabels = ["FICTION", "LOCAL AUTHORS", "NEW + NOTED"];
shelfLevels.forEach((shelfY, rowIndex) => {
box(
cabinet,
[3.72, 0.13, 1.17],
[0.90, shelfY, 0],
materials.woodLight
);
// Front shelf lip.
box(
cabinet,
[3.72, 0.15, 0.10],
[0.90, shelfY, 0.61],
materials.wood
);
addSign(
cabinet,
shelfLabels[rowIndex],
"",
[0.90, shelfY, 0.685],
[1.42, 0.145],
"#fff8e8",
"#153c3a",
"#e96f45"
);
let x = -0.57;
let bookIndex = 0;
while (x < 2.48) {
const thickness =
0.22 + ((rowIndex * 3 + bookIndex * 2) % 5) * 0.025;
let height;
if (rowIndex === 2) {
height = 0.56 + ((bookIndex + rowIndex) % 4) * 0.025;
} else {
height = 0.66 + ((bookIndex * 2 + rowIndex) % 5) * 0.035;
}
const depth = 0.54 + ((bookIndex + 1) % 3) * 0.025;
const color = bookPalette[
(bookIndex + rowIndex * 2) % bookPalette.length
];
const book = createBook(thickness, height, depth, color);
const pulledForward = bookIndex === 5 && rowIndex !== 1;
book.position.set(
x + thickness / 2,
shelfY + 0.07,
pulledForward ? 0.34 : 0.22 + ((bookIndex % 2) * 0.035)
);
if (bookIndex === 6 && rowIndex !== 2) {
book.rotation.z = 0.07;
} else if (bookIndex === 2 && rowIndex === 1) {
book.rotation.z = -0.055;
}
cabinet.add(book);
x += thickness + 0.075;
bookIndex += 1;
}
});
addSign(
cabinet,
"BROWSE + BORROW",
"CHOOSE SOMETHING TO READ",
[0.91, 3.91, 0.68],
[3.48, 0.40],
"#247d75",
"#fff8e8",
"#f5c75b"
);
// ------------------------------------------------------------
// Weather cover: overhanging roof, ribs, fascia, gutter
// ------------------------------------------------------------
const roof = new THREE.Group();
roof.position.set(0, 4.40, -0.08);
roof.rotation.x = 0.065;
cabinet.add(roof);
box(
roof,
[6.58, 0.17, 2.08],
[0, 0, 0],
materials.teal
);
// Raised roof ribs.
[-2.45, -1.25, 0, 1.25, 2.45].forEach((x) => {
box(
roof,
[0.065, 0.045, 1.91],
[x, 0.105, 0],
materials.tealDark
);
});
box(
cabinet,
[6.62, 0.37, 0.14],
[0, 4.27, 1.00],
materials.tealDark
);
box(
cabinet,
[6.66, 0.10, 0.16],
[0, 4.43, 1.04],
materials.teal
);
// Front gutter.
const gutter = new THREE.Mesh(
new THREE.CylinderGeometry(0.065, 0.065, 6.48, 12),
materials.tealDark
);
gutter.rotation.z = Math.PI / 2;
gutter.position.set(0, 4.12, 1.075);
gutter.castShadow = true;
cabinet.add(gutter);
// Right-side downspout.
cylinderBetween(
cabinet,
[3.17, 0.25, 0.90],
[3.17, 4.10, 0.90],
0.055,
materials.tealDark,
10
);
cylinderBetween(
cabinet,
[3.17, 4.10, 0.90],
[3.02, 4.10, 1.04],
0.055,
materials.tealDark,
10
);
// Side brackets under the weather cover.
[-2.75, 2.75].forEach((x) => {
cylinderBetween(
cabinet,
[x, 3.76, 0.52],
[x, 4.34, 0.91],
0.045,
materials.tealDark,
8
);
});
addSign(
cabinet,
"COMMUNITY BOOK CABINET",
"TAKE A BOOK • LEAVE A BOOK",
[0, 4.28, 1.09],
[4.75, 0.29],
"#153c3a",
"#fff8e8",
"#f5c75b"
);
// ------------------------------------------------------------
// Small hardware details
// ------------------------------------------------------------
const boltMaterial = new THREE.MeshStandardMaterial({
color: 0xd6d0b8,
roughness: 0.38,
metalness: 0.55
});
[-2.75, -1.02, 2.75].forEach((x) => {
[0.62, 3.95].forEach((y) => {
const bolt = new THREE.Mesh(
new THREE.CylinderGeometry(0.045, 0.045, 0.028, 12),
boltMaterial
);
bolt.rotation.x = Math.PI / 2;
bolt.position.set(x, y, 0.68);
cabinet.add(bolt);
});
});
// ------------------------------------------------------------
// Direct camera controls: orbit, zoom, pan, and touch pinch
// ------------------------------------------------------------
const pointers = new Map();
let dragged = false;
let previousPinchDistance = 0;
function pointerDistance() {
const values = [...pointers.values()];
if (values.length < 2) return 0;
return values[0].distanceTo(values[1]);
}
canvas.addEventListener("pointerdown", (event) => {
canvas.setPointerCapture(event.pointerId);
pointers.set(event.pointerId, new THREE.Vector2(
event.clientX,
event.clientY
));
dragged = false;
canvas.classList.add("dragging");
});
canvas.addEventListener("pointermove", (event) => {
if (!pointers.has(event.pointerId)) return;
const previous = pointers.get(event.pointerId);
const current = new THREE.Vector2(event.clientX, event.clientY);
const dx = current.x - previous.x;
const dy = current.y - previous.y;
pointers.set(event.pointerId, current);
if (pointers.size === 2) {
const distance = pointerDistance();
if (previousPinchDistance > 0) {
controls.radius = THREE.MathUtils.clamp(
controls.radius * (previousPinchDistance / distance),
controls.minRadius,
controls.maxRadius
);
}
previousPinchDistance = distance;
} else {
dragged = true;
if (event.shiftKey) {
const panScale = controls.radius * 0.0012;
controls.target.x -= dx * panScale;
controls.target.y += dy * panScale;
controls.target.y = THREE.MathUtils.clamp(
controls.target.y,
0.8,
3.4
);
} else {
controls.theta -= dx * 0.005;
controls.phi = THREE.MathUtils.clamp(
controls.phi - dy * 0.005,
0.62,
1.38
);
}
}
updateCamera();
});
function releasePointer(event) {
pointers.delete(event.pointerId);
previousPinchDistance = pointerDistance();
if (pointers.size === 0) {
canvas.classList.remove("dragging");
}
}
canvas.addEventListener("pointerup", releasePointer);
canvas.addEventListener("pointercancel", releasePointer);
canvas.addEventListener("lostpointercapture", releasePointer);
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
const scale = Math.exp(event.deltaY * 0.001);
controls.radius = THREE.MathUtils.clamp(
controls.radius * scale,
controls.minRadius,
controls.maxRadius
);
updateCamera();
},
{ passive: false }
);
canvas.addEventListener("contextmenu", (event) => event.preventDefault());
function resetView() {
controls.target.set(0, 2.15, 0);
controls.theta = 0.34;
controls.phi = 1.12;
controls.radius = 11.5;
updateCamera();
}
function frontView() {
controls.target.set(0, 2.15, 0);
controls.theta = 0;
controls.phi = 1.20;
controls.radius = 10.6;
updateCamera();
}
document.querySelector("#reset").addEventListener("click", resetView);
document.querySelector("#front").addEventListener("click", frontView);
canvas.addEventListener("dblclick", resetView);
// ------------------------------------------------------------
// Resize and render loop
// ------------------------------------------------------------
function resize() {
const width = window.innerWidth;
const height = window.innerHeight;
camera.aspect = width / height;
camera.updateProjectionMatrix();
renderer.setSize(width, height);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
}
window.addEventListener("resize", resize);
renderer.setAnimationLoop(() => {
renderer.render(scene, camera);
});
requestAnimationFrame(() => loading.classList.add("hidden"));
</script>
</body>
</html>
```