Prompt specification
Build or customize the Three.js 3D scene "Pocket access while seated" 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: Pocket access while seated
Category: Everyday & Industrial · Style: Procedural Solid / Shader 3D
License: MIT · Reference: https://three3d.app/scene/pocket-access-while-seated-x0602
Scene Description Prompt:
Please create one self-contained HTML file with Three.js from cdnjs showing garment pockets. Animate a simplified figure sitting with two differently located pockets; hand paths reveal which opening becomes obstructed by the body pose in this geometric demonstration. Low-poly.
Working Three.js Starter (pocket-access-while-seated-x0602):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<title>Seated Pocket Reachability — Low-Poly Study</title>
<style>
:root{
--panel:rgba(17,19,25,.86); --line:rgba(255,255,255,.10); --txt:#e8ecf2; --dim:#98a2b3;
--a:#3fd6c6; --b:#ffb454; --bad:#ff5a68; --good:#4fd38a;
}
*{box-sizing:border-box}
html,body{margin:0;height:100%;background:#0e1014;color:var(--txt);overflow:hidden;
font:12.5px/1.5 ui-sans-serif,system-ui,"Segoe UI",Roboto,Helvetica,Arial,sans-serif}
#view{position:fixed;inset:0}
#view canvas{display:block;touch-action:none}
.panel{position:fixed;left:16px;top:16px;width:322px;background:var(--panel);backdrop-filter:blur(12px);
border:1px solid var(--line);border-radius:14px;padding:14px 15px 13px;z-index:5;
box-shadow:0 18px 50px rgba(0,0,0,.5)}
h1{margin:0;font-size:12px;letter-spacing:.16em;text-transform:uppercase}
.sub{color:var(--dim);font-size:11px;margin:3px 0 12px}
.card{border:1px solid var(--line);border-radius:10px;padding:9px 10px;margin-bottom:8px;position:relative;
background:rgba(255,255,255,.02)}
.card .nm{font-weight:600;font-size:12px;letter-spacing:.03em}
.card .dp{color:var(--dim);font-size:10.5px;margin-top:1px}
.badge{position:absolute;right:9px;top:9px;font:600 9.5px/1 ui-monospace,monospace;letter-spacing:.12em;
padding:4px 7px;border-radius:99px}
.good .badge{color:var(--good);background:rgba(79,211,138,.13);border:1px solid rgba(79,211,138,.4)}
.bad .badge{color:var(--bad); background:rgba(255,90,104,.13);border:1px solid rgba(255,90,104,.4)}
.metric{color:var(--dim);font:11px/1.4 ui-monospace,monospace;margin-top:6px;border-top:1px dashed var(--line);padding-top:5px}
.metric b{color:var(--txt);font-weight:600}
.keys{display:flex;flex-wrap:wrap;gap:6px;margin-top:10px}
button{font:inherit;font-size:11px;color:var(--txt);background:rgba(255,255,255,.05);border:1px solid var(--line);
border-radius:8px;padding:6px 9px;cursor:pointer;transition:.15s}
button:hover{background:rgba(255,255,255,.12)}
button.on{background:rgba(63,214,198,.16);border-color:rgba(63,214,198,.5);color:#bff5ee}
#diag{width:100%;height:auto;margin-top:10px;border:1px solid var(--line);border-radius:9px;background:rgba(0,0,0,.28);display:block}
.legend{display:flex;gap:14px;color:var(--dim);font-size:10.5px;margin-top:7px;flex-wrap:wrap}
.legend i{display:inline-block;width:16px;height:3px;border-radius:2px;vertical-align:middle;margin-right:5px}
.hint{position:fixed;left:50%;bottom:14px;transform:translateX(-50%);color:var(--dim);font-size:11px;z-index:4;
background:rgba(17,19,25,.6);border:1px solid var(--line);padding:6px 12px;border-radius:99px;backdrop-filter:blur(6px)}
.lbl{position:fixed;transform:translate(-50%,-50%);z-index:3;pointer-events:none;font:600 10px/1 ui-monospace,monospace;
letter-spacing:.1em;padding:4px 7px;border-radius:6px;background:rgba(14,16,20,.72);border:1px solid var(--line);
white-space:nowrap;backdrop-filter:blur(4px)}
.lbl i{display:inline-block;width:7px;height:7px;border-radius:50%;margin-right:6px;vertical-align:middle}
.lbl.bad{border-color:rgba(255,90,104,.55);color:#ffd2d6}
</style>
</head>
<body>
<div id="view"></div>
<div class="panel">
<h1>Seated pocket reachability</h1>
<div class="sub">Low-poly geometric study · one hand, two garment openings, reach paths tested against body volumes</div>
<div class="card good" id="cardA">
<div class="badge" id="badgeA">CLEAR</div>
<div class="nm" style="color:var(--a)">A · THIGH POCKET</div>
<div class="dp">near leg · opening faces up on the lap</div>
<div class="metric" id="metA">—</div>
</div>
<div class="card bad" id="cardB">
<div class="badge" id="badgeB">BLOCKED</div>
<div class="nm" style="color:var(--b)">B · WAIST SIDE POCKET</div>
<div class="dp">far side of the jacket · lateral slit</div>
<div class="metric" id="metB">—</div>
</div>
<canvas id="diag" width="300" height="330"></canvas>
<div class="legend">
<span><i style="background:var(--good)"></i>clear approach</span>
<span><i style="background:var(--bad)"></i>intersects body</span>
<span><i style="background:#8b93a3"></i>body proxy</span>
</div>
<div class="keys">
<button id="bA">Reach A</button>
<button id="bB">Reach B</button>
<button id="bAuto" class="on">Auto</button>
<button id="bGhost">Ghost body</button>
<button id="bPaths" class="on">Paths</button>
<button id="bF">Front</button>
<button id="bS">Side</button>
<button id="bR">Rear</button>
</div>
</div>
<div class="hint">drag = orbit · wheel = zoom · shift-drag = pan</div>
<script type="module">
import * as THREE from 'https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js';
/* ---------------------------------------------------------------- helpers */
const V=(x=0,y=0,z=0)=>new THREE.Vector3(x,y,z);
const clamp=THREE.MathUtils.clamp, lerp=THREE.MathUtils.lerp;
const smooth=t=>{t=clamp(t,0,1);return t*t*(3-2*t);};
const $=s=>document.querySelector(s);
function hashStr(s){let h=2166136261;for(let i=0;i<s.length;i++){h^=s.charCodeAt(i);h=Math.imul(h,16777619);}return h>>>0;}
function jitter(geo,amt){
const p=geo.attributes.position, map=new Map();
for(let i=0;i<p.count;i++){
const x=p.getX(i),y=p.getY(i),z=p.getZ(i);
const k=x.toFixed(3)+','+y.toFixed(3)+','+z.toFixed(3);
let o=map.get(k);
if(!o){const h=hashStr(k);o=[(h%997)/997-.5,((h/997|0)%997)/997-.5,((h/997/997|0)%997)/997-.5];map.set(k,o);}
p.setXYZ(i,x+o[0]*amt,y+o[1]*amt,z+o[2]*amt);
}
p.needsUpdate=true; geo.computeVertexNormals(); return geo;
}
/* ------------------------------------------------------------- renderer */
const renderer=new THREE.WebGLRenderer({antialias:true});
renderer.setPixelRatio(Math.min(devicePixelRatio,2));
renderer.setSize(innerWidth,innerHeight);
renderer.shadowMap.enabled=true; renderer.shadowMap.type=THREE.PCFSoftShadowMap;
renderer.toneMapping=THREE.ACESFilmicToneMapping; renderer.toneMappingExposure=1.02;
$('#view').appendChild(renderer.domElement);
const scene=new THREE.Scene();
scene.background=new THREE.Color(0x14161c);
scene.fog=new THREE.Fog(0x14161c,7,18);
const camera=new THREE.PerspectiveCamera(38,innerWidth/innerHeight,.05,60);
/* --------------------------------------------------------------- lights */
scene.add(new THREE.HemisphereLight(0x9fb6dc,0x15171c,.55));
const key=new THREE.DirectionalLight(0xfff1dd,2.0); key.position.set(2.4,3.6,2.8);
key.castShadow=true; key.shadow.mapSize.set(1024,1024);
key.shadow.camera.left=-3;key.shadow.camera.right=3;key.shadow.camera.top=3;key.shadow.camera.bottom=-3;
key.shadow.camera.near=.5;key.shadow.camera.far=12;key.shadow.bias=-.0006;scene.add(key);
const rim=new THREE.DirectionalLight(0x86a8ff,.75); rim.position.set(-2.8,2.2,-2.6); scene.add(rim);
const fill=new THREE.DirectionalLight(0xffd9b0,.35); fill.position.set(-1.2,1.4,3.0); scene.add(fill);
/* ------------------------------------------------------------ materials */
const std=(c,r=.85)=>new THREE.MeshStandardMaterial({color:c,roughness:r,metalness:0,flatShading:true});
const M={
jacket: std(0xc9873a), trouser: std(0x39455c), skin: std(0xe2b492),
bench: std(0x8a6a4a,.95), ground: new THREE.MeshStandardMaterial({color:0x1b1e25,roughness:1}),
hole: new THREE.MeshStandardMaterial({color:0x0d0f13,roughness:1,flatShading:true}),
accA: new THREE.MeshBasicMaterial({color:0x3fd6c6}), accB: new THREE.MeshBasicMaterial({color:0xffb454}),
ghost: new THREE.MeshBasicMaterial({color:0x6f86a8,wireframe:true,transparent:true,opacity:.32})
};
const BODY_MATS=[M.jacket,M.trouser,M.skin,M.bench];
/* ----------------------------------------------------------- primitives */
function mesh(geo,mat,parent){const m=new THREE.Mesh(geo,mat);m.castShadow=true;m.receiveShadow=true;parent.add(m);return m;}
function limb(a,b,r0,r1,mat,parent,seg=8){
const dir=b.clone().sub(a), len=dir.length();
const g=new THREE.CylinderGeometry(r1,r0,len,seg,1,false);
const m=mesh(g,mat,parent);
m.position.copy(a).add(b).multiplyScalar(.5);
m.quaternion.setFromUnitVectors(V(0,1,0),dir.clone().normalize());
return m;
}
function ico(r,mat,parent,det=1){const m=mesh(new THREE.IcosahedronGeometry(r,det),mat,parent);return m;}
/* ---------------------------------------------------------------- world */
const ground=mesh(new THREE.CircleGeometry(9,28),M.ground,scene);
ground.rotation.x=-Math.PI/2; ground.position.y=-.002; ground.castShadow=false;
const grid=new THREE.GridHelper(16,32,0x2e3440,0x22273130|0x222731);
grid.material.transparent=true; grid.material.opacity=.45; grid.position.y=.004; scene.add(grid);
/* bench */
const bench=new THREE.Group(); scene.add(bench);
const seat=mesh(new THREE.BoxGeometry(1.5,.09,.86),M.bench,bench); seat.position.set(0,.455,.05);
const back=mesh(new THREE.BoxGeometry(1.5,.56,.07),M.bench,bench); back.position.set(0,.77,-.345);
for(const sx of [-.7,.7]){const leg=mesh(new THREE.BoxGeometry(.07,.41,.78),M.bench,bench); leg.position.set(sx,.205,.05);}
/* figure */
const figure=new THREE.Group(); scene.add(figure);
const torsoGeo=jitter(new THREE.CylinderGeometry(.285,.245,.62,9,2,false),.014);
const torso=mesh(torsoGeo,M.jacket,figure);
torso.position.set(0,.99,-.045); torso.rotation.x=-Math.atan2(.09,.62);
const pelvis=mesh(new THREE.CylinderGeometry(.235,.225,.20,9),M.trouser,figure); pelvis.position.set(0,.655,.005);
const neck=limb(V(0,1.28,-.10),V(0,1.44,-.11),.075,.07,M.skin,figure,7);
const head=ico(.165,M.skin,figure,1); head.position.set(0,1.56,-.11); jitter(head.geometry,.012);
head.rotation.set(.12,0,.2);
const shoulderGeo=new THREE.IcosahedronGeometry(.095,1);
for(const sx of [-.30,.30]){const s=mesh(shoulderGeo,M.jacket,figure); s.position.set(sx,1.215,-.055);}
const HIP={r:V(.17,.64,.02),k:V(.18,.61,.56),a:V(.18,.13,.62)};
for(const s of [1,-1]){
limb(V(s*.17,.645,.02),V(s*.18,.61,.56),.145,.135,M.trouser,figure,8);
const kn=ico(.135,M.trouser,figure,1); kn.position.set(s*.18,.61,.56);
limb(V(s*.18,.60,.56),V(s*.18,.13,.62),.105,.085,M.trouser,figure,7);
const ft=mesh(new THREE.BoxGeometry(.21,.10,.33),M.trouser,figure); ft.position.set(s*.18,.05,.74);
}
/* --------------------------------------------------------- arm + hand */
const armGroup=new THREE.Group(); scene.add(armGroup);
const upperArm=mesh(new THREE.CylinderGeometry(.052,.046,.34,7),M.jacket,armGroup);
const foreArm =mesh(new THREE.CylinderGeometry(.044,.038,.32,7),M.skin,armGroup);
const elbowBall=ico(.048,M.skin,armGroup,0);
const hand=new THREE.Group(); scene.add(hand);
const palm=mesh(new THREE.BoxGeometry(.095,.032,.085),M.skin,hand); palm.position.set(0,0,.022);
for(let i=0;i<4;i++){const f=mesh(new THREE.BoxGeometry(.021,.026,.072),M.skin,hand);
f.position.set(-.033+i*.022,-.002,.096);}
const thumb=mesh(new THREE.BoxGeometry(.022,.024,.042),M.skin,hand);
thumb.position.set(.052,-.006,.045); thumb.rotation.set(0,0,.5);
function orientSegment(m,a,b){
const d=b.clone().sub(a);
m.position.copy(a).add(b).multiplyScalar(.5);
m.scale.y=d.length()/.34;
m.quaternion.setFromUnitVectors(V(0,1,0),d.clone().normalize());
}
function solveElbow(S,H,l1,l2,pole){
const d=H.clone().sub(S); let dist=d.length();
dist=Math.min(dist,l1+l2-.0005); d.normalize();
const a=(l1*l1-l2*l2+dist*dist)/(2*dist);
const h=Math.sqrt(Math.max(0,l1*l1-a*a));
let p=pole.clone().addScaledVector(d,-pole.dot(d));
if(p.lengthSq()<1e-6) p=V(1,0,0).addScaledVector(d,-d.x);
p.normalize();
return S.clone().addScaledVector(d,a).addScaledVector(p,h);
}
/* static far arm */
(function(){
const S=V(-.30,1.215,-.055), E=V(-.40,.96,.02), W=V(-.31,.865,.22);
const ua=mesh(new THREE.CylinderGeometry(.052,.046,.34,7),M.jacket,scene);
const fa=mesh(new THREE.CylinderGeometry(.044,.038,.32,7),M.skin,scene);
orientSegment(ua,S,E); orientSegment(fa,E,W);
const h=ico(.055,M.skin,scene,1); h.position.copy(W); h.scale.set(1,.6,1.3);
})();
/* --------------------------------------------------------- body proxies */
const proxies=[
{name:'torso (abdomen)',type:'tapered',a:V(0,.68,0),b:V(0,1.28,-.09),r0:.245,r1:.285},
{name:'pelvis / hips',type:'sphere',c:V(0,.66,0),r:.235},
{name:'near thigh',type:'capsule',a:V(.17,.645,.02),b:V(.18,.61,.56),r:.145},
{name:'far thigh',type:'capsule',a:V(-.17,.645,.02),b:V(-.18,.61,.56),r:.145},
{name:'near shin',type:'capsule',a:V(.18,.60,.56),b:V(.18,.13,.62),r:.105},
{name:'far shin',type:'capsule',a:V(-.18,.60,.56),b:V(-.18,.13,.62),r:.105},
{name:'neck',type:'capsule',a:V(0,1.28,-.10),b:V(0,1.44,-.11),r:.075},
{name:'bench seat',type:'box',min:V(-.75,.41,-.38),max:V(.75,.50,.48)},
{name:'bench back',type:'box',min:V(-.75,.50,-.38),max:V(.75,1.05,-.31)}
];
function closestOnSeg(p,a,b){
const ab=b.clone().sub(a);
const t=clamp(p.clone().sub(a).dot(ab)/ab.lengthSq(),0,1);
return {t,q:a.clone().addScaledVector(ab,t)};
}
function rawClearance(p){
let best=Infinity,name='';
for(const pr of proxies){
let d;
if(pr.type==='sphere') d=p.distanceTo(pr.c)-pr.r;
else if(pr.type==='capsule'){const {q}=closestOnSeg(p,pr.a,pr.b); d=p.distanceTo(q)-pr.r;}
else if(pr.type==='tapered'){const {t,q}=closestOnSeg(p,pr.a,pr.b); d=p.distanceTo(q)-(pr.r0+(pr.r1-pr.r0)*t);}
else {const dx=Math.max(pr.min.x-p.x,0,p.x-pr.max.x),
dy=Math.max(pr.min.y-p.y,0,p.y-pr.max.y),
dz=Math.max(pr.min.z-p.z,0,p.z-pr.max.z); d=Math.hypot(dx,dy,dz);}
if(d<best){best=d;name=pr.name;}
}
return {d:best,name};
}
/* ------------------------------------------------------------- pockets */
const POCKETS={
A:{id:'A',name:'THIGH POCKET',sub:'near leg · opening faces up on the lap',accent:0x3fd6c6,
mouth:V(.185,.764,.40),mouthIndex:4,
pts:[V(.28,.86,.28),V(.305,.915,.345),V(.275,.90,.425),V(.225,.865,.445),V(.19,.80,.41),V(.185,.72,.40)]},
B:{id:'B',name:'WAIST SIDE POCKET',sub:'far side of the jacket · lateral slit',accent:0xffb454,
mouth:V(-.262,.86,-.027),mouthIndex:6,
pts:[V(.28,.86,.28),V(.315,.945,.25),V(.245,.995,.185),V(.10,.975,.145),V(-.045,.935,.10),
V(-.165,.895,.05),V(-.245,.868,.005),V(-.30,.86,-.03)]}
};
const HAND_R=.060, EXEMPT=.12;
/* build curves */
for(const k in POCKETS){
const pk=POCKETS[k];
pk.curve=new THREE.CatmullRomCurve3(pk.pts,false,'catmullrom',.5);
const ap=new THREE.CatmullRomCurve3(pk.pts.slice(0,pk.mouthIndex+1));
const ins=new THREE.CatmullRomCurve3(pk.pts.slice(pk.mouthIndex));
pk.uMouth=ap.getLength()/(ap.getLength()+ins.getLength());
}
function clearanceAt(pk,u){
const p=pk.curve.getPointAt(clamp(u,0,1));
if(p.distanceTo(pk.mouth)<EXEMPT) return {d:EXEMPT,name:'pocket opening',exempt:true};
return rawClearance(p);
}
const ANALYSIS={};
for(const k in POCKETS){
const pk=POCKETS[k],N=220; let stopU=1,blocker=null,minD=Infinity,minName='';
for(let i=0;i<=N;i++){
const u=i/N,c=clearanceAt(pk,u);
if(blocker===null && c.d<HAND_R){stopU=u;blocker=c.name;}
if(u<pk.uMouth && c.d<minD){minD=c.d;minName=c.name;}
}
if(blocker===null){ // no collision: use the raw minimum for reporting
for(let i=0;i<=N;i++){const u=i/N,p=pk.curve.getPointAt(u);const c=rawClearance(p);
if(u<pk.uMouth+0.06 && c.d<minD){minD=c.d;minName=c.name;}}
}
ANALYSIS[k]={stopU,blocker,minD,minName};
}
/* path tubes with vertex colours */
function buildTube(pk,an){
const T=140,R=6,r=.014;
const g=new THREE.TubeGeometry(pk.curve,T,r,R,false);
const cols=new Float32Array(g.attributes.position.count*3);
const cClear=new THREE.Color(0x4fd38a),cBad=new THREE.Color(0xff5a68);
for(let i=0;i<=T;i++){
const u=i/T,c=clearanceAt(pk,u);
const col=(an.blocker && u>=an.stopU)?cBad:cClear;
for(let j=0;j<=R;j++){
const idx=i*(R+1)+j; cols[idx*3]=col.r; cols[idx*3+1]=col.g; cols[idx*3+2]=col.b;
}
}
g.setAttribute('color',new THREE.BufferAttribute(cols,3));
const mat=new THREE.MeshBasicMaterial({vertexColors:true,transparent:true,opacity:.92});
return new THREE.Mesh(g,mat);
}
for(const k in POCKETS){POCKETS[k].tube=buildTube(POCKETS[k],ANALYSIS[k]);scene.add(POCKETS[k].tube);}
/* pocket mouths (visible openings) + rings */
function addMouth(pk){
const accentMat=pk.id==='A'?M.accA:M.accB;
if(pk.id==='A'){
const h=mesh(new THREE.BoxGeometry(.17,.07,.115),M.hole,figure); h.position.set(.185,.735,.40); h.rotation.x=-.05;
const trim=mesh(new THREE.BoxGeometry(.175,.016,.028),accentMat,figure); trim.position.set(.185,.772,.455); trim.rotation.x=-.05;
const ring=mesh(new THREE.TorusGeometry(.085,.006,4,16),accentMat,figure);
ring.position.set(.185,.775,.40); ring.rotation.x=-Math.PI/2+.05;
pk.ring=ring;
}else{
const h=mesh(new THREE.BoxGeometry(.06,.175,.075),M.hole,figure); h.position.set(-.238,.86,-.027);
const trim=mesh(new THREE.BoxGeometry(.016,.178,.078),accentMat,figure); trim.position.set(-.268,.86,-.027);
const ring=mesh(new THREE.TorusGeometry(.085,.006,4,16),accentMat,figure);
ring.position.set(-.262,.86,-.027); ring.rotation.y=Math.PI/2;
pk.ring=ring;
}
}
for(const k in POCKETS) addMouth(POCKETS[k]);
/* ghost proxies */
const ghostGroup=new THREE.Group(); ghostGroup.visible=false; scene.add(ghostGroup);
function ghostCapsule(a,b,r0,r1){
const g=new THREE.Group();
const len=a.distanceTo(b);
const c=new THREE.Mesh(new THREE.CylinderGeometry(r1,r0,len,10,1,true),M.ghost); g.add(c);
const s0=new THREE.Mesh(new THREE.SphereGeometry(r0,10,6),M.ghost); s0.position.copy(a); g.add(s0);
const s1=new THREE.Mesh(new THREE.SphereGeometry(r1,10,6),M.ghost); s1.position.copy(b); g.add(s1);
g.quaternion.setFromUnitVectors(V(0,1,0),b.clone().sub(a).normalize());
g.position.copy(a).add(b).multiplyScalar(.5);
return g;
}
ghostGroup.add(ghostCapsule(V(0,.68,0),V(0,1.28,-.09),.245,.285));
ghostGroup.add(ghostCapsule(V(.17,.645,.02),V(.18,.61,.56),.145,.145));
ghostGroup.add(ghostCapsule(V(-.17,.645,.02),V(-.18,.61,.56),.145,.145));
ghostGroup.add(ghostCapsule(V(.18,.60,.56),V(.18,.13,.62),.105,.105));
ghostGroup.add(ghostCapsule(V(-.18,.60,.56),V(-.18,.13,.62),.105,.105));
const pelvisGhost=new THREE.Mesh(new THREE.SphereGeometry(.235,12,8),M.ghost);
pelvisGhost.position.set(0,.66,0); ghostGroup.add(pelvisGhost);
/* blocked marker */
const blockMarker=new THREE.Group(); scene.add(blockMarker); blockMarker.visible=false;
const blockSphere=new THREE.Mesh(new THREE.IcosahedronGeometry(HAND_R,1),
new THREE.MeshBasicMaterial({color:0xff5a68,wireframe:true,transparent:true,opacity:.9}));
blockMarker.add(blockSphere);
const blockDisc=new THREE.Mesh(new THREE.CircleGeometry(.028,12),new THREE.MeshBasicMaterial({color:0xff5a68}));
blockMarker.add(blockDisc);
/* ---------------------------------------------------------------- labels */
function makeLabel(txt,color,anchor){
const el=document.createElement('div'); el.className='lbl';
el.innerHTML='<i style="background:'+color+'"></i>'+txt; document.body.appendChild(el);
return {el,anchor};
}
const lblA=makeLabel('A · THIGH POCKET','#3fd6c6',V(.185,.80,.40));
const lblB=makeLabel('B · SIDE POCKET','#ffb454',V(-.30,.95,-.03));
const lblBad=makeLabel('BLOCKED · TORSO','#ff5a68',V(0,0,0)); lblBad.el.classList.add('bad'); lblBad.el.style.display='none';
const tmp=V();
function project(el,anchor,dx=0,dy=0){
tmp.copy(anchor).project(camera);
if(tmp.z>1){el.style.display='none';return;}
el.style.display='';
el.style.left=((tmp.x*.5+.5)*innerWidth+dx)+'px';
el.style.top=((-tmp.y*.5+.5)*innerHeight+dy)+'px';
}
/* ------------------------------------------------------------ animation */
const anim={id:null,phase:'idle',t:0,u:0,from:0,to:0,dur:1,press:0};
let auto=true,autoIdx=0,autoHold=0;
function startPocket(id){
const pk=POCKETS[id],an=ANALYSIS[id];
anim.id=id; anim.phase='reach'; anim.t=0; anim.press=0;
anim.from=0; anim.to=an.blocker?an.stopU:pk.uMouth; anim.dur=2.0;
}
function updateAnim(dt){
if(anim.phase==='idle'){
if(auto){ if(autoHold<=0){ startPocket(autoIdx===0?'A':'B'); autoIdx^=1; autoHold=99; } }
else autoHold-=dt;
return;
}
const pk=POCKETS[anim.id],an=ANALYSIS[anim.id];
anim.t+=dt;
switch(anim.phase){
case 'reach':
anim.u=lerp(anim.from,anim.to,smooth(anim.t/anim.dur));
if(anim.t>=anim.dur){
if(an.blocker){anim.phase='press';anim.t=0;}
else{anim.phase='insert';anim.t=0;anim.from=pk.uMouth;anim.to=1;anim.dur=.55;}
} break;
case 'insert':
anim.u=lerp(anim.from,anim.to,smooth(anim.t/anim.dur));
if(anim.t>=anim.dur){anim.phase='hold';anim.t=0;} break;
case 'press':
anim.u=an.stopU; anim.press=Math.sin(anim.t*9)*.014;
if(anim.t>1.35){anim.phase='retract';anim.t=0;anim.from=an.stopU;anim.to=0;anim.dur=1.15;}
break;
case 'hold':
anim.u=1-Math.abs(Math.sin(anim.t*4))*.012;
if(anim.t>1.15){anim.phase='retract';anim.t=0;anim.from=1;anim.to=pk.uMouth;anim.dur=.85;}
break;
case 'retract':
anim.u=lerp(anim.from,anim.to,smooth(anim.t/anim.dur));
if(anim.t>=anim.dur){anim.phase='rest';anim.t=0;}
break;
case 'rest':
anim.u=0; anim.press=0;
if(anim.t>.75){anim.phase='idle';anim.t=0;}
break;
}
}
/* ------------------------------------------------------- camera (custom) */
const rig={target:V(0,.95,.12),az:.95,el:.20,dist:3.05,
azT:.95,elT:.20,distT:3.05,targetT:V(0,.95,.12)};
let dragging=false,lastX=0,lastY=0,btn=0,userUntil=0;
const cv=renderer.domElement;
cv.addEventListener('contextmenu',e=>e.preventDefault());
cv.addEventListener('pointerdown',e=>{dragging=true;btn=e.button;lastX=e.clientX;lastY=e.clientY;cv.setPointerCapture(e.pointerId);userUntil=performance.now()+4000;});
cv.addEventListener('pointerup',e=>{dragging=false;});
cv.addEventListener('pointermove',e=>{
if(!dragging)return;
const dx=e.clientX-lastX, dy=e.clientY-lastY; lastX=e.clientX; lastY=e.clientY;
userUntil=performance.now()+4000;
if(btn===2||e.shiftKey){
const right=V(Math.cos(rig.azT),0,-Math.sin(rig.azT));
const up=V(0,1,0);
const s=rig.distT*.0016;
rig.targetT.addScaledVector(right,-dx*s).addScaledVector(up,dy*s);
}else{
rig.azT-=dx*.006;
rig.elT=clamp(rig.elT+dy*.005,-.15,1.35);
}
});
cv.addEventListener('wheel',e=>{e.preventDefault();rig.distT=clamp(rig.distT*(1+e.deltaY*.0012),1.5,8);userUntil=performance.now()+4000;},{passive:false});
function preset(az,el,dist,tgt){rig.azT=az;rig.elT=el;rig.distT=dist;rig.targetT.copy(tgt||V(0,.95,.12));userUntil=performance.now()+4000;}
/* --------------------------------------------------------------- UI */
function setCard(id,an){
const card=$('#card'+id), badge=$('#badge'+id), met=$('#met'+id);
if(an.blocker){card.className='card bad';badge.textContent='BLOCKED';
met.innerHTML='path intersects <b>'+an.blocker+'</b> · would penetrate <b>'+
(Math.abs(an.minD)*100).toFixed(1)+' cm</b> at rest-length '+ '≈ '+ (an.stopU*100).toFixed(0)+'% of path';}
else{card.className='card good';badge.textContent='CLEAR';
met.innerHTML='approach clears body by <b>'+(an.minD*100).toFixed(1)+' cm</b> · no proxy intersection';}
}
setCard('A',ANALYSIS.A); setCard('B',ANALYSIS.B);
$('#bA').onclick=()=>{auto=false;$('#bAuto').classList.remove('on');startPocket('A');};
$('#bB').onclick=()=>{auto=false;$('#bAuto').classList.remove('on');startPocket('B');};
$('#bAuto').onclick=e=>{auto=!auto;e.target.classList.toggle('on',auto);autoHold=0;};
$('#bGhost').onclick=e=>{
const on=!e.target.classList.contains('on'); e.target.classList.toggle('on',on);
ghostGroup.visible=on;
BODY_MATS.forEach(m=>{m.transparent=on;m.opacity=on?.24:1;m.depthWrite=!on;});
};
$('#bPaths').onclick=e=>{const on=!e.target.classList.contains('on');e.target.classList.toggle('on',on);
for(const k in POCKETS) POCKETS[k].tube.visible=on;};
$('#bF').onclick=()=>preset(.15,.18,3.0);
$('#bS').onclick=()=>preset(1.45,.16,2.9);
$('#bR').onclick=()=>preset(2.75,.20,3.0);
/* ------------------------------------------------- side-view diagram */
const diag=$('#diag'),dctx=diag.getContext('2d');
const S=150,X0=25,YB=300,ZW=-.60;
const mx=z=>X0+(z-ZW)*S, my=y=>YB-y*S;
function drawDiagram(){
const w=diag.width,h=diag.height;
dctx.clearRect(0,0,w,h);
dctx.fillStyle='rgba(10,12,16,.55)'; dctx.fillRect(0,0,w,h);
// seat + back
dctx.fillStyle='rgba(120,96,68,.45)';
dctx.fillRect(mx(-.38),my(.50),(.86)*S,.09*S);
dctx.fillRect(mx(-.38),my(1.05),.07*S,.55*S);
// proxies
dctx.lineCap='round';
const cap=(a,b,r,col)=>{dctx.strokeStyle=col;dctx.lineWidth=r*2*S;
dctx.beginPath();dctx.moveTo(mx(a.z),my(a.y));dctx.lineTo(mx(b.z),my(b.y));dctx.stroke();};
cap(V(0,.68,0),V(0,1.28,-.09),.255,'rgba(120,140,175,.30)');
cap(V(.17,.645,.02),V(.18,.61,.56),.145,'rgba(120,140,175,.30)');
cap(V(-.17,.645,.02),V(-.18,.61,.56),.145,'rgba(120,140,175,.30)');
cap(V(.18,.60,.56),V(.18,.13,.62),.105,'rgba(120,140,175,.22)');
cap(V(-.18,.60,.56),V(-.18,.13,.62),.105,'rgba(120,140,175,.22)');
// active path
const pk=POCKETS[anim.id]||POCKETS.A, an=ANALYSIS[pk.id];
const N=90;
let prev=null,prevBad=false;
for(let i=0;i<=N;i++){
const u=i/N,p=pk.curve.getPointAt(u);
const bad=an.blocker&&u>=an.stopU;
if(prev){
dctx.strokeStyle=bad?'#ff5a68':'#4fd38a'; dctx.lineWidth=2.4;
dctx.beginPath();dctx.moveTo(mx(prev.z),my(prev.y));dctx.lineTo(mx(p.z),my(p.y));dctx.stroke();
}
prev=p;prevBad=bad;
}
// pockets
for(const k in POCKETS){
const p=POCKETS[k],c=k==='A'?'#3fd6c6':'#ffb454';
dctx.strokeStyle=c;dctx.fillStyle=c;dctx.lineWidth=2;
dctx.beginPath();dctx.arc(mx(p.mouth.z),my(p.mouth.y),6,0,6.3);dctx.stroke();
dctx.font='600 10px ui-monospace,monospace';dctx.fillText(k,mx(p.mouth.z)+9,my(p.mouth.y)+3);
}
// hand
const hp=hand.position;
dctx.fillStyle='rgba(226,180,146,.95)';
dctx.beginPath();dctx.arc(mx(hp.z),my(hp.y),5.5,0,6.3);dctx.fill();
dctx.strokeStyle='rgba(255,255,255,.35)';dctx.lineWidth=1;
dctx.beginPath();dctx.arc(mx(hp.z),my(hp.y),HAND_R*S,0,6.3);dctx.stroke();
// scale note
dctx.fillStyle='rgba(152,162,179,.8)';dctx.font='9px ui-monospace,monospace';
dctx.fillText('side view · z–y projection',10,16);
}
/* --------------------------------------------------------------- loop */
const clock=new THREE.Clock();
let elapsed=0;
function animate(){
requestAnimationFrame(animate);
const dt=Math.min(clock.getDelta(),.05); elapsed+=dt;
updateAnim(dt);
const pk=anim.id?POCKETS[anim.id]:null;
if(pk){
const u=clamp(anim.u,0,1);
const p=pk.curve.getPointAt(u);
const tan=pk.curve.getTangentAt(u).normalize();
hand.position.copy(p).addScaledVector(tan,anim.press);
// orient hand: fingers along travel, back of hand up
const zAxis=tan.clone().normalize();
let xAxis=new THREE.Vector3().crossVectors(V(0,1,0),zAxis);
if(xAxis.lengthSq()<1e-6)xAxis.set(1,0,0); xAxis.normalize();
const yAxis=new THREE.Vector3().crossVectors(zAxis,xAxis).normalize();
const m4=new THREE.Matrix4().makeBasis(xAxis,yAxis,zAxis);
hand.quaternion.setFromRotationMatrix(m4);
// arm IK
const S0=V(.30,1.215,-.055);
const wrist=hand.position.clone().addScaledVector(zAxis,-.03);
const E=solveElbow(S0,wrist,.34,.32,V(1,.05,-.45));
orientSegment(upperArm,S0,E); orientSegment(foreArm,E,wrist);
elbowBall.position.copy(E);
}else{
hand.position.copy(POCKETS.A.pts[0]);
}
// blocked marker
if(pk&&ANALYSIS[pk.id].blocker&&(anim.phase==='press'||anim.phase==='reach')){
const an=ANALYSIS[pk.id], u=clamp(an.stopU,0,1);
const p=pk.curve.getPointAt(u), t=pk.curve.getTangentAt(u).normalize();
blockMarker.visible=true;
blockMarker.position.copy(p).addScaledVector(t,anim.press*.5);
blockDisc.quaternion.setFromUnitVectors(V(0,0,1),t);
const s=1+Math.sin(elapsed*6)*.12; blockSphere.scale.setScalar(s);
lblBad.anchor.copy(p).add(V(0,.16,0));
project(lblBad.el,lblBad.anchor);
}else{ blockMarker.visible=false; lblBad.el.style.display='none'; }
// tube emphasis
for(const k in POCKETS){
const t=POCKETS[k].tube;
t.material.opacity=(anim.id===k)?.95:.22;
}
// pocket rings pulse
for(const k in POCKETS){
const r=POCKETS[k].ring; const s=1+Math.sin(elapsed*2.2+(k==='A'?0:1.6))*.07; r.scale.setScalar(s);
}
// camera
rig.az=lerp(rig.az,rig.azT,.12); rig.el=lerp(rig.el,rig.elT,.12);
rig.dist=lerp(rig.dist,rig.distT,.12); rig.target.lerp(rig.targetT,.12);
const cp=Math.cos(rig.el);
camera.position.set(rig.target.x+rig.dist*Math.sin(rig.az)*cp,
rig.target.y+rig.dist*Math.sin(rig.el),
rig.target.z+rig.dist*Math.cos(rig.az)*cp);
camera.lookAt(rig.target);
project(lblA.el,lblA.anchor,0,-18);
project(lblB.el,lblB.anchor,0,-18);
drawDiagram();
renderer.render(scene,camera);
}
animate();
addEventListener('resize',()=>{
camera.aspect=innerWidth/innerHeight; camera.updateProjectionMatrix();
renderer.setSize(innerWidth,innerHeight);
});
</script>
</body>
</html>
```