Prompt specification
Build or customize the Three.js 3D scene "Clockwork display with ratio stations" 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: Clockwork display with ratio stations
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/clockwork-display-with-ratio-stations-x1387
Scene Description Prompt:
Three voxel gear trains drive pointers at different synchronized ratios from one input. One HTML file using Three.js from cdnjs.
Working Three.js Starter (clockwork-display-with-ratio-stations-x1387):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Voxel Gear Ratio Rig</title>
<style>
:root {
color-scheme: dark;
--bg: #06101d;
--panel: rgba(9, 22, 38, 0.84);
--border: rgba(148, 183, 211, 0.2);
--text: #e8f3ff;
--muted: #91a9bf;
--accent: #fbbf24;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: var(--bg);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
color: var(--text);
}
#scene {
position: fixed;
inset: 0;
width: 100%;
height: 100%;
display: block;
touch-action: none;
cursor: grab;
}
#scene:active {
cursor: grabbing;
}
.hud {
position: fixed;
inset: 0;
pointer-events: none;
z-index: 2;
}
.panel {
background: var(--panel);
border: 1px solid var(--border);
box-shadow: 0 18px 50px rgba(0, 0, 0, 0.26);
backdrop-filter: blur(14px);
-webkit-backdrop-filter: blur(14px);
}
.title-panel {
position: absolute;
top: 22px;
left: 22px;
width: min(430px, calc(100vw - 44px));
padding: 17px 19px;
border-radius: 16px;
}
.eyebrow {
margin-bottom: 7px;
color: var(--accent);
font: 700 11px/1.2 ui-monospace, SFMono-Regular, Menlo, monospace;
letter-spacing: 0.16em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: clamp(21px, 2.4vw, 31px);
line-height: 1.08;
letter-spacing: -0.035em;
}
.subtitle {
margin-top: 8px;
color: var(--muted);
font-size: 13px;
line-height: 1.45;
}
.metrics {
position: absolute;
top: 22px;
right: 22px;
width: 260px;
padding: 14px;
border-radius: 16px;
}
.metric {
display: grid;
grid-template-columns: 34px 1fr auto;
align-items: center;
gap: 9px;
padding: 10px 8px;
border-top: 1px solid rgba(148, 183, 211, 0.12);
font: 600 12px/1.2 ui-monospace, SFMono-Regular, Menlo, monospace;
}
.metric:first-of-type {
border-top: 0;
}
.metric-id {
display: grid;
width: 27px;
height: 27px;
place-items: center;
border-radius: 8px;
color: #05101c;
font-weight: 900;
}
.metric-ratio {
color: var(--muted);
font-size: 10px;
letter-spacing: 0.04em;
}
.metric-angle {
color: var(--text);
font-variant-numeric: tabular-nums;
}
.controls {
position: absolute;
left: 50%;
bottom: 22px;
width: min(650px, calc(100vw - 32px));
padding: 15px 17px;
border-radius: 17px;
transform: translateX(-50%);
pointer-events: auto;
}
.control-row {
display: grid;
grid-template-columns: auto 1fr auto auto;
align-items: center;
gap: 12px;
}
.control-label {
color: var(--muted);
font: 700 10px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
letter-spacing: 0.12em;
text-transform: uppercase;
white-space: nowrap;
}
input[type="range"] {
width: 100%;
height: 5px;
margin: 0;
appearance: none;
border-radius: 999px;
outline: none;
background:
linear-gradient(90deg, #fbbf24 0%, #fbbf24 35%, #1a334a 35%, #1a334a 100%);
cursor: ew-resize;
}
input[type="range"]::-webkit-slider-thumb {
width: 18px;
height: 18px;
appearance: none;
border: 3px solid #fff4c7;
border-radius: 50%;
background: var(--accent);
box-shadow: 0 0 0 5px rgba(251, 191, 36, 0.13);
}
input[type="range"]::-moz-range-thumb {
width: 14px;
height: 14px;
border: 3px solid #fff4c7;
border-radius: 50%;
background: var(--accent);
box-shadow: 0 0 0 5px rgba(251, 191, 36, 0.13);
}
button {
min-width: 72px;
height: 36px;
padding: 0 13px;
border: 1px solid rgba(251, 191, 36, 0.32);
border-radius: 10px;
background: rgba(251, 191, 36, 0.1);
color: #ffe29a;
font: 800 11px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
letter-spacing: 0.06em;
cursor: pointer;
}
button:hover {
background: rgba(251, 191, 36, 0.19);
}
button.secondary {
min-width: 60px;
border-color: rgba(148, 183, 211, 0.2);
background: rgba(148, 183, 211, 0.06);
color: var(--muted);
}
.camera-help {
position: absolute;
right: 22px;
bottom: 24px;
color: rgba(176, 198, 218, 0.7);
font: 600 10px/1.5 ui-monospace, SFMono-Regular, Menlo, monospace;
letter-spacing: 0.04em;
text-align: right;
}
.legend {
position: absolute;
left: 22px;
bottom: 26px;
display: flex;
gap: 14px;
color: var(--muted);
font: 700 10px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
letter-spacing: 0.08em;
}
.legend span::before {
display: inline-block;
width: 7px;
height: 7px;
margin-right: 6px;
border-radius: 2px;
background: currentColor;
content: "";
}
@media (max-width: 760px) {
.metrics,
.camera-help,
.legend {
display: none;
}
.title-panel {
top: 14px;
left: 14px;
width: calc(100vw - 28px);
padding: 14px 15px;
}
.subtitle {
font-size: 11px;
}
.controls {
bottom: 14px;
padding: 13px;
}
.control-row {
grid-template-columns: auto 1fr auto;
gap: 8px;
}
.control-label {
display: none;
}
button {
min-width: 62px;
padding: 0 9px;
}
}
</style>
</head>
<body>
<canvas id="scene" aria-label="Interactive three-dimensional voxel gear train"></canvas>
<div class="hud">
<section class="panel title-panel">
<div class="eyebrow">Synchronized mechanical study</div>
<h1>One Drive. Three Ratios.</h1>
<div class="subtitle">
A single 16-tooth input drives three voxel gear trains.
Intermediate gears telescope into three precisely synchronized output ratios.
</div>
</section>
<section class="panel metrics" aria-live="polite">
<div class="metric">
<span class="metric-id" style="background:#fbbf24">IN</span>
<span class="metric-ratio">16T MASTER</span>
<span class="metric-angle" id="input-angle">0.0°</span>
</div>
<div class="metric">
<span class="metric-id" style="background:#38bdf8">A</span>
<span class="metric-ratio">4:3 · CCW</span>
<span class="metric-angle" id="angle-a">0.0°</span>
</div>
<div class="metric">
<span class="metric-id" style="background:#e879f9">B</span>
<span class="metric-ratio">4:5 · CW</span>
<span class="metric-angle" id="angle-b">0.0°</span>
</div>
<div class="metric">
<span class="metric-id" style="background:#4ade80">C</span>
<span class="metric-ratio">1:2 · CCW</span>
<span class="metric-angle" id="angle-c">0.0°</span>
</div>
</section>
<section class="panel controls">
<div class="control-row">
<label class="control-label" for="drive">Drive input</label>
<input id="drive" type="range" min="0" max="720" step="0.1" value="0"
aria-label="Master input rotation">
<button id="play" type="button">PAUSE</button>
<button id="reset" class="secondary" type="button">RESET</button>
</div>
</section>
<div class="legend">
<span style="color:#fbbf24">MASTER</span>
<span style="color:#38bdf8">TRAIN A</span>
<span style="color:#e879f9">TRAIN B</span>
<span style="color:#4ade80">TRAIN C</span>
</div>
<div class="camera-help">
DRAG TO ORBIT · SHIFT-DRAG TO PAN<br>
WHEEL TO ZOOM · SPACE TO PAUSE
</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 canvas = document.querySelector("#scene");
const driveSlider = document.querySelector("#drive");
const playButton = document.querySelector("#play");
const resetButton = document.querySelector("#reset");
const inputAngleElement = document.querySelector("#input-angle");
const outputAngleElements = [
document.querySelector("#angle-a"),
document.querySelector("#angle-b"),
document.querySelector("#angle-c")
];
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.outputColorSpace = THREE.SRGBColorSpace;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x06101d);
scene.fog = new THREE.Fog(0x06101d, 48, 92);
const camera = new THREE.PerspectiveCamera(
42,
window.innerWidth / window.innerHeight,
0.1,
180
);
const cameraTarget = new THREE.Vector3(1, -0.3, 0);
let cameraYaw = 0;
let cameraPitch = 0.075;
let cameraDistance = 34;
let cameraAdjusted = false;
scene.add(new THREE.HemisphereLight(0xa8ddff, 0x07101d, 1.55));
const keyLight = new THREE.DirectionalLight(0xffffff, 2.8);
keyLight.position.set(-7, 11, 19);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(1024, 1024);
keyLight.shadow.camera.left = -21;
keyLight.shadow.camera.right = 21;
keyLight.shadow.camera.top = 21;
keyLight.shadow.camera.bottom = -21;
keyLight.shadow.camera.near = 1;
keyLight.shadow.camera.far = 55;
keyLight.shadow.bias = -0.0004;
scene.add(keyLight);
const cyanLight = new THREE.PointLight(0x22d3ee, 26, 25, 2);
cyanLight.position.set(8, 8, 9);
scene.add(cyanLight);
const magentaLight = new THREE.PointLight(0xe879f9, 18, 23, 2);
magentaLight.position.set(-12, -6, 8);
scene.add(magentaLight);
const MODULE = 0.28;
const INPUT_TEETH = 16;
const unitBox = new THREE.BoxGeometry(1, 1, 1);
function standardMaterial(hex, emissive = 0.065, metalness = 0.48) {
const color = new THREE.Color(hex);
return new THREE.MeshStandardMaterial({
color,
roughness: 0.34,
metalness,
emissive: color.clone().multiplyScalar(emissive)
});
}
const masterMaterial = standardMaterial(0xf59e0b, 0.1);
const masterToothMaterial = standardMaterial(0xfcd34d, 0.13);
const pointerMaterial = standardMaterial(0xf8fafc, 0.18, 0.28);
const boltMaterial = standardMaterial(0x172536, 0.015, 0.72);
const tickMaterial = standardMaterial(0x7890a8, 0.035, 0.35);
const base = new THREE.Mesh(
new THREE.PlaneGeometry(46, 34),
new THREE.MeshStandardMaterial({
color: 0x081421,
roughness: 0.94,
metalness: 0.04
})
);
base.position.set(1, -0.3, -0.56);
base.receiveShadow = true;
scene.add(base);
const grid = new THREE.GridHelper(44, 44, 0x34516c, 0x183148);
grid.rotation.x = Math.PI / 2;
grid.position.set(1, -0.3, -0.49);
grid.material.transparent = true;
grid.material.opacity = 0.34;
scene.add(grid);
function makeBox(material, sx, sy, sz, x, y, z, parent) {
const mesh = new THREE.Mesh(unitBox, material);
mesh.scale.set(sx, sy, sz);
mesh.position.set(x, y, z);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
// A gear is assembled entirely from block-like pieces.
function makeVoxelGear(teeth, bodyMaterial, toothMaterial) {
const group = new THREE.Group();
const pitchRadius = teeth * MODULE * 0.5;
const rootRadius = pitchRadius - MODULE * 0.42;
// Segmented outer body.
const segmentCount = Math.max(12, Math.round(teeth * 0.9));
const ringRadius = rootRadius * 0.73;
const segmentLength =
Math.max(MODULE * 0.82, (Math.PI * 2 * ringRadius) / segmentCount * 0.9);
for (let i = 0; i < segmentCount; i++) {
const angle = (i / segmentCount) * Math.PI * 2;
const segment = makeBox(
bodyMaterial,
segmentLength,
MODULE * 0.76,
0.37,
Math.cos(angle) * ringRadius,
Math.sin(angle) * ringRadius,
0,
group
);
segment.rotation.z = angle + Math.PI * 0.5;
}
// Blocky spokes.
const spokeCount = teeth >= 24 ? 6 : 4;
const spokeLength = rootRadius * 0.78;
for (let i = 0; i < spokeCount; i++) {
const angle = (i / spokeCount) * Math.PI * 2;
const spoke = makeBox(
bodyMaterial,
MODULE * 0.34,
spokeLength,
0.34,
Math.cos(angle) * spokeLength * 0.5,
Math.sin(angle) * spokeLength * 0.5,
0.025,
group
);
spoke.rotation.z = angle - Math.PI * 0.5;
}
// Square hub and axle cap.
makeBox(
bodyMaterial,
MODULE * 1.28,
MODULE * 1.28,
0.62,
0,
0,
0.04,
group
);
makeBox(
boltMaterial,
MODULE * 0.48,
MODULE * 0.48,
0.12,
0,
0,
0.39,
group
);
// One square tooth per nominal gear tooth.
for (let i = 0; i < teeth; i++) {
const angle = (i / teeth) * Math.PI * 2;
const tooth = makeBox(
toothMaterial,
MODULE * 0.70,
MODULE * 0.70,
0.52,
Math.cos(angle) * (rootRadius + MODULE * 0.27),
Math.sin(angle) * (rootRadius + MODULE * 0.27),
0,
group
);
tooth.rotation.z = angle;
}
group.userData.pitchRadius = pitchRadius;
return group;
}
function addPointer(gear, pitchRadius) {
const pointer = new THREE.Group();
pointer.position.z = 0.51;
makeBox(
pointerMaterial,
0.105,
pitchRadius * 0.74,
0.105,
0,
pitchRadius * 0.34,
0,
pointer
);
makeBox(
pointerMaterial,
0.27,
0.27,
0.18,
0,
0,
0.02,
pointer
);
gear.add(pointer);
}
function makeGauge(position, radius, color) {
const gauge = new THREE.Group();
gauge.position.copy(position);
const points = [];
for (let i = 0; i < 96; i++) {
const angle = (i / 96) * Math.PI * 2;
points.push(
new THREE.Vector3(
Math.cos(angle) * (radius + 0.38),
Math.sin(angle) * (radius + 0.38),
0.36
)
);
}
const ring = new THREE.LineLoop(
new THREE.BufferGeometry().setFromPoints(points),
new THREE.LineBasicMaterial({
color,
transparent: true,
opacity: 0.42,
depthWrite: false
})
);
gauge.add(ring);
for (let i = 0; i < 32; i++) {
const angle = (i / 32) * Math.PI * 2;
const major = i % 4 === 0;
const tick = makeBox(
major ? pointerMaterial : tickMaterial,
0.045,
major ? 0.21 : 0.12,
0.045,
Math.cos(angle) * (radius + 0.30),
Math.sin(angle) * (radius + 0.30),
0.43,
gauge
);
tick.rotation.z = angle - Math.PI * 0.5;
tick.castShadow = false;
}
scene.add(gauge);
}
function makeLabel(text, color, worldWidth = 4.8) {
const labelCanvas = document.createElement("canvas");
labelCanvas.width = 768;
labelCanvas.height = 160;
const context = labelCanvas.getContext("2d");
context.clearRect(0, 0, 768, 160);
context.fillStyle = "rgba(5, 15, 27, 0.88)";
context.fillRect(8, 18, 752, 124);
context.strokeStyle = color;
context.lineWidth = 4;
context.strokeRect(8, 18, 752, 124);
context.fillStyle = color;
context.font = "700 43px ui-monospace, SFMono-Regular, Menlo, monospace";
context.textAlign = "center";
context.textBaseline = "middle";
context.fillText(text, 384, 82);
const texture = new THREE.CanvasTexture(labelCanvas);
texture.colorSpace = THREE.SRGBColorSpace;
texture.minFilter = THREE.LinearFilter;
const sprite = new THREE.Sprite(
new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthTest: false,
depthWrite: false
})
);
sprite.scale.set(worldWidth, worldWidth * 160 / 768, 1);
sprite.renderOrder = 20;
return sprite;
}
const inputGear = makeVoxelGear(
INPUT_TEETH,
masterMaterial,
masterToothMaterial
);
inputGear.position.set(0, 0, 0);
scene.add(inputGear);
// A visible crank confirms the single master input.
const crank = new THREE.Group();
crank.position.z = 0.56;
makeBox(
masterToothMaterial,
0.14,
1.38,
0.14,
0,
0.69,
0,
crank
);
makeBox(
pointerMaterial,
0.29,
0.29,
0.29,
0,
1.38,
0,
crank
);
inputGear.add(crank);
const inputLabel = makeLabel(
"MASTER INPUT · 16T",
"#fbbf24",
3.8
);
inputLabel.position.set(0, -3.25, 0.9);
scene.add(inputLabel);
const branchSpecs = [
{
id: "A",
counts: [12, 12],
direction: 90,
color: 0x38bdf8,
ratioText: "4:3"
},
{
id: "B",
counts: [12, 18, 20],
direction: 202.5,
color: 0xe879f9,
ratioText: "4:5"
},
{
id: "C",
counts: [12, 10, 16, 32],
direction: 337.5,
color: 0x4ade80,
ratioText: "1:2"
}
];
const branches = branchSpecs.map((spec) => {
const bodyMaterial = standardMaterial(spec.color, 0.085);
const toothMaterial = standardMaterial(
new THREE.Color(spec.color).offsetHSL(0, 0, 0.08),
0.1
);
const direction = THREE.MathUtils.degToRad(spec.direction);
const positions = [];
const gears = [];
const phases = [];
const angles = [];
const inputRadius = INPUT_TEETH * MODULE * 0.5;
const firstRadius = spec.counts[0] * MODULE * 0.5;
let radialDistance = inputRadius + firstRadius;
positions.push(
new THREE.Vector3(
Math.cos(direction) * radialDistance,
Math.sin(direction) * radialDistance,
0
)
);
for (let i = 1; i < spec.counts.length; i++) {
const previousRadius = spec.counts[i - 1] * MODULE * 0.5;
const currentRadius = spec.counts[i] * MODULE * 0.5;
radialDistance += previousRadius + currentRadius;
positions.push(
new THREE.Vector3(
Math.cos(direction) * radialDistance,
Math.sin(direction) * radialDistance,
0
)
);
}
// Phase offsets alternate teeth and gaps at every mesh.
for (let i = 0; i < spec.counts.length; i++) {
const contactPhase =
direction + Math.PI + Math.PI / spec.counts[i];
if (i === 0) {
phases.push(contactPhase);
} else {
phases.push(
contactPhase +
(spec.counts[i - 1] / spec.counts[i]) * phases[i - 1]
);
}
}
for (let i = 0; i < spec.counts.length; i++) {
const gear = makeVoxelGear(
spec.counts[i],
bodyMaterial,
toothMaterial
);
gear.position.copy(positions[i]);
scene.add(gear);
gears.push(gear);
angles.push(phases[i]);
}
const outputGear = gears[gears.length - 1];
const outputRadius = outputGear.userData.pitchRadius;
addPointer(outputGear, outputRadius);
makeGauge(positions[positions.length - 1], outputRadius, spec.color);
// Dashed path makes the shared power source easy to follow.
const routePoints = [
new THREE.Vector3(0, 0, -0.30),
...positions.map((point) =>
new THREE.Vector3(point.x, point.y, -0.30)
)
];
const route = new THREE.Line(
new THREE.BufferGeometry().setFromPoints(routePoints),
new THREE.LineDashedMaterial({
color: spec.color,
transparent: true,
opacity: 0.28,
dashSize: 0.22,
gapSize: 0.16,
depthWrite: false
})
);
route.computeLineDistances();
scene.add(route);
const directionText =
spec.counts.length % 2 === 0 ? "CCW" : "CW";
const label = makeLabel(
`TRAIN ${spec.id} · ${spec.ratioText} · ${directionText}`,
`#${spec.color.toString(16).padStart(6, "0")}`,
4.9
);
const labelPosition = positions[positions.length - 1].clone();
labelPosition.y += outputRadius + 0.95;
labelPosition.z = 1.05;
label.position.copy(labelPosition);
scene.add(label);
const signedRatio =
(spec.counts.length % 2 === 0 ? 1 : -1) *
INPUT_TEETH /
spec.counts[spec.counts.length - 1];
return {
...spec,
gears,
positions,
phases,
angles,
signedRatio
};
});
let inputAngle = 0;
let running = true;
let scrubbing = false;
let previousTime = performance.now();
function updateMechanism() {
inputGear.rotation.z = inputAngle;
for (const branch of branches) {
for (let i = 0; i < branch.gears.length; i++) {
if (i === 0) {
branch.angles[i] =
branch.phases[i] -
(INPUT_TEETH / branch.counts[i]) * inputAngle;
} else {
branch.angles[i] =
branch.phases[i] -
(branch.counts[i - 1] / branch.counts[i]) *
branch.angles[i - 1];
}
branch.gears[i].rotation.z = branch.angles[i];
}
}
}
function normalizeAngle(angle) {
return ((angle % 360) + 360) % 360;
}
function updateReadouts() {
inputAngleElement.textContent =
`${normalizeAngle(inputAngle).toFixed(1)}°`;
branches.forEach((branch, index) => {
const angle = normalizeAngle(branch.angles[branch.angles.length - 1]);
outputAngleElements[index].textContent = `${angle.toFixed(1)}°`;
});
if (!scrubbing) {
driveSlider.value = ((inputAngle % 720) + 720) % 720;
}
}
function setRunning(value) {
running = value;
playButton.textContent = running ? "PAUSE" : "RUN";
}
playButton.addEventListener("click", () => setRunning(!running));
resetButton.addEventListener("click", () => {
inputAngle = 0;
updateMechanism();
updateReadouts();
});
driveSlider.addEventListener("pointerdown", () => {
scrubbing = true;
});
window.addEventListener("pointerup", () => {
scrubbing = false;
});
driveSlider.addEventListener("input", () => {
inputAngle = Number(driveSlider.value);
updateMechanism();
updateReadouts();
});
window.addEventListener("keydown", (event) => {
if (
event.code === "Space" &&
event.target.tagName !== "INPUT" &&
event.target.tagName !== "BUTTON"
) {
event.preventDefault();
setRunning(!running);
}
});
// Direct camera controls: no external examples or addons are used.
function updateCamera() {
const cosinePitch = Math.cos(cameraPitch);
camera.position.set(
cameraTarget.x + Math.sin(cameraYaw) * cosinePitch * cameraDistance,
cameraTarget.y + Math.sin(cameraPitch) * cameraDistance,
cameraTarget.z + Math.cos(cameraYaw) * cosinePitch * cameraDistance
);
camera.lookAt(cameraTarget);
camera.updateMatrixWorld();
}
let dragging = false;
let panning = false;
let previousX = 0;
let previousY = 0;
canvas.addEventListener("contextmenu", (event) => {
event.preventDefault();
});
canvas.addEventListener("pointerdown", (event) => {
dragging = true;
panning = event.button === 2 || event.shiftKey;
previousX = event.clientX;
previousY = event.clientY;
canvas.setPointerCapture(event.pointerId);
});
canvas.addEventListener("pointermove", (event) => {
if (!dragging) return;
const deltaX = event.clientX - previousX;
const deltaY = event.clientY - previousY;
previousX = event.clientX;
previousY = event.clientY;
if (panning) {
const right = new THREE.Vector3().setFromMatrixColumn(
camera.matrixWorld,
0
);
const up = new THREE.Vector3().setFromMatrixColumn(
camera.matrixWorld,
1
);
const scale = cameraDistance / Math.max(canvas.clientHeight, 1);
cameraTarget.addScaledVector(right, -deltaX * scale);
cameraTarget.addScaledVector(up, deltaY * scale);
cameraAdjusted = true;
} else {
cameraYaw -= deltaX * 0.006;
cameraPitch = THREE.MathUtils.clamp(
cameraPitch + deltaY * 0.005,
-1.15,
1.15
);
cameraAdjusted = true;
}
updateCamera();
});
function endDrag(event) {
dragging = false;
panning = false;
if (canvas.hasPointerCapture(event.pointerId)) {
canvas.releasePointerCapture(event.pointerId);
}
}
canvas.addEventListener("pointerup", endDrag);
canvas.addEventListener("pointercancel", endDrag);
canvas.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraDistance = THREE.MathUtils.clamp(
cameraDistance * Math.exp(event.deltaY * 0.001),
14,
100
);
cameraAdjusted = true;
updateCamera();
},
{ passive: false }
);
function resize() {
const width = window.innerWidth;
const height = window.innerHeight;
const aspect = width / height;
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(width, height);
camera.aspect = aspect;
camera.updateProjectionMatrix();
if (!cameraAdjusted) {
cameraDistance = Math.max(34, 37.8 / aspect);
}
updateCamera();
}
window.addEventListener("resize", resize);
function animate(now) {
const delta = Math.min((now - previousTime) / 1000, 0.05);
previousTime = now;
if (running && !scrubbing) {
inputAngle += delta * 42;
}
updateMechanism();
updateReadouts();
renderer.render(scene, camera);
requestAnimationFrame(animate);
}
resize();
updateMechanism();
updateReadouts();
requestAnimationFrame(animate);
</script>
</body>
</html>
```