/** * Fondos en capas con parallax real. Cada capa es una malla 2D (colores por vértice, sin luz ni * niebla) a profundidad z = -zl, escalada para que sus unidades equivalgan a unidades "aparentes" * del plano de juego. El sistema de animación la desplaza con la cámara con factor `s`. * No pintan el cielo (lo pone el render): solo siluetas delante. */ import * as THREE from 'three'; import type { Game } from '../../core/game'; import type { Props, ZoneDef } from '../../core/types'; import { anim } from './anim'; import { noise1, rng } from './util'; const CAM_DIST = 26; /** Semiancho visible en u a aspecto 3.2 con margen (cubre pantallas ultra-panorámicas y zoom out). */ const HALF_W = 7.46 * 3.2 + 8; const NIGHT_TINT = new THREE.Color(0.24, 0.27, 0.48); const DUSK_TINT = new THREE.Color(0.95, 0.68, 0.72); class LB { pos: number[] = []; day: number[] = []; nightC: number[] = []; haze: number[] = []; glow: number[] = []; vOff = 0; // z: desplazamiento hacia la cámara DENTRO de la capa. Todas las formas de // una capa nacían en z=0 (coplanares): fachada y ventanas, tronco y copa, // nube de sombra y nube blanca compartían el mismo plano y el test de // profundidad las resolvía por redondeo — al moverse la cámara en Y (lo // aéreo) el redondeo cambiaba y las ventanas aparecían/desaparecían por // fragmentos triangulares: el parpadeo. Cada forma que se dibuja ENCIMA de // otra lleva una z mayor (paso 0,1 local ≈ 0,25–0,64 u reales tras la escala // de la capa, 75–1000× la precisión del buffer de profundidad y sub-píxel // en pantalla). El orden z sigue el orden de dibujo para no cambiar el look. private v(x: number, y: number, c: THREE.Color, n: THREE.Color | null, h: number, gl: number, z = 0): void { this.pos.push(x, y + this.vOff, z); this.day.push(c.r, c.g, c.b); if (n) this.nightC.push(n.r, n.g, n.b); else this.nightC.push(c.r * NIGHT_TINT.r, c.g * NIGHT_TINT.g, c.b * NIGHT_TINT.b); this.haze.push(h); this.glow.push(gl); } tri(ax: number, ay: number, bx: number, by: number, cx: number, cy: number, ca: THREE.Color, cb = ca, cc = ca, h = 0, night: THREE.Color | null = null, gl = 0, z = 0): void { // orden antihorario if ((bx - ax) * (cy - ay) - (by - ay) * (cx - ax) < 0) { this.v(ax, ay, ca, night, h, gl, z); this.v(cx, cy, cc, night, h, gl, z); this.v(bx, by, cb, night, h, gl, z); } else { this.v(ax, ay, ca, night, h, gl, z); this.v(bx, by, cb, night, h, gl, z); this.v(cx, cy, cc, night, h, gl, z); } } rect(x0: number, y0: number, x1: number, y1: number, top: THREE.Color, bot = top, h = 0, night: THREE.Color | null = null, gl = 0, z = 0): void { this.tri(x0, y0, x1, y0, x1, y1, bot, bot, top, h, night, gl, z); this.tri(x0, y0, x1, y1, x0, y1, bot, top, top, h, night, gl, z); } /** Banda bajo un perfil: tops[i] en xs[i], hasta `bottom`, con degradado y bruma creciente hacia abajo. */ strip(xs: number[], tops: number[], bottom: number, cTop: THREE.Color, cBot: THREE.Color, hTop: number, hBot: number, gradDepth = 6, z = 0): void { const mid = new THREE.Color(); for (let i = 0; i < xs.length - 1; i++) { const x0 = xs[i], x1 = xs[i + 1], t0 = tops[i], t1 = tops[i + 1]; const m0 = Math.max(bottom, Math.min(t0, t1) - gradDepth); mid.copy(cBot); // tramo superior con degradado this.tri(x0, t0, x1, t1, x1, m0, cTop, cTop, mid, hTop, null, 0, z); this.tri(x0, t0, x1, m0, x0, m0, cTop, mid, mid, hTop, null, 0, z); // relleno hasta abajo if (m0 > bottom) this.rect(x0, bottom, x1, m0, cBot, cBot, hBot, null, 0, z); } } circle(cx: number, cy: number, r: number, c: THREE.Color, edge = c, h = 0, seg = 18, sx = 1, night: THREE.Color | null = null, gl = 0, offX = 0, offY = 0, z = 0): void { for (let i = 0; i < seg; i++) { const a0 = (i / seg) * Math.PI * 2; const a1 = ((i + 1) / seg) * Math.PI * 2; this.tri(cx + offX * r, cy + offY * r, cx + Math.cos(a0) * r * sx, cy + Math.sin(a0) * r, cx + Math.cos(a1) * r * sx, cy + Math.sin(a1) * r, c, edge, edge, h, night, gl, z); } } build(): { mesh: THREE.Mesh; retint: (night: number, fog: THREE.Color) => void } { const g = new THREE.BufferGeometry(); g.setAttribute('position', new THREE.Float32BufferAttribute(this.pos, 3)); const col = new THREE.Float32BufferAttribute(new Float32Array(this.day.length), 3); g.setAttribute('color', col); g.computeBoundingSphere(); const mat = new THREE.MeshBasicMaterial({ vertexColors: true, fog: false }); const mesh = new THREE.Mesh(g, mat); mesh.frustumCulled = false; const day = this.day, nc = this.nightC, hz = this.haze, gl = this.glow; const arr = col.array as Float32Array; const retint = (night: number, fog: THREE.Color) => { const duskW = Math.max(0, 1 - Math.abs(night - 0.5) * 2) * 0.8; for (let i = 0, v = 0; i < arr.length; i += 3, v++) { for (let k = 0; k < 3; k++) { const d = day[i + k]; const tint = k === 0 ? DUSK_TINT.r : k === 1 ? DUSK_TINT.g : DUSK_TINT.b; let c = d + (nc[i + k] - d) * night; c = c * (1 - duskW * (1 - tint)); const fk = k === 0 ? fog.r : k === 1 ? fog.g : fog.b; const h = hz[v] * (gl[v] ? 0.25 * (1 - night) : 1); arr[i + k] = c + (fk - c) * h; } } col.needsUpdate = true; }; return { mesh, retint }; } } const C = (hex: number) => new THREE.Color(hex); const lerpC = (a: number, b: number, t: number) => C(a).lerp(C(b), t); interface LayerSpec { zl: number; s: number; sy: number; draw: (lb: LB, u0: number, u1: number, r: () => number) => void; } function hillProfile(u0: number, u1: number, r: () => number, minW: number, maxW: number, minH: number, maxH: number, base: number): (u: number) => number { const domes: Array<[number, number, number]> = []; let u = u0 - maxW; while (u < u1 + maxW) { const w = minW + r() * (maxW - minW); domes.push([u + w / 2, w / 2, minH + r() * (maxH - minH)]); u += w * (0.45 + r() * 0.35); } return (x: number) => { let h = base; for (const [c, hw, H] of domes) { const d = (x - c) / hw; if (d > -1 && d < 1) h = Math.max(h, base + H * Math.sqrt(1 - d * d)); } return h; }; } function ridge(lb: LB, u0: number, u1: number, prof: (u: number) => number, cTop: THREE.Color, cBot: THREE.Color, hTop: number, hBot: number, step = 0.3, grad = 6, z = 0): (u: number) => number { const xs: number[] = []; const ts: number[] = []; for (let u = u0; u <= u1 + step; u += step) { xs.push(u); ts.push(prof(u)); } lb.strip(xs, ts, -90, cTop, cBot, hTop, hBot, grad, z); return prof; } function cloudPuff(lb: LB, x: number, y: number, s: number, r: () => number, h: number): void { const shadeC = C(0xdfe8fb); const white = C(0xffffff); const blobs: Array<[number, number, number]> = [[0, 0, 1], [-1.1, -0.25, 0.75], [1.1, -0.2, 0.8], [0.45, 0.55, 0.7], [-0.5, 0.45, 0.62]]; for (const [bx, by, bs] of blobs) lb.circle(x + bx * s, y + by * s - 0.12 * s, bs * s, shadeC, shadeC, h, 16, 1.05); for (const [bx, by, bs] of blobs) lb.circle(x + bx * s, y + by * s, bs * s * 0.92, white, white, h, 16, 1.05, null, 0, -0.2, 0.25, 0.1); lb.rect(x - 1.2 * s, y - 0.55 * s, x + 1.2 * s, y - 0.1 * s, shadeC, shadeC, h, null, 0, 0.2); void r; } function lollipopTree(lb: LB, x: number, y: number, s: number, crown: THREE.Color, h: number, z0 = 0): void { lb.rect(x - 0.08 * s, y - 0.3, x + 0.08 * s, y + 0.6 * s, C(0x7a5a44), C(0x7a5a44), h, null, 0, z0 + 0.1); lb.circle(x, y + 0.85 * s, 0.5 * s, crown.clone().multiplyScalar(1.15), crown, h, 14, 1, null, 0, -0.25, 0.3, z0 + 0.2); } function pineSil(lb: LB, x: number, y: number, s: number, c: THREE.Color, h: number): void { for (let i = 0; i < 3; i++) { const w = (0.8 - i * 0.2) * s; const yb = y + i * 0.55 * s; lb.tri(x - w, yb, x + w, yb, x, yb + 0.95 * s, c, c, c.clone().multiplyScalar(1.15), h, null, 0, 0.1); } lb.rect(x - 0.07 * s, y - 0.4, x + 0.07 * s, y + 0.1, c.clone().multiplyScalar(0.6), undefined, h, null, 0, 0.2); } function building(lb: LB, x: number, w: number, top: number, c: THREE.Color, h: number, r: () => number, winSize = 0.22): void { lb.rect(x, -90, x + w, top, c, c.clone().multiplyScalar(0.92), h); lb.rect(x - 0.05, top - 0.08, x + w + 0.05, top + 0.06, c.clone().multiplyScalar(1.1), undefined, h, null, 0, 0.1); const day = c.clone().lerp(C(0xcfe6ff), 0.35); const lit = C(0xffd36a); // bajo el umbral del bloom: ventanas fijas, sin destellos const off = C(0x1c1838); const nx = Math.max(1, Math.floor((w - 0.2) / (winSize * 2.2))); const gap = (w - nx * winSize) / (nx + 1); for (let y = top - 0.45; y > -3; y -= winSize * 2.4) { for (let i = 0; i < nx; i++) { const wx = x + gap + i * (winSize + gap); const on = r() < 0.55; lb.rect(wx, y - winSize, wx + winSize, y, day, day, h, on ? lit : off, on ? 1 : 0, 0.1); } } if (r() < 0.3) lb.rect(x + w * 0.5 - 0.03, top, x + w * 0.5 + 0.03, top + 0.8, c, c, h, null, 0, 0.1); } function floatingIsland(lb: LB, x: number, y: number, s: number, h: number): void { lb.tri(x - 1.2 * s, y, x + 1.2 * s, y, x + 0.1 * s, y - 1.4 * s, C(0xb0784a), C(0xb0784a), C(0x7a4c32), h); lb.circle(x, y, 1.25 * s, C(0x8fe060), C(0x6fc84c), h, 16, 1, null, 0, 0, 0.3, 0.1); lb.rect(x - 1.25 * s, y - 0.25 * s, x + 1.25 * s, y, C(0x6fc84c), C(0x5ab83e), h, null, 0, 0.2); lollipopTree(lb, x + 0.3 * s, y + 0.2 * s, 0.9 * s, C(0x4fb848), h, 0.2); } const PRESETS: Record = { hills: [ { zl: 120, s: 0.08, sy: 0.06, draw: (lb, u0, u1, r) => ridge(lb, u0, u1, hillProfile(u0, u1, r, 8, 18, 3, 7, 1), C(0x9cc0f0), C(0xbcd6f6), 0.45, 0.6) }, { zl: 100, s: 0.1, sy: 0.08, draw: (lb, u0, u1, r) => { for (let u = u0; u < u1; u += 9 + r() * 10) cloudPuff(lb, u, 6 + r() * 3, 0.8 + r() * 0.6, r, 0.08); } }, { zl: 75, s: 0.2, sy: 0.15, draw: (lb, u0, u1, r) => { const p = ridge(lb, u0, u1, hillProfile(u0, u1, r, 6, 13, 1.5, 4, 0.5), C(0x8fd86a), C(0x9fd2a0), 0.25, 0.45); for (let u = u0; u < u1; u += 1.5 + r() * 4) lollipopTree(lb, u, p(u) - 0.1, 0.6 + r() * 0.3, C(0x5fbf58), 0.25); for (let u = u0 + 5; u < u1; u += 18 + r() * 16) floatingIsland(lb, u, 6.5 + r() * 2, 0.8 + r() * 0.4, 0.2); } }, { zl: 40, s: 0.4, sy: 0.32, draw: (lb, u0, u1, r) => { const p = ridge(lb, u0, u1, hillProfile(u0, u1, r, 5, 11, 0.6, 2.2, -0.2), C(0x6ccc4e), C(0x5aa84a), 0.1, 0.25, 0.25, 4); for (let u = u0; u < u1; u += 2 + r() * 5) lollipopTree(lb, u, p(u) - 0.1, 0.8 + r() * 0.5, C(0x46b03e), 0.1); } }, ], city: [ { zl: 120, s: 0.08, sy: 0.06, draw: (lb, u0, u1, r) => { for (let u = u0; u < u1;) { const w = 1.2 + r() * 2.5; building(lb, u, w, 2 + r() * 5.5, C(0xa3b2de), 0.45, r, 0.16); u += w + r() * 0.3; } } }, { zl: 100, s: 0.1, sy: 0.08, draw: (lb, u0, u1, r) => { for (let u = u0; u < u1; u += 10 + r() * 12) cloudPuff(lb, u, 6.5 + r() * 2.5, 0.8 + r() * 0.5, r, 0.06); } }, { zl: 70, s: 0.22, sy: 0.16, draw: (lb, u0, u1, r) => { const cols = [0x7f8fd0, 0x8f80c8, 0x7aa2cf, 0x9a8fc4]; for (let u = u0; u < u1;) { const w = 1.5 + r() * 2.2; building(lb, u, w, 1 + r() * 4, C(cols[Math.floor(r() * 4)]), 0.22, r); u += w + 0.2 + r() * 0.8; } } }, { zl: 38, s: 0.42, sy: 0.32, draw: (lb, u0, u1, r) => { const cols = [0x6a78b8, 0x7d6aae, 0x5f8ab8]; for (let u = u0; u < u1;) { const w = 1.8 + r() * 2; if (r() < 0.35) { lollipopTree(lb, u + 0.8, -0.3, 1.1, C(0x3f9a4a), 0.08); u += 1.6; continue; } building(lb, u, w, 0.2 + r() * 2, C(cols[Math.floor(r() * 3)]), 0.08, r, 0.26); u += w + 0.6 + r() * 1.5; } } }, ], lagoon: [ { zl: 120, s: 0.08, sy: 0.06, draw: (lb, u0, u1, r) => ridge(lb, u0, u1, hillProfile(u0, u1, r, 10, 20, 2, 6, 0.6), C(0x8fc4e8), C(0xb6dcf2), 0.45, 0.6) }, { zl: 100, s: 0.1, sy: 0.08, draw: (lb, u0, u1, r) => { for (let u = u0; u < u1; u += 9 + r() * 10) cloudPuff(lb, u, 6.5 + r() * 2.5, 0.8 + r() * 0.5, r, 0.06); } }, { zl: 80, s: 0.16, sy: 0.12, draw: (lb, u0, u1, r) => { const p = ridge(lb, u0, u1, hillProfile(u0, u1, r, 6, 14, 0.8, 2.5, 0.8), C(0x7fd28a), C(0x86c6a8), 0.25, 0.35, 0.3, 2); for (let u = u0; u < u1; u += 3 + r() * 6) { const x = u, y = p(u) - 0.1, s = 0.9 + r() * 0.4; lb.rect(x - 0.05, y - 0.2, x + 0.08, y + 1.4 * s, C(0x9a7a5a), undefined, 0.25, null, 0, 0.1); for (let i = 0; i < 5; i++) { const a = (i / 5) * Math.PI + 0.2; lb.tri(x, y + 1.4 * s, x + Math.cos(a) * 0.9 * s, y + 1.4 * s + Math.sin(a) * 0.35 * s - 0.25 * s, x + Math.cos(a) * 0.5 * s, y + 1.4 * s + 0.2 * s, C(0x4fae5a), C(0x3f9a4a), C(0x5fbf68), 0.25, null, 0, 0.2 + i * 0.02); } } // agua de la laguna en el horizonte lb.rect(u0, -90, u1, 0.6, C(0x6fd4f4), C(0x4aa8e8), 0.3, null, 0, 0.3); for (let u = u0; u < u1; u += 1 + r() * 2) lb.rect(u, 0.1 + r() * 0.4, u + 0.6 + r(), 0.16 + r() * 0.4, C(0xe6fbff), undefined, 0.2, null, 0, 0.4); } }, { zl: 36, s: 0.44, sy: 0.34, draw: (lb, u0, u1, r) => { for (let u = u0; u < u1; u += 2 + r() * 5) { for (let i = 0; i < 4; i++) { const x = u + i * 0.18; const hh = 0.8 + r() * 0.8; lb.rect(x, -1, x + 0.06, hh, C(0x3f8f3a), undefined, 0.1); if (i % 2 === 0) lb.rect(x - 0.05, hh - 0.35, x + 0.11, hh, C(0x7a4a26), undefined, 0.1, null, 0, 0.1); } } } }, ], forest: [ { zl: 120, s: 0.08, sy: 0.06, draw: (lb, u0, u1, r) => ridge(lb, u0, u1, hillProfile(u0, u1, r, 8, 16, 2.5, 6, 1), C(0x7fb8a8), C(0xa6cfc4), 0.45, 0.6) }, { zl: 85, s: 0.15, sy: 0.12, draw: (lb, u0, u1, r) => { const p = ridge(lb, u0, u1, hillProfile(u0, u1, r, 6, 12, 1, 3, 0.8), C(0x5fa878), C(0x6f9e8a), 0.3, 0.4, 0.3, 3); for (let u = u0; u < u1; u += 0.6 + r() * 0.9) pineSil(lb, u, p(u) - 0.3, 1 + r() * 0.8, C(0x4a9a70), 0.32); } }, { zl: 55, s: 0.26, sy: 0.2, draw: (lb, u0, u1, r) => { for (let u = u0; u < u1; u += 0.9 + r() * 1.4) pineSil(lb, u, 0.2 + r() * 0.6, 1.4 + r() * 1.2, C(0x3a8a5c), 0.18); lb.rect(u0, -90, u1, 0.4, C(0x3a7a52), C(0x2f6a46), 0.18, null, 0, 0.3); } }, { zl: 32, s: 0.46, sy: 0.36, draw: (lb, u0, u1, r) => { for (let u = u0; u < u1; u += 2.5 + r() * 4) lollipopTree(lb, u, -0.2, 1.6 + r() * 0.8, C(0x2f8a48), 0.06); ridge(lb, u0, u1, hillProfile(u0, u1, r, 3, 7, 0.3, 1, -0.5), C(0x3f9a4a), C(0x2f7a3e), 0.06, 0.15, 0.3, 2, 0.3); } }, ], castle: [ { zl: 120, s: 0.08, sy: 0.06, draw: (lb, u0, u1, r) => ridge(lb, u0, u1, hillProfile(u0, u1, r, 6, 12, 3, 7.5, 1), C(0x9c9ad8), C(0xbdb8e6), 0.45, 0.6) }, { zl: 80, s: 0.16, sy: 0.12, draw: (lb, u0, u1, r) => { ridge(lb, u0, u1, hillProfile(u0, u1, r, 8, 16, 0.5, 2, 0.4), C(0x7aa98a), C(0x8aa8a8), 0.3, 0.4, 0.3, 2); const wall = C(0x8a84b8), roof = C(0xd0505a), lit = C(0xffd36a); for (let u = u0 + 6; u < u1; u += 28 + r() * 20) { const w = 8 + r() * 4; lb.rect(u, 0, u + w, 2.6, wall, wall.clone().multiplyScalar(0.85), 0.28); for (let x = u; x < u + w - 0.3; x += 0.6) lb.rect(x, 2.6, x + 0.35, 2.95, wall, wall, 0.28, null, 0, 0.1); const towers = [u - 0.4, u + w * 0.35, u + w - 0.6]; towers.forEach((tx, i) => { const th = i === 1 ? 5.2 : 3.8; lb.rect(tx, 0, tx + 1.2, th, wall.clone().multiplyScalar(1.05), wall, 0.28, null, 0, 0.1); lb.tri(tx - 0.2, th, tx + 1.4, th, tx + 0.6, th + 1.6, roof, roof, roof.clone().multiplyScalar(1.2), 0.28, null, 0, 0.2); lb.rect(tx + 0.58, th + 1.5, tx + 0.64, th + 2.2, wall, wall, 0.28, null, 0, 0.2); lb.tri(tx + 0.64, th + 2.2, tx + 0.64, th + 1.9, tx + 1.2, th + 2.05, C(0xffc928), C(0xffc928), C(0xffc928), 0.2, null, 0, 0.3); lb.rect(tx + 0.45, th - 1.4, tx + 0.75, th - 0.9, C(0x5a548a), undefined, 0.28, lit, 1, 0.2); }); } } }, { zl: 40, s: 0.4, sy: 0.32, draw: (lb, u0, u1, r) => { const p = ridge(lb, u0, u1, hillProfile(u0, u1, r, 5, 10, 0.5, 2, -0.3), C(0x5fae5a), C(0x4a8a52), 0.1, 0.25, 0.25, 3); for (let u = u0; u < u1; u += 1.5 + r() * 3) pineSil(lb, u, p(u) - 0.2, 0.8 + r() * 0.5, C(0x2f7a4e), 0.1); } }, ], volcano: [ { zl: 120, s: 0.08, sy: 0.06, draw: (lb, u0, u1, r) => ridge(lb, u0, u1, hillProfile(u0, u1, r, 6, 12, 2, 5, 0.5), C(0x6a3a4a), C(0x8a4a50), 0.4, 0.55) }, { zl: 95, s: 0.12, sy: 0.1, draw: (lb, u0, u1, r) => { for (let u = u0 + 8; u < u1; u += 30 + r() * 25) { const H = 7 + r() * 2, W = 7 + r() * 3, top = 1.2; const dark = C(0x4a2a34), side = C(0x5e3440); lb.tri(u - W, -2, u - top, H, u + top, H, dark, side, side, 0.2); lb.tri(u - W, -2, u + top, H, u + W, -2, dark, side, dark, 0.2); lb.rect(u - W, -90, u + W, -2, dark, dark, 0.3); const lava = C(0xff7a1a).multiplyScalar(1.6); lb.rect(u - top, H - 0.25, u + top, H + 0.1, lava, lava, 0.05, lava, 1, 0.1); for (let i = 0; i < 3; i++) { const x = u + (r() - 0.5) * top * 1.5, len = 3 + r() * 4; lb.tri(x - 0.25, H, x + 0.25, H, x + (r() - 0.5) * 2, H - len, lava, lava, C(0xc4260e), 0.05, lava, 1, 0.1); } for (let i = 0; i < 6; i++) lb.circle(u + (r() - 0.3) * 2 + i * 0.5, H + 1 + i * 1.1, 0.8 + i * 0.35, C(0x8a7a80), C(0x6a5a64), 0.35, 14, 1, null, 0, 0, 0, 0.1 + i * 0.05); } } }, { zl: 60, s: 0.24, sy: 0.18, draw: (lb, u0, u1, r) => { const p = (u: number) => 0.6 + noise1(u * 0.35, 3) * 2.6 + noise1(u * 1.3, 9) * 0.6; ridge(lb, u0, u1, p, C(0x4a2a30), C(0x3a2026), 0.2, 0.3, 0.25, 3); for (let u = u0; u < u1; u += 4 + r() * 6) { const g = C(0xff8a2a).multiplyScalar(1.4); lb.tri(u, p(u) - 0.1, u + 0.2, p(u + 0.2) - 0.1, u + 0.3 + r(), -3, g, g, C(0xc4260e), 0.05, g, 1, 0.1); } } }, { zl: 34, s: 0.45, sy: 0.35, draw: (lb, u0, u1, r) => { const p = (u: number) => -0.4 + noise1(u * 0.5, 11) * 1.6; ridge(lb, u0, u1, p, C(0x3a2026), C(0x2a1418), 0.06, 0.1, 0.25, 2); void r; } }, ], cave: [ { zl: 90, s: 0.1, sy: 0.08, draw: (lb, u0, u1, r) => { const ceil = (u: number) => 8.5 + noise1(u * 0.3, 2) * 2; const floor = (u: number) => -1 + noise1(u * 0.25, 5) * 2.5; const step = 0.4; const rock = C(0x3a2c5c), rockD = C(0x2a1f45); for (let u = u0; u < u1; u += step) { lb.rect(u, ceil(u), u + step + 0.01, 60, rockD, rock, 0.3); lb.tri(u, ceil(u), u + step, ceil(u + step), u + step, ceil(u) + 1, rock, rock, rockD, 0.3, null, 0, 0.1); lb.rect(u, -90, u + step + 0.01, floor(u), rock, rockD, 0.3); } for (let u = u0; u < u1; u += 1 + r() * 2.5) { const c = ceil(u), len = 1 + r() * 2.5, w = 0.3 + r() * 0.5; lb.tri(u - w, c + 0.3, u + w, c + 0.3, u + (r() - 0.5) * 0.2, c - len, rock, rock, rockD, 0.3, null, 0, 0.1); } for (let u = u0; u < u1; u += 6 + r() * 10) { const gc = (r() < 0.5 ? C(0x8ff0ff) : C(0xff9ae6)).multiplyScalar(1.4); const y = floor(u) + 0.1; lb.tri(u - 0.2, y, u + 0.2, y, u, y + 0.9, gc, gc, gc, 0.1, gc, 1, 0.1); lb.tri(u + 0.1, y, u + 0.4, y, u + 0.45, y + 0.55, gc, gc, gc, 0.1, gc, 1, 0.1); } } }, { zl: 50, s: 0.25, sy: 0.2, draw: (lb, u0, u1, r) => { const rock = C(0x4a3a72), rockD = C(0x33285a); for (let u = u0; u < u1; u += 1.2 + r() * 2.5) { const len = 1.5 + r() * 3, w = 0.35 + r() * 0.6; lb.tri(u - w, 12, u + w, 12, u, 9.2 - len, rock, rock, rockD, 0.2); } lb.rect(u0, 9.5, u1, 60, rockD, rockD, 0.2, null, 0, 0.1); for (let u = u0; u < u1; u += 10 + r() * 14) { const w = 1 + r() * 1.5; lb.rect(u - w / 2, -90, u + w / 2, 60, rock, rockD, 0.2, null, 0, 0.2); } const floor = (u: number) => -0.8 + noise1(u * 0.5, 8) * 1.5; ridge(lb, u0, u1, floor, rock, rockD, 0.2, 0.25, 0.3, 2, 0.3); } }, { zl: 26, s: 0.5, sy: 0.4, draw: (lb, u0, u1, r) => { const rock = C(0x2a2048); for (let u = u0; u < u1; u += 2 + r() * 4) { const len = 1 + r() * 2.2, w = 0.3 + r() * 0.4; lb.tri(u - w, 14, u + w, 14, u, 10.5 - len, rock, rock, rock, 0.05); } lb.rect(u0, 10.8, u1, 60, rock, rock, 0.05, null, 0, 0.1); } }, ], space: [ { zl: 140, s: 0.04, sy: 0.03, draw: (lb, u0, u1, r) => { const n = Math.floor((u1 - u0) * 3); for (let i = 0; i < n; i++) { const x = u0 + r() * (u1 - u0), y = -6 + r() * 22, s = 0.03 + r() * 0.06; const c = C([0xffffff, 0xfff1c0, 0xc0e0ff][i % 3]).multiplyScalar(1.5); lb.tri(x - s, y, x + s, y, x, y + s * 2.2, c, c, c, 0, c, 1); lb.tri(x - s, y, x, y - s * 2.2, x + s, y, c, c, c, 0, c, 1); } } }, { zl: 110, s: 0.07, sy: 0.05, draw: (lb, u0, u1, r) => { const cols: Array<[number, number]> = [[0xffb36a, 0xc4602e], [0x7fd6ff, 0x2f6ac4], [0xc89aff, 0x6a3ab8], [0x9fe08a, 0x3f8a4a]]; for (let u = u0 + 4; u < u1; u += 16 + r() * 18) { const [a, b] = cols[Math.floor(r() * cols.length)]; const y = 3 + r() * 6, rad = 0.8 + r() * 1.8; lb.circle(u, y, rad, C(a).multiplyScalar(1.1), C(b), 0, 28, 1, C(a).multiplyScalar(0.7), 1, -0.35, 0.35); if (r() < 0.5) { const rc = C(0xf4e6c8); for (let i = 0; i < 32; i++) { const a0 = (i / 32) * Math.PI * 2, a1 = ((i + 1) / 32) * Math.PI * 2; if (Math.sin(a0) > 0.2) continue; const R0 = rad * 1.35, R1 = rad * 1.75; lb.tri(u + Math.cos(a0) * R0, y + Math.sin(a0) * R0 * 0.28, u + Math.cos(a0) * R1, y + Math.sin(a0) * R1 * 0.28, u + Math.cos(a1) * R1, y + Math.sin(a1) * R1 * 0.28, rc, rc, rc, 0, rc.clone().multiplyScalar(0.7), 1, 0.1); lb.tri(u + Math.cos(a0) * R0, y + Math.sin(a0) * R0 * 0.28, u + Math.cos(a1) * R1, y + Math.sin(a1) * R1 * 0.28, u + Math.cos(a1) * R0, y + Math.sin(a1) * R0 * 0.28, rc, rc, rc, 0, rc.clone().multiplyScalar(0.7), 1, 0.1); } } } } }, { zl: 45, s: 0.3, sy: 0.25, draw: (lb, u0, u1, r) => { let ci = 0; for (let u = u0; u < u1; u += 3 + r() * 6) { const c = C(0x6a6088); lb.circle(u, -1 + r() * 10, 0.2 + r() * 0.5, c.clone().multiplyScalar(1.3), c, 0.05, 7, 1.3, c.clone().multiplyScalar(0.8), 1, -0.3, 0.3, (ci++ % 3) * 0.05); } } }, ], }; PRESETS.jungle = PRESETS.forest; PRESETS.fire = PRESETS.volcano; export const BACKDROP_PRESETS = Object.keys(PRESETS); /** * Crea las capas del preset. opts: { horizon?: y del horizonte (por defecto spawn.default.y - 0.5), * layers?: número máximo de capas, seed?, tint?: color hex que se mezcla en todas las capas, tintAmount? }. */ export function buildBackdrop(game: Game, def: ZoneDef, parent: THREE.Object3D, preset: string, opts: Props = {}): void { const specs = PRESETS[preset] ?? PRESETS.hills; const b = def.bounds; const h0 = opts.horizon ?? (def.spawns.default?.y ?? 0) - 0.5; const r = rng(opts.seed ?? preset.length * 13 + 5); const tint = opts.tint !== undefined ? C(opts.tint) : null; const tintAmount = opts.tintAmount ?? 0.3; const list = opts.layers ? specs.slice(0, opts.layers) : specs; for (const sp of list) { const k = (CAM_DIST + sp.zl) / CAM_DIST; const u0 = sp.s * b.minX - HALF_W; const u1 = sp.s * b.maxX + HALF_W; const lb = new LB(); // diseñado en alturas "aparentes" sobre el horizonte con la cámara a ras de suelo lb.vOff = -1.7 * (1 - sp.sy); sp.draw(lb, u0, u1, r); if (tint) { for (let i = 0; i < lb.day.length; i += 3) { lb.day[i] += (tint.r - lb.day[i]) * tintAmount; lb.day[i + 1] += (tint.g - lb.day[i + 1]) * tintAmount; lb.day[i + 2] += (tint.b - lb.day[i + 2]) * tintAmount; } } const { mesh, retint } = lb.build(); const group = new THREE.Group(); group.name = `backdrop:${preset}:${sp.zl}`; group.position.z = -sp.zl; group.scale.setScalar(k); group.add(mesh); mesh.renderOrder = -10; parent.add(group); anim.layer({ group, k, s: sp.s, sy: sp.sy, h0, retint: (night, _sky, fog) => retint(night, fog) }); } void game; void lerpC; }