Prompt specification
Build or customize the Three.js 3D scene "Weight-driven clock release cycle" 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: Weight-driven clock release cycle
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/weight-driven-clock-release-cycle-x2502
Scene Description Prompt:
Make a voxel Three.js scene in a self-contained HTML file using cdnjs: Weight-driven clock release cycle. Create a compact historical clock mechanism with hanging weight, gear, and simplified escapement. Animate discrete release steps and keep the energy source visibly connected to the wheel.
Working Three.js Starter (weight-driven-clock-release-cycle-x2502):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Weight-Driven Voxel Escapement</title>
<style>
:root {
color-scheme: dark;
--paper: #eadbb9;
--muted: #b5a78b;
--gold: #d6ad55;
--line: rgba(226, 205, 158, 0.22);
--panel: rgba(22, 19, 15, 0.78);
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #181613;
font-family: Georgia, "Times New Roman", serif;
}
canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
cursor: grab;
outline: none;
}
canvas:active {
cursor: grabbing;
}
.title {
position: fixed;
top: 28px;
left: 30px;
z-index: 2;
max-width: 510px;
pointer-events: none;
text-shadow: 0 2px 18px rgba(0, 0, 0, 0.7);
}
.eyebrow {
margin-bottom: 8px;
color: var(--gold);
font: 700 11px/1.2 system-ui, sans-serif;
letter-spacing: 0.28em;
text-transform: uppercase;
}
h1 {
margin: 0;
color: var(--paper);
font-size: clamp(27px, 4vw, 52px);
font-weight: 400;
line-height: 0.95;
letter-spacing: -0.035em;
}
.subtitle {
max-width: 420px;
margin: 13px 0 0;
color: var(--muted);
font: 13px/1.55 system-ui, sans-serif;
}
.panel {
position: fixed;
top: 28px;
right: 28px;
z-index: 3;
width: 255px;
padding: 18px;
border: 1px solid var(--line);
border-radius: 4px;
background: var(--panel);
box-shadow: 0 18px 55px rgba(0, 0, 0, 0.25);
backdrop-filter: blur(12px);
pointer-events: none;
}
.panel-title {
margin-bottom: 14px;
color: var(--muted);
font: 700 10px/1 system-ui, sans-serif;
letter-spacing: 0.22em;
text-transform: uppercase;
}
.status-row {
display: flex;
align-items: center;
justify-content: space-between;
margin-bottom: 9px;
}
.status {
display: flex;
align-items: center;
gap: 8px;
color: var(--paper);
font: 700 12px/1 system-ui, sans-serif;
letter-spacing: 0.12em;
}
.status-dot {
width: 7px;
height: 7px;
border-radius: 50%;
background: #70866d;
box-shadow: 0 0 0 4px rgba(112, 134, 109, 0.12);
transition: background 100ms, box-shadow 100ms;
}
.status-dot.active {
background: var(--gold);
box-shadow: 0 0 0 4px rgba(214, 173, 85, 0.18), 0 0 16px var(--gold);
}
.release-count {
color: var(--gold);
font: 700 12px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
}
.meter {
height: 4px;
margin: 7px 0 17px;
overflow: hidden;
border-radius: 2px;
background: rgba(255, 255, 255, 0.08);
}
.meter > span {
display: block;
width: 100%;
height: 100%;
background: linear-gradient(90deg, #8d6427, #e0bc70);
transition: width 180ms ease-out;
}
.legend {
display: grid;
gap: 11px;
padding-top: 13px;
border-top: 1px solid var(--line);
}
.legend-item {
display: grid;
grid-template-columns: 11px 1fr;
gap: 10px;
align-items: start;
color: var(--muted);
font: 11px/1.35 system-ui, sans-serif;
}
.swatch {
width: 9px;
height: 9px;
margin-top: 2px;
border-radius: 1px;
background: #8d9296;
}
.swatch.gold {
background: var(--gold);
}
.swatch.rope {
background: #a6633d;
}
.panel-note {
margin: 14px 0 0;
padding-top: 13px;
border-top: 1px solid var(--line);
color: #8f866f;
font: 10px/1.5 system-ui, sans-serif;
}
#reset-view {
width: 100%;
margin-top: 14px;
padding: 9px 10px;
border: 1px solid var(--line);
border-radius: 3px;
color: var(--paper);
background: rgba(255, 255, 255, 0.035);
font: 700 10px/1 system-ui, sans-serif;
letter-spacing: 0.12em;
text-transform: uppercase;
cursor: pointer;
pointer-events: auto;
}
#reset-view:hover {
border-color: rgba(214, 173, 85, 0.65);
color: var(--gold);
}
.hint {
position: fixed;
bottom: 22px;
left: 50%;
z-index: 2;
padding: 8px 13px;
border: 1px solid var(--line);
border-radius: 999px;
color: #a99e87;
background: rgba(20, 18, 14, 0.55);
font: 10px/1 system-ui, sans-serif;
letter-spacing: 0.08em;
transform: translateX(-50%);
white-space: nowrap;
pointer-events: none;
}
@media (max-width: 760px) {
.title {
top: 18px;
left: 18px;
max-width: calc(100vw - 36px);
}
.subtitle {
display: none;
}
.panel {
top: auto;
right: 14px;
bottom: 48px;
width: 205px;
padding: 13px;
}
.legend,
.panel-note {
display: none;
}
.meter {
margin-bottom: 0;
}
.hint {
bottom: 13px;
}
}
</style>
</head>
<body>
<canvas
id="scene"
aria-label="Interactive voxel clock mechanism with a hanging weight, gears, and an escapement"
></canvas>
<header class="title">
<div class="eyebrow">A mechanical study · 01</div>
<h1>The Weight<br>Drives the Hour</h1>
<p class="subtitle">
Stored gravity descends one measured notch at a time, turning the power
wheel and releasing the escapement.
</p>
</header>
<aside class="panel">
<div class="panel-title">Release mechanism</div>
<div class="status-row">
<div class="status">
<i id="status-dot" class="status-dot"></i>
<span id="phase">HOLDING</span>
</div>
<span id="release-count" class="release-count">000</span>
</div>
<div class="meter"><span id="energy-fill"></span></div>
<div class="legend">
<div class="legend-item">
<i class="swatch"></i>
<span>Iron frame, pendulum, and escape-wheel pallets</span>
</div>
<div class="legend-item">
<i class="swatch gold"></i>
<span>Brass power wheel, pinion, and winding drum</span>
</div>
<div class="legend-item">
<i class="swatch rope"></i>
<span>Continuous tension path from weight to drum</span>
</div>
</div>
<p class="panel-note">
The light pulses show stored force traveling through the cord during
each discrete release.
</p>
<button id="reset-view" type="button">Reset view</button>
</aside>
<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, camera, and scene
// ------------------------------------------------------------
const canvas = document.querySelector("#scene");
const phaseElement = document.querySelector("#phase");
const statusDot = document.querySelector("#status-dot");
const releaseCountElement = document.querySelector("#release-count");
const energyFill = document.querySelector("#energy-fill");
const renderer = new THREE.WebGLRenderer({
canvas,
antialias: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.15;
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x181613);
scene.fog = new THREE.Fog(0x181613, 12, 25);
const camera = new THREE.PerspectiveCamera(
42,
window.innerWidth / window.innerHeight,
0.1,
60
);
const cameraTarget = new THREE.Vector3(-0.15, 3.2, 0);
const view = {
azimuth: 0.48,
polar: 1.18,
radius: 12.6
};
const currentView = { ...view };
// ------------------------------------------------------------
// Materials
// ------------------------------------------------------------
const material = {
wall: new THREE.MeshStandardMaterial({
color: 0x25221d, roughness: 0.95, metalness: 0
}),
floor: new THREE.MeshStandardMaterial({
color: 0x302a22, roughness: 0.88, metalness: 0
}),
wood: new THREE.MeshStandardMaterial({
color: 0x5a3824, roughness: 0.75, metalness: 0.02
}),
woodLight: new THREE.MeshStandardMaterial({
color: 0x795038, roughness: 0.72, metalness: 0.02
}),
woodDark: new THREE.MeshStandardMaterial({
color: 0x352117, roughness: 0.82, metalness: 0.01
}),
brass: new THREE.MeshStandardMaterial({
color: 0xc59a43, roughness: 0.3, metalness: 0.76
}),
brassLight: new THREE.MeshStandardMaterial({
color: 0xe0bb69, roughness: 0.24, metalness: 0.72
}),
brassDark: new THREE.MeshStandardMaterial({
color: 0x795723, roughness: 0.42, metalness: 0.68
}),
iron: new THREE.MeshStandardMaterial({
color: 0x55585a, roughness: 0.48, metalness: 0.72
}),
ironDark: new THREE.MeshStandardMaterial({
color: 0x292c2d, roughness: 0.58, metalness: 0.62
}),
lead: new THREE.MeshStandardMaterial({
color: 0x737577, roughness: 0.58, metalness: 0.48
}),
rope: new THREE.MeshStandardMaterial({
color: 0x9a5938, roughness: 0.92, metalness: 0
}),
ropeLight: new THREE.MeshStandardMaterial({
color: 0xb87448, roughness: 0.88, metalness: 0
}),
red: new THREE.MeshStandardMaterial({
color: 0x743c30, roughness: 0.72, metalness: 0.05
}),
pulse: new THREE.MeshStandardMaterial({
color: 0xffd878,
emissive: 0xffa928,
emissiveIntensity: 2.5,
roughness: 0.25,
metalness: 0.2
})
};
function addBox(parent, size, position, mat, castShadow = true) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(size[0], size[1], size[2]),
mat
);
mesh.position.set(position[0], position[1], position[2]);
mesh.castShadow = castShadow;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function addBeam(parent, a, b, width, depth, mat) {
const start = new THREE.Vector3(...a);
const end = new THREE.Vector3(...b);
const direction = end.clone().sub(start);
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(width, direction.length(), depth),
mat
);
mesh.position.copy(start).add(end).multiplyScalar(0.5);
mesh.quaternion.setFromUnitVectors(
new THREE.Vector3(0, 1, 0),
direction.clone().normalize()
);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
// ------------------------------------------------------------
// Voxel environment and wooden clock frame
// ------------------------------------------------------------
addBox(scene, [16, 10, 0.2], [0, 3.5, -2.25], material.wall, false);
addBox(scene, [16, 0.25, 9], [0, -0.18, -0.2], material.floor, false);
const frame = new THREE.Group();
scene.add(frame);
// Main posts and rails.
addBox(frame, [8.05, 0.30, 0.58], [-0.35, 0.38, -0.58], material.wood);
addBox(frame, [8.05, 0.30, 0.58], [-0.35, 6.16, -0.58], material.wood);
addBox(frame, [0.31, 5.85, 0.58], [-4.18, 3.25, -0.58], material.wood);
addBox(frame, [0.31, 5.85, 0.58], [3.48, 3.25, -0.58], material.wood);
// Inner wood strips create a hand-built, layered appearance.
addBox(frame, [7.45, 0.10, 0.12], [-0.35, 0.60, -0.23], material.woodLight);
addBox(frame, [7.45, 0.10, 0.12], [-0.35, 5.96, -0.23], material.woodLight);
addBox(frame, [0.10, 5.25, 0.12], [-3.99, 3.25, -0.23], material.woodLight);
addBox(frame, [0.10, 5.25, 0.12], [3.29, 3.25, -0.23], material.woodLight);
// Corner braces.
addBeam(
frame,
[-3.95, 0.62, -0.18],
[-2.55, 2.06, -0.18],
0.16,
0.18,
material.woodDark
);
addBeam(
frame,
[3.25, 0.62, -0.18],
[1.82, 2.06, -0.18],
0.16,
0.18,
material.woodDark
);
// Feet and brass frame caps.
for (const x of [-4.18, 3.48]) {
addBox(frame, [0.78, 0.25, 0.95], [x, 0.12, -0.48], material.woodDark);
addBox(frame, [0.38, 0.18, 0.68], [x, 6.38, -0.48], material.brassDark);
}
// Rear mounting blocks for both wheel axles.
addBox(frame, [0.82, 1.08, 0.42], [-0.10, 3.55, -0.52], material.woodDark);
addBox(frame, [0.70, 0.94, 0.40], [1.78, 3.55, -0.52], material.woodDark);
// Weight guide rails.
addBox(frame, [0.10, 3.75, 0.15], [-3.62, 2.65, -0.05], material.brassDark);
addBox(frame, [0.10, 3.75, 0.15], [-2.48, 2.65, -0.05], material.brassDark);
// ------------------------------------------------------------
// Voxel gear factory
// ------------------------------------------------------------
function createVoxelGear(radius, teeth, depth, mat) {
const group = new THREE.Group();
const pitch = (Math.PI * 2 * radius) / teeth;
for (let i = 0; i < teeth; i++) {
const angle = (i / teeth) * Math.PI * 2;
const rim = addBox(
group,
[0.34, pitch * 0.67, depth * 0.78],
[
Math.cos(angle) * (radius - 0.18),
Math.sin(angle) * (radius - 0.18),
0
],
mat
);
rim.rotation.z = angle;
const tooth = addBox(
group,
[0.34, pitch * 0.43, depth],
[
Math.cos(angle) * radius,
Math.sin(angle) * radius,
0
],
material.brassLight
);
tooth.rotation.z = angle;
}
const spokeCount = 6;
for (let i = 0; i < spokeCount; i++) {
const angle = (i / spokeCount) * Math.PI * 2;
const spoke = addBox(
group,
[radius * 1.03, 0.105, depth * 0.55],
[
Math.cos(angle) * radius * 0.43,
Math.sin(angle) * radius * 0.43,
0
],
mat
);
spoke.rotation.z = angle;
}
addBox(group, [0.43, 0.43, depth * 1.25], [0, 0, 0], material.brassDark);
addBox(group, [0.18, 0.18, depth * 1.6], [0, 0, 0.02], material.ironDark);
return group;
}
// ------------------------------------------------------------
// Main power wheel and winding drum
// ------------------------------------------------------------
const mainWheel = new THREE.Group();
mainWheel.position.set(-0.10, 3.55, 0);
scene.add(mainWheel);
const mainGear = createVoxelGear(1.16, 20, 0.30, material.brass);
mainWheel.add(mainGear);
// Square, visibly rotating winding drum behind the power wheel.
addBox(mainWheel, [0.82, 0.82, 0.25], [0, 0, -0.34], material.brassDark);
addBox(mainWheel, [0.56, 0.56, 0.57], [0, 0, -0.17], material.ironDark);
addBox(mainWheel, [0.92, 0.13, 0.31], [0, 0, -0.31], material.brass);
// A bright square lug marks the exact rope attachment.
const ropeMarker = addBox(
mainWheel,
[0.17, 0.17, 0.17],
[
Math.cos(1.08) * 0.47,
Math.sin(1.08) * 0.47,
0.68
],
material.brassLight
);
// ------------------------------------------------------------
// Escape wheel and compound pinion
// ------------------------------------------------------------
const secondaryWheel = new THREE.Group();
secondaryWheel.position.set(1.78, 3.55, 0.06);
scene.add(secondaryWheel);
const escapeWheel = createVoxelGear(0.72, 14, 0.23, material.brass);
secondaryWheel.add(escapeWheel);
const compoundPinion = createVoxelGear(0.34, 10, 0.31, material.iron);
compoundPinion.position.z = 0.24;
secondaryWheel.add(compoundPinion);
addBox(scene, [0.22, 0.22, 0.88], [-0.10, 3.55, 0.02], material.ironDark);
addBox(scene, [0.20, 0.20, 0.76], [1.78, 3.55, 0.18], material.ironDark);
// ------------------------------------------------------------
// Simplified anchor escapement and pendulum
// ------------------------------------------------------------
const anchor = new THREE.Group();
anchor.position.set(1.78, 4.70, 0.37);
scene.add(anchor);
addBox(anchor, [1.22, 0.14, 0.18], [0, 0, 0], material.iron);
addBox(anchor, [0.14, 0.42, 0.18], [-0.46, -0.17, 0], material.iron);
addBox(anchor, [0.14, 0.42, 0.18], [0.46, -0.17, 0], material.iron);
addBox(anchor, [0.28, 0.16, 0.25], [-0.46, -0.36, 0.03], material.brassLight);
addBox(anchor, [0.28, 0.16, 0.25], [0.46, -0.36, 0.03], material.brassLight);
addBox(anchor, [0.24, 0.24, 0.20], [0, 0, 0], material.brassDark);
const pendulum = new THREE.Group();
pendulum.position.set(1.78, 4.68, -0.58);
scene.add(pendulum);
addBox(pendulum, [0.105, 3.35, 0.105], [0, -1.675, 0], material.iron);
addBox(pendulum, [0.72, 0.72, 0.30], [0, -3.30, 0], material.brassDark);
addBox(pendulum, [0.42, 0.42, 0.34], [0, -3.30, 0], material.brass);
addBox(pendulum, [0.19, 0.19, 0.20], [0, 0, 0], material.ironDark);
// ------------------------------------------------------------
// Ratchet pawl and fixed stop
// ------------------------------------------------------------
const pawl = new THREE.Group();
pawl.position.set(-1.25, 5.04, 0.39);
scene.add(pawl);
addBox(pawl, [1.0, 0.13, 0.17], [0.46, 0, 0], material.iron);
addBox(pawl, [0.19, 0.25, 0.23], [0.92, -0.06, 0.02], material.brassLight);
addBox(pawl, [0.23, 0.23, 0.23], [0, 0, 0], material.brassDark);
addBox(scene, [0.18, 0.34, 0.24], [-0.28, 5.04, 0.38], material.ironDark);
// ------------------------------------------------------------
// Hanging weight
// ------------------------------------------------------------
const weight = new THREE.Group();
weight.position.set(-3.05, 4.40, 0.58);
scene.add(weight);
addBox(weight, [0.62, 0.18, 0.52], [0, -0.17, 0], material.lead);
addBox(weight, [0.78, 1.02, 0.58], [0, -0.73, 0], material.lead);
addBox(weight, [0.87, 0.10, 0.65], [0, -0.40, 0], material.brassDark);
addBox(weight, [0.87, 0.10, 0.65], [0, -0.74, 0], material.brassDark);
addBox(weight, [0.87, 0.10, 0.65], [0, -1.08, 0], material.brassDark);
addBox(weight, [0.16, 0.28, 0.16], [0, 0.03, 0], material.iron);
addBox(weight, [0.31, 0.11, 0.16], [0, 0.10, 0], material.iron);
// ------------------------------------------------------------
// Pulley and its mounting bracket
// ------------------------------------------------------------
const pulley = new THREE.Group();
pulley.position.set(-3.05, 5.48, 0.39);
scene.add(pulley);
addBox(pulley, [0.86, 0.13, 0.18], [0, 0, 0], material.brass);
addBox(pulley, [0.13, 0.86, 0.18], [0, 0, 0], material.brass);
addBox(pulley, [0.59, 0.12, 0.19], [0, 0, 0], material.brassDark);
addBox(pulley, [0.12, 0.59, 0.19], [0, 0, 0], material.brassDark);
addBox(pulley, [0.28, 0.28, 0.29], [0, 0, 0], material.ironDark);
addBox(frame, [0.16, 0.85, 0.18], [-3.50, 5.57, 0.22], material.ironDark);
addBox(frame, [0.16, 0.85, 0.18], [-2.60, 5.57, 0.22], material.ironDark);
addBox(frame, [1.10, 0.16, 0.20], [-3.05, 5.98, 0.22], material.ironDark);
// ------------------------------------------------------------
// Voxel rope and traveling energy pulses
// ------------------------------------------------------------
const ropeGeometry = new THREE.BoxGeometry(0.058, 1, 0.058);
const ropeLinks = [];
for (let i = 0; i < 58; i++) {
const mesh = new THREE.Mesh(
ropeGeometry,
i % 5 === 0 ? material.ropeLight : material.rope
);
mesh.castShadow = true;
mesh.receiveShadow = true;
scene.add(mesh);
ropeLinks.push(mesh);
}
const energyPulses = [];
for (let i = 0; i < 4; i++) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(0.12, 0.12, 0.12),
material.pulse
);
mesh.visible = false;
scene.add(mesh);
energyPulses.push(mesh);
}
let routePoints = [];
let routeLengths = [];
let routeTotalLength = 1;
function updateRope(route) {
routePoints = route;
routeLengths.length = 0;
routeTotalLength = 0;
for (let i = 0; i < route.length - 1; i++) {
const length = routePoints[i].distanceTo(routePoints[i + 1]);
routeLengths.push(length);
routeTotalLength += length;
}
const pieceLength = routeTotalLength / ropeLinks.length;
const position = new THREE.Vector3();
const direction = new THREE.Vector3();
const up = new THREE.Vector3(0, 1, 0);
ropeLinks.forEach((mesh, i) => {
sampleRoute((i + 0.5) / ropeLinks.length, position, direction);
mesh.position.copy(position);
mesh.quaternion.setFromUnitVectors(up, direction);
mesh.scale.set(1, pieceLength * 1.13, 1);
});
}
function sampleRoute(t, outPosition, outDirection) {
let remaining = THREE.MathUtils.clamp(t, 0, 1) * routeTotalLength;
for (let i = 0; i < routeLengths.length; i++) {
const length = routeLengths[i];
if (remaining <= length || i === routeLengths.length - 1) {
const amount = length > 0 ? remaining / length : 0;
outPosition.lerpVectors(routePoints[i], routePoints[i + 1], amount);
outDirection
.subVectors(routePoints[i + 1], routePoints[i])
.normalize();
return;
}
remaining -= length;
}
}
// ------------------------------------------------------------
// Lighting
// ------------------------------------------------------------
scene.add(new THREE.HemisphereLight(0xf5dfb7, 0x241c16, 1.65));
const keyLight = new THREE.DirectionalLight(0xffe4b5, 3.2);
keyLight.position.set(4.5, 9.0, 7.5);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -7;
keyLight.shadow.camera.right = 7;
keyLight.shadow.camera.top = 8;
keyLight.shadow.camera.bottom = -2;
keyLight.shadow.camera.near = 0.5;
keyLight.shadow.camera.far = 24;
keyLight.shadow.bias = -0.00025;
scene.add(keyLight);
const warmLight = new THREE.PointLight(0xffbd64, 34, 11, 2);
warmLight.position.set(-3.5, 4.0, 4.5);
scene.add(warmLight);
const rimLight = new THREE.PointLight(0x9bb6d2, 18, 12, 2);
rimLight.position.set(5.0, 5.5, -1.0);
scene.add(rimLight);
// ------------------------------------------------------------
// Direct camera controls — no external control addons
// ------------------------------------------------------------
let dragging = false;
let previousX = 0;
let previousY = 0;
canvas.addEventListener("pointerdown", (event) => {
dragging = true;
previousX = event.clientX;
previousY = event.clientY;
canvas.setPointerCapture(event.pointerId);
});
canvas.addEventListener("pointermove", (event) => {
if (!dragging) return;
const dx = event.clientX - previousX;
const dy = event.clientY - previousY;
previousX = event.clientX;
previousY = event.clientY;
view.azimuth -= dx * 0.006;
view.polar = THREE.MathUtils.clamp(
view.polar - dy * 0.005,
0.55,
1.47
);
});
function endDrag(event) {
dragging = false;
if (event.pointerId !== undefined && canvas.hasPointerCapture(event.pointerId)) {
canvas.releasePointerCapture(event.pointerId);
}
}
canvas.addEventListener("pointerup", endDrag);
canvas.addEventListener("pointercancel", endDrag);
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
view.radius = THREE.MathUtils.clamp(
view.radius + event.deltaY * 0.008,
8.2,
18
);
},
{ passive: false }
);
document.querySelector("#reset-view").addEventListener("click", () => {
view.azimuth = 0.48;
view.polar = 1.18;
view.radius = 12.6;
});
// ------------------------------------------------------------
// Discrete release-cycle state
// ------------------------------------------------------------
const TWO_PI = Math.PI * 2;
const MAIN_TEETH = 20;
const HALF_OSCILLATION = 1.2;
const initialEyeHeight = 4.40;
const minimumEyeHeight = 2.72;
let elapsed = 0;
let nextReleaseTime = 0.6;
let lastReleaseTime = -10;
let releaseCount = 0;
let currentMainAngle = 0;
let targetMainAngle = 0;
let currentEscapeAngle = 0;
let targetEscapeAngle = 0;
let currentEyeHeight = initialEyeHeight;
let targetEyeHeight = initialEyeHeight;
let releaseMotion = null;
function triggerRelease(time) {
targetMainAngle -= TWO_PI / MAIN_TEETH;
targetEscapeAngle += TWO_PI / 14;
targetEyeHeight = Math.max(
minimumEyeHeight,
targetEyeHeight - 0.045
);
releaseMotion = {
start: time,
fromMain: currentMainAngle,
toMain: targetMainAngle,
fromEscape: currentEscapeAngle,
toEscape: targetEscapeAngle,
fromWeight: currentEyeHeight,
toWeight: targetEyeHeight
};
lastReleaseTime = time;
releaseCount++;
}
const pulsePosition = new THREE.Vector3();
const pulseDirection = new THREE.Vector3();
function updateEnergyPulses() {
const age = elapsed - lastReleaseTime;
energyPulses.forEach((pulse, index) => {
const progress = age * 1.45 - index * 0.17;
pulse.visible = progress >= 0 && progress <= 1;
if (!pulse.visible) return;
sampleRoute(progress, pulsePosition, pulseDirection);
pulse.position.copy(pulsePosition);
pulse.rotation.x += 0.04;
pulse.rotation.y += 0.07;
const fade = Math.sin(progress * Math.PI);
pulse.scale.setScalar(0.65 + fade * 0.55);
});
}
// ------------------------------------------------------------
// Animation
// ------------------------------------------------------------
const clock = new THREE.Clock();
function animate() {
const dt = Math.min(clock.getDelta(), 0.05);
elapsed += dt;
while (elapsed >= nextReleaseTime) {
triggerRelease(nextReleaseTime);
nextReleaseTime += HALF_OSCILLATION;
}
if (releaseMotion) {
const progress = THREE.MathUtils.clamp(
(elapsed - releaseMotion.start) / 0.23,
0,
1
);
const eased = 1 - Math.pow(1 - progress, 3);
currentMainAngle = THREE.MathUtils.lerp(
releaseMotion.fromMain,
releaseMotion.toMain,
eased
);
currentEscapeAngle = THREE.MathUtils.lerp(
releaseMotion.fromEscape,
releaseMotion.toEscape,
eased
);
currentEyeHeight = THREE.MathUtils.lerp(
releaseMotion.fromWeight,
releaseMotion.toWeight,
eased
);
}
mainWheel.rotation.z = currentMainAngle;
secondaryWheel.rotation.z = currentEscapeAngle;
pulley.rotation.z = -currentMainAngle * 0.48;
weight.position.y = currentEyeHeight;
// The escapement pallet rocks with the pendulum. Its center
// crossings occur at the same moments as the gear releases.
const escapementAngle =
Math.cos((Math.PI * elapsed) / HALF_OSCILLATION) * 0.13;
anchor.rotation.z = escapementAngle;
pendulum.rotation.z = escapementAngle * 1.35;
const releaseAge = elapsed - lastReleaseTime;
const pawlKick =
releaseAge >= 0 && releaseAge < 0.48
? Math.sin((Math.PI * releaseAge) / 0.48)
: 0;
pawl.rotation.z = -0.38 * pawlKick;
mainWheel.updateMatrixWorld(true);
const markerPosition = new THREE.Vector3();
ropeMarker.getWorldPosition(markerPosition);
// A single continuous rope route:
// weight -> pulley underside -> around pulley -> winding drum.
const ropeRoute = [
new THREE.Vector3(-3.05, currentEyeHeight + 0.10, 0.68),
new THREE.Vector3(-3.05, 5.16, 0.68),
new THREE.Vector3(-3.28, 5.27, 0.68),
new THREE.Vector3(-3.37, 5.48, 0.68),
new THREE.Vector3(-3.28, 5.69, 0.68),
new THREE.Vector3(-3.05, 5.80, 0.68),
markerPosition
];
updateRope(ropeRoute);
updateEnergyPulses();
// Camera damping.
const damping = 1 - Math.exp(-dt * 8);
currentView.azimuth = THREE.MathUtils.lerp(
currentView.azimuth,
view.azimuth,
damping
);
currentView.polar = THREE.MathUtils.lerp(
currentView.polar,
view.polar,
damping
);
currentView.radius = THREE.MathUtils.lerp(
currentView.radius,
view.radius,
damping
);
const sinPolar = Math.sin(currentView.polar);
camera.position.set(
cameraTarget.x + currentView.radius * sinPolar * Math.sin(currentView.azimuth),
cameraTarget.y + currentView.radius * Math.cos(currentView.polar),
cameraTarget.z + currentView.radius * sinPolar * Math.cos(currentView.azimuth)
);
camera.lookAt(cameraTarget);
// Readouts.
const activelyReleasing = releaseAge >= 0 && releaseAge < 0.25;
phaseElement.textContent = activelyReleasing ? "RELEASE" : "HOLDING";
statusDot.classList.toggle("active", activelyReleasing);
releaseCountElement.textContent = String(releaseCount).padStart(3, "0");
const energy = THREE.MathUtils.clamp(
(currentEyeHeight - minimumEyeHeight) /
(initialEyeHeight - minimumEyeHeight),
0,
1
);
energyFill.style.width = `${(energy * 100).toFixed(1)}%`;
renderer.render(scene, camera);
}
function resize() {
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
camera.aspect = window.innerWidth / window.innerHeight;
camera.fov = window.innerWidth < 700 ? 50 : 42;
camera.updateProjectionMatrix();
}
window.addEventListener("resize", resize);
resize();
renderer.setAnimationLoop(animate);
</script>
</body>
</html>
```