// "Pha trà" loading scene (three.js): a kettle tilts and pours into a porcelain cup, tea leaves swirl, steam rises, // and the tea darkens as the model downloads. startBrew(canvas) -> { setProgress(0..1), stop() }. import * as THREE from './vendor/three.module.min.js'; export function startBrew(canvas) { const renderer = new THREE.WebGLRenderer({ canvas, antialias: true }); renderer.setPixelRatio(Math.min(2, devicePixelRatio || 1)); renderer.setClearColor(0x201911, 1); const scene = new THREE.Scene(); scene.fog = new THREE.Fog(0x201911, 9, 18); const camera = new THREE.PerspectiveCamera(32, 1, 0.1, 50); camera.position.set(0, 3.1, 8.2); camera.lookAt(0.35, 1.0, 0); scene.add(new THREE.HemisphereLight(0xfff1dc, 0x2a1d12, 0.9)); const key = new THREE.DirectionalLight(0xffe2b8, 1.6); key.position.set(-3, 6, 4); scene.add(key); const rim = new THREE.PointLight(0xe69a4c, 18, 12, 2); rim.position.set(2.5, 2.2, -2.5); scene.add(rim); // table const table = new THREE.Mesh(new THREE.CircleGeometry(14, 64), new THREE.MeshStandardMaterial({ color: 0x3b2a1c, roughness: 0.85 })); table.rotation.x = -Math.PI / 2; scene.add(table); // cup (lathe profile, porcelain) const cupPts = [[0, 0.02], [0.62, 0.02], [0.7, 0.08], [0.86, 0.95], [0.9, 1.0], [0.84, 1.0], [0.8, 0.95], [0.64, 0.14], [0, 0.14]] .map(([x, y]) => new THREE.Vector2(x, y)); const porcelain = new THREE.MeshStandardMaterial({ color: 0xf1e9dc, roughness: 0.35, metalness: 0.0, side: THREE.DoubleSide }); const cup = new THREE.Mesh(new THREE.LatheGeometry(cupPts, 64), porcelain); cup.position.set(-0.55, 0, 0.4); scene.add(cup); const band = new THREE.Mesh(new THREE.TorusGeometry(0.885, 0.018, 8, 64), new THREE.MeshStandardMaterial({ color: 0x2f6b7b, roughness: 0.5 })); band.rotation.x = Math.PI / 2; band.position.set(-0.55, 0.88, 0.4); scene.add(band); // tea surface (rises while pouring, darkens with download progress) const teaMat = new THREE.MeshStandardMaterial({ color: 0xe8c28a, roughness: 0.15, metalness: 0.05, transparent: true, opacity: 0.92 }); const tea = new THREE.Mesh(new THREE.CircleGeometry(1, 48), teaMat); tea.rotation.x = -Math.PI / 2; tea.position.set(-0.55, 0.3, 0.4); scene.add(tea); const teaLight = new THREE.Color(0xe8c28a), teaDark = new THREE.Color(0x8f4b10); // tea leaves on the surface const leafMat = new THREE.MeshStandardMaterial({ color: 0x4f6b3a, roughness: 0.7, side: THREE.DoubleSide }); const leaves = []; for (let i = 0; i < 6; i++) { const l = new THREE.Mesh(new THREE.CircleGeometry(0.09, 12), leafMat); l.scale.set(1.9, 0.75, 1); l.rotation.x = -Math.PI / 2; l.userData = { r: 0.15 + 0.08 * i, a: i * 1.1, s: 0.5 + 0.12 * i }; scene.add(l); leaves.push(l); } // kettle: body, lid, knob, spout, handle — pivots around its base edge to pour const clay = new THREE.MeshStandardMaterial({ color: 0x3c4a3f, roughness: 0.55, metalness: 0.05 }); const accent = new THREE.MeshStandardMaterial({ color: 0x7d9884, roughness: 0.5 }); const kettle = new THREE.Group(), pivot = new THREE.Group(); const bodyPts = [[0, 0], [0.7, 0], [0.95, 0.25], [1.0, 0.6], [0.88, 0.98], [0.55, 1.18], [0, 1.2]].map(([x, y]) => new THREE.Vector2(x, y)); kettle.add(new THREE.Mesh(new THREE.LatheGeometry(bodyPts, 48), clay)); const lid = new THREE.Mesh(new THREE.SphereGeometry(0.42, 32, 12, 0, Math.PI * 2, 0, Math.PI / 2), accent); lid.position.y = 1.12; kettle.add(lid); const knob = new THREE.Mesh(new THREE.SphereGeometry(0.11, 16, 12), accent); knob.position.y = 1.56; kettle.add(knob); const spout = new THREE.Mesh(new THREE.CylinderGeometry(0.07, 0.15, 1.0, 16), clay); spout.position.set(-1.08, 0.72, 0); spout.rotation.z = Math.PI / 3.2; kettle.add(spout); const handle = new THREE.Mesh(new THREE.TorusGeometry(0.42, 0.07, 10, 32, Math.PI * 1.15), clay); handle.position.set(0.98, 0.62, 0); handle.rotation.z = -Math.PI / 1.8; kettle.add(handle); kettle.position.set(0, -0.6, 0); // pivot = body centre; the kettle rests on the table between pours pivot.add(kettle); pivot.position.set(1.95, 0.6, 0.4); scene.add(pivot); const spoutTip = new THREE.Object3D(); spoutTip.position.set(-1.5, 1.02, 0); kettle.add(spoutTip); // water stream from spout tip to cup const streamMat = new THREE.MeshStandardMaterial({ color: 0xcfe3e8, roughness: 0.1, transparent: true, opacity: 0 }); const stream = new THREE.Mesh(new THREE.CylinderGeometry(0.035, 0.05, 1, 12), streamMat); scene.add(stream); // steam: soft sprites rising from the cup const sprite = (() => { const c = document.createElement('canvas'); c.width = c.height = 64; const g = c.getContext('2d'); const r = g.createRadialGradient(32, 32, 0, 32, 32, 32); r.addColorStop(0, 'rgba(255,255,255,0.9)'); r.addColorStop(1, 'rgba(255,255,255,0)'); g.fillStyle = r; g.fillRect(0, 0, 64, 64); return new THREE.CanvasTexture(c); })(); const N = 70, pos = new Float32Array(N * 3), life = new Float32Array(N); const resetP = i => { pos[i * 3] = -0.55 + (Math.random() - 0.5) * 0.9; pos[i * 3 + 1] = 1.0 + Math.random() * 0.2; pos[i * 3 + 2] = 0.4 + (Math.random() - 0.5) * 0.6; life[i] = Math.random(); }; for (let i = 0; i < N; i++) { resetP(i); pos[i * 3 + 1] += Math.random() * 1.6; } const steamGeo = new THREE.BufferGeometry(); steamGeo.setAttribute('position', new THREE.BufferAttribute(pos, 3)); const steamMat = new THREE.PointsMaterial({ map: sprite, size: 0.9, transparent: true, opacity: 0.14, depthWrite: false, color: 0xfff6ea }); scene.add(new THREE.Points(steamGeo, steamMat)); let progress = 0, shown = 0, raf = 0, last = performance.now(), t = 0; const v = new THREE.Vector3(); function resize() { const w = canvas.clientWidth || 320, h = canvas.clientHeight || 220; renderer.setSize(w, h, false); camera.aspect = w / h; camera.updateProjectionMatrix(); } function frame(now) { const dt = Math.min(0.05, (now - last) / 1000); last = now; t += dt; shown += (progress - shown) * Math.min(1, dt * 4); // pour cycle: tilt up, hold, tilt back (3.2 s), tea level follows progress const c = (t % 3.2) / 3.2, tilt = c < 0.25 ? c / 0.25 : c < 0.7 ? 1 : c < 0.95 ? 1 - (c - 0.7) / 0.25 : 0; const ease = tilt * tilt * (3 - 2 * tilt); pivot.rotation.z = ease * 0.78; // lift, slide over the cup and tilt so the spout is above it pivot.position.set(1.95 - ease * 0.4, 0.6 + ease * 1.35, 0.4); spoutTip.getWorldPosition(v); const pouring = ease > 0.85 && progress < 1; streamMat.opacity += ((pouring ? 0.8 : 0) - streamMat.opacity) * Math.min(1, dt * 10); const top = v.y, bottom = tea.position.y, mid = (top + bottom) / 2; stream.position.set(v.x, mid, 0.4); stream.scale.set(1, Math.max(0.01, top - bottom), 1); tea.position.y = 0.2 + 0.68 * (0.25 + 0.75 * shown); const r = 0.64 + (0.84 - 0.64) * ((tea.position.y - 0.14) / 0.81); tea.scale.set(r, r, 1); teaMat.color.copy(teaLight).lerp(teaDark, shown); leaves.forEach((l, i) => { const d = l.userData; d.a += dt * d.s; l.position.set(-0.55 + Math.cos(d.a) * d.r * r, tea.position.y + 0.005 + i * 0.001, 0.4 + Math.sin(d.a) * d.r * r); l.rotation.z = d.a * 1.7; }); for (let i = 0; i < N; i++) { life[i] += dt * 0.35; pos[i * 3 + 1] += dt * (0.35 + 0.25 * life[i]); pos[i * 3] += Math.sin(t * 1.3 + i) * dt * 0.08; if (pos[i * 3 + 1] > 3.0) resetP(i); } steamGeo.attributes.position.needsUpdate = true; renderer.render(scene, camera); raf = requestAnimationFrame(frame); } const ro = new ResizeObserver(resize); ro.observe(canvas); resize(); raf = requestAnimationFrame(frame); return { setProgress(f) { progress = Math.max(0, Math.min(1, f)); }, stop() { cancelAnimationFrame(raf); ro.disconnect(); renderer.dispose(); }, }; }