Prompt specification
Build or customize the Three.js 3D scene "Kitchen pouring comparison scene" inside the active workspace.
Engineering Constraints:
1. Runtime & Orbit: Use Three.js r160 with OrbitControls (damping enabled) and ACESFilmicToneMapping.
2. Frame Timing: Drive all motion strictly with clock.getDelta() so animation speed is FPS-independent.
3. Viewport Hygiene: Handle window resize cleanly, cap DPR at Math.min(window.devicePixelRatio, 2), and dispose geometries/materials/renderer on unmount.
Scene Metadata:
Scene: Kitchen pouring comparison scene
Category: Voxel Worlds · Style: Voxel 3D
License: MIT · Reference: https://three3d.app/scene/kitchen-pouring-comparison-scene-x1519
Scene Description Prompt:
Please use Three.js from cdnjs for a voxel scene based on this idea: kitchen pouring comparison scene. Compose two pitchers pouring visually distinct model liquids into cups; connect apparent flow differences to a viscosity concept while labeling the animation qualitative. Return one self-contained HTML file.
Working Three.js Starter (kitchen-pouring-comparison-scene-x1519):
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Voxel Kitchen · Viscosity Pouring Comparison (Qualitative)</title>
<style>
:root{
--a:#38bdf8; --b:#f59e0b; --ink:#e9f2f8; --dim:#9db2c4;
--card:rgba(10,18,30,.78); --line:rgba(255,255,255,.14);
}
*{box-sizing:border-box}
html,body{margin:0;height:100%;overflow:hidden;background:#0a1220;
font-family:ui-monospace,SFMono-Regular,Menlo,Consolas,"Liberation Mono",monospace;
color:var(--ink)}
#scene{position:fixed;inset:0;width:100%;height:100%;display:block;touch-action:none}
#labels{position:fixed;inset:0;pointer-events:none;z-index:5}
.card{position:fixed;z-index:6;background:var(--card);border:1px solid var(--line);
border-radius:12px;padding:12px 14px;backdrop-filter:blur(6px);
box-shadow:0 10px 30px rgba(0,0,0,.45);font-size:12px;line-height:1.55}
#title{top:14px;left:14px;max-width:360px}
#title h1{margin:0 0 6px;font-size:14px;letter-spacing:.06em;text-transform:uppercase}
#title p{margin:0;color:var(--dim)}
#badge{top:14px;right:14px;padding:8px 12px;border-radius:999px;font-size:11px;
letter-spacing:.12em;text-transform:uppercase;color:#ffe9b8;
background:rgba(60,42,8,.8);border:1px solid rgba(245,158,11,.5)}
#stats{top:60px;right:14px;width:250px}
#stats .row{display:flex;align-items:center;gap:8px;margin:8px 0 4px}
#stats .dot{width:9px;height:9px;border-radius:2px;display:inline-block}
#stats .dot.a{background:var(--a);box-shadow:0 0 10px var(--a)}
#stats .dot.b{background:var(--b);box-shadow:0 0 10px var(--b)}
#stats .lbl{font-size:11px;letter-spacing:.04em}
#stats .val{margin-left:auto;font-size:12px;color:var(--dim)}
#stats .bar{height:7px;background:rgba(255,255,255,.09);border-radius:4px;overflow:hidden}
#stats .bar i{display:block;height:100%;width:0;border-radius:4px;transition:width .12s linear}
#stats .bar i.a{background:linear-gradient(90deg,#0ea5e9,#7dd3fc)}
#stats .bar i.b{background:linear-gradient(90deg,#b45309,#fbbf24)}
#stats .note{color:var(--dim);font-size:10px;margin-top:3px}
#controls{bottom:14px;left:50%;transform:translateX(-50%);display:flex;gap:12px;
align-items:center;flex-wrap:wrap;justify-content:center;max-width:min(94vw,820px)}
button{font:inherit;font-size:12px;color:var(--ink);background:rgba(255,255,255,.08);
border:1px solid var(--line);border-radius:8px;padding:7px 12px;cursor:pointer}
button:hover{background:rgba(255,255,255,.16)}
.slider{display:flex;align-items:center;gap:8px;font-size:11px;color:var(--dim)}
.slider input{width:140px;accent-color:var(--b)}
.slider b{color:var(--b)}
#controls .hint{color:#6f8496;font-size:10px}
#panel{bottom:14px;left:14px;width:min(92vw,430px);max-height:52vh;overflow:auto}
#panel summary{cursor:pointer;font-size:12px;letter-spacing:.06em;text-transform:uppercase}
#panel p{margin:8px 0;color:var(--dim)}
#panel b{color:var(--ink)}
#panel ul{margin:6px 0;padding-left:18px;color:var(--dim)}
#panel li{margin:3px 0}
#panel .warn{border-left:2px solid var(--b);padding-left:9px;color:#e8d6ae}
.tag{position:absolute;transform:translate(-50%,-50%);background:rgba(8,14,24,.82);
border:1px solid var(--c,rgba(255,255,255,.22));border-radius:9px;padding:5px 9px;
font-size:11px;line-height:1.35;white-space:nowrap;backdrop-filter:blur(4px);
box-shadow:0 6px 18px rgba(0,0,0,.4);text-align:center}
.tag b{color:var(--c,#fff);font-weight:600}
.tag span{display:block;color:#9db2c4;font-size:10px}
.tag.small{font-size:10px;padding:4px 7px;opacity:.95}
@media (max-width:900px){
#panel{display:none}
#stats{top:auto;bottom:74px;right:10px;width:200px}
#title{max-width:60vw}
}
</style>
</head>
<body>
<canvas id="scene"></canvas>
<div id="labels"></div>
<div id="title" class="card">
<h1>Voxel Kitchen · Pouring Comparison</h1>
<p>Two pitchers, two model liquids, one cup each. The only difference is
<b>viscosity</b> — resistance to being pushed out of shape.</p>
</div>
<div id="badge">Qualitative animation · illustrative only</div>
<div id="stats" class="card">
<div class="row"><span class="dot a"></span><span class="lbl">A · low viscosity</span><span class="val" id="valA">0 %</span></div>
<div class="bar"><i class="a" id="barA"></i></div>
<div class="note" id="noteA">fills in ≈ 8 s (model proxy for flow rate)</div>
<div class="row"><span class="dot b"></span><span class="lbl">B · high viscosity</span><span class="val" id="valB">0 %</span></div>
<div class="bar"><i class="b" id="barB"></i></div>
<div class="note" id="noteB">fills in ≈ 21 s (model proxy for flow rate)</div>
</div>
<div id="controls" class="card">
<button id="replay">↻ Replay</button>
<button id="pause">⏸ Pause</button>
<label class="slider">Liquid B model viscosity
<input id="visc" type="range" min="0" max="100" value="80">
<b id="viscLbl">thick</b>
</label>
<span class="hint">drag = orbit · wheel = zoom · right-drag = pan</span>
</div>
<details id="panel" class="card" open>
<summary>Viscosity, in plain words</summary>
<p><b>Viscosity</b> is a fluid's resistance to flowing — to <i>shearing</i>, i.e. to being
deformed and pushed past itself. At the same driving force (here: gravity acting on a
tilted pitcher), a <b>more viscous</b> liquid:</p>
<ul>
<li>deforms and drains <b>slower</b> (thicker, more coherent stream);</li>
<li>falls as a <b>rope-like strand</b> instead of many separate droplets;</li>
<li>arrives at the cup <b>later</b>, so the cup fills <b>slower</b>;</li>
<li>can even gurgle — the flow breaks into intermittent pulses.</li>
</ul>
<p><b>Fair comparison:</b> both pitchers use the same geometry, the same authored stream path
and the same cup. Only the flow parameters differ (speed, droplet size, jitter, gurgling,
fill time), so what changes on screen is the liquid, not the setup.</p>
<p class="warn"><b>Qualitative model — not a simulation.</b> No Navier–Stokes solver, no surface
tension or wetting model. Droplets follow a hand-authored path; “≈ s to fill” is a <i>proxy
for flow rate</i>, not a measurement. Real viscosity differences are enormous
(water ≈ 1 mPa·s, honey ≈ 10 000 mPa·s), so thickness and speed are exaggerated for
visibility, and real pouring also depends on tilt angle, head height and the jug's shape.</p>
</details>
<script type="module">
import * as THREE from 'https://cdnjs.cloudflare.com/ajax/libs/three.js/0.160.0/three.module.min.js';
/* ------------------------------------------------------------------ setup */
const canvas = document.getElementById('scene');
const renderer = new THREE.WebGLRenderer({ canvas, antialias:true });
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xd9e5ec);
scene.fog = new THREE.Fog(0xd9e5ec, 17, 36);
const camera = new THREE.PerspectiveCamera(42, 1, 0.1, 100);
const hemi = new THREE.HemisphereLight(0xeaf6ff, 0x8a7a63, 0.85); scene.add(hemi);
const key = new THREE.DirectionalLight(0xfff4e2, 1.55);
key.position.set(4.5, 7.5, 5.2); key.castShadow = true;
key.shadow.mapSize.set(2048, 2048);
const sc = key.shadow.camera;
sc.left = -7; sc.right = 7; sc.top = 7; sc.bottom = -5; sc.near = 0.5; sc.far = 32;
key.shadow.bias = -0.0006; key.shadow.normalBias = 0.02;
scene.add(key);
const fillLight = new THREE.DirectionalLight(0xcfe6ff, 0.45);
fillLight.position.set(-5, 4, -3); scene.add(fillLight);
/* ------------------------------------------------------- voxel utilities */
const boxGeo = new THREE.BoxGeometry(1, 1, 1);
const _m = new THREE.Matrix4(), _q = new THREE.Quaternion(), _v = new THREE.Vector3(), _s = new THREE.Vector3();
const _c = new THREE.Color();
function mulberry32(a){ return function(){ a|=0; a=a+0x6D2B79F5|0;
let t=Math.imul(a^a>>>15,1|a); t=t+Math.imul(t^t>>>7,61|t)^t;
return ((t^t>>>14)>>>0)/4294967296; }; }
const rnd = mulberry32(20240711);
function V(arr, x, y, z, sx, sy, sz, color){ arr.push({x,y,z,sx,sy,sz,color}); }
function makeVoxels(items, opts = {}){
const mat = new THREE.MeshStandardMaterial({
color: 0xffffff, roughness: opts.rough ?? 0.85, metalness: opts.metal ?? 0.03,
transparent: !!opts.transparent, opacity: opts.opacity ?? 1,
depthWrite: opts.depthWrite ?? !opts.transparent,
side: opts.side ?? THREE.FrontSide, flatShading: true
});
const mesh = new THREE.InstancedMesh(boxGeo, mat, items.length);
items.forEach((it, i) => {
_m.compose(_v.set(it.x, it.y, it.z), _q.identity(), _s.set(it.sx, it.sy, it.sz));
mesh.setMatrixAt(i, _m);
mesh.setColorAt(i, _c.set(it.color));
});
mesh.instanceMatrix.needsUpdate = true;
if (mesh.instanceColor) mesh.instanceColor.needsUpdate = true;
mesh.castShadow = !opts.transparent; mesh.receiveShadow = true;
return mesh;
}
/* ------------------------------------------------------------ kitchen set */
const scenery = [];
// counter body + cabinet doors
V(scenery, 0, -0.85, -0.55, 7.4, 1.5, 5.1, 0x6f4f3a);
for (const x of [-3.35, -1.7, 0, 1.7, 3.35]){
V(scenery, x, -0.85, 2.02, 1.55, 1.34, 0.06, 0x7d5a40);
V(scenery, x, -0.34, 2.09, 0.52, 0.07, 0.07, 0xcfd6db);
}
// counter top tiles
const topCols = [0xcfc8bc, 0xc6beb1, 0xd4cdc1];
for (let ix = 0; ix < 19; ix++){
for (let iz = 0; iz < 13; iz++){
V(scenery, -3.6 + ix*0.4, -0.07, -2.8 + iz*0.4, 0.4, 0.14, 0.4,
topCols[(ix + iz*2) % 3]);
}
}
// backsplash wall
const wallCols = [0xefe8dc, 0xe8e0d3, 0xf3ece1];
for (let ix = 0; ix < 20; ix++){
for (let iy = 0; iy < 10; iy++){
let col = wallCols[(ix*3 + iy) % 3];
if (iy === 2) col = (ix % 2) ? 0xcfe0ea : 0xbfd6e4;
V(scenery, -3.8 + ix*0.4, 0.2 + iy*0.4, -3.15, 0.4, 0.4, 0.4, col);
}
}
// upper cabinets
for (const [x0, x1] of [[-3.8, -0.4], [0.4, 3.8]]){
const cx = (x0 + x1)/2;
V(scenery, cx, 3.3, -2.65, x1 - x0, 1.4, 0.6, 0x8a6a4f);
for (const dx of [-0.9, 0.9]){
V(scenery, cx + dx, 3.3, -2.32, 1.65, 1.26, 0.06, 0x7b5d45);
V(scenery, cx + dx, 2.82, -2.28, 0.44, 0.06, 0.06, 0xd8dee2);
}
}
// salt & pepper
for (const [x, col] of [[-0.55, 0xf2f4f2], [-0.25, 0x40474d]]){
for (let ix = 0; ix < 3; ix++) for (let iy = 0; iy < 3; iy++) for (let iz = 0; iz < 4; iz++){
V(scenery, x + (ix-1)*0.11, 0.055 + iz*0.11, -1.55 + (iy-1)*0.11, 0.11, 0.11, 0.11, col);
}
}
// potted plant
for (let ix = 0; ix < 4; ix++) for (let iy = 0; iy < 4; iy++) for (let iz = 0; iz < 3; iz++){
if (ix === 0 || ix === 3 || iy === 0 || iy === 3 || iz === 0)
V(scenery, 3.05 + (ix-1.5)*0.12, 0.06 + iz*0.12, -1.75 + (iy-1.5)*0.12, 0.12,0.12,0.12, 0xb4623f);
}
V(scenery, 3.05, 0.40, -1.75, 0.36, 0.08, 0.36, 0x4a3b28);
for (let i = 0; i < 16; i++){
V(scenery, 3.05 + (rnd()-0.5)*0.42, 0.46 + rnd()*0.34, -1.75 + (rnd()-0.5)*0.42,
0.11, 0.11, 0.11, [0x3f8f4a, 0x57a862, 0x2f7a3c][i % 3]);
}
const sceneryMesh = makeVoxels(scenery); scene.add(sceneryMesh);
// under-cabinet light strip (emissive)
const strip = new THREE.Mesh(new THREE.BoxGeometry(7.0, 0.06, 0.2),
new THREE.MeshStandardMaterial({ color:0xfff3d8, emissive:0xffe2a8, emissiveIntensity:0.9 }));
strip.position.set(0, 2.58, -2.6); scene.add(strip);
/* --------------------------------------------------------- scene geometry */
const CFG = {
cupCell: 0.15, cupOuter: 6, cupLevels: 4,
pCell: 0.22, pW: 5, pD: 4, pH: 7,
tiltMax: 0.78, pivotY: 1.35, baseZ: -1.60, cupDrop: 0.85
};
const offZ = (CFG.pD / 2) * CFG.pCell; // base centre → rear edge
const pHeight = CFG.pH * CFG.pCell;
const cupRimY = 5 * CFG.cupCell;
function pitcherVoxels(){
const it = [], { pW:w, pD:d, pH:h, pCell:c } = CFG;
const x0 = -(w-1)/2*c, z0 = -(d-1)/2*c;
for (let iz = 0; iz < h; iz++) for (let ix = 0; ix < w; ix++) for (let iy = 0; iy < d; iy++){
const wall = ix === 0 || ix === w-1 || iy === 0 || iy === d-1 || iz === 0;
if (!wall) continue;
if (iz === h-1 && iy === d-1 && ix > 0 && ix < w-1) continue; // V-shaped pour notch
let col = 0xe9f2f7;
if (iz === 0) col = 0xbcd2de;
else if (iz === h-1) col = 0xd3e4ee;
else if (ix === 0 || ix === w-1) col = 0xdceaf2;
V(it, x0 + ix*c, (iz + 0.5)*c, z0 + iy*c + offZ, c, c, c, col);
}
// handle on the back-left side
for (let iz = 2; iz <= 4; iz++)
V(it, x0 - 0.5*c, (iz + 0.5)*c, z0 + 1.5*c + offZ, c, c, c, 0xd3e4ee);
return it;
}
function cupVoxels(){
const it = [], c = CFG.cupCell, n = CFG.cupOuter, h = 5;
const x0 = -(n-1)/2*c, z0 = -(n-1)/2*c;
for (let iz = 0; iz < h; iz++) for (let ix = 0; ix < n; ix++) for (let iy = 0; iy < n; iy++){
const wall = ix === 0 || ix === n-1 || iy === 0 || iy === n-1 || iz === 0;
if (!wall) continue;
V(it, x0 + ix*c, (iz + 0.5)*c, z0 + iy*c, c, c, c, iz === 0 ? 0xc9dbe5 : 0xeaf3f8);
}
for (let iz = 1; iz <= 2; iz++)
V(it, (n/2)*c + 0.5*c, (iz + 0.5)*c, 0, c, c, c, 0xd9e8f0);
return it;
}
/* ---------------------------------------------- flow parameter presets */
function paramsFor(v){ // v: 0 = thin/water-like, 1 = thick/honey-like
return {
duration : 8 + 16*v, // seconds to complete the pour
travel : 0.50 + 1.40*v, // droplet flight time
size : 0.075 + 0.095*v, // droplet / strand thickness
interval : 0.032 + 0.078*v, // emission interval while pouring
jitter : 0.050 - 0.038*v, // lateral scatter
spread : 0.075 - 0.060*v, // spout width
glug : v > 0.50,
glugCycle: 0.95 + 0.55*v,
splashN : Math.round(5 - 2*v),
splashSz : 0.045 + 0.075*v,
splashLife:0.45 + 0.55*v,
splashUp : 1.45 - 0.75*v,
wobble : v * 0.030 // rope-like undulation
};
}
/* ------------------------------------------------------------- stations */
const stations = [];
function buildStation(id, x, color, css){
const st = { id, x, color, css, params: null, droplets: [], splashes: [],
cupLevel: 0, emitAcc: 0, glugPhase: 0, spout: new THREE.Vector3(),
cupTarget: new THREE.Vector3() };
const group = new THREE.Group();
group.position.set(x, CFG.pivotY, CFG.baseZ - offZ);
scene.add(group); st.group = group;
const body = makeVoxels(pitcherVoxels(), { transparent:true, opacity:0.30, rough:0.10,
side:THREE.DoubleSide, depthWrite:false });
body.renderOrder = 3; group.add(body); st.body = body;
st.fill = new THREE.InstancedMesh(boxGeo,
new THREE.MeshStandardMaterial({ color:0xffffff, roughness:0.32, metalness:0.04, flatShading:true }), 48);
st.fill.frustumCulled = false; group.add(st.fill);
st.anchor = new THREE.Object3D();
st.anchor.position.set(0, (CFG.pH - 1) * CFG.pCell - 0.03, offZ * 2);
group.add(st.anchor);
// place the cup under the spout of the fully tilted pitcher
group.rotation.x = CFG.tiltMax; group.updateMatrixWorld(true);
st.anchor.getWorldPosition(st.spout);
const cupZ = st.spout.z + CFG.cupDrop;
group.rotation.x = 0; group.updateMatrixWorld(true);
const cupGroup = new THREE.Group();
cupGroup.position.set(x, 0, cupZ); scene.add(cupGroup); st.cupGroup = cupGroup;
const glass = makeVoxels(cupVoxels(), { transparent:true, opacity:0.26, rough:0.08,
side:THREE.DoubleSide, depthWrite:false });
glass.renderOrder = 3; cupGroup.add(glass);
st.cupFill = new THREE.InstancedMesh(boxGeo,
new THREE.MeshStandardMaterial({ color:0xffffff, roughness:0.30, metalness:0.05, flatShading:true }), 80);
st.cupFill.frustumCulled = false; cupGroup.add(st.cupFill);
st.drops = new THREE.InstancedMesh(boxGeo,
new THREE.MeshStandardMaterial({ color:0xffffff, roughness:0.22, metalness:0.05, flatShading:true }), 320);
st.drops.frustumCulled = false; scene.add(st.drops);
st.splash = new THREE.InstancedMesh(boxGeo,
new THREE.MeshStandardMaterial({ color:0xffffff, roughness:0.25, metalness:0.05, flatShading:true }), 220);
st.splash.frustumCulled = false; scene.add(st.splash);
for (const m of [st.fill, st.cupFill, st.drops, st.splash]){
for (let i = 0; i < m.count; i++) m.setColorAt(i, _c.set(color));
if (m.instanceColor) m.instanceColor.needsUpdate = true;
}
st.cupTarget.set(x, cupRimY + 0.02, cupZ);
st.cupWorld = new THREE.Vector3(x, 0, cupZ);
return st;
}
const stA = buildStation('A', -2.45, 0x38bdf8, '#38bdf8');
const stB = buildStation('B', 2.45, 0xf59e0b, '#f59e0b');
stations.push(stA, stB);
stA.params = paramsFor(0.0); // fixed low-viscosity reference liquid
stB.params = paramsFor(0.8); // driven by the slider
/* ------------------------------------------------- pitcher liquid surface */
const columns = [];
for (let ix = 1; ix < CFG.pW - 1; ix++) for (let iy = 1; iy < CFG.pD - 1; iy++){
const zc = -(CFG.pD-1)/2*CFG.pCell + iy*CFG.pCell;
columns.push({ x: -(CFG.pW-1)/2*CFG.pCell + ix*CFG.pCell, zg: zc + offZ });
}
function countAt(H, sinT, cosT){
let c = 0;
for (const col of columns){
const ymax = (H - CFG.pivotY + col.zg * sinT) / cosT;
c += Math.max(0, Math.min(CFG.pH - 1, Math.floor(ymax / CFG.pCell) - 1));
}
return c;
}
function rimHeight(sinT, cosT){ // world height of the notch floor (spill level)
return CFG.pivotY + (CFG.pH - 1) * CFG.pCell * cosT - offZ * 2 * sinT;
}
const _col = new THREE.Color();
function updatePitcherFill(st, H, sinT, cosT){
let n = 0;
for (const col of columns){
const ymax = (H - CFG.pivotY + col.zg * sinT) / cosT;
const levels = Math.max(0, Math.min(CFG.pH - 1, Math.floor(ymax / CFG.pCell) - 1));
for (let k = 0; k < levels; k++){
_m.compose(_v.set(col.x, (k + 1.5) * CFG.pCell, col.zg - offZ),
_q.identity(), _s.set(CFG.pCell, CFG.pCell, CFG.pCell));
st.fill.setMatrixAt(n, _m);
const shade = 0.82 + ((n * 37) % 7) * 0.045;
_col.set(st.color).multiplyScalar(shade);
st.fill.setColorAt(n, _col); n++;
}
}
st.fill.count = n;
st.fill.instanceMatrix.needsUpdate = true;
if (st.fill.instanceColor) st.fill.instanceColor.needsUpdate = true;
}
function updateCupFill(st, level){
const c = CFG.cupCell, n = CFG.cupOuter - 2;
let idx = 0;
for (let ix = 0; ix < n; ix++) for (let iy = 0; iy < n; iy++) for (let k = 0; k < level; k++){
_m.compose(_v.set((ix - (n-1)/2) * c, (k + 1.5) * c, (iy - (n-1)/2) * c),
_q.identity(), _s.set(c, c, c));
st.cupFill.setMatrixAt(idx, _m);
const top = (k === level - 1);
_col.set(st.color).multiplyScalar(top ? 1.12 : (0.86 + ((idx * 29) % 6) * 0.04));
st.cupFill.setColorAt(idx, _col); idx++;
}
st.cupFill.count = idx;
st.cupFill.instanceMatrix.needsUpdate = true;
if (st.cupFill.instanceColor) st.cupFill.instanceColor.needsUpdate = true;
}
/* ------------------------------------------------------------- animation */
const TILT_START = 0.35, TILT_END = 1.15;
let clock = 0, paused = false;
const ease = k => k * k * (3 - 2 * k);
function resetSim(){
clock = 0;
for (const st of stations){
st.droplets.length = 0; st.splashes.length = 0;
st.cupLevel = 0; st.emitAcc = 0; st.glugPhase = 0;
st.group.rotation.x = 0; st.group.updateMatrixWorld(true);
}
}
function emit(st){
const p = st.params;
const p0 = st.spout.clone();
p0.x += (Math.random() - 0.5) * p.spread;
p0.y -= 0.03;
const p1 = p0.clone().add(new THREE.Vector3(0, -0.30, 0.55));
const p2 = st.cupTarget.clone();
st.droplets.push({
p0, p1, p2, t0: clock, dur: p.travel * (0.9 + Math.random() * 0.2),
size: p.size * (0.82 + Math.random() * 0.36),
jx: (Math.random() - 0.5) * 2, jz: (Math.random() - 0.5) * 2,
phase: Math.random() * Math.PI * 2, spin: Math.random() * Math.PI * 2
});
}
function spawnSplash(st){
const p = st.params;
for (let i = 0; i < p.splashN; i++){
const a = Math.random() * Math.PI * 2, sp = (0.5 + Math.random()) * 0.7;
st.splashes.push({
pos: st.cupTarget.clone(),
vel: new THREE.Vector3(Math.cos(a) * sp * 0.55, p.splashUp * (0.55 + Math.random() * 0.7),
Math.sin(a) * sp * 0.55),
life: 0, max: p.splashLife * (0.8 + Math.random() * 0.4),
size: p.splashSz * (0.7 + Math.random() * 0.6)
});
}
}
const _bez = new THREE.Vector3(), _tan = new THREE.Vector3(), _up = new THREE.Vector3(0,1,0);
function bezier(d, s, out){
const u = 1 - s;
out.set(u*u*d.p0.x + 2*u*s*d.p1.x + s*s*d.p2.x,
u*u*d.p0.y + 2*u*s*d.p1.y + s*s*d.p2.y,
u*u*d.p0.z + 2*u*s*d.p1.z + s*s*d.p2.z);
return out;
}
function updateStation(st, dt){
const p = st.params;
const dur = p.duration;
const pourEnd = TILT_END + dur;
// --- tilt schedule
let ang = 0, pouring = false;
if (clock < TILT_START) ang = 0;
else if (clock < TILT_END){ ang = CFG.tiltMax * ease((clock - TILT_START) / (TILT_END - TILT_START)); }
else if (clock < pourEnd){ ang = CFG.tiltMax; pouring = true; }
else { ang = CFG.tiltMax * (1 - ease(Math.min(1, (clock - pourEnd) / 0.9))); }
st.group.rotation.x = ang; st.group.updateMatrixWorld(true);
st.anchor.getWorldPosition(st.spout);
const sinT = Math.sin(ang), cosT = Math.cos(ang);
// --- liquid level inside the pitcher (surface stays world-horizontal)
const frac = Math.max(0, Math.min(1, (clock - TILT_START) / dur));
let H = 2.67 + (1.20 - 2.67) * frac; // authored drain-down
H = Math.min(H, rimHeight(sinT, cosT)); // never above the notch
updatePitcherFill(st, H, sinT, cosT);
// --- cup level (integer voxel steps)
const targetLevel = Math.min(CFG.cupLevels, Math.floor(frac * CFG.cupLevels));
if (targetLevel !== st.cupLevel){ st.cupLevel = targetLevel; updateCupFill(st, st.cupLevel); }
// --- emission
if (pouring && frac < 1){
st.glugPhase += dt;
let active = true;
if (p.glug){ const ph = (st.glugPhase % p.glugCycle) / p.glugCycle; active = ph < 0.62; }
if (active){
st.emitAcc += dt;
while (st.emitAcc >= p.interval){ st.emitAcc -= p.interval; emit(st); }
}
}
// --- droplet flight
const p0 = st.spout, p1 = _v.copy(p0).add(new THREE.Vector3(0, -0.30, 0.55));
for (let i = st.droplets.length - 1; i >= 0; i--){
const d = st.droplets[i];
const s = (clock - d.t0) / d.dur;
if (s >= 1){ st.droplets.splice(i, 1); spawnSplash(st); continue; }
bezier(d, s, _bez);
const w = Math.sin(Math.PI * s);
_bez.x += d.jx * p.jitter * w;
_bez.z += d.jz * p.jitter * w;
if (p.wobble) _bez.x += p.wobble * Math.sin(s * 7 + d.phase) * w;
_tan.set(2*(1-s)*(d.p1.x - d.p0.x) + 2*s*(d.p2.x - d.p1.x),
2*(1-s)*(d.p1.y - d.p0.y) + 2*s*(d.p2.y - d.p1.y),
2*(1-s)*(d.p1.z - d.p0.z) + 2*s*(d.p2.z - d.p1.z)).normalize();
if (st.id === 'A'){ // thin liquid: tumbling droplets
_q.setFromEuler(new THREE.Euler(d.spin + clock * 2.2, d.phase, d.spin * 0.7));
_s.set(d.size, d.size, d.size);
} else { // viscous liquid: stretched strand
_q.setFromUnitVectors(_up, _tan);
_s.set(d.size, d.size * 1.85, d.size);
}
_m.compose(_bez, _q, _s);
st.drops.setMatrixAt(i, _m);
st.drops.setColorAt(i, _col.set(st.color).multiplyScalar(0.9 + (i % 5) * 0.03));
}
st.drops.count = st.droplets.length;
st.drops.instanceMatrix.needsUpdate = true;
if (st.drops.instanceColor) st.drops.instanceColor.needsUpdate = true;
// --- splashes
for (let i = st.splashes.length - 1; i >= 0; i--){
const s = st.splashes[i];
s.life += dt;
s.vel.y -= 7.5 * dt;
s.pos.addScaledVector(s.vel, dt);
if (s.life > s.max || s.pos.y < cupRimY - 0.08){ st.splashes.splice(i, 1); continue; }
const k = 1 - s.life / s.max;
_m.compose(s.pos, _q.identity(), _s.set(s.size * k, s.size * k, s.size * k));
st.splash.setMatrixAt(i, _m);
}
st.splash.count = st.splashes.length;
st.splash.instanceMatrix.needsUpdate = true;
}
/* ---------------------------------------------------------- orbit camera */
const cam = {
theta: 0.18, phi: 1.14, radius: 9.4,
dTheta: 0.18, dPhi: 1.14, dRadius: 9.4,
target: new THREE.Vector3(0, 1.05, -0.35), idle: 0, auto: true
};
let dragging = false, lastX = 0, lastY = 0, button = 0;
canvas.addEventListener('pointerdown', e => {
dragging = true; lastX = e.clientX; lastY = e.clientY; button = e.button;
canvas.setPointerCapture(e.pointerId); cam.idle = 0;
});
canvas.addEventListener('pointermove', e => {
if (!dragging) return;
const dx = e.clientX - lastX, dy = e.clientY - lastY;
lastX = e.clientX; lastY = e.clientY; cam.idle = 0;
if (button === 2 || e.shiftKey){
const right = new THREE.Vector3(Math.cos(cam.theta), 0, -Math.sin(cam.theta));
const up = new THREE.Vector3(0, 1, 0);
cam.target.addScaledVector(right, -dx * 0.006).addScaledVector(up, dy * 0.006);
cam.target.y = Math.max(-0.5, Math.min(2.6, cam.target.y));
} else {
cam.dTheta -= dx * 0.006;
cam.dPhi = Math.max(0.22, Math.min(1.48, cam.dPhi - dy * 0.005));
}
});
window.addEventListener('pointerup', () => dragging = false);
canvas.addEventListener('pointercancel', () => dragging = false);
canvas.addEventListener('contextmenu', e => e.preventDefault());
canvas.addEventListener('wheel', e => {
e.preventDefault();
cam.dRadius = Math.max(4.2, Math.min(18, cam.dRadius * Math.exp(e.deltaY * 0.001)));
cam.idle = 0;
}, { passive:false });
function updateCamera(dt){
cam.idle += dt;
if (cam.auto && cam.idle > 3.5) cam.dTheta += 0.10 * dt;
const k = Math.min(1, dt * 8);
cam.theta += (cam.dTheta - cam.theta) * k;
cam.phi += (cam.dPhi - cam.phi) * k;
cam.radius+= (cam.dRadius- cam.radius)*k;
const sp = Math.sin(cam.phi), cp = Math.cos(cam.phi);
camera.position.set(cam.target.x + cam.radius * sp * Math.sin(cam.theta),
cam.target.y + cam.radius * cp,
cam.target.z + cam.radius * sp * Math.cos(cam.theta));
camera.lookAt(cam.target);
}
/* ---------------------------------------------------------------- labels */
const labelHost = document.getElementById('labels');
function makeTag(html, css){ const el = document.createElement('div'); el.className = 'tag';
el.style.setProperty('--c', css); el.innerHTML = html; labelHost.appendChild(el); return el; }
function place(el, pos){
const v = pos.clone().project(camera);
if (v.z > 1){ el.style.display = 'none'; return; }
el.style.display = 'block';
el.style.left = ((v.x * 0.5 + 0.5) * innerWidth) + 'px';
el.style.top = ((-v.y * 0.5 + 0.5) * innerHeight) + 'px';
}
const tagA = makeTag('<b>A · low viscosity</b><span>thin · fast droplets · cup fills quickly</span>', '#38bdf8');
const tagB = makeTag('<b>B · high viscosity</b><span>thick · slow strand · cup fills slowly</span>', '#f59e0b');
const tagSA = makeTag('<b>fast · narrow stream</b>', '#38bdf8'); tagSA.classList.add('small');
const tagSB = makeTag('<b>slow · thick strand</b>', '#f59e0b'); tagSB.classList.add('small');
const tagCA = makeTag('<b>cup 0 %</b><span>fills in ≈ 8 s (model proxy)</span>', '#38bdf8');
const tagCB = makeTag('<b>cup 0 %</b><span>fills in ≈ 21 s (model proxy)</span>', '#f59e0b');
tagCA.classList.add('small'); tagCB.classList.add('small');
const tagAPos = new THREE.Vector3(stA.x, 2.55, -1.35);
const tagBPos = new THREE.Vector3(stB.x, 2.55, -1.35);
const _mid = new THREE.Vector3();
function updateLabels(){
place(tagA, tagAPos); place(tagB, tagBPos);
const mA = _mid.copy(stA.spout).lerp(stA.cupTarget, 0.45); mA.y += 0.30; mA.x -= 0.05;
const mB = _mid.copy(stB.spout).lerp(stB.cupTarget, 0.45); mB.y += 0.38; mB.x += 0.05;
place(tagSA, mA); place(tagSB, mB);
const cA = new THREE.Vector3(stA.cupWorld.x, cupRimY + 0.55, stA.cupWorld.z);
const cB = new THREE.Vector3(stB.cupWorld.x, cupRimY + 0.55, stB.cupWorld.z);
place(tagCA, cA); place(tagCB, cB);
tagCA.innerHTML = `<b>cup ${Math.round(stA.cupLevel / CFG.cupLevels * 100)} %</b><span>fills in ≈ ${Math.round(stA.params.duration)} s (model proxy)</span>`;
tagCB.innerHTML = `<b>cup ${Math.round(stB.cupLevel / CFG.cupLevels * 100)} %</b><span>fills in ≈ ${Math.round(stB.params.duration)} s (model proxy)</span>`;
}
/* --------------------------------------------------------------- HUD/UI */
const barA = document.getElementById('barA'), barB = document.getElementById('barB');
const valA = document.getElementById('valA'), valB = document.getElementById('valB');
const noteB = document.getElementById('noteB');
const visc = document.getElementById('visc'), viscLbl = document.getElementById('viscLbl');
const pauseBtn = document.getElementById('pause');
function applyViscosity(v){
stB.params = paramsFor(v);
const word = v < 0.2 ? 'thin' : v < 0.45 ? 'light' : v < 0.7 ? 'medium' : v < 0.9 ? 'thick' : 'honey-like';
viscLbl.textContent = word;
noteB.textContent = `fills in ≈ ${Math.round(stB.params.duration)} s (model proxy for flow rate)`;
resetSim();
}
visc.addEventListener('input', () => applyViscosity(visc.value / 100));
document.getElementById('replay').addEventListener('click', resetSim);
pauseBtn.addEventListener('click', () => {
paused = !paused;
pauseBtn.textContent = paused ? '▶ Resume' : '⏸ Pause';
});
applyViscosity(0.8);
/* ------------------------------------------------------------- main loop */
let last = performance.now();
function frame(now){
const dt = Math.min(0.05, (now - last) / 1000); last = now;
if (!paused){
clock += dt;
const maxEnd = Math.max(...stations.map(s => TILT_END + s.params.duration));
if (clock > maxEnd + 3.2) resetSim();
}
for (const st of stations) updateStation(st, paused ? 0 : dt);
updateCamera(dt);
updateLabels();
barA.style.width = (stA.cupLevel / CFG.cupLevels * 100) + '%';
barB.style.width = (stB.cupLevel / CFG.cupLevels * 100) + '%';
valA.textContent = Math.round(stA.cupLevel / CFG.cupLevels * 100) + ' %';
valB.textContent = Math.round(stB.cupLevel / CFG.cupLevels * 100) + ' %';
renderer.render(scene, camera);
requestAnimationFrame(frame);
}
function resize(){
const w = innerWidth, h = innerHeight;
renderer.setSize(w, h, false);
camera.aspect = w / h; camera.updateProjectionMatrix();
}
addEventListener('resize', resize);
resize();
requestAnimationFrame(frame);
</script>
</body>
</html>
```