// Procedural fluffy cat: shell-fur parts in a small rig with expressive procedural animation. import * as THREE from 'three'; import { furCone, furMaterial, furSphere, softMaterial } from '../render/fur'; import { clamp, damp, lerp, rand } from '../core/util'; export type Pose = 'stand' | 'sit' | 'flop' | 'wiggle' | 'dance' | 'statue' | 'sleep' | 'pancake' | 'spin' | 'eat' | 'roll'; export type Face = 'normal' | 'happy' | 'sleep' | 'surprised' | 'dizzy' | 'smug' | 'meow'; export type Accessory = 'leaf' | 'crown' | 'monocle' | 'hat' | 'nightcap' | 'bow' | 'star' | 'wings' | 'none' | 'tiny'; export interface CatLook { top: string; bottom: string; scale: number; split?: number; stripes?: number; accessory: Accessory; glow?: number; earSize?: number; furLen?: number; eye?: string; } const UP = new THREE.Vector3(0, 1, 0); export class CatRig { root = new THREE.Group(); private pivot = new THREE.Group(); private bodyMesh: THREE.Mesh; private head = new THREE.Group(); private ears: THREE.Group[] = []; private eyes: THREE.Group[] = []; private paws: THREE.Mesh[] = []; private tail: THREE.Group[] = []; private mouthW: THREE.Group; private mouthO: THREE.Mesh; private blep: THREE.Mesh; heartT = 0; private shadow: THREE.Mesh; private dizzy: THREE.Group; private zzz: THREE.Group; readonly drift = { value: new THREE.Vector3(0, -0.02, 0) }; readonly mats: THREE.ShaderMaterial[] = []; scale: number; // animation state pose: Pose = 'stand'; face: Face = 'normal'; speed = 0; // horizontal speed (world units / s) airborne = false; vy = 0; lookYaw = 0; lookPitch = 0; wag = 1; // tail wag energy private phase = rand() * 10; private t = rand() * 10; private blinkT = 2 + rand() * 3; private blink = 0; private squash = 0; private earTwitch = [0, 0]; private poseW: Record = {}; private spinA = 0; private faceTimer = 0; constructor(look: CatLook) { this.scale = look.scale; const s = look.scale; this.root.scale.setScalar(s); this.root.add(this.pivot); this.pivot.position.y = 0.42; const fl = look.furLen ?? 1; const bodyMat = furMaterial({ top: look.top, bottom: look.bottom, split: look.split ?? -0.15, stripes: look.stripes ?? 0.8, stripeAxis: new THREE.Vector3(0, 0, 1), length: 0.24 * fl, density: 24, drift: this.drift, glow: look.glow }); const headMat = furMaterial({ top: look.top, bottom: look.bottom, axis: new THREE.Vector3(0, 0.75, -0.66), split: 0.05, stripes: (look.stripes ?? 0.8) * 0.6, stripeAxis: new THREE.Vector3(1.4, 0, 0), length: 0.2 * fl, density: 26, drift: this.drift, glow: look.glow }); const pawMat = furMaterial({ top: look.bottom, bottom: look.bottom, length: 0.25 * fl, density: 30, drift: this.drift, glow: (look.glow ?? 0) * 0.5 }); const earMat = furMaterial({ top: look.top, bottom: look.top, length: 0.18, density: 34, drift: this.drift, glow: look.glow }); const tailMat = furMaterial({ top: look.top, bottom: look.top, stripes: 0.9, stripeAxis: new THREE.Vector3(0, 1.6, 0), length: 0.42 * fl, density: 22, drift: this.drift, glow: look.glow }); const tipMat = furMaterial({ top: look.bottom, bottom: look.bottom, length: 0.45 * fl, density: 22, drift: this.drift, glow: look.glow }); this.mats.push(bodyMat, headMat, pawMat, earMat, tailMat, tipMat); const sph = furSphere(), cone = furCone(); this.bodyMesh = new THREE.Mesh(sph, bodyMat); this.bodyMesh.scale.set(0.4, 0.36, 0.5); this.pivot.add(this.bodyMesh); // chest fluff ruff gives the cat a fluffy silhouette const ruff = new THREE.Mesh(sph, pawMat); ruff.scale.set(0.26, 0.24, 0.18); ruff.position.set(0, 0.06, 0.36); this.pivot.add(ruff); this.head.position.set(0, 0.3, 0.42); this.pivot.add(this.head); const headMesh = new THREE.Mesh(sph, headMat); headMesh.scale.set(0.4, 0.35, 0.36); this.head.add(headMesh); for (const sx of [-1, 1]) { const cheek = new THREE.Mesh(sph, pawMat); cheek.scale.set(0.15, 0.13, 0.14); cheek.position.set(sx * 0.27, -0.12, 0.12); this.head.add(cheek); const eg = new THREE.Group(); eg.position.set(sx * 0.27 * (look.earSize ?? 1) * 0.85, 0.27, -0.03); eg.rotation.z = -sx * 0.32; const es = look.earSize ?? 1; const ear = new THREE.Mesh(cone, earMat); ear.scale.set(0.13 * es, 0.13 * es, 0.075 * es); ear.position.y = 0.08 * es; eg.add(ear); const inner = new THREE.Mesh(new THREE.ConeGeometry(0.07 * es, 0.16 * es, 8), softMaterial('#ffb0c8')); inner.position.set(0, 0.07 * es, 0.045 * es); inner.scale.z = 0.4; eg.add(inner); this.head.add(eg); this.ears.push(eg); // big glossy eyes const eye = new THREE.Group(); eye.position.set(sx * 0.155, 0.02, 0.3); eye.scale.setScalar(1.18); const ball = new THREE.Mesh(new THREE.SphereGeometry(0.085, 16, 12), softMaterial(look.eye ?? '#2a1d3a', 1.2)); ball.scale.z = 0.55; eye.add(ball); const hl = new THREE.Mesh(new THREE.SphereGeometry(0.026, 8, 6), softMaterial('#ffffff', 0, 2)); hl.position.set(sx * -0.025 + 0.012, 0.035, 0.04); eye.add(hl); const hl2 = new THREE.Mesh(new THREE.SphereGeometry(0.012, 6, 4), softMaterial('#ffffff', 0, 2)); hl2.position.set(0.02 * sx, -0.03, 0.045); eye.add(hl2); this.head.add(eye); this.eyes.push(eye); const blush = new THREE.Mesh(new THREE.SphereGeometry(0.062, 10, 6), softMaterial('#ff8fb8', 0, 0.35)); blush.scale.set(1, 0.55, 0.3); blush.position.set(sx * 0.245, -0.085, 0.265); this.head.add(blush); } const nose = new THREE.Mesh(new THREE.SphereGeometry(0.04, 10, 6), softMaterial('#ff7fa6', 0.6)); nose.scale.set(1, 0.7, 0.6); nose.position.set(0, -0.04, 0.355); this.head.add(nose); this.mouthW = new THREE.Group(); const arc = new THREE.TorusGeometry(0.028, 0.008, 5, 10, Math.PI); for (const sx of [-1, 1]) { const a = new THREE.Mesh(arc, softMaterial('#5a3340')); a.rotation.z = Math.PI; a.position.set(sx * 0.028, -0.085, 0.345); this.mouthW.add(a); } this.head.add(this.mouthW); this.blep = new THREE.Mesh(new THREE.SphereGeometry(0.03, 10, 8), softMaterial('#ff6f9a', 0.6)); this.blep.scale.set(1, 0.7, 0.6); this.blep.position.set(0.012, -0.115, 0.345); this.blep.visible = false; this.head.add(this.blep); this.mouthO = new THREE.Mesh(new THREE.SphereGeometry(0.04, 10, 8), softMaterial('#c44d6e')); this.mouthO.scale.set(1, 0.8, 0.4); this.mouthO.position.set(0, -0.11, 0.33); this.mouthO.visible = false; this.head.add(this.mouthO); // whiskers const wv: number[] = []; for (const sx of [-1, 1]) for (const dy of [-0.02, 0.015]) wv.push(sx * 0.16, -0.06, 0.32, sx * 0.42, -0.06 + dy * 3, 0.3); const wg = new THREE.BufferGeometry(); wg.setAttribute('position', new THREE.Float32BufferAttribute(wv, 3)); this.head.add(new THREE.LineSegments(wg, new THREE.LineBasicMaterial({ color: '#fff6ea' }))); for (const [x, z] of [[-0.2, 0.28], [0.2, 0.28], [-0.2, -0.3], [0.2, -0.3]]) { const p = new THREE.Mesh(sph, pawMat); p.scale.set(0.11, 0.1, 0.13); p.position.set(x, -0.33, z); this.pivot.add(p); this.paws.push(p); if (z > 0) { const bean = new THREE.Mesh(new THREE.SphereGeometry(0.4, 8, 6), softMaterial('#ff9ec0', 0.3)); bean.scale.set(0.55, 0.35, 0.3); bean.position.set(0, -0.35, 0.85); p.add(bean); } } let parent: THREE.Object3D = this.pivot; for (let i = 0; i < 6; i++) { const g = new THREE.Group(); if (i === 0) { g.position.set(0, 0.05, -0.45); g.rotation.x = -0.9; } else g.position.y = 0.12; const r = 0.08 + i * 0.012; const m = new THREE.Mesh(sph, i === 5 ? tipMat : tailMat); m.scale.setScalar(r); m.scale.y = r * 1.25; m.position.y = 0.06; g.add(m); parent.add(g); this.tail.push(g); parent = g; } this.addAccessory(look.accessory); // soft blob shadow this.shadow = new THREE.Mesh(new THREE.CircleGeometry(0.5, 20), new THREE.MeshBasicMaterial({ color: '#7a4a6a', transparent: true, opacity: 0.22, depthWrite: false })); this.shadow.rotation.x = -Math.PI / 2; this.shadow.position.y = 0.03; this.shadow.renderOrder = -1; this.root.add(this.shadow); // dizzy stars + zzz bubbles this.dizzy = new THREE.Group(); for (let i = 0; i < 3; i++) { const st = new THREE.Mesh(starGeo(), softMaterial('#ffe066', 0, 0.8)); st.position.set(Math.cos(i * 2.1) * 0.3, 0, Math.sin(i * 2.1) * 0.3); st.scale.setScalar(0.12); this.dizzy.add(st); } this.dizzy.position.set(0, 0.42, 0); this.dizzy.visible = false; this.head.add(this.dizzy); this.zzz = new THREE.Group(); for (let i = 0; i < 3; i++) { const z = new THREE.Mesh(new THREE.SphereGeometry(0.06 + i * 0.03, 10, 8), new THREE.MeshBasicMaterial({ color: '#cfe8ff', transparent: true, opacity: 0.8 })); this.zzz.add(z); } this.zzz.visible = false; this.head.add(this.zzz); } private addAccessory(a: Accessory) { const h = this.head; if (a === 'leaf') { const stem = new THREE.Mesh(new THREE.CylinderGeometry(0.025, 0.035, 0.12, 6), softMaterial('#6bbf59')); stem.position.set(0, 0.4, 0); stem.rotation.z = 0.3; h.add(stem); const leaf = new THREE.Mesh(new THREE.SphereGeometry(0.08, 8, 6), softMaterial('#8ee06e', 0.3)); leaf.scale.set(1, 0.3, 0.6); leaf.position.set(0.07, 0.44, 0); leaf.rotation.z = -0.5; h.add(leaf); } else if (a === 'crown') { const g = new THREE.Group(); g.position.set(0, 0.38, 0); g.rotation.z = 0.18; const band = new THREE.Mesh(new THREE.CylinderGeometry(0.13, 0.14, 0.08, 14, 1, true), softMaterial('#ffd34d', 1.2, 0.2)); g.add(band); for (let i = 0; i < 5; i++) { const sp = new THREE.Mesh(new THREE.ConeGeometry(0.035, 0.1, 5), softMaterial('#ffd34d', 1.2, 0.2)); const an = i / 5 * Math.PI * 2; sp.position.set(Math.cos(an) * 0.13, 0.08, Math.sin(an) * 0.13); g.add(sp); const gem = new THREE.Mesh(new THREE.SphereGeometry(0.022, 6, 4), softMaterial(['#ff6fa8', '#6fd4ff', '#b07dff'][i % 3], 1.5, 0.3)); gem.position.set(Math.cos(an) * 0.14, 0.0, Math.sin(an) * 0.14); g.add(gem); } h.add(g); } else if (a === 'monocle') { const m = new THREE.Mesh(new THREE.TorusGeometry(0.075, 0.012, 6, 16), softMaterial('#ffd34d', 1.5)); m.position.set(0.15, 0.03, 0.34); h.add(m); const tash = new THREE.Group(); tash.position.set(0, -0.08, 0.36); for (const sx of [-1, 1]) { const p = new THREE.Mesh(new THREE.SphereGeometry(0.06, 10, 6), softMaterial('#7a4a2a')); p.scale.set(1.4, 0.45, 0.5); p.position.x = sx * 0.07; p.rotation.z = sx * -0.3; tash.add(p); const curl = new THREE.Mesh(new THREE.SphereGeometry(0.03, 8, 6), softMaterial('#7a4a2a')); curl.position.set(sx * 0.15, 0.03, 0); tash.add(curl); } h.add(tash); this.mouthW.visible = false; } else if (a === 'hat') { const hat = new THREE.Mesh(new THREE.CylinderGeometry(0.05, 0.22, 0.14, 3), softMaterial('#3c5aa6', 0.4)); hat.position.set(0, 0.37, 0); hat.rotation.y = Math.PI; h.add(hat); const feather = new THREE.Mesh(new THREE.SphereGeometry(0.06, 8, 6), softMaterial('#ff6fa8')); feather.scale.set(0.4, 1.4, 0.4); feather.position.set(0.1, 0.48, -0.05); feather.rotation.z = -0.5; h.add(feather); } else if (a === 'nightcap') { const cap = new THREE.Mesh(new THREE.ConeGeometry(0.2, 0.42, 12), softMaterial('#8fb8ff')); cap.position.set(0.05, 0.42, -0.02); cap.rotation.z = -0.6; h.add(cap); const pom = new THREE.Mesh(new THREE.SphereGeometry(0.06, 10, 8), softMaterial('#ffffff')); pom.position.set(0.24, 0.53, -0.02); h.add(pom); } else if (a === 'bow') { const g = new THREE.Group(); g.position.set(0.18, 0.3, 0.05); const cols = ['#ff5e7e', '#ffb648', '#7ee081', '#5ec8ff', '#b47dff']; for (const sx of [-1, 1]) { const l = new THREE.Mesh(new THREE.ConeGeometry(0.07, 0.14, 4), softMaterial(cols[sx > 0 ? 3 : 0], 0.6)); l.rotation.z = sx * Math.PI / 2; l.position.x = sx * 0.07; g.add(l); } g.add(new THREE.Mesh(new THREE.SphereGeometry(0.035, 8, 6), softMaterial(cols[1], 0.6))); h.add(g); } else if (a === 'star') { const st = new THREE.Mesh(starGeo(), softMaterial('#fff07a', 0.5, 0.8)); st.scale.setScalar(0.16); st.position.y = 0.2; this.tail[5].add(st); } else if (a === 'wings') { for (const sx of [-1, 1]) { const w = new THREE.Mesh(furSphere(), this.mats[2]); w.scale.set(0.05, 0.18, 0.28); w.position.set(sx * 0.38, 0.22, -0.05); w.rotation.z = sx * 0.6; this.pivot.add(w); w.name = 'wing'; } } } /** Brief expression override, e.g. 'meow' for half a second. */ flash(face: Face, seconds: number) { this.face = face; this.faceTimer = seconds; } land(strength = 1) { this.squash = Math.max(this.squash, 0.6 * strength); } update(dt: number) { this.t += dt; const sp = clamp(this.speed / 4, 0, 1.6); this.phase += dt * (4 + this.speed * 3.2); if (this.faceTimer > 0) { this.faceTimer -= dt; if (this.faceTimer <= 0) this.face = 'normal'; } // pose blending weights for (const p of ['sit', 'flop', 'wiggle', 'dance', 'statue', 'sleep', 'pancake', 'spin', 'eat', 'roll']) { const target = this.pose === p ? 1 : 0; this.poseW[p] = lerp(this.poseW[p] ?? 0, target, damp(9, dt)); } const W = this.poseW; const run = (1 - W.sit - W.flop - W.sleep - W.statue - W.pancake - W.eat) * sp; const bob = Math.abs(Math.sin(this.phase)) * 0.11 * run; this.squash = Math.max(0, this.squash - dt * 3); const sq = Math.sin(this.squash * Math.PI) * 0.35 + W.pancake * 0.6; const stretch = this.airborne ? clamp(this.vy * 0.04, -0.15, 0.2) : 0; // body pose const pv = this.pivot; pv.position.y = 0.42 + bob + (this.airborne ? 0.05 : 0) - W.sit * 0.04 - W.flop * 0.12 - W.sleep * 0.16 - W.pancake * 0.25 + W.dance * Math.abs(Math.sin(this.t * 7)) * 0.25; pv.rotation.x = -W.sit * 0.55 + Math.sin(this.phase * 2) * 0.06 * run - stretch * 0.6 + W.wiggle * 0.35 - W.eat * -0.3; pv.rotation.z = W.flop * 1.35 + W.wiggle * Math.sin(this.t * 14) * 0.18 + W.dance * Math.sin(this.t * 7) * 0.2 + W.roll * Math.sin(this.t * 9) * 1.4; this.spinA += dt * (W.spin * 14 + W.dance * 3); pv.rotation.y = this.spinA * (W.spin + W.dance) + W.wiggle * Math.PI; pv.scale.set(1 + sq * 0.6 - stretch * 0.4, 1 - sq + stretch, 1 + sq * 0.3); if (W.wiggle > 0.01) { pv.position.y += W.wiggle * 0.05; } // paws: trot cycle const pa = this.phase; const swing = [Math.sin(pa), Math.sin(pa + Math.PI), Math.sin(pa + Math.PI), Math.sin(pa)]; this.paws.forEach((p, i) => { const front = i < 2; const air = this.airborne ? (front ? 0.08 : -0.06) : 0; p.position.z = (front ? 0.28 : -0.3) + swing[i] * 0.13 * run + air + W.sit * (front ? 0.06 : -0.02); p.position.y = -0.33 + Math.max(0, -Math.cos(pa + (i === 0 || i === 3 ? 0 : Math.PI))) * 0.09 * run + W.sit * (front ? -0.02 : 0.05) + W.sleep * 0.1 + W.dance * (front ? 0.2 + Math.sin(this.t * 14 + i) * 0.08 : 0); if (W.flop > 0.5 || W.roll > 0.5) p.position.y += Math.sin(this.t * 10 + i) * 0.03; }); // tail: travelling wave; tease = straight up and wiggly const wagSpeed = 3 + this.wag * 5 + W.wiggle * 8; this.tail.forEach((g, i) => { if (i === 0) { g.rotation.x = lerp(-0.9 - run * 0.5, -0.15, W.wiggle + W.statue) + W.sleep * -0.6 + W.sit * 0.5; g.rotation.z = Math.sin(this.t * wagSpeed) * (0.25 + this.wag * 0.2); } else { g.rotation.x = 0.28 - run * 0.12 - W.sleep * 0.1 + Math.sin(this.t * 2 + i) * 0.05 - W.statue * 0.25; g.rotation.z = Math.sin(this.t * wagSpeed - i * 0.7) * (0.12 + this.wag * 0.12) * (1 - W.statue); } }); // head look + tilt const h = this.head; h.rotation.y = lerp(h.rotation.y, clamp(this.lookYaw, -0.9, 0.9) * (1 - W.sleep), damp(6, dt)); h.rotation.x = lerp(h.rotation.x, clamp(this.lookPitch, -0.4, 0.4) + W.sit * 0.45 + W.sleep * 0.3 + W.eat * 0.5 + W.wiggle * -0.4, damp(6, dt)); h.rotation.z = Math.sin(this.t * 1.3) * 0.08 + W.dance * Math.sin(this.t * 7 + 1) * 0.25 + (this.face === 'happy' ? 0.15 : 0); // ears this.earTwitch = this.earTwitch.map(v => Math.max(0, v - dt * 5)); if (rand() < dt * 0.4) this.earTwitch[rand() < 0.5 ? 0 : 1] = 1; this.ears.forEach((e, i) => { const sx = i === 0 ? -1 : 1; const tw = Math.sin(this.earTwitch[i] * Math.PI) * 0.4; e.rotation.z = -sx * (0.32 + (this.face === 'surprised' ? -0.15 : 0) + W.sleep * 0.4) - sx * tw; e.rotation.x = -tw * 0.5 + (this.airborne ? -0.3 : 0) - run * 0.25; }); // eyes: blink + expressions this.blinkT -= dt; if (this.blinkT < 0) { this.blink = 1; this.blinkT = 2 + rand() * 4; } this.blink = Math.max(0, this.blink - dt * 8); const f = this.face; let ey = 1 - Math.sin(this.blink * Math.PI) * 0.9; let es = 1; if (f === 'happy' || f === 'smug') ey = 0.22; if (f === 'sleep' || W.sleep > 0.5) ey = 0.08; if (f === 'surprised') { es = 1.3; ey = 1.25; } if (f === 'dizzy') ey = 0.5 + Math.sin(this.t * 12) * 0.4; this.eyes.forEach((e, i) => { e.scale.set(es, lerp(e.scale.y, ey * es, damp(20, dt)), es); e.rotation.z = f === 'happy' ? (i ? -0.2 : 0.2) : 0; }); const open = f === 'meow' || f === 'surprised'; this.mouthO.visible = open; if (this.mouthW.children.length && !this.mouthW.userData.hidden) this.mouthW.visible = !open; this.mouthO.scale.y = 0.6 + Math.abs(Math.sin(this.t * 18)) * 0.5; this.blep.visible = (f === 'happy' || f === 'smug') && !open && Math.sin(this.t * 0.7) > 0.2; this.dizzy.visible = f === 'dizzy'; if (this.dizzy.visible) this.dizzy.rotation.y += dt * 5; this.zzz.visible = W.sleep > 0.5; if (this.zzz.visible) this.zzz.children.forEach((z, i) => { const k = (this.t * 0.6 + i / 3) % 1; z.position.set(0.25 + k * 0.3, 0.3 + k * 0.6, 0); z.scale.setScalar(0.4 + k); ((z as THREE.Mesh).material as THREE.MeshBasicMaterial).opacity = 0.9 * (1 - k); }); this.pivot.children.forEach(c => { if (c.name === 'wing') c.rotation.y = Math.sin(this.t * 16) * 0.5; }); // fur drift: gravity + lag behind motion this.drift.value.set(0, -0.025 - (this.airborne ? this.vy * 0.004 : 0), 0); this.shadow.scale.setScalar(1 - clamp((this.root.position.y - (this.root.userData.groundY ?? this.root.position.y)) * 0.2, 0, 0.6)); this.shadow.position.y = ((this.root.userData.groundY ?? this.root.position.y) - this.root.position.y) / this.scale + 0.03; } setMotionDrift(vx: number, vz: number) { this.drift.value.x = -vx * 0.012; this.drift.value.z = -vz * 0.012; } } let _star: THREE.BufferGeometry | null = null; export function starGeo(): THREE.BufferGeometry { if (_star) return _star; const s = new THREE.Shape(); for (let i = 0; i < 10; i++) { const r = i % 2 ? 0.45 : 1; const a = i / 10 * Math.PI * 2 + Math.PI / 2; const x = Math.cos(a) * r, y = Math.sin(a) * r; i ? s.lineTo(x, y) : s.moveTo(x, y); } _star = new THREE.ExtrudeGeometry(s, { depth: 0.35, bevelEnabled: true, bevelSize: 0.12, bevelThickness: 0.12, bevelSegments: 1 }); _star.center(); return _star; } export { UP };