Back to the app
The code
Jelly Orange Slice by Claude Sonnet 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:radial-gradient(ellipse at 50% 30%,#fff4e2 0%,#f4d9b6 60%,#e2b98a 100%);font-family:system-ui,Segoe UI,Roboto,sans-serif;-webkit-user-select:none;user-select:none}
canvas{position:fixed;inset:0;width:100%;height:100%;display:block;touch-action:none}
#hint{position:fixed;top:12px;left:50%;transform:translateX(-50%);background:rgba(255,255,255,.75);color:#6a3b00;padding:8px 14px;border-radius:20px;font-size:14px;text-align:center;max-width:90vw;pointer-events:none;transition:opacity 1s}
#demo{position:fixed;top:56px;left:50%;transform:translateX(-50%);background:#ff8a00;color:#fff;padding:6px 14px;border-radius:16px;font-size:13px;font-weight:700;display:none;pointer-events:none}
#ui{position:fixed;left:0;right:0;bottom:0;padding:12px 16px calc(14px + env(safe-area-inset-bottom));display:flex;flex-wrap:wrap;gap:12px;align-items:center;justify-content:center;background:linear-gradient(transparent,rgba(120,60,0,.18))}
button{font:700 18px system-ui,sans-serif;min-height:56px;min-width:130px;border:0;border-radius:28px;padding:0 26px;cursor:pointer;box-shadow:0 4px 0 rgba(0,0,0,.18);touch-action:manipulation}
#sq{background:#ff8a00;color:#fff}#rs{background:#fff;color:#6a3b00}
button:active{transform:translateY(2px);box-shadow:0 2px 0 rgba(0,0,0,.18)}
#fw{display:flex;align-items:center;gap:10px;background:rgba(255,255,255,.8);border-radius:28px;padding:0 18px;min-height:56px;color:#6a3b00;font-weight:700}
#fw input{width:min(240px,42vw);height:40px;accent-color:#ff8a00}
</style>
<script type="importmap">{"imports":{"three":"https://cdn.jsdelivr.net/npm/three@0.160.0/build/three.module.js"}}</script>
</head>
<body>
<canvas id="cv"></canvas>
<div id="hint">Drag the jelly to stretch it, tap to poke. Drag the background to rotate.</div>
<div id="demo">DEMO - touch to take over</div>
<div id="ui">
<button id="sq">Squeeze</button>
<button id="rs">Reset</button>
<label id="fw">Firmness <input id="fm" type="range" min="0" max="100" value="50"></label>
</div>
<script type="module">
import * as THREE from 'three';
const cv = document.getElementById('cv');
const renderer = new THREE.WebGLRenderer({canvas: cv, antialias: true, alpha: true});
renderer.setPixelRatio(Math.min(devicePixelRatio || 1, 2));
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.05;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(40, 1, 0.1, 50);
const camT = new THREE.Vector3(0, 0.3, 0);
let az = 0.5, el = 0.75, dist = 6.4;
// environment for gloss (optional)
try {
const mod = await import('https://cdn.jsdelivr.net/npm/three@0.160.0/examples/jsm/environments/RoomEnvironment.js');
const pm = new THREE.PMREMGenerator(renderer);
scene.environment = pm.fromScene(new mod.RoomEnvironment(), 0.04).texture;
} catch (e) {}
scene.add(new THREE.HemisphereLight(0xfff2dd, 0xc89a68, 0.7));
const key = new THREE.DirectionalLight(0xffffff, 2.4);
key.position.set(3, 6, 3); key.castShadow = true;
key.shadow.mapSize.set(1024, 1024);
Object.assign(key.shadow.camera, {left: -4, right: 4, top: 4, bottom: -4, near: 1, far: 20});
key.shadow.bias = -0.0004;
scene.add(key);
const back = new THREE.DirectionalLight(0xff9a30, 2.2);
back.position.set(-3, 2.5, -4); scene.add(back);
const rim = new THREE.PointLight(0xffa640, 6, 8); rim.position.set(0, 1.6, -2.4); scene.add(rim);
// ---------- plate ----------
const prof = [[0,-0.1],[1.5,-0.1],[2.0,-0.08],[2.5,0.0],[2.7,0.12],[2.82,0.25],[2.86,0.29],[2.8,0.3],[2.7,0.26],[2.3,0.1],[1.9,0.0],[0,0]].map(p => new THREE.Vector2(p[0], p[1]));
const plate = new THREE.Mesh(new THREE.LatheGeometry(prof, 72),
new THREE.MeshStandardMaterial({color: 0xffffff, roughness: 0.22, metalness: 0, side: THREE.DoubleSide}));
plate.receiveShadow = true; plate.castShadow = true; scene.add(plate);
const ground = new THREE.Mesh(new THREE.PlaneGeometry(30, 30), new THREE.ShadowMaterial({opacity: 0.22}));
ground.rotation.x = -Math.PI / 2; ground.position.y = -0.11; ground.receiveShadow = true; scene.add(ground);
const plateY = r => r < 1.9 ? 0 : (r < 2.7 ? (r - 1.9) * 0.3 : 0.24);
// ---------- soft body ----------
const R = 1.45, H = 0.6, NR = 5, NA = 32, NL = 3;
const NC = 1 + NR * NA, N = NC * NL;
const colOf = (i, a) => i === 0 ? 0 : 1 + (i - 1) * NA + (((a % NA) + NA) % NA);
const pid = (i, a, l) => colOf(i, a) * NL + l;
const P = new Float32Array(N * 3), Q = new Float32Array(N * 3), RS = new Float32Array(N * 3), REL = new Float32Array(N * 3);
{
for (let l = 0; l < NL; l++) { const k = l * 3; RS[k] = 0; RS[k + 1] = l * H / (NL - 1); RS[k + 2] = 0; }
for (let i = 1; i <= NR; i++) for (let a = 0; a < NA; a++) for (let l = 0; l < NL; l++) {
const k = pid(i, a, l) * 3, r = i / NR * R, t = a / NA * Math.PI * 2;
RS[k] = r * Math.cos(t); RS[k + 1] = l * H / (NL - 1); RS[k + 2] = r * Math.sin(t);
}
for (let i = 0; i < N; i++) { REL[i * 3] = RS[i * 3]; REL[i * 3 + 1] = RS[i * 3 + 1] - H / 2; REL[i * 3 + 2] = RS[i * 3 + 2]; }
}
const spA = [], spB = [], spL = [];
const addS = (a, b) => {
const dx = RS[a*3]-RS[b*3], dy = RS[a*3+1]-RS[b*3+1], dz = RS[a*3+2]-RS[b*3+2];
spA.push(a); spB.push(b); spL.push(Math.sqrt(dx*dx + dy*dy + dz*dz));
};
for (let l = 0; l < NL; l++) {
if (l < NL - 1) addS(pid(0,0,l), pid(0,0,l+1));
for (let a = 0; a < NA; a++) {
addS(pid(0,0,l), pid(1,a,l));
if (l < NL - 1) { addS(pid(0,0,l), pid(1,a,l+1)); addS(pid(0,0,l+1), pid(1,a,l)); }
}
}
for (let i = 1; i <= NR; i++) for (let a = 0; a < NA; a++) for (let l = 0; l < NL; l++) {
const p = pid(i, a, l);
addS(p, pid(i, a + 1, l));
if (i < NR) { addS(p, pid(i + 1, a, l)); addS(p, pid(i + 1, a + 1, l)); }
if (l < NL - 1) {
addS(p, pid(i, a, l + 1));
addS(p, pid(i, a + 1, l + 1)); addS(pid(i, a + 1, l), pid(i, a, l + 1));
if (i < NR) { addS(p, pid(i + 1, a, l + 1)); addS(pid(i, a, l + 1), pid(i + 1, a, l)); }
}
}
const NS = spA.length;
const SA = Int32Array.from(spA), SB = Int32Array.from(spB), SL = Float32Array.from(spL);
P.set(RS); Q.set(RS);
let firm = 0.5, press = 0;
const grab = {on: false, idx: [], w: [], off: [], target: new THREE.Vector3(), origin: new THREE.Vector3()};
const cen = [0, 0, 0];
function startGrab(hit) {
grab.idx = []; grab.w = []; grab.off = [];
for (let i = 0; i < N; i++) {
const dx = P[i*3]-hit.x, dy = P[i*3+1]-hit.y, dz = P[i*3+2]-hit.z, d2 = dx*dx + dy*dy + dz*dz;
const w = Math.exp(-d2 / (2 * 0.45 * 0.45));
if (w > 0.03) { grab.idx.push(i); grab.w.push(w); grab.off.push(dx, dy, dz); }
}
grab.origin.copy(hit); grab.target.copy(hit); grab.on = true;
}
function setTarget(v) {
const d = v.clone().sub(grab.origin);
if (d.length() > 2.4) d.setLength(2.4);
grab.target.copy(grab.origin).add(d);
}
function endGrab() { grab.on = false; }
function poke(hit, dir) {
for (let i = 0; i < N; i++) {
const dx = P[i*3]-hit.x, dy = P[i*3+1]-hit.y, dz = P[i*3+2]-hit.z;
const w = Math.exp(-(dx*dx + dy*dy + dz*dz) / (2 * 0.5 * 0.5));
if (w < 0.03) continue;
Q[i*3] -= dir.x * 0.035 * w; Q[i*3+1] -= dir.y * 0.035 * w; Q[i*3+2] -= dir.z * 0.035 * w;
}
}
function substep(sdt) {
const damp = 0.9965, G = -9.8 * sdt * sdt, vmax = 0.06;
const kGoal = 0.0015 + 0.02 * firm * firm, kSpr = 0.03 + 0.25 * firm;
let cx = 0, cy = 0, cz = 0;
for (let i = 0; i < N; i++) {
const k = i * 3;
let vx = (P[k] - Q[k]) * damp, vy = (P[k+1] - Q[k+1]) * damp, vz = (P[k+2] - Q[k+2]) * damp;
const m = Math.sqrt(vx*vx + vy*vy + vz*vz);
if (m > vmax) { const s = vmax / m; vx *= s; vy *= s; vz *= s; }
Q[k] = P[k]; Q[k+1] = P[k+1]; Q[k+2] = P[k+2];
P[k] += vx; P[k+1] += vy + G; P[k+2] += vz;
}
if (grab.on) {
for (let j = 0; j < grab.idx.length; j++) {
const i = grab.idx[j], w = grab.w[j] * 0.35, k = i * 3;
P[k] += (grab.target.x + grab.off[j*3] - P[k]) * w;
P[k+1] += (grab.target.y + grab.off[j*3+1] - P[k+1]) * w;
P[k+2] += (grab.target.z + grab.off[j*3+2] - P[k+2]) * w;
}
}
for (let i = 0; i < N; i++) { cx += P[i*3]; cy += P[i*3+1]; cz += P[i*3+2]; }
cx /= N; cy /= N; cz /= N; cen[0] = cx; cen[1] = cy; cen[2] = cz;
const sx = 1 + 0.2 * press, sy = 1 - 0.3 * press;
for (let i = 0; i < N; i++) {
const k = i * 3;
P[k] += (cx + REL[k] * sx - P[k]) * kGoal;
P[k+1] += (cy + REL[k+1] * sy - P[k+1]) * kGoal;
P[k+2] += (cz + REL[k+2] * sx - P[k+2]) * kGoal;
P[k] -= cx * 0.0006; P[k+2] -= cz * 0.0006;
}
for (let it = 0; it < 2; it++) {
for (let s = 0; s < NS; s++) {
const a = SA[s] * 3, b = SB[s] * 3;
const dx = P[b]-P[a], dy = P[b+1]-P[a+1], dz = P[b+2]-P[a+2];
const len = Math.sqrt(dx*dx + dy*dy + dz*dz) + 1e-9;
const f = (len - SL[s]) / len * 0.5 * kSpr;
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;
}
}
const pressY = H * (1 - 0.5 * press);
for (let i = 0; i < N; i++) {
const k = i * 3, l = i % NL;
if (press > 0.001) {
if (l === NL - 1 && P[k+1] > pressY) P[k+1] = pressY;
else if (l === 1 && P[k+1] > pressY * 0.5 + 0.02) P[k+1] = pressY * 0.5 + 0.02;
}
const rad = Math.hypot(P[k], P[k+2]);
if (rad > 2.3) { const s = 2.3 / rad; P[k] *= s; P[k+2] *= s; }
const fl = plateY(rad) + 0.004;
if (P[k+1] < fl) {
P[k+1] = fl;
Q[k] += (P[k] - Q[k]) * 0.25; Q[k+2] += (P[k+2] - Q[k+2]) * 0.25;
Q[k+1] = Math.min(Q[k+1], P[k+1]);
}
}
}
// ---------- textures ----------
function makeTex(seed, size) {
const c = document.createElement('canvas'); c.width = c.height = size;
const g = c.getContext('2d'); let s = seed;
const rnd = () => { s = (s * 1664525 + 1013904223) >>> 0; return s / 4294967296; };
const C = size / 2, sc = size / 1024;
g.fillStyle = '#ff8a00'; g.fillRect(0, 0, size, size);
g.fillStyle = '#fff3d6'; g.beginPath(); g.arc(C, C, C * 0.965, 0, 7); g.fill();
const n = 10, gap = 0.07;
for (let k = 0; k < n; k++) {
const a0 = k / n * Math.PI * 2 + gap / 2, a1 = (k + 1) / n * Math.PI * 2 - gap / 2;
g.save(); g.beginPath(); g.arc(C, C, C * 0.9, a0, a1); g.arc(C, C, C * 0.07, a1, a0, true); g.closePath(); g.clip();
const gr = g.createRadialGradient(C, C, C * 0.05, C, C, C * 0.9);
gr.addColorStop(0, '#ffc15a'); gr.addColorStop(1, '#ff8f0a');
g.fillStyle = gr; g.fill();
for (let j = 0; j < 1500; j++) {
const a = a0 + rnd() * (a1 - a0), r = C * (0.08 + 0.82 * Math.sqrt(rnd()));
const x = C + Math.cos(a) * r, y = C + Math.sin(a) * r;
const w = (3 + rnd() * 4) * sc, h = (7 + rnd() * 9) * sc;
g.fillStyle = 'hsla(' + (26 + rnd() * 16) + ',100%,' + (52 + rnd() * 22) + '%,0.6)';
g.beginPath(); g.ellipse(x, y, h, w, a, 0, 7); g.fill();
g.fillStyle = 'rgba(255,255,230,0.4)';
g.beginPath(); g.arc(x - 1.5 * sc, y - 1.5 * sc, 1.4 * sc, 0, 7); g.fill();
}
g.restore();
}
g.fillStyle = '#fff3d6'; g.beginPath(); g.arc(C, C, C * 0.06, 0, 7); g.fill();
const t = new THREE.CanvasTexture(c); t.colorSpace = THREE.SRGBColorSpace; t.anisotropy = 8; return t;
}
function makeBump(size, count, rep) {
const c = document.createElement('canvas'); c.width = c.height = size;
const g = c.getContext('2d'); g.fillStyle = '#000'; g.fillRect(0, 0, size, size);
for (let i = 0; i < count; i++) {
const x = Math.random() * size, y = Math.random() * size, r = size * (0.012 + Math.random() * 0.012);
const gr = g.createRadialGradient(x, y, 0, x, y, r);
gr.addColorStop(0, 'rgba(255,255,255,0.9)'); gr.addColorStop(1, 'rgba(255,255,255,0)');
g.fillStyle = gr; g.fillRect(x - r, y - r, r * 2, r * 2);
}
const t = new THREE.CanvasTexture(c); t.wrapS = t.wrapT = THREE.RepeatWrapping; t.repeat.set(rep[0], rep[1]); return t;
}
const texTop = makeTex(7, 1024), texMid = makeTex(99, 1024);
const bumpTop = makeBump(512, 1400, [4, 4]), bumpWall = makeBump(512, 2200, [8, 1]);
// ---------- deformable visual meshes ----------
function makeBind(list) {
const n = list.length;
const b = {n, cols: new Int32Array(n * 4), w: new Float32Array(n * 4), l0: new Int32Array(n), wl: new Float32Array(n)};
list.forEach((e, v) => {
const r = e[0], th = e[1], yl = e[2];
const fi = r / R * NR, i0 = Math.min(Math.floor(fi), NR - 1), wi = fi - i0;
const fa = th / (Math.PI * 2) * NA, a0 = Math.floor(fa), wa = fa - a0;
const c = [colOf(i0, a0), colOf(i0, a0 + 1), colOf(i0 + 1, a0), colOf(i0 + 1, a0 + 1)];
const w = [(1-wi)*(1-wa), (1-wi)*wa, wi*(1-wa), wi*wa];
for (let k = 0; k < 4; k++) { b.cols[v*4+k] = c[k]; b.w[v*4+k] = w[k]; }
const fl = yl * (NL - 1), l0 = Math.min(Math.floor(fl), NL - 2);
b.l0[v] = l0; b.wl[v] = fl - l0;
});
return b;
}
function updateMesh(m) {
const b = m.userData.bind, pos = m.geometry.attributes.position.array, col = m.userData.colors;
for (let v = 0; v < b.n; v++) {
let x = 0, y = 0, z = 0, th = 0; const l0 = b.l0[v], wl = b.wl[v];
for (let k = 0; k < 4; k++) {
const c = b.cols[v*4+k], w = b.w[v*4+k];
const a = (c * NL + l0) * 3, d = a + 3;
x += w * ((1-wl) * P[a] + wl * P[d]);
y += w * ((1-wl) * P[a+1] + wl * P[d+1]);
z += w * ((1-wl) * P[a+2] + wl * P[d+2]);
if (col) th += w * (P[(c*NL+NL-1)*3+1] - P[c*NL*3+1]);
}
pos[v*3] = x; pos[v*3+1] = y; pos[v*3+2] = z;
if (col) {
const g = Math.min(1.7, Math.max(0.8, H / Math.max(th, 0.15)));
col.array[v*3] = g; col.array[v*3+1] = g * 0.97; col.array[v*3+2] = g * 0.88;
}
}
m.geometry.attributes.position.needsUpdate = true;
if (col) col.needsUpdate = true;
m.geometry.computeVertexNormals();
}
function makeDisc(yl, nr, na, mat, colored) {
const list = [], uv = [], idx = [];
list.push([0, 0, yl]); uv.push(0.5, 0.5);
for (let i = 1; i <= nr; i++) for (let a = 0; a < na; a++) {
const r = i / nr * R, th = a / na * Math.PI * 2;
list.push([r, th, yl]); uv.push(0.5 + r * Math.cos(th) / (2 * R), 0.5 + r * Math.sin(th) / (2 * R));
}
const vi = (i, a) => i === 0 ? 0 : 1 + (i - 1) * na + (a % na);
for (let a = 0; a < na; a++) idx.push(0, vi(1, a + 1), vi(1, a));
for (let i = 1; i < nr; i++) for (let a = 0; a < na; a++) {
const A = vi(i, a), B = vi(i, a + 1), C = vi(i + 1, a), D = vi(i + 1, a + 1);
idx.push(A, B, C, B, D, C);
}
const geo = new THREE.BufferGeometry();
geo.setAttribute('position', new THREE.BufferAttribute(new Float32Array(list.length * 3), 3));
geo.setAttribute('uv', new THREE.BufferAttribute(new Float32Array(uv), 2));
geo.setIndex(idx);
const m = new THREE.Mesh(geo, mat);
m.userData.bind = makeBind(list); m.frustumCulled = false; m.castShadow = true; m.receiveShadow = true;
if (colored) { m.userData.colors = new THREE.BufferAttribute(new Float32Array(list.length * 3).fill(1), 3); geo.setAttribute('color', m.userData.colors); }
scene.add(m); return m;
}
function makeWall(na, nj, mat) {
const list = [], uv = [], idx = [];
for (let j = 0; j <= nj; j++) for (let a = 0; a <= na; a++) {
list.push([R, a / na * Math.PI * 2, j / nj]); uv.push(a / na, j / nj);
}
const vi = (a, j) => j * (na + 1) + a;
for (let j = 0; j < nj; j++) for (let a = 0; a < na; a++) {
idx.push(vi(a, j), vi(a, j + 1), vi(a + 1, j), vi(a + 1, j), vi(a, j + 1), vi(a + 1, j + 1));
}
const geo = new THREE.BufferGeometry();
geo.setAttribute('position', new THREE.BufferAttribute(new Float32Array(list.length * 3), 3));
geo.setAttribute('uv', new THREE.BufferAttribute(new Float32Array(uv), 2));
geo.setIndex(idx);
const m = new THREE.Mesh(geo, mat);
m.userData.bind = makeBind(list); m.frustumCulled = false; m.castShadow = true; m.receiveShadow = true;
scene.add(m); return m;
}
const matMid = new THREE.MeshPhysicalMaterial({map: texMid, color: 0xffffff, emissive: 0xff7a00, emissiveMap: texMid, emissiveIntensity: 0.35,
roughness: 0.3, vertexColors: true, side: THREE.DoubleSide});
const matTop = new THREE.MeshPhysicalMaterial({map: texTop, emissive: 0xff8a10, emissiveMap: texTop, emissiveIntensity: 0.3, bumpMap: bumpTop, bumpScale: 1.5,
roughness: 0.06, clearcoat: 1, clearcoatRoughness: 0.03, transparent: true, opacity: 0.55, vertexColors: true, envMapIntensity: 1.6, side: THREE.DoubleSide});
const matWall = new THREE.MeshPhysicalMaterial({color: 0xff9a14, emissive: 0xff6a00, emissiveIntensity: 0.28, roughness: 0.32, clearcoat: 0.7, clearcoatRoughness: 0.2,
bumpMap: bumpWall, bumpScale: 2.2, side: THREE.DoubleSide});
const meshMid = makeDisc(0.45, 12, 48, matMid, true);
const meshTop = makeDisc(1.0, 16, 64, matTop, true);
const meshWall = makeWall(64, 6, matWall);
const meshes = [meshMid, meshTop, meshWall];
const presser = new THREE.Mesh(new THREE.CylinderGeometry(1.9, 1.9, 0.06, 48),
new THREE.MeshPhysicalMaterial({color: 0xffffff, transparent: true, opacity: 0.3, roughness: 0.05, clearcoat: 1}));
presser.visible = false; scene.add(presser);
// ---------- juice drops ----------
const dropGeo = new THREE.SphereGeometry(0.07, 14, 10);
const dropMat = new THREE.MeshPhysicalMaterial({color: 0xff9400, emissive: 0xff6a00, emissiveIntensity: 0.3, roughness: 0.05, clearcoat: 1, transparent: true, opacity: 0.9});
const drops = [];
for (let i = 0; i < 40; i++) {
const m = new THREE.Mesh(dropGeo, dropMat); m.visible = false; scene.add(m);
drops.push({m, on: false, vx: 0, vy: 0, vz: 0, landed: false, age: 0});
}
function spawnDrop() {
const d = drops.find(x => !x.on); if (!d) return;
const a = Math.floor(Math.random() * NA), k = pid(NR, a, 1) * 3;
const ox = P[k] - cen[0], oz = P[k+2] - cen[2], l = Math.hypot(ox, oz) + 1e-6, sp = 0.8 + Math.random() * 0.8;
d.m.position.set(P[k] + ox / l * 0.08, P[k+1], P[k+2] + oz / l * 0.08);
d.vx = ox / l * sp; d.vz = oz / l * sp; d.vy = 0.6 + Math.random() * 0.8;
d.on = true; d.landed = false; d.age = 0; d.m.visible = true; d.m.scale.set(0.8 + Math.random() * 0.6, 0, 0);
d.m.scale.setScalar(0.8 + Math.random() * 0.6);
}
function updateDrops(dt) {
for (const d of drops) {
if (!d.on) continue;
const m = d.m;
if (!d.landed) {
d.vy -= 9.8 * dt; m.position.x += d.vx * dt; m.position.y += d.vy * dt; m.position.z += d.vz * dt;
const r = Math.hypot(m.position.x, m.position.z), fl = plateY(r) + 0.02;
if (m.position.y < fl) { m.position.y = fl; d.landed = true; d.age = 0; d.base = m.scale.x; m.scale.set(d.base * 1.8, d.base * 0.3, d.base * 1.8); }
} else {
d.age += dt;
if (d.age > 5) { const s = Math.max(0, 1 - (d.age - 5) / 2); m.scale.set(d.base * 1.8 * s, d.base * 0.3 * s, d.base * 1.8 * s); if (s <= 0) { d.on = false; m.visible = false; } }
}
}
}
// ---------- squeeze ----------
const sq = {on: false, t: 0, emitted: 0};
const emitTimes = [0.8, 1.05, 1.3, 1.6, 1.9];
function startSqueeze() { if (!sq.on) { sq.on = true; sq.t = 0; sq.emitted = 0; } }
function updateSqueeze(dt) {
if (!sq.on) { press = Math.max(0, press - dt * 3); }
else {
sq.t += dt; const t = sq.t;
const e = x => x * x * (3 - 2 * x);
if (t < 0.7) press = e(t / 0.7); else if (t < 2.2) press = 1; else if (t < 2.9) press = 1 - e((t - 2.2) / 0.7); else { press = 0; sq.on = false; }
while (sq.emitted < emitTimes.length && t > emitTimes[sq.emitted]) { spawnDrop(); spawnDrop(); sq.emitted++; }
}
const py = H * (1 - 0.5 * press);
presser.visible = press > 0.01;
presser.position.set(cen[0], py + 0.05, cen[2]);
}
function resetJelly() {
P.set(RS); Q.set(RS); endGrab(); sq.on = false; press = 0;
for (const d of drops) { d.on = false; d.m.visible = false; }
}
// ---------- UI ----------
const fm = document.getElementById('fm');
function setFirm(f) { firm = Math.max(0, Math.min(1, f)); fm.value = Math.round(firm * 100); }
fm.addEventListener('input', () => { firm = fm.value / 100; });
document.getElementById('sq').addEventListener('click', startSqueeze);
document.getElementById('rs').addEventListener('click', () => { resetJelly(); setFirm(0.5); });
const hint = document.getElementById('hint');
const hideHint = () => { hint.style.opacity = 0; };
setTimeout(hideHint, 9000);
// ---------- pointer ----------
const ray = new THREE.Raycaster(), ndc = new THREE.Vector2();
function getRay(e) {
const r = cv.getBoundingClientRect();
ndc.set((e.clientX - r.left) / r.width * 2 - 1, -((e.clientY - r.top) / r.height * 2 - 1));
ray.setFromCamera(ndc, camera); return ray.ray;
}
function pick(rr) {
let best = -1, bt = 1e9; const o = rr.origin, d = rr.direction;
for (let i = 0; i < N; i++) {
const vx = P[i*3] - o.x, vy = P[i*3+1] - o.y, vz = P[i*3+2] - o.z;
const t = vx*d.x + vy*d.y + vz*d.z; if (t < 0) continue;
const d2 = vx*vx + vy*vy + vz*vz - t*t;
if (d2 < 0.16 && t < bt) { bt = t; best = i; }
}
return best < 0 ? null : new THREE.Vector3(o.x + d.x * bt, o.y + d.y * bt, o.z + d.z * bt);
}
let ptr = null; const plane = new THREE.Plane(), tmp = new THREE.Vector3();
window.addEventListener('pointerdown', () => endDemo(), true);
cv.addEventListener('pointerdown', e => {
if (ptr) return; hideHint();
try { cv.setPointerCapture(e.pointerId); } catch (x) {}
const rr = getRay(e), hit = pick(rr);
if (hit) {
ptr = {id: e.pointerId, mode: 'grab'};
startGrab(hit); poke(hit, rr.direction.clone());
plane.setFromNormalAndCoplanarPoint(rr.direction.clone().negate(), hit);
} else ptr = {id: e.pointerId, mode: 'orbit', x: e.clientX, y: e.clientY};
});
cv.addEventListener('pointermove', e => {
if (!ptr || e.pointerId !== ptr.id) return;
if (ptr.mode === 'grab') { const rr = getRay(e); if (rr.intersectPlane(plane, tmp)) setTarget(tmp); }
else { az -= (e.clientX - ptr.x) * 0.008; el = Math.max(0.3, Math.min(1.45, el + (e.clientY - ptr.y) * 0.006)); ptr.x = e.clientX; ptr.y = e.clientY; }
});
const up = e => { if (ptr && e.pointerId === ptr.id) { if (ptr.mode === 'grab') endGrab(); ptr = null; } };
cv.addEventListener('pointerup', up); cv.addEventListener('pointercancel', up);
// ---------- demo ----------
const demo = {on: location.hash === '#demo', t: 0, seg: -1, sqDone: false};
const demoBadge = document.getElementById('demo');
if (demo.on) { demoBadge.style.display = 'block'; hideHint(); }
function endDemo() { if (demo.on) { demo.on = false; demoBadge.style.display = 'none'; endGrab(); } }
window.addEventListener('hashchange', () => { demo.on = location.hash === '#demo'; demo.t = 0; demoBadge.style.display = demo.on ? 'block' : 'none'; });
const ease = x => x * x * (3 - 2 * x);
const demoSegs = [
{a: 1.0, b: 1.6, hit: [0.3, 0.6, 0.2], tg: u => [0.3, 0.6 - 0.45 * Math.sin(Math.PI * u), 0.2]},
{a: 3.0, b: 5.6, hit: [1.35, 0.3, 0.1], tg: u => { const e = ease(Math.min(1, u / 0.7)); return [1.35 + 1.5 * e, 0.3 + 0.8 * e, 0.1]; }},
{a: 9.0, b: 9.5, hit: [-0.8, 0.6, 0.4], tg: u => [-0.8, 0.6 - 0.5 * Math.sin(Math.PI * u), 0.4]},
{a: 11.0, b: 11.9, hit: [-1.3, 0.3, -0.2], tg: u => [-1.3 - 1.2 * Math.sin(Math.PI * u), 0.3 + 0.3 * Math.sin(Math.PI * u), -0.2]}
];
function updateDemo(dt) {
demo.t += dt; const tt = demo.t % 20;
if (demo.t > 20 && tt < dt + 0.001) { demo.sqDone = false; }
az += dt * 0.12;
let want = -1;
demoSegs.forEach((s, i) => { if (tt >= s.a && tt < s.b) want = i; });
if (want !== demo.seg) {
endGrab();
if (want >= 0) { const h = new THREE.Vector3(...demoSegs[want].hit); startGrab(h); poke(h, new THREE.Vector3(0, -0.6, -0.4).normalize()); }
demo.seg = want;
}
if (want >= 0) { const s = demoSegs[want]; grab.target.set(...s.tg((tt - s.a) / (s.b - s.a))); }
if (tt < 7.5) setFirm(0.5);
else if (tt < 9) setFirm(0.5 + 0.45 * ease((tt - 7.5) / 1.5));
else if (tt < 10) setFirm(0.95);
else if (tt < 11) setFirm(0.95 - 0.75 * ease(tt - 10));
else if (tt < 18) setFirm(0.2);
else setFirm(0.2 + 0.3 * ease(Math.min(1, (tt - 18) / 1.5)));
if (tt > 13.5 && tt < 14 && !demo.sqDone) { demo.sqDone = true; startSqueeze(); }
if (tt < 1) demo.sqDone = demo.sqDone && tt > 13;
}
// ---------- loop ----------
function resize() {
const w = innerWidth, h = innerHeight;
renderer.setSize(w, h, false); camera.aspect = w / h;
dist = w / h < 0.8 ? 9.2 : 6.4; camera.updateProjectionMatrix();
}
addEventListener('resize', resize); resize();
let last = performance.now(), acc = 0;
function frame(now) {
requestAnimationFrame(frame);
const dt = Math.min(0.05, (now - last) / 1000); last = now; acc += dt;
if (demo.on) updateDemo(dt);
let n = 0;
while (acc >= 1 / 60 && n < 3) {
updateSqueeze(1 / 60);
for (let s = 0; s < 4; s++) substep(1 / 240);
acc -= 1 / 60; n++;
}
if (n === 3) acc = 0;
updateDrops(dt);
for (const m of meshes) updateMesh(m);
camera.position.set(camT.x + dist * Math.cos(el) * Math.sin(az), camT.y + dist * Math.sin(el), camT.z + dist * Math.cos(el) * Math.cos(az));
camera.lookAt(camT);
renderer.render(scene, camera);
}
requestAnimationFrame(frame);
</script>
</body>
</html>
