Prompt specification
Build or customize the Three.js 3D scene "Simple wind-up toy part map" 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: Simple wind-up toy part map
Category: Everyday & Industrial · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/simple-wind-up-toy-part-map-x0257
Scene Description Prompt:
Make a standalone HTML scene with Three.js from cdnjs that depicts a wind-up toy's components. Display a toy wheel, axle, spacer, and casing in an exploded row; connect each part to its original location with unobtrusive alignment guides. Stylized solid forms.
Working Three.js Starter (simple-wind-up-toy-part-map-x0257):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Wind-up Toy — Exploded Component Study</title>
<style>
:root {
color-scheme: light;
--ink: #23302f;
--muted: #687471;
--paper: #f1eadf;
--line: rgba(35, 48, 47, 0.16);
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: var(--paper);
color: var(--ink);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
#viewport {
position: fixed;
inset: 0;
}
canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
outline: none;
}
header {
position: fixed;
top: clamp(20px, 4vw, 48px);
left: clamp(20px, 4vw, 54px);
z-index: 5;
max-width: 390px;
pointer-events: none;
}
.eyebrow {
display: flex;
align-items: center;
gap: 10px;
margin-bottom: 13px;
color: var(--muted);
font-size: 10px;
font-weight: 750;
letter-spacing: 0.18em;
text-transform: uppercase;
}
.eyebrow::before {
width: 30px;
height: 1px;
content: "";
background: currentColor;
}
h1 {
margin: 0;
font-size: clamp(31px, 4.1vw, 57px);
font-weight: 620;
letter-spacing: -0.055em;
line-height: 0.98;
}
header p {
max-width: 330px;
margin: 17px 0 0;
color: var(--muted);
font-size: 13px;
line-height: 1.6;
}
#controls {
position: fixed;
top: clamp(20px, 4vw, 48px);
right: clamp(20px, 4vw, 54px);
z-index: 7;
width: 218px;
padding: 15px 16px 13px;
border: 1px solid var(--line);
border-radius: 16px;
background: rgba(248, 244, 236, 0.76);
box-shadow: 0 12px 35px rgba(49, 57, 52, 0.08);
backdrop-filter: blur(12px);
}
.control-label {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 12px;
color: var(--muted);
font-size: 10px;
font-weight: 750;
letter-spacing: 0.09em;
text-transform: uppercase;
}
#explodeValue {
color: var(--ink);
font-variant-numeric: tabular-nums;
}
input[type="range"] {
width: 100%;
height: 4px;
margin: 0;
appearance: none;
border-radius: 999px;
outline: none;
background: linear-gradient(
90deg,
#2e8d89 0%,
#2e8d89 var(--progress, 100%),
rgba(35, 48, 47, 0.14) var(--progress, 100%)
);
cursor: pointer;
}
input[type="range"]::-webkit-slider-thumb {
width: 16px;
height: 16px;
appearance: none;
border: 3px solid #f7f2e9;
border-radius: 50%;
background: #2e8d89;
box-shadow: 0 1px 6px rgba(35, 48, 47, 0.28);
}
input[type="range"]::-moz-range-thumb {
width: 12px;
height: 12px;
border: 3px solid #f7f2e9;
border-radius: 50%;
background: #2e8d89;
box-shadow: 0 1px 6px rgba(35, 48, 47, 0.28);
}
.hint {
margin-top: 12px;
padding-top: 11px;
border-top: 1px solid var(--line);
color: var(--muted);
font-size: 10px;
line-height: 1.5;
}
#labels {
position: fixed;
inset: 0;
z-index: 4;
overflow: hidden;
pointer-events: none;
}
.part-label {
position: absolute;
display: flex;
align-items: center;
gap: 8px;
min-width: max-content;
padding: 7px 10px 7px 7px;
border: 1px solid rgba(35, 48, 47, 0.13);
border-radius: 999px;
background: rgba(248, 244, 236, 0.86);
box-shadow: 0 7px 22px rgba(41, 51, 47, 0.08);
transform: translate(-50%, -50%);
backdrop-filter: blur(8px);
}
.part-label .number {
display: grid;
width: 23px;
height: 23px;
place-items: center;
border-radius: 50%;
color: white;
font-size: 9px;
font-weight: 800;
letter-spacing: 0.03em;
}
.part-label strong,
.part-label small {
display: block;
}
.part-label strong {
font-size: 11px;
font-weight: 730;
letter-spacing: 0.01em;
}
.part-label small {
margin-top: 1px;
color: var(--muted);
font-size: 9px;
line-height: 1.2;
}
#datum-note {
position: fixed;
bottom: clamp(20px, 4vw, 43px);
left: 50%;
z-index: 5;
display: flex;
align-items: center;
gap: 9px;
padding: 8px 12px;
border: 1px solid var(--line);
border-radius: 999px;
color: var(--muted);
background: rgba(248, 244, 236, 0.68);
font-size: 9px;
letter-spacing: 0.04em;
transform: translateX(-50%);
pointer-events: none;
backdrop-filter: blur(8px);
}
.datum-symbol {
position: relative;
width: 19px;
height: 10px;
border-top: 1px dashed rgba(35, 48, 47, 0.48);
}
.datum-symbol::after {
position: absolute;
top: -4px;
left: 7px;
width: 5px;
height: 5px;
border: 1px solid rgba(35, 48, 47, 0.48);
border-radius: 50%;
content: "";
}
#loader {
position: fixed;
inset: 0;
z-index: 20;
display: grid;
place-items: center;
background: var(--paper);
transition: opacity 0.45s ease;
}
#loader::before {
width: 30px;
height: 30px;
border: 2px solid rgba(35, 48, 47, 0.12);
border-top-color: #2e8d89;
border-radius: 50%;
content: "";
animation: spin 0.9s linear infinite;
}
.ready #loader {
opacity: 0;
pointer-events: none;
}
@keyframes spin {
to { transform: rotate(360deg); }
}
@media (max-width: 760px) {
header {
max-width: 250px;
}
header p {
display: none;
}
#controls {
top: auto;
right: 16px;
bottom: 16px;
width: 190px;
}
.part-label small,
#datum-note {
display: none;
}
.part-label {
padding: 5px;
}
.part-label > div:last-child {
display: none;
}
}
</style>
</head>
<body>
<div id="viewport"></div>
<header>
<div class="eyebrow">Mechanical study / 01</div>
<h1>Wind-up,<br>unpacked.</h1>
<p>
Four solid-form components, separated along a shared assembly axis.
Dashed guides mark each part’s original position.
</p>
</header>
<aside id="controls" aria-label="Scene controls">
<div class="control-label">
<span>Exploded spacing</span>
<output id="explodeValue">100%</output>
</div>
<input
id="explode"
type="range"
min="0"
max="100"
value="100"
aria-label="Exploded spacing"
>
<div class="hint">
Drag to orbit · Scroll to zoom<br>
Double-click or press R to reset
</div>
</aside>
<div id="labels" aria-hidden="true"></div>
<div id="datum-note">
<span class="datum-symbol"></span>
Dashed guides indicate original assembly positions
</div>
<div id="loader" aria-label="Loading scene"></div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const viewport = document.getElementById("viewport");
const explodeInput = document.getElementById("explode");
const explodeValue = document.getElementById("explodeValue");
const labelsRoot = document.getElementById("labels");
// -------------------------------------------------------------------------
// Renderer, camera, and lighting
// -------------------------------------------------------------------------
const renderer = new THREE.WebGLRenderer({
antialias: true,
alpha: false,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.08;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.domElement.setAttribute("aria-label", "Interactive exploded wind-up toy scene");
viewport.appendChild(renderer.domElement);
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xf1eadf);
scene.fog = new THREE.Fog(0xf1eadf, 24, 42);
const camera = new THREE.OrthographicCamera(-1, 1, 1, -1, 0.1, 100);
const cameraTarget = new THREE.Vector3(0, -0.05, 0);
const cameraState = {
theta: 0.28,
phi: 1.16,
zoom: 1
};
const defaultCamera = { ...cameraState };
function updateCamera() {
const radius = 18;
const sinPhi = Math.sin(cameraState.phi);
camera.position.set(
cameraTarget.x + radius * sinPhi * Math.sin(cameraState.theta),
cameraTarget.y + radius * Math.cos(cameraState.phi),
cameraTarget.z + radius * sinPhi * Math.cos(cameraState.theta)
);
camera.lookAt(cameraTarget);
camera.zoom = cameraState.zoom;
camera.updateProjectionMatrix();
}
function resize() {
const width = window.innerWidth;
const height = window.innerHeight;
const aspect = width / height;
// Keep the full exploded row visible on both wide and narrow screens.
const viewHeight = Math.max(8.35, 13.9 / aspect);
camera.left = (-viewHeight * aspect) / 2;
camera.right = (viewHeight * aspect) / 2;
camera.top = viewHeight / 2;
camera.bottom = -viewHeight / 2;
camera.updateProjectionMatrix();
renderer.setSize(width, height);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
}
window.addEventListener("resize", resize);
resize();
const hemisphere = new THREE.HemisphereLight(
0xfff9ec,
0x84928c,
2.0
);
scene.add(hemisphere);
const keyLight = new THREE.DirectionalLight(0xfff2d8, 3.5);
keyLight.position.set(-5, 10, 8);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -11;
keyLight.shadow.camera.right = 11;
keyLight.shadow.camera.top = 8;
keyLight.shadow.camera.bottom = -8;
keyLight.shadow.camera.near = 1;
keyLight.shadow.camera.far = 30;
keyLight.shadow.bias = -0.0006;
keyLight.shadow.normalBias = 0.025;
scene.add(keyLight);
const fillLight = new THREE.DirectionalLight(0xc8e7e5, 1.15);
fillLight.position.set(7, 4, -7);
scene.add(fillLight);
const warmFill = new THREE.PointLight(0xffc987, 18, 28, 2);
warmFill.position.set(-7, 2, 4);
scene.add(warmFill);
// -------------------------------------------------------------------------
// Materials and geometry helpers
// -------------------------------------------------------------------------
const materials = {
casing: new THREE.MeshStandardMaterial({
color: 0x318f8c,
roughness: 0.52,
metalness: 0.04
}),
casingInset: new THREE.MeshStandardMaterial({
color: 0x226f70,
roughness: 0.62,
metalness: 0.02
}),
casingEdge: new THREE.MeshStandardMaterial({
color: 0x65b4ac,
roughness: 0.5,
metalness: 0.03
}),
cream: new THREE.MeshStandardMaterial({
color: 0xf2dfbd,
roughness: 0.58,
metalness: 0.02
}),
coral: new THREE.MeshStandardMaterial({
color: 0xdf654d,
roughness: 0.58,
metalness: 0.02
}),
coralDark: new THREE.MeshStandardMaterial({
color: 0xb84637,
roughness: 0.72,
metalness: 0.01
}),
brass: new THREE.MeshStandardMaterial({
color: 0xd69b2e,
roughness: 0.36,
metalness: 0.38
}),
brassDark: new THREE.MeshStandardMaterial({
color: 0x9f6d1f,
roughness: 0.44,
metalness: 0.32
}),
steel: new THREE.MeshStandardMaterial({
color: 0x89999c,
roughness: 0.28,
metalness: 0.62
}),
steelDark: new THREE.MeshStandardMaterial({
color: 0x4c5d60,
roughness: 0.34,
metalness: 0.55
}),
dark: new THREE.MeshStandardMaterial({
color: 0x263638,
roughness: 0.64,
metalness: 0.12
}),
ground: new THREE.MeshStandardMaterial({
color: 0xeee6da,
roughness: 0.96,
metalness: 0
})
};
function roundedRectGeometry(
width,
height,
radius,
depth,
bevel = 0.05
) {
const w = width / 2;
const h = height / 2;
const r = Math.min(radius, w, h);
const shape = new THREE.Shape();
shape.moveTo(-w + r, -h);
shape.lineTo(w - r, -h);
shape.quadraticCurveTo(w, -h, w, -h + r);
shape.lineTo(w, h - r);
shape.quadraticCurveTo(w, h, w - r, h);
shape.lineTo(-w + r, h);
shape.quadraticCurveTo(-w, h, -w, h - r);
shape.lineTo(-w, -h + r);
shape.quadraticCurveTo(-w, -h, -w + r, -h);
shape.closePath();
const geometry = new THREE.ExtrudeGeometry(shape, {
depth,
bevelEnabled: bevel > 0,
bevelThickness: bevel,
bevelSize: bevel,
bevelSegments: 3,
curveSegments: 12,
steps: 1
});
geometry.center();
return geometry;
}
function addMesh(parent, geometry, material, position = [0, 0, 0]) {
const mesh = new THREE.Mesh(geometry, material);
mesh.position.set(...position);
parent.add(mesh);
return mesh;
}
function addCylinder(
parent,
radius,
depth,
material,
position = [0, 0, 0],
segments = 32
) {
const mesh = addMesh(
parent,
new THREE.CylinderGeometry(radius, radius, depth, segments),
material,
position
);
mesh.rotation.x = Math.PI / 2;
return mesh;
}
function enableShadows(group) {
group.traverse((object) => {
if (object.isMesh) {
object.castShadow = true;
object.receiveShadow = true;
}
});
}
// -------------------------------------------------------------------------
// Ground
// -------------------------------------------------------------------------
const ground = addMesh(
scene,
new THREE.PlaneGeometry(80, 80),
materials.ground,
[0, -1.48, 0]
);
ground.rotation.x = -Math.PI / 2;
ground.receiveShadow = true;
// -------------------------------------------------------------------------
// 01: Casing
// -------------------------------------------------------------------------
const casing = new THREE.Group();
addMesh(
casing,
roundedRectGeometry(3.5, 2.05, 0.55, 0.72, 0.09),
materials.casing
);
addMesh(
casing,
roundedRectGeometry(2.62, 1.25, 0.34, 0.08, 0.025),
materials.casingInset,
[0, 0, 0.405]
);
// Fake recessed axle opening and its raised rim.
addCylinder(
casing,
0.49,
0.09,
materials.dark,
[0.67, -0.02, 0.49],
40
);
const casingRim = addMesh(
casing,
new THREE.TorusGeometry(0.52, 0.085, 16, 48),
materials.casingEdge,
[0.67, -0.02, 0.55]
);
casingRim.castShadow = true;
// Small wind-up dial.
addCylinder(
casing,
0.3,
0.18,
materials.brass,
[-0.83, 0.24, 0.53],
36
);
addMesh(
casing,
new THREE.BoxGeometry(0.075, 0.28, 0.055),
materials.dark,
[-0.83, 0.24, 0.64]
);
// Decorative switch.
addMesh(
casing,
roundedRectGeometry(0.47, 0.18, 0.07, 0.09, 0.018),
materials.coral,
[-0.83, -0.37, 0.51]
);
// Four inset screw heads.
for (const x of [-1.08, 1.08]) {
for (const y of [-0.43, 0.43]) {
addCylinder(
casing,
0.085,
0.065,
materials.steel,
[x, y, 0.49],
18
);
addMesh(
casing,
new THREE.BoxGeometry(0.095, 0.018, 0.018),
materials.dark,
[x, y, 0.535]
);
}
}
enableShadows(casing);
// -------------------------------------------------------------------------
// 02: Spacer
// -------------------------------------------------------------------------
const spacer = new THREE.Group();
// Closed lathed profile creates a substantial washer with a real center hole.
const spacerProfile = [
new THREE.Vector2(0.34, -0.18),
new THREE.Vector2(0.66, -0.18),
new THREE.Vector2(0.77, -0.10),
new THREE.Vector2(0.77, 0.10),
new THREE.Vector2(0.66, 0.18),
new THREE.Vector2(0.34, 0.18),
new THREE.Vector2(0.29, 0.10),
new THREE.Vector2(0.29, -0.10),
new THREE.Vector2(0.34, -0.18)
];
const spacerBody = addMesh(
spacer,
new THREE.LatheGeometry(spacerProfile, 48),
materials.brass
);
spacerBody.rotation.x = Math.PI / 2;
for (const z of [-0.17, 0.17]) {
addMesh(
spacer,
new THREE.TorusGeometry(0.55, 0.025, 10, 40),
materials.brassDark,
[0, 0, z]
);
}
enableShadows(spacer);
// -------------------------------------------------------------------------
// 03: Axle
// -------------------------------------------------------------------------
const axle = new THREE.Group();
addCylinder(
axle,
0.14,
2.85,
materials.steel,
[0, 0, 0],
20
);
// Flattened section suggests a drive shaft rather than a plain pin.
addMesh(
axle,
new THREE.BoxGeometry(0.27, 0.19, 0.82),
materials.steelDark,
[0, 0, 0.43]
);
addCylinder(
axle,
0.255,
0.3,
materials.steelDark,
[0, 0, 0.86],
18
);
const axleTipFront = addMesh(
axle,
new THREE.ConeGeometry(0.19, 0.3, 18),
materials.steel,
[0, 0, 1.52]
);
axleTipFront.rotation.x = Math.PI / 2;
const axleTipBack = addMesh(
axle,
new THREE.ConeGeometry(0.19, 0.3, 18),
materials.steel,
[0, 0, -1.52]
);
axleTipBack.rotation.x = -Math.PI / 2;
enableShadows(axle);
// -------------------------------------------------------------------------
// 04: Wheel
// -------------------------------------------------------------------------
const wheel = new THREE.Group();
// Chunky toy tire.
addMesh(
wheel,
new THREE.TorusGeometry(1.0, 0.36, 20, 64),
materials.coral
);
// Rounded-looking tread blocks distributed around the tire.
for (let i = 0; i < 20; i++) {
const angle = (i / 20) * Math.PI * 2;
const tread = addMesh(
wheel,
new THREE.BoxGeometry(0.26, 0.34, 0.7),
materials.coralDark,
[Math.cos(angle) * 1.29, Math.sin(angle) * 1.29, 0]
);
tread.rotation.z = angle - Math.PI / 2;
}
// Six broad spokes.
for (let i = 0; i < 6; i++) {
const angle = (i / 6) * Math.PI * 2;
const spoke = addMesh(
wheel,
new THREE.BoxGeometry(0.21, 0.66, 0.48),
materials.cream,
[Math.sin(angle) * 0.48, Math.cos(angle) * 0.48, 0]
);
spoke.rotation.z = -angle;
}
addCylinder(
wheel,
0.5,
0.65,
materials.cream,
[0, 0, 0],
40
);
addCylinder(
wheel,
0.23,
0.77,
materials.brass,
[0, 0, 0.02],
32
);
addMesh(
wheel,
new THREE.TorusGeometry(0.36, 0.035, 10, 36),
materials.brassDark,
[0, 0, 0.345]
);
enableShadows(wheel);
// -------------------------------------------------------------------------
// Exploded positions, original assembly locations, and labels
// -------------------------------------------------------------------------
const parts = [
{
name: "Casing",
subtitle: "wind-up housing",
number: "01",
color: "#318f8c",
group: casing,
originalZ: -1.58,
explodedX: -4.85,
anchor: new THREE.Vector3(0, -1.36, 0.25)
},
{
name: "Spacer",
subtitle: "brass washer",
number: "02",
color: "#d69b2e",
group: spacer,
originalZ: -0.55,
explodedX: -2.35,
anchor: new THREE.Vector3(0, -1.02, 0)
},
{
name: "Axle",
subtitle: "drive shaft",
number: "03",
color: "#687d81",
group: axle,
originalZ: 0.35,
explodedX: 0.24,
anchor: new THREE.Vector3(0, -0.56, 0.05)
},
{
name: "Wheel",
subtitle: "toy wheel",
number: "04",
color: "#df654d",
group: wheel,
originalZ: 1.36,
explodedX: 4.15,
anchor: new THREE.Vector3(0, -1.72, 0)
}
];
const assemblyY = 0.2;
const guideMaterial = new THREE.LineDashedMaterial({
color: 0x64716e,
transparent: true,
opacity: 0.4,
dashSize: 0.13,
gapSize: 0.1,
depthWrite: false
});
for (const part of parts) {
part.group.position.set(0, assemblyY, part.originalZ);
scene.add(part.group);
const geometry = new THREE.BufferGeometry().setFromPoints([
new THREE.Vector3(0, assemblyY, part.originalZ),
new THREE.Vector3(0, assemblyY, part.originalZ)
]);
const line = new THREE.Line(geometry, guideMaterial.clone());
line.computeLineDistances();
scene.add(line);
part.guide = line;
const markerMaterial = new THREE.MeshBasicMaterial({
color: 0x6d7975,
transparent: true,
opacity: 0.48,
depthWrite: false
});
const marker = addMesh(
scene,
new THREE.TorusGeometry(0.13, 0.017, 8, 28),
markerMaterial,
[0, assemblyY, part.originalZ]
);
marker.rotation.y = Math.PI / 2;
part.marker = marker;
const label = document.createElement("div");
label.className = "part-label";
label.innerHTML = `
<span class="number" style="background:${part.color}">${part.number}</span>
<div>
<strong>${part.name}</strong>
<small>${part.subtitle}</small>
</div>
`;
labelsRoot.appendChild(label);
part.label = label;
}
// Shared original assembly axis.
const axisMaterial = new THREE.LineDashedMaterial({
color: 0x687572,
transparent: true,
opacity: 0.24,
dashSize: 0.08,
gapSize: 0.085,
depthWrite: false
});
const assemblyAxis = new THREE.Line(
new THREE.BufferGeometry().setFromPoints([
new THREE.Vector3(0, assemblyY, -2.12),
new THREE.Vector3(0, assemblyY, 2.08)
]),
axisMaterial
);
assemblyAxis.computeLineDistances();
scene.add(assemblyAxis);
// -------------------------------------------------------------------------
// Explosion slider
// -------------------------------------------------------------------------
let currentExplosion = 1;
let targetExplosion = 1;
function setExplosion(value) {
targetExplosion = value;
const percentage = Math.round(value * 100);
explodeValue.textContent = `${percentage}%`;
explodeInput.style.setProperty("--progress", `${percentage}%`);
}
explodeInput.addEventListener("input", () => {
setExplosion(Number(explodeInput.value) / 100);
});
setExplosion(1);
// -------------------------------------------------------------------------
// Custom camera controls — no external controls addon
// -------------------------------------------------------------------------
let dragging = false;
let activePointer = null;
let lastX = 0;
let lastY = 0;
const canvas = renderer.domElement;
canvas.addEventListener("pointerdown", (event) => {
if (event.pointerType === "mouse" && event.button !== 0) return;
dragging = true;
activePointer = event.pointerId;
lastX = event.clientX;
lastY = event.clientY;
canvas.setPointerCapture(event.pointerId);
});
canvas.addEventListener("pointermove", (event) => {
if (!dragging || event.pointerId !== activePointer) return;
const dx = event.clientX - lastX;
const dy = event.clientY - lastY;
cameraState.theta -= dx * 0.006;
cameraState.phi = THREE.MathUtils.clamp(
cameraState.phi + dy * 0.005,
0.62,
1.43
);
lastX = event.clientX;
lastY = event.clientY;
});
function endDrag(event) {
if (event.pointerId !== activePointer) return;
dragging = false;
activePointer = null;
}
canvas.addEventListener("pointerup", endDrag);
canvas.addEventListener("pointercancel", endDrag);
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraState.zoom = THREE.MathUtils.clamp(
cameraState.zoom * Math.exp(-event.deltaY * 0.001),
0.68,
1.75
);
},
{ passive: false }
);
function resetCamera() {
cameraState.theta = defaultCamera.theta;
cameraState.phi = defaultCamera.phi;
cameraState.zoom = defaultCamera.zoom;
}
canvas.addEventListener("dblclick", resetCamera);
window.addEventListener("keydown", (event) => {
if (event.key.toLowerCase() === "r") resetCamera();
});
// -------------------------------------------------------------------------
// Animation
// -------------------------------------------------------------------------
const projected = new THREE.Vector3();
function updatePartPositions(delta) {
const easing = 1 - Math.exp(-7 * delta);
currentExplosion = THREE.MathUtils.lerp(
currentExplosion,
targetExplosion,
easing
);
for (const part of parts) {
const x = part.explodedX * currentExplosion;
part.group.position.x = x;
const positions = part.guide.geometry.attributes.position;
positions.setXYZ(0, x, assemblyY, part.originalZ);
positions.setXYZ(1, 0, assemblyY, part.originalZ);
positions.needsUpdate = true;
part.guide.computeLineDistances();
part.guide.material.opacity = 0.4 * currentExplosion;
part.marker.material.opacity = 0.48 * currentExplosion;
}
assemblyAxis.material.opacity = 0.24 * currentExplosion;
}
function updateLabels() {
const width = window.innerWidth;
const height = window.innerHeight;
for (const part of parts) {
projected.copy(part.anchor);
part.group.localToWorld(projected);
projected.project(camera);
const x = (projected.x * 0.5 + 0.5) * width;
const y = (-projected.y * 0.5 + 0.5) * height;
part.label.style.left = `${x}px`;
part.label.style.top = `${y}px`;
part.label.style.opacity =
projected.z < -1 || projected.z > 1 ? "0" : "1";
}
}
const clock = new THREE.Clock();
let firstFrame = true;
function animate() {
const delta = Math.min(clock.getDelta(), 0.05);
updatePartPositions(delta);
updateCamera();
updateLabels();
renderer.render(scene, camera);
if (firstFrame) {
firstFrame = false;
requestAnimationFrame(() => document.body.classList.add("ready"));
}
}
renderer.setAnimationLoop(animate);
</script>
</body>
</html>
```