Back to the app
The code
Jelly Orange Slice by Claude Opus 5.5
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.)<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no">
<title>Jelly Orange Slice</title>
<style>
html,body{margin:0;height:100%;overflow:hidden;background:#2a2320;font-family:system-ui,-apple-system,Segoe UI,Roboto,sans-serif;color:#fff}
canvas{display:block;touch-action:none}
#ui{position:fixed;left:0;right:0;bottom:0;display:flex;flex-wrap:wrap;gap:12px;justify-content:center;align-items:center;padding:14px 12px calc(14px + env(safe-area-inset-bottom));background:linear-gradient(transparent,rgba(0,0,0,.55))}
button{font-size:18px;font-weight:700;min-width:120px;min-height:54px;border:0;border-radius:16px;padding:0 20px;color:#3a1c00;background:linear-gradient(#ffc15a,#ff8a1c);box-shadow:0 4px 12px rgba(0,0,0,.35);cursor:pointer}
button:active{transform:scale(.96)}
button.alt{background:linear-gradient(#fff,#ddd);color:#333}
label{display:flex;align-items:center;gap:10px;font-size:17px;background:rgba(0,0,0,.35);padding:8px 14px;border-radius:16px;min-height:38px}
input[type=range]{width:160px;height:36px;accent-color:#ff8a1c}
#hint{position:fixed;top:12px;left:0;right:0;text-align:center;font-size:15px;opacity:.8;pointer-events:none;text-shadow:0 1px 3px #000}
</style>
</head>
<body>
<div id="hint">Poke the jelly · drag it to stretch · drag the background to look around</div>
<div id="ui">
<button id="squeeze">Squeeze</button>
<label>Firmness <input id="firm" type="range" min="0" max="1" step="0.01" value="0.5"></label>
<button id="reset" class="alt">Reset</button>
</div>
<script type="importmap">{"imports":{"three":"https://cdn.jsdelivr.net/npm/three@0.160.0/build/three.module.js"}}</script>
<script type="module">
import * as THREE from 'three';
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.1;
renderer.outputColorSpace=THREE.SRGBColorSpace;
document.body.appendChild(renderer.domElement);
const scene=new THREE.Scene();
scene.background=new THREE.Color(0x3b302a);
const camera=new THREE.PerspectiveCamera(40,innerWidth/innerHeight,0.05,50);
const camT=new THREE.Vector3(0,0.15,0.35);
let theta=0.35,phi=0.95,camDist=4.4;
function placeCam(){const a=innerWidth/innerHeight;const d=camDist*(a<0.8?1.45:1);camera.position.set(camT.x+d*Math.sin(phi)*Math.sin(theta),camT.y+d*Math.cos(phi),camT.z+d*Math.sin(phi)*Math.cos(theta));camera.lookAt(camT);}
placeCam();
addEventListener('resize',()=>{renderer.setSize(innerWidth,innerHeight);camera.aspect=innerWidth/innerHeight;camera.updateProjectionMatrix();placeCam();});
// lights
scene.add(new THREE.HemisphereLight(0xfff4e6,0x40302a,0.7));
const key=new THREE.DirectionalLight(0xffffff,2.2);key.position.set(2.5,5,3);key.castShadow=true;
key.shadow.mapSize.set(1024,1024);Object.assign(key.shadow.camera,{left:-3,right:3,top:3,bottom:-3,near:1,far:15});key.shadow.radius=4;scene.add(key);
const back=new THREE.PointLight(0xffd29a,6,8,1.5);back.position.set(-0.6,0.9,-1.6);scene.add(back);
const rim=new THREE.DirectionalLight(0xffe0b0,1.2);rim.position.set(-3,2,-3);scene.add(rim);
// table + plate
const table=new THREE.Mesh(new THREE.CircleGeometry(12,48),new THREE.MeshStandardMaterial({color:0x6b4a35,roughness:0.8}));
table.rotation.x=-Math.PI/2;table.position.y=-0.09;table.receiveShadow=true;scene.add(table);
const pp=[];pp.push(new THREE.Vector2(0,-0.09));pp.push(new THREE.Vector2(1.3,-0.09));pp.push(new THREE.Vector2(1.45,-0.07));
pp.push(new THREE.Vector2(1.5,-0.01));pp.push(new THREE.Vector2(1.6,0));pp.push(new THREE.Vector2(2.05,0.09));pp.push(new THREE.Vector2(2.15,0.1));pp.push(new THREE.Vector2(2.1,0.07));pp.push(new THREE.Vector2(1.6,-0.02));pp.push(new THREE.Vector2(1.5,-0.03));pp.push(new THREE.Vector2(0,-0.03));
const plateG=new THREE.LatheGeometry(pp,80);
const plate=new THREE.Mesh(plateG,new THREE.MeshPhysicalMaterial({color:0xffffff,roughness:0.18,clearcoat:1,clearcoatRoughness:0.08,side:THREE.DoubleSide}));
plate.receiveShadow=true;plate.castShadow=true;scene.add(plate);
const plateTop=new THREE.Mesh(new THREE.CircleGeometry(1.5,64),plate.material);plateTop.rotation.x=-Math.PI/2;plateTop.position.y=0;plateTop.receiveShadow=true;scene.add(plateTop);
// ---------- textures ----------
function rnd(a,b){return a+Math.random()*(b-a);}
function pulpTexture(){
const W=1024,H=512,c=document.createElement('canvas');c.width=W;c.height=H;const g=c.getContext('2d');
const C={x:512,y:512},R=508;
g.fillStyle='#ff7a00';g.fillRect(0,0,W,H);
g.beginPath();g.arc(C.x,C.y,R,Math.PI,0);g.fillStyle='#ff8c10';g.fill();
for(let i=0;i<1400;i++){const a=Math.random()*Math.PI,r=R*rnd(0.955,1);g.fillStyle=Math.random()<.5?'rgba(255,170,60,.5)':'rgba(200,80,0,.45)';g.beginPath();g.arc(C.x+Math.cos(a)*r,C.y-Math.sin(a)*r,rnd(1,2.5),0,7);g.fill();}
g.beginPath();g.arc(C.x,C.y,R*0.952,Math.PI,0);g.fillStyle='#fff2d8';g.fill();
g.beginPath();g.arc(C.x,C.y,R*0.92,Math.PI,0);g.fillStyle='#fbe6c0';g.fill();
const N=6,gap=0.018;
for(let s=0;s<N;s++){
const a0=s*Math.PI/N+gap,a1=(s+1)*Math.PI/N-gap,r0=R*0.07,r1=R*0.9;
g.save();g.beginPath();
g.moveTo(C.x+Math.cos(a0)*r0,C.y-Math.sin(a0)*r0);
g.arc(C.x,C.y,r1,-a0,-a1,true);
g.lineTo(C.x+Math.cos(a1)*r0,C.y-Math.sin(a1)*r0);g.closePath();
const gr=g.createRadialGradient(C.x,C.y,r0,C.x,C.y,r1);gr.addColorStop(0,'#ffc65a');gr.addColorStop(0.5,'#ffa020');gr.addColorStop(1,'#ff8400');
g.fillStyle=gr;g.fill();g.clip();
for(let i=0;i<520;i++){
const a=rnd(a0,a1),r=Math.sqrt(rnd(r0*r0,r1*r1)),x=C.x+Math.cos(a)*r,y=C.y-Math.sin(a)*r;
const L=rnd(10,26)*(0.5+r/r1),w=rnd(2.5,5.5);
g.save();g.translate(x,y);g.rotate(-a);
g.beginPath();g.ellipse(0,0,L,w,0,0,7);
g.fillStyle=`hsla(${rnd(26,40)},100%,${rnd(50,64)}%,.75)`;g.fill();
g.strokeStyle='rgba(255,230,170,.55)';g.lineWidth=1;g.stroke();
g.beginPath();g.ellipse(-L*.2,-w*.35,L*.45,w*.25,0,0,7);g.fillStyle='rgba(255,255,230,.45)';g.fill();
g.restore();
}
g.restore();
g.strokeStyle='rgba(255,245,225,.95)';g.lineWidth=5;g.beginPath();g.moveTo(C.x+Math.cos(a0-gap)*r0*0.5,C.y-Math.sin(a0-gap)*r0*0.5);g.lineTo(C.x+Math.cos(a0-gap)*R*0.92,C.y-Math.sin(a0-gap)*R*0.92);g.stroke();
}
g.beginPath();g.arc(C.x,C.y,R*0.075,Math.PI,0);g.fillStyle='#fff4dc';g.fill();
const t=new THREE.CanvasTexture(c);t.colorSpace=THREE.SRGBColorSpace;t.anisotropy=8;return t;
}
function peelTexture(){
const c=document.createElement('canvas');c.width=1024;c.height=128;const g=c.getContext('2d');
const gr=g.createLinearGradient(0,0,0,128);gr.addColorStop(0,'#ffe6b8');gr.addColorStop(0.1,'#ff9a20');gr.addColorStop(0.5,'#ff8000');gr.addColorStop(0.9,'#ff9a20');gr.addColorStop(1,'#ffe6b8');
g.fillStyle=gr;g.fillRect(0,0,1024,128);
for(let i=0;i<3000;i++){g.fillStyle=Math.random()<.5?'rgba(255,190,90,.45)':'rgba(190,70,0,.35)';g.beginPath();g.arc(Math.random()*1024,rnd(12,116),rnd(.8,2.4),0,7);g.fill();}
const t=new THREE.CanvasTexture(c);t.colorSpace=THREE.SRGBColorSpace;return t;
}
function cutTexture(){
const W=1024,H=128,c=document.createElement('canvas');c.width=W;c.height=H;const g=c.getContext('2d');
const gr=g.createLinearGradient(0,0,0,H);gr.addColorStop(0,'#ffb030');gr.addColorStop(1,'#ff8a10');g.fillStyle=gr;g.fillRect(0,0,W,H);
for(let i=0;i<1600;i++){const x=Math.random()*W,y=Math.random()*H;g.save();g.translate(x,y);g.rotate(rnd(-.25,.25));g.beginPath();g.ellipse(0,0,rnd(8,20),rnd(2,4),0,0,7);g.fillStyle=`hsla(${rnd(26,40)},100%,${rnd(52,66)}%,.7)`;g.fill();g.strokeStyle='rgba(255,235,180,.5)';g.stroke();g.restore();}
g.fillStyle='#ff8000';g.fillRect(0,0,22,H);g.fillRect(W-22,0,22,H);
g.fillStyle='#fff0d2';g.fillRect(22,0,24,H);g.fillRect(W-46,0,24,H);g.fillRect(W/2-14,0,28,H);
const t=new THREE.CanvasTexture(c);t.colorSpace=THREE.SRGBColorSpace;return t;
}
// ---------- slice geometry ----------
const HT=0.34;
const yTop=r=>0.012+HT*(0.86+0.14*(1-r*r));
const pos=[],uv=[],idx=[];
function V(x,y,z,u,v){pos.push(x,y,z);uv.push(u,v);return pos.length/3-1;}
const NR=18,NA=64,NH=5,NX=40;
const groups=[];
function grid(nu,nv,fn,flip){
const base=pos.length/3;
for(let i=0;i<=nu;i++)for(let j=0;j<=nv;j++)fn(i,j);
const s=idx.length;
for(let i=0;i<nu;i++)for(let j=0;j<nv;j++){
const a=base+i*(nv+1)+j,b=a+nv+1,c=a+1,d=b+1;
if(flip)idx.push(a,c,b,b,c,d);else idx.push(a,b,c,b,d,c);
}
return [s,idx.length-s];
}
// top: (a,c,b) order is outward
let g0=grid(NR,NA,(i,j)=>{const r=i/NR,a=Math.PI*j/NA,x=r*Math.cos(a),z=r*Math.sin(a);V(x,yTop(r),z,(x+1)/2,z);},true);
let g1=grid(NR,NA,(i,j)=>{const r=i/NR,a=Math.PI*j/NA,x=r*Math.cos(a),z=r*Math.sin(a);V(x,0.008,z,(x+1)/2,z);},false);
groups.push([g0[0],g0[1]+g1[1],0]);
let g2=grid(NA,NH,(j,k)=>{const a=Math.PI*j/NA;V(Math.cos(a),0.008+(yTop(1)-0.008)*k/NH,Math.sin(a),j/NA,k/NH);},true);
groups.push([g2[0],g2[1],1]);
let g3=grid(NX,NH,(i,k)=>{const x=-1+2*i/NX;V(x,0.008+(yTop(Math.abs(x))-0.008)*k/NH,0,i/NX,k/NH);},true);
groups.push([g3[0],g3[1],2]);
const geo=new THREE.BufferGeometry();
const posAttr=new THREE.BufferAttribute(new Float32Array(pos),3);posAttr.setUsage(THREE.DynamicDrawUsage);
geo.setAttribute('position',posAttr);geo.setAttribute('uv',new THREE.Float32BufferAttribute(uv,2));geo.setIndex(idx);
groups.forEach(g=>geo.addGroup(g[0],g[1],g[2]));
geo.computeVertexNormals();
const pulpMap=pulpTexture();
const matCommon={roughness:0.12,clearcoat:1,clearcoatRoughness:0.05,ior:1.36,thickness:0.5,attenuationColor:new THREE.Color(0xff7a00),attenuationDistance:0.6,emissive:new THREE.Color(0xff7000),side:THREE.DoubleSide};
const pulpMat=new THREE.MeshPhysicalMaterial({...matCommon,map:pulpMap,emissiveMap:pulpMap,emissiveIntensity:0.32,transmission:0.55,specularIntensity:1});
const peelMap=peelTexture();
const peelMat=new THREE.MeshPhysicalMaterial({...matCommon,map:peelMap,emissiveMap:peelMap,emissiveIntensity:0.18,transmission:0.25,roughness:0.28});
const cutMap=cutTexture();
const cutMat=new THREE.MeshPhysicalMaterial({...matCommon,map:cutMap,emissiveMap:cutMap,emissiveIntensity:0.35,transmission:0.55});
const slice=new THREE.Mesh(geo,[pulpMat,peelMat,cutMat]);
slice.castShadow=true;slice.frustumCulled=false;scene.add(slice);
// ---------- soft body lattice ----------
const nx=13,ny=4,nz=7,X0=-1.06,X1=1.06,Y0=0,Y1=0.44,Z0=-0.04,Z1=1.06;
const hx=(X1-X0)/(nx-1),hy=(Y1-Y0)/(ny-1),hz=(Z1-Z0)/(nz-1);
const N=nx*ny*nz,id=(i,j,k)=>(k*ny+j)*nx+i;
const P=new Float32Array(N*3),PP=new Float32Array(N*3),REST=new Float32Array(N*3),Q=new Float32Array(N*3);
for(let k=0;k<nz;k++)for(let j=0;j<ny;j++)for(let i=0;i<nx;i++){const n=id(i,j,k)*3;REST[n]=X0+i*hx;REST[n+1]=Y0+j*hy;REST[n+2]=Z0+k*hz;}
let rc=[0,0,0];for(let n=0;n<N;n++){rc[0]+=REST[n*3];rc[1]+=REST[n*3+1];rc[2]+=REST[n*3+2];}rc=rc.map(v=>v/N);
for(let n=0;n<N;n++)for(let d=0;d<3;d++)Q[n*3+d]=REST[n*3+d]-rc[d];
const springs=[];
for(let k=0;k<nz;k++)for(let j=0;j<ny;j++)for(let i=0;i<nx;i++)
for(let dk=-1;dk<=1;dk++)for(let dj=-1;dj<=1;dj++)for(let di=-1;di<=1;di++){
const a=id(i,j,k);const I=i+di,J=j+dj,K=k+dk;if(I<0||J<0||K<0||I>=nx||J>=ny||K>=nz)continue;const b=id(I,J,K);if(b<=a)continue;
const L=Math.hypot(REST[a*3]-REST[b*3],REST[a*3+1]-REST[b*3+1],REST[a*3+2]-REST[b*3+2]);springs.push(a,b,L);
}
const SP=new Float32Array(springs);const NS=SP.length/3;
// embed mesh vertices
const NV=posAttr.count,EI=new Int32Array(NV*3),EF=new Float32Array(NV*3),base0=new Float32Array(pos);
for(let v=0;v<NV;v++){
const x=base0[v*3],y=base0[v*3+1],z=base0[v*3+2];
let fi=(x-X0)/hx,fj=(y-Y0)/hy,fk=(z-Z0)/hz;
let i=Math.min(nx-2,Math.max(0,Math.floor(fi))),j=Math.min(ny-2,Math.max(0,Math.floor(fj))),k=Math.min(nz-2,Math.max(0,Math.floor(fk)));
EI[v*3]=i;EI[v*3+1]=j;EI[v*3+2]=k;EF[v*3]=fi-i;EF[v*3+1]=fj-j;EF[v*3+2]=fk-k;
}
const rot=new THREE.Quaternion(),m4=new THREE.Matrix4();
function reset(){P.set(REST);PP.set(REST);rot.identity();squeezeT=-1;drops.forEach(d=>scene.remove(d.m));drops.length=0;drag=null;}
// ---------- state ----------
let firm=0.5,squeezeT=-1,drag=null;const drops=[];
const firmEl=document.getElementById('firm');firmEl.oninput=()=>firm=+firmEl.value;
const pokeImp=[];
function extractRotation(A){
const R=new THREE.Matrix4(),te=R.elements,om=new THREE.Vector3(),q2=new THREE.Quaternion();
for(let it=0;it<8;it++){
R.makeRotationFromQuaternion(rot);
const r0=[te[0],te[1],te[2]],r1=[te[4],te[5],te[6]],r2=[te[8],te[9],te[10]];
const a0=A[0],a1=A[1],a2=A[2];
const cr=(u,w)=>[u[1]*w[2]-u[2]*w[1],u[2]*w[0]-u[0]*w[2],u[0]*w[1]-u[1]*w[0]];
const dt=(u,w)=>u[0]*w[0]+u[1]*w[1]+u[2]*w[2];
const c0=cr(r0,a0),c1=cr(r1,a1),c2=cr(r2,a2);
const den=Math.abs(dt(r0,a0)+dt(r1,a1)+dt(r2,a2))+1e-9;
om.set((c0[0]+c1[0]+c2[0])/den,(c0[1]+c1[1]+c2[1])/den,(c0[2]+c1[2]+c2[2])/den);
const w=om.length();if(w<1e-9)break;
q2.setFromAxisAngle(om.multiplyScalar(1/w),w);rot.premultiply(q2).normalize();
}
}
const G=-9.8,DT=1/120;
function step(){
const damp=0.992-0.004*(1-firm);
for(let n=0;n<N*3;n+=3){
const vx=(P[n]-PP[n])*damp,vy=(P[n+1]-PP[n+1])*damp,vz=(P[n+2]-PP[n+2])*damp;
PP[n]=P[n];PP[n+1]=P[n+1];PP[n+2]=P[n+2];
P[n]+=vx;P[n+1]+=vy+G*DT*DT;P[n+2]+=vz;
}
// drag
if(drag){
for(const g of drag.nodes){const n=g.n*3,s=0.35*g.w;
P[n]+=(drag.P.x+g.ox-P[n])*s;P[n+1]+=(drag.P.y+g.oy-P[n+1])*s;P[n+2]+=(drag.P.z+g.oz-P[n+2])*s;}
}
// springs
const st=0.12+0.55*firm;
for(let it=0;it<5;it++){
for(let s=0;s<NS;s++){
const a=SP[s*3]*3,b=SP[s*3+1]*3,L=SP[s*3+2];
const dx=P[b]-P[a],dy=P[b+1]-P[a+1],dz=P[b+2]-P[a+2];
const d=Math.sqrt(dx*dx+dy*dy+dz*dz)+1e-9,f=(d-L)/d*0.5*st;
P[a]+=dx*f;P[a+1]+=dy*f;P[a+2]+=dz*f;P[b]-=dx*f;P[b+1]-=dy*f;P[b+2]-=dz*f;
}
}
// shape matching
let cx=0,cy=0,cz=0;for(let n=0;n<N*3;n+=3){cx+=P[n];cy+=P[n+1];cz+=P[n+2];}cx/=N;cy/=N;cz/=N;
let sq=0;if(squeezeT>=0){const t=squeezeT;sq=t<0.7?Math.sin(t/0.7*Math.PI/2):t<2.0?1:Math.max(0,1-(t-2.0)/0.6);sq*=0.42;}
const sy=1-sq,sxz=1+sq*0.45;
const A=[[0,0,0],[0,0,0],[0,0,0]];
for(let n=0;n<N*3;n+=3){const dx=P[n]-cx,dy=P[n+1]-cy,dz=P[n+2]-cz,qx=Q[n]*sxz,qy=Q[n+1]*sy,qz=Q[n+2]*sxz;
A[0][0]+=dx*qx;A[0][1]+=dy*qx;A[0][2]+=dz*qx;A[1][0]+=dx*qy;A[1][1]+=dy*qy;A[1][2]+=dz*qy;A[2][0]+=dx*qz;A[2][1]+=dy*qz;A[2][2]+=dz*qz;}
extractRotation(A);
m4.makeRotationFromQuaternion(rot);const e=m4.elements;
const al=0.015+0.09*firm+sq*0.25;
const cyG=cy;
for(let n=0;n<N*3;n+=3){const qx=Q[n]*sxz,qy=Q[n+1]*sy,qz=Q[n+2]*sxz;
const gx=cx+e[0]*qx+e[4]*qy+e[8]*qz,gy=cyG+e[1]*qx+e[5]*qy+e[9]*qz,gz=cz+e[2]*qx+e[6]*qy+e[10]*qz;
P[n]+=(gx-P[n])*al;P[n+1]+=(gy-P[n+1])*al;P[n+2]+=(gz-P[n+2])*al;}
// squeeze press from above
if(sq>0){const top=Y1*(1-sq);for(let n=0;n<N*3;n+=3){if(P[n+1]>top){P[n+1]+=(top-P[n+1])*0.5;}}}
// floor and plate bounds
for(let n=0;n<N*3;n+=3){
if(P[n+1]<0){P[n+1]=0;PP[n]=P[n]-(P[n]-PP[n])*0.4;PP[n+2]=P[n+2]-(P[n+2]-PP[n+2])*0.4;if(PP[n+1]<0)PP[n+1]=0;}
const r=Math.hypot(P[n],P[n+2]);if(r>1.45){P[n]*=1.45/r;P[n+2]*=1.45/r;}
if(P[n+1]>2.2)P[n+1]=2.2;
}
}
function skin(){
const a=posAttr.array;
for(let v=0;v<NV;v++){
const i=EI[v*3],j=EI[v*3+1],k=EI[v*3+2],fx=EF[v*3],fy=EF[v*3+1],fz=EF[v*3+2];
let x=0,y=0,z=0;
for(let c=0;c<8;c++){const di=c&1,dj=(c>>1)&1,dk=(c>>2)&1;
const w=(di?fx:1-fx)*(dj?fy:1-fy)*(dk?fz:1-fz);if(w===0)continue;const n=id(i+di,j+dj,k+dk)*3;
x+=P[n]*w;y+=P[n+1]*w;z+=P[n+2]*w;}
a[v*3]=x;a[v*3+1]=Math.max(0.004,y);a[v*3+2]=z;
}
posAttr.needsUpdate=true;geo.computeVertexNormals();geo.computeBoundingSphere();
}
// ---------- juice drops ----------
const dropGeo=new THREE.SphereGeometry(1,16,10);
const dropMat=new THREE.MeshPhysicalMaterial({color:0xffa020,roughness:0.05,transmission:0.8,thickness:0.2,ior:1.34,clearcoat:1,emissive:0xff6a00,emissiveIntensity:0.25,attenuationColor:new THREE.Color(0xff8000),attenuationDistance:0.3});
function spawnDrop(){
if(drops.length>40)return;
const a=Math.random()<0.75?rnd(0.15,Math.PI-0.15):(Math.random()<.5?0.02:Math.PI-0.02);
// find skinned rim point
const j=Math.round(a/Math.PI*NA),vi=g2[0]?null:null;
const rimBase=(NR+1)*(NA+1)*2;const v=rimBase+j*(NH+1)+Math.floor(rnd(1,NH));
const arr=posAttr.array;const p=new THREE.Vector3(arr[v*3],arr[v*3+1],arr[v*3+2]);
const dir=new THREE.Vector3(Math.cos(a),0,Math.sin(a));
if(Math.random()<0.3){p.set(rnd(-0.8,0.8),arr[v*3+1],-0.01);dir.set(0,0,-1);}
p.addScaledVector(dir,0.03);
const m=new THREE.Mesh(dropGeo,dropMat);const r=rnd(0.028,0.05);m.scale.setScalar(r);m.position.copy(p);m.castShadow=true;scene.add(m);
drops.push({m,r,v:dir.multiplyScalar(rnd(0.15,0.4)).add(new THREE.Vector3(0,rnd(0,0.2),0)),flat:0});
}
function updDrops(dt){
for(const d of drops){
d.v.y+=G*dt*0.6;d.m.position.addScaledVector(d.v,dt);
const p=d.m.position;const r=Math.hypot(p.x,p.z);
if(r<1.0&&p.z>-0.02&&p.y<0.4){p.x*=1.0/(r||1)*1.0;p.z*=1.0/(r||1)*1.0;if(p.z<0)p.z=0;} // pushed out of slice rim
const fy=d.r*(1-d.flat*0.65);
if(p.y<fy){p.y=fy;d.v.y=0;d.v.x*=0.94;d.v.z*=0.94;d.flat=Math.min(1,d.flat+dt*2);}
if(r>1.42){p.x*=1.42/r;p.z*=1.42/r;}
d.m.scale.set(d.r*(1+d.flat*0.9),d.r*(1-d.flat*0.65),d.r*(1+d.flat*0.9));
}
}
let dropClock=0;
// ---------- interaction ----------
const ray=new THREE.Raycaster(),ndc=new THREE.Vector2(),plane=new THREE.Plane();
function grabAt(hit){
const nodes=[];const R=0.5;
for(let n=0;n<N;n++){const dx=P[n*3]-hit.x,dy=P[n*3+1]-hit.y,dz=P[n*3+2]-hit.z,d=Math.hypot(dx,dy,dz);if(d<R)nodes.push({n,w:1-d/R,ox:dx,oy:dy,oz:dz});}
const cd=new THREE.Vector3();camera.getWorldDirection(cd);plane.setFromNormalAndCoplanarPoint(cd,hit);
drag={nodes,P:hit.clone(),hit:hit.clone()};
}
function dragTo(p){const d=p.clone().sub(drag.hit);if(d.length()>1.0)d.setLength(1.0);drag.P.copy(drag.hit).add(d);}
function poke(hit,dir,str){
for(let n=0;n<N;n++){const dx=P[n*3]-hit.x,dy=P[n*3+1]-hit.y,dz=P[n*3+2]-hit.z,d=Math.hypot(dx,dy,dz);if(d<0.55){const w=(1-d/0.55)*str;PP[n*3]-=dir.x*w;PP[n*3+1]-=dir.y*w;PP[n*3+2]-=dir.z*w;}}
}
let ptr=null;
const cvs=renderer.domElement;
function setNdc(e){ndc.set(e.clientX/innerWidth*2-1,-(e.clientY/innerHeight)*2+1);ray.setFromCamera(ndc,camera);}
cvs.addEventListener('pointerdown',e=>{
if(e.isTrusted)stopDemo();
cvs.setPointerCapture(e.pointerId);setNdc(e);
const h=ray.intersectObject(slice,false)[0];
ptr={id:e.pointerId,x:e.clientX,y:e.clientY,t:performance.now(),moved:0,onSlice:!!h,hit:h?h.point.clone():null};
if(h)grabAt(h.point.clone());
});
cvs.addEventListener('pointermove',e=>{
if(!ptr||e.pointerId!==ptr.id)return;
const dx=e.clientX-ptr.x,dy=e.clientY-ptr.y;ptr.moved+=Math.abs(dx)+Math.abs(dy);
if(ptr.onSlice){setNdc(e);const p=new THREE.Vector3();if(ray.ray.intersectPlane(plane,p))dragTo(p);}
else{theta-=dx*0.006;phi=Math.min(1.45,Math.max(0.25,phi-dy*0.005));placeCam();}
ptr.x=e.clientX;ptr.y=e.clientY;
});
function up(e){
if(!ptr||e.pointerId!==ptr.id)return;
if(ptr.onSlice&&ptr.moved<10){drag=null;const dir=ray.ray.direction.clone();poke(ptr.hit,dir,0.05);}
drag=null;ptr=null;
}
cvs.addEventListener('pointerup',up);cvs.addEventListener('pointercancel',up);
cvs.addEventListener('wheel',e=>{camDist=Math.min(8,Math.max(2.5,camDist*(1+e.deltaY*0.001)));placeCam();},{passive:true});
document.getElementById('squeeze').addEventListener('click',e=>{if(e.isTrusted)stopDemo();if(squeezeT<0)squeezeT=0;});
document.getElementById('reset').addEventListener('click',e=>{if(e.isTrusted)stopDemo();reset();});
document.getElementById('ui').addEventListener('pointerdown',e=>{if(e.isTrusted)stopDemo();});
// ---------- demo ----------
let demo=location.hash==='#demo',demoT=0,demoStage=-1;
function stopDemo(){if(demo){demo=false;drag=null;}}
function demoUpdate(dt){
demoT+=dt;if(demoT>20){demoT=0;demoStage=-1;reset();firm=0.5;firmEl.value=0.5;}
const t=demoT;
theta=0.35+Math.sin(t*0.3)*0.35;placeCam();
const at=(s,f)=>{if(t>=s&&demoStage<s){demoStage=s;f();}};
at(1.5,()=>poke(new THREE.Vector3(0.2,0.38,0.5),new THREE.Vector3(0,-1,0),0.06));
at(3,()=>poke(new THREE.Vector3(-0.5,0.36,0.4),new THREE.Vector3(0.2,-1,0),0.06));
at(4.5,()=>grabAt(new THREE.Vector3(0.75,0.3,0.55)));
if(t>4.5&&t<7&&drag){const k=Math.min(1,(t-4.5)/1.8);dragTo(new THREE.Vector3(0.75+0.45*k,0.3+0.5*k,0.55+0.2*k));}
at(7,()=>{drag=null;});
at(10,()=>{if(squeezeT<0)squeezeT=0;});
at(14,()=>{firm=0.1;firmEl.value=0.1;});
at(14.5,()=>poke(new THREE.Vector3(0,0.38,0.45),new THREE.Vector3(0,-1,0),0.07));
at(16.5,()=>{firm=1;firmEl.value=1;});
at(17,()=>poke(new THREE.Vector3(0,0.38,0.45),new THREE.Vector3(0,-1,0),0.07));
at(19,()=>{reset();firm=0.5;firmEl.value=0.5;});
}
// ---------- loop ----------
reset();
let last=performance.now(),acc=0;
function frame(now){
requestAnimationFrame(frame);
let dt=Math.min(0.05,(now-last)/1000);last=now;
if(demo)demoUpdate(dt);
acc+=dt;let s=0;
while(acc>=DT&&s<6){step();acc-=DT;s++;}
if(s===6)acc=0;
if(squeezeT>=0){squeezeT+=dt;
if(squeezeT>0.5&&squeezeT<2.3){dropClock+=dt;while(dropClock>0.16){dropClock-=0.16;spawnDrop();}}
if(squeezeT>2.7)squeezeT=-1;}
updDrops(dt);
skin();
renderer.render(scene,camera);
}
requestAnimationFrame(frame);
</script>
</body>
</html>
