Prompt specification
Build or customize the Three.js 3D scene "Escapement tooth and alternating pallet pair" 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: Escapement tooth and alternating pallet pair
Category: Art & Culture · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/escapement-tooth-and-alternating-pallet-pair-x2501
Scene Description Prompt:
Create a single self-contained HTML file using Three.js from cdnjs that shows a mechanical clock escapement. Build a simplified toothed wheel beside two pallet faces. Highlight the contact point that stops rotation and the small release angle before the next tooth engages. Stylized solid forms.
Working Three.js Starter (escapement-tooth-and-alternating-pallet-pair-x2501):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<title>Clock Escapement — Three.js</title>
<style>
:root{--mint:#6ff0c6;--amber:#ffb454;--ink:#e9eff7;--dim:#8ea3ba}
*{box-sizing:border-box}
html,body{margin:0;height:100%;background:#0a0f16;overflow:hidden;
font-family:ui-sans-serif,system-ui,-apple-system,"Segoe UI",Roboto,Helvetica,Arial,sans-serif;color:var(--ink)}
canvas{display:block;width:100%;height:100%}
.panel{position:fixed;top:18px;left:18px;width:296px;padding:15px 17px;border-radius:14px;
background:rgba(11,17,25,.74);border:1px solid rgba(255,255,255,.09);
box-shadow:0 14px 40px rgba(0,0,0,.45);backdrop-filter:blur(9px)}
h1{margin:0;font-size:14px;letter-spacing:.18em;text-transform:uppercase;font-weight:600}
.sub{margin:5px 0 12px;font-size:11px;color:var(--dim);letter-spacing:.05em}
.row{display:flex;align-items:center;gap:9px;font-size:12px;margin:6px 0;color:#b7c5d6}
.dot{width:9px;height:9px;border-radius:50%;flex:0 0 auto;box-shadow:0 0 10px currentColor}
.state{margin-top:11px;padding-top:10px;border-top:1px solid rgba(255,255,255,.08);
font-size:12px;line-height:1.45;color:#cfdae7;min-height:34px}
.bar{margin-top:9px;height:3px;border-radius:3px;background:rgba(255,255,255,.10);overflow:hidden}
.bar i{display:block;height:100%;width:0;background:var(--mint);transition:background .2s}
.ctl{position:fixed;right:18px;bottom:16px;display:flex;gap:10px;align-items:center;
padding:9px 14px;border-radius:12px;background:rgba(11,17,25,.72);border:1px solid rgba(255,255,255,.09);
font-size:11px;letter-spacing:.12em;text-transform:uppercase;color:var(--dim)}
.ctl input{width:96px;accent-color:var(--mint)}
.lab{position:fixed;left:0;top:0;transform:translate(-50%,-50%);font-size:10.5px;letter-spacing:.11em;
text-transform:uppercase;padding:4px 9px;border-radius:999px;white-space:nowrap;pointer-events:none;
background:rgba(9,14,21,.80);border:1px solid rgba(255,255,255,.15);opacity:0;transition:opacity .22s}
.lab.mint{color:var(--mint);border-color:rgba(111,240,198,.45);box-shadow:0 0 18px rgba(111,240,198,.18)}
.lab.amber{color:var(--amber);border-color:rgba(255,180,84,.45);box-shadow:0 0 18px rgba(255,180,84,.16)}
.lab b{font-weight:600}
</style>
</head>
<body>
<canvas id="c"></canvas>
<div class="panel">
<h1>Escapement</h1>
<p class="sub">simplified anchor escapement · 12‑tooth escape wheel · two pallet faces</p>
<div class="row"><i class="dot" style="color:#6ff0c6;background:#6ff0c6"></i>Contact — tooth tip held on pallet face</div>
<div class="row"><i class="dot" style="color:#ffb454;background:#ffb454"></i>Release angle — 5° before the tooth escapes</div>
<div class="row"><i class="dot" style="color:#7fa8ff;background:#7fa8ff"></i>Drop — free flight to the opposite pallet</div>
<div class="state" id="state">initialising…</div>
<div class="bar"><i id="bar"></i></div>
</div>
<div class="ctl"><span>speed</span><input id="spd" type="range" min="20" max="180" value="100"></div>
<div class="lab mint" id="l1"></div>
<div class="lab amber" id="l2"></div>
<div class="lab" id="l3" style="color:#9dc0ff;border-color:rgba(157,192,255,.4)"></div>
<script type="module">
import * as THREE from 'https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js';
/* ───────────────────────── geometry constants ───────────────────────── */
const DEG = Math.PI/180;
const N = 12, PITCH = 30*DEG; // teeth / tooth pitch
const Rt = 2.05, Rr = 1.62; // tip radius / root radius
const PIVOT = {x:0, y:3.35}; // anchor pivot
const LAMBDA = 15*DEG; // pallet impulse-face lift angle
const RELEASE = 5*DEG; // ← the highlighted small release angle
const ENG = {L:120*DEG, R:60*DEG}; // where each pallet meets the rim
const B_LOCK = {L:-1.2*DEG, R:1.2*DEG}; // anchor angle at each lock
const FACE_LEN = 0.19, FACE_TH = 0.26; // pallet face length / thickness
const rot2=(v,a)=>{const c=Math.cos(a),s=Math.sin(a);return{x:v.x*c-v.y*s,y:v.x*s+v.y*c};};
const polar=(r,a)=>({x:r*Math.cos(a),y:r*Math.sin(a)});
/* ───────────────────────── renderer / scene ───────────────────────── */
const canvas = document.getElementById('c');
const renderer = new THREE.WebGLRenderer({canvas,antialias:true});
renderer.setPixelRatio(Math.min(devicePixelRatio,2));
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.06;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x0a0f16);
scene.fog = new THREE.Fog(0x0a0f16, 16, 30);
const camera = new THREE.PerspectiveCamera(40,1,0.1,100);
camera.position.set(0.85,2.15,12.2);
camera.lookAt(0,1.95,0);
scene.add(new THREE.HemisphereLight(0x9db6d6,0x1a1410,0.55));
const key = new THREE.DirectionalLight(0xfff1d6,1.55);
key.position.set(-5.5,7.5,8.5); key.castShadow=true;
key.shadow.mapSize.set(2048,2048);
Object.assign(key.shadow.camera,{left:-7,right:7,top:8,bottom:-5,near:1,far:30});
key.shadow.bias=-0.0012; key.shadow.normalBias=0.02;
scene.add(key);
const fill = new THREE.DirectionalLight(0x86b0ff,0.55); fill.position.set(6,1.5,5); scene.add(fill);
const rim = new THREE.DirectionalLight(0xffb060,0.35); rim.position.set(0,-6,3); scene.add(rim);
const matBrass = new THREE.MeshStandardMaterial({color:0xc9973f,metalness:.88,roughness:.30,flatShading:true});
const matSteel = new THREE.MeshStandardMaterial({color:0x445266,metalness:.82,roughness:.34,flatShading:true});
const matFace = new THREE.MeshStandardMaterial({color:0xc7d3e0,metalness:.95,roughness:.16,flatShading:true});
const matPlate = new THREE.MeshStandardMaterial({color:0x18212c,metalness:.25,roughness:.78,flatShading:true});
const matPlate2= new THREE.MeshStandardMaterial({color:0x101820,metalness:.20,roughness:.85,flatShading:true});
const matDark = new THREE.MeshStandardMaterial({color:0x2b3644,metalness:.60,roughness:.45,flatShading:true});
/* ───────────────────────── back plate ───────────────────────── */
function roundedRect(w,h,r){
const s=new THREE.Shape(),x=-w/2,y=-h/2;
s.moveTo(x+r,y); s.lineTo(x+w-r,y); s.quadraticCurveTo(x+w,y,x+w,y+r);
s.lineTo(x+w,y+h-r); s.quadraticCurveTo(x+w,y+h,x+w-r,y+h);
s.lineTo(x+r,y+h); s.quadraticCurveTo(x,y+h,x,y+h-r);
s.lineTo(x,y+r); s.quadraticCurveTo(x,y,x+r,y); return s;
}
const plate = new THREE.Mesh(new THREE.ExtrudeGeometry(roundedRect(9.4,9.6,0.7),{depth:.55,bevelEnabled:true,bevelSize:.05,bevelThickness:.05,bevelSegments:2}),matPlate);
plate.position.set(0,1.9,-1.35); plate.receiveShadow=true; scene.add(plate);
const recess = new THREE.Mesh(new THREE.CylinderGeometry(2.62,2.62,.22,72),matPlate2);
recess.rotation.x=Math.PI/2; recess.position.set(0,0,-0.86); recess.receiveShadow=true; scene.add(recess);
const bezel = new THREE.Mesh(new THREE.TorusGeometry(2.64,.055,10,90),matDark);
bezel.position.set(0,0,-0.72); scene.add(bezel);
for(const [sx,sy] of [[-3.9,-2.3],[3.9,-2.3],[-3.9,6.1],[3.9,6.1]]){
const s=new THREE.Mesh(new THREE.CylinderGeometry(.20,.20,.14,20),matDark);
s.rotation.x=Math.PI/2; s.position.set(sx,sy,-0.78); scene.add(s);
}
/* ───────────────────────── escape wheel ───────────────────────── */
function buildWheelShape(){
const LEAD=4*DEG, BACK=20*DEG, s=new THREE.Shape();
s.moveTo(Rr*Math.cos(-LEAD), Rr*Math.sin(-LEAD));
for(let i=0;i<N;i++){
const psi=i*PITCH, tip=polar(Rt,psi), ctrl=polar(1.87,psi+9*DEG), back=polar(Rr,psi+BACK);
s.lineTo(tip.x,tip.y); // leading flank
s.quadraticCurveTo(ctrl.x,ctrl.y,back.x,back.y); // swept back
s.absarc(0,0,Rr,psi+BACK,psi+PITCH-LEAD,false); // root arc
}
s.closePath();
for(let i=0;i<5;i++){ // lightening holes
const a=i*2*Math.PI/5+0.35, h=new THREE.Path();
h.absarc(1.02*Math.cos(a),1.02*Math.sin(a),.36,0,Math.PI*2,true); s.holes.push(h);
}
return s;
}
const wheel = new THREE.Group(); scene.add(wheel);
const wheelBody = new THREE.Mesh(new THREE.ExtrudeGeometry(buildWheelShape(),
{depth:.30,bevelEnabled:true,bevelSize:.014,bevelThickness:.014,bevelSegments:2}),matBrass);
wheelBody.geometry.translate(0,0,-.15); wheelBody.castShadow=true; wheel.add(wheelBody);
const hub = new THREE.Mesh(new THREE.CylinderGeometry(.46,.46,.46,36),matBrass);
hub.rotation.x=Math.PI/2; hub.position.z=.06; hub.castShadow=true; wheel.add(hub);
const arbor = new THREE.Mesh(new THREE.CylinderGeometry(.15,.15,.9,20),matDark);
arbor.rotation.x=Math.PI/2; arbor.position.z=-.30; wheel.add(arbor);
const collar = new THREE.Mesh(new THREE.CylinderGeometry(.26,.26,.10,24),matDark);
collar.rotation.x=Math.PI/2; collar.position.z=.34; wheel.add(collar);
/* ───────────────────────── anchor + pallet faces ───────────────────────── */
// world-space face geometry at each lock state
function faceWorld(pallet){
const a=ENG[pallet], t={x:Math.sin(a),y:-Math.cos(a)}; // clockwise tangent
const u=pallet==='L'? rot2(t,-LAMBDA) : {x:-rot2(t,-LAMBDA).x,y:rot2(t,-LAMBDA).y};
const n=rot2(u,Math.PI/2); // outward normal
const C=polar(Rt,a);
return {C,u,n};
}
const FW={L:faceWorld('L'),R:faceWorld('R')};
// local (anchor frame) copies — the anchor is rigid, so each pallet's line is fixed locally
const FACE_LOCAL={};
for(const k of ['L','R']){
const {C,u,n}=FW[k], b=B_LOCK[k];
const A=rot2({x:C.x-PIVOT.x,y:C.y-PIVOT.y},-b);
FACE_LOCAL[k]={A,d:rot2(u,-b),n:rot2(n,-b)};
}
const anchor=new THREE.Group(); anchor.position.set(PIVOT.x,PIVOT.y,0); scene.add(anchor);
function armShape(p0,p1,w0,w1){
const dx=p1.x-p0.x,dy=p1.y-p0.y,L=Math.hypot(dx,dy),ux=dx/L,uy=dy/L,px=-uy,py=ux;
const A={x:p0.x+px*w0/2,y:p0.y+py*w0/2},B={x:p1.x+px*w1/2,y:p1.y+py*w1/2};
const C={x:p1.x-px*w1/2,y:p1.y-py*w1/2},D={x:p0.x-px*w0/2,y:p0.y-py*w0/2};
const m1={x:(p0.x+p1.x)/2+px*(w0+w1)*.42,y:(p0.y+p1.y)/2+py*(w0+w1)*.42};
const m2={x:(p0.x+p1.x)/2-px*(w0+w1)*.42,y:(p0.y+p1.y)/2-py*(w0+w1)*.42};
const s=new THREE.Shape();
s.moveTo(A.x,A.y); s.quadraticCurveTo(m1.x,m1.y,B.x,B.y);
s.lineTo(C.x,C.y); s.quadraticCurveTo(m2.x,m2.y,D.x,D.y); s.closePath(); return s;
}
function boxFromShape(shape,depth,mat,bevel=.012){
const g=new THREE.ExtrudeGeometry(shape,{depth,bevelEnabled:true,bevelSize:bevel,bevelThickness:bevel,bevelSegments:1});
g.translate(0,0,-depth/2); return new THREE.Mesh(g,mat);
}
function blockGeo(len,th,depth,round){
const s=new THREE.Shape();
const x=-len/2,y=-th/2;
s.moveTo(x+round,y); s.lineTo(x+len-round,y); s.quadraticCurveTo(x+len,y,x+len,y+round);
s.lineTo(x+len,y+th-round); s.quadraticCurveTo(x+len,y+th,x+len-round,y+th);
s.lineTo(x+round,y+th); s.quadraticCurveTo(x,y+th,x,y+th-round);
s.lineTo(x,y+round); s.quadraticCurveTo(x,y,x+round,y); return s;
}
for(const k of ['L','R']){
const {A,d,n}=FACE_LOCAL[k];
const mid={x:A.x+FACE_LEN/2*d.x,y:A.y+FACE_LEN/2*d.y};
const ctr={x:mid.x+(FACE_TH/2)*n.x, y:mid.y+(FACE_TH/2)*n.y};
// arm
const arm=new THREE.Mesh(new THREE.ExtrudeGeometry(armShape({x:0,y:0},ctr,.50,.30),
{depth:.30,bevelEnabled:true,bevelSize:.015,bevelThickness:.015,bevelSegments:2}),matSteel);
arm.geometry.translate(0,0,-.15); arm.castShadow=true; anchor.add(arm);
// pallet body
const body=boxFromShape(blockGeo(FACE_LEN+.10,FACE_TH,.34,.035),.34,matSteel,.014);
body.position.set(ctr.x,ctr.y,0); body.rotation.z=Math.atan2(d.y,d.x); body.castShadow=true; anchor.add(body);
// polished pallet face (the surface that actually stops the wheel)
const face=boxFromShape(blockGeo(FACE_LEN+.05,.055,.365,.02),.365,matFace,.008);
face.position.set(mid.x+n.x*.028, mid.y+n.y*.028,0); face.rotation.z=Math.atan2(d.y,d.x); anchor.add(face);
}
// hub + crutch + pendulum
const aHub=new THREE.Mesh(new THREE.CylinderGeometry(.42,.42,.38,36),matSteel);
aHub.rotation.x=Math.PI/2; aHub.castShadow=true; anchor.add(aHub);
const aCap=new THREE.Mesh(new THREE.CylinderGeometry(.19,.19,.52,24),matBrass);
aCap.rotation.x=Math.PI/2; aCap.position.z=.02; anchor.add(aCap);
const rod=new THREE.Mesh(new THREE.BoxGeometry(.085,1.95,.14),matDark);
rod.position.y=1.30; rod.castShadow=true; anchor.add(rod);
const bob=new THREE.Mesh(new THREE.CylinderGeometry(.50,.50,.24,44),matBrass);
bob.rotation.x=Math.PI/2; bob.position.y=2.30; bob.castShadow=true; anchor.add(bob);
const bobRing=new THREE.Mesh(new THREE.TorusGeometry(.50,.045,8,44),matDark);
bobRing.position.y=2.30; anchor.add(bobRing);
/* ───────────────────────── highlights ───────────────────────── */
const contactMat=new THREE.MeshBasicMaterial({color:0x8cffd8,transparent:true,opacity:1});
const contact=new THREE.Mesh(new THREE.SphereGeometry(.085,20,20),contactMat);
contact.position.z=.26; scene.add(contact);
const halo=new THREE.Mesh(new THREE.RingGeometry(.14,.19,36),
new THREE.MeshBasicMaterial({color:0x6ff0c6,transparent:true,opacity:.55,side:THREE.DoubleSide}));
halo.position.z=.265; scene.add(halo);
const glowLight=new THREE.PointLight(0x6ff0c6,2.4,3.2,2); scene.add(glowLight);
const nextMat=new THREE.MeshBasicMaterial({color:0xffb454,transparent:true,opacity:0});
const nextMark=new THREE.Mesh(new THREE.SphereGeometry(.075,18,18),nextMat);
nextMark.position.z=.26; scene.add(nextMark);
function makeArc(thetaStartDeg){
const g=new THREE.RingGeometry(Rt-.05,Rt+.30,40,1,thetaStartDeg*DEG,RELEASE);
const m=new THREE.Mesh(g,new THREE.MeshBasicMaterial({color:0xffb454,transparent:true,opacity:0,side:THREE.DoubleSide}));
m.position.z=.25; scene.add(m); return m;
}
const arcL=makeArc(120-RELEASE/DEG), arcR=makeArc(60-RELEASE/DEG);
// ghost of the tooth at the escape (release) position
function ghostMark(){
const s=new THREE.Shape(); s.moveTo(0,.16); s.lineTo(-.085,-.07); s.lineTo(.085,-.07); s.closePath();
const m=new THREE.Mesh(new THREE.ExtrudeGeometry(s,{depth:.03,bevelEnabled:false}),
new THREE.MeshBasicMaterial({color:0xffb454,transparent:true,opacity:0}));
m.position.z=.24; return m;
}
const ghostL=ghostMark(), ghostR=ghostMark(); scene.add(ghostL,ghostR);
/* ───────────────────────── kinematics ───────────────────────── */
// signed distance of a world point from the (rotated) pallet face line
function faceErr(b,L,T){
const A=rot2(L.A,b), d=rot2(L.d,b);
const Qx=PIVOT.x+A.x, Qy=PIVOT.y+A.y;
return (T.x-Qx)*d.y-(T.y-Qy)*d.x;
}
function solveAnchor(T,L,b0){ // find the anchor angle that keeps T on the face
const h=1e-4; let b=b0;
for(let i=0;i<14;i++){
const f=faceErr(b,L,T);
if(Math.abs(f)<1e-7) break;
const der=(faceErr(b+h,L,T)-faceErr(b-h,L,T))/(2*h);
if(Math.abs(der)<1e-8) break;
let nb=b-f/der;
if(!isFinite(nb)||Math.abs(nb-b0)>0.16) nb=b0+Math.sign(nb-b0)*0.16;
b=nb;
}
return b;
}
function facePoint(b,L,T){ // closest point of the face line to T
const A=rot2(L.A,b), d=rot2(L.d,b);
const Qx=PIVOT.x+A.x, Qy=PIVOT.y+A.y;
const t=(T.x-Qx)*d.x+(T.y-Qy)*d.y;
return {x:Qx+t*d.x, y:Qy+t*d.y};
}
/* ───────────────────────── escapement state machine ───────────────────────── */
const HOLD=1.25, IMP=0.55, DROP=0.75, BEATLEN=HOLD+IMP+DROP;
const smooth=p=>p*p*(3-2*p);
const stateEl=document.getElementById('state'), barEl=document.getElementById('bar');
const l1=document.getElementById('l1'), l2=document.getElementById('l2'), l3=document.getElementById('l3');
function stateAt(t){
const n=Math.floor(t/BEATLEN), p=t-n*BEATLEN;
const pallet=(n%2===0)?'L':'R', next=(pallet==='L')?'R':'L';
const w0=(120-30*n)*DEG;
let w,b,phase,cAng,inAng=null;
if(p<HOLD){
phase='hold'; w=w0; b=B_LOCK[pallet]+0.16*DEG*Math.sin(t*5.5); cAng=ENG[pallet];
}else if(p<HOLD+IMP){
phase='impulse';
const e=smooth((p-HOLD)/IMP);
w=w0-RELEASE*e; cAng=ENG[pallet]-RELEASE*e;
b=solveAnchor(polar(Rt,cAng),FACE_LOCAL[pallet],B_LOCK[pallet]);
}else{
phase='drop';
const e=smooth((p-HOLD-IMP)/DROP);
w=(w0-RELEASE)-(PITCH-RELEASE)*e;
inAng=ENG[next]+(PITCH-RELEASE)*(1-e);
const bEnd=solveAnchor(polar(Rt,ENG[pallet]-RELEASE),FACE_LOCAL[pallet],B_LOCK[pallet]);
b=bEnd+(B_LOCK[next]-bEnd)*e;
cAng=ENG[pallet]-RELEASE-(PITCH-RELEASE)*e;
}
return {phase,pallet,next,w,b,cAng,inAng,n,p};
}
/* ───────────────────────── HUD ───────────────────────── */
function place(el,v3,dx,dy){
const p=v3.clone().project(camera);
el.style.left=((p.x*.5+.5)*innerWidth+dx)+'px';
el.style.top =((-p.y*.5+.5)*innerHeight+dy)+'px';
}
const tmpV=new THREE.Vector3();
function updateHUD(st,cpt){
l1.style.opacity = st.phase === 'drop' ? 0 : 1;
l2.style.opacity = st.phase === 'drop' ? 0 : 1;
l3.style.opacity = st.phase === 'drop' ? 1 : 0;
if(st.phase==='hold'){
l1.className='lab mint'; l1.innerHTML='<b>contact</b> · wheel held';
l2.className='lab amber'; l2.innerHTML='<b>release</b> 5°';
place(l1,tmpV.set(cpt.x,cpt.y,.26),58,-26);
const mid=(st.pallet==='L'?ENG.L:ENG.R)-RELEASE/2;
const a=polar(Rt+.36,mid);
place(l2,tmpV.set(a.x,a.y,.25),54,-6);
l3.style.opacity=0;
stateEl.innerHTML='<b style="color:#6ff0c6">LOCKED</b> — a tooth tip rests on the pallet face; the escape wheel cannot turn.';
barEl.style.background='#6ff0c6';
}else if(st.phase==='impulse'){
l1.className='lab mint'; l1.innerHTML='<b>sliding contact</b>';
l2.className='lab amber'; l2.innerHTML='<b>release angle</b> 5°';
place(l1,tmpV.set(cpt.x,cpt.y,.26),64,-24);
const mid=(st.pallet==='L'?ENG.L:ENG.R)-RELEASE/2;
const a=polar(Rt+.36,mid);
place(l2,tmpV.set(a.x,a.y,.25),56,-4);
l3.style.opacity=0;
stateEl.innerHTML='<b style="color:#ffb454">RELEASE</b> — the tooth slides along the pallet face through 5° of travel before it clears the corner.';
barEl.style.background='#ffb454';
}else{
l1.style.opacity=0; l2.style.opacity=0;
l3.innerHTML='<b>next tooth</b> → pallet';
place(l3,tmpV.set(polar(Rt,st.inAng).x,polar(Rt,st.inAng).y,.26),56,-24);
stateEl.innerHTML='<b style="color:#9dc0ff">DROP</b> — the wheel is free; the next tooth flies to the opposite pallet face.';
barEl.style.background='#9dc0ff';
}
}
/* ───────────────────────── main loop ───────────────────────── */
let time=0, speed=1;
document.getElementById('spd').addEventListener('input',e=>speed=+e.target.value/100);
function resize(){
const w=innerWidth,h=innerHeight;
renderer.setSize(w,h,false); camera.aspect=w/h; camera.updateProjectionMatrix();
}
addEventListener('resize',resize); resize();
const clock=new THREE.Clock();
function animate(){
const dt=Math.min(clock.getDelta(),.05);
time+=dt*speed;
const st=stateAt(time);
wheel.rotation.z=st.w;
anchor.rotation.z=st.b;
// contact point on the pallet face
const T=polar(Rt,st.cAng);
const cpt=facePoint(st.b,FACE_LOCAL[st.pallet],T);
contact.position.set(cpt.x,cpt.y,.26);
halo.position.set(cpt.x,cpt.y,.265);
const pulse=1+.14*Math.sin(time*7);
halo.scale.setScalar(pulse);
glowLight.position.set(cpt.x,cpt.y,.55);
glowLight.intensity=st.phase==='drop'?0:2.2+0.6*Math.sin(time*7);
// release-angle arcs + ghost tooth
const arc=st.pallet==='L'?arcL:arcR, arcO=st.pallet==='L'?arcR:arcL;
const gho=st.pallet==='L'?ghostL:ghostR, ghoO=st.pallet==='L'?ghostR:ghostL;
arc.material.opacity=st.phase==='impulse'?.95:(st.phase==='hold'?.30:0);
arcO.material.opacity=0;
const relA=(st.pallet==='L'?ENG.L:ENG.R)-RELEASE;
const gp=polar(Rt+.10,relA);
gho.position.set(gp.x,gp.y,.24); gho.rotation.z=relA-Math.PI/2;
gho.material.opacity=st.phase==='hold'?.55:(st.phase==='impulse'?.95:0);
ghoO.material.opacity=0;
// incoming-tooth marker during the drop
if(st.inAng!==null){
const p=polar(Rt,st.inAng);
nextMark.position.set(p.x,p.y,.26); nextMark.material.opacity=Math.min(1,(st.p- (HOLD+IMP))/0.12);
}else nextMark.material.opacity=0;
contactMat.opacity=st.phase==='drop'?Math.max(0,1-(st.p-(HOLD+IMP))/0.18):1;
halo.material.opacity=st.phase==='drop'?0:0.55;
updateHUD(st,cpt);
barEl.style.width=((st.p/BEATLEN)*100).toFixed(1)+'%';
renderer.render(scene,camera);
requestAnimationFrame(animate);
}
animate();
</script>
</body>
</html>
```