/** * Props 3D del kit hechos con formas redondeadas compartidas. Cada builder recibe un grupo con * origen en la base (y = 0) y devuelve su altura de referencia. Las mallas con `userData.sway` * se doblan con el viento (shader) cuando el prop se fusiona en `finish()`. */ import * as THREE from 'three'; import type { Props } from '../../core/types'; import { emojiMesh } from '../../render/emoji'; import { anim } from './anim'; import { crystalMat } from './terrain'; import { P, canvasTex, kitMat, mixHex, rng, shade, solid } from './util'; export interface PropInfo { /** Altura de referencia (para el viento). */ h: number; /** No se fusiona (animado por objeto). */ dynamic?: boolean; } interface PC { r: () => number; o: Props; } type Builder = (g: THREE.Group, c: PC) => PropInfo; const PALETTE_FLOWERS = [0xf2452e, 0xff7ab8, 0xffffff, 0xa455f0, 0x2fa8f5, 0xffc928]; function part(g: THREE.Object3D, geo: THREE.BufferGeometry, mat: THREE.Material, x: number, y: number, z: number, sx: number, sy: number, sz: number, rx = 0, ry = 0, rz = 0, sway = 0): THREE.Mesh { const m = new THREE.Mesh(geo, mat); m.position.set(x, y, z); m.scale.set(sx, sy, sz); m.rotation.set(rx, ry, rz); m.castShadow = true; m.receiveShadow = true; if (sway) m.userData.sway = sway; g.add(m); return m; } // ───────────── Materiales que dependen de la fase del día ───────────── let globalsReady = false; const pulsing = new Set(); export function winMat(lit: unknown): THREE.Material { if (lit === true) return kitMat('winOn', { color: 0x3a3550, emissive: 0xffc95a, emissiveIntensity: 1.5, gloss: 0.9 }); if (lit === false) return kitMat('winOff', { color: 0x9fd3ff, gloss: 0.95, rim: 0.8 }); ensureGlobals(); return kitMat('winAuto', { color: 0x9fd3ff, emissive: 0xffc95a, emissiveIntensity: 0, gloss: 0.95, rim: 0.8 }); } function lampMat(lit: unknown): THREE.Material { if (lit === false) return kitMat('lampOff', { color: 0xfff3c0, gloss: 0.9 }); ensureGlobals(); return lit === true ? kitMat('lampOn', { color: 0xfff3c0, emissive: 0xffd36a, emissiveIntensity: 2.2, gloss: 0.9 }) : kitMat('lampAuto', { color: 0xfff3c0, emissive: 0xffd36a, emissiveIntensity: 0.4, gloss: 0.9 }); } function flameMat(inner = false): THREE.Material { ensureGlobals(); return inner ? kitMat('flameIn', { color: 0xfff2a0, emissive: 0xffe070, emissiveIntensity: 2.5, gloss: 0 }) : kitMat('flame', { color: 0xff8a1a, emissive: 0xff6a10, emissiveIntensity: 2, gloss: 0 }); } export function glowCrystalMat(color: number): THREE.Material { ensureGlobals(); const m = kitMat(`crys:${color}`, { color, emissive: color, emissiveIntensity: 0.6, gloss: 1, rim: 0.9, transparent: true, opacity: 0.92 }); pulsing.add(m); return m; } type EmissiveMat = THREE.Material & { emissiveIntensity?: number; color?: THREE.Color }; export function ensureGlobals(): void { if (globalsReady) return; globalsReady = true; // crea los materiales con sus opciones completas antes de que el ticker los pida por clave winMat(undefined); lampMat(undefined); flameMat(); flameMat(true); crystalMat(0); crystalMat(1); anim.global((t, phase) => { // Las ventanas/lámparas siguen el factor de noche SUAVIZADO (anim.night), // no la fase discreta: así se encienden y apagan con un fundido, en sync // con el cielo y con las ventanas del fondo, en vez de saltar de golpe en // el cambio de fase (ese salto es lo que se veía como «prenden y apagan»). void phase; const night = Math.max(0, anim.night); const w = kitMat('winAuto', { color: 0x9fd3ff }) as EmissiveMat; w.emissiveIntensity = night * 1.6; w.color?.setHex(night > 0 ? mixHex(0x9fd3ff, 0x3a3550, Math.min(1, night * 1.4)) : 0x9fd3ff); const l = kitMat('lampAuto', { color: 0xfff3c0 }) as EmissiveMat; l.emissiveIntensity = 0.4 + night * 2.2; const f = kitMat('flame', { color: 0xff8a1a }) as EmissiveMat; f.emissiveIntensity = 1.8 + Math.sin(t * 3) * 0.08; // vaivén suave, sin titileo const fi = kitMat('flameIn', { color: 0xfff2a0 }) as EmissiveMat; fi.emissiveIntensity = 2.3 + Math.sin(t * 3.4) * 0.08; for (const m of pulsing) (m as EmissiveMat).emissiveIntensity = 0.5 + 0.25 * Math.sin(t * 2.2) + night * 0.3; for (const key of ['crystalCyan', 'crystalPink']) { const c = kitMat(key, { color: 0xffffff }) as EmissiveMat; c.emissiveIntensity = 0.6 + 0.25 * Math.sin(t * 2 + key.length); } }); } // ───────────── Variante con viento ───────────── const swayCache = new Map(); /** Copia del material que dobla los vértices con el atributo aSway (fusionados en mundo). */ export function swayVariant(base: THREE.Material): THREE.Material { let v = swayCache.get(base); if (v) return v; v = base.clone(); v.userData.shared = true; const proto = Object.getPrototypeOf(base) as THREE.Material; const time = anim.time; v.onBeforeCompile = function (this: THREE.Material, sh, r) { proto.onBeforeCompile.call(this, sh, r); if (!sh.vertexShader.includes('#include ')) return; sh.uniforms.uWindT = time; sh.vertexShader = 'attribute float aSway;\nuniform float uWindT;\n' + sh.vertexShader.replace('#include ', `#include { float ehS = sin(uWindT * 1.7 + transformed.x * 0.55 + transformed.z * 0.3) + 0.35 * sin(uWindT * 3.3 + transformed.x * 1.4); transformed.x += aSway * ehS * 0.11; transformed.z += aSway * cos(uWindT * 1.3 + transformed.x * 0.7) * 0.04; transformed.y -= aSway * abs(ehS) * 0.015; }`); }; v.customProgramCacheKey = function (this: THREE.Material) { return proto.customProgramCacheKey.call(this) + '|ehsway'; }; swayCache.set(base, v); return v; } // ───────────── Texto de carteles ───────────── function signTexture(text: string): THREE.Texture { return canvasTex(`sign:${text}`, 512, (g, s) => { g.fillStyle = '#f3d9a8'; g.fillRect(0, 0, s, s); g.fillStyle = '#5a3418'; g.textAlign = 'center'; g.textBaseline = 'middle'; const lines = text.split('\n'); let size = 120; g.font = `700 ${size}px Fredoka, "Segoe UI", sans-serif`; const widest = Math.max(...lines.map((l) => g.measureText(l).width)); size = Math.min(size, (size * s * 0.86) / Math.max(1, widest), (s * 0.8) / lines.length); g.font = `700 ${size}px Fredoka, "Segoe UI", sans-serif`; lines.forEach((l, i) => g.fillText(l, s / 2, s / 2 + (i - (lines.length - 1) / 2) * size * 1.05)); }); } // ───────────── Builders ───────────── const wood = (k = 1) => solid(shade(0x9a5f34, k), 0.25); const B: Record = { tree(g, { r, o }) { const leaf = o.color ?? [0x5bd23c, 0x4cc45a, 0x72d84a][Math.floor(r() * 3)]; part(g, P.trunk(), wood(), 0, 0.95, 0, 0.24, 1.9, 0.24); part(g, P.trunk(), wood(), 0.28, 1.6, 0, 0.09, 0.6, 0.09, 0, 0, -0.9); const crowns: Array<[number, number, number, number, number]> = [ [0, 2.55, 0, 1.05, 1], [-0.8, 2.15, 0.15, 0.75, 0.9], [0.85, 2.2, -0.05, 0.8, 0.92], [0.15, 3.25, -0.1, 0.72, 1.12], [0.35, 2.35, 0.65, 0.66, 1.05], [-0.4, 2.8, -0.55, 0.7, 0.88], ]; for (const [x, y, z, s, k] of crowns) part(g, P.sphere(), solid(shade(leaf, k), 0.3), x, y, z, s, s * 0.92, s, 0, 0, 0, 0.35); if (o.fruit) for (let i = 0; i < 5; i++) part(g, P.sphereLo(), solid(0xf2452e, 0.8), (r() - 0.5) * 1.6, 2 + r(), 0.7 + r() * 0.2, 0.1, 0.1, 0.1); return { h: 3.9 }; }, pine(g, { o }) { const c = o.color ?? 0x2f9e5a; part(g, P.trunk(), wood(0.85), 0, 0.4, 0, 0.17, 0.8, 0.17); const tiers: Array<[number, number, number]> = [[1.25, 1.15, 1.5], [2.05, 0.9, 1.3], [2.75, 0.62, 1.05]]; tiers.forEach(([y, rad, h], i) => { part(g, P.cone(), solid(shade(c, 0.9 + i * 0.1), 0.3), 0, y, 0, rad, h, rad, 0, i * 0.4, 0, 0.3); if (o.snow) part(g, P.cone(), solid(0xffffff, 0.4), 0, y + h * 0.28, 0, rad * 0.55, h * 0.45, rad * 0.55, 0, 0, 0, 0.3); }); return { h: 3.3 }; }, palm(g, { r, o }) { const lean = o.lean ?? 0.45; const n = 7; let x = 0; let y = 0; for (let i = 0; i < n; i++) { const a = (lean * i) / n; part(g, P.trunk(), solid(i % 2 ? 0xb27a45 : 0xc58b52, 0.25), x, y + 0.25, 0, 0.17 - i * 0.008, 0.5, 0.17 - i * 0.008, 0, 0, -a, i / n * 0.3); x += Math.sin(a) * 0.48; y += Math.cos(a) * 0.48; } const crown = new THREE.Group(); crown.position.set(x, y, 0); g.add(crown); const leaf = o.color ?? 0x48c24a; for (let i = 0; i < 7; i++) { const piv = new THREE.Group(); piv.rotation.y = (i / 7) * Math.PI * 2 + r() * 0.3; crown.add(piv); part(piv, P.sphere(), solid(shade(leaf, 0.9 + (i % 3) * 0.08), 0.3), 0.85, -0.25, 0, 0.95, 0.09, 0.28, 0, 0, -0.45, 1); } for (let i = 0; i < 3; i++) part(crown, P.sphereLo(), solid(0x7a4a26, 0.4), Math.cos(i * 2.1) * 0.18, -0.2, Math.sin(i * 2.1) * 0.18, 0.15, 0.15, 0.15); return { h: y + 0.4 }; }, bush(g, { r, o }) { const c = o.color ?? 0x4cc43e; const blobs: Array<[number, number, number, number]> = [[0, 0.45, 0, 0.58], [-0.55, 0.33, 0.08, 0.42], [0.55, 0.36, -0.05, 0.46], [0.12, 0.78, -0.1, 0.42]]; blobs.forEach(([x, y, z, s], i) => part(g, P.sphere(), solid(shade(c, 0.9 + i * 0.06), 0.3), x, y, z, s, s * 0.9, s, 0, 0, 0, 0.25)); if (o.berries || o.flowers) { const bc = o.flowers ? 0xffffff : 0xf2452e; for (let i = 0; i < 7; i++) { const a = r() * Math.PI; part(g, P.sphereLo(), solid(bc, 0.7), Math.cos(a) * 0.6, 0.35 + Math.sin(a) * 0.45, 0.45, 0.07, 0.07, 0.07, 0, 0, 0, 0.2); } } return { h: 1.2 }; }, flower(g, { r, o }) { const c = o.color ?? PALETTE_FLOWERS[Math.floor(r() * PALETTE_FLOWERS.length)]; part(g, P.cyl(), solid(0x3fa52e, 0.3), 0, 0.3, 0, 0.03, 0.6, 0.03, 0, 0, 0, 0.6); part(g, P.sphereLo(), solid(0x5bd23c, 0.3), 0.1, 0.18, 0, 0.13, 0.04, 0.07, 0, 0, 0.5, 0.3); part(g, P.sphereLo(), solid(0x5bd23c, 0.3), -0.1, 0.26, 0, 0.12, 0.04, 0.06, 0, 0, -0.5, 0.4); for (let i = 0; i < 6; i++) { const a = (i / 6) * Math.PI * 2; part(g, P.sphereLo(), solid(c, 0.45), Math.cos(a) * 0.12, 0.64 + Math.sin(a) * 0.12, 0.02, 0.1, 0.055, 0.045, 0, 0, a, 1); } part(g, P.sphereLo(), solid(c === 0xffc928 ? 0xf2852e : 0xffc928, 0.6), 0, 0.64, 0.05, 0.075, 0.075, 0.06, 0, 0, 0, 1); return { h: 0.75 }; }, grassTuft(g, { r, o }) { const c = o.color ?? 0x5bd23c; const n = 6 + Math.floor(r() * 3); for (let i = 0; i < n; i++) { const h = 0.35 + r() * 0.35; const x = (r() - 0.5) * 0.4; part(g, P.coneLo(), solid(shade(c, 0.85 + r() * 0.3), 0.3), x, h / 2, (r() - 0.5) * 0.25, 0.06, h, 0.06, 0, 0, -x * 0.9 + (r() - 0.5) * 0.3, 1); } return { h: 0.6 }; }, rock(g, { r, o }) { const c = o.color ?? 0xa3abbf; const i = Math.floor(r() * 4); part(g, P.rock(i), solid(c, 0.25), 0, 0.42, 0, 0.75, 0.6, 0.65, 0, r() * 6, 0); part(g, P.rock(i + 1), solid(shade(c, 0.9), 0.25), 0.7, 0.18, 0.25, 0.35, 0.3, 0.32, 0, r() * 6, 0); if (o.moss !== false && r() < 0.6) part(g, P.hemi(), solid(0x5bd23c, 0.2), -0.1, 0.75, 0, 0.42, 0.14, 0.35, 0, 0, 0.2); return { h: 1 }; }, house(g, { o }) { const body = o.color ?? 0xfff1d6; const roof = o.roof ?? 0xf2452e; part(g, P.rbox(), solid(0xb4bccd, 0.2), 0, 0.12, 0, 2.8, 0.24, 2.3); part(g, P.rbox(), solid(body, 0.3), 0, 1.05, 0, 2.6, 1.85, 2.1); part(g, P.roof(), solid(roof, 0.45), 0, 1.92, 0, 3.15, 1.35, 2.55); part(g, P.rbox(), solid(shade(0x9a5f34, 0.8), 0.2), 0.8, 2.75, -0.35, 0.36, 0.8, 0.36); part(g, P.rbox(), solid(0x9a5f34, 0.35), -0.55, 0.62, 1.05, 0.62, 1.05, 0.12); part(g, P.sphereLo(), solid(0xffc928, 0.9), -0.36, 0.62, 1.13, 0.05, 0.05, 0.05); const wm = winMat(o.lit); part(g, P.rbox(), wm, 0.6, 1.15, 1.06, 0.62, 0.6, 0.08); part(g, P.rbox(), solid(0xffffff, 0.3), 0.6, 1.15, 1.1, 0.06, 0.62, 0.05); part(g, P.rbox(), solid(0xffffff, 0.3), 0.6, 1.15, 1.1, 0.64, 0.06, 0.05); part(g, P.rbox(), solid(0x5bd23c, 0.3), 0.6, 0.78, 1.14, 0.7, 0.14, 0.18); part(g, P.rbox(), wm, 0, 2.45, 1.0, 0.42, 0.42, 0.08); return { h: 3.3 }; }, building(g, { r, o }) { const floors = Math.max(1, Math.round(o.floors ?? 4)); const w = o.width ?? 3.6; const fh = 1.3; const H = floors * fh + 0.3; const c = o.color ?? [0x6ec3f4, 0xf2a65a, 0xa98ee8, 0xf27a7a, 0x7fd6a6, 0xffd36a][Math.floor(r() * 6)]; const d = 2.4; part(g, P.rbox(), solid(c, 0.3), 0, H / 2, 0, w, H, d); part(g, P.rbox(), solid(shade(c, 1.25), 0.35), 0, H + 0.1, 0, w + 0.25, 0.25, d + 0.25); const wm = winMat(o.lit); const frame = solid(0xf4f1ff, 0.3); const cols = Math.max(1, Math.floor((w - 0.4) / 0.95)); for (let f = 0; f < floors; f++) { const y = f * fh + fh * 0.6; if (f > 0) part(g, P.rbox(), solid(shade(c, 0.85), 0.3), 0, f * fh + 0.03, d / 2 + 0.02, w + 0.04, 0.1, 0.08); for (let i = 0; i < cols; i++) { const x = -w / 2 + 0.2 + ((w - 0.4) * (i + 0.5)) / cols; if (f === 0 && i === Math.floor(cols / 2)) { part(g, P.rbox(), solid(0x3b2fb8, 0.6), x, 0.55, d / 2 + 0.03, 0.75, 1.1, 0.08); part(g, P.rbox(), solid(0xf2452e, 0.4), x, 1.2, d / 2 + 0.25, 1.1, 0.12, 0.5); continue; } part(g, P.rbox(), frame, x, y, d / 2 + 0.02, 0.66, 0.78, 0.06); part(g, P.rbox(), wm, x, y, d / 2 + 0.05, 0.54, 0.66, 0.06); } } if (o.roofTop !== false) { if (r() < 0.5) { part(g, P.cyl(), solid(0x9aa3b5, 0.4), w * 0.25, H + 0.65, -0.3, 0.38, 0.8, 0.38); part(g, P.cone(), solid(0x7d8599, 0.4), w * 0.25, H + 1.2, -0.3, 0.42, 0.3, 0.42); } else { part(g, P.cyl(), solid(0x5d6478, 0.5), -w * 0.25, H + 0.8, -0.2, 0.04, 1.2, 0.04); part(g, P.sphereLo(), solid(0xf2452e, 0.6, { emissive: 0xf2452e, emissiveIntensity: 0.8 }), -w * 0.25, H + 1.45, -0.2, 0.09, 0.09, 0.09); } } return { h: H }; }, lamp(g, { o }) { const dark = solid(o.color ?? 0x3b3f5c, 0.6); part(g, P.cyl(), dark, 0, 0.12, 0, 0.2, 0.24, 0.2); part(g, P.cyl(), dark, 0, 1.6, 0, 0.07, 3, 0.07); part(g, P.sphereLo(), dark, 0, 3.08, 0, 0.12, 0.12, 0.12); part(g, P.sphere(), lampMat(o.lit), 0, 3.32, 0, 0.24, 0.26, 0.24); part(g, P.cone(), dark, 0, 3.62, 0, 0.3, 0.2, 0.3); return { h: 3.7 }; }, fence(g, { o }) { const w = o.width ?? 3; const c = solid(o.color ?? 0xf4f1ff, 0.35); const n = Math.max(2, Math.round(w) + 1); for (let i = 0; i < n; i++) { const x = -w / 2 + (w * i) / (n - 1); part(g, P.rbox(), c, x, 0.42, 0, 0.16, 0.84, 0.12); part(g, P.sphereLo(), c, x, 0.86, 0, 0.09, 0.09, 0.07); } part(g, P.rbox(), c, 0, 0.32, 0.03, w, 0.1, 0.06); part(g, P.rbox(), c, 0, 0.62, 0.03, w, 0.1, 0.06); return { h: 0.95 }; }, bench(g, { o }) { const wd = solid(o.color ?? 0xd9874a, 0.35); const iron = solid(0x3b3f5c, 0.6); for (const z of [-0.12, 0.12]) part(g, P.rbox(), wd, 0, 0.5, z, 1.7, 0.08, 0.2); for (const y of [0.78, 0.98]) part(g, P.rbox(), wd, 0, y, -0.28, 1.7, 0.14, 0.06); for (const x of [-0.7, 0.7]) { part(g, P.rbox(), iron, x, 0.25, 0, 0.08, 0.5, 0.45); part(g, P.rbox(), iron, x, 0.75, -0.3, 0.06, 0.55, 0.06); } return { h: 1.05 }; }, cloud(g, { r, o }) { const m = kitMat(`cloudProp:${o.color ?? 0}`, { color: o.color ?? 0xffffff, emissive: 0x8899bb, emissiveIntensity: 0.25, gloss: 0.15, rim: 1 }); const blobs: Array<[number, number, number, number]> = [[0, 0, 0, 0.9], [-0.95, -0.2, 0.05, 0.62], [0.95, -0.15, -0.05, 0.7], [0.4, 0.42, -0.1, 0.62], [-0.45, 0.3, 0.12, 0.55]]; for (const [x, y, z, s] of blobs) part(g, P.sphere(), m, x * (0.9 + r() * 0.2), y, z, s, s * 0.82, s * 0.8); for (const m_ of g.children as THREE.Mesh[]) m_.castShadow = false; return { h: 1, dynamic: true }; }, sign(g, { o }) { part(g, P.rbox(), wood(), 0, 0.55, -0.02, 0.14, 1.1, 0.14); part(g, P.rbox(), solid(0xc98a4b, 0.3), 0, 1.25, 0, 1.4, 0.85, 0.14); if (o.text) { const m = new THREE.Mesh(sharedPlane(), kitMat(`signText:${o.text}`, { color: 0xffffff, map: signTexture(String(o.text)), gloss: 0.2 })); m.position.set(0, 1.25, 0.075); m.scale.set(1.22, 0.7, 1); m.userData.noMerge = true; m.receiveShadow = true; g.add(m); } else { const e = emojiMesh(o.emoji ?? '➡️', 0.62); e.position.set(0, 1.25, 0.09); e.castShadow = false; e.userData.noMerge = true; g.add(e); } return { h: 1.7 }; }, crystal(g, { r, o }) { const c = o.color ?? 0xb07bff; const m = glowCrystalMat(c); const n = 4 + Math.floor(r() * 2); for (let i = 0; i < n; i++) { const h = i === 0 ? 1.1 : 0.45 + r() * 0.45; const tilt = i === 0 ? 0 : (i % 2 ? 1 : -1) * (0.35 + r() * 0.4); part(g, P.octa(), m, Math.sin(tilt) * h * 0.4, h * 0.55, (r() - 0.5) * 0.3, 0.2 + h * 0.08, h, 0.2 + h * 0.08, 0, r() * 3, tilt); } part(g, P.rock(1), solid(0x6c5a86, 0.2), 0, 0.08, 0, 0.5, 0.2, 0.4); return { h: 1.2 }; }, mushroom(g, { r, o }) { const c = o.color ?? 0xf2452e; part(g, P.trunk(), solid(0xfff1d6, 0.3), 0, 0.28, 0, 0.17, 0.56, 0.17); part(g, P.cyl(), solid(0xe9d6b8, 0.2), 0, 0.5, 0, 0.5, 0.06, 0.5); part(g, P.hemi(), solid(c, 0.55), 0, 0.5, 0, 0.58, 0.45, 0.58); for (let i = 0; i < 6; i++) { const a = r() * Math.PI * 2; const el = 0.25 + r() * 0.9; const x = Math.cos(a) * Math.cos(el) * 0.58; const z = Math.sin(a) * Math.cos(el) * 0.58; const y = 0.5 + Math.sin(el) * 0.45; if (z < -0.1) continue; part(g, P.sphereLo(), solid(0xffffff, 0.4), x, y, z, 0.08, 0.05, 0.08, 0, 0, 0); } return { h: 1 }; }, crate(g, { o }) { const c = o.color ?? 0xc98a4b; const s = 1; part(g, P.rbox(), solid(c, 0.3), 0, s / 2, 0, s, s, s); const fr = solid(shade(c, 0.7), 0.3); for (const z of [s / 2 + 0.01, -s / 2 - 0.01]) { part(g, P.rbox(), fr, 0, 0.06, z, s, 0.12, 0.05); part(g, P.rbox(), fr, 0, s - 0.06, z, s, 0.12, 0.05); part(g, P.rbox(), fr, -s / 2 + 0.06, s / 2, z, 0.12, s, 0.05); part(g, P.rbox(), fr, s / 2 - 0.06, s / 2, z, 0.12, s, 0.05); part(g, P.rbox(), fr, 0, s / 2, z, 0.1, s * 1.2, 0.04, 0, 0, Math.PI / 4); } return { h: 1 }; }, bridge(g, { r, o }) { const w = o.width ?? 6; const sag = o.sag ?? (o.solid ? 0 : 0.2); const n = Math.max(3, Math.round(w / 0.42)); const y = (t: number) => -sag * Math.sin(Math.PI * t); for (let i = 0; i < n; i++) { const t = (i + 0.5) / n; part(g, P.rbox(), solid(shade(0xc98a4b, 0.88 + r() * 0.2), 0.3), -w / 2 + t * w, y(t) - 0.08, 0, w / n - 0.06, 0.14, 1.6, 0, 0, (r() - 0.5) * 0.05); } const rope = solid(0xe8d3a8, 0.2); for (const side of [-0.75, 0.75]) { for (const x of [-w / 2, w / 2]) { part(g, P.cyl(), wood(), x, 0.35, side, 0.09, 1.1, 0.09); part(g, P.sphereLo(), wood(1.1), x, 0.92, side, 0.11, 0.08, 0.11); } const seg = 10; for (let i = 0; i < seg; i++) { const t0 = i / seg; const t1 = (i + 1) / seg; const x0 = -w / 2 + t0 * w; const x1 = -w / 2 + t1 * w; const y0 = 0.8 + y(t0) * 1.4; const y1 = 0.8 + y(t1) * 1.4; const len = Math.hypot(x1 - x0, y1 - y0); part(g, P.cyl(), rope, (x0 + x1) / 2, (y0 + y1) / 2, side, 0.035, len + 0.02, 0.035, 0, 0, Math.PI / 2 + Math.atan2(y1 - y0, x1 - x0), 0); } } return { h: 1 }; }, reeds(g, { r, o }) { const c = o.color ?? 0x4fae3c; const n = 6; for (let i = 0; i < n; i++) { const h = 1.1 + r() * 0.7; const x = (r() - 0.5) * 0.7; const lean = (r() - 0.5) * 0.2; part(g, P.cyl(), solid(shade(c, 0.9 + r() * 0.2), 0.3), x, h / 2, (r() - 0.5) * 0.3, 0.025, h, 0.025, 0, 0, lean, 1); if (i % 2 === 0) part(g, P.capsule(), solid(0x7a4a26, 0.3), x - Math.sin(lean) * h, h + 0.05, 0, 0.09, 0.22, 0.09, 0, 0, lean, 1); else part(g, P.coneLo(), solid(shade(c, 1.1), 0.3), x, h * 0.4, 0, 0.05, h * 0.8, 0.05, 0, 0, (r() - 0.5) * 0.6, 1); } return { h: 1.9 }; }, lilypad(g, { r, o }) { part(g, P.lily(), solid(o.color ?? 0x48c24a, 0.4), 0, 0.02, 0, 0.6, 0.05, 0.6, 0, r() * 6, 0); if (o.flower) { for (let i = 0; i < 6; i++) { const a = (i / 6) * Math.PI * 2; part(g, P.sphereLo(), solid(0xff9ad0, 0.5), Math.cos(a) * 0.12, 0.14, Math.sin(a) * 0.12, 0.1, 0.06, 0.05, 0, -a, 0.6); } part(g, P.sphereLo(), solid(0xffc928, 0.6), 0, 0.17, 0, 0.07, 0.06, 0.07); } return { h: 0.3, dynamic: true }; }, torch(g, { o }) { part(g, P.trunk(), wood(0.9), 0, 0.55, 0, 0.07, 1.1, 0.07); part(g, P.cone(), solid(0x5d6478, 0.6), 0, 1.15, 0, 0.18, 0.22, 0.18, Math.PI, 0, 0); part(g, P.sphere(), flameMat(), 0, 1.42, 0, 0.17, 0.3, 0.17, 0, 0, 0, 1.2); part(g, P.sphere(), flameMat(true), 0, 1.36, 0.05, 0.09, 0.16, 0.09, 0, 0, 0, 1.2); for (const m of g.children.slice(2) as THREE.Mesh[]) m.castShadow = false; void o; return { h: 1.4 }; }, stalagmite(g, { r, o }) { const c = o.color ?? 0x7a62a6; part(g, P.cone(), solid(c, 0.3), 0, 0.8, 0, 0.42, 1.6, 0.42, 0, r(), 0); part(g, P.cone(), solid(shade(c, 0.85), 0.3), 0.45, 0.4, 0.1, 0.24, 0.8, 0.24, 0, r(), 0.15); return { h: 1.6 }; }, barrel(g, { o }) { const c = o.color ?? 0xb8783f; part(g, P.sphere(), solid(c, 0.35), 0, 0.55, 0, 0.45, 0.6, 0.45); part(g, P.cyl(), solid(c, 0.35), 0, 0.55, 0, 0.42, 0.95, 0.42); for (const y of [0.22, 0.88]) part(g, P.cyl(), solid(0x5d6478, 0.7), 0, y, 0, 0.45, 0.07, 0.45); return { h: 1.1 }; }, }; const PROP_ALIASES: Record = { palmTree: 'palm', grass: 'grassTuft', tuft: 'grassTuft', stone: 'rock', box: 'crate' }; /** Profundidad por defecto de cada prop. */ const DEFAULT_Z: Record = { tree: -2.6, pine: -2.8, palm: -2.2, bush: -1.4, flower: -0.6, grassTuft: -0.3, rock: -1.2, house: -3, building: -3.2, lamp: -1.4, fence: -1.1, bench: -1.3, cloud: -6, sign: -0.9, crystal: -1.3, mushroom: -1, crate: 0, bridge: 0, reeds: -0.4, lilypad: 0.4, torch: -0.8, stalagmite: -1.8, barrel: -1, }; /** Props que se balancean con el viento por defecto (si no se fusionan, rotan como objeto). */ const SWAYERS = new Set(['flower', 'grassTuft', 'reeds']); export const PROP_KINDS = Object.keys(B); let planeGeo: THREE.PlaneGeometry | null = null; function sharedPlane(): THREE.PlaneGeometry { if (!planeGeo) { planeGeo = new THREE.PlaneGeometry(1, 1); planeGeo.userData.shared = true; } return planeGeo; } export interface BuiltProp { group: THREE.Group; info: PropInfo; kind: string; swayer: boolean; } /** Crea el prop (sin añadirlo a la escena). Devuelve null si el tipo no existe. */ export function buildProp(kind: string, x: number, y: number, o: Props): BuiltProp | null { const k = PROP_ALIASES[kind] ?? kind; const b = B[k]; if (!b) return null; const g = new THREE.Group(); g.name = `prop:${k}`; const info = b(g, { r: rng(o.seed ?? (x * 131 + y * 17 + 7)), o }); const s = o.scale ?? 1; g.position.set(x, y, o.z ?? DEFAULT_Z[k] ?? -1); g.scale.setScalar(s); if (o.rotation) g.rotation.y = o.rotation; if (o.flipX) g.rotation.y += Math.PI; return { group: g, info, kind: k, swayer: SWAYERS.has(k) && o.sway !== false }; }