/** * Recursos compartidos (geometrías/materiales cacheados con userData.shared) y utilidades del rig. */ import * as THREE from 'three'; import type { ChibiRig } from '../../characters/types'; import { toonMaterial, type ToonOpts } from '../../render'; const mats = new Map(); const geos = new Map(); /** Material toon cacheado por clave (no se libera al morir la entidad). */ export function sharedMat(key: string, opts: ToonOpts): THREE.Material { let m = mats.get(key); if (!m) { m = toonMaterial(opts); m.userData.shared = true; mats.set(key, m); } return m; } /** Geometría cacheada por clave. */ export function sharedGeo(key: string, make: () => THREE.BufferGeometry): THREE.BufferGeometry { let g = geos.get(key); if (!g) { g = make(); g.userData.shared = true; geos.set(key, g); } return g; } export function meshOf(geo: THREE.BufferGeometry, mat: THREE.Material, shadow = true): THREE.Mesh { const m = new THREE.Mesh(geo, mat); m.castShadow = shadow; return m; } export const sphereGeo = () => sharedGeo('sphere', () => new THREE.SphereGeometry(1, 24, 16)); export const capsuleGeo = () => sharedGeo('capsule', () => new THREE.CapsuleGeometry(0.5, 1, 4, 10)); /** * Grupo del cuerpo del chibi (sigue el giro 3/4, el squash y la respiración). Los adornos que se añaden * aquí se mueven con la cara. Si la estructura interna cambiara, cae a `root`. * Las coordenadas son las del cuerpo a tamaño 1 (radio 0.5); el rig aplica `size`. */ /** * Fidelidad al emoji: deja visible solo el glifo del emoji en el cuerpo del rig y oculta las piezas 3D * añadidas a mano (alas, pinzas, faldón…). Llamar al final del constructor del enemigo. */ export function emojiOnly(rig: ChibiRig): void { for (const c of rigBody(rig).children) if (!c.name.startsWith('emoji:')) c.visible = false; } export function rigBody(rig: ChibiRig): THREE.Object3D { const inner = rig.root.children[0]; const lean = inner?.children[0]; const pivot = lean?.children[0]; const bodyG = pivot?.children[0]; return bodyG ?? rig.root; } /** Punto de la superficie del cuerpo (radio 0.5) por acimut/elevación (rad). */ export function onSurface(obj: THREE.Object3D, az: number, el: number, r = 0.5): THREE.Object3D { obj.position.set(r * Math.sin(az) * Math.cos(el), r * Math.sin(el) * 0.95, r * Math.cos(az) * Math.cos(el)); return obj; } /** Textura de halo radial (para brillos tipo sprite). */ let haloTex: THREE.Texture | null = null; export function haloTexture(): THREE.Texture { if (!haloTex) { const n = 128; const c = document.createElement('canvas'); c.width = c.height = n; const g = c.getContext('2d')!; const grd = g.createRadialGradient(n / 2, n / 2, 0, n / 2, n / 2, n / 2); grd.addColorStop(0, 'rgba(255,255,255,1)'); grd.addColorStop(0.35, 'rgba(255,255,255,0.55)'); grd.addColorStop(0.7, 'rgba(255,255,255,0.12)'); grd.addColorStop(1, 'rgba(255,255,255,0)'); g.fillStyle = grd; g.fillRect(0, 0, n, n); haloTex = new THREE.CanvasTexture(c); haloTex.colorSpace = THREE.SRGBColorSpace; haloTex.userData.shared = true; } return haloTex; }