/** * Caras fieles al emoji. * El glifo real del emoji se proyecta sobre la esfera del cuerpo (emojis de cara redonda: 😊 😡 🥶 😎 🌝…) * o se usa como cabeza con relieve (emojis con silueta propia: 👵 👷 👻…). Así cada personaje conserva * la expresión, el color y los rasgos de su emoji, con volumen 3D. */ import * as THREE from 'three'; import { emojiReady, emojiTexture } from '../render/emoji'; const PX = 256; const R = 0.5; export interface GlyphInfo { /** true: el glifo es un disco (cara redonda) y puede envolver la esfera. */ round: boolean; /** Centro y radio del disco principal en uv (v hacia arriba). */ cx: number; cy: number; r: number; /** Color del borde del glifo: sirve para manos, pies y la parte trasera. */ rim: THREE.Color; /** Color medio del centro del glifo (piel/cara): manos y pies cuando el glifo hace de cabeza. */ skin: THREE.Color; } const infos = new Map(); const geos = new Map(); const listeners = new Set<() => void>(); const watching = new Set(); function analyze(emoji: string): GlyphInfo { const info: GlyphInfo = { round: false, cx: 0.5, cy: 0.5, r: 0.4, rim: new THREE.Color(0xffc928), skin: new THREE.Color(0xffc928) }; const canvas = emojiTexture(emoji, PX).image as HTMLCanvasElement; const ctx = canvas.getContext('2d', { willReadFrequently: true }); if (!ctx) return info; const d = ctx.getImageData(0, 0, PX, PX).data; const solid = (x: number, y: number) => d[(y * PX + x) * 4 + 3] > 128; // Fila más ancha = ecuador del disco. const widths = new Int16Array(PX); const lefts = new Int16Array(PX); let best = 0; for (let y = 0; y < PX; y++) { let x0 = -1; let x1 = -1; for (let x = 0; x < PX; x++) { if (!solid(x, y)) continue; if (x0 < 0) x0 = x; x1 = x; } widths[y] = x0 < 0 ? 0 : x1 - x0 + 1; lefts[y] = x0; if (widths[y] > best) best = widths[y]; } if (best < 16) return info; let ya = -1; let yb = -1; for (let y = 0; y < PX; y++) { if (widths[y] < best - 1) continue; if (ya < 0) ya = y; yb = y; } let cyp = (ya + yb) / 2; const row = Math.round(cyp); let cxp = lefts[row] + widths[row] / 2; let rp = best / 2; // Si algo sobresale por los lados (gafas de 😎, manos), el alto del glifo da el disco real. let top = 0; while (top < PX && !widths[top]) top++; let bottom = PX - 1; while (bottom > top && !widths[bottom]) bottom--; const rH = (bottom - top + 1) / 2; if (rH < rp - 1.5) { rp = rH; cyp = (top + bottom + 1) / 2; let sx = 0, sn2 = 0; for (let y = top; y < top + rH * 0.35; y++) { sx += lefts[y] + widths[y] / 2; sn2++; } if (sn2) cxp = sx / sn2; } let inside = 0; let insideSolid = 0; let outsideSolid = 0; let rr = 0, rg = 0, rb = 0, rn = 0; let sr = 0, sg = 0, sb = 0, sn = 0; for (let y = 0; y < PX; y++) { for (let x = 0; x < PX; x++) { const dist = Math.hypot(x + 0.5 - cxp, y + 0.5 - cyp); const s = solid(x, y); if (dist <= rp - 1.5) { inside++; if (s) insideSolid++; if (s && dist < rp * 0.6) { const i = (y * PX + x) * 4; // se ignoran los rasgos oscuros (ojos, boca) para quedarse con el tono de la cara if (d[i] + d[i + 1] + d[i + 2] > 330) { sr += d[i]; sg += d[i + 1]; sb += d[i + 2]; sn++; } } if (s && dist > rp * 0.86 && dist < rp * 0.93) { const i = (y * PX + x) * 4; rr += d[i]; rg += d[i + 1]; rb += d[i + 2]; rn++; } } else if (dist > rp + 2 && s) outsideSolid++; } } info.cx = cxp / PX; info.cy = 1 - cyp / PX; info.r = rp / PX; info.round = insideSolid / inside > 0.965 && outsideSolid / inside < 0.1; if (sn > 0) info.skin.setRGB(sr / sn / 255, sg / sn / 255, sb / sn / 255, THREE.SRGBColorSpace); if (rn > 0) info.rim.setRGB(rr / rn / 255, rg / rn / 255, rb / rn / 255, THREE.SRGBColorSpace); return info; } /** Datos del glifo (cacheados; se recalculan si la fuente de emojis llega más tarde). */ export function glyphInfo(emoji: string): GlyphInfo { let info = infos.get(emoji); if (!info) { info = analyze(emoji); infos.set(emoji, info); watch(emoji); } return info; } function writeUv(geo: THREE.SphereGeometry, info: GlyphInfo): void { const pos = geo.attributes.position; const uv = geo.attributes.uv; for (let i = 0; i < pos.count; i++) { let x = pos.getX(i) / R; let y = pos.getY(i) / R; const z = pos.getZ(i); let k = 0.955; if (z < 0) { // Parte trasera: toma el color del borde del disco. const l = Math.hypot(x, y); if (l < 1e-4) { x = 0; y = 1; } else { x /= l; y /= l; } k = 0.9; } uv.setXY(i, info.cx + x * info.r * k, info.cy + y * info.r * k); } uv.needsUpdate = true; } /** Esfera del cuerpo con el glifo proyectado en la cara frontal. Compartida: no la liberes. */ export function glyphSphere(emoji: string): THREE.SphereGeometry { let g = geos.get(emoji); if (!g) { g = new THREE.SphereGeometry(R, 44, 30); g.userData.shared = true; writeUv(g, glyphInfo(emoji)); geos.set(emoji, g); } return g; } /** Avisa cuando algún glifo se ha redibujado (llegó la fuente) y sus datos cambiaron. */ export function onGlyphRefresh(fn: () => void): () => void { listeners.add(fn); return () => listeners.delete(fn); } function watch(emoji: string): void { if (watching.has(emoji)) return; watching.add(emoji); const redo = () => { const next = analyze(emoji); const prev = infos.get(emoji); if (prev && Math.abs(prev.r - next.r) < 0.004 && Math.abs(prev.cx - next.cx) < 0.004 && Math.abs(prev.cy - next.cy) < 0.004 && prev.round === next.round) return; if (prev) Object.assign(prev, next); const g = geos.get(emoji); if (g && prev) writeUv(g, prev); for (const fn of [...listeners]) fn(); }; void emojiReady().then(() => { redo(); // La textura puede redibujarse un instante después de resolver la fuente. setTimeout(redo, 400); }); }