Prompt specification
Build or customize the Three.js 3D scene "Pocket bag garment cutaway" 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: Pocket bag garment cutaway
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/pocket-bag-garment-cutaway-x0601
Scene Description Prompt:
Please make a voxel Three.js scene in one standalone HTML file using cdnjs. Model a trouser panel with an angled pocket opening and visible inner bag; place a key partly inside to show the difference between opening and storage volume.
Working Three.js Starter (pocket-bag-garment-cutaway-x0601):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Voxel Pocket Anatomy</title>
<style>
:root {
--ink: #203338;
--muted: #647477;
--paper: rgba(248, 246, 240, 0.88);
--coral: #d97852;
--burgundy: #8e3d43;
--brass: #d9a73d;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #e7e3da;
color: var(--ink);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
cursor: grab;
}
canvas.dragging {
cursor: grabbing;
}
.intro {
position: fixed;
z-index: 5;
top: 24px;
left: 24px;
width: min(340px, calc(100vw - 48px));
padding: 18px 19px 16px;
border: 1px solid rgba(32, 51, 56, 0.12);
border-radius: 16px;
background: var(--paper);
box-shadow: 0 18px 50px rgba(40, 48, 45, 0.12);
backdrop-filter: blur(14px);
pointer-events: none;
}
.eyebrow {
margin-bottom: 7px;
color: var(--coral);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.18em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: clamp(23px, 3vw, 31px);
line-height: 1.05;
letter-spacing: -0.045em;
}
.intro p {
margin: 11px 0 14px;
color: var(--muted);
font-size: 13px;
line-height: 1.5;
}
.legend {
display: flex;
flex-wrap: wrap;
gap: 8px 13px;
padding-top: 12px;
border-top: 1px solid rgba(32, 51, 56, 0.1);
color: #526366;
font-size: 10px;
font-weight: 700;
letter-spacing: 0.04em;
text-transform: uppercase;
}
.legend span {
display: inline-flex;
align-items: center;
gap: 6px;
}
.swatch {
width: 9px;
height: 9px;
border-radius: 2px;
box-shadow: inset 0 0 0 1px rgba(0, 0, 0, 0.12);
}
.swatch.edge {
background: var(--coral);
}
.swatch.bag {
background: var(--burgundy);
}
.swatch.key {
background: var(--brass);
}
.toolbar {
position: fixed;
z-index: 6;
right: 24px;
bottom: 24px;
display: flex;
flex-wrap: wrap;
justify-content: flex-end;
gap: 8px;
max-width: calc(100vw - 48px);
}
button {
appearance: none;
border: 1px solid rgba(32, 51, 56, 0.14);
border-radius: 999px;
padding: 11px 15px;
background: rgba(248, 246, 240, 0.92);
color: var(--ink);
box-shadow: 0 8px 22px rgba(40, 48, 45, 0.1);
font: inherit;
font-size: 11px;
font-weight: 750;
letter-spacing: 0.035em;
cursor: pointer;
backdrop-filter: blur(12px);
transition:
transform 160ms ease,
background 160ms ease,
color 160ms ease;
}
button:hover {
transform: translateY(-2px);
}
button:focus-visible {
outline: 3px solid rgba(217, 120, 82, 0.32);
outline-offset: 2px;
}
button[aria-pressed="true"] {
border-color: var(--coral);
background: var(--coral);
color: #fffaf4;
}
.hint {
position: fixed;
z-index: 4;
left: 24px;
bottom: 25px;
color: rgba(32, 51, 56, 0.68);
font-size: 10px;
font-weight: 700;
letter-spacing: 0.06em;
text-transform: uppercase;
pointer-events: none;
}
#leaders {
position: fixed;
z-index: 2;
inset: 0;
width: 100%;
height: 100%;
overflow: visible;
pointer-events: none;
}
.callout {
position: fixed;
z-index: 3;
min-width: 145px;
padding: 10px 12px;
border: 1px solid rgba(32, 51, 56, 0.12);
border-left: 4px solid var(--callout-color);
border-radius: 10px;
background: rgba(250, 248, 243, 0.94);
box-shadow: 0 10px 26px rgba(40, 48, 45, 0.12);
transform: translate(-50%, -50%);
pointer-events: none;
backdrop-filter: blur(9px);
}
.callout strong {
display: block;
color: var(--ink);
font-size: 10px;
font-weight: 850;
letter-spacing: 0.09em;
text-transform: uppercase;
}
.callout small {
display: block;
margin-top: 4px;
color: #69777a;
font-size: 10px;
line-height: 1.35;
}
#calloutOpening {
--callout-color: var(--coral);
}
#calloutStorage {
--callout-color: var(--burgundy);
}
#calloutKey {
--callout-color: var(--brass);
}
@media (max-width: 680px) {
.intro {
top: 12px;
left: 12px;
width: calc(100vw - 24px);
padding: 13px 14px;
}
.intro p {
margin: 8px 0 10px;
font-size: 11px;
}
.legend {
gap: 6px 10px;
padding-top: 9px;
font-size: 8px;
}
.callout {
min-width: 112px;
padding: 8px 9px;
}
.callout strong {
font-size: 8px;
}
.callout small {
font-size: 8px;
}
.toolbar {
right: 12px;
bottom: 12px;
}
button {
padding: 10px 12px;
font-size: 9px;
}
.hint {
display: none;
}
}
</style>
</head>
<body>
<div class="intro">
<div class="eyebrow">Voxel material study</div>
<h1>Pocket anatomy</h1>
<p>
The angled slit is only the access opening. A recessed inner bag behind it
provides the actual storage volume. The brass key crosses that boundary.
</p>
<div class="legend">
<span><i class="swatch edge"></i> Cut edge</span>
<span><i class="swatch bag"></i> Inner bag</span>
<span><i class="swatch key"></i> Key</span>
</div>
</div>
<svg id="leaders" aria-hidden="true">
<line id="lineOpening" stroke="#d97852" stroke-width="1.5" />
<circle id="dotOpening" r="4" fill="#d97852" />
<line id="lineStorage" stroke="#8e3d43" stroke-width="1.5" />
<circle id="dotStorage" r="4" fill="#8e3d43" />
<line id="lineKey" stroke="#d9a73d" stroke-width="1.5" />
<circle id="dotKey" r="4" fill="#d9a73d" />
</svg>
<div id="calloutOpening" class="callout">
<strong>Angled opening</strong>
<small>Access mouth through the outer panel</small>
</div>
<div id="calloutStorage" class="callout">
<strong>Inner bag</strong>
<small>Storage volume behind the panel</small>
</div>
<div id="calloutKey" class="callout">
<strong>Partly inserted</strong>
<small>Bow outside · blade inside</small>
</div>
<div class="toolbar">
<button id="revealButton" aria-pressed="false">Reveal storage volume</button>
<button id="resetButton">Reset view</button>
</div>
<div class="hint">Drag to orbit · Scroll or pinch to zoom</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 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.12;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
document.body.prepend(renderer.domElement);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xe7e3da);
scene.fog = new THREE.Fog(0xe7e3da, 19, 42);
const camera = new THREE.PerspectiveCamera(
39,
window.innerWidth / window.innerHeight,
0.1,
100
);
const target = new THREE.Vector3(0, 4.45, 0);
const defaultView = {
radius: window.innerWidth < 680 ? 22 : 14.2,
theta: 0.48,
phi: 1.20
};
const orbit = {
radius: defaultView.radius,
theta: defaultView.theta,
phi: defaultView.phi
};
function updateCamera() {
const sinPhi = Math.sin(orbit.phi);
camera.position.set(
target.x + orbit.radius * sinPhi * Math.sin(orbit.theta),
target.y + orbit.radius * Math.cos(orbit.phi),
target.z + orbit.radius * sinPhi * Math.cos(orbit.theta)
);
camera.lookAt(target);
camera.updateMatrixWorld();
}
updateCamera();
// -------------------------------------------------------------------------
// Lighting
// -------------------------------------------------------------------------
const hemisphere = new THREE.HemisphereLight(0xfffbf2, 0x64706f, 2.1);
scene.add(hemisphere);
const keyLight = new THREE.DirectionalLight(0xfff2d8, 3.2);
keyLight.position.set(-6, 12, 8);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -8;
keyLight.shadow.camera.right = 8;
keyLight.shadow.camera.top = 12;
keyLight.shadow.camera.bottom = -3;
keyLight.shadow.camera.near = 0.5;
keyLight.shadow.camera.far = 35;
keyLight.shadow.bias = -0.0005;
scene.add(keyLight);
const fillLight = new THREE.DirectionalLight(0xcce6f0, 1.25);
fillLight.position.set(8, 6, 5);
scene.add(fillLight);
const cavityLight = new THREE.PointLight(0xffad78, 10, 5, 2);
cavityLight.position.set(0, 4.5, -0.55);
scene.add(cavityLight);
// -------------------------------------------------------------------------
// Helpers
// -------------------------------------------------------------------------
function seededValue(a, b, c = 0) {
const value = Math.sin(a * 127.1 + b * 311.7 + c * 74.7) * 43758.5453;
return value - Math.floor(value);
}
function colorWithVariation(hex, amount = 0.04, seedA = 0, seedB = 0) {
const color = new THREE.Color(hex);
const variation = (seededValue(seedA, seedB) - 0.5) * amount;
color.offsetHSL(variation * 0.28, variation * 0.18, variation);
return color;
}
function addBox(
width,
height,
depth,
color,
x,
y,
z,
materialOptions = {},
parent = scene
) {
const material = new THREE.MeshStandardMaterial({
color,
roughness: 0.78,
metalness: 0,
...materialOptions
});
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(width, height, depth),
material
);
mesh.position.set(x, y, z);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
// -------------------------------------------------------------------------
// Ground and small voxel supports
// -------------------------------------------------------------------------
const ground = new THREE.Mesh(
new THREE.PlaneGeometry(60, 60),
new THREE.MeshStandardMaterial({
color: 0xe4dfd5,
roughness: 0.96,
metalness: 0
})
);
ground.rotation.x = -Math.PI / 2;
ground.position.y = 0;
ground.receiveShadow = true;
scene.add(ground);
const supportMaterial = new THREE.MeshStandardMaterial({
color: 0x72584a,
roughness: 0.9
});
for (const x of [-2.7, 2.7]) {
const support = new THREE.Mesh(
new THREE.BoxGeometry(0.52, 0.36, 1.25),
supportMaterial
);
support.position.set(x, 0.18, -0.25);
support.castShadow = true;
support.receiveShadow = true;
scene.add(support);
}
// -------------------------------------------------------------------------
// Voxel trouser panel
//
// The aperture is made by omitting voxels along a rising diagonal.
// This keeps the hole genuinely open rather than placing it over a decal.
// -------------------------------------------------------------------------
const COLS = 18;
const ROWS = 21;
const CELL = 0.44;
const PANEL_WIDTH = COLS * CELL;
const PANEL_HEIGHT = ROWS * CELL;
const PANEL_BOTTOM = 0.42;
const PANEL_DEPTH = 0.72;
function cellX(col) {
return -PANEL_WIDTH / 2 + CELL * (col + 0.5);
}
function cellY(row) {
return PANEL_BOTTOM + CELL * (row + 0.5);
}
function pocketLine(x) {
return 4.62 + 0.29 * x;
}
function isOpeningCell(col, row) {
if (
col < 0 ||
row < 0 ||
col >= COLS ||
row >= ROWS
) {
return false;
}
const x = cellX(col);
const y = cellY(row);
return Math.abs(y - pocketLine(x)) < 0.38;
}
const panelGeometry = new THREE.BoxGeometry(
CELL * 0.975,
CELL * 0.975,
PANEL_DEPTH
);
const panelMaterial = new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 0.94,
metalness: 0,
transparent: true,
opacity: 1
});
const panel = new THREE.InstancedMesh(
panelGeometry,
panelMaterial,
COLS * ROWS
);
panel.castShadow = true;
panel.receiveShadow = true;
panel.instanceMatrix.setUsage(THREE.StaticDrawUsage);
const matrix = new THREE.Matrix4();
let panelInstance = 0;
const neighborChecks = [
[-1, -1],
[0, -1],
[1, -1],
[-1, 0],
[1, 0],
[-1, 1],
[0, 1],
[1, 1]
];
const stitchTransforms = [];
for (let row = 0; row < ROWS; row++) {
for (let col = 0; col < COLS; col++) {
if (isOpeningCell(col, row)) {
continue;
}
const x = cellX(col);
const y = cellY(row);
matrix.makeTranslation(x, y, 0);
panel.setMatrixAt(panelInstance, matrix);
let baseColor = "#496970";
if (row >= ROWS - 2) {
baseColor = "#2c4249";
} else if (row === 0) {
baseColor = "#344e55";
} else if (col === 1 || col === COLS - 2) {
baseColor = "#304b53";
}
const touchesOpening = neighborChecks.some(([dc, dr]) =>
isOpeningCell(col + dc, row + dr)
);
if (touchesOpening) {
baseColor = "#d97852";
}
const color = colorWithVariation(
baseColor,
touchesOpening ? 0.025 : 0.065,
col,
row
);
panel.setColorAt(panelInstance, color);
panelInstance++;
if (touchesOpening) {
stitchTransforms.push(
new THREE.Matrix4().makeTranslation(
x,
y,
PANEL_DEPTH / 2 + 0.027
)
);
}
}
}
panel.count = panelInstance;
panel.instanceMatrix.needsUpdate = true;
if (panel.instanceColor) {
panel.instanceColor.needsUpdate = true;
}
scene.add(panel);
// Fine pale stitches around the diagonal cut.
const stitchMaterial = new THREE.MeshStandardMaterial({
color: 0xf2d2b8,
roughness: 0.9,
transparent: true,
opacity: 1
});
const stitchGeometry = new THREE.BoxGeometry(0.075, 0.075, 0.035);
const stitches = new THREE.InstancedMesh(
stitchGeometry,
stitchMaterial,
stitchTransforms.length
);
stitchTransforms.forEach((transform, index) => {
stitches.setMatrixAt(index, transform);
});
stitches.instanceMatrix.needsUpdate = true;
stitches.castShadow = true;
scene.add(stitches);
// Waistband loops make the object read as a trouser panel.
const beltMaterial = new THREE.MeshStandardMaterial({
color: 0x2b4148,
roughness: 0.92,
transparent: true,
opacity: 1
});
for (const x of [-2.45, 0, 2.45]) {
const loop = new THREE.Mesh(
new THREE.BoxGeometry(0.25, 0.66, 0.13),
beltMaterial
);
loop.position.set(x, PANEL_BOTTOM + PANEL_HEIGHT - 0.43, 0.43);
loop.castShadow = true;
scene.add(loop);
}
// -------------------------------------------------------------------------
// Recessed inner bag
// -------------------------------------------------------------------------
const bagMaterial = new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 0.9,
metalness: 0
});
const bagTileGeometry = new THREE.BoxGeometry(0.39, 0.39, 0.16);
const BAG_COLS = 14;
const BAG_ROWS = 8;
const BAG_CELL = 0.39;
const bagTiles = new THREE.InstancedMesh(
bagTileGeometry,
bagMaterial,
BAG_COLS * BAG_ROWS
);
const bagMatrix = new THREE.Matrix4();
let bagIndex = 0;
for (let row = 0; row < BAG_ROWS; row++) {
for (let col = 0; col < BAG_COLS; col++) {
const x = (col - (BAG_COLS - 1) / 2) * BAG_CELL;
const y = 2.43 + row * BAG_CELL;
bagMatrix.makeTranslation(x, y, -1.26);
bagTiles.setMatrixAt(bagIndex, bagMatrix);
const shade = seededValue(col + 31, row + 17);
const color = new THREE.Color("#8e3d43");
color.offsetHSL(
(shade - 0.5) * 0.025,
(shade - 0.5) * 0.08,
(shade - 0.5) * 0.11
);
bagTiles.setColorAt(bagIndex, color);
bagIndex++;
}
}
bagTiles.instanceMatrix.needsUpdate = true;
if (bagTiles.instanceColor) {
bagTiles.instanceColor.needsUpdate = true;
}
bagTiles.castShadow = true;
bagTiles.receiveShadow = true;
scene.add(bagTiles);
// Structural backing and gussets establish the bag's depth.
const gussetMaterial = new THREE.MeshStandardMaterial({
color: 0x6f3038,
roughness: 0.95
});
addBox(
5.55,
3.18,
0.17,
0x632b32,
0,
3.80,
-1.47,
{ roughness: 0.95 },
scene
);
addBox(
0.20,
3.18,
1.08,
0x6f3038,
-2.78,
3.80,
-0.92,
{ roughness: 0.95 },
scene
);
addBox(
0.20,
3.18,
1.08,
0x6f3038,
2.78,
3.80,
-0.92,
{ roughness: 0.95 },
scene
);
addBox(
5.55,
0.20,
1.08,
0x6f3038,
0,
2.30,
-0.92,
{ roughness: 0.95 },
scene
);
// A subtle vertical construction seam on the lining.
for (let row = 0; row < 7; row++) {
addBox(
0.065,
0.16,
0.035,
0xb45b55,
0,
2.63 + row * 0.39,
-1.165,
{ roughness: 0.92 },
scene
);
}
// -------------------------------------------------------------------------
// Voxel key: square bow outside, shaft passing through the opening
// -------------------------------------------------------------------------
const keyMaterial = new THREE.MeshStandardMaterial({
color: 0xd9a73d,
roughness: 0.34,
metalness: 0.78
});
const keyAccentMaterial = new THREE.MeshStandardMaterial({
color: 0xf0c967,
roughness: 0.28,
metalness: 0.8
});
const keyHeadX = -0.76;
const keyHeadY = 4.56;
const keyHeadZ = 1.08;
const keyPixel = 0.15;
// Five-by-five square ring with a hollow center.
for (let row = -2; row <= 2; row++) {
for (let col = -2; col <= 2; col++) {
if (Math.abs(row) <= 1 && Math.abs(col) <= 1) {
continue;
}
addBox(
keyPixel,
keyPixel,
keyPixel,
row === 2 || col === 2 ? 0xf0c967 : 0xd9a73d,
keyHeadX + col * keyPixel,
keyHeadY + row * keyPixel,
keyHeadZ,
{
roughness: keyMaterial.roughness,
metalness: keyMaterial.metalness
},
scene
);
}
}
// Shaft travels through the true hole and into the recessed bag.
for (let i = 0; i < 11; i++) {
const t = i / 10;
addBox(
0.125,
0.125,
0.15,
0xd9a73d,
-0.37 + t * 0.07,
4.56 + t * 0.055,
0.86 - i * 0.145,
{
roughness: 0.34,
metalness: 0.78
},
scene
);
}
// Collar at the entrance to the pocket.
addBox(
0.22,
0.19,
0.28,
0xf0c967,
-0.30,
4.58,
0.76,
{
roughness: 0.28,
metalness: 0.8
},
scene
);
// Two chunky key teeth remain visible outside the panel.
addBox(
0.22,
0.13,
0.14,
0xd9a73d,
-0.17,
4.59,
0.47,
{
roughness: 0.34,
metalness: 0.78
},
scene
);
addBox(
0.18,
0.13,
0.14,
0xd9a73d,
-0.19,
4.60,
0.27,
{
roughness: 0.34,
metalness: 0.78
},
scene
);
// -------------------------------------------------------------------------
// Diagnostic storage-volume overlay, shown by the reveal button
// -------------------------------------------------------------------------
const storageOverlay = new THREE.Group();
storageOverlay.visible = false;
scene.add(storageOverlay);
const volumeGeometry = new THREE.BoxGeometry(5.7, 3.2, 1.25);
const volumeEdges = new THREE.LineSegments(
new THREE.EdgesGeometry(volumeGeometry),
new THREE.LineBasicMaterial({
color: 0xf1a263,
transparent: true,
opacity: 0.95
})
);
volumeEdges.position.set(0, 3.80, -0.82);
storageOverlay.add(volumeEdges);
const volumeFill = new THREE.Mesh(
volumeGeometry,
new THREE.MeshBasicMaterial({
color: 0xf1a263,
transparent: true,
opacity: 0.08,
depthWrite: false,
side: THREE.DoubleSide
})
);
volumeFill.position.copy(volumeEdges.position);
storageOverlay.add(volumeFill);
// -------------------------------------------------------------------------
// HTML callouts projected from 3D anchor points
// -------------------------------------------------------------------------
const calloutData = {
opening: {
element: document.getElementById("calloutOpening"),
line: document.getElementById("lineOpening"),
dot: document.getElementById("dotOpening"),
anchor: new THREE.Vector3(-2.55, 4.03, 0.42),
desktopOffset: [-150, -54],
mobileOffset: [-78, -66]
},
storage: {
element: document.getElementById("calloutStorage"),
line: document.getElementById("lineStorage"),
dot: document.getElementById("dotStorage"),
anchor: new THREE.Vector3(1.25, 5.00, -1.10),
desktopOffset: [150, -20],
mobileOffset: [82, -5]
},
key: {
element: document.getElementById("calloutKey"),
line: document.getElementById("lineKey"),
dot: document.getElementById("dotKey"),
anchor: new THREE.Vector3(-0.72, 4.56, 1.12),
desktopOffset: [-128, 80],
mobileOffset: [-72, 65]
}
};
function updateCallouts() {
const width = window.innerWidth;
const height = window.innerHeight;
const mobile = width < 680;
Object.values(calloutData).forEach((data) => {
const projected = data.anchor.clone().project(camera);
const x = (projected.x * 0.5 + 0.5) * width;
const y = (-projected.y * 0.5 + 0.5) * height;
const [offsetX, offsetY] = mobile
? data.mobileOffset
: data.desktopOffset;
const labelX = x + offsetX;
const labelY = y + offsetY;
data.element.style.left = `${labelX}px`;
data.element.style.top = `${labelY}px`;
data.line.setAttribute("x1", x);
data.line.setAttribute("y1", y);
data.line.setAttribute("x2", labelX);
data.line.setAttribute("y2", labelY);
data.dot.setAttribute("cx", x);
data.dot.setAttribute("cy", y);
});
}
// -------------------------------------------------------------------------
// Direct camera controls: pointer orbit, wheel zoom, touch pinch
// -------------------------------------------------------------------------
const canvas = renderer.domElement;
const pointers = new Map();
let previousPinchDistance = 0;
function pointerDistance() {
const points = [...pointers.values()];
if (points.length < 2) {
return 0;
}
return Math.hypot(
points[0].x - points[1].x,
points[0].y - points[1].y
);
}
canvas.addEventListener("pointerdown", (event) => {
canvas.setPointerCapture(event.pointerId);
pointers.set(event.pointerId, {
x: event.clientX,
y: event.clientY
});
canvas.classList.add("dragging");
if (pointers.size === 2) {
previousPinchDistance = pointerDistance();
}
});
canvas.addEventListener("pointermove", (event) => {
const previous = pointers.get(event.pointerId);
if (!previous) {
return;
}
pointers.set(event.pointerId, {
x: event.clientX,
y: event.clientY
});
if (pointers.size === 1) {
const dx = event.clientX - previous.x;
const dy = event.clientY - previous.y;
orbit.theta -= dx * 0.006;
orbit.phi = THREE.MathUtils.clamp(
orbit.phi - dy * 0.005,
0.58,
1.48
);
} else if (pointers.size === 2) {
const distance = pointerDistance();
if (previousPinchDistance > 0 && distance > 0) {
orbit.radius = THREE.MathUtils.clamp(
orbit.radius * (previousPinchDistance / distance),
8.5,
29
);
}
previousPinchDistance = distance;
}
updateCamera();
});
function releasePointer(event) {
pointers.delete(event.pointerId);
if (pointers.size < 2) {
previousPinchDistance = 0;
}
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();
orbit.radius = THREE.MathUtils.clamp(
orbit.radius * Math.exp(event.deltaY * 0.001),
8.5,
29
);
updateCamera();
},
{ passive: false }
);
function resetView() {
orbit.radius = window.innerWidth < 680 ? 22 : 14.2;
orbit.theta = defaultView.theta;
orbit.phi = defaultView.phi;
updateCamera();
}
document.getElementById("resetButton").addEventListener("click", resetView);
canvas.addEventListener("dblclick", resetView);
window.addEventListener("keydown", (event) => {
if (event.key === "ArrowLeft") {
orbit.theta += 0.08;
} else if (event.key === "ArrowRight") {
orbit.theta -= 0.08;
} else if (event.key === "ArrowUp") {
orbit.phi = Math.max(0.58, orbit.phi - 0.06);
} else if (event.key === "ArrowDown") {
orbit.phi = Math.min(1.48, orbit.phi + 0.06);
} else {
return;
}
updateCamera();
});
// -------------------------------------------------------------------------
// Reveal control
// -------------------------------------------------------------------------
const revealButton = document.getElementById("revealButton");
let revealTarget = 0;
revealButton.addEventListener("click", () => {
revealTarget = revealTarget > 0.5 ? 0 : 1;
revealButton.setAttribute("aria-pressed", String(revealTarget > 0.5));
revealButton.textContent =
revealTarget > 0.5 ? "Restore outer panel" : "Reveal storage volume";
});
// -------------------------------------------------------------------------
// Resize and render loop
// -------------------------------------------------------------------------
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
});
function animate() {
requestAnimationFrame(animate);
const currentOpacity = panelMaterial.opacity;
const nextOpacity = THREE.MathUtils.lerp(
currentOpacity,
1 - revealTarget * 0.82,
0.08
);
panelMaterial.opacity = nextOpacity;
panelMaterial.depthWrite = nextOpacity > 0.96;
stitchMaterial.opacity = THREE.MathUtils.lerp(
stitchMaterial.opacity,
1 - revealTarget * 0.82,
0.08
);
beltMaterial.opacity = stitchMaterial.opacity;
storageOverlay.visible = revealTarget > 0.01;
if (storageOverlay.visible) {
volumeEdges.material.opacity = 0.55 + Math.sin(performance.now() * 0.003) * 0.2;
volumeFill.material.opacity = 0.055 + Math.sin(performance.now() * 0.003) * 0.02;
}
renderer.render(scene, camera);
updateCallouts();
}
animate();
</script>
</body>
</html>
```