Prompt specification
Build or customize the Three.js 3D scene "Pawl Orientation Changes Allowed Rotation" 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: Pawl Orientation Changes Allowed Rotation
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/pawl-orientation-changes-allowed-rotation-x2056
Scene Description Prompt:
Reverse a voxel ratchet's pawl orientation and show the resulting change in permitted wheel rotation. Make one HTML file using Three.js from cdnjs.
Working Three.js Starter (pawl-orientation-changes-allowed-rotation-x2056):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Voxel Ratchet — Reverse the Pawl</title>
<style>
:root {
color-scheme: dark;
--bg: #07111e;
--panel: rgba(12, 24, 40, 0.84);
--panel-border: rgba(126, 216, 255, 0.18);
--text: #eaf6ff;
--muted: #8fa8bd;
--cyan: #5ee7f2;
--green: #61f2ad;
--red: #ff657a;
--amber: #ffd166;
--pink: #ff6fae;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background:
radial-gradient(circle at 46% 42%, #122b43 0%, #091727 38%, #050c15 78%),
var(--bg);
color: var(--text);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
button {
font: inherit;
}
#stage {
position: fixed;
inset: 0;
}
#stage canvas {
display: block;
width: 100%;
height: 100%;
cursor: grab;
touch-action: none;
}
#stage canvas:active {
cursor: grabbing;
}
.header {
position: fixed;
z-index: 10;
top: 24px;
left: 26px;
pointer-events: none;
text-shadow: 0 2px 18px rgba(0, 0, 0, 0.6);
}
.eyebrow {
color: var(--cyan);
font-size: 11px;
font-weight: 800;
letter-spacing: 0.18em;
text-transform: uppercase;
}
h1 {
margin: 7px 0 5px;
font-size: clamp(25px, 3vw, 42px);
line-height: 1;
letter-spacing: -0.04em;
}
.subtitle {
margin: 0;
color: var(--muted);
font-size: 13px;
}
.panel {
position: fixed;
z-index: 20;
top: 24px;
right: 24px;
width: min(340px, calc(100vw - 48px));
padding: 19px;
border: 1px solid var(--panel-border);
border-radius: 18px;
background: var(--panel);
box-shadow:
0 18px 60px rgba(0, 0, 0, 0.38),
inset 0 1px rgba(255, 255, 255, 0.035);
backdrop-filter: blur(16px);
}
.status-row {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
margin-bottom: 15px;
}
.status-label {
color: var(--muted);
font-size: 10px;
font-weight: 800;
letter-spacing: 0.13em;
text-transform: uppercase;
}
.status-pill {
display: inline-flex;
align-items: center;
gap: 8px;
min-width: 112px;
justify-content: center;
padding: 7px 10px;
border: 1px solid rgba(255, 209, 102, 0.35);
border-radius: 999px;
background: rgba(255, 209, 102, 0.09);
color: var(--amber);
font-size: 11px;
font-weight: 900;
letter-spacing: 0.09em;
}
.status-pill::before {
content: "";
width: 7px;
height: 7px;
border-radius: 50%;
background: currentColor;
box-shadow: 0 0 14px currentColor;
}
.status-pill.reversed {
border-color: rgba(255, 111, 174, 0.42);
background: rgba(255, 111, 174, 0.1);
color: var(--pink);
}
.primary {
width: 100%;
padding: 13px 15px;
border: 0;
border-radius: 12px;
background: linear-gradient(135deg, #70eaf2, #3dc9e9);
color: #04121c;
box-shadow: 0 9px 24px rgba(61, 201, 233, 0.2);
cursor: pointer;
font-size: 13px;
font-weight: 900;
letter-spacing: 0.025em;
transition: transform 160ms ease, filter 160ms ease;
}
.primary:hover {
filter: brightness(1.08);
transform: translateY(-1px);
}
.primary:active {
transform: translateY(1px);
}
.readouts {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 9px;
margin: 14px 0;
}
.readout {
min-height: 73px;
padding: 11px 12px;
border: 1px solid rgba(255, 255, 255, 0.075);
border-radius: 12px;
background: rgba(255, 255, 255, 0.025);
}
.readout small {
display: block;
margin-bottom: 7px;
color: var(--muted);
font-size: 9px;
font-weight: 800;
letter-spacing: 0.12em;
text-transform: uppercase;
}
.readout strong {
display: block;
font-size: 13px;
line-height: 1.25;
}
#freeDirection {
color: var(--green);
}
#heldDirection {
color: var(--red);
}
.secondary-row {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 9px;
}
.secondary {
padding: 10px 9px;
border: 1px solid rgba(255, 255, 255, 0.1);
border-radius: 10px;
background: rgba(255, 255, 255, 0.045);
color: var(--text);
cursor: pointer;
font-size: 11px;
font-weight: 750;
transition: background 150ms ease, border-color 150ms ease;
}
.secondary:hover {
border-color: rgba(94, 231, 242, 0.4);
background: rgba(94, 231, 242, 0.08);
}
.instructions {
margin: 14px 1px 0;
padding-top: 13px;
border-top: 1px solid rgba(255, 255, 255, 0.07);
color: var(--muted);
font-size: 11px;
line-height: 1.55;
}
kbd {
display: inline-block;
min-width: 19px;
padding: 1px 5px;
border: 1px solid rgba(255, 255, 255, 0.15);
border-bottom-width: 2px;
border-radius: 5px;
background: rgba(255, 255, 255, 0.055);
color: #d9ecf8;
font: 10px ui-monospace, SFMono-Regular, Menlo, monospace;
text-align: center;
}
#labels {
position: fixed;
z-index: 8;
inset: 0;
overflow: hidden;
pointer-events: none;
}
.world-label {
position: absolute;
left: 0;
top: 0;
transform: translate(-50%, -50%);
padding: 6px 9px;
border: 1px solid rgba(255, 255, 255, 0.13);
border-radius: 8px;
background: rgba(4, 12, 21, 0.8);
box-shadow: 0 6px 22px rgba(0, 0, 0, 0.25);
color: var(--text);
font-size: 9px;
font-weight: 850;
letter-spacing: 0.09em;
white-space: nowrap;
backdrop-filter: blur(8px);
}
#permittedLabel {
border-color: rgba(97, 242, 173, 0.35);
color: var(--green);
}
#blockedLabel {
border-color: rgba(255, 101, 122, 0.38);
color: var(--red);
}
#catchLabel {
border-color: rgba(255, 209, 102, 0.38);
color: var(--amber);
}
.legend {
position: fixed;
z-index: 9;
left: 26px;
bottom: 23px;
display: flex;
gap: 17px;
align-items: center;
color: var(--muted);
font-size: 10px;
font-weight: 700;
letter-spacing: 0.06em;
pointer-events: none;
}
.legend span {
display: inline-flex;
gap: 7px;
align-items: center;
}
.dot {
width: 8px;
height: 8px;
border-radius: 50%;
box-shadow: 0 0 10px currentColor;
}
.dot.green {
color: var(--green);
background: currentColor;
}
.dot.red {
color: var(--red);
background: currentColor;
}
.dot.amber {
color: var(--amber);
background: currentColor;
}
#toast {
position: fixed;
z-index: 30;
left: 50%;
bottom: 28px;
max-width: calc(100vw - 40px);
padding: 11px 17px;
border: 1px solid rgba(255, 255, 255, 0.14);
border-radius: 999px;
background: rgba(7, 17, 30, 0.92);
box-shadow: 0 10px 40px rgba(0, 0, 0, 0.38);
color: var(--text);
font-size: 11px;
font-weight: 750;
text-align: center;
pointer-events: none;
opacity: 0;
transform: translate(-50%, 12px);
transition: opacity 180ms ease, transform 180ms ease;
}
#toast.show {
opacity: 1;
transform: translate(-50%, 0);
}
@media (max-width: 760px) {
.header {
top: 17px;
left: 18px;
}
.header .subtitle {
display: none;
}
.panel {
top: auto;
right: 12px;
bottom: 12px;
left: 12px;
width: auto;
padding: 14px;
}
.readouts {
margin: 10px 0;
}
.readout {
min-height: 60px;
padding: 9px 10px;
}
.instructions {
display: none;
}
.legend {
display: none;
}
.world-label {
font-size: 8px;
}
}
</style>
</head>
<body>
<div id="stage"></div>
<header class="header">
<div class="eyebrow">Mechanical Logic / 01</div>
<h1>Voxel Ratchet</h1>
<p class="subtitle">Reverse the pawl catch to reverse the permitted wheel rotation.</p>
</header>
<section class="panel" aria-label="Ratchet controls">
<div class="status-row">
<div>
<div class="status-label">Pawl orientation</div>
<div style="margin-top:6px;color:var(--muted);font-size:11px">
Catch face: <strong id="catchFace" style="color:var(--amber)">↑ Upper</strong>
</div>
</div>
<div id="statusPill" class="status-pill" aria-live="polite">ORIGINAL</div>
</div>
<button id="reverseButton" class="primary">⇄ Reverse Pawl Orientation</button>
<div class="readouts">
<div class="readout">
<small>Permitted rotation</small>
<strong id="freeDirection">↻ Clockwise</strong>
</div>
<div class="readout">
<small>Pawl holds</small>
<strong id="heldDirection">↺ Counter-clockwise</strong>
</div>
</div>
<div class="secondary-row">
<button id="freeButton" class="secondary">Pause Free Run</button>
<button id="blockedButton" class="secondary">Try Blocked Way</button>
</div>
<p class="instructions">
Drag to orbit · Mouse wheel to zoom · Double-click to reset the camera.<br />
Keyboard: <kbd>R</kbd> reverse pawl · <kbd>B</kbd> blocked test · <kbd>P</kbd> pause.
</p>
</section>
<div id="labels" aria-hidden="true">
<div id="permittedLabel" class="world-label">PERMITTED · ↻ CLOCKWISE</div>
<div id="blockedLabel" class="world-label">PAWL HOLDS · ↺ COUNTER-CLOCKWISE</div>
<div id="catchLabel" class="world-label">CATCH FACE ↑</div>
</div>
<div class="legend" aria-hidden="true">
<span><i class="dot green"></i> Permitted motion</span>
<span><i class="dot red"></i> Blocked motion</span>
<span><i class="dot amber"></i> Pawl assembly</span>
</div>
<div id="toast" role="status" aria-live="polite"></div>
<script type="module">
import * as THREE from
"https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
const host = document.getElementById("stage");
const renderer = new THREE.WebGLRenderer({
antialias: true,
alpha: true,
powerPreference: "high-performance"
});
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setClearColor(0x000000, 0);
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.15;
host.appendChild(renderer.domElement);
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(
42,
window.innerWidth / window.innerHeight,
0.1,
100
);
const view = {
yaw: 0.08,
pitch: 0.12,
radius: 11.7,
target: new THREE.Vector3(0, 0, 0)
};
function updateCamera() {
const horizontal = Math.cos(view.pitch) * view.radius;
camera.position.set(
view.target.x + Math.sin(view.yaw) * horizontal,
view.target.y + Math.sin(view.pitch) * view.radius,
view.target.z + Math.cos(view.yaw) * horizontal
);
camera.lookAt(view.target);
}
updateCamera();
// Lighting
scene.add(new THREE.HemisphereLight(0xbcecff, 0x142033, 1.65));
const key = new THREE.DirectionalLight(0xffffff, 3.2);
key.position.set(5, 8, 9);
key.castShadow = true;
key.shadow.mapSize.set(2048, 2048);
key.shadow.camera.left = -8;
key.shadow.camera.right = 8;
key.shadow.camera.top = 8;
key.shadow.camera.bottom = -8;
key.shadow.bias = -0.0006;
scene.add(key);
const rimLight = new THREE.DirectionalLight(0x58d9ff, 2.3);
rimLight.position.set(-7, 2, 4);
scene.add(rimLight);
const warmLight = new THREE.PointLight(0xffb347, 18, 12, 2);
warmLight.position.set(5, -1, 5);
scene.add(warmLight);
// Floor and grid
const floor = new THREE.Mesh(
new THREE.PlaneGeometry(30, 30),
new THREE.MeshStandardMaterial({
color: 0x08121e,
roughness: 0.88,
metalness: 0.08
})
);
floor.rotation.x = -Math.PI / 2;
floor.position.y = -3.36;
floor.receiveShadow = true;
scene.add(floor);
const grid = new THREE.GridHelper(24, 24, 0x315069, 0x172b3d);
grid.position.y = -3.345;
grid.material.transparent = true;
grid.material.opacity = 0.58;
scene.add(grid);
// Dark backing ring
const backing = new THREE.Mesh(
new THREE.RingGeometry(3.13, 3.3, 80),
new THREE.MeshBasicMaterial({
color: 0x1b6078,
transparent: true,
opacity: 0.23,
side: THREE.DoubleSide,
depthWrite: false
})
);
backing.position.z = -0.58;
scene.add(backing);
// Voxel helpers
const voxelGeometry = new THREE.BoxGeometry(1, 1, 1);
const dummy = new THREE.Object3D();
function makeInstancedVoxels(items, material) {
const mesh = new THREE.InstancedMesh(
voxelGeometry,
material,
items.length
);
items.forEach((item, index) => {
dummy.position.set(item.x, item.y, item.z ?? 0);
dummy.scale.set(item.sx, item.sy, item.sz);
dummy.updateMatrix();
mesh.setMatrixAt(index, dummy.matrix);
mesh.setColorAt(index, new THREE.Color(item.color));
});
mesh.instanceMatrix.needsUpdate = true;
if (mesh.instanceColor) mesh.instanceColor.needsUpdate = true;
mesh.castShadow = true;
mesh.receiveShadow = true;
mesh.computeBoundingSphere();
return mesh;
}
// Wheel assembly
const wheel = new THREE.Group();
scene.add(wheel);
const wheelMaterial = new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 0.43,
metalness: 0.24
});
const wheelVoxels = [];
const step = 0.32;
// Voxel rim
for (let ix = -8; ix <= 8; ix++) {
for (let iy = -8; iy <= 8; iy++) {
const x = ix * step;
const y = iy * step;
const radius = Math.hypot(x, y);
if (radius >= 1.78 && radius <= 2.43) {
const alternate = (ix + iy) & 1;
wheelVoxels.push({
x,
y,
sx: 0.302,
sy: 0.302,
sz: 0.5,
color: alternate ? 0x1a9fb6 : 0x1688a5
});
}
}
}
// Voxel hub
for (let ix = -2; ix <= 2; ix++) {
for (let iy = -2; iy <= 2; iy++) {
const x = ix * step;
const y = iy * step;
if (Math.hypot(x, y) <= 0.72) {
wheelVoxels.push({
x,
y,
sx: 0.302,
sy: 0.302,
sz: 0.54,
color: 0x34c6d2
});
}
}
}
// Six radial voxel spokes
for (let spoke = 0; spoke < 6; spoke++) {
const angle = (spoke / 6) * Math.PI * 2;
for (let radius = 0.67; radius <= 1.96; radius += step) {
wheelVoxels.push({
x: Math.cos(angle) * radius,
y: Math.sin(angle) * radius,
sx: 0.302,
sy: 0.302,
sz: 0.5,
color: 0x146c8a
});
}
}
const wheelBody = makeInstancedVoxels(wheelVoxels, wheelMaterial);
wheel.add(wheelBody);
// Asymmetric voxel ratchet teeth
const toothVoxels = [];
const toothCount = 16;
for (let index = 0; index < toothCount; index++) {
const angle = (index / toothCount) * Math.PI * 2;
const radialX = Math.cos(angle);
const radialY = Math.sin(angle);
const tangentX = -Math.sin(angle);
const tangentY = Math.cos(angle);
toothVoxels.push({
x: radialX * 2.57,
y: radialY * 2.57,
sx: 0.34,
sy: 0.34,
sz: 0.56,
color: 0x54d7df
});
toothVoxels.push({
x: radialX * 2.88 + tangentX * 0.16,
y: radialY * 2.88 + tangentY * 0.16,
sx: 0.34,
sy: 0.34,
sz: 0.56,
color: index % 2 ? 0x7beee1 : 0x69e7e5
});
}
const teeth = makeInstancedVoxels(toothVoxels, wheelMaterial);
wheel.add(teeth);
// Axle
const axle = new THREE.Mesh(
new THREE.CylinderGeometry(0.28, 0.28, 1.25, 20),
new THREE.MeshStandardMaterial({
color: 0x34475a,
roughness: 0.3,
metalness: 0.8
})
);
axle.rotation.x = Math.PI / 2;
axle.position.z = 0.08;
axle.castShadow = true;
wheel.add(axle);
const axleCap = new THREE.Mesh(
new THREE.CylinderGeometry(0.13, 0.13, 1.4, 16),
new THREE.MeshStandardMaterial({
color: 0xffc45e,
roughness: 0.26,
metalness: 0.72
})
);
axleCap.rotation.x = Math.PI / 2;
axleCap.position.z = 0.14;
axleCap.castShadow = true;
wheel.add(axleCap);
// Fixed pawl support
const supportMaterial = new THREE.MeshStandardMaterial({
color: 0x20384c,
roughness: 0.52,
metalness: 0.55
});
const support = new THREE.Mesh(
new THREE.BoxGeometry(0.25, 3.18, 0.28),
supportMaterial
);
support.position.set(3.64, -1.65, 0.18);
support.castShadow = true;
scene.add(support);
const supportFoot = new THREE.Mesh(
new THREE.BoxGeometry(0.9, 0.18, 0.72),
supportMaterial
);
supportFoot.position.set(3.64, -3.08, 0.12);
supportFoot.castShadow = true;
scene.add(supportFoot);
// Pawl assembly
const pawlRoot = new THREE.Group();
pawlRoot.position.set(3.64, 0, 0.64);
scene.add(pawlRoot);
const pawlMaterial = new THREE.MeshStandardMaterial({
color: 0xffbd3e,
roughness: 0.32,
metalness: 0.52
});
const pawlArmVoxels = [];
for (let index = 0; index < 4; index++) {
pawlArmVoxels.push({
x: -0.15 - index * 0.27,
y: 0,
z: 0,
sx: 0.28,
sy: 0.28,
sz: 0.31,
color: index % 2 ? 0xffc44f : 0xf2a92f
});
}
const pawlArm = makeInstancedVoxels(pawlArmVoxels, pawlMaterial);
pawlRoot.add(pawlArm);
const catchMaterial = new THREE.MeshStandardMaterial({
color: 0xffd166,
roughness: 0.25,
metalness: 0.58,
emissive: 0x3a2100,
emissiveIntensity: 0.25
});
const catchBlock = new THREE.Mesh(
new THREE.BoxGeometry(0.34, 0.34, 0.34),
catchMaterial
);
catchBlock.position.set(-1.03, 0.18, 0);
catchBlock.castShadow = true;
pawlRoot.add(catchBlock);
const pivot = new THREE.Mesh(
new THREE.CylinderGeometry(0.29, 0.29, 0.42, 18),
new THREE.MeshStandardMaterial({
color: 0x455d71,
roughness: 0.25,
metalness: 0.85
})
);
pivot.rotation.x = Math.PI / 2;
pivot.position.set(3.64, 0, 0.76);
pivot.castShadow = true;
scene.add(pivot);
const pivotCap = new THREE.Mesh(
new THREE.CylinderGeometry(0.11, 0.11, 0.5, 16),
new THREE.MeshStandardMaterial({
color: 0xffd166,
roughness: 0.2,
metalness: 0.75
})
);
pivotCap.rotation.x = Math.PI / 2;
pivotCap.position.set(3.64, 0, 0.81);
pivotCap.castShadow = true;
scene.add(pivotCap);
// Direction arrows
function createArcArrow(start, end, radius, color, z, thickness = 0.035) {
const points = [];
const segments = 72;
for (let index = 0; index <= segments; index++) {
const t = index / segments;
const angle = THREE.MathUtils.lerp(start, end, t);
points.push(
new THREE.Vector3(
Math.cos(angle) * radius,
Math.sin(angle) * radius,
z
)
);
}
const curve = new THREE.CatmullRomCurve3(points);
const material = new THREE.MeshBasicMaterial({
color,
transparent: true,
opacity: 1,
depthTest: false,
depthWrite: false
});
const group = new THREE.Group();
const tube = new THREE.Mesh(
new THREE.TubeGeometry(curve, 72, thickness, 8, false),
material
);
const arrowHead = new THREE.Mesh(
new THREE.ConeGeometry(0.13, 0.34, 12),
material
);
const tangent = points[points.length - 1]
.clone()
.sub(points[points.length - 2])
.normalize();
arrowHead.position.copy(points[points.length - 1]);
arrowHead.quaternion.setFromUnitVectors(
new THREE.Vector3(0, 1, 0),
tangent
);
group.add(tube, arrowHead);
group.renderOrder = 20;
group.userData.material = material;
return group;
}
// +Z is counter-clockwise when viewed from the front.
const greenCW = createArcArrow(
2.35, -2.35, 3.55, 0x61f2ad, 1.0, 0.038
);
const greenCCW = createArcArrow(
-2.35, 2.35, 3.55, 0x61f2ad, 1.0, 0.038
);
const redCCW = createArcArrow(
-0.42, 0.42, 3.28, 0xff657a, 1.12, 0.044
);
const redCW = createArcArrow(
0.42, -0.42, 3.28, 0xff657a, 1.12, 0.044
);
scene.add(greenCW, greenCCW, redCCW, redCW);
// Simulation state
let reversed = false;
let orientationMix = 0;
let wheelAngle = 0;
let wheelSpeed = -0.5;
let freeRunning = true;
let blockedTestTime = -1;
let blockedDirection = 1;
let pawlFlex = 0;
const statusPill = document.getElementById("statusPill");
const catchFace = document.getElementById("catchFace");
const freeDirection = document.getElementById("freeDirection");
const heldDirection = document.getElementById("heldDirection");
const reverseButton = document.getElementById("reverseButton");
const freeButton = document.getElementById("freeButton");
const blockedButton = document.getElementById("blockedButton");
const toast = document.getElementById("toast");
const permittedLabel = document.getElementById("permittedLabel");
const blockedLabel = document.getElementById("blockedLabel");
const catchLabel = document.getElementById("catchLabel");
function showToast(message, duration = 1800) {
toast.textContent = message;
toast.classList.add("show");
window.clearTimeout(showToast.timeout);
toast.timeout = window.setTimeout(() => {
toast.classList.remove("show");
}, duration);
}
function updateStateUI() {
statusPill.textContent = reversed ? "REVERSED" : "ORIGINAL";
statusPill.classList.toggle("reversed", reversed);
catchFace.textContent = reversed ? "↓ Lower" : "↑ Upper";
catchFace.style.color = reversed ? "var(--pink)" : "var(--amber)";
freeDirection.textContent = reversed
? "↺ Counter-clockwise"
: "↻ Clockwise";
heldDirection.textContent = reversed
? "↻ Clockwise"
: "↺ Counter-clockwise";
permittedLabel.textContent = reversed
? "PERMITTED · ↺ COUNTER-CLOCKWISE"
: "PERMITTED · ↻ CLOCKWISE";
blockedLabel.textContent = reversed
? "PAWL HOLDS · ↻ CLOCKWISE"
: "PAWL HOLDS · ↺ COUNTER-CLOCKWISE";
catchLabel.textContent = reversed ? "CATCH FACE ↓" : "CATCH FACE ↑";
}
function reversePawl() {
reversed = !reversed;
blockedTestTime = -1;
updateStateUI();
showToast(
reversed
? "Pawl reversed — counter-clockwise rotation is now permitted."
: "Pawl restored — clockwise rotation is now permitted."
);
}
function testBlockedDirection() {
blockedDirection = reversed ? -1 : 1;
blockedTestTime = 0;
wheelSpeed = blockedDirection * 0.92;
showToast(
"Blocked direction attempted — the pawl catches and pushes back.",
2100
);
}
function toggleFreeRun() {
freeRunning = !freeRunning;
freeButton.textContent = freeRunning ? "Pause Free Run" : "Resume Free Run";
showToast(
freeRunning
? "Free run resumed in the permitted direction."
: "Free run paused."
);
}
reverseButton.addEventListener("click", reversePawl);
freeButton.addEventListener("click", toggleFreeRun);
blockedButton.addEventListener("click", testBlockedDirection);
window.addEventListener("keydown", (event) => {
if (event.repeat) return;
const key = event.key.toLowerCase();
if (key === "r") {
reversePawl();
} else if (key === "b") {
testBlockedDirection();
} else if (key === "p") {
toggleFreeRun();
} else if (event.code === "Space") {
event.preventDefault();
testBlockedDirection();
}
});
updateStateUI();
// Direct camera controls, implemented without addons
let dragging = false;
let previousX = 0;
let previousY = 0;
const canvas = renderer.domElement;
canvas.addEventListener("pointerdown", (event) => {
dragging = true;
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;
view.yaw -= deltaX * 0.0055;
view.pitch = THREE.MathUtils.clamp(
view.pitch + deltaY * 0.0055,
-0.62,
0.72
);
previousX = event.clientX;
previousY = event.clientY;
updateCamera();
});
function endDrag(event) {
dragging = false;
if (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,
7.5,
18
);
updateCamera();
},
{ passive: false }
);
canvas.addEventListener("dblclick", () => {
view.yaw = 0.08;
view.pitch = 0.12;
view.radius = 11.7;
updateCamera();
});
function resize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
}
window.addEventListener("resize", resize);
// Project HTML labels from 3D coordinates
const labelPosition = new THREE.Vector3();
function positionLabel(element, x, y, z) {
labelPosition.set(x, y, z).project(camera);
const visible = labelPosition.z > -1 && labelPosition.z < 1;
element.style.display = visible ? "block" : "none";
if (!visible) return;
const screenX = (labelPosition.x * 0.5 + 0.5) * window.innerWidth;
const screenY = (-labelPosition.y * 0.5 + 0.5) * window.innerHeight;
element.style.left = `${screenX}px`;
element.style.top = `${screenY}px`;
}
function damp(current, target, smoothing, delta) {
return THREE.MathUtils.lerp(
current,
target,
1 - Math.exp(-smoothing * delta)
);
}
const originalCatchColor = new THREE.Color(0xffd166);
const reversedCatchColor = new THREE.Color(0xff6fae);
const temporaryColor = new THREE.Color();
const clock = new THREE.Clock();
function animate() {
requestAnimationFrame(animate);
const delta = Math.min(clock.getDelta(), 0.05);
const elapsed = clock.elapsedTime;
// Animate the physical catch between its two orientations.
orientationMix = damp(
orientationMix,
reversed ? 1 : 0,
7,
delta
);
const easedMix =
orientationMix * orientationMix *
(3 - 2 * orientationMix);
const allowedSign = reversed ? 1 : -1;
let targetSpeed = freeRunning ? allowedSign * 0.52 : 0;
let targetFlex = 0;
if (blockedTestTime >= 0) {
blockedTestTime += delta;
if (blockedTestTime < 0.38) {
targetSpeed = blockedDirection * 1.05;
targetFlex = 1;
} else if (blockedTestTime < 0.72) {
targetSpeed = -blockedDirection * 0.14;
targetFlex = 1;
} else if (blockedTestTime < 0.92) {
targetSpeed = 0;
targetFlex = 0.35;
} else {
blockedTestTime = -1;
targetSpeed = freeRunning ? allowedSign * 0.52 : 0;
targetFlex = 0;
showToast(
reversed
? "The pawl held. Counter-clockwise motion remains free."
: "The pawl held. Clockwise motion remains free."
);
}
}
wheelSpeed = damp(wheelSpeed, targetSpeed, 7.5, delta);
wheelAngle += wheelSpeed * delta;
wheel.rotation.z = wheelAngle;
pawlFlex = damp(pawlFlex, targetFlex, 14, delta);
const catchY = THREE.MathUtils.lerp(0.18, -0.18, easedMix);
catchBlock.position.y = catchY + pawlFlex * blockedDirection * 0.055;
catchBlock.position.x = -1.03 + pawlFlex * 0.045;
temporaryColor.lerpColors(
originalCatchColor,
reversedCatchColor,
easedMix
);
catchMaterial.color.copy(temporaryColor);
catchMaterial.emissive.setHex(
reversed ? 0x3b061e : 0x3a2100
);
// Fade the green and red arrow sets as the pawl changes.
const pulse =
blockedTestTime >= 0
? 0.28 + Math.max(0, Math.sin(elapsed * 14)) * 0.48
: 0;
greenCW.userData.material.opacity = (1 - easedMix) * 0.88;
greenCCW.userData.material.opacity = easedMix * 0.88;
redCW.userData.material.opacity =
easedMix * (0.72 + pulse);
redCCW.userData.material.opacity =
(1 - easedMix) * (0.72 + pulse);
greenCW.visible = greenCW.userData.material.opacity > 0.01;
greenCCW.visible = greenCCW.userData.material.opacity > 0.01;
redCW.visible = redCW.userData.material.opacity > 0.01;
redCCW.visible = redCCW.userData.material.opacity > 0.01;
positionLabel(
permittedLabel,
reversed ? -1.75 : -1.9,
3.86,
1.05
);
positionLabel(
blockedLabel,
4.33,
1.12,
1.18
);
positionLabel(
catchLabel,
2.45,
catchY + 0.64,
1.15
);
renderer.render(scene, camera);
}
animate();
</script>
</body>
</html>
```