Skip to content
VibeCodeBench
Back to the app

The code

Build a Cell, Part by Part by GPT-6.2 Sol

Exactly as the AI wrote it. Shown as text, so nothing runs on this page.

index.html

works · auto-checked (Checked by an automated pass. Not hand-played yet.)29.5 KB · 390 lines · loads cdn.jsdelivr.net/npm/[email protected]/build/three.min.js
Download
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="theme-color" content="#101322">
<title>Cell Lab | Inside an animal cell</title>
<style>
:root{color-scheme:dark;--bg:#101322;--panel:#191d31;--border:#333952;--text:#f7f4ff;--muted:#bcc5df;--mint:#87eddc;--lav:#d8c6ff}
*{box-sizing:border-box}body{margin:0;background:var(--bg);color:var(--text);font-family:system-ui,-apple-system,"Segoe UI",sans-serif}button,input{font:inherit}button{color:inherit;cursor:pointer}button:disabled{opacity:.45;cursor:default}button:focus-visible,input:focus-visible{outline:2px solid var(--mint);outline-offset:3px}
main{max-width:1536px;margin:auto;padding:0 30px 18px}.top{height:70px;display:flex;justify-content:space-between;align-items:center}.brand,.mode{display:flex;align-items:center;gap:10px}.brand{font-size:16px;font-weight:780}.mode{font-size:12px;color:var(--muted)}.dot{display:inline-block;width:7px;height:7px;border-radius:50%;background:var(--mint);box-shadow:0 0 10px #87eddc77}.mark{display:grid;place-items:center;width:37px;height:37px;border-radius:11px;background:#292645;color:#d1baff}
.layout{display:grid;grid-template-columns:minmax(0,1fr) 312px;gap:15px}.stage,.side,.controls{border:1px solid var(--border);border-radius:18px;overflow:hidden}.stage{height:clamp(537px,calc(100svh - 165px),840px);position:relative;isolation:isolate;background:radial-gradient(ellipse at 49% 51%,#30315b 0,#202543 42%,#15192f 75%,#13172a 100%)}.stage:before{content:"";position:absolute;inset:0;z-index:-1;pointer-events:none;background-image:radial-gradient(#adbef13d 1px,transparent 1px);background-size:24px 24px;mask-image:radial-gradient(ellipse at center,transparent 36%,#000 100%)}canvas{position:absolute;inset:0;width:100%;height:100%;touch-action:none;cursor:grab}canvas:active{cursor:grabbing}.title{position:absolute;left:24px;top:24px;pointer-events:none}.eyebrow{font-size:10px;text-transform:uppercase;font-weight:800;letter-spacing:.16em;color:#a3e2e3;display:flex;align-items:center;gap:7px}.eyebrow:before{content:"";width:17px;height:1px;background:currentColor}h1{font-size:clamp(29px,3.2vw,46px);letter-spacing:-.06em;line-height:1.01;margin:6px 0}
.cut{position:absolute;right:19px;top:22px;display:flex;align-items:center;gap:8px;border:1px solid #505573;background:#222640;min-height:44px;padding:0 10px;border-radius:10px;font-weight:700;font-size:12px}.toggle{width:29px;height:16px;border-radius:12px;background:#505875;padding:2px}.toggle b{display:block;background:#fff;width:12px;height:12px;border-radius:50%;transition:transform .2s}.cut[aria-checked=true] .toggle{background:#448d82}.cut[aria-checked=true] b{transform:translateX(13px)}
.hints{position:absolute;bottom:17px;left:19px;right:19px;display:flex;gap:13px;flex-wrap:wrap;align-items:center;color:var(--muted);font-size:11px;pointer-events:none}.hints span{display:inline-flex;align-items:center;gap:5px}.hints .status{margin-left:auto;color:#a3eee3;font-weight:720}.tag{position:absolute;display:flex;align-items:center;gap:6px;pointer-events:none;white-space:nowrap;font-size:11px;font-weight:780;transform:translateY(-50%)}.tag[hidden]{display:none}.tag i{width:8px;height:8px;border-radius:50%;border:1px solid var(--ink);box-shadow:0 0 9px var(--ink)}.tag b{width:15px;height:1px;background:var(--ink)}.tag span{border:1px solid var(--ink);border-radius:6px;padding:6px 8px;background:#181d34ef}
.side{display:flex;flex-direction:column;background:var(--panel)}.sidehead{display:flex;align-items:center;justify-content:space-between;padding:15px 16px 10px;gap:5px}.sidehead h2{margin:0;font-size:17px;letter-spacing:-.025em}.count{background:#272d46;border:1px solid #3b435f;border-radius:20px;padding:4px 8px;color:#d1d7ec;font-size:11px;white-space:nowrap;font-variant-numeric:tabular-nums}.parts{display:grid;align-content:start;gap:2px;flex:1;padding:0 9px 9px}.part{display:grid;grid-template-columns:23px minmax(0,1fr) auto;align-items:center;gap:7px;text-align:left;border:1px solid transparent;background:transparent;border-radius:8px;min-height:32px;padding:3px 7px;font-size:13px;color:#e2e6f7}.part:hover{background:#262b43}.part[aria-current=true]{background:#30304a;border-color:#585173;color:#fff}.glyph{display:grid;place-items:center;width:22px;height:22px;border-radius:7px;color:var(--ink);background:color-mix(in srgb,var(--ink) 14%,transparent)}.glyph svg{width:15px;height:15px}.num{font-size:10px;color:#abb4cf}.part[aria-current=true] .num{color:var(--ink)}.detail{min-height:104px;border-top:1px solid var(--border);background:#1f243b;padding:12px 16px 15px}.detail small{color:var(--ink);text-transform:uppercase;font-weight:800;letter-spacing:.12em;font-size:10px}.detail h3{margin:3px 0 4px;font-size:17px;letter-spacing:-.02em}.detail p{color:#d5ddf1;margin:0;font-size:12px;line-height:1.5}
.controls{margin-top:14px;padding:11px 13px;display:grid;grid-template-columns:auto 1fr auto;align-items:center;gap:17px;background:var(--panel)}.control{display:inline-flex;align-items:center;justify-content:center;gap:6px;border:1px solid #49516e;background:#252b44;padding:0 12px;border-radius:9px;font-size:13px;font-weight:750;min-height:44px;white-space:nowrap}.control svg,.hints svg{width:15px;height:15px}.play{min-width:98px;background:var(--lav);color:#241e37;border-color:transparent}.control:hover,.cut:hover{filter:brightness(1.12)}.slider{min-width:0}.slider label{display:flex;justify-content:space-between;gap:6px;font-weight:720;font-size:12px}.slider output{color:#d4bdfc;white-space:nowrap}input[type=range]{appearance:none;width:100%;display:block;height:23px;background:transparent;margin:0;cursor:pointer;--progress:0%}input[type=range]::-webkit-slider-runnable-track{height:5px;border-radius:6px;background:linear-gradient(90deg,#c5a7ff var(--progress),#454c69 var(--progress))}input[type=range]::-webkit-slider-thumb{appearance:none;background:#f5eaff;border:2px solid #be9cf5;border-radius:50%;width:16px;height:16px;margin-top:-5.5px}input[type=range]::-moz-range-track{height:5px;border-radius:6px;background:linear-gradient(90deg,#c5a7ff var(--progress),#454c69 var(--progress))}input[type=range]::-moz-range-thumb{background:#f5eaff;border:2px solid #be9cf5;border-radius:50%;width:13px;height:13px}.error{position:absolute;inset:38% 10% auto;background:#232641;border:1px solid var(--border);padding:16px;border-radius:10px}
@media(max-width:900px){main{padding:0 12px 14px}.top{height:58px}.layout{grid-template-columns:minmax(0,1fr) 249px;gap:9px}.stage{height:clamp(530px,calc(100svh - 147px),760px)}.title{left:14px}.cut{right:13px}.part{font-size:12px}}
@media(max-width:680px){main{padding:0 8px 12px;display:grid;gap:8px}.top{height:48px;padding:0 4px}.brand{gap:7px}.mark{width:31px;height:31px}.mode{font-size:10px}.layout{display:contents}.stage{grid-row:2;height:clamp(340px,56svh,510px);border-radius:14px}.controls{grid-row:3;margin:0;padding:7px;gap:7px;border-radius:13px}.control{min-width:0;padding:0 7px;font-size:12px}.slider label{font-size:10px}.side{grid-row:4;border-radius:14px}.sidehead{padding:10px 12px 7px}.detail{order:1;padding:7px 12px 10px;min-height:0;border-top:0;border-bottom:1px solid var(--border)}.parts{order:2;grid-template-columns:repeat(2,minmax(0,1fr));padding:4px 6px 7px}.part{min-height:44px;font-size:11px;gap:5px;padding:3px 5px}.num{display:none}.title{top:14px;left:12px}.cut{top:11px;right:10px;padding:0 7px;gap:5px}.hints{bottom:10px;left:9px;right:9px;gap:6px;font-size:9px}.tag{font-size:9px}.tag b{width:8px}}
</style>
</head>
<body>
<main>
<header class="top"><div class="brand"><span class="mark" aria-hidden="true"><svg viewBox="0 0 24 24" width="23" height="23" fill="none" stroke="currentColor" stroke-width="1.4"><path d="M20 7c2 4 1 8-2 11s-7 3-11 1-5-6-4-10 4-6 8-6 7 1 9 4Z"/><ellipse cx="11" cy="11" rx="3.5" ry="3"/><circle cx="16.5" cy="15" r=".7"/></svg></span>Cell Lab</div><div class="mode"><i class="dot"></i> Interactive 3D model</div></header>
<div class="layout">
<section class="stage" aria-labelledby="title">
<canvas id="view" aria-label="Interactive animal cell model"></canvas>
<div class="title"><div class="eyebrow">Cell explorer</div><h1 id="title">Inside an<br>animal cell</h1></div>
<button id="cut" class="cut" type="button" role="switch" aria-checked="false"><span class="toggle" aria-hidden="true"><b></b></span>Cut open</button>
<div id="tag" class="tag" hidden aria-hidden="true"><i></i><b></b><span></span></div>
<div class="hints"><span><svg viewBox="0 0 16 16" fill="none" stroke="currentColor" aria-hidden="true"><path d="M3 4a6 6 0 1 1-1 7M3 1v4h4"/></svg>Drag to orbit</span><span><svg viewBox="0 0 16 16" fill="none" stroke="currentColor" aria-hidden="true"><circle cx="7" cy="7" r="4"/><path d="m10 10 4 4M5 7h4M7 5v4"/></svg>Pinch or scroll to zoom</span><span>Tap a part</span><span class="status"><i class="dot"></i><span id="status">Building cell</span></span></div>
</section>
<aside class="side" aria-label="Cell parts">
<div class="sidehead"><h2>Cell parts</h2><span class="count" id="count"></span></div>
<div class="parts" id="parts"></div>
<article class="detail" id="detail" aria-live="polite"><small id="kind"></small><h3 id="name"></h3><p id="description"></p></article>
</aside>
</div>
<section class="controls" aria-label="Build controls">
<button id="play" class="control play" type="button"></button>
<div class="slider"><label for="build">Build progress <output id="step" for="build"></output></label><input id="build" type="range" min="0" max="11" step="1" value="0"></div>
<button id="reset" class="control" type="button"><svg viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1.4" aria-hidden="true"><path d="M3 5a5.5 5.5 0 1 1-.5 5M3 2v4h4"/></svg>Rebuild</button>
</section>
</main>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/build/three.min.js"></script>
<script>
'use strict';
const info=[
['Cell membrane','#8af4df','The membrane holds the cell together and controls what enters and leaves.','M3 8c0-5 10-6 10 0s-10 5-10 0Z'],
['Nucleus','#c2a1ff','The nucleus stores DNA, which carries the instructions the cell uses to work.','M8 3a5 5 0 1 0 0 10 5 5 0 0 0 0-10Zm0 3a2 2 0 1 0 0 4 2 2 0 0 0 0-4Z'],
['Nucleolus','#f9a0d8','The nucleolus assembles the pieces that make up ribosomes.','M8 4a4 4 0 1 0 0 8 4 4 0 0 0 0-8Z'],
['Rough ER','#90b6ff','The rough endoplasmic reticulum helps make and fold proteins using ribosomes attached to it.','M3 5c3-4 7 4 10 0M3 9c3-4 7 4 10 0M3 13c3-4 7 4 10 0'],
['Smooth ER','#74e6ee','The smooth endoplasmic reticulum makes fats the cell can use to build membranes.','M3 12c1-8 5 1 6-5s4-3 4 0'],
['Ribosomes','#ffe0a2','Ribosomes read instructions copied from DNA to build proteins.','M4 6h.1m7.9-1h.1M8 11h.1'],
['Golgi apparatus','#ffa3ba','The Golgi apparatus modifies, sorts and packages proteins so they reach the right place.','M3 4q5 5 10 0M4 8q4 4 8 0M6 12q2 2 4 0'],
['Mitochondria','#ffb67e','Mitochondria use energy from food to make ATP, which powers much of the cell’s work.','M3 11c-3-4 5-10 9-7s-6 11-9 7Zm3-3 2 1 1-3 2 1'],
['Lysosomes','#e6a8ff','Lysosomes break down worn-out cell parts so their materials can be reused.','M8 3a5 5 0 1 0 0 10 5 5 0 0 0 0-10ZM6 7h.1m3.9 2h.1'],
['Vesicles','#9bf3b7','Vesicles are tiny bubbles that carry materials from one place to another.','M6 5a3 3 0 1 0 0 6 3 3 0 0 0 0-6Zm6-2a1.5 1.5 0 1 0 0 3 1.5 1.5 0 0 0 0-3Z'],
['Centrioles','#f9d77f','Centrioles help arrange the fibers that move chromosomes when the cell divides.','M2 5h7v3H2zM10 7v7h3V7z'],
['Cytoskeleton','#87c7f7','The cytoskeleton is a network of fibers that supports the cell and helps move things inside it.','m2 12 5-8 7 8M4 9l8-3m-6 7 5-10']
];
const $=id=>document.getElementById(id);
const last=info.length-1;
let step=0,active=0,playing=true,cut=false,age=0;
let demo=location.hash==='#demo',clock=0,beat=-1;
let reveal=()=>{},aim=()=>{},slice=()=>{};
const buttons=info.map(([name,color,desc,symbol],index)=>{
 const el=document.createElement('button');
 el.type='button';el.className='part';el.style.setProperty('--ink',color);
 el.innerHTML='<span class="glyph" aria-hidden="true"><svg viewBox="0 0 16 16" fill="none"><path d="'+symbol+'" stroke="currentColor" stroke-width="1.4" stroke-linecap="round" stroke-linejoin="round"/></svg></span><span>'+name+'</span><span class="num" aria-hidden="true">'+String(index).padStart(2,'0')+'</span>';
 el.onclick=()=>select(index);$('parts').append(el);return el;
});
function ui(){
 $('count').textContent=step+' / '+last+' built';
 $('step').textContent='Step '+step+' of '+last;
 $('build').value=step;$('build').style.setProperty('--progress',100*step/last+'%');
 $('build').setAttribute('aria-valuetext',step+' of '+last+' parts built');
 $('status').textContent=cut?'Cutaway':step===last?'Cell complete':playing?'Building cell':'Build paused';
 const icon=playing?'<path d="M5 3v10m6-10v10" stroke="currentColor" stroke-width="2" stroke-linecap="round"/>':'<path d="m5 3 8 5-8 5Z" fill="currentColor"/>';
 $('play').innerHTML='<svg viewBox="0 0 16 16" aria-hidden="true">'+icon+'</svg>'+(playing?'Pause':step===last?'Replay':'Play');
 buttons.forEach((button,i)=>button.setAttribute('aria-current',i===active?'true':'false'));
 const [name,color,description]=info[active];
 if($('name').textContent!==name){
  $('detail').style.setProperty('--ink',color);
  $('kind').textContent=active?'Part '+String(active).padStart(2,'0'):'Cell boundary';
  $('name').textContent=name;$('description').textContent=description;
  $('tag').style.setProperty('--ink',color);$('tag').lastElementChild.textContent=name;
 }
}
function seek(n,label=n,snap=false){step=n;active=label;age=0;reveal(n,snap);ui();}
function select(n,point){playing=false;seek(Math.max(n,step),n);aim(n,point);}
function section(on){cut=on;$('cut').setAttribute('aria-checked',String(on));slice(on);ui();}
function rebuild(){playing=true;section(false);aim(0);seek(0,0,true);}
function stopDemo(){demo=false;}
document.addEventListener('pointerdown',stopDemo,{capture:true,passive:true});
document.addEventListener('touchstart',stopDemo,{capture:true,passive:true});
$('play').onclick=()=>{stopDemo();if(!playing&&step===last){rebuild();return;}playing=!playing;if(playing)aim(0);ui();};
$('reset').onclick=()=>{stopDemo();rebuild();};
$('cut').onclick=()=>{stopDemo();section(!cut);};
$('build').oninput=e=>{stopDemo();playing=false;aim(0);seek(+e.target.value,+e.target.value,true);};
ui();
function advance(dt){
 if(demo){
  clock+=dt;if(clock>=20){clock%=20;beat=-1;}
  const next=clock<10?0:clock<13?1:clock<16.5?2:clock<18?3:4;
  if(next!==beat){
   beat=next;
   if(beat===0)rebuild();
   if(beat===1){playing=false;seek(last,last,true);aim(0);}
   if(beat===2){section(true);select(7);}
   if(beat===3){section(false);aim(0);seek(4,4,true);}
   if(beat===4){seek(last,last,true);aim(0);}
  }
 }
 if(playing&&step<last){
  age+=dt;
  if(age>(demo?.84:step===0?1.45:2.15)){
   if(step===last-1)playing=false;
   seek(step+1);
  }
 }
}
function model(){
 const T=THREE,canvas=$('view'),scene=new T.Scene();
 const renderer=new T.WebGLRenderer({canvas,alpha:true,antialias:true});
 renderer.setPixelRatio(Math.min(devicePixelRatio||1,1.65));renderer.localClippingEnabled=true;
 const camera=new T.PerspectiveCamera(37,1,.1,60),V=(x,y,z)=>new T.Vector3(x,y,z);
 const hemi=new T.HemisphereLight(0xf0eaff,0x655380,1.95);
 const light=new T.DirectionalLight(0xffedf5,2.55),rim=new T.DirectionalLight(0x8adeff,2.45);
 light.position.set(-4,6,5);rim.position.set(4,-1,-5);scene.add(hemi,light,rim);
 const plane=new T.Plane(V(0,0,-1),20),clips=[plane],items=[];
 let seed=9821;
 function random(){seed=(Math.imul(seed,1664525)+1013904223)>>>0;return seed/4294967296;}
 const material=(color,opts={})=>new T.MeshStandardMaterial({color,emissive:color,emissiveIntensity:.12,roughness:.58,clippingPlanes:clips,...opts});
 const sphere=new T.SphereGeometry(1,32,22),bead=new T.SphereGeometry(1,11,8);
 function mesh(group,geometry,mat,xyz=[0,0,0]){
  const obj=new T.Mesh(geometry,mat);obj.position.set(...xyz);group.add(obj);return obj;
 }
 function ball(group,mat,xyz,size,geometry=sphere){
  const obj=mesh(group,geometry,mat,xyz);
  if(Array.isArray(size))obj.scale.set(...size);else obj.scale.setScalar(size);return obj;
 }
 function tube(group,mat,pts,width,closed=false){
  const path=new T.CatmullRomCurve3(pts.map(p=>V(...p)),closed);
  mesh(group,new T.TubeGeometry(path,Math.max(24,pts.length*4),width,7,closed),mat);
 }
 function group(i,xyz){
  const g=new T.Group();g.position.set(...xyz);g.userData.part=i;scene.add(g);
  items[i]={g,origin:g.position.clone(),progress:i?0:1};return g;
 }
 function cloud(g,xyz,mat){
  const geo=new T.BufferGeometry();geo.setAttribute('position',new T.Float32BufferAttribute(xyz,3));g.add(new T.Points(geo,mat));
 }
 const TAU=Math.PI*2;
 function boundary(x,y,z){
  const a=Math.atan2(z,x),f=1+.049*Math.sin(3*a+.52)+.034*Math.cos(2*a-1)+.022*Math.sin(5*a+3*y);
  return [3.42*x*f+.16*y,2.53*y*(1+.04*x-.027*z),2.43*z*f];
 }
 const membrane=group(0,[0,0,0]);
 const shell=new T.SphereGeometry(1,72,48),positions=shell.attributes.position;
 for(let i=0;i<positions.count;i++)positions.setXYZ(i,...boundary(positions.getX(i),positions.getY(i),positions.getZ(i)));
 shell.computeVertexNormals();
 for(const side of [T.BackSide,T.FrontSide]){
  const mat=new T.ShaderMaterial({
   side,transparent:true,depthWrite:false,clipping:true,clippingPlanes:clips,
   uniforms:{face:{value:side===T.FrontSide?1:.49}},
   vertexShader:'#include <clipping_planes_pars_vertex>\n varying vec3 n; varying vec3 eye; varying vec3 pos; void main(){pos=position; vec4 mvPosition=modelViewMatrix*vec4(position,1.); eye=-mvPosition.xyz; n=normalMatrix*normal;gl_Position=projectionMatrix*mvPosition;\n#include <clipping_planes_vertex>\n}',
   fragmentShader:'#include <clipping_planes_pars_fragment>\n varying vec3 n; varying vec3 eye; varying vec3 pos;uniform float face;void main(){\n#include <clipping_planes_fragment>\n float f=pow(1.-abs(dot(normalize(n),normalize(eye))),2.7);float ribbon=.5+.5*sin(pos.x*1.3+pos.y*2.+pos.z);vec3 c=mix(vec3(.48,.49,.97),vec3(.36,.96,.84),ribbon);float grains=.5+.5*sin(pos.x*30.+sin(pos.y*16.)*3.)*sin(pos.y*26.+pos.z*16.);gl_FragColor=vec4(c,.018+f*(.51+.13*grains)*face);}'
  });
  mesh(membrane,shell,mat).renderOrder=side===T.FrontSide?8:-2;
 }
 const sectionRim=new T.Group();membrane.add(sectionRim);sectionRim.visible=false;
 tube(sectionRim,new T.MeshBasicMaterial({color:0x91f3dd}),Array.from({length:70},(_,i)=>{
  const a=i/70*TAU;return boundary(Math.cos(a),Math.sin(a),0);
 }),.021,true);
 const grains=[];
 for(let i=0;i<440;i++){
  const h=random()*2-1,a=random()*TAU,r=Math.sqrt(1-h*h);grains.push(...boundary(r*Math.cos(a),h,r*Math.sin(a)));
 }
 cloud(membrane,grains,new T.PointsMaterial({color:0xaceded,size:.036,transparent:true,opacity:.36,depthWrite:false,clippingPlanes:clips}));
 const stars=[];
 for(let i=0;i<210;i++)stars.push((random()-.5)*24,(random()-.5)*16,-5-random()*9);
 cloud(scene,stars,new T.PointsMaterial({color:0xacbaf3,size:.038,transparent:true,opacity:.42,depthWrite:false}));
 const nucleus=group(1,[-.68,.34,-.13]);
 ball(nucleus,material(0x9865db,{transparent:true,opacity:.46,depthWrite:false}),[0,0,0],[1.08,.96,.94]).renderOrder=4;
 ball(nucleus,material(0x6841a1,{transparent:true,opacity:.38,depthWrite:false}),[-.06,-.055,-.07],[.89,.80,.77]);
 const pores=material(0xd5b6ff);
 for(let i=0;i<77;i++){
  const y=random()*2-1,a=random()*TAU,r=Math.sqrt(1-y*y);ball(nucleus,pores,[1.07*r*Math.cos(a),.955*y,.933*r*Math.sin(a)],.022+random()*.019,bead);
 }
 const dna=material(0xbc87fc,{transparent:true,opacity:.59,depthWrite:false});
 for(let k=0;k<5;k++)tube(nucleus,dna,Array.from({length:16},(_,i)=>{
  const a=i*.47+k*1.25;return [.66*Math.cos(a)*Math.sin(i*.2+k),.65*Math.sin(a),.49*Math.cos(a+k)-.18];
 }),.017);
 const nucleolus=group(2,[-.55,.43,.09]);
 ball(nucleolus,material(0xdc63ac),[0,0,0],[.38,.35,.32]);
 const flecks=material(0xfa9bcd);
 for(let i=0;i<38;i++){
  const y=random()*2-1,a=random()*TAU,r=Math.sqrt(1-y*y);ball(nucleolus,flecks,[.37*r*Math.cos(a),.345*y,.315*r*Math.sin(a)],.021+random()*.015,bead);
 }
 const rough=group(3,[-.83,-.39,-.12]),ribosomes=group(5,[-.83,-.39,-.12]);
 const rib=material(0xf6cc88);
 function ribbon(g,mat,spine,width,dots=0){
  const path=new T.CatmullRomCurve3(spine.map(p=>V(...p))),n=39,m=14,p=[],ix=[];
  for(let i=0;i<=n;i++){
   const t=i/n,c=path.getPoint(t),d=path.getTangent(t),normal=V(-d.z,0,d.x).normalize();
   const taper=.12+.88*Math.pow(Math.sin(Math.PI*t),.48);
   for(let j=0;j<=m;j++){
    const a=j/m*TAU,off=Math.cos(a)*width*taper;
    p.push(c.x+normal.x*off,c.y+Math.sin(a)*.067*taper,c.z+normal.z*off);
    if(i<n&&j<m){const q=i*(m+1)+j;ix.push(q,q+1,q+m+1,q+1,q+m+2,q+m+1);}
   }
  }
  const geo=new T.BufferGeometry();geo.setAttribute('position',new T.Float32BufferAttribute(p,3));geo.setIndex(ix);geo.computeVertexNormals();mesh(g,geo,mat);
  for(let i=0;i<dots;i++){
   const p=path.getPoint(.09+random()*.82),j=(random()-.5)*width;
   ball(ribosomes,rib,[p.x+j,p.y+.071,p.z+j],.032+random()*.018,bead);
  }
 }
 const roughMat=material(0x587fd7,{side:T.DoubleSide});
 for(let k=0;k<5;k++)ribbon(rough,roughMat,Array.from({length:14},(_,i)=>{
  const a=i/13*3.77-.71+k*.12,r=.77+k*.16+.095*Math.cos(i*1.1+k);
  return [.18+r*Math.cos(a),.45-k*.217+.108*Math.sin(i*.71+k*.8),r*Math.sin(a)-.35];
 }),.2+k*.009,14);
 const smooth=group(4,[.78,1.03,-.22]),smoothMat=material(0x3cb8cf);
 for(let k=0;k<7;k++)tube(smooth,smoothMat,Array.from({length:16},(_,i)=>{
  const t=i/15,a=t*5.8+k*.65;
  return [t*1.37-.69+.13*Math.cos(a*2),.29*Math.sin(a)+.072*(k-3),.43*Math.cos(a)+.11*Math.sin(a*2+k)];
 }),.049+k%3*.012);
 for(let i=0;i<53;i++){
  const x=(random()-.5)*5.3,y=(random()-.5)*3.8,z=(random()-.5)*3.6;
  if(x*x/8+y*y/4.6+z*z/3.9<1&&(x+.68)**2+(y-.34)**2+z*z>1.19)ball(ribosomes,rib,[x+.83,y+.39,z+.12],.028+random()*.024,bead);
 }
 const golgi=group(6,[1.39,-.48,.43]);golgi.rotation.set(.22,-.53,-.27);
 const pinks=[0xb33d83,0xd35191,0xe8669b,0xf47ba8,0xf998b7,0xffb6c8].map(c=>material(c,{side:T.DoubleSide}));
 for(let k=0;k<6;k++)ribbon(golgi,pinks[k],Array.from({length:15},(_,i)=>{
  const t=i/7-1;return [t*(1.01-k*.073),.161*k-.49-(.18+k*.018)*t*t,.42*t*t-.32+k*.047];
 }),.27-k*.023);
 for(let i=0;i<8;i++)ball(golgi,pinks[i%6],[.71+random()*.44,-.57+random()*.83,(random()-.5)*.65],.04+random()*.04,bead);
 const mito=group(7,[-2.02,1.08,-.26]);
 const orange=material(0xf67d51),fold=material(0xffc27e),coat=material(0xff9770,{transparent:true,opacity:.33,depthWrite:false});
 function bean(xyz,rotation,size){
  const g=new T.Group();g.position.set(...xyz);g.rotation.set(...rotation);g.scale.setScalar(size);mito.add(g);
  ball(g,coat,[0,0,0],[.76,.38,.39]).renderOrder=5;
  ball(g,orange,[0,-.058,-.08],[.67,.265,.26]);
  tube(g,fold,Array.from({length:40},(_,i)=>{
   const t=i/39;return [(t-.5)*1.28,.23*Math.sin(t*9.1*Math.PI)*(.74+.26*Math.sin(t*Math.PI)),.10+.17*Math.sin(t*Math.PI)];
  }),.05);
  tube(g,fold,Array.from({length:34},(_,i)=>{const a=i/34*TAU;return [.74*Math.cos(a),.365*Math.sin(a),0];}),.024,true);
 }
 bean([0,0,0],[.2,.35,.77],1);
 bean([3.9,.06,-.61],[.25,-.51,-.56],.85);
 bean([1.01,-2.65,-.68],[-.15,.31,-.31],1.02);
 bean([4.45,-1.93,-.57],[.57,.62,.88],.64);
 const lys=group(8,[-2.37,-.59,.35]),violet=material(0xa85dc8),crumb=material(0xe6a3ef);
 for(const [x,y,z,r] of [[0,0,0,.29],[2.22,1.92,-1.13,.22],[3.38,-1.06,-.41,.24],[4.84,.95,-.82,.19]]){
  ball(lys,violet,[x,y,z],r);
  for(let i=0;i<17;i++){const h=random()*2-1,a=random()*TAU,s=Math.sqrt(1-h*h);ball(lys,crumb,[x+r*s*Math.cos(a),y+r*h,z+r*s*Math.sin(a)],.022+random()*.014,bead);}
 }
 const ves=group(9,[2.36,.24,-.34]),green=material(0x63d7b1,{transparent:true,opacity:.77,roughness:.31});
 for(const [x,y,z,r] of [[0,0,0,.21],[.35,-.34,.36,.14],[-.48,.15,1,.14],[-.36,-1.51,.49,.14],[-2.61,1.23,.79,.17],[-4.41,-1.24,-.42,.18],[-4.72,.58,.10,.12],[-4.03,-.47,1.36,.12],[-1.41,-1.79,-.54,.19],[-.12,1.06,.08,.10]])ball(ves,green,[x,y,z],r);
 const centr=group(10,[.55,-1.32,.08]),gold=material(0xd8913d),tips=material(0xffd27c);
 for(const [x,y,z,rx,rz] of [[0,0,0,.61,.39],[.37,.19,-.29,-.94,1.25]]){
  const g=new T.Group();g.position.set(x,y,z);g.rotation.set(rx,0,rz);centr.add(g);
  for(let i=0;i<9;i++){
   const a=i/9*TAU,dx=.13*Math.cos(a),dz=.13*Math.sin(a);
   mesh(g,new T.CylinderGeometry(.028,.028,.50,8),gold,[dx,0,dz]);
   ball(g,tips,[dx,.251,dz],.031,bead);ball(g,tips,[dx,-.251,dz],.031,bead);
  }
 }
 const cyt=group(11,[0,0,0]);
 const fibers=[0x527cbe,0x6aa8cd,0x6fc8ca].map(color=>new T.MeshBasicMaterial({color,transparent:true,opacity:.46,depthWrite:false,clippingPlanes:clips}));
 for(let i=0;i<23;i++){
  const a=i/23*TAU,b=a+.98+random()*1.42;
  tube(cyt,fibers[i%3],Array.from({length:13},(_,j)=>{
   const t=j/12,q=a+(b-a)*t,r=.36+.57*Math.abs(t*2-1);
   return [3.15*r*Math.cos(q)+.14*Math.sin(j*.72+i),2.32*r*Math.sin(q),(i%4===0?.60:-1)*(.24+1.13*Math.sin(t*Math.PI))+.26*Math.cos(t*6+i)];
  }),.014+i%3*.004);
 }
 const reduced=matchMedia('(prefers-reduced-motion: reduce)');
 const mobile=matchMedia('(max-width:680px)');
 const anchor=V(0,0,0),goal=V(0,0,0),projected=V(0,0,0);
 let yaw=.13,pitch=.11,z=1,turn=yaw,tilt=pitch,zoom=1,fit=1,zoomed=false,time=0;
 const clamp=(n,min,max)=>Math.min(max,Math.max(min,n));
 reveal=(n,snap)=>items.forEach(({g},i)=>{
  g.visible=i<=n;
  if(i>n)items[i].progress=0;else if(snap)items[i].progress=1;
 });
 aim=(n,point)=>{
  zoomed=Boolean(n);
  if(n){
   goal.copy(point||items[n].g.position).multiplyScalar(.88);
   if(cut)goal.z=Math.min(goal.z,-.10);
   zoom=.65;
  }else{goal.set(0,0,0);zoom=1;turn=cut?0:.13;tilt=cut?.055:.11;}
 };
 slice=on=>{
  plane.constant=on?0:20;sectionRim.visible=on;
  if(on)aim(0);
 };
 function resize(){
  const w=canvas.clientWidth,h=canvas.clientHeight;
  if(!w||!h)return;
  renderer.setSize(w,h,false);camera.aspect=w/h;camera.updateProjectionMatrix();
  fit=Math.max(1.04,(mobile.matches?1.39:1.35)/camera.aspect);
 }
 new ResizeObserver(resize).observe(canvas);resize();reveal(0,true);
 const pointers=new Map(),ray=new T.Raycaster(),cursor=new T.Vector2();
 let dragged=false,pinch=0;
 function pick(e){
  const r=canvas.getBoundingClientRect();
  cursor.set(2*(e.clientX-r.left)/r.width-1,1-2*(e.clientY-r.top)/r.height);
  ray.setFromCamera(cursor,camera);
  const targets=items.slice(1,step+1).filter(o=>o.progress>.35).map(o=>o.g);
  const hits=ray.intersectObjects(targets,true);
  for(const hit of hits){
   if(cut&&hit.point.z>.001)continue;
   let owner=hit.object;while(owner&&owner.userData.part===undefined)owner=owner.parent;
   if(owner){select(owner.userData.part,hit.point);return;}
  }
  if(ray.intersectObject(membrane,true).some(hit=>hit.object.isMesh&&(!cut||hit.point.z<=.001)))select(0);
 }
 function span(){const [a,b]=pointers.values();return Math.hypot(a.x-b.x,a.y-b.y);}
 canvas.onpointerdown=e=>{
  if(e.button>0)return;
  if(!pointers.size)dragged=false;
  pointers.set(e.pointerId,{x:e.clientX,y:e.clientY,sx:e.clientX,sy:e.clientY});
  canvas.setPointerCapture(e.pointerId);
  if(pointers.size===2){dragged=true;pinch=span();}
 };
 canvas.onpointermove=e=>{
  const p=pointers.get(e.pointerId);if(!p)return;
  const dx=e.clientX-p.x,dy=e.clientY-p.y;p.x=e.clientX;p.y=e.clientY;
  if(pointers.size===2){
   const next=span();if(pinch>0&&next>0)zoom=clamp(zoom*pinch/next,.33,1.8);pinch=next;
  }else if(pointers.size===1){
   if(Math.hypot(p.x-p.sx,p.y-p.sy)>5)dragged=true;
   if(dragged){turn-=dx*.009;tilt=clamp(tilt+dy*.009,-1.25,1.25);}
  }
 };
 canvas.onpointerup=e=>{if(pointers.delete(e.pointerId)&&!pointers.size&&!dragged)pick(e);};
 canvas.onpointercancel=e=>{pointers.delete(e.pointerId);dragged=true;};
 canvas.onlostpointercapture=e=>pointers.delete(e.pointerId);
 canvas.addEventListener('wheel',e=>{e.preventDefault();stopDemo();zoom=clamp(zoom*Math.exp(clamp(e.deltaY,-120,120)*.0023),.33,1.8);},{passive:false});
 function placeLabel(){
  const node=$('tag'),w=canvas.clientWidth,h=canvas.clientHeight;
  if(active)projected.copy(items[active].g.position);else projected.set(2.51,1.44,0);
  if(cut)projected.z=Math.min(-.05,projected.z);
  projected.project(camera);
  const x=(projected.x+1)*w/2,y=(1-projected.y)*h/2;
  node.hidden=x<8||x>w-8||y<79||y>h-41||projected.z>1;
  if(node.hidden)return;
  node.style.left=clamp(x+9,7,w-node.offsetWidth-8)+'px';
  node.style.top=y+'px';
 }
 let prev=performance.now();
 function frame(now){
  const dt=Math.min(.05,(now-prev)/1000);prev=now;time+=dt;
  advance(dt);
  const motion=reduced.matches?0:1;
  items.forEach((item,i)=>{
   if(!item.g.visible)return;
   item.progress=Math.min(1,item.progress+dt*2.2);
   const pulse=1+motion*.014*Math.sin(time*1.7+i);
   const grow=.45+.55*Math.sin(item.progress*Math.PI/2);
   item.g.scale.setScalar(i===0&&cut?1:grow*pulse);
   if(i){
    const phase=i===5?3:i,amp=(i===3||i===5?.022:.048)*motion,p=item.origin;
    item.g.position.set(p.x+amp*Math.sin(time*.87+phase*1.48),p.y+amp*Math.cos(time*1.14+phase),p.z+amp*Math.sin(time*1.18+phase*.72));
   }
  });
  if(demo&&beat===1)turn+=dt*.45;
  if(demo&&beat===4)turn+=dt*.20;
  const mix=1-Math.exp(-5.6*dt);
  yaw+=(turn-yaw)*mix;pitch+=(tilt-pitch)*mix;z+=(zoom-z)*mix;anchor.lerp(goal,mix);
  const distance=11.5*fit*z;
  const angle=yaw+(!zoomed&&!cut&&!pointers.size?motion*.07*Math.sin(time*.49):0);
  camera.position.set(anchor.x+distance*Math.sin(angle)*Math.cos(pitch),anchor.y+distance*Math.sin(pitch),anchor.z+distance*Math.cos(angle)*Math.cos(pitch));
  camera.lookAt(anchor.x,anchor.y+.12*fit,anchor.z);
  renderer.render(scene,camera);placeLabel();requestAnimationFrame(frame);
 }
 requestAnimationFrame(frame);
}
try{model();}catch(error){
 const note=document.createElement('p');note.className='error';
 note.textContent='The 3D view could not start. Tap a part in the guide to learn what it does.';
 $('view').after(note);playing=false;demo=false;$('cut').disabled=true;$('reset').disabled=true;$('play').disabled=true;ui();
}
</script>
</body>
</html>