Prompt specification
Build or customize the Three.js 3D scene "Pawl and ratchet tooth study" 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 and ratchet tooth study
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/pawl-and-ratchet-tooth-study-x0293
Scene Description Prompt:
Show a voxel ratchet wheel and spring-loaded pawl, clearly distinguishing permitted and blocked rotation. Create one HTML file using Three.js from cdnjs.
Working Three.js Starter (pawl-and-ratchet-tooth-study-x0293):
```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 Mechanism</title>
<style>
:root {
--bg: #071018;
--panel: rgba(10, 20, 30, 0.88);
--border: rgba(154, 203, 222, 0.2);
--text: #eaf7fb;
--muted: #8ca7b2;
--green: #39e6b3;
--red: #ff5964;
--blue: #72c7ff;
}
* {
box-sizing: border-box;
}
html,
body {
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
background:
radial-gradient(circle at 50% 30%, #102431 0%, #071018 58%, #03080d 100%);
color: var(--text);
font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
"Segoe UI", sans-serif;
}
#scene {
position: fixed;
inset: 0;
width: 100%;
height: 100%;
display: block;
touch-action: none;
cursor: grab;
}
#scene:active {
cursor: grabbing;
}
.brand {
position: fixed;
top: 26px;
left: 28px;
z-index: 5;
pointer-events: none;
}
.eyebrow {
margin-bottom: 8px;
color: var(--blue);
font-size: 11px;
font-weight: 800;
letter-spacing: 0.22em;
text-transform: uppercase;
}
h1 {
margin: 0;
font-size: clamp(25px, 4vw, 44px);
line-height: 0.95;
letter-spacing: -0.045em;
}
.subtitle {
max-width: 430px;
margin-top: 12px;
color: var(--muted);
font-size: 13px;
line-height: 1.55;
}
.status {
position: fixed;
top: 26px;
right: 26px;
z-index: 6;
width: min(340px, calc(100vw - 52px));
padding: 16px 18px;
border: 1px solid var(--border);
border-radius: 14px;
background: var(--panel);
box-shadow: 0 18px 55px rgba(0, 0, 0, 0.25);
backdrop-filter: blur(16px);
transition: border-color 180ms ease, box-shadow 180ms ease;
}
.status.allowed {
border-color: rgba(57, 230, 179, 0.46);
box-shadow: 0 0 28px rgba(57, 230, 179, 0.08);
}
.status.blocked {
border-color: rgba(255, 89, 100, 0.55);
box-shadow: 0 0 28px rgba(255, 89, 100, 0.1);
}
.status-row {
display: flex;
align-items: center;
gap: 10px;
}
.status-dot {
width: 10px;
height: 10px;
flex: 0 0 auto;
border-radius: 50%;
background: var(--green);
box-shadow: 0 0 14px var(--green);
}
.blocked .status-dot {
background: var(--red);
box-shadow: 0 0 14px var(--red);
animation: warning 0.7s ease-in-out infinite alternate;
}
.status-title {
font-size: 12px;
font-weight: 850;
letter-spacing: 0.11em;
}
.allowed .status-title {
color: var(--green);
}
.blocked .status-title {
color: var(--red);
}
.status-detail {
margin-top: 7px;
color: var(--muted);
font-size: 12px;
line-height: 1.45;
}
.controls {
position: fixed;
left: 28px;
bottom: 26px;
z-index: 8;
width: min(520px, calc(100vw - 56px));
padding: 16px;
border: 1px solid var(--border);
border-radius: 16px;
background: var(--panel);
box-shadow: 0 20px 60px rgba(0, 0, 0, 0.32);
backdrop-filter: blur(18px);
}
.mode-row,
.transport {
display: flex;
gap: 9px;
}
.mode-row {
margin-bottom: 11px;
}
button {
min-height: 42px;
border: 1px solid rgba(164, 204, 219, 0.18);
border-radius: 10px;
padding: 0 14px;
color: #d8e9ef;
background: rgba(255, 255, 255, 0.045);
font: inherit;
font-size: 12px;
font-weight: 760;
cursor: pointer;
transition: transform 120ms ease, background 120ms ease, border-color 120ms ease;
}
button:hover {
transform: translateY(-1px);
background: rgba(255, 255, 255, 0.09);
}
button:focus-visible,
input:focus-visible {
outline: 2px solid var(--blue);
outline-offset: 2px;
}
.mode-row button {
flex: 1;
}
#allowedButton.active {
color: #071712;
border-color: var(--green);
background: var(--green);
box-shadow: 0 0 22px rgba(57, 230, 179, 0.2);
}
#blockedButton.active {
color: #210609;
border-color: var(--red);
background: var(--red);
box-shadow: 0 0 22px rgba(255, 89, 100, 0.2);
}
.transport button {
min-height: 36px;
padding-inline: 12px;
}
.speed-row {
display: grid;
grid-template-columns: auto 1fr auto;
align-items: center;
gap: 12px;
margin-top: 13px;
color: var(--muted);
font-size: 11px;
font-weight: 750;
letter-spacing: 0.06em;
text-transform: uppercase;
}
input[type="range"] {
width: 100%;
accent-color: var(--blue);
cursor: pointer;
}
#speedValue {
min-width: 42px;
color: var(--text);
text-align: right;
}
.hint {
position: fixed;
right: 27px;
bottom: 27px;
z-index: 5;
color: rgba(181, 207, 216, 0.65);
font-size: 11px;
line-height: 1.7;
text-align: right;
pointer-events: none;
}
.hint strong {
color: rgba(225, 243, 248, 0.88);
}
@keyframes warning {
from {
opacity: 0.45;
transform: scale(0.85);
}
to {
opacity: 1;
transform: scale(1.1);
}
}
@media (max-width: 720px) {
.brand {
top: 18px;
left: 18px;
}
.subtitle {
display: none;
}
.status {
top: 105px;
right: 18px;
width: 260px;
}
.controls {
left: 14px;
bottom: 14px;
width: calc(100vw - 28px);
padding: 12px;
}
.hint {
display: none;
}
button {
padding-inline: 10px;
}
}
</style>
</head>
<body>
<canvas id="scene" aria-label="Interactive voxel ratchet wheel and spring-loaded pawl"></canvas>
<header class="brand">
<div class="eyebrow">Mechanical interaction study</div>
<h1>Voxel Ratchet</h1>
<p class="subtitle">
A block-built ratchet wheel with a spring-loaded pawl.
Green shows permitted counter-clockwise travel; red shows the locked clockwise direction.
</p>
</header>
<aside id="status" class="status allowed" aria-live="polite">
<div class="status-row">
<span class="status-dot"></span>
<span id="statusTitle" class="status-title">PERMITTED ROTATION</span>
</div>
<div id="statusDetail" class="status-detail">
Counter-clockwise travel is free. The pawl rides over each voxel tooth.
</div>
</aside>
<section class="controls" aria-label="Ratchet controls">
<div class="mode-row">
<button id="allowedButton" class="active" type="button">↺ CCW · ALLOWED</button>
<button id="blockedButton" type="button">↻ CW · BLOCKED</button>
</div>
<div class="transport">
<button id="playButton" type="button">Pause</button>
<button id="resetButton" type="button">Reset motion</button>
<button id="viewButton" type="button">Reset view</button>
</div>
<label class="speed-row" for="speed">
Speed
<input id="speed" type="range" min="0.25" max="2" value="1" step="0.05" />
<output id="speedValue">1.00×</output>
</label>
</section>
<div class="hint">
<strong>Drag</strong> to orbit · <strong>Scroll</strong> to zoom<br />
<strong>Space</strong> pause · <strong>← / →</strong> test direction
</div>
<script type="module">
import * as THREE from "https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js";
// -------------------------------------------------------------------------
// Renderer and scene
// -------------------------------------------------------------------------
const canvas = document.querySelector("#scene");
const renderer = new THREE.WebGLRenderer({
canvas,
antialias: true,
alpha: false
});
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.12;
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x071018);
scene.fog = new THREE.FogExp2(0x071018, 0.028);
const camera = new THREE.PerspectiveCamera(
43,
window.innerWidth / window.innerHeight,
0.1,
80
);
const cameraState = {
theta: 0.54,
phi: 1.16,
radius: 14.4,
target: new THREE.Vector3(0, 0.05, 0)
};
function updateCamera() {
const { theta, phi, radius, target } = cameraState;
const sinPhi = Math.sin(phi);
camera.position.set(
target.x + radius * sinPhi * Math.sin(theta),
target.y + radius * Math.cos(phi),
target.z + radius * sinPhi * Math.cos(theta)
);
camera.lookAt(target);
}
updateCamera();
// Direct, dependency-free camera controls.
let dragging = false;
let lastPointerX = 0;
let lastPointerY = 0;
canvas.addEventListener("pointerdown", (event) => {
dragging = true;
lastPointerX = event.clientX;
lastPointerY = event.clientY;
canvas.setPointerCapture(event.pointerId);
});
canvas.addEventListener("pointermove", (event) => {
if (!dragging) return;
const dx = event.clientX - lastPointerX;
const dy = event.clientY - lastPointerY;
lastPointerX = event.clientX;
lastPointerY = event.clientY;
cameraState.theta -= dx * 0.006;
cameraState.phi = THREE.MathUtils.clamp(
cameraState.phi - dy * 0.005,
0.48,
1.48
);
updateCamera();
});
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();
cameraState.radius = THREE.MathUtils.clamp(
cameraState.radius + event.deltaY * 0.01,
9,
21
);
updateCamera();
},
{ passive: false }
);
// -------------------------------------------------------------------------
// Lighting
// -------------------------------------------------------------------------
const hemisphere = new THREE.HemisphereLight(0xb9e7ff, 0x182018, 2.05);
scene.add(hemisphere);
const keyLight = new THREE.DirectionalLight(0xe8f5ff, 4.2);
keyLight.position.set(5.5, 9, 10);
keyLight.castShadow = true;
keyLight.shadow.mapSize.set(2048, 2048);
keyLight.shadow.camera.left = -9;
keyLight.shadow.camera.right = 9;
keyLight.shadow.camera.top = 9;
keyLight.shadow.camera.bottom = -9;
keyLight.shadow.camera.near = 1;
keyLight.shadow.camera.far = 30;
scene.add(keyLight);
const rimLight = new THREE.DirectionalLight(0x4d9dff, 2.3);
rimLight.position.set(-7, 3, -8);
scene.add(rimLight);
const contactLight = new THREE.PointLight(0x39e6b3, 4, 4.5, 2);
contactLight.position.set(0.4, 2.2, 1.6);
scene.add(contactLight);
// -------------------------------------------------------------------------
// Materials and helpers
// -------------------------------------------------------------------------
const wheelMaterial = new THREE.MeshStandardMaterial({
color: 0x617789,
metalness: 0.72,
roughness: 0.31,
emissive: 0x08775f,
emissiveIntensity: 0.08
});
const hubMaterial = new THREE.MeshStandardMaterial({
color: 0x1b2b38,
metalness: 0.78,
roughness: 0.25
});
const edgeMaterial = new THREE.MeshStandardMaterial({
color: 0x9fc4d2,
metalness: 0.82,
roughness: 0.24
});
const pawlMaterial = new THREE.MeshStandardMaterial({
color: 0xf0ad46,
metalness: 0.58,
roughness: 0.3,
emissive: 0x7f3d00,
emissiveIntensity: 0.08
});
const springMaterial = new THREE.MeshStandardMaterial({
color: 0xffd275,
metalness: 0.8,
roughness: 0.22,
emissive: 0x8b5200,
emissiveIntensity: 0.12
});
const darkMaterial = new THREE.MeshStandardMaterial({
color: 0x101b25,
metalness: 0.52,
roughness: 0.58
});
function makeBox(
width,
height,
depth,
material,
x = 0,
y = 0,
z = 0,
parent = scene,
rotationZ = 0
) {
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(width, height, depth),
material
);
mesh.position.set(x, y, z);
mesh.rotation.z = rotationZ;
mesh.castShadow = true;
mesh.receiveShadow = true;
parent.add(mesh);
return mesh;
}
function makeVoxelPlatform() {
const floor = new THREE.Mesh(
new THREE.PlaneGeometry(40, 40),
new THREE.MeshStandardMaterial({
color: 0x08131b,
roughness: 0.92,
metalness: 0.08
})
);
floor.rotation.x = -Math.PI / 2;
floor.position.y = -3.13;
floor.receiveShadow = true;
scene.add(floor);
const grid = new THREE.GridHelper(28, 28, 0x2e5869, 0x17323f);
grid.position.y = -3.11;
grid.material.transparent = true;
grid.material.opacity = 0.2;
scene.add(grid);
makeBox(8.7, 0.42, 5.4, darkMaterial, 0, -2.92, -0.25);
makeBox(7.8, 0.16, 4.55, hubMaterial, 0, -2.64, -0.25);
// Small voxel details around the platform.
const details = [
[-4.0, -2.48, 1.45, 0.42],
[-3.55, -2.48, 1.78, 0.25],
[3.95, -2.48, 1.45, 0.38],
[3.55, -2.48, 1.72, 0.23],
[-3.7, -2.48, -2.0, 0.28],
[3.65, -2.48, -2.0, 0.3]
];
for (const [x, y, z, size] of details) {
makeBox(size, size, size, edgeMaterial, x, y, z);
}
}
makeVoxelPlatform();
// -------------------------------------------------------------------------
// Voxel ratchet wheel
// -------------------------------------------------------------------------
const wheelGroup = new THREE.Group();
scene.add(wheelGroup);
const toothCount = 14;
// Two pixel-like rings.
for (let i = 0; i < 32; i++) {
const angle = (i / 32) * Math.PI * 2;
const radius = 1.55;
makeBox(
0.5,
0.43,
0.7,
wheelMaterial,
Math.cos(angle) * radius,
Math.sin(angle) * radius,
0,
wheelGroup,
angle
);
}
for (let i = 0; i < 32; i++) {
const angle = ((i + 0.5) / 32) * Math.PI * 2;
const radius = 1.94;
makeBox(
0.39,
0.35,
0.66,
wheelMaterial,
Math.cos(angle) * radius,
Math.sin(angle) * radius,
0,
wheelGroup,
angle
);
}
// Block spokes.
for (let i = 0; i < 8; i++) {
const angle = (i / 8) * Math.PI * 2;
makeBox(
1.45,
0.27,
0.54,
edgeMaterial,
Math.cos(angle) * 0.94,
Math.sin(angle) * 0.94,
-0.03,
wheelGroup,
angle
);
}
// Central square hub and axle.
makeBox(0.95, 0.95, 0.98, hubMaterial, 0, 0, 0, wheelGroup);
makeBox(0.59, 0.59, 1.12, edgeMaterial, 0, 0, 0.12, wheelGroup);
makeBox(0.45, 0.45, 2.25, hubMaterial, 0, 0, -0.88);
// Four small hub bolts.
for (let i = 0; i < 4; i++) {
const angle = Math.PI / 4 + (i / 4) * Math.PI * 2;
makeBox(
0.16,
0.16,
0.15,
springMaterial,
Math.cos(angle) * 0.32,
Math.sin(angle) * 0.32,
0.66,
wheelGroup,
angle
);
}
// Asymmetric stepped teeth: one steep face and one ramp-like face.
for (let i = 0; i < toothCount; i++) {
const angle = (i / toothCount) * Math.PI * 2;
const tooth = new THREE.Group();
tooth.rotation.z = angle;
wheelGroup.add(tooth);
makeBox(
0.63,
0.58,
0.8,
wheelMaterial,
2.08,
0,
0,
tooth
);
makeBox(
0.44,
0.32,
0.8,
wheelMaterial,
2.47,
-0.13,
0,
tooth
);
makeBox(
0.2,
0.32,
0.8,
edgeMaterial,
2.64,
-0.13,
0,
tooth
);
}
// -------------------------------------------------------------------------
// Spring-loaded pawl
// -------------------------------------------------------------------------
const pivotPosition = new THREE.Vector3(1.5, 3.18, 0.78);
const tipTarget = new THREE.Vector3(0.35, 2.23, 0.78);
const baseAngle = Math.atan2(
tipTarget.y - pivotPosition.y,
tipTarget.x - pivotPosition.x
);
const pawlPivot = new THREE.Group();
pawlPivot.position.copy(pivotPosition);
pawlPivot.rotation.z = baseAngle;
scene.add(pawlPivot);
// Stepped block beam extending along local negative X.
for (let i = 0; i < 5; i++) {
makeBox(
0.34,
0.3 - i * 0.012,
0.4,
pawlMaterial,
-(i * 0.31 + 0.16),
0,
0,
pawlPivot
);
}
makeBox(0.34, 0.23, 0.49, edgeMaterial, -1.5, -0.01, 0.02, pawlPivot);
// Fixed hinge bracket and spring anchor.
makeBox(
0.72,
0.72,
0.38,
hubMaterial,
pivotPosition.x,
pivotPosition.y,
0.48
);
makeBox(
0.4,
0.4,
0.26,
edgeMaterial,
pivotPosition.x,
pivotPosition.y,
1.02
);
const springAnchor = new THREE.Vector3(3.55, 3.52, 0.86);
makeBox(0.42, 0.42, 0.48, hubMaterial, 3.55, 3.52, 0.58);
// Dynamic extension spring built from small cylindrical segments.
const springSegments = 36;
const springCylinderGeometry = new THREE.CylinderGeometry(0.026, 0.026, 1, 6);
const springMeshes = [];
const springPoints = [];
for (let i = 0; i < springSegments; i++) {
const mesh = new THREE.Mesh(springCylinderGeometry, springMaterial);
mesh.castShadow = true;
scene.add(mesh);
springMeshes.push(mesh);
}
for (let i = 0; i <= springSegments; i++) {
springPoints.push(new THREE.Vector3());
}
const springAxis = new THREE.Vector3();
const springU = new THREE.Vector3();
const springV = new THREE.Vector3();
const springDirection = new THREE.Vector3();
const springAttach = new THREE.Vector3();
const springMid = new THREE.Vector3();
const yAxis = new THREE.Vector3(0, 1, 0);
const zAxis = new THREE.Vector3(0, 0, 1);
function updateSpring() {
pawlPivot.updateWorldMatrix(true, false);
pawlPivot.localToWorld(springAttach.set(-0.58, 0, 0.2));
springAxis.subVectors(springAttach, springAnchor);
const length = springAxis.length();
springAxis.normalize();
springU.crossVectors(springAxis, zAxis);
if (springU.lengthSq() < 0.0001) {
springU.crossVectors(springAxis, yAxis);
}
springU.normalize();
springV.crossVectors(springAxis, springU).normalize();
const turns = 8;
const coilRadius = 0.13;
for (let i = 0; i <= springSegments; i++) {
const t = i / springSegments;
const angle = t * turns * Math.PI * 2;
const taper = Math.pow(Math.max(0, Math.sin(Math.PI * t)), 0.55);
const radial = coilRadius * taper;
springPoints[i]
.copy(springAnchor)
.addScaledVector(springAxis, length * t)
.addScaledVector(springU, Math.cos(angle) * radial)
.addScaledVector(springV, Math.sin(angle) * radial);
}
for (let i = 0; i < springSegments; i++) {
springDirection.subVectors(springPoints[i + 1], springPoints[i]);
const segmentLength = springDirection.length();
springMid
.copy(springPoints[i])
.add(springPoints[i + 1])
.multiplyScalar(0.5);
springDirection.normalize();
const mesh = springMeshes[i];
mesh.position.copy(springMid);
mesh.quaternion.setFromUnitVectors(yAxis, springDirection);
mesh.scale.set(1, segmentLength, 1);
}
}
// -------------------------------------------------------------------------
// Direction arrows and canvas-generated labels
// -------------------------------------------------------------------------
function createArcArrow(startAngle, endAngle, radius, color) {
const points = [];
const steps = 56;
for (let i = 0; i <= steps; i++) {
const t = i / steps;
const angle = THREE.MathUtils.lerp(startAngle, endAngle, t);
points.push(
new THREE.Vector3(
Math.cos(angle) * radius,
Math.sin(angle) * radius,
0.88
)
);
}
const material = new THREE.MeshStandardMaterial({
color,
metalness: 0.35,
roughness: 0.25,
emissive: color,
emissiveIntensity: 1.25
});
const curve = new THREE.CatmullRomCurve3(points);
const tube = new THREE.Mesh(
new THREE.TubeGeometry(curve, 72, 0.037, 7, false),
material
);
scene.add(tube);
const end = points[points.length - 1];
const previous = points[points.length - 2];
const tangent = end.clone().sub(previous).normalize();
const head = new THREE.Mesh(
new THREE.ConeGeometry(0.145, 0.43, 8),
material
);
head.position.copy(end);
head.quaternion.setFromUnitVectors(yAxis, tangent);
scene.add(head);
}
// Counter-clockwise arc on the left.
createArcArrow(2.18, 3.75, 3.02, 0x39e6b3);
// Clockwise arc on the right.
createArcArrow(0.72, -0.72, 3.02, 0xff5964);
function roundedRect(ctx, x, y, width, height, radius) {
ctx.beginPath();
ctx.moveTo(x + radius, y);
ctx.lineTo(x + width - radius, y);
ctx.quadraticCurveTo(x + width, y, x + width, y + radius);
ctx.lineTo(x + width, y + height - radius);
ctx.quadraticCurveTo(
x + width,
y + height,
x + width - radius,
y + height
);
ctx.lineTo(x + radius, y + height);
ctx.quadraticCurveTo(x, y + height, x, y + height - radius);
ctx.lineTo(x, y + radius);
ctx.quadraticCurveTo(x, y, x + radius, y);
ctx.closePath();
}
function createLabel(title, subtitle, color, position) {
const labelCanvas = document.createElement("canvas");
labelCanvas.width = 512;
labelCanvas.height = 160;
const ctx = labelCanvas.getContext("2d");
const hex = `#${color.toString(16).padStart(6, "0")}`;
roundedRect(ctx, 8, 8, 496, 144, 22);
ctx.fillStyle = "rgba(5, 13, 20, 0.91)";
ctx.fill();
ctx.lineWidth = 5;
ctx.strokeStyle = hex;
ctx.stroke();
ctx.textAlign = "center";
ctx.fillStyle = hex;
ctx.font = "800 45px system-ui, sans-serif";
ctx.fillText(title, 256, 66);
ctx.fillStyle = "#bcd1da";
ctx.font = "700 24px system-ui, sans-serif";
ctx.fillText(subtitle, 256, 113);
const texture = new THREE.CanvasTexture(labelCanvas);
texture.colorSpace = THREE.SRGBColorSpace;
const sprite = new THREE.Sprite(
new THREE.SpriteMaterial({
map: texture,
transparent: true,
depthTest: false,
depthWrite: false
})
);
sprite.position.copy(position);
sprite.scale.set(2.75, 0.86, 1);
sprite.renderOrder = 20;
scene.add(sprite);
}
createLabel(
"ALLOWED",
"COUNTER-CLOCKWISE",
0x39e6b3,
new THREE.Vector3(-3.75, 2.55, 1.1)
);
createLabel(
"BLOCKED",
"CLOCKWISE",
0xff5964,
new THREE.Vector3(3.6, -1.48, 1.1)
);
// -------------------------------------------------------------------------
// Contact marker and voxel particles
// -------------------------------------------------------------------------
const contactHaloMaterial = new THREE.MeshBasicMaterial({
color: 0x39e6b3,
transparent: true,
opacity: 0.9,
depthTest: false
});
const contactHalo = new THREE.Mesh(
new THREE.TorusGeometry(0.19, 0.035, 6, 18),
contactHaloMaterial
);
contactHalo.renderOrder = 15;
scene.add(contactHalo);
const contactCore = new THREE.Mesh(
new THREE.BoxGeometry(0.13, 0.13, 0.13),
new THREE.MeshBasicMaterial({ color: 0x39e6b3 })
);
contactCore.rotation.z = Math.PI / 4;
contactCore.renderOrder = 16;
scene.add(contactCore);
const particleGeometry = new THREE.BoxGeometry(0.075, 0.075, 0.075);
const particleMaterial = new THREE.MeshBasicMaterial({ color: 0x39e6b3 });
const particles = [];
for (let i = 0; i < 14; i++) {
const mesh = new THREE.Mesh(particleGeometry, particleMaterial);
mesh.visible = false;
scene.add(mesh);
particles.push({
mesh,
velocity: new THREE.Vector3(),
life: 0
});
}
function burstParticles(position, color) {
particleMaterial.color.setHex(color);
for (let i = 0; i < particles.length; i++) {
const particle = particles[i];
const angle = (i / particles.length) * Math.PI * 2 + Math.random() * 0.35;
const speed = 0.55 + Math.random() * 1.25;
particle.mesh.position.copy(position);
particle.mesh.position.z += Math.random() * 0.16;
particle.mesh.scale.setScalar(1);
particle.mesh.visible = true;
particle.velocity.set(
Math.cos(angle) * speed,
Math.sin(angle) * speed + 0.25,
(Math.random() - 0.5) * 0.6
);
particle.life = 0.45 + Math.random() * 0.28;
}
}
function updateParticles(dt) {
for (const particle of particles) {
if (particle.life <= 0) continue;
particle.life -= dt;
particle.mesh.position.addScaledVector(particle.velocity, dt);
particle.velocity.y -= 2.4 * dt;
particle.mesh.rotation.x += dt * 5;
particle.mesh.rotation.y += dt * 7;
particle.mesh.scale.setScalar(Math.max(0.01, particle.life * 2.5));
if (particle.life <= 0) {
particle.mesh.visible = false;
}
}
}
// -------------------------------------------------------------------------
// UI and simulation state
// -------------------------------------------------------------------------
const allowedButton = document.querySelector("#allowedButton");
const blockedButton = document.querySelector("#blockedButton");
const playButton = document.querySelector("#playButton");
const resetButton = document.querySelector("#resetButton");
const viewButton = document.querySelector("#viewButton");
const speedInput = document.querySelector("#speed");
const speedValue = document.querySelector("#speedValue");
const statusElement = document.querySelector("#status");
const statusTitle = document.querySelector("#statusTitle");
const statusDetail = document.querySelector("#statusDetail");
let mode = "allowed";
let playing = true;
let speed = 1;
let wheelAngle = 0;
let blockedAnchor = 0;
let attemptTime = 0;
let previousBump = 0;
let blockedBurstTimer = 0;
function updateStatusUI() {
const isAllowed = mode === "allowed";
statusElement.className = `status ${mode}`;
allowedButton.classList.toggle("active", isAllowed);
blockedButton.classList.toggle("active", !isAllowed);
if (isAllowed) {
statusTitle.textContent = "PERMITTED ROTATION";
statusDetail.textContent =
"Counter-clockwise travel is free. The pawl rides over each voxel tooth.";
} else {
statusTitle.textContent = "ROTATION BLOCKED";
statusDetail.textContent =
"Clockwise travel is locked. The pawl catches the steep tooth face while the spring holds the load.";
}
}
function setMode(nextMode) {
mode = nextMode;
if (mode === "blocked") {
blockedAnchor = wheelAngle;
attemptTime = 0;
blockedBurstTimer = 0;
}
previousBump = 0;
updateStatusUI();
}
function togglePlay() {
playing = !playing;
playButton.textContent = playing ? "Pause" : "Play";
}
allowedButton.addEventListener("click", () => setMode("allowed"));
blockedButton.addEventListener("click", () => setMode("blocked"));
playButton.addEventListener("click", togglePlay);
resetButton.addEventListener("click", () => {
wheelAngle = 0;
blockedAnchor = 0;
attemptTime = 0;
previousBump = 0;
setMode("allowed");
});
viewButton.addEventListener("click", () => {
cameraState.theta = 0.54;
cameraState.phi = 1.16;
cameraState.radius = 14.4;
updateCamera();
});
speedInput.addEventListener("input", () => {
speed = Number(speedInput.value);
speedValue.value = `${speed.toFixed(2)}×`;
});
window.addEventListener("keydown", (event) => {
if (event.target instanceof HTMLInputElement) return;
if (event.code === "Space") {
event.preventDefault();
togglePlay();
} else if (event.code === "ArrowLeft") {
setMode("allowed");
} else if (event.code === "ArrowRight") {
setMode("blocked");
} else if (event.code === "KeyR") {
resetButton.click();
}
});
updateStatusUI();
// -------------------------------------------------------------------------
// Animation
// -------------------------------------------------------------------------
function clamp01(value) {
return Math.max(0, Math.min(1, value));
}
function smooth01(value) {
const t = clamp01(value);
return t * t * (3 - 2 * t);
}
function toothBump(phase) {
const rise = smooth01((phase - 0.05) / 0.22);
const fall = 1 - smooth01((phase - 0.32) / 0.28);
return rise * fall;
}
const clock = new THREE.Clock();
const tipWorld = new THREE.Vector3();
let elapsed = 0;
function animate() {
requestAnimationFrame(animate);
const dt = Math.min(clock.getDelta(), 0.04);
elapsed += dt;
const allowed = mode === "allowed";
let bump = 0;
if (playing) {
if (allowed) {
// Positive Z rotation is counter-clockwise from the camera's view.
wheelAngle += dt * 1.15 * speed;
} else {
// The attempted reverse movement compresses into a small recoil.
attemptTime += dt * speed;
const compression = Math.min(1, attemptTime / 0.18);
const settle = Math.exp(-attemptTime * 2.2);
wheelAngle =
blockedAnchor -
0.065 * compression +
0.014 * Math.sin(attemptTime * 24) * settle * compression;
}
}
wheelGroup.rotation.z = wheelAngle;
if (allowed) {
const toothStep = (Math.PI * 2) / toothCount;
const phase = ((wheelAngle % toothStep) + toothStep) % toothStep / toothStep;
bump = toothBump(phase);
if (playing && bump > 0.88 && previousBump <= 0.88) {
pawlPivot.updateWorldMatrix(true, false);
pawlPivot.localToWorld(tipWorld.set(-1.54, 0, 0.28));
burstParticles(tipWorld, 0x39e6b3);
}
} else {
blockedBurstTimer -= dt;
if (playing && blockedBurstTimer <= 0) {
pawlPivot.updateWorldMatrix(true, false);
pawlPivot.localToWorld(tipWorld.set(-1.54, 0, 0.28));
burstParticles(tipWorld, 0xff5964);
blockedBurstTimer = 0.48;
}
}
previousBump = bump;
// The pawl rotates over the tooth in the free direction, or flexes
// against the catch when reverse motion is attempted.
if (allowed) {
pawlPivot.rotation.z = baseAngle - 0.14 * bump;
} else {
pawlPivot.rotation.z =
baseAngle + 0.028 + 0.012 * Math.sin(attemptTime * 24);
}
pawlPivot.updateWorldMatrix(true, false);
pawlPivot.localToWorld(tipWorld.set(-1.54, 0, 0.28));
contactHalo.position.copy(tipWorld);
contactCore.position.copy(tipWorld);
const haloPulse = allowed
? 1 + bump * 0.5
: 1.05 + Math.abs(Math.sin(attemptTime * 24)) * 0.28;
contactHalo.scale.setScalar(haloPulse);
contactHalo.rotation.z += dt * (allowed ? 2.5 : -3.5);
if (allowed) {
wheelMaterial.emissive.setHex(0x08775f);
wheelMaterial.emissiveIntensity = 0.08 + bump * 0.14;
pawlMaterial.emissive.setHex(0x7f3d00);
pawlMaterial.emissiveIntensity = 0.08;
contactHaloMaterial.color.setHex(0x39e6b3);
contactCore.material.color.setHex(0x39e6b3);
contactLight.color.setHex(0x39e6b3);
contactLight.intensity = 2.5 + bump * 5;
} else {
wheelMaterial.emissive.setHex(0xa7192b);
wheelMaterial.emissiveIntensity = 0.18;
pawlMaterial.emissive.setHex(0xb31320);
pawlMaterial.emissiveIntensity = 0.23;
contactHaloMaterial.color.setHex(0xff5964);
contactCore.material.color.setHex(0xff5964);
contactLight.color.setHex(0xff5964);
contactLight.intensity = 3.5 + Math.abs(Math.sin(attemptTime * 24)) * 4;
}
updateSpring();
updateParticles(dt);
renderer.render(scene, camera);
}
animate();
// -------------------------------------------------------------------------
// Resize handling
// -------------------------------------------------------------------------
window.addEventListener("resize", () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.setSize(window.innerWidth, window.innerHeight);
});
</script>
</body>
</html>
```