Back to the app
The code
Jelly Orange Slice by GPT-6.1 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.)<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8"><meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1">
<meta name="theme-color" content="#f8f6ef">
<title>Little Citrus — The Jelly Lab</title>
<style>
:root{--ink:#26372e;--orange:#ed7135;--paper:#f8f6ef}*{box-sizing:border-box}html,body{margin:0;width:100%;height:100%;overflow:hidden}body{background:var(--paper);color:var(--ink);font-family:Arial,Helvetica,sans-serif;-webkit-font-smoothing:antialiased}button,input{font:inherit}button{cursor:pointer;-webkit-tap-highlight-color:transparent}button:focus-visible,input:focus-visible{outline:3px solid #ee9b67;outline-offset:5px}#scene{position:absolute;inset:0;touch-action:none;cursor:grab}#scene:active{cursor:grabbing}#scene canvas{display:block;width:100%;height:100%}.header{position:absolute;z-index:2;left:4.3%;right:4.3%;top:34px;display:flex;align-items:center;justify-content:space-between;pointer-events:none}.brand{display:flex;gap:12px;align-items:center;font-size:12px;font-weight:700;letter-spacing:2px}.brand-mark{width:31px;height:31px;border:2px solid var(--orange);border-radius:50%;position:relative;background:repeating-conic-gradient(from 15deg,#f8f6ef 0deg 3deg,#ed7135 3deg 5deg,#f8f6ef 5deg 45deg);transform:rotate(-15deg);box-shadow:inset 0 0 0 4px var(--paper)}.edition{font-size:10px;letter-spacing:2px;color:#777f75;display:flex;align-items:center;gap:12px}.edition i{width:6px;height:6px;border-radius:50%;background:#829276;box-shadow:0 0 0 4px #82927612}.intro{position:absolute;z-index:2;left:7%;top:24%;width:300px;pointer-events:none}.eyebrow{display:flex;align-items:center;gap:10px;font-size:9px;font-weight:bold;letter-spacing:2.3px;color:#a2a48d}.eyebrow:before{content:'';height:1px;width:27px;background:#c4c5b2}h1{font-family:Georgia,'Times New Roman',serif;font-weight:400;font-size:clamp(46px,4.5vw,71px);line-height:1.05;letter-spacing:-3px;margin:25px 0 21px}h1 em{color:var(--orange);font-weight:400}.description{font-size:13px;line-height:1.9;color:#818679;max-width:237px;margin:0}.gesture{margin-top:34px;display:flex;align-items:center;gap:12px;color:#697361;font-size:10px;letter-spacing:.3px}.gesture svg{width:30px;height:30px;stroke:#88917c;fill:none;stroke-width:1.2}.gesture span{display:block;line-height:1.8}.gesture strong{font-weight:400;color:#384333}.control-card{position:absolute;z-index:3;right:5.2%;bottom:12.7%;width:262px;padding:25px 24px 20px;border:1px solid #ffffffd9;border-radius:19px;background:#ffffffb8;box-shadow:0 12px 50px #64724909,0 1px 3px #888d7b09;backdrop-filter:blur(14px)}.card-top{display:flex;justify-content:space-between;align-items:center;margin-bottom:23px}.card-title{font-family:Georgia,serif;font-size:20px;letter-spacing:-.4px}.live{display:flex;align-items:center;gap:5px;font-size:8px;letter-spacing:1px;color:#8c9685}.live:before{content:'';width:5px;height:5px;background:#8a9d6e;border-radius:50%}.range-row{display:flex;justify-content:space-between;font-size:11px;margin-bottom:14px}.range-row output{font-size:10px;color:#929786;font-variant-numeric:tabular-nums}input[type=range]{display:block;appearance:none;width:100%;height:28px;background:transparent;margin:0;cursor:pointer;touch-action:pan-y}input[type=range]::-webkit-slider-runnable-track{height:3px;border-radius:4px;background:linear-gradient(to right,var(--orange) var(--fill,48%),#e6e7dd var(--fill,48%))}input[type=range]::-moz-range-track{height:3px;border-radius:4px;background:#dadfd0}input[type=range]::-moz-range-progress{height:3px;background:var(--orange)}input[type=range]::-webkit-slider-thumb{appearance:none;width:17px;height:17px;border:3px solid white;border-radius:50%;background:var(--orange);margin-top:-7px;box-shadow:0 1px 6px #6d4c2930}input[type=range]::-moz-range-thumb{width:11px;height:11px;border:3px solid white;border-radius:50%;background:var(--orange)}.scale{display:flex;justify-content:space-between;font-size:9px;color:#9ba18f;margin:6px 0 22px}.squeeze{width:100%;min-height:47px;border:0;border-radius:9px;background:var(--orange);color:white;display:flex;align-items:center;justify-content:center;gap:9px;font-size:12px;letter-spacing:.25px;box-shadow:0 4px 12px #ee713520;transition:background .2s,transform .2s}.squeeze:active{transform:scale(.98)}.squeeze:disabled{background:#e78b56;cursor:default}.squeeze svg{width:17px;height:17px;fill:none;stroke:currentColor;stroke-width:1.5}.reset{display:flex;align-items:center;justify-content:center;gap:7px;width:100%;height:44px;border:0;background:none;color:#818976;font-size:10px;margin-top:7px}.reset svg{width:12px;height:12px;fill:none;stroke:currentColor;stroke-width:1.5}.caption{position:absolute;left:52%;bottom:13.4%;transform:translateX(-50%);z-index:2;text-align:center;pointer-events:none}.caption .name{font-family:Georgia,serif;font-size:18px;letter-spacing:-.4px}.caption .spec{font-size:8px;letter-spacing:1.7px;color:#929985;margin-top:8px}.status{display:inline-flex;gap:6px;align-items:center;font-size:8px;letter-spacing:1px;text-transform:uppercase;color:#8b987e;margin-top:17px}.status i{width:4px;height:4px;border-radius:50%;background:#91a17f}.footer{position:absolute;z-index:2;left:4.3%;right:4.3%;bottom:28px;display:flex;justify-content:space-between;align-items:center;font-size:8px;letter-spacing:1.4px;color:#9a9e8b;pointer-events:none}.footer .note{letter-spacing:.2px;font-size:10px}.footer b{font-weight:400;color:#c07849}.demo-tag{position:absolute;top:90px;left:50%;transform:translateX(-50%);font-size:9px;color:#bd723e;letter-spacing:1.4px;z-index:4;border:1px solid #e9d7b9;border-radius:20px;padding:9px 14px;background:#fff9eddd;display:none;pointer-events:none}.loading{position:absolute;top:53%;left:54%;transform:translate(-50%,-50%);font-size:11px;color:#a0a28e;letter-spacing:1px;pointer-events:none;transition:opacity .5s}.loading.hidden{opacity:0}.touch-hint{position:absolute;z-index:2;top:19%;left:53%;transform:translateX(-50%);display:flex;gap:6px;align-items:center;color:#a5a68c;font-size:9px;letter-spacing:.5px;pointer-events:none}.touch-hint:before,.touch-hint:after{content:'';height:1px;width:22px;background:#d8d9c9}.error{position:absolute;left:50%;top:52%;transform:translate(-50%,-50%);background:#fff9ed;padding:25px;max-width:350px;border-radius:15px;color:#8d654b;font-size:13px;line-height:1.6;z-index:5}
@media(min-width:1600px){.control-card{right:8%;bottom:18%}.intro{left:10%;top:26%}.caption{bottom:17%}}
@media(max-width:1050px){.intro{left:5%;width:240px;top:23%}h1{font-size:54px}.description{max-width:205px;font-size:12px}.control-card{right:4%;width:225px;padding:21px 19px;bottom:12%}.caption{left:49%;bottom:13%}.touch-hint{left:54%}}
@media(max-width:700px){.header{top:23px;left:24px;right:24px}.brand{font-size:10px;letter-spacing:1.5px;gap:9px}.brand-mark{width:25px;height:25px}.edition{font-size:8px;letter-spacing:1px}.edition .hide-mobile{display:none}.intro{top:95px;left:27px;width:calc(100% - 54px)}.eyebrow{font-size:8px;letter-spacing:1.8px}h1{font-size:42px;line-height:1.04;letter-spacing:-1.9px;margin:17px 0 10px}h1 br{display:none}.description{font-size:11px;max-width:270px;line-height:1.7}.gesture{display:none}.control-card{bottom:56px;left:22px;right:22px;width:auto;padding:17px 20px 10px;display:grid;grid-template-columns:1fr 1fr;column-gap:24px;border-radius:15px}.card-top{grid-column:1/-1;margin-bottom:13px}.card-title{font-size:17px}.firmness-control{grid-column:1;grid-row:2/4;align-self:center}.range-row{font-size:10px;margin-bottom:7px}.scale{margin:4px 0 0;font-size:8px}.squeeze{grid-column:2;grid-row:2;min-height:44px;font-size:11px}.reset{grid-column:2;grid-row:3;height:44px;margin:0;font-size:9px}.caption{bottom:265px;left:50%;white-space:nowrap}.caption .name{font-size:15px}.caption .spec{display:none}.status{margin-top:6px;font-size:7px}.footer{left:25px;right:25px;bottom:23px;font-size:7px}.footer .note{font-size:8px}.touch-hint{display:none}.demo-tag{top:67px;font-size:7px;padding:6px 10px;left:auto;right:24px;transform:none}.loading{left:50%;top:47%}}
@media(max-width:700px) and (max-height:720px){.intro{top:82px}h1{font-size:35px;margin-top:12px}.description{font-size:10px;max-width:250px}.control-card{bottom:44px;padding:12px 18px 6px}.card-top{margin-bottom:5px}.caption{bottom:230px}.footer{bottom:17px}}
@media(prefers-reduced-motion:reduce){button{transition:none}}
</style>
</head>
<body>
<div id="scene" aria-label="Interactive three dimensional orange jelly. Touch or drag the orange to make it wobble."></div>
<header class="header"><div class="brand"><span class="brand-mark"></span>THE JELLY LAB</div><div class="edition"><i></i><span class="hide-mobile">A SMALL STUDY IN </span>SOFTNESS / 001</div></header>
<section class="intro"><div class="eyebrow">FRESHLY SIMULATED</div><h1>A little slice<br> of <em>sunshine.</em></h1><p class="description">All the joy of an orange.<br>A little more wobble.<br>Go on, give it a gentle poke.</p><div class="gesture"><svg viewBox="0 0 32 32"><path d="M13 16V7a2 2 0 0 1 4 0v8-3a2 2 0 0 1 4 0v4-2a2 2 0 0 1 4 0v8c0 6-3 8-8 8-3 0-5-2-7-5l-4-6a2 2 0 0 1 3-2l4 4"/><path d="M7 9H2m3-3L2 9l3 3M23 5h6m-3-3 3 3-3 3"/></svg><span><strong>Poke. Pull. Let go.</strong><br>It always finds its shape.</span></div></section>
<div class="touch-hint">a little touch goes a long way</div><div id="loading" class="loading">Preparing something juicy…</div><div class="demo-tag" id="demoTag">DEMO PLAYING · TOUCH TO EXPLORE</div>
<div class="caption"><div class="name">Orange, reimagined.</div><div class="spec">CITRUS JELLY · MADE OF LITTLE MOMENTS</div><div class="status"><i></i><span id="stateLabel">Perfectly at rest</span></div></div>
<aside class="control-card" aria-label="Jelly controls"><div class="card-top"><span class="card-title">Make it your own.</span><span class="live">LIVE</span></div><div class="firmness-control"><div class="range-row"><label for="firmness">Firmness</label><output id="firmValue" for="firmness">48%</output></div><input id="firmness" type="range" min="0" max="100" value="48" aria-label="Jelly firmness"><div class="scale"><span>Extra wobbly</span><span>Nice & firm</span></div></div><button class="squeeze" id="squeeze"><svg viewBox="0 0 24 24"><path d="M8 4C8 7 4 9 4 13a4 4 0 0 0 8 0C12 9 8 7 8 4Z"/><path d="M17 3v7m-3-4 3 4 3-4M16 15h5m-2-2 2 2-2 2M4 21h16"/></svg><span id="squeezeText">Give it a squeeze</span></button><button class="reset" id="reset"><svg viewBox="0 0 16 16"><path d="M3 6a5 5 0 1 1 0 5M3 2v4h4"/></svg>Reset the slice</button></aside>
<footer class="footer"><span>PLAY WITH YOUR FOOD.</span><span class="note">A tiny experiment in <b>feel-good physics.</b></span></footer>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/build/three.min.js"></script>
<script>
(()=>{'use strict';
const host=document.getElementById('scene'),loading=document.getElementById('loading');
if(typeof THREE==='undefined'){loading.className='error';loading.textContent='The 3D library could not load. Please check your connection and reload to enjoy your little slice.';return;}
let renderer;try{renderer=new THREE.WebGLRenderer({antialias:true,alpha:true,powerPreference:'high-performance'});}catch(e){loading.className='error';loading.textContent='This little experiment needs WebGL. Please try a browser with 3D graphics enabled.';return;}
renderer.setPixelRatio(Math.min(devicePixelRatio,2));renderer.setClearColor(0xf8f6ef,0);renderer.shadowMap.enabled=true;renderer.shadowMap.type=THREE.PCFSoftShadowMap;renderer.outputColorSpace=THREE.SRGBColorSpace;renderer.toneMapping=THREE.ACESFilmicToneMapping;renderer.toneMappingExposure=1.25;host.appendChild(renderer.domElement);
const scene=new THREE.Scene(),camera=new THREE.PerspectiveCamera(34,1,.1,80);scene.fog=new THREE.Fog(0xf8f6ef,23,48);
scene.add(new THREE.HemisphereLight(0xfff8df,0xb9b8a2,2.6));const key=new THREE.DirectionalLight(0xfff3dc,4.7);key.position.set(-4,9,5);key.castShadow=true;key.shadow.mapSize.set(2048,2048);key.shadow.camera.left=-7;key.shadow.camera.right=7;key.shadow.camera.top=7;key.shadow.camera.bottom=-7;key.shadow.normalBias=.035;key.shadow.bias=-.00015;key.shadow.radius=5;scene.add(key);const glow=new THREE.DirectionalLight(0xffd397,3.5);glow.position.set(2,4,-6);scene.add(glow);const fill=new THREE.DirectionalLight(0xffffff,1.4);fill.position.set(6,3,4);scene.add(fill);
// A generated studio environment: no downloaded images or textures.
const envScene=new THREE.Scene();envScene.background=new THREE.Color(0xe9e7d9);const envMat=new THREE.MeshBasicMaterial({color:0xffffff,side:THREE.DoubleSide});for(const p of [[-4,5,1,5,5],[5,4,2,3,6],[0,7,-3,6,3]]){const panel=new THREE.Mesh(new THREE.PlaneGeometry(p[3],p[4]),envMat);panel.position.set(p[0],p[1],p[2]);panel.lookAt(0,0,0);envScene.add(panel);}const pmrem=new THREE.PMREMGenerator(renderer);const envMap=pmrem.fromScene(envScene,.04).texture;scene.environment=envMap;pmrem.dispose();
const ground=new THREE.Mesh(new THREE.PlaneGeometry(200,200),new THREE.MeshStandardMaterial({color:0xf5f3ea,roughness:1}));ground.rotation.x=-Math.PI/2;ground.position.y=-.13;ground.receiveShadow=true;scene.add(ground);
// Lathed porcelain with an actual raised rim and recessed eating surface.
const plateProfile=[[0,.18],[.7,.18],[2.5,.18],[2.95,.19],[3.2,.23],[3.45,.34],[3.68,.40],[3.82,.415],[3.9,.39],[3.91,.34],[3.81,.29],[3.6,.23],[3.3,.13],[2.9,.075],[2.7,.025],[2.6,-.025],[2.42,-.025],[2.38,.025],[.6,.07],[0,.07]].map(p=>new THREE.Vector2(...p));
const plate=new THREE.Mesh(new THREE.LatheGeometry(plateProfile,128),new THREE.MeshPhysicalMaterial({color:0xfffefd,roughness:.22,metalness:0,clearcoat:.75,clearcoatRoughness:.17,envMapIntensity:.45}));plate.castShadow=true;plate.receiveShadow=true;scene.add(plate);
const rim=new THREE.Mesh(new THREE.TorusGeometry(3.79,.012,8,160),new THREE.MeshStandardMaterial({color:0xf5f2e5,roughness:.3}));rim.rotation.x=-Math.PI/2;rim.position.y=.415;scene.add(rim);
let seed=8021;function rand(){seed=(seed*1664525+1013904223)>>>0;return seed/4294967296;}const R=2.63,Z=.68,BASE=.235,TOP=.94;
// Procedural pulp: individually drawn, radially oriented juice vesicles.
const texCanvas=document.createElement('canvas');texCanvas.width=texCanvas.height=1536;const ctx=texCanvas.getContext('2d');ctx.fillStyle='#f8a120';ctx.fillRect(0,0,1536,1536);const TS=1536/5.6;function uvPoint(x,z){return [(x+2.8)*TS,(2.8-z)*TS];}
for(let i=0;i<13500;i++){let a=rand()*Math.PI,r=Math.sqrt(rand())*2.55,x=Math.cos(a)*r,z=Z-Math.sin(a)*r;let p=uvPoint(x,z);ctx.save();ctx.translate(p[0],p[1]);ctx.rotate(a);let len=(.036+rand()*.105)*TS,w=(.013+rand()*.025)*TS;ctx.beginPath();ctx.ellipse(0,0,len,w,0,0,Math.PI*2);let hue=30+rand()*12;ctx.fillStyle='hsla('+hue+',100%,'+(48+rand()*20)+'%,'+(.20+rand()*.34)+')';ctx.fill();ctx.strokeStyle='rgba(255,233,145,'+(.12+rand()*.18)+')';ctx.lineWidth=.7;ctx.stroke();ctx.beginPath();ctx.ellipse(-len*.1,-w*.28,len*.65,w*.20,0,.1,Math.PI*.86);ctx.strokeStyle='rgba(255,246,193,.3)';ctx.stroke();ctx.restore();}
const pulpMap=new THREE.CanvasTexture(texCanvas);pulpMap.colorSpace=THREE.SRGBColorSpace;pulpMap.anisotropy=renderer.capabilities.getMaxAnisotropy();
const rindCanvas=document.createElement('canvas');rindCanvas.width=rindCanvas.height=256;const rc=rindCanvas.getContext('2d');rc.fillStyle='#ef8510';rc.fillRect(0,0,256,256);for(let i=0;i<5400;i++){let x=rand()*256,y=rand()*256,r=.3+rand()*1.2;rc.fillStyle=rand()>.5?'rgba(255,203,50,.36)':'rgba(156,69,0,.15)';rc.beginPath();rc.arc(x,y,r,0,7);rc.fill();}const rindMap=new THREE.CanvasTexture(rindCanvas);rindMap.colorSpace=THREE.SRGBColorSpace;rindMap.wrapS=rindMap.wrapT=THREE.RepeatWrapping;rindMap.repeat.set(3,2);
const pithMat=new THREE.MeshStandardMaterial({color:0xffefc5,roughness:.63,envMapIntensity:.3});const rindMat=new THREE.MeshPhysicalMaterial({color:0xffa012,map:rindMap,bumpMap:rindMap,bumpScale:.025,roughness:.34,clearcoat:.5,clearcoatRoughness:.22});
const pulpMat=new THREE.MeshPhysicalMaterial({color:0xffc34b,map:pulpMap,bumpMap:pulpMap,bumpScale:.018,roughness:.19,metalness:0,transmission:.35,thickness:.57,ior:1.36,clearcoat:1,clearcoatRoughness:.12,attenuationColor:new THREE.Color(0xff940e),attenuationDistance:1.8,envMapIntensity:.85,side:THREE.DoubleSide});
// A volumetric lattice. Each tetrahedron constrains signed volume; all its
// edges constrain distance. Positions use XPBD rather than an animation.
const nodes=[],lookup=new Map(),spacing=.44,ny=4;let restCenter=new THREE.Vector3();
for(let ix=0;ix<=12;ix++)for(let iz=0;iz<=6;iz++){let x=(ix-6)*spacing,z=-iz*spacing;if(x*x+z*z>2.91*2.91)continue;for(let iy=0;iy<ny;iy++){let n={x:x,y:BASE+iy*(TOP-BASE)/3,z:z+Z,rx:x,ry:BASE+iy*(TOP-BASE)/3,rz:z+Z,px:x,py:BASE+iy*(TOP-BASE)/3,pz:z+Z,vx:0,vy:0,vz:0};lookup.set(ix+','+iy+','+iz,nodes.length);nodes.push(n);restCenter.add(new THREE.Vector3(n.rx,n.ry,n.rz));}}restCenter.multiplyScalar(1/nodes.length);
const edges=[],tetra=[],edgeSet=new Set();function edge(a,b){let k=Math.min(a,b)+':'+Math.max(a,b);if(edgeSet.has(k))return;edgeSet.add(k);let A=nodes[a],B=nodes[b];edges.push({a,b,len:Math.hypot(A.x-B.x,A.y-B.y,A.z-B.z),lambda:0});}
function volume(A,B,C,D){let bx=B.x-A.x,by=B.y-A.y,bz=B.z-A.z,cx=C.x-A.x,cy=C.y-A.y,cz=C.z-A.z,dx=D.x-A.x,dy=D.y-A.y,dz=D.z-A.z;return (bx*(cy*dz-cz*dy)+by*(cz*dx-cx*dz)+bz*(cx*dy-cy*dx))/6;}
function addTet(a,b,c,d){tetra.push({ids:[a,b,c,d],v:volume(nodes[a],nodes[b],nodes[c],nodes[d]),lambda:0});edge(a,b);edge(a,c);edge(a,d);edge(b,c);edge(b,d);edge(c,d);}
for(let ix=0;ix<12;ix++)for(let iz=0;iz<6;iz++)for(let iy=0;iy<3;iy++){let ids=[[0,0,0],[1,0,0],[0,1,0],[1,1,0],[0,0,1],[1,0,1],[0,1,1],[1,1,1]].map(o=>lookup.get((ix+o[0])+','+(iy+o[1])+','+(iz+o[2])));if(ids.some(i=>i===undefined))continue;for(const t of [[0,1,3,7],[0,3,2,7],[0,2,6,7],[0,6,4,7],[0,4,5,7],[0,5,1,7]])addTet(...t.map(i=>ids[i]));}
// Trilinear embedding makes peel, pith and pulp follow the same soft body.
function bind(x,y,z){let gx=x/spacing+6,gz=(Z-z)/spacing,gy=(y-BASE)/(TOP-BASE)*3;let ix=Math.max(0,Math.min(11,Math.floor(gx))),iz=Math.max(0,Math.min(5,Math.floor(gz))),iy=Math.max(0,Math.min(2,Math.floor(gy)));let tx=Math.max(0,Math.min(1,gx-ix)),tz=Math.max(0,Math.min(1,gz-iz)),ty=Math.max(0,Math.min(1,gy-iy));let ids=[],ws=[];for(let a=0;a<2;a++)for(let b=0;b<2;b++)for(let c=0;c<2;c++){let id=lookup.get((ix+a)+','+(iy+b)+','+(iz+c));if(id===undefined)continue;let w=(a?tx:1-tx)*(b?ty:1-ty)*(c?tz:1-tz);if(w>1e-7){ids.push(id);ws.push(w);}}let sum=ws.reduce((a,b)=>a+b,0);if(sum<.99){let near=nodes.map((n,i)=>({i,d:(n.rx-x)**2+(n.ry-y)**2+(n.rz-z)**2})).sort((a,b)=>a.d-b.d).slice(0,4);ids=near.map(n=>n.i);ws=near.map(n=>1/Math.pow(n.d+.008,2));sum=ws.reduce((a,b)=>a+b,0);}return {x,y,z,ids,ws:ws.map(w=>w/sum)};}
function sample(b){let x=b.x,y=b.y,z=b.z;for(let j=0;j<b.ids.length;j++){let n=nodes[b.ids[j]],w=b.ws[j];x+=(n.x-n.rx)*w;y+=(n.y-n.ry)*w;z+=(n.z-n.rz)*w;}return [x,y,z];}
const surfaces=[],pickMeshes=[];function sector(r0,r1,a0,a1,lo,hi,nr,nt,material,domed=false){const positions=[],uvs=[],idx=[];function put(r,a,y){let x=r*Math.cos(a),z=Z-r*Math.sin(a);positions.push(x,y,z);uvs.push((x+2.8)/5.6,(z+2.8)/5.6);}
for(let layer=0;layer<2;layer++)for(let i=0;i<=nr;i++)for(let j=0;j<=nt;j++){let r=r0+(r1-r0)*i/nr,a=a0+(a1-a0)*j/nt;let y=layer?hi:lo;if(domed&&layer)y+=.027*Math.sin(Math.PI*i/nr)*Math.sin(Math.PI*j/nt);put(r,a,y);}const stride=nt+1,layerCount=(nr+1)*stride;for(let i=0;i<nr;i++)for(let j=0;j<nt;j++){let a=i*stride+j,b=a+stride;idx.push(a,b,a+1,a+1,b,b+1);let o=layerCount;idx.push(o+a,o+a+1,o+b,o+a+1,o+b+1,o+b);}
function side(a,b){idx.push(a,a+layerCount,b,b,a+layerCount,b+layerCount);}for(let j=0;j<nt;j++){side(j+1,j);side(nr*stride+j,nr*stride+j+1);}for(let i=0;i<nr;i++){side(i*stride,(i+1)*stride);side((i+1)*stride+nt,i*stride+nt);}
const g=new THREE.BufferGeometry();g.setAttribute('position',new THREE.Float32BufferAttribute(positions,3));g.setAttribute('uv',new THREE.Float32BufferAttribute(uvs,2));g.setIndex(idx);g.computeVertexNormals();const mesh=new THREE.Mesh(g,material);mesh.castShadow=true;mesh.receiveShadow=true;mesh.frustumCulled=false;scene.add(mesh);const bindings=[];for(let i=0;i<positions.length;i+=3)bindings.push(bind(positions[i],positions[i+1],positions[i+2]));surfaces.push({g,bindings});pickMeshes.push(mesh);return mesh;}
sector(0,2.55,0,Math.PI,BASE,BASE+.035,5,80,pithMat);sector(2.48,2.55,0,Math.PI,BASE,.935,2,90,pithMat);sector(2.55,R,0,Math.PI,BASE-.007,.939,2,100,rindMat);sector(0,.16,0,Math.PI,BASE,.945,2,24,pithMat);
const segCount=10;for(let s=0;s<=segCount;s++){let a=s*Math.PI/segCount;sector(.05,2.5,Math.max(0,a-.008),Math.min(Math.PI,a+.008),BASE,.932,7,1,pithMat);}for(let s=0;s<segCount;s++){let a0=s*Math.PI/segCount+.012,a1=(s+1)*Math.PI/segCount-.012;let material=pulpMat.clone();material.color.setHSL(.105+(s%3)*.005,1,.64+(s%2)*.015);sector(.15,2.48,a0,a1,BASE+.025,TOP+.012,9,9,material,true);}
// Raised, translucent juice cells catch studio highlights above the pulp.
const cellGeo=new THREE.SphereGeometry(1,5,4),cellMat=new THREE.MeshPhysicalMaterial({color:0xffd16b,roughness:.13,transmission:.38,thickness:.07,ior:1.34,clearcoat:1,transparent:true,opacity:.68,depthWrite:false});const cellCount=650,cells=new THREE.InstancedMesh(cellGeo,cellMat,cellCount),cellBindings=[],cellTransforms=[];cells.frustumCulled=false;scene.add(cells);const dummy=new THREE.Object3D();
for(let i=0;i<cellCount;i++){let s=Math.floor(rand()*segCount),a=(s+.08+rand()*.84)*Math.PI/segCount,r=.33+Math.sqrt(rand())*2.05;cellBindings.push(bind(Math.cos(a)*r,TOP+.022,Z-Math.sin(a)*r));cellTransforms.push({a,sx:.025+rand()*.035,sz:.009+rand()*.012,sy:.008+rand()*.014});}
const dropMat=new THREE.MeshPhysicalMaterial({color:0xffb126,roughness:.08,transmission:.55,thickness:.18,ior:1.34,clearcoat:1,transparent:true,opacity:.92});const drops=[],dropGeo=new THREE.SphereGeometry(1,12,8);let squeezeTime=-1,dropSpawned=false,firm=.48,drag=null,pointer=null,demo=location.hash==='#demo',demoStart=0,demoStage=-1,motion=0;
const label=document.getElementById('stateLabel'),squeezeBtn=document.getElementById('squeeze'),squeezeText=document.getElementById('squeezeText'),slider=document.getElementById('firmness'),firmValue=document.getElementById('firmValue'),demoTag=document.getElementById('demoTag');demoTag.style.display=demo?'block':'none';
function setFirm(v){firm=Math.max(0,Math.min(1,v));slider.value=Math.round(firm*100);slider.style.setProperty('--fill',Math.round(firm*100)+'%');firmValue.textContent=Math.round(firm*100)+'%';}setFirm(.48);
function endDemo(){if(!demo)return;demo=false;demoTag.style.display='none';if(drag&&drag.auto)drag=null;}window.addEventListener('pointerdown',endDemo,{capture:true,passive:true});slider.addEventListener('input',()=>setFirm(Number(slider.value)/100));
function squeeze(){if(squeezeTime>=0)return;squeezeTime=0;dropSpawned=false;squeezeBtn.disabled=true;squeezeText.textContent='A little pressure…';}squeezeBtn.addEventListener('click',squeeze);
function clearDrops(){for(const d of drops)scene.remove(d.mesh);drops.length=0;}
function reset(){drag=null;pointer=null;for(const n of nodes){n.x=n.px=n.rx;n.y=n.py=n.ry;n.z=n.pz=n.rz;n.vx=n.vy=n.vz=0;}clearDrops();squeezeTime=-1;squeezeBtn.disabled=false;squeezeText.textContent='Give it a squeeze';setFirm(.48);motion=0;label.textContent='Perfectly at rest';}document.getElementById('reset').addEventListener('click',reset);
function spit(){for(let i=0;i<7;i++){let x=-1.5+rand()*2.4,z=Z+.04;let b=bind(x,.42+rand()*.16,z),p=sample(b);const mesh=new THREE.Mesh(dropGeo,dropMat);let radius=.038+rand()*.032;mesh.position.set(...p);mesh.scale.set(radius,radius*1.5,radius);scene.add(mesh);drops.push({mesh,r:radius,vx:(rand()-.5)*.65,vy:.1+rand()*.35,vz:.7+rand()*.65,age:0,landed:false});}}
// Pointer interaction uses a camera-facing plane and a local weighted handle.
const raycaster=new THREE.Raycaster(),ndc=new THREE.Vector2(),plane=new THREE.Plane(),worldHit=new THREE.Vector3(),camNormal=new THREE.Vector3();
function rayAt(e){ndc.set(e.clientX/innerWidth*2-1,1-e.clientY/innerHeight*2);raycaster.setFromCamera(ndc,camera);}function localHandle(p){let best=0,dist=Infinity;for(let i=0;i<nodes.length;i++){let n=nodes[i],d=(p.x-n.x)**2+(p.y-n.y)**2+(p.z-n.z)**2;if(d<dist){best=i;dist=d;}}let n=nodes[best];return {id:best,target:new THREE.Vector3(n.x,n.y,n.z),start:new THREE.Vector3(n.x,n.y,n.z),weights:nodes.map(a=>Math.exp(-((a.rx-n.rx)**2+(a.ry-n.ry)**2+(a.rz-n.rz)**2)/.45)),auto:false};}
function poke(p,strength=2.6){for(const n of nodes){let w=Math.exp(-((p.x-n.x)**2+(p.z-n.z)**2)/.65);n.vy-=strength*w;n.vx+=(n.x-p.x)*w*.7;n.vz+=(n.z-p.z)*w*.7;}motion=1;}
host.addEventListener('pointerdown',e=>{if(e.button!==undefined&&e.button!==0)return;rayAt(e);const hits=raycaster.intersectObjects(pickMeshes,false);if(!hits.length)return;e.preventDefault();let hit=hits[0].point;poke(hit,1.7);drag=localHandle(hit);camera.getWorldDirection(camNormal);plane.setFromNormalAndCoplanarPoint(camNormal,hit);pointer={id:e.pointerId,x:e.clientX,y:e.clientY,hit:hit.clone(),offset:drag.target.clone().sub(hit),moved:false};host.setPointerCapture(e.pointerId);});
host.addEventListener('pointermove',e=>{if(!pointer||pointer.id!==e.pointerId)return;if(Math.hypot(e.clientX-pointer.x,e.clientY-pointer.y)>5)pointer.moved=true;if(!pointer.moved)return;rayAt(e);if(raycaster.ray.intersectPlane(plane,worldHit)){let target=worldHit.clone().add(pointer.offset),delta=target.sub(drag.start);delta.clampLength(0,1.38);drag.target.copy(drag.start).add(delta);drag.target.y=Math.max(.33,Math.min(2.6,drag.target.y));}});
function release(e){if(pointer&&pointer.id===e.pointerId){if(host.hasPointerCapture(e.pointerId))host.releasePointerCapture(e.pointerId);pointer=null;drag=null;motion=1;}}host.addEventListener('pointerup',release);host.addEventListener('pointercancel',release);host.addEventListener('lostpointercapture',()=>{pointer=null;drag=null;});
function solveVolume(t,alpha){const a=nodes[t.ids[0]],b=nodes[t.ids[1]],c=nodes[t.ids[2]],d=nodes[t.ids[3]];let bx=b.x-a.x,by=b.y-a.y,bz=b.z-a.z,cx=c.x-a.x,cy=c.y-a.y,cz=c.z-a.z,dx=d.x-a.x,dy=d.y-a.y,dz=d.z-a.z;
let gbx=(cy*dz-cz*dy)/6,gby=(cz*dx-cx*dz)/6,gbz=(cx*dy-cy*dx)/6,gcx=(dy*bz-dz*by)/6,gcy=(dz*bx-dx*bz)/6,gcz=(dx*by-dy*bx)/6,gdx=(by*cz-bz*cy)/6,gdy=(bz*cx-bx*cz)/6,gdz=(bx*cy-by*cx)/6,gax=-gbx-gcx-gdx,gay=-gby-gcy-gdy,gaz=-gbz-gcz-gdz;let v=(bx*gbx+by*gby+bz*gbz),den=gax*gax+gay*gay+gaz*gaz+gbx*gbx+gby*gby+gbz*gbz+gcx*gcx+gcy*gcy+gcz*gcz+gdx*gdx+gdy*gdy+gdz*gdz;let dl=(-(v-t.v)-alpha*t.lambda)/(den+alpha+1e-12);dl=Math.max(-.08,Math.min(.08,dl));t.lambda+=dl;a.x+=gax*dl;a.y+=gay*dl;a.z+=gaz*dl;b.x+=gbx*dl;b.y+=gby*dl;b.z+=gbz*dl;c.x+=gcx*dl;c.y+=gcy*dl;c.z+=gcz*dl;d.x+=gdx*dl;d.y+=gdy*dl;d.z+=gdz*dl;}
const step=1/90;function physics(h,pressure){const edgeAlpha=(.000012+Math.pow(1-firm,2)*.00019)/(h*h),volAlpha=.000000045/(h*h),damping=Math.exp(-(1.25+firm*1.5)*h);
for(const n of nodes){n.px=n.x;n.py=n.y;n.pz=n.z;n.vy-=4.1*h;n.x+=n.vx*h;n.y+=n.vy*h;n.z+=n.vz*h;}for(const e of edges)e.lambda=0;for(const t of tetra)t.lambda=0;
for(let it=0;it<6;it++){
for(const e of edges){let a=nodes[e.a],b=nodes[e.b],dx=b.x-a.x,dy=b.y-a.y,dz=b.z-a.z,l=Math.sqrt(dx*dx+dy*dy+dz*dz)||1e-8;let dl=(-(l-e.len)-edgeAlpha*e.lambda)/(2+edgeAlpha);e.lambda+=dl;let k=dl/l;a.x-=dx*k;a.y-=dy*k;a.z-=dz*k;b.x+=dx*k;b.y+=dy*k;b.z+=dz*k;}
for(const t of tetra)solveVolume(t,volAlpha);
// Weak elastic shape memory supplies a stress-free reference, not keyframes.
let cx=0,cy=0,cz=0;for(const n of nodes){cx+=n.x;cy+=n.y;cz+=n.z;}cx=cx/nodes.length-restCenter.x;cy=cy/nodes.length-restCenter.y;cz=cz/nodes.length-restCenter.z;const memory=.007+firm*.008;
let hx=0,hy=0,hz=0;if(drag){let source=nodes[drag.id];hx=drag.target.x-source.x;hy=drag.target.y-source.y;hz=drag.target.z-source.z;}
for(let i=0;i<nodes.length;i++){let n=nodes[i];n.x+=(n.rx+cx-n.x)*memory;n.y+=(n.ry+cy-n.y)*memory;n.z+=(n.rz+cz-n.z)*memory;if(pressure>0&&n.ry>TOP-.02){let ceiling=TOP-pressure*.28;n.y+=Math.min(0,ceiling-n.y)*.66;}
if(drag&&(!pointer||pointer.moved||drag.auto)){let w=drag.weights[i]*.16;n.x+=hx*w;n.y+=hy*w;n.z+=hz*w;}
if(n.y<BASE)n.y=BASE;
// Bound strain during extreme pointer input without hard-pinning the lattice.
let dx=n.x-n.rx-cx,dy=n.y-n.ry-cy,dz=n.z-n.rz-cz,len=Math.hypot(dx,dy,dz);if(len>1.15){let f=1.15/len;n.x=n.rx+cx+dx*f;n.y=Math.max(BASE,n.ry+cy+dy*f);n.z=n.rz+cz+dz*f;}}
}
let energy=0;for(const n of nodes){n.vx=(n.x-n.px)/h*damping;n.vy=(n.y-n.py)/h*damping;n.vz=(n.z-n.pz)/h*damping;if(n.y<=BASE+.0001){n.vx*=.8;n.vz*=.8;n.vy=Math.max(0,n.vy);}let speed=Math.hypot(n.vx,n.vy,n.vz);if(speed>5){let f=5/speed;n.vx*=f;n.vy*=f;n.vz*=f;}energy+=n.vx*n.vx+n.vy*n.vy+n.vz*n.vz;}
let mx=0,mz=0;for(const n of nodes){mx+=n.x-n.rx;mz+=n.z-n.rz;}mx=mx/nodes.length*.003;mz=mz/nodes.length*.003;for(const n of nodes){n.x-=mx;n.z-=mz;}motion=energy/nodes.length;}
function updateSurface(){for(const s of surfaces){let arr=s.g.attributes.position.array;for(let i=0;i<s.bindings.length;i++){let b=s.bindings[i],x=b.x,y=b.y,z=b.z;for(let j=0;j<b.ids.length;j++){let n=nodes[b.ids[j]],w=b.ws[j];x+=(n.x-n.rx)*w;y+=(n.y-n.ry)*w;z+=(n.z-n.rz)*w;}arr[i*3]=x;arr[i*3+1]=Math.max(.215,y);arr[i*3+2]=z;}s.g.attributes.position.needsUpdate=true;s.g.computeVertexNormals();}for(let i=0;i<cellCount;i++){let p=sample(cellBindings[i]),t=cellTransforms[i];dummy.position.set(...p);dummy.rotation.set(0,t.a,0);dummy.scale.set(t.sx,t.sy,t.sz);dummy.updateMatrix();cells.setMatrixAt(i,dummy.matrix);}cells.instanceMatrix.needsUpdate=true;}
let width=innerWidth,height=innerHeight;function resize(){width=innerWidth;height=innerHeight;renderer.setSize(width,height);camera.aspect=width/height;let mobile=width<=700;if(mobile){camera.fov=38;let distance=Math.max(1,((height/width)/2.05));camera.position.set(8.4*distance,14*distance,19*distance);camera.lookAt(0,.25,0);camera.setViewOffset(width,height,0,Math.round(height*.018),width,height);}else{camera.clearViewOffset();camera.fov=33;camera.position.set(7.4,10.8,13.4);camera.lookAt(0,.2,0);camera.setViewOffset(width,height,Math.round(-width*.045),Math.round(height*.03),width,height);}camera.updateProjectionMatrix();}window.addEventListener('resize',resize);resize();
// Demo uses exactly the same impulses, handles and pressure as a person.
function runDemo(now){if(!demo)return;if(!demoStart)demoStart=now;const t=(now-demoStart)%20,stage=t<3?0:t<7?1:t<10?2:t<14?3:t<17?4:5;if(stage!==demoStage){demoStage=stage;if(stage===0){reset();poke(new THREE.Vector3(.8,TOP,-.1),3.2);}if(stage===1){let p=new THREE.Vector3(-1.2,TOP,-.6);drag=localHandle(p);drag.auto=true;}if(stage===2)drag=null;if(stage===3)squeeze();if(stage===4){setFirm(.86);poke(new THREE.Vector3(.5,TOP,-.3),3);}if(stage===5)setFirm(.25);}if(stage===1&&drag){let k=Math.sin(Math.min(1,(t-3)/2)*Math.PI/2);drag.target.copy(drag.start).add(new THREE.Vector3(-.5*k,.9*k,.25*k));}}
let last=0,acc=0,frame=0;function animate(ms){requestAnimationFrame(animate);const now=ms/1000,dt=last?Math.min(.04,now-last):1/60;last=now;runDemo(now);let pressure=0;if(squeezeTime>=0){squeezeTime+=dt;let t=squeezeTime;pressure=t<.65?Math.sin(t/.65*Math.PI/2):t<1.65?1:t<2.5?Math.cos((t-1.65)/.85*Math.PI/2):0;if(t>.72&&!dropSpawned){spit();dropSpawned=true;}if(t>2.6){squeezeTime=-1;squeezeBtn.disabled=false;squeezeText.textContent='Give it a squeeze';}}
acc+=dt;let steps=0;while(acc>=step&&steps<4){physics(step,pressure);acc-=step;steps++;}if(steps===4)acc=0;updateSurface();for(let i=drops.length-1;i>=0;i--){let d=drops[i];d.age+=dt;if(!d.landed){d.vy-=6*dt;d.mesh.position.x+=d.vx*dt;d.mesh.position.y+=d.vy*dt;d.mesh.position.z+=d.vz*dt;if(d.mesh.position.y<.192+d.r*.25){d.mesh.position.y=.192+d.r*.25;d.landed=true;d.mesh.scale.set(d.r*1.7,d.r*.25,d.r*1.4);}}else{d.mesh.scale.x+=dt*.005;d.mesh.scale.z+=dt*.005;}if(d.age>7){d.mesh.scale.multiplyScalar(Math.max(0,1-dt*2));if(d.age>9){scene.remove(d.mesh);drops.splice(i,1);}}}
if(frame++%8===0){label.textContent=squeezeTime>=0?'Just a little juicy':drag&&(drag.auto||(pointer&&pointer.moved))?'A lovely little stretch':motion>.007?'Finding its happy shape':'Perfectly at rest';}renderer.render(scene,camera);if(frame===2)loading.classList.add('hidden');}requestAnimationFrame(animate);
document.addEventListener('visibilitychange',()=>{last=0;acc=0;});
})();
</script>
</body>
</html>
