/** * Atlas de sprites de partículas generado por canvas (sin assets externos). * Rejilla de GRID×GRID celdas de CELL px. Las primeras celdas son formas fijas en blanco/gris * (se tiñen por instancia); el resto son ranuras dinámicas para emojis (LRU). */ import * as THREE from 'three'; import { emojiTexture } from '../render/emoji'; export const CELL = 128; export const GRID = 8; /** Índice de celda de cada sprite fijo. */ export const Sp = { Glow: 0, // círculo suave Disc: 1, // bolita maciza con volumen Star: 2, // estrella de 5 puntas redondeada Sparkle: 3, // destello de 4 puntas Streak: 4, // chispa / línea (horizontal) Flake: 5, // copo de nieve Heart: 6, Drop: 7, // gota (cabeza hacia +x) Smoke: 8, // nube grumosa suave Flame: 9, // llama (punta arriba) Confetti: 10, // papelito Shard: 11, // fragmento Bubble: 12, Leaf: 13, Ring: 14, // anillo de grosor fijo Dot: 15, // punto duro } as const; const FIRST_EMOJI = 16; const EMOJI_SLOTS = GRID * GRID - FIRST_EMOJI; const FONT = '"Noto Color Emoji", "Segoe UI Emoji", "Apple Color Emoji", sans-serif'; function gray(v: number, a = 1): string { const c = Math.round(v * 255); return `rgba(${c},${c},${c},${a})`; } function starPath(g: CanvasRenderingContext2D, pts: number, ro: number, ri: number): void { g.beginPath(); for (let i = 0; i < pts * 2; i++) { const r = i % 2 === 0 ? ro : ri; const a = -Math.PI / 2 + (i * Math.PI) / pts; const x = Math.cos(a) * r; const y = Math.sin(a) * r; if (i === 0) g.moveTo(x, y); else g.lineTo(x, y); } g.closePath(); } function heartPath(g: CanvasRenderingContext2D, s: number): void { g.beginPath(); g.moveTo(0, 0.9 * s); g.bezierCurveTo(-1.25 * s, 0.05 * s, -0.95 * s, -0.95 * s, -0.42 * s, -0.78 * s); g.bezierCurveTo(-0.18 * s, -0.7 * s, -0.05 * s, -0.52 * s, 0, -0.38 * s); g.bezierCurveTo(0.05 * s, -0.52 * s, 0.18 * s, -0.7 * s, 0.42 * s, -0.78 * s); g.bezierCurveTo(0.95 * s, -0.95 * s, 1.25 * s, 0.05 * s, 0, 0.9 * s); g.closePath(); } /** Forma maciza "de juguete": borde más oscuro y redondeado + relleno claro. */ function chunky(g: CanvasRenderingContext2D, path: () => void, rim = 14): void { g.lineJoin = 'round'; g.lineCap = 'round'; path(); g.strokeStyle = gray(0.62); g.lineWidth = rim; g.stroke(); path(); g.fillStyle = gray(1); g.fill(); g.strokeStyle = gray(1); g.lineWidth = rim * 0.3; g.stroke(); } const DRAW: Array<(g: CanvasRenderingContext2D) => void> = []; DRAW[Sp.Glow] = (g) => { const gr = g.createRadialGradient(0, 0, 0, 0, 0, 54); gr.addColorStop(0, gray(1, 1)); gr.addColorStop(0.22, gray(1, 0.8)); gr.addColorStop(0.55, gray(1, 0.24)); gr.addColorStop(1, gray(1, 0)); g.fillStyle = gr; g.fillRect(-56, -56, 112, 112); }; DRAW[Sp.Disc] = (g) => { const gr = g.createRadialGradient(-14, -16, 4, 0, 0, 50); gr.addColorStop(0, gray(1)); gr.addColorStop(0.55, gray(0.96)); gr.addColorStop(1, gray(0.74)); g.fillStyle = gr; g.beginPath(); g.arc(0, 0, 50, 0, Math.PI * 2); g.fill(); }; DRAW[Sp.Star] = (g) => { chunky(g, () => starPath(g, 5, 42, 20), 18); g.fillStyle = gray(1, 0.0); }; DRAW[Sp.Sparkle] = (g) => { const gr = g.createRadialGradient(0, 0, 0, 0, 0, 40); gr.addColorStop(0, gray(1, 0.55)); gr.addColorStop(1, gray(1, 0)); g.fillStyle = gr; g.fillRect(-56, -56, 112, 112); const R = 54; const k = 7; g.beginPath(); g.moveTo(0, -R); g.quadraticCurveTo(k, -k, R, 0); g.quadraticCurveTo(k, k, 0, R); g.quadraticCurveTo(-k, k, -R, 0); g.quadraticCurveTo(-k, -k, 0, -R); g.fillStyle = gray(1); g.fill(); }; DRAW[Sp.Streak] = (g) => { // Rombo afilado y suave: 108 × 24 px. g.save(); g.scale(1, 0.24); const gr = g.createRadialGradient(0, 0, 0, 0, 0, 54); gr.addColorStop(0, gray(1, 1)); gr.addColorStop(0.5, gray(1, 0.85)); gr.addColorStop(1, gray(1, 0)); g.fillStyle = gr; g.beginPath(); g.arc(0, 0, 54, 0, Math.PI * 2); g.fill(); g.restore(); g.beginPath(); g.moveTo(-50, 0); g.quadraticCurveTo(0, -9, 50, 0); g.quadraticCurveTo(0, 9, -50, 0); g.fillStyle = gray(1); g.fill(); }; DRAW[Sp.Flake] = (g) => { g.strokeStyle = gray(1); g.lineCap = 'round'; g.lineJoin = 'round'; for (let i = 0; i < 6; i++) { g.save(); g.rotate((i * Math.PI) / 3); g.lineWidth = 9; g.beginPath(); g.moveTo(0, 0); g.lineTo(0, -48); g.stroke(); g.lineWidth = 7; g.beginPath(); g.moveTo(-13, -40); g.lineTo(0, -27); g.lineTo(13, -40); g.stroke(); g.restore(); } g.fillStyle = gray(1); g.beginPath(); g.arc(0, 0, 9, 0, Math.PI * 2); g.fill(); }; DRAW[Sp.Heart] = (g) => { g.translate(0, 2); chunky(g, () => heartPath(g, 44), 14); g.fillStyle = gray(1, 1); g.globalAlpha = 0.0; }; DRAW[Sp.Drop] = (g) => { const gr = g.createLinearGradient(-50, 0, 46, 0); gr.addColorStop(0, gray(1, 0.55)); gr.addColorStop(0.6, gray(1, 1)); g.fillStyle = gr; g.beginPath(); g.moveTo(-52, 0); g.quadraticCurveTo(-6, -8, 16, -27); g.arc(20, 0, 27, -Math.PI / 2 - 0.15, Math.PI / 2 + 0.15); g.quadraticCurveTo(-6, 8, -52, 0); g.fill(); }; DRAW[Sp.Smoke] = (g) => { const blobs = [[0, 4, 34], [-22, 10, 24], [22, 8, 26], [-8, -16, 26], [16, -14, 22]]; for (const [x, y, r] of blobs) { const gr = g.createRadialGradient(x, y, r * 0.2, x, y, r); gr.addColorStop(0, gray(1, 0.95)); gr.addColorStop(0.7, gray(1, 0.7)); gr.addColorStop(1, gray(1, 0)); g.fillStyle = gr; g.beginPath(); g.arc(x, y, r, 0, Math.PI * 2); g.fill(); } }; DRAW[Sp.Flame] = (g) => { g.shadowColor = gray(1, 0.9); g.shadowBlur = 10; g.fillStyle = gray(1); g.beginPath(); g.moveTo(2, -46); g.bezierCurveTo(6, -22, 34, -8, 30, 16); g.bezierCurveTo(27, 36, 12, 44, 0, 44); g.bezierCurveTo(-14, 44, -30, 34, -30, 14); g.bezierCurveTo(-30, -2, -16, -10, -12, -22); g.bezierCurveTo(-8, -14, -4, -12, -2, -10); g.bezierCurveTo(-6, -24, -2, -36, 2, -46); g.fill(); }; DRAW[Sp.Confetti] = (g) => { const gr = g.createLinearGradient(0, -24, 0, 24); gr.addColorStop(0, gray(1)); gr.addColorStop(1, gray(0.78)); g.fillStyle = gr; g.beginPath(); g.roundRect(-38, -23, 76, 46, 9); g.fill(); }; DRAW[Sp.Shard] = (g) => { g.lineJoin = 'round'; const path = () => { g.beginPath(); g.moveTo(-36, 30); g.lineTo(-6, -44); g.lineTo(40, -4); g.lineTo(14, 40); g.closePath(); }; path(); g.strokeStyle = gray(0.66); g.lineWidth = 10; g.stroke(); path(); g.fillStyle = gray(0.84); g.fill(); g.beginPath(); g.moveTo(-36, 30); g.lineTo(-6, -44); g.lineTo(6, 4); g.closePath(); g.fillStyle = gray(1); g.fill(); }; DRAW[Sp.Bubble] = (g) => { const gr = g.createRadialGradient(0, 0, 20, 0, 0, 48); gr.addColorStop(0, gray(1, 0.1)); gr.addColorStop(0.8, gray(1, 0.3)); gr.addColorStop(1, gray(1, 0.95)); g.fillStyle = gr; g.beginPath(); g.arc(0, 0, 48, 0, Math.PI * 2); g.fill(); g.strokeStyle = gray(1); g.lineWidth = 5; g.stroke(); g.lineCap = 'round'; g.lineWidth = 9; g.beginPath(); g.arc(0, 0, 33, Math.PI * 1.08, Math.PI * 1.42); g.stroke(); g.fillStyle = gray(1); g.beginPath(); g.arc(20, 22, 5, 0, Math.PI * 2); g.fill(); }; DRAW[Sp.Leaf] = (g) => { g.rotate(-0.5); g.fillStyle = gray(1); g.beginPath(); g.moveTo(-50, 0); g.quadraticCurveTo(0, -42, 50, 0); g.quadraticCurveTo(0, 42, -50, 0); g.fill(); g.strokeStyle = gray(0.68); g.lineWidth = 5; g.lineCap = 'round'; g.beginPath(); g.moveTo(-40, 0); g.lineTo(40, 0); g.stroke(); }; DRAW[Sp.Ring] = (g) => { g.strokeStyle = gray(1); g.lineWidth = 12; g.beginPath(); g.arc(0, 0, 44, 0, Math.PI * 2); g.stroke(); }; DRAW[Sp.Dot] = (g) => { g.fillStyle = gray(1); g.beginPath(); g.arc(0, 0, 34, 0, Math.PI * 2); g.fill(); }; export class SpriteAtlas { readonly texture: THREE.CanvasTexture; private canvas: HTMLCanvasElement; private g: CanvasRenderingContext2D; private emojiCell = new Map(); private slotEmoji: (string | null)[] = new Array(EMOJI_SLOTS).fill(null); private slotUse: number[] = new Array(EMOJI_SLOTS).fill(0); private tick = 0; constructor() { this.canvas = document.createElement('canvas'); this.canvas.width = this.canvas.height = CELL * GRID; this.g = this.canvas.getContext('2d')!; this.drawFixed(); this.texture = new THREE.CanvasTexture(this.canvas); this.texture.colorSpace = THREE.SRGBColorSpace; this.texture.generateMipmaps = true; this.texture.minFilter = THREE.LinearMipmapLinearFilter; this.texture.magFilter = THREE.LinearFilter; this.texture.userData.shared = true; } private drawFixed(): void { const g = this.g; for (let i = 0; i < DRAW.length; i++) { const fn = DRAW[i]; if (!fn) continue; const cx = (i % GRID) * CELL; const cy = Math.floor(i / GRID) * CELL; g.save(); g.beginPath(); g.rect(cx, cy, CELL, CELL); g.clip(); g.translate(cx + CELL / 2, cy + CELL / 2); fn(g); g.restore(); } } /** Celda del atlas con el emoji (la dibuja la primera vez; recicla la menos usada recientemente). */ emoji(emoji: string): number { this.tick++; const known = this.emojiCell.get(emoji); if (known !== undefined) { this.slotUse[known - FIRST_EMOJI] = this.tick; return known; } let slot = 0; for (let i = 0; i < EMOJI_SLOTS; i++) { if (this.slotEmoji[i] === null) { slot = i; break; } if (this.slotUse[i] < this.slotUse[slot]) slot = i; } const old = this.slotEmoji[slot]; if (old !== null) this.emojiCell.delete(old); this.slotEmoji[slot] = emoji; this.slotUse[slot] = this.tick; const cell = FIRST_EMOJI + slot; this.emojiCell.set(emoji, cell); const g = this.g; const cx = (cell % GRID) * CELL; const cy = Math.floor(cell / GRID) * CELL; const pad = 10; g.clearRect(cx, cy, CELL, CELL); let drawn = false; try { const img = emojiTexture(emoji).image as CanvasImageSource | undefined; if (img && (img as HTMLCanvasElement).width > 0) { g.drawImage(img, cx + pad, cy + pad, CELL - pad * 2, CELL - pad * 2); drawn = true; } } catch { drawn = false; } if (!drawn) { g.save(); g.textAlign = 'center'; g.textBaseline = 'middle'; g.font = `${Math.floor((CELL - pad * 2) * 0.78)}px ${FONT}`; g.fillStyle = '#fff'; g.fillText(emoji, cx + CELL / 2, cy + CELL / 2 + 4); g.restore(); } this.texture.needsUpdate = true; return cell; } }