Prompt specification
Build or customize the Three.js 3D scene "Everyday polymer materials cabinet" 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: Everyday polymer materials cabinet
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/everyday-polymer-materials-cabinet-x1983
Scene Description Prompt:
I'd like to see this as a voxel Three.js scene: everyday polymer materials cabinet. Compose a flexible bottle, elastic band, and rigid resin-like object beside chain-structure insets; label material examples generically and avoid inferring exact chemistry from appearance. Use cdnjs and return one self-contained HTML file.
Working Three.js Starter (everyday-polymer-materials-cabinet-x1983):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Everyday Polymer Cabinet — Voxel Study</title>
<style>
:root {
color-scheme: dark;
--ink: #eef7f4;
--muted: #9fb8b8;
--panel: rgba(8, 20, 26, 0.82);
--line: rgba(207, 235, 224, 0.22);
--accent: #8ee0cf;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #071219;
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
canvas {
display: block;
width: 100%;
height: 100%;
cursor: grab;
touch-action: none;
}
canvas:active {
cursor: grabbing;
}
.vignette {
position: fixed;
inset: 0;
pointer-events: none;
background:
radial-gradient(circle at 50% 43%, transparent 42%, rgba(0, 0, 0, 0.28) 100%),
linear-gradient(180deg, rgba(0, 0, 0, 0.15), transparent 22%);
}
.hud {
position: fixed;
inset: 0;
pointer-events: none;
color: var(--ink);
}
.brand {
position: absolute;
top: clamp(18px, 3vw, 36px);
left: clamp(18px, 3vw, 42px);
max-width: min(620px, calc(100vw - 120px));
padding: 18px 20px 17px;
border: 1px solid var(--line);
border-left: 4px solid var(--accent);
border-radius: 4px 14px 14px 4px;
background: var(--panel);
box-shadow: 0 18px 50px rgba(0, 0, 0, 0.24);
backdrop-filter: blur(12px);
}
.eyebrow {
margin-bottom: 7px;
color: var(--accent);
font-size: 11px;
font-weight: 800;
letter-spacing: 0.22em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: clamp(24px, 3.1vw, 44px);
font-weight: 760;
line-height: 1.02;
letter-spacing: -0.035em;
}
.subtitle {
margin: 9px 0 0;
color: var(--muted);
font-size: clamp(12px, 1.2vw, 15px);
line-height: 1.45;
}
.generic-badge {
position: absolute;
top: clamp(18px, 3vw, 36px);
right: clamp(18px, 3vw, 42px);
padding: 9px 12px;
border: 1px solid rgba(142, 224, 207, 0.36);
border-radius: 999px;
color: #bceee3;
background: rgba(14, 37, 42, 0.78);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.16em;
text-transform: uppercase;
backdrop-filter: blur(10px);
}
.note {
position: absolute;
left: clamp(18px, 3vw, 42px);
bottom: clamp(18px, 3vw, 32px);
max-width: min(610px, calc(100vw - 220px));
padding: 10px 13px;
border: 1px solid var(--line);
border-radius: 8px;
color: #b9cbca;
background: rgba(7, 18, 25, 0.78);
font-size: 11px;
line-height: 1.45;
backdrop-filter: blur(10px);
}
.controls {
position: absolute;
right: clamp(18px, 3vw, 42px);
bottom: clamp(18px, 3vw, 32px);
display: flex;
align-items: center;
gap: 10px;
pointer-events: auto;
}
.hint {
color: #9eb5b4;
font-size: 10px;
font-weight: 700;
letter-spacing: 0.12em;
text-transform: uppercase;
}
button {
border: 1px solid rgba(142, 224, 207, 0.35);
border-radius: 7px;
padding: 9px 12px;
color: #dff8f1;
background: rgba(16, 40, 45, 0.9);
font: inherit;
font-size: 11px;
font-weight: 750;
cursor: pointer;
}
button:hover {
background: rgba(31, 67, 69, 0.95);
}
.loading {
position: fixed;
inset: 0;
z-index: 10;
display: grid;
place-items: center;
color: #bceee3;
background: #071219;
font-size: 12px;
font-weight: 800;
letter-spacing: 0.18em;
text-transform: uppercase;
transition: opacity 0.35s ease;
}
.loading.hidden {
opacity: 0;
pointer-events: none;
}
.sr-only {
position: absolute;
width: 1px;
height: 1px;
padding: 0;
margin: -1px;
overflow: hidden;
clip: rect(0, 0, 0, 0);
white-space: nowrap;
border: 0;
}
@media (max-width: 700px) {
.generic-badge,
.hint {
display: none;
}
.brand {
max-width: calc(100vw - 36px);
padding: 14px 15px;
}
.subtitle {
max-width: 88%;
}
.note {
max-width: calc(100vw - 36px);
bottom: 18px;
padding-right: 82px;
font-size: 10px;
}
.controls {
right: 18px;
bottom: 18px;
}
}
</style>
</head>
<body>
<div id="loading" class="loading">Building voxel cabinet</div>
<div class="vignette"></div>
<main class="hud">
<header class="brand">
<div class="eyebrow">Material form study · Voxel cabinet</div>
<h1>Everyday Polymer Forms</h1>
<p class="subtitle">
Flexible, elastic, rigid, and chain-like examples arranged as generic
structural illustrations.
</p>
</header>
<div class="generic-badge">Generic examples</div>
<div class="note">
Labels describe shape and structure only. Color, gloss, and geometry are
illustrative and do not identify an exact material or chemistry.
</div>
<div class="controls">
<span class="hint">Drag to orbit · Scroll to zoom</span>
<button id="resetView" type="button">Reset view</button>
</div>
</main>
<p class="sr-only">
A voxel-style cabinet displaying a flexible bottle, an elastic band, a rigid
resin-like object, and two generic chain-structure insets.
</p>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const loading = document.querySelector("#loading");
const resetButton = document.querySelector("#resetView");
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x071219);
scene.fog = new THREE.Fog(0x071219, 36, 105);
const camera = new THREE.PerspectiveCamera(
42,
window.innerWidth / window.innerHeight,
0.1,
140
);
const renderer = new THREE.WebGLRenderer({
antialias: true,
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;
renderer.domElement.setAttribute(
"aria-label",
"Interactive voxel display of generic everyday polymer forms"
);
document.body.prepend(renderer.domElement);
const materials = {
cabinetBack: new THREE.MeshStandardMaterial({
color: 0x10272d,
roughness: 0.84,
metalness: 0.02
}),
cabinetBayA: new THREE.MeshStandardMaterial({
color: 0x163139,
roughness: 0.88
}),
cabinetBayB: new THREE.MeshStandardMaterial({
color: 0x122c33,
roughness: 0.9
}),
frame: new THREE.MeshStandardMaterial({
color: 0xb87945,
roughness: 0.67
}),
shelf: new THREE.MeshStandardMaterial({
color: 0xd09a5d,
roughness: 0.62
}),
pedestal: new THREE.MeshStandardMaterial({
color: 0x1a3339,
roughness: 0.76
}),
pedestalTop: new THREE.MeshStandardMaterial({
color: 0x29474b,
roughness: 0.55
}),
floor: new THREE.MeshStandardMaterial({
color: 0x08161c,
roughness: 0.95
}),
bottleA: new THREE.MeshPhysicalMaterial({
color: 0x8ad9d0,
roughness: 0.3,
clearcoat: 0.62,
clearcoatRoughness: 0.3
}),
bottleB: new THREE.MeshPhysicalMaterial({
color: 0x74cbc4,
roughness: 0.34,
clearcoat: 0.55,
clearcoatRoughness: 0.34
}),
bottleC: new THREE.MeshPhysicalMaterial({
color: 0xa5e2d8,
roughness: 0.28,
clearcoat: 0.66,
clearcoatRoughness: 0.28
}),
bottleCap: new THREE.MeshStandardMaterial({
color: 0x245b60,
roughness: 0.46
}),
paper: new THREE.MeshStandardMaterial({
color: 0xe8eee7,
roughness: 0.86
}),
ink: new THREE.MeshStandardMaterial({
color: 0x27454a,
roughness: 0.8
}),
bandA: new THREE.MeshPhysicalMaterial({
color: 0x9b75c7,
roughness: 0.36,
clearcoat: 0.42,
clearcoatRoughness: 0.38
}),
bandB: new THREE.MeshPhysicalMaterial({
color: 0x8059ad,
roughness: 0.4,
clearcoat: 0.38,
clearcoatRoughness: 0.42
}),
resinA: new THREE.MeshPhysicalMaterial({
color: 0xffb25f,
roughness: 0.18,
clearcoat: 0.92,
clearcoatRoughness: 0.12,
emissive: 0x281207,
emissiveIntensity: 0.18
}),
resinB: new THREE.MeshPhysicalMaterial({
color: 0xffca79,
roughness: 0.2,
clearcoat: 0.88,
clearcoatRoughness: 0.15,
emissive: 0x2d1708,
emissiveIntensity: 0.15
}),
panelFrame: new THREE.MeshStandardMaterial({
color: 0xb98852,
roughness: 0.58
}),
panelInset: new THREE.MeshStandardMaterial({
color: 0x091a20,
roughness: 0.76
}),
chainCyan: new THREE.MeshStandardMaterial({
color: 0x70d9ca,
emissive: 0x123b36,
emissiveIntensity: 0.55,
roughness: 0.42
}),
chainGold: new THREE.MeshStandardMaterial({
color: 0xe7b76a,
emissive: 0x3a2107,
emissiveIntensity: 0.45,
roughness: 0.42
}),
lamp: new THREE.MeshStandardMaterial({
color: 0xffdf9a,
emissive: 0xffc765,
emissiveIntensity: 1.4,
roughness: 0.35
})
};
function addBox(parent, size, position, material, castShadow = true) {
const geometry = new THREE.BoxGeometry(size[0], size[1], size[2]);
const mesh = new THREE.Mesh(geometry, material);
mesh.position.set(position[0], position[1], position[2]);
mesh.castShadow = castShadow;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function roundedRect(ctx, x, y, w, h, r) {
const radius = Math.min(r, w * 0.5, h * 0.5);
ctx.beginPath();
ctx.moveTo(x + radius, y);
ctx.arcTo(x + w, y, x + w, y + h, radius);
ctx.arcTo(x + w, y + h, x, y + h, radius);
ctx.arcTo(x, y + h, x, y, radius);
ctx.arcTo(x, y, x + w, y, radius);
ctx.closePath();
}
function makePlaque(title, subtitle, worldWidth = 3.05, worldHeight = 0.72) {
const canvas = document.createElement("canvas");
canvas.width = 1024;
canvas.height = 240;
const ctx = canvas.getContext("2d");
ctx.clearRect(0, 0, canvas.width, canvas.height);
roundedRect(ctx, 8, 8, 1008, 224, 26);
ctx.fillStyle = "rgba(8, 22, 28, 0.94)";
ctx.fill();
ctx.lineWidth = 3;
ctx.strokeStyle = "rgba(176, 223, 213, 0.38)";
ctx.stroke();
ctx.fillStyle = "#83dbc9";
ctx.fillRect(8, 8, 18, 224);
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.fillStyle = "#eff8f5";
ctx.font = "800 67px Inter, Arial, sans-serif";
ctx.fillText(title.toUpperCase(), 520, 94);
ctx.fillStyle = "#9fb9b6";
ctx.font = "650 30px Inter, Arial, sans-serif";
ctx.letterSpacing = "3px";
ctx.fillText(subtitle.toUpperCase(), 520, 169);
const texture = new THREE.CanvasTexture(canvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.minFilter = THREE.LinearFilter;
texture.generateMipmaps = false;
const material = new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthTest: false,
depthWrite: false,
toneMapped: false
});
const sprite = new THREE.Sprite(material);
sprite.scale.set(worldWidth, worldHeight, 1);
sprite.renderOrder = 50;
return sprite;
}
// Cabinet shell and shelves.
const cabinet = new THREE.Group();
scene.add(cabinet);
addBox(cabinet, [22.4, 8.8, 0.32], [0, 4.0, -1.85], materials.cabinetBack);
addBox(cabinet, [0.48, 9.0, 3.2], [-11.05, 4.0, -0.25], materials.frame);
addBox(cabinet, [0.48, 9.0, 3.2], [11.05, 4.0, -0.25], materials.frame);
addBox(cabinet, [22.55, 0.48, 3.2], [0, 8.3, -0.25], materials.frame);
addBox(cabinet, [22.35, 0.22, 2.75], [0, 7.65, -0.15], materials.pedestalTop);
const bayCenters = [-8.55, -4.3, 0, 4.3, 8.55];
bayCenters.forEach((x, index) => {
addBox(
cabinet,
[4.02, 7.25, 0.06],
[x, 3.72, -1.66],
index % 2 === 0 ? materials.cabinetBayA : materials.cabinetBayB,
false
);
});
[-10.78, -6.42, -2.15, 2.15, 6.42, 10.78].forEach((x) => {
addBox(cabinet, [0.08, 7.2, 0.08], [x, 3.73, -1.57], materials.pedestalTop, false);
});
addBox(cabinet, [22.3, 0.36, 3.25], [0, 0.0, -0.05], materials.shelf);
addBox(cabinet, [22.15, 1.2, 3.1], [0, -0.79, -0.08], materials.cabinetBack);
addBox(cabinet, [21.7, 0.18, 3.22], [0, -0.03, -0.05], materials.shelf);
for (let i = -2; i <= 2; i++) {
addBox(cabinet, [4.05, 0.85, 0.08], [i * 4.25, -0.78, 1.5], materials.pedestal);
addBox(cabinet, [0.72, 0.08, 0.12], [i * 4.25 + 1.28, -0.55, 1.57], materials.shelf);
}
for (let i = -7; i <= 7; i++) {
addBox(cabinet, [0.55, 0.07, 0.08], [i * 1.42, 7.42, 1.03], materials.lamp, false);
}
const floor = addBox(
scene,
[80, 0.2, 80],
[0, -1.48, 0],
materials.floor,
false
);
floor.receiveShadow = true;
// Display plinths.
const pedestalX = [-6.1, -2.05, 2.0];
pedestalX.forEach((x) => {
addBox(cabinet, [3.35, 0.36, 2.15], [x, 0.37, 0.05], materials.pedestal);
addBox(cabinet, [3.12, 0.08, 1.92], [x, 0.59, 0.05], materials.pedestalTop);
});
// Flexible bottle: a subtly pinched, asymmetric field of cube voxels.
const bottle = new THREE.Group();
bottle.position.x = -6.1;
scene.add(bottle);
const bottleBase = 0.65;
const voxel = 0.27;
const rowWidths = [5, 7, 8, 8, 8, 8, 7, 6, 5, 4, 3];
const rowOffsets = [0, 0, 0.03, 0.08, 0.13, 0.15, 0.12, 0.08, 0.03, 0, 0];
const bottleMaterials = [materials.bottleA, materials.bottleB, materials.bottleC];
rowWidths.forEach((width, row) => {
const start = -((width - 1) * voxel) / 2 + rowOffsets[row];
for (let col = 0; col < width; col++) {
for (let depth = 0; depth < 2; depth++) {
const x = start + col * voxel;
const y = bottleBase + row * voxel;
const z = depth === 0 ? -0.13 : 0.13;
const colorIndex = (row * 3 + col + depth) % bottleMaterials.length;
addBox(
bottle,
[0.255, 0.255, 0.255],
[x, y, z],
bottleMaterials[colorIndex]
);
}
}
});
const bottleTop = bottleBase + rowWidths.length * voxel;
addBox(bottle, [0.78, 0.31, 0.62], [0, bottleTop + 0.15, 0], materials.bottleCap);
addBox(bottle, [0.57, 0.16, 0.5], [0, bottleTop + 0.38, 0], materials.bottleCap);
addBox(bottle, [1.08, 0.55, 0.055], [0.08, 1.62, 0.285], materials.paper);
addBox(bottle, [0.62, 0.07, 0.06], [0.08, 1.73, 0.325], materials.ink);
addBox(bottle, [0.43, 0.055, 0.06], [0.08, 1.54, 0.325], materials.ink);
// Elastic band: a segmented oval assembled from slightly rotated blocks.
const band = new THREE.Group();
band.position.x = -2.05;
scene.add(band);
const segmentGeometry = new THREE.BoxGeometry(0.22, 0.14, 0.36);
const segmentCount = 48;
const bandCenterY = 1.94;
for (let i = 0; i < segmentCount; i++) {
const t = (i / segmentCount) * Math.PI * 2;
const x = Math.cos(t) * 1.04;
const y = bandCenterY + Math.sin(t) * 1.29;
const tangent = Math.atan2(1.29 * Math.cos(t), -1.04 * Math.sin(t));
const segment = new THREE.Mesh(
segmentGeometry,
i % 7 === 0 ? materials.bandB : materials.bandA
);
segment.position.set(x, y, 0.18);
segment.rotation.z = tangent;
segment.castShadow = true;
segment.receiveShadow = true;
band.add(segment);
}
// Rigid resin-like object: stacked, crisp voxel contours with glossy material.
const resin = new THREE.Group();
resin.position.x = 2.0;
scene.add(resin);
const resinBase = 0.66;
const resinVoxel = 0.39;
const resinLayers = [
[5, 4],
[5, 4],
[4, 4],
[3, 3],
[2, 2]
];
resinLayers.forEach(([width, depth], layer) => {
for (let x = 0; x < width; x++) {
for (let z = 0; z < depth; z++) {
const px = (x - (width - 1) / 2) * resinVoxel;
const pz = (z - (depth - 1) / 2) * resinVoxel;
const py = resinBase + layer * resinVoxel;
const mat = (x + z + layer) % 5 === 0 ? materials.resinB : materials.resinA;
addBox(
resin,
[0.375, 0.375, 0.375],
[px, py, pz],
mat
);
}
}
});
// Chain-like schematic insets.
function addRing(parent, centerX, centerY, width, height, z, material) {
const t = 0.085;
addBox(parent, [width, t, 0.1], [centerX, centerY + height / 2, z], material, false);
addBox(parent, [width, t, 0.1], [centerX, centerY - height / 2, z], material, false);
addBox(parent, [t, height, 0.1], [centerX - width / 2, centerY, z], material, false);
addBox(parent, [t, height, 0.1], [centerX + width / 2, centerY, z], material, false);
}
function addChainPanel(x, title) {
const panel = new THREE.Group();
panel.position.x = x;
scene.add(panel);
addBox(panel, [2.72, 3.82, 0.2], [0, 2.7, -1.49], materials.panelFrame);
addBox(panel, [2.4, 3.5, 0.12], [0, 2.7, -1.34], materials.panelInset, false);
const titlePlaque = makePlaque(title, "Chain-like schematic", 2.26, 0.54);
titlePlaque.position.set(0, 4.08, -0.72);
panel.add(titlePlaque);
return panel;
}
const linkedPanel = addChainPanel(5.55, "Linked units");
addRing(linkedPanel, 0, 1.68, 0.74, 0.38, -1.12, materials.chainCyan);
addRing(linkedPanel, 0, 2.33, 0.38, 0.74, -1.09, materials.chainGold);
addRing(linkedPanel, 0, 2.98, 0.74, 0.38, -1.06, materials.chainCyan);
addRing(linkedPanel, 0, 3.55, 0.38, 0.48, -1.03, materials.chainGold);
const repeatingPanel = addChainPanel(8.58, "Repeating chain");
const chainPoints = [
[-0.68, 1.45],
[0.68, 1.78],
[-0.68, 2.11],
[0.68, 2.44],
[-0.68, 2.77],
[0.68, 3.10],
[-0.68, 3.43]
];
chainPoints.forEach(([x, y], index) => {
addBox(
repeatingPanel,
[0.21, 0.21, 0.11],
[x, y, -1.08],
index % 2 === 0 ? materials.chainCyan : materials.chainGold,
false
);
if (index < chainPoints.length - 1) {
const [x1, y1] = chainPoints[index];
const [x2, y2] = chainPoints[index + 1];
const length = Math.hypot(x2 - x1, y2 - y1);
const angle = Math.atan2(y2 - y1, x2 - x1);
const connector = addBox(
repeatingPanel,
[length, 0.085, 0.1],
[(x1 + x2) / 2, (y1 + y2) / 2, -1.11],
materials.chainGold,
false
);
connector.rotation.z = angle;
}
});
// Generic object labels.
const bottleLabel = makePlaque("Flexible bottle", "Flexible form", 3.0, 0.7);
bottleLabel.position.set(-6.1, 0.43, 1.48);
scene.add(bottleLabel);
const bandLabel = makePlaque("Elastic band", "Stretchable loop", 3.0, 0.7);
bandLabel.position.set(-2.05, 0.43, 1.48);
scene.add(bandLabel);
const resinLabel = makePlaque("Rigid resin-like solid", "Rigid, glossy form", 3.35, 0.7);
resinLabel.position.set(2.0, 0.43, 1.48);
scene.add(resinLabel);
// Lighting.
scene.add(new THREE.HemisphereLight(0xc8eff4, 0x261b22, 2.25));
const keyLight = new THREE.DirectionalLight(0xfff1d4, 3.15);
keyLight.position.set(7, 13, 11);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -16;
keyLight.shadow.camera.right = 16;
keyLight.shadow.camera.top = 14;
keyLight.shadow.camera.bottom = -8;
keyLight.shadow.camera.near = 1;
keyLight.shadow.camera.far = 40;
keyLight.shadow.bias = -0.00035;
keyLight.shadow.normalBias = 0.025;
scene.add(keyLight);
const cyanLight = new THREE.PointLight(0x6ee7d8, 18, 22, 2);
cyanLight.position.set(-7, 5.4, 4.5);
scene.add(cyanLight);
const warmLight = new THREE.PointLight(0xffbd72, 15, 18, 2);
warmLight.position.set(7.5, 3.8, 4.2);
scene.add(warmLight);
const rearLight = new THREE.DirectionalLight(0x73b8cf, 0.8);
rearLight.position.set(-8, 5, -9);
scene.add(rearLight);
// Lightweight camera controls implemented without addons.
class OrbitControls {
constructor(element) {
this.element = element;
this.target = new THREE.Vector3(0, 2.75, -0.2);
this.yaw = 0.24;
this.elevation = 0.24;
this.distance = 22.5;
this.defaults = {
yaw: this.yaw,
elevation: this.elevation,
distance: this.distance
};
this.pointers = new Map();
this.lastPinch = 0;
element.addEventListener("pointerdown", (event) => {
element.setPointerCapture(event.pointerId);
this.pointers.set(event.pointerId, {
x: event.clientX,
y: event.clientY
});
if (this.pointers.size === 2) {
this.lastPinch = this.pinchDistance();
}
});
element.addEventListener("pointermove", (event) => {
if (!this.pointers.has(event.pointerId)) return;
if (this.pointers.size === 1) {
const previous = this.pointers.get(event.pointerId);
const dx = event.clientX - previous.x;
const dy = event.clientY - previous.y;
this.yaw = THREE.MathUtils.clamp(this.yaw - dx * 0.005, -0.72, 0.72);
this.elevation = THREE.MathUtils.clamp(
this.elevation + dy * 0.004,
0.08,
0.72
);
}
this.pointers.set(event.pointerId, {
x: event.clientX,
y: event.clientY
});
if (this.pointers.size === 2) {
const distance = this.pinchDistance();
if (this.lastPinch > 0 && distance > 0) {
this.distance = THREE.MathUtils.clamp(
this.distance * (this.lastPinch / distance),
15,
34
);
}
this.lastPinch = distance;
}
});
const release = (event) => {
this.pointers.delete(event.pointerId);
if (this.pointers.size < 2) this.lastPinch = 0;
};
element.addEventListener("pointerup", release);
element.addEventListener("pointercancel", release);
element.addEventListener("lostpointercapture", release);
element.addEventListener(
"wheel",
(event) => {
event.preventDefault();
this.distance = THREE.MathUtils.clamp(
this.distance * Math.exp(event.deltaY * 0.001),
15,
34
);
},
{ passive: false }
);
}
pinchDistance() {
const points = [...this.pointers.values()];
if (points.length < 2) return 0;
return Math.hypot(
points[0].x - points[1].x,
points[0].y - points[1].y
);
}
reset() {
this.yaw = this.defaults.yaw;
this.elevation = this.defaults.elevation;
this.distance = this.defaults.distance;
}
update(camera) {
const aspect = window.innerWidth / window.innerHeight;
const fitFactor = Math.max(1, 1.2 / aspect);
const distance = this.distance * fitFactor;
const horizontal = Math.cos(this.elevation) * distance;
camera.position.set(
this.target.x + Math.sin(this.yaw) * horizontal,
this.target.y + Math.sin(this.elevation) * distance,
this.target.z + Math.cos(this.yaw) * horizontal
);
camera.lookAt(this.target);
}
}
const controls = new OrbitControls(renderer.domElement);
resetButton.addEventListener("click", () => controls.reset());
window.addEventListener("keydown", (event) => {
if (event.key.toLowerCase() === "r") controls.reset();
});
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
});
const clock = new THREE.Clock();
renderer.setAnimationLoop(() => {
const elapsed = clock.getElapsedTime();
materials.lamp.emissiveIntensity = 1.28 + Math.sin(elapsed * 1.4) * 0.12;
controls.update(camera);
renderer.render(scene, camera);
});
requestAnimationFrame(() => {
loading.classList.add("hidden");
setTimeout(() => loading.remove(), 400);
});
</script>
</body>
</html>
```