Prompt specification
Build or customize the Three.js 3D scene "One-way mechanism comparison table" 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: One-way mechanism comparison table
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/one-way-mechanism-comparison-table-x0296
Scene Description Prompt:
Make a standalone HTML scene with Three.js from cdnjs that depicts a ratchet and pawl. Build three toy machines using ratchets for lifting, counting, and winding; one control advances each system and exposes the different downstream effects of the same engagement principle. Voxel style.
Working Three.js Starter (one-way-mechanism-comparison-table-x0296):
```html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Ratchet Lab — One Control, Three Machines</title>
<style>
:root {
--ink: #243447;
--paper: #f4ead2;
--paper-light: #fff9e9;
--orange: #f06b3b;
--teal: #3daea9;
--yellow: #f2c14e;
--muted: #65717b;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background: #e9e1cf;
color: var(--ink);
font-family: "Courier New", Courier, monospace;
}
button {
font: inherit;
}
#viewport {
position: fixed;
inset: 0;
}
#viewport canvas {
display: block;
width: 100%;
height: 100%;
touch-action: none;
cursor: grab;
}
#viewport canvas:active {
cursor: grabbing;
}
#labels {
position: fixed;
inset: 0;
overflow: hidden;
pointer-events: none;
}
.machine-label {
position: absolute;
left: 0;
top: 0;
min-width: 142px;
padding: 9px 12px 8px;
border: 2px solid var(--ink);
border-left: 7px solid var(--accent, var(--orange));
background: rgba(255, 249, 233, 0.92);
box-shadow: 5px 5px 0 rgba(36, 52, 71, 0.16);
transform: translate(-50%, -100%);
white-space: nowrap;
text-align: left;
}
.machine-label b {
display: block;
font-size: 13px;
letter-spacing: 0.08em;
}
.machine-label span {
display: block;
margin-top: 3px;
color: var(--muted);
font-size: 10px;
letter-spacing: 0.04em;
}
.panel {
position: fixed;
z-index: 5;
border: 2px solid var(--ink);
background: rgba(255, 249, 233, 0.94);
box-shadow: 6px 6px 0 rgba(36, 52, 71, 0.14);
}
#intro {
top: 18px;
left: 18px;
width: 330px;
padding: 15px 17px 14px;
pointer-events: none;
}
.eyebrow {
margin-bottom: 6px;
color: var(--orange);
font-size: 10px;
font-weight: 700;
letter-spacing: 0.18em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: clamp(22px, 2.5vw, 34px);
line-height: 0.96;
letter-spacing: -0.07em;
text-transform: uppercase;
}
#intro p {
margin: 11px 0 0;
color: #4f5d68;
font-family: system-ui, sans-serif;
font-size: 13px;
line-height: 1.45;
}
#intro strong {
color: var(--ink);
}
#telemetry {
top: 18px;
right: 18px;
width: 226px;
padding: 12px 14px;
}
.telemetry-title {
display: flex;
justify-content: space-between;
margin-bottom: 9px;
color: var(--muted);
font-size: 9px;
letter-spacing: 0.14em;
text-transform: uppercase;
}
.readout {
display: grid;
grid-template-columns: 10px 1fr auto;
gap: 8px;
align-items: center;
padding: 7px 0;
border-top: 1px solid rgba(36, 52, 71, 0.16);
font-size: 11px;
}
.readout i {
width: 9px;
height: 9px;
background: var(--dot);
}
.readout output {
font-weight: 700;
}
#dock {
bottom: 18px;
left: 50%;
display: flex;
align-items: center;
gap: 13px;
padding: 10px 12px;
transform: translateX(-50%);
}
#advance {
min-width: 190px;
padding: 13px 17px;
border: 2px solid var(--ink);
background: var(--orange);
color: #182432;
font-weight: 700;
letter-spacing: 0.08em;
text-transform: uppercase;
box-shadow: 4px 4px 0 var(--ink);
cursor: pointer;
transition:
transform 100ms ease,
box-shadow 100ms ease,
background 100ms ease;
}
#advance:hover {
background: #ff8151;
}
#advance:active,
#advance.busy {
transform: translate(3px, 3px);
box-shadow: 1px 1px 0 var(--ink);
}
#advance:focus-visible,
#reset-view:focus-visible {
outline: 3px solid var(--yellow);
outline-offset: 3px;
}
#status {
width: min(330px, 42vw);
color: var(--muted);
font-family: system-ui, sans-serif;
font-size: 12px;
line-height: 1.35;
}
#reset-view {
position: fixed;
right: 18px;
bottom: 18px;
z-index: 6;
padding: 9px 12px;
border: 2px solid var(--ink);
background: rgba(255, 249, 233, 0.94);
color: var(--ink);
box-shadow: 4px 4px 0 rgba(36, 52, 71, 0.14);
cursor: pointer;
font-size: 10px;
font-weight: 700;
letter-spacing: 0.08em;
text-transform: uppercase;
}
#hint {
position: fixed;
left: 18px;
bottom: 18px;
z-index: 4;
color: rgba(36, 52, 71, 0.65);
font-family: system-ui, sans-serif;
font-size: 11px;
pointer-events: none;
}
.key {
display: inline-block;
padding: 1px 5px;
border: 1px solid rgba(36, 52, 71, 0.4);
background: rgba(255, 249, 233, 0.8);
font-family: "Courier New", monospace;
font-size: 10px;
}
@media (max-width: 760px) {
#intro {
top: 10px;
left: 10px;
width: calc(100% - 20px);
padding: 10px 12px;
}
#intro p {
display: none;
}
#telemetry {
top: 92px;
right: 10px;
width: 174px;
padding: 8px 10px;
}
.machine-label {
min-width: 112px;
padding: 6px 8px;
}
.machine-label b {
font-size: 10px;
}
.machine-label span {
font-size: 8px;
}
#dock {
bottom: 10px;
width: calc(100% - 20px);
gap: 8px;
padding: 8px;
}
#advance {
min-width: 142px;
padding: 12px 10px;
font-size: 11px;
}
#status {
width: auto;
flex: 1;
font-size: 10px;
}
#reset-view {
right: 10px;
bottom: 82px;
}
#hint {
display: none;
}
}
</style>
</head>
<body>
<div id="viewport"></div>
<div id="labels">
<div class="machine-label" id="label-lift" style="--accent: var(--teal)">
<b>01 · LIFT</b>
<span>one-way climb</span>
</div>
<div class="machine-label" id="label-counter" style="--accent: var(--yellow)">
<b>02 · COUNT</b>
<span>one-way tally</span>
</div>
<div class="machine-label" id="label-wind" style="--accent: var(--orange)">
<b>03 · WIND</b>
<span>one-way spring</span>
</div>
</div>
<section class="panel" id="intro">
<div class="eyebrow">Voxel mechanism lab</div>
<h1>One tooth.<br>Three machines.</h1>
<p>
The yellow <strong>pawl</strong> clicks over each tooth and drops into the
next notch. One turn therefore advances every ratchet—but each ratchet
drives a different output.
</p>
</section>
<section class="panel" id="telemetry" aria-label="Machine readouts">
<div class="telemetry-title">
<span>Live outputs</span>
<span>Clockwise only</span>
</div>
<div class="readout" style="--dot: var(--teal)">
<i></i><span>Lift</span><output id="lift-read">0 / 8</output>
</div>
<div class="readout" style="--dot: var(--yellow)">
<i></i><span>Counter</span><output id="count-read">00</output>
</div>
<div class="readout" style="--dot: var(--orange)">
<i></i><span>Winding</span><output id="wind-read">0 turns</output>
</div>
</section>
<div class="panel" id="dock">
<button id="advance" type="button">Advance all</button>
<div id="status" role="status" aria-live="polite">
Ready. Click the orange lever in the scene or press Space.
</div>
</div>
<button id="reset-view" type="button">Reset view</button>
<div id="hint">
Drag to orbit · Scroll or pinch to zoom · <span class="key">SPACE</span> to advance
</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 lighting
// -------------------------------------------------------------------------
const viewport = document.getElementById("viewport");
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xe9e1cf);
scene.fog = new THREE.Fog(0xe9e1cf, 19, 34);
const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.05;
viewport.appendChild(renderer.domElement);
const camera = new THREE.OrthographicCamera(-8, 8, 5, -5, 0.1, 100);
const target = new THREE.Vector3(0, 1.15, 0.15);
let cameraTheta = 0;
let cameraPhi = 1.03;
let cameraZoom = 1;
const cameraRadius = 17;
scene.add(new THREE.HemisphereLight(0xfff5db, 0x8b9aa3, 2.25));
const sun = new THREE.DirectionalLight(0xfff1cf, 3.2);
sun.position.set(-5, 11, 8);
sun.castShadow = true;
sun.shadow.mapSize.set(2048, 2048);
sun.shadow.camera.left = -11;
sun.shadow.camera.right = 11;
sun.shadow.camera.top = 10;
sun.shadow.camera.bottom = -8;
sun.shadow.camera.near = 1;
sun.shadow.camera.far = 30;
sun.shadow.bias = -0.00035;
scene.add(sun);
const fill = new THREE.DirectionalLight(0x8ed9df, 0.85);
fill.position.set(8, 5, -7);
scene.add(fill);
// -------------------------------------------------------------------------
// Voxel construction helpers
// -------------------------------------------------------------------------
const material = (color, options = {}) =>
new THREE.MeshStandardMaterial({
color,
roughness: options.roughness ?? 0.78,
metalness: options.metalness ?? 0,
flatShading: true,
emissive: options.emissive ?? 0x000000,
emissiveIntensity: options.emissiveIntensity ?? 1
});
const M = {
navy: material(0x243447),
navy2: material(0x3c5266),
cream: material(0xf7ebcf),
white: material(0xfff9e9),
wood: material(0xb87848),
woodDark: material(0x8b5638),
teal: material(0x3daea9),
tealDark: material(0x247c79),
orange: material(0xf06b3b),
orangeDark: material(0xb94728),
yellow: material(0xf2c14e),
red: material(0xd84d47),
metal: material(0x8495a0, { metalness: 0.42, roughness: 0.5 }),
brass: material(0xc89b45, { metalness: 0.55, roughness: 0.43 }),
ground: material(0xd9cfb9),
tile: material(0xeee5d1),
digitOn: material(0xffdf67, {
emissive: 0xff9d22,
emissiveIntensity: 0.55
}),
digitOff: material(0x38414a)
};
function box(w, h, d, mat, x = 0, y = 0, z = 0, parent = scene) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(w, h, d),
typeof mat === "string" ? M[mat] : mat
);
mesh.position.set(x, y, z);
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function beam(parent, a, b, width, depth, mat) {
const va = new THREE.Vector3(...a);
const vb = new THREE.Vector3(...b);
const midpoint = va.clone().add(vb).multiplyScalar(0.5);
const mesh = box(
va.distanceTo(vb),
width,
depth,
mat,
midpoint.x,
midpoint.y,
midpoint.z,
parent
);
mesh.rotation.z = Math.atan2(vb.y - va.y, vb.x - va.x);
return mesh;
}
function beam3D(parent, a, b, width, mat) {
const va = new THREE.Vector3(...a);
const vb = new THREE.Vector3(...b);
const midpoint = va.clone().add(vb).multiplyScalar(0.5);
const direction = vb.clone().sub(va).normalize();
const mesh = box(
va.distanceTo(vb),
width,
width,
mat,
midpoint.x,
midpoint.y,
midpoint.z,
parent
);
mesh.quaternion.setFromUnitVectors(
new THREE.Vector3(1, 0, 0),
direction
);
return mesh;
}
function makeGear(parent, x, y, z, radius, color, teeth = 10) {
const gear = new THREE.Group();
gear.position.set(x, y, z);
parent.add(gear);
box(radius * 0.55, radius * 0.55, 0.25, "navy", 0, 0, 0, gear);
box(radius * 1.7, 0.11, 0.22, color, 0, 0, 0, gear);
box(0.11, radius * 1.7, 0.22, color, 0, 0, 0, gear);
for (let i = 0; i < teeth; i++) {
const angle = (i / teeth) * Math.PI * 2;
const tooth = box(
radius * 0.35,
radius * 0.28,
0.28,
color,
Math.cos(angle) * radius * 0.83,
Math.sin(angle) * radius * 0.83,
0,
gear
);
tooth.rotation.z = angle;
}
return gear;
}
function makeRatchet(parent, x, y, color) {
const wheel = new THREE.Group();
wheel.position.set(x, y, 0.7);
parent.add(wheel);
// Axle extending behind the wheel.
box(0.16, 0.16, 0.8, "metal", 0, 0, -0.43, wheel);
// Voxel hub and four full spokes.
box(0.48, 0.48, 0.36, "navy", 0, 0, 0, wheel);
for (let i = 0; i < 4; i++) {
const spoke = box(
1.22,
0.105,
0.25,
"navy2",
0,
0,
-0.015,
wheel
);
spoke.rotation.z = (i * Math.PI) / 4;
}
// Twelve chunky rim segments and twelve asymmetric ratchet teeth.
for (let i = 0; i < 12; i++) {
const angle = (i / 12) * Math.PI * 2;
const rim = box(
0.3,
0.2,
0.31,
color,
Math.cos(angle) * 0.54,
Math.sin(angle) * 0.54,
0,
wheel
);
rim.rotation.z = angle;
const tooth = box(
0.27,
0.18,
0.34,
color,
Math.cos(angle + 0.08) * 0.76,
Math.sin(angle + 0.08) * 0.76,
0.015,
wheel
);
tooth.rotation.z = angle + 0.08;
}
box(0.2, 0.2, 0.42, "orange", 0, 0, 0.08, wheel);
// Yellow pawl, pivoted just beyond the upper-right tooth.
const pawl = new THREE.Group();
pawl.position.set(x + 0.98, y + 0.68, 0.81);
parent.add(pawl);
beam(pawl, [0, 0, 0], [-0.39, -0.23, 0], 0.14, 0.18, "yellow");
box(0.22, 0.22, 0.2, "yellow", 0, 0, 0, pawl);
box(0.18, 0.18, 0.22, "orange", -0.39, -0.23, 0.02, pawl);
// Fixed mounting bracket behind the pawl pivot.
box(
0.3,
0.54,
0.18,
"navy",
x + 0.98,
y + 0.68,
0.58,
parent
);
return { wheel, pawl, angle: 0 };
}
// -------------------------------------------------------------------------
// Ground and shared power conduit
// -------------------------------------------------------------------------
box(15.2, 0.18, 9.2, "ground", 0, -0.57, 0);
const tileGeometry = new THREE.BoxGeometry(0.96, 0.07, 0.96);
const tileCount = 15 * 9;
const tiles = new THREE.InstancedMesh(tileGeometry, M.tile, tileCount);
const tileMatrix = new THREE.Matrix4();
const tileColor = new THREE.Color();
let tileIndex = 0;
for (let z = -4; z <= 4; z++) {
for (let x = -7; x <= 7; x++) {
tileMatrix.makeTranslation(x * 0.98, -0.44, z * 0.98);
tiles.setMatrixAt(tileIndex, tileMatrix);
tileColor.set((x + z) % 2 === 0 ? 0xeee5d1 : 0xe6dcc7);
tiles.setColorAt(tileIndex, tileColor);
tileIndex++;
}
}
tiles.receiveShadow = true;
scene.add(tiles);
// The single control feeds all three machines through a visible conduit.
for (const x of [-4.6, 0, 4.6]) {
beam3D(
scene,
[0, -0.365, 2.2],
[x, -0.365, 1.25],
0.075,
"brass"
);
box(0.18, 0.12, 0.18, "brass", x, -0.365, 1.24);
}
// -------------------------------------------------------------------------
// Machine 1: lifting platform
// -------------------------------------------------------------------------
function buildLift() {
const group = new THREE.Group();
group.position.x = -4.6;
scene.add(group);
box(3.45, 0.34, 2.35, "wood", 0, -0.23, 0, group);
box(3.2, 0.11, 2.1, "woodDark", 0, -0.015, 0, group);
// Tower frame.
box(0.24, 3.2, 0.28, "navy", -1.15, 1.55, -0.34, group);
box(0.24, 3.2, 0.28, "navy", 0.62, 1.55, -0.34, group);
box(2.02, 0.25, 0.32, "navy", -0.27, 3.08, -0.34, group);
box(1.92, 0.08, 0.12, "teal", -0.27, 2.9, -0.14, group);
// Toothed rack.
for (let i = 0; i < 16; i++) {
box(
0.17,
0.075,
0.14,
i % 2 ? "metal" : "brass",
0.48,
0.28 + i * 0.16,
0.25,
group
);
}
// Moving platform and crate.
const platform = new THREE.Group();
platform.position.set(-0.28, 0.24, 0.23);
group.add(platform);
box(1.72, 0.17, 1.35, "teal", 0, 0, 0, platform);
box(1.48, 0.06, 1.14, "tealDark", 0, 0.11, 0, platform);
box(0.74, 0.68, 0.74, "orange", 0, 0.47, 0, platform);
box(0.78, 0.08, 0.78, "cream", 0, 0.5, 0, platform);
box(0.08, 0.72, 0.78, "cream", 0, 0.47, 0, platform);
box(0.3, 0.14, 0.3, "yellow", 0.18, 0.87, 0, platform);
// Output drum above the rack.
box(0.74, 0.74, 0.34, "metal", 0.78, 2.62, -0.12, group);
box(0.88, 0.12, 0.42, "orange", 0.78, 2.62, -0.12, group);
box(0.88, 0.12, 0.42, "orange", 0.78, 2.62, -0.12, group);
box(0.12, 0.9, 0.42, "orange", 0.78, 2.62, -0.12, group);
const ratchet = makeRatchet(group, 1.15, 1.46, "teal");
makeGear(group, 0.76, 2.05, 0.35, 0.34, "yellow", 8);
return { group, platform, ratchet };
}
// -------------------------------------------------------------------------
// Machine 2: mechanical counter
// -------------------------------------------------------------------------
const segmentMap = {
0: "abcdef",
1: "bc",
2: "abdeg",
3: "abcdg",
4: "bcfg",
5: "acdfg",
6: "acdefg",
7: "abc",
8: "abcdefg",
9: "abcdfg"
};
function createDigit(parent, x, y, z) {
const group = new THREE.Group();
group.position.set(x, y, z);
parent.add(group);
const segments = {};
const definitions = {
a: [0, 0.45, 0.5, 0.09],
b: [0.28, 0.225, 0.09, 0.38],
c: [0.28, -0.225, 0.09, 0.38],
d: [0, -0.45, 0.5, 0.09],
e: [-0.28, -0.225, 0.09, 0.38],
f: [-0.28, 0.225, 0.09, 0.38],
g: [0, 0, 0.5, 0.09]
};
for (const [name, values] of Object.entries(definitions)) {
const [sx, sy, w, h] = values;
segments[name] = box(w, h, 0.09, "digitOff", sx, sy, 0, group);
}
function set(value) {
const active = segmentMap[value];
for (const [name, mesh] of Object.entries(segments)) {
mesh.material = active.includes(name) ? M.digitOn : M.digitOff;
}
}
set(0);
return { group, set };
}
function buildCounter() {
const group = new THREE.Group();
scene.add(group);
box(3.45, 0.34, 2.35, "wood", 0, -0.23, 0, group);
box(3.2, 0.11, 2.1, "woodDark", 0, -0.015, 0, group);
// Display board.
box(0.22, 2.4, 0.25, "navy", -0.75, 1.15, -0.35, group);
box(0.22, 2.4, 0.25, "navy", 1.25, 1.15, -0.35, group);
box(2.18, 1.3, 0.18, "navy", 0.25, 2.62, -0.18, group);
box(2.02, 1.14, 0.07, "navy2", 0.25, 2.62, -0.075, group);
const tens = createDigit(group, -0.04, 2.62, -0.015);
const ones = createDigit(group, 0.66, 2.62, -0.015);
// Little mechanical tally wheel under the display.
const tallyWheel = makeGear(group, 0.26, 1.55, 0.34, 0.47, "yellow", 12);
box(0.13, 1.02, 0.16, "metal", 0.26, 2.05, 0.34, group);
const ratchet = makeRatchet(group, -1.15, 1.25, "yellow");
makeGear(group, -0.69, 1.49, 0.36, 0.31, "teal", 8);
return { group, ratchet, digits: [tens, ones], tallyWheel };
}
// -------------------------------------------------------------------------
// Machine 3: winding spool
// -------------------------------------------------------------------------
function buildWind() {
const group = new THREE.Group();
group.position.x = 4.6;
scene.add(group);
box(3.45, 0.34, 2.35, "wood", 0, -0.23, 0, group);
box(3.2, 0.11, 2.1, "woodDark", 0, -0.015, 0, group);
// Spool support.
box(0.25, 1.55, 0.3, "navy", -1.16, 0.72, -0.35, group);
box(0.25, 1.55, 0.3, "navy", 0.24, 0.72, -0.35, group);
box(1.62, 0.2, 0.3, "navy", -0.46, 1.48, -0.35, group);
const spool = new THREE.Group();
spool.position.set(-0.46, 0.92, 0.4);
group.add(spool);
box(0.36, 0.36, 0.88, "navy", 0, 0, 0, spool);
// Square flanges made from four beams.
for (const z of [-0.46, 0.46]) {
box(1.25, 0.13, 0.15, "metal", 0, 0.59, z, spool);
box(1.25, 0.13, 0.15, "metal", 0, -0.59, z, spool);
box(0.13, 1.05, 0.15, "metal", 0.59, 0, z, spool);
box(0.13, 1.05, 0.15, "metal", -0.59, 0, z, spool);
}
const wraps = [];
for (let i = 0; i < 14; i++) {
const wrap = new THREE.Group();
wrap.position.z = -0.3 + i * 0.045;
spool.add(wrap);
box(0.76, 0.12, 0.11, "red", 0, 0.29, 0, wrap);
box(0.76, 0.12, 0.11, "red", 0, -0.29, 0, wrap);
box(0.12, 0.46, 0.11, "red", 0.29, 0, 0, wrap);
box(0.12, 0.46, 0.11, "red", -0.29, 0, 0, wrap);
wrap.visible = false;
wraps.push(wrap);
}
// Fixed ribbon feed and anchor.
beam(
group,
[-0.84, 0.92, 0.73],
[-1.48, 0.68, 0.73],
0.12,
0.13,
"red"
);
box(0.3, 0.38, 0.32, "orange", -1.51, 0.62, 0.73, group);
const ratchet = makeRatchet(group, 1.15, 1.5, "orange");
makeGear(group, 0.66, 1.5, 0.4, 0.31, "yellow", 8);
return { group, ratchet, spool, wraps };
}
const lift = buildLift();
const counter = buildCounter();
const wind = buildWind();
// -------------------------------------------------------------------------
// One shared physical control
// -------------------------------------------------------------------------
const control = new THREE.Group();
control.position.set(0, -0.36, 2.72);
scene.add(control);
box(0.92, 0.22, 0.9, "navy", 0, 0.11, 0, control);
box(0.52, 0.46, 0.52, "cream", 0, 0.42, 0, control);
box(0.58, 0.08, 0.58, "orange", 0, 0.67, 0, control);
const leverPivot = new THREE.Group();
leverPivot.position.set(0, 0.68, 0);
leverPivot.rotation.x = -0.18;
control.add(leverPivot);
const leverBeam = beam(
leverPivot,
[0, 0, 0],
[0, 0.9, 0],
0.13,
0.15,
"metal"
);
const leverHandle = box(
0.5,
0.22,
0.24,
"orange",
0,
0.98,
0,
leverPivot
);
const leverHitTargets = [leverHandle, leverBeam];
// A little voxel arrow pointing toward the button.
box(0.12, 0.08, 0.26, "orange", -0.19, 0.12, 0.51, control);
box(0.12, 0.08, 0.26, "orange", 0.19, 0.12, 0.51, control);
box(0.28, 0.08, 0.16, "orange", 0, 0.12, 0.67, control);
// -------------------------------------------------------------------------
// State and interaction
// -------------------------------------------------------------------------
const advanceButton = document.getElementById("advance");
const statusElement = document.getElementById("status");
const liftRead = document.getElementById("lift-read");
const countRead = document.getElementById("count-read");
const windRead = document.getElementById("wind-read");
const state = {
lift: 0,
count: 0,
wind: 0,
busy: false
};
const ratchets = [lift.ratchet, counter.ratchet, wind.ratchet];
const toothAngle = (Math.PI * 2) / 12;
let animation = null;
let audioContext = null;
function updateTelemetry() {
liftRead.textContent = `${state.lift} / 8`;
countRead.textContent = String(state.count).padStart(2, "0");
windRead.textContent = `${state.wind} ${state.wind === 1 ? "turn" : "turns"}`;
}
function updateCounterDigits() {
const tens = Math.floor(state.count / 10);
const ones = state.count % 10;
counter.digits[0].set(tens);
counter.digits[1].set(ones);
}
function updateWindLayers() {
wind.wraps.forEach((wrap, index) => {
wrap.visible = index < Math.min(state.wind, wind.wraps.length);
});
}
function makeClickSound(frequency = 190, duration = 0.045) {
try {
audioContext ??= new (window.AudioContext || window.webkitAudioContext)();
const oscillator = audioContext.createOscillator();
const gain = audioContext.createGain();
oscillator.type = "square";
oscillator.frequency.setValueAtTime(frequency, audioContext.currentTime);
oscillator.frequency.exponentialRampToValueAtTime(
frequency * 0.55,
audioContext.currentTime + duration
);
gain.gain.setValueAtTime(0.08, audioContext.currentTime);
gain.gain.exponentialRampToValueAtTime(
0.0001,
audioContext.currentTime + duration
);
oscillator.connect(gain);
gain.connect(audioContext.destination);
oscillator.start();
oscillator.stop(audioContext.currentTime + duration);
} catch {
// Audio is optional.
}
}
function easeInOutCubic(t) {
return t < 0.5
? 4 * t * t * t
: 1 - Math.pow(-2 * t + 2, 3) / 2;
}
function easeOutCubic(t) {
return 1 - Math.pow(1 - t, 3);
}
function advanceAll() {
if (state.busy) {
statusElement.textContent = "The pawls are catching. Let this tooth finish.";
return;
}
state.busy = true;
advanceButton.classList.add("busy");
statusElement.textContent = "Clockwise drive engaged — all three pawls are lifting.";
animation = {
start: performance.now(),
duration: 920,
committed: false,
liftStart: lift.platform.position.y,
spoolStart: wind.spool.rotation.z,
ratchetStarts: ratchets.map((r) => r.angle)
};
makeClickSound(145, 0.055);
}
function commitOutputs() {
state.lift = Math.min(8, state.lift + 1);
state.count = (state.count + 1) % 100;
state.wind += 1;
updateTelemetry();
updateCounterDigits();
updateWindLayers();
makeClickSound(260, 0.035);
window.setTimeout(() => makeClickSound(105, 0.04), 55);
}
function updateAnimation(now) {
if (!animation) return;
const p = Math.min(1, (now - animation.start) / animation.duration);
// The wheel turns smoothly, then makes a tiny settling snap.
const turn =
p < 0.7
? easeInOutCubic(p / 0.7)
: 1 + Math.sin(((p - 0.7) / 0.3) * Math.PI) * 0.035;
ratchets.forEach((ratchet, index) => {
ratchet.angle = animation.ratchetStarts[index] - toothAngle * turn;
ratchet.wheel.rotation.z = ratchet.angle;
const flex =
p < 0.52
? Math.sin((p / 0.52) * Math.PI) * 0.27
: Math.sin(((p - 0.52) / 0.48) * Math.PI) * 0.06;
ratchet.pawl.rotation.z = -flex;
});
// The lever presses forward and returns.
const leverPress = p < 0.44
? Math.sin((p / 0.44) * Math.PI)
: 0;
leverPivot.rotation.x = -0.18 + leverPress * 0.68;
// Outputs engage after the ratchet teeth have passed the pawls.
if (p >= 0.38 && !animation.committed) {
animation.committed = true;
commitOutputs();
}
if (animation.committed) {
const outputProgress = easeOutCubic((p - 0.38) / 0.62);
const targetLiftY = 0.24 + state.lift * 0.27;
lift.platform.position.y = THREE.MathUtils.lerp(
animation.liftStart,
targetLiftY,
outputProgress
);
wind.spool.rotation.z = animation.spoolStart - toothAngle * turn;
counter.tallyWheel.rotation.z = -toothAngle * turn * 2.2;
}
if (p >= 1) {
animation = null;
state.busy = false;
advanceButton.classList.remove("busy");
const liftNote = state.lift === 8 ? " Lift is at the top." : "";
statusElement.textContent =
`One tooth complete: crate lifted, counter incremented, ribbon wound.${liftNote}`;
}
}
advanceButton.addEventListener("click", advanceAll);
window.addEventListener("keydown", (event) => {
if (
event.code === "Space" &&
event.target instanceof HTMLElement &&
event.target.tagName !== "BUTTON"
) {
event.preventDefault();
advanceAll();
}
});
// -------------------------------------------------------------------------
// Custom camera controls: orbit, zoom, and direct 3D lever clicking
// -------------------------------------------------------------------------
const raycaster = new THREE.Raycaster();
const pointerNDC = new THREE.Vector2();
const pointers = new Map();
let gestureMoved = false;
let previousPinchDistance = 0;
function setPointerNDC(event) {
const rect = renderer.domElement.getBoundingClientRect();
pointerNDC.x = ((event.clientX - rect.left) / rect.width) * 2 - 1;
pointerNDC.y = -((event.clientY - rect.top) / rect.height) * 2 + 1;
}
renderer.domElement.addEventListener("pointerdown", (event) => {
renderer.domElement.setPointerCapture(event.pointerId);
pointers.set(event.pointerId, { x: event.clientX, y: event.clientY });
if (pointers.size === 1) {
gestureMoved = false;
}
if (pointers.size === 2) {
const points = [...pointers.values()];
previousPinchDistance = Math.hypot(
points[0].x - points[1].x,
points[0].y - points[1].y
);
}
});
renderer.domElement.addEventListener("pointermove", (event) => {
const previous = pointers.get(event.pointerId);
if (!previous) return;
const dx = event.clientX - previous.x;
const dy = event.clientY - previous.y;
pointers.set(event.pointerId, { x: event.clientX, y: event.clientY });
if (Math.abs(dx) + Math.abs(dy) > 2) {
gestureMoved = true;
}
if (pointers.size === 1) {
cameraTheta -= dx * 0.006;
cameraPhi = THREE.MathUtils.clamp(
cameraPhi - dy * 0.005,
0.63,
1.36
);
} else if (pointers.size === 2) {
const points = [...pointers.values()];
const distance = Math.hypot(
points[0].x - points[1].x,
points[0].y - points[1].y
);
if (previousPinchDistance > 0) {
cameraZoom = THREE.MathUtils.clamp(
cameraZoom * (distance / previousPinchDistance),
0.7,
1.8
);
}
previousPinchDistance = distance;
}
});
renderer.domElement.addEventListener("pointerup", (event) => {
const wasSingle = pointers.size === 1;
pointers.delete(event.pointerId);
if (wasSingle && !gestureMoved) {
setPointerNDC(event);
raycaster.setFromCamera(pointerNDC, camera);
const hits = raycaster.intersectObjects(leverHitTargets, false);
if (hits.length) advanceAll();
}
if (pointers.size < 2) previousPinchDistance = 0;
});
renderer.domElement.addEventListener("pointercancel", (event) => {
pointers.delete(event.pointerId);
previousPinchDistance = 0;
});
renderer.domElement.addEventListener(
"wheel",
(event) => {
event.preventDefault();
cameraZoom = THREE.MathUtils.clamp(
cameraZoom * Math.exp(-event.deltaY * 0.001),
0.7,
1.8
);
},
{ passive: false }
);
document.getElementById("reset-view").addEventListener("click", () => {
cameraTheta = 0;
cameraPhi = 1.03;
cameraZoom = 1;
});
function updateCamera() {
const width = viewport.clientWidth;
const height = viewport.clientHeight;
const aspect = width / Math.max(1, height);
const viewHeight = Math.max(8.15, 15.1 / aspect) / cameraZoom;
camera.left = (-viewHeight * aspect) / 2;
camera.right = (viewHeight * aspect) / 2;
camera.top = viewHeight / 2;
camera.bottom = -viewHeight / 2;
const sinPhi = Math.sin(cameraPhi);
camera.position.set(
target.x + cameraRadius * sinPhi * Math.sin(cameraTheta),
target.y + cameraRadius * Math.cos(cameraPhi),
target.z + cameraRadius * sinPhi * Math.cos(cameraTheta)
);
camera.lookAt(target);
camera.updateProjectionMatrix();
camera.updateMatrixWorld();
}
// -------------------------------------------------------------------------
// HTML labels projected from 3D anchor points
// -------------------------------------------------------------------------
const projectedLabels = [
{
element: document.getElementById("label-lift"),
anchor: new THREE.Vector3(-4.6, 3.68, 0)
},
{
element: document.getElementById("label-counter"),
anchor: new THREE.Vector3(0, 3.95, 0)
},
{
element: document.getElementById("label-wind"),
anchor: new THREE.Vector3(4.6, 3.68, 0)
}
];
function updateLabels() {
const width = viewport.clientWidth;
const height = viewport.clientHeight;
projectedLabels.forEach(({ element, anchor }) => {
const projected = anchor.clone().project(camera);
const x = (projected.x * 0.5 + 0.5) * width;
const y = (-projected.y * 0.5 + 0.5) * height;
element.style.left = `${x}px`;
element.style.top = `${y}px`;
element.style.visibility =
projected.z > -1 && projected.z < 1 ? "visible" : "hidden";
});
}
function resize() {
renderer.setSize(viewport.clientWidth, viewport.clientHeight, false);
updateCamera();
}
window.addEventListener("resize", resize);
updateTelemetry();
updateCounterDigits();
updateWindLayers();
resize();
function animate(now) {
requestAnimationFrame(animate);
updateAnimation(now);
updateCamera();
updateLabels();
renderer.render(scene, camera);
}
requestAnimationFrame(animate);
</script>
</body>
</html>
```