Prompt specification
Build or customize the Three.js 3D scene "Discrete belt adjustment sequence" 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: Discrete belt adjustment sequence
Category: Everyday & Industrial · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/discrete-belt-adjustment-sequence-x0566
Scene Description Prompt:
Create a single self-contained HTML file using Three.js from cdnjs that shows belt buckles. Animate the pin lifting, strap sliding, and pin entering a neighboring hole; the loop size changes only while the pin is disengaged from the strap. Low-poly.
Working Three.js Starter (discrete-belt-adjustment-sequence-x0566):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Low-Poly Belt Buckle Mechanism</title>
<style>
:root {
--bg: #090d13;
--panel: rgba(17, 23, 32, 0.82);
--line: rgba(255, 255, 255, 0.12);
--text: #eef2f7;
--muted: #9ba8b8;
--gold: #f0ad55;
--green: #78dfad;
--blue: #7ebcff;
}
* { box-sizing: border-box; }
html, body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: var(--bg);
color: var(--text);
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;
}
#ui {
position: fixed;
inset: 0;
pointer-events: none;
padding: 24px;
}
.top {
display: flex;
align-items: flex-start;
justify-content: space-between;
gap: 20px;
}
.brand {
max-width: 420px;
padding: 16px 18px;
border: 1px solid var(--line);
border-radius: 16px;
background: linear-gradient(135deg, rgba(19, 27, 38, 0.92), rgba(11, 15, 22, 0.65));
backdrop-filter: blur(12px);
box-shadow: 0 18px 50px rgba(0, 0, 0, 0.22);
}
.eyebrow {
margin-bottom: 7px;
color: var(--gold);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.22em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: clamp(22px, 3vw, 38px);
line-height: 1;
letter-spacing: -0.045em;
}
.subtitle {
margin: 9px 0 0;
color: var(--muted);
font-size: 12px;
line-height: 1.5;
}
.status {
min-width: 220px;
padding: 13px 15px;
border: 1px solid var(--line);
border-radius: 14px;
background: var(--panel);
backdrop-filter: blur(12px);
}
.status-row {
display: flex;
align-items: center;
justify-content: space-between;
gap: 14px;
}
.status-label {
color: var(--muted);
font-size: 9px;
font-weight: 800;
letter-spacing: 0.16em;
text-transform: uppercase;
}
#state {
font-size: 12px;
font-weight: 800;
letter-spacing: 0.08em;
}
#state::before {
content: "";
display: inline-block;
width: 7px;
height: 7px;
margin-right: 8px;
border-radius: 50%;
background: var(--gold);
box-shadow: 0 0 14px currentColor;
color: var(--gold);
vertical-align: 1px;
}
.status[data-state="slide"] #state { color: var(--blue); }
.status[data-state="slide"] #state::before { color: var(--blue); background: var(--blue); }
.status[data-state="locked"] #state { color: var(--green); }
.status[data-state="locked"] #state::before { color: var(--green); background: var(--green); }
.progress {
height: 3px;
margin-top: 12px;
overflow: hidden;
border-radius: 10px;
background: rgba(255, 255, 255, 0.09);
}
#progressFill {
width: 100%;
height: 100%;
transform-origin: left;
background: linear-gradient(90deg, var(--gold), #ffe0a7);
}
.bottom {
position: absolute;
left: 24px;
right: 24px;
bottom: 24px;
display: flex;
align-items: flex-end;
justify-content: space-between;
gap: 18px;
}
.readout {
width: min(460px, calc(100vw - 48px));
padding: 15px 17px;
border: 1px solid var(--line);
border-radius: 16px;
background: var(--panel);
backdrop-filter: blur(12px);
}
.mechanism-note {
display: flex;
align-items: center;
gap: 9px;
margin-bottom: 13px;
color: var(--muted);
font-size: 11px;
}
.mechanism-note strong {
color: var(--green);
font-weight: 750;
}
.metrics {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 16px;
}
.metric-label {
margin-bottom: 5px;
color: var(--muted);
font-size: 9px;
font-weight: 800;
letter-spacing: 0.13em;
text-transform: uppercase;
}
.metric-value {
font-variant-numeric: tabular-nums;
font-size: 17px;
font-weight: 750;
letter-spacing: -0.025em;
}
.controls {
display: flex;
align-items: center;
gap: 8px;
pointer-events: auto;
}
button {
min-height: 42px;
padding: 0 15px;
border: 1px solid var(--line);
border-radius: 12px;
background: rgba(20, 27, 38, 0.88);
color: var(--text);
font: inherit;
font-size: 11px;
font-weight: 750;
letter-spacing: 0.04em;
cursor: pointer;
backdrop-filter: blur(10px);
transition: background 150ms ease, border-color 150ms ease, transform 150ms ease;
}
button:hover {
border-color: rgba(240, 173, 85, 0.55);
background: rgba(36, 43, 53, 0.94);
transform: translateY(-1px);
}
button:active { transform: translateY(0); }
#pause {
min-width: 92px;
color: #10151c;
border-color: transparent;
background: var(--gold);
}
.hint {
position: absolute;
right: 24px;
bottom: 76px;
color: rgba(220, 228, 238, 0.48);
font-size: 9px;
font-weight: 750;
letter-spacing: 0.1em;
text-transform: uppercase;
}
#annotation {
position: fixed;
left: 0;
top: 0;
z-index: 4;
pointer-events: none;
opacity: 0;
transform: translate(-50%, -100%);
transition: opacity 120ms ease;
white-space: nowrap;
}
#annotation.visible { opacity: 1; }
#annotation .tag {
position: relative;
padding: 8px 10px;
border: 1px solid rgba(240, 173, 85, 0.45);
border-radius: 8px;
background: rgba(12, 16, 23, 0.9);
color: #ffe2b6;
font-size: 9px;
font-weight: 800;
letter-spacing: 0.13em;
text-transform: uppercase;
box-shadow: 0 10px 28px rgba(0, 0, 0, 0.3);
}
#annotation .tag::after {
content: "";
position: absolute;
left: 50%;
top: 100%;
width: 1px;
height: 22px;
background: linear-gradient(var(--gold), transparent);
}
@media (max-width: 700px) {
#ui { padding: 13px; }
.brand { max-width: calc(100vw - 26px); padding: 13px 14px; }
.subtitle { display: none; }
.status { display: none; }
.bottom { left: 13px; right: 13px; bottom: 13px; }
.readout { width: calc(100vw - 26px); padding: 13px; }
.controls {
position: absolute;
right: 0;
bottom: 130px;
}
.hint { display: none; }
.metrics { gap: 8px; }
.metric-value { font-size: 14px; }
}
</style>
</head>
<body>
<div id="ui">
<div class="top">
<div class="brand">
<div class="eyebrow">Mechanism study / 03</div>
<h1>Belt buckle motion</h1>
<p class="subtitle">
A low-poly tang buckle. The pin clears the leather before the strap and adjustable loop move.
</p>
</div>
<div class="status" id="status" data-state="locked">
<div class="status-row">
<span class="status-label">Mechanism</span>
<span id="state">LOCKED</span>
</div>
<div class="progress"><div id="progressFill"></div></div>
</div>
</div>
<div class="bottom">
<div class="readout">
<div class="mechanism-note">
<strong>Constraint satisfied</strong>
<span>Loop adjustment is permitted only while the pin is clear.</span>
</div>
<div class="metrics">
<div>
<div class="metric-label">Pin angle</div>
<div class="metric-value" id="pinMetric">0.0°</div>
</div>
<div>
<div class="metric-label">Strap offset</div>
<div class="metric-value" id="strapMetric">0.00</div>
</div>
<div>
<div class="metric-label">Loop size</div>
<div class="metric-value" id="loopMetric">100%</div>
</div>
</div>
</div>
<div class="controls">
<button id="pause" type="button">Pause</button>
<button id="restart" type="button">Restart</button>
<button id="cameraReset" type="button">Reset view</button>
</div>
</div>
<div class="hint">Drag to orbit · Scroll to zoom · Space to pause</div>
</div>
<div id="annotation"><div class="tag" id="annotationText">Engaged</div></div>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x090d13);
scene.fog = new THREE.Fog(0x090d13, 11, 22);
const camera = new THREE.PerspectiveCamera(42, innerWidth / innerHeight, 0.1, 50);
const renderer = new THREE.WebGLRenderer({ antialias: true, powerPreference: "high-performance" });
renderer.setSize(innerWidth, innerHeight);
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.1;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
document.body.prepend(renderer.domElement);
const clock = new THREE.Clock();
// ------------------------------------------------------------------
// Lighting and low-poly environment
// ------------------------------------------------------------------
scene.add(new THREE.HemisphereLight(0xbdd7ff, 0x17100b, 1.45));
const key = new THREE.DirectionalLight(0xffe0b3, 4.2);
key.position.set(-4, 7, 6);
key.castShadow = true;
key.shadow.mapSize.set(1536, 1536);
key.shadow.camera.left = -8;
key.shadow.camera.right = 8;
key.shadow.camera.top = 7;
key.shadow.camera.bottom = -5;
key.shadow.camera.near = 0.5;
key.shadow.camera.far = 20;
key.shadow.bias = -0.0005;
scene.add(key);
const rim = new THREE.DirectionalLight(0x6baaff, 2.2);
rim.position.set(5, 2, -5);
scene.add(rim);
const warm = new THREE.PointLight(0xff9b45, 13, 8, 2);
warm.position.set(3.5, -1, 2.5);
scene.add(warm);
const floor = new THREE.Mesh(
new THREE.PlaneGeometry(30, 22),
new THREE.MeshStandardMaterial({ color: 0x10151d, roughness: 0.95, metalness: 0.02 })
);
floor.rotation.x = -Math.PI / 2;
floor.position.y = -1.58;
floor.receiveShadow = true;
scene.add(floor);
const pedestal = new THREE.Mesh(
new THREE.CylinderGeometry(3.25, 3.5, 0.18, 20),
new THREE.MeshStandardMaterial({ color: 0x171e28, roughness: 0.76, metalness: 0.18 })
);
pedestal.position.set(0.15, -1.47, 0);
pedestal.receiveShadow = true;
pedestal.castShadow = true;
scene.add(pedestal);
// ------------------------------------------------------------------
// Shared geometries and materials
// ------------------------------------------------------------------
const frameShape = new THREE.Shape();
frameShape.moveTo(-1.55, -0.69);
frameShape.lineTo(-1.31, -0.94);
frameShape.lineTo(1.31, -0.94);
frameShape.lineTo(1.55, -0.69);
frameShape.lineTo(1.55, 0.69);
frameShape.lineTo(1.31, 0.94);
frameShape.lineTo(-1.31, 0.94);
frameShape.lineTo(-1.55, 0.69);
frameShape.closePath();
const frameOpening = new THREE.Path();
frameOpening.moveTo(-1.21, -0.44);
frameOpening.lineTo(-1.06, -0.63);
frameOpening.lineTo(1.06, -0.63);
frameOpening.lineTo(1.21, -0.44);
frameOpening.lineTo(1.21, 0.44);
frameOpening.lineTo(1.06, 0.63);
frameOpening.lineTo(-1.06, 0.63);
frameOpening.lineTo(-1.21, 0.44);
frameOpening.closePath();
frameShape.holes.push(frameOpening);
const frameGeometry = new THREE.ExtrudeGeometry(frameShape, {
depth: 0.22,
steps: 1,
bevelEnabled: false,
curveSegments: 1
});
const frameEdges = new THREE.EdgesGeometry(frameGeometry, 28);
const bronze = new THREE.MeshStandardMaterial({
color: 0xc58b48,
roughness: 0.29,
metalness: 0.82,
flatShading: true
});
const darkMetal = new THREE.MeshStandardMaterial({
color: 0x313944,
roughness: 0.34,
metalness: 0.78,
flatShading: true
});
const silver = new THREE.MeshStandardMaterial({
color: 0xd5dce3,
roughness: 0.23,
metalness: 0.9,
flatShading: true
});
const leather = new THREE.MeshStandardMaterial({
color: 0x75452d,
roughness: 0.88,
metalness: 0.02,
flatShading: true
});
const edgeMaterial = new THREE.LineBasicMaterial({
color: 0xffd08a,
transparent: true,
opacity: 0.26
});
function createFrame(material = bronze) {
const frame = new THREE.Mesh(frameGeometry, material);
frame.position.z = 0.18;
frame.castShadow = true;
frame.receiveShadow = true;
const edges = new THREE.LineSegments(frameEdges, edgeMaterial);
frame.add(edges);
return frame;
}
function createPin(pinMaterial = silver) {
const pivot = new THREE.Vector3(1.38, 0.42, 0);
const target = new THREE.Vector3(0.50, 0, 0);
const dx = target.x - pivot.x;
const dy = target.y - pivot.y;
const length = Math.hypot(dx, dy);
const baseAngle = Math.atan2(dx, -dy);
const group = new THREE.Group();
group.position.copy(pivot);
const shaftLength = length - 0.14;
const shaft = new THREE.Mesh(
new THREE.CylinderGeometry(0.066, 0.075, shaftLength, 8),
pinMaterial
);
shaft.position.y = -(0.07 + shaftLength / 2);
shaft.castShadow = true;
group.add(shaft);
const tip = new THREE.Mesh(
new THREE.ConeGeometry(0.075, 0.17, 8),
pinMaterial
);
tip.position.y = -(length - 0.025);
tip.rotation.z = Math.PI;
tip.castShadow = true;
group.add(tip);
const rivet = new THREE.Mesh(
new THREE.CylinderGeometry(0.13, 0.13, 0.12, 10),
pinMaterial
);
rivet.rotation.x = Math.PI / 2;
rivet.position.z = 0.13;
rivet.castShadow = true;
group.add(rivet);
group.rotation.z = baseAngle;
group.userData = { baseAngle, length, pivot };
return group;
}
function createShortBelt(color) {
const shape = new THREE.Shape();
shape.moveTo(-2.35, -0.34);
shape.lineTo(-2.18, -0.42);
shape.lineTo(1.02, -0.42);
shape.lineTo(1.15, -0.3);
shape.lineTo(1.15, 0.3);
shape.lineTo(1.02, 0.42);
shape.lineTo(-2.18, 0.42);
shape.closePath();
const hole = new THREE.Path();
hole.absarc(0.50, 0, 0.105, 0, Math.PI * 2, true);
shape.holes.push(hole);
const geometry = new THREE.ExtrudeGeometry(shape, {
depth: 0.15,
steps: 1,
bevelEnabled: false,
curveSegments: 12
});
const mesh = new THREE.Mesh(
geometry,
new THREE.MeshStandardMaterial({ color, roughness: 0.9, metalness: 0.02 })
);
mesh.castShadow = true;
mesh.receiveShadow = true;
return mesh;
}
// ------------------------------------------------------------------
// Display buckles in the background
// ------------------------------------------------------------------
function createDisplayBuckle(frameMaterial, beltColor, accent) {
const group = new THREE.Group();
const plaque = new THREE.Mesh(
new THREE.CircleGeometry(1.46, 8),
new THREE.MeshStandardMaterial({ color: accent, roughness: 0.92, metalness: 0.03 })
);
plaque.position.z = -0.36;
plaque.receiveShadow = true;
group.add(plaque);
group.add(createShortBelt(beltColor));
group.add(createFrame(frameMaterial));
const pin = createPin(silver);
pin.position.z += 0.08;
group.add(pin);
return group;
}
const leftDisplay = createDisplayBuckle(darkMetal, 0x3c3f45, 0x18202b);
leftDisplay.position.set(-3.55, 1.52, -1.55);
leftDisplay.scale.setScalar(0.55);
leftDisplay.rotation.set(-0.05, 0.12, 0.1);
scene.add(leftDisplay);
const rightDisplay = createDisplayBuckle(bronze, 0x253d36, 0x172823);
rightDisplay.position.set(3.52, 1.62, -2.0);
rightDisplay.scale.setScalar(0.51);
rightDisplay.rotation.set(0.06, -0.12, -0.08);
scene.add(rightDisplay);
// ------------------------------------------------------------------
// Main animated buckle and belt
// ------------------------------------------------------------------
const mainRig = new THREE.Group();
mainRig.position.set(0.2, -0.12, 0);
scene.add(mainRig);
mainRig.add(createFrame(bronze));
const centerBar = new THREE.Mesh(
new THREE.BoxGeometry(0.19, 1.28, 0.27),
bronze
);
centerBar.position.set(-0.38, 0, 0.31);
centerBar.castShadow = true;
mainRig.add(centerBar);
const strapMotion = new THREE.Group();
mainRig.add(strapMotion);
const strapShape = new THREE.Shape();
strapShape.moveTo(-4.25, -0.4);
strapShape.lineTo(-4.08, -0.49);
strapShape.lineTo(1.98, -0.49);
strapShape.lineTo(2.18, -0.36);
strapShape.lineTo(2.18, 0.36);
strapShape.lineTo(1.98, 0.49);
strapShape.lineTo(-4.08, 0.49);
strapShape.closePath();
const holePositions = [0.5, 1.2, 1.9];
for (const x of holePositions) {
const hole = new THREE.Path();
hole.absarc(x, 0, 0.105, 0, Math.PI * 2, true);
strapShape.holes.push(hole);
}
const strapGeometry = new THREE.ExtrudeGeometry(strapShape, {
depth: 0.2,
steps: 1,
bevelEnabled: false,
curveSegments: 12
});
const strap = new THREE.Mesh(strapGeometry, leather);
strap.castShadow = true;
strap.receiveShadow = true;
strapMotion.add(strap);
const strapEdges = new THREE.LineSegments(
new THREE.EdgesGeometry(strapGeometry, 34),
new THREE.LineBasicMaterial({ color: 0xc38b62, transparent: true, opacity: 0.28 })
);
strap.add(strapEdges);
// Stitch dashes, also moving with the strap.
const stitchCount = 44;
const stitchGeometry = new THREE.BoxGeometry(0.085, 0.022, 0.014);
const stitchMaterial = new THREE.MeshStandardMaterial({
color: 0xd8b28c,
roughness: 1,
metalness: 0
});
const stitches = new THREE.InstancedMesh(stitchGeometry, stitchMaterial, stitchCount * 2);
const dummy = new THREE.Object3D();
let stitchIndex = 0;
for (let side = -1; side <= 1; side += 2) {
for (let i = 0; i < stitchCount; i++) {
dummy.position.set(-3.93 + i * 0.135, side * 0.375, 0.207);
dummy.updateMatrix();
stitches.setMatrixAt(stitchIndex++, dummy.matrix);
}
}
stitches.instanceMatrix.needsUpdate = true;
strapMotion.add(stitches);
// The adjustable loop is an extruded elliptical leather band.
const loopShape = new THREE.Shape();
loopShape.absellipse(0, 0, 0.66, 0.83, 0, Math.PI * 2, false, 0);
const loopOpening = new THREE.Path();
loopOpening.absellipse(0, 0, 0.40, 0.56, 0, Math.PI * 2, true, 0);
loopShape.holes.push(loopOpening);
const loopGeometry = new THREE.ExtrudeGeometry(loopShape, {
depth: 0.14,
steps: 1,
bevelEnabled: false,
curveSegments: 12
});
const tailLoop = new THREE.Mesh(
loopGeometry,
new THREE.MeshStandardMaterial({
color: 0x5d3828,
roughness: 0.9,
metalness: 0.01,
flatShading: true
})
);
tailLoop.position.set(2.48, 0, 0.025);
tailLoop.castShadow = true;
tailLoop.receiveShadow = true;
strapMotion.add(tailLoop);
// Hole halos help show the neighboring holes without covering the openings.
const holeMarkers = [];
const markerGeometry = new THREE.TorusGeometry(0.125, 0.018, 4, 16);
for (const x of holePositions) {
const marker = new THREE.Mesh(
markerGeometry,
new THREE.MeshStandardMaterial({
color: 0xf0ad55,
emissive: 0xf0ad55,
emissiveIntensity: 1.2,
transparent: true,
opacity: 0.25,
roughness: 0.35,
metalness: 0.35
})
);
marker.position.set(x, 0, 0.213);
strapMotion.add(marker);
holeMarkers.push(marker);
}
const animatedPin = createPin(silver);
animatedPin.position.z = 0.2;
mainRig.add(animatedPin);
// ------------------------------------------------------------------
// Animation timeline
//
// The strap and loop are changed only in the two SLIDE windows.
// Both windows occur while the pin is fully lifted and disengaged.
// ------------------------------------------------------------------
const CYCLE = 12.4;
const HOLE_SPACING = 0.7;
const MAX_LOOP_SCALE = 1.18;
let elapsed = 0;
let playing = true;
let currentLoopScale = 1;
const clamp = THREE.MathUtils.clamp;
const smooth = (x) => {
const t = clamp(x, 0, 1);
return t * t * (3 - 2 * t);
};
const ramp = (t, a, b) => smooth((t - a) / (b - a));
function pinLiftAt(t) {
if (t < 1.1) return 0;
if (t < 2.5) return ramp(t, 1.1, 2.5);
if (t < 4.3) return 1;
if (t < 5.4) return 1 - ramp(t, 4.3, 5.4);
if (t < 6.6) return 0;
if (t < 8.0) return ramp(t, 6.6, 8.0);
if (t < 9.8) return 1;
if (t < 10.9) return 1 - ramp(t, 9.8, 10.9);
return 0;
}
function strapOffsetAt(t) {
if (t < 2.5) return 0;
if (t < 4.3) return -HOLE_SPACING * ramp(t, 2.5, 4.3);
if (t < 8.0) return -HOLE_SPACING;
if (t < 9.8) return -HOLE_SPACING * (1 - ramp(t, 8.0, 9.8));
return 0;
}
function phaseAt(t) {
if (t < 1.1) return { name: "LOCKED", state: "locked", hole: 1 };
if (t < 2.5) return { name: "LIFTING PIN", state: "lift", hole: 1 };
if (t < 4.3) return { name: "SLIDING + LOOP", state: "slide", hole: 2 };
if (t < 5.4) return { name: "ENTERING HOLE", state: "enter", hole: 2 };
if (t < 6.6) return { name: "LOCKED", state: "locked", hole: 2 };
if (t < 8.0) return { name: "LIFTING PIN", state: "lift", hole: 2 };
if (t < 9.8) return { name: "SLIDING + LOOP", state: "slide", hole: 1 };
if (t < 10.9) return { name: "ENTERING HOLE", state: "enter", hole: 1 };
return { name: "LOCKED", state: "locked", hole: 1 };
}
const statusElement = document.getElementById("status");
const stateElement = document.getElementById("state");
const progressElement = document.getElementById("progressFill");
const pinMetric = document.getElementById("pinMetric");
const strapMetric = document.getElementById("strapMetric");
const loopMetric = document.getElementById("loopMetric");
const annotation = document.getElementById("annotation");
const annotationText = document.getElementById("annotationText");
function updateMechanism(t) {
const lift = pinLiftAt(t);
const offset = strapOffsetAt(t);
const phase = phaseAt(t);
animatedPin.rotation.z = animatedPin.userData.baseAngle - 1.08 * lift;
animatedPin.position.z = 0.2 + 0.2 * lift;
// Strap movement is confined to the slide phases.
strapMotion.position.x = offset;
// Hard mechanism constraint:
// the loop's scale is mutable only when the pin is fully disengaged.
// It is intentionally not interpolated during lift, engage, or locked states.
const pinEngaged = lift < 0.015;
const isSlideWindow = (t >= 2.5 && t < 4.3) || (t >= 8.0 && t < 9.8);
if (isSlideWindow && !pinEngaged) {
const firstSlide = t < 4.3;
const localProgress = firstSlide
? ramp(t, 2.5, 4.3)
: ramp(t, 8.0, 9.8);
const desiredLoopScale = firstSlide
? THREE.MathUtils.lerp(1, MAX_LOOP_SCALE, localProgress)
: THREE.MathUtils.lerp(MAX_LOOP_SCALE, 1, localProgress);
currentLoopScale = desiredLoopScale;
tailLoop.scale.set(
currentLoopScale,
1 + (currentLoopScale - 1) * 0.52,
1
);
}
// Highlight the hole that the pin is approaching.
const activeHole = phase.hole - 1;
holeMarkers.forEach((marker, index) => {
const active = index === activeHole;
marker.material.opacity = active ? 0.95 : 0.14;
marker.material.emissiveIntensity = active ? 2.1 : 0.35;
marker.scale.setScalar(active ? 1.14 : 1);
});
statusElement.dataset.state = phase.state;
stateElement.textContent = phase.name;
progressElement.style.transform = `scaleX(${1 - t / CYCLE})`;
const pinDegrees = lift * 61.9;
pinMetric.textContent = `${pinDegrees.toFixed(1)}°`;
strapMetric.textContent = `${offset >= 0 ? "+" : ""}${offset.toFixed(2)}`;
loopMetric.textContent = `${Math.round(currentLoopScale * 100)}%`;
let annotationValue = "Engaged";
if (phase.state === "lift") annotationValue = "Pin clear";
if (phase.state === "slide") annotationValue = "Strap sliding";
if (phase.state === "enter") annotationValue = `Hole ${phase.hole}`;
annotationText.textContent = annotationValue;
}
// ------------------------------------------------------------------
// Custom camera controls — no addons or examples modules
// ------------------------------------------------------------------
const cameraTarget = new THREE.Vector3(0.15, 0.05, 0);
let targetTheta = 0.08;
let targetPhi = 1.47;
let targetRadius = 8.2;
let theta = targetTheta;
let phi = targetPhi;
let radius = targetRadius;
function updateCamera(delta) {
const damping = 1 - Math.exp(-delta * 9);
theta = THREE.MathUtils.lerp(theta, targetTheta, damping);
phi = THREE.MathUtils.lerp(phi, targetPhi, damping);
radius = THREE.MathUtils.lerp(radius, targetRadius, damping);
camera.position.set(
cameraTarget.x + radius * Math.sin(phi) * Math.sin(theta),
cameraTarget.y + radius * Math.cos(phi),
cameraTarget.z + radius * Math.sin(phi) * Math.cos(theta)
);
camera.lookAt(cameraTarget);
}
let dragging = false;
let previousX = 0;
let previousY = 0;
renderer.domElement.addEventListener("pointerdown", (event) => {
dragging = true;
previousX = event.clientX;
previousY = event.clientY;
renderer.domElement.setPointerCapture(event.pointerId);
});
renderer.domElement.addEventListener("pointermove", (event) => {
if (!dragging) return;
const dx = event.clientX - previousX;
const dy = event.clientY - previousY;
previousX = event.clientX;
previousY = event.clientY;
targetTheta -= dx * 0.006;
targetPhi = clamp(targetPhi - dy * 0.005, 0.78, 1.92);
});
function stopDragging(event) {
dragging = false;
if (event.pointerId !== undefined && renderer.domElement.hasPointerCapture(event.pointerId)) {
renderer.domElement.releasePointerCapture(event.pointerId);
}
}
renderer.domElement.addEventListener("pointerup", stopDragging);
renderer.domElement.addEventListener("pointercancel", stopDragging);
renderer.domElement.addEventListener("contextmenu", (event) => event.preventDefault());
renderer.domElement.addEventListener("wheel", (event) => {
event.preventDefault();
targetRadius = clamp(targetRadius + event.deltaY * 0.006, 5.2, 13.5);
}, { passive: false });
document.getElementById("cameraReset").addEventListener("click", () => {
targetTheta = 0.08;
targetPhi = 1.47;
targetRadius = 8.2;
});
const pauseButton = document.getElementById("pause");
function togglePause() {
playing = !playing;
pauseButton.textContent = playing ? "Pause" : "Resume";
}
pauseButton.addEventListener("click", togglePause);
document.getElementById("restart").addEventListener("click", () => {
elapsed = 0;
currentLoopScale = 1;
tailLoop.scale.set(1, 1, 1);
});
addEventListener("keydown", (event) => {
if (event.code === "Space") {
event.preventDefault();
togglePause();
}
if (event.key.toLowerCase() === "r") {
elapsed = 0;
currentLoopScale = 1;
tailLoop.scale.set(1, 1, 1);
}
});
// ------------------------------------------------------------------
// Project the animated annotation into screen space
// ------------------------------------------------------------------
const annotationWorld = new THREE.Vector3();
function updateAnnotation() {
annotationWorld.set(
animatedPin.userData.pivot.x - 0.28,
animatedPin.userData.pivot.y - 0.18,
animatedPin.position.z + 0.2
);
mainRig.localToWorld(annotationWorld);
annotationWorld.project(camera);
const x = (annotationWorld.x * 0.5 + 0.5) * innerWidth;
const y = (-annotationWorld.y * 0.5 + 0.5) * innerHeight;
annotation.style.left = `${x}px`;
annotation.style.top = `${y - 28}px`;
annotation.style.opacity = annotationWorld.z < 1 ? "1" : "0";
}
function resize() {
camera.aspect = innerWidth / innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(innerWidth, innerHeight);
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
}
addEventListener("resize", resize);
// ------------------------------------------------------------------
// Render loop
// ------------------------------------------------------------------
function animate() {
requestAnimationFrame(animate);
const delta = Math.min(clock.getDelta(), 0.05);
if (playing) {
elapsed = (elapsed + delta) % CYCLE;
}
const t = elapsed;
updateMechanism(t);
updateCamera(delta);
updateAnnotation();
renderer.render(scene, camera);
}
animate();
</script>
</body>
</html>
```