/** * Presets de ambiente e interpolación entre ellos. * * Campos extra que entiende el render (todos opcionales en EnvParams): * skyHorizon color de la franja del horizonte del cielo * groundColor color de rebote del suelo (luz hemisférica) * sun 0..1 visibilidad del disco solar moon 0..1 visibilidad de la luna * sunX, sunY posición del astro en pantalla (0..1) sunDiscColor color del disco * stars 0..1 densidad/brillo de estrellas clouds 0..1 cobertura de nubes cloudColor * rimColor, rimIntensity luz de borde global de los materiales toon * exposure exposición (1 = normal) * warmth -1..1 gradación (cálido > 0) saturation 1 = neutro * vignette 0..1 darkness 0..1 (cueva: apaga sol y ambiente) * aurora 0..1 velo de color en el cielo (espacio, castillo) */ import * as THREE from 'three'; import type { EnvParams } from '../core/types'; export interface EnvFull extends EnvParams { bloom: number; skyHorizon: number; groundColor: number; sun: number; moon: number; sunX: number; sunY: number; sunDiscColor: number; stars: number; clouds: number; cloudColor: number; rimColor: number; rimIntensity: number; exposure: number; warmth: number; saturation: number; vignette: number; darkness: number; aurora: number; auroraColor: number; } /** Claves que son colores (se interpolan en espacio lineal). */ export const COLOR_KEYS = [ 'skyTop', 'skyBottom', 'skyHorizon', 'fogColor', 'sunColor', 'ambientColor', 'groundColor', 'sunDiscColor', 'cloudColor', 'rimColor', 'auroraColor', ] as const; export const BASE_ENV: EnvFull = { phase: 'day', skyTop: 0x2f8cff, skyBottom: 0xbfe8ff, skyHorizon: 0xfff1d0, fogColor: 0xc8e6ff, fogNear: 38, fogFar: 150, sunColor: 0xfff0d2, sunIntensity: 2.7, ambientColor: 0xd6e8ff, ambientIntensity: 1.25, groundColor: 0xa08c6e, bloom: 0.45, sun: 1, moon: 0, sunX: 0.8, sunY: 0.82, sunDiscColor: 0xfff6d8, stars: 0, clouds: 0.6, cloudColor: 0xffffff, rimColor: 0xfff4e0, rimIntensity: 0.9, exposure: 1, warmth: 0.25, saturation: 1.12, vignette: 0.28, darkness: 0, aurora: 0, auroraColor: 0x8a5cff, }; export const ENV_PRESETS: Record> = { 'city-day': {}, 'city-dusk': { phase: 'dusk', skyTop: 0x4a3fb8, skyHorizon: 0xffb066, skyBottom: 0xff8a6a, fogColor: 0xe89a8a, fogNear: 34, fogFar: 140, sunColor: 0xffa860, sunIntensity: 2.2, ambientColor: 0xb89ad8, ambientIntensity: 1.05, groundColor: 0x7a4a5a, sun: 1, sunX: 0.22, sunY: 0.38, sunDiscColor: 0xffd27a, stars: 0.15, clouds: 0.55, cloudColor: 0xffb89a, rimColor: 0xffb070, rimIntensity: 1.2, bloom: 0.6, warmth: 0.45, saturation: 1.15, vignette: 0.34, }, 'city-night': { phase: 'night', skyTop: 0x0c0f3a, skyHorizon: 0x3a2f8a, skyBottom: 0x26206a, fogColor: 0x241f5c, fogNear: 30, fogFar: 120, sunColor: 0x9ab4ff, sunIntensity: 1.4, ambientColor: 0x7a7ae8, ambientIntensity: 1.15, groundColor: 0x2a2450, sun: 0, moon: 1, sunX: 0.78, sunY: 0.8, sunDiscColor: 0xf4f1ff, stars: 1, clouds: 0.25, cloudColor: 0x5a5aa8, rimColor: 0x8ab0ff, rimIntensity: 1.3, bloom: 0.85, warmth: -0.05, saturation: 1.12, vignette: 0.42, }, lagoon: { skyTop: 0x1f9cff, skyHorizon: 0xd8fff4, skyBottom: 0x8ee8f0, fogColor: 0x9fe6ee, fogNear: 34, fogFar: 140, sunColor: 0xfff4dc, sunIntensity: 2.6, ambientColor: 0xc4f0ff, ambientIntensity: 1.35, groundColor: 0x5aa89a, sunX: 0.7, sunY: 0.86, clouds: 0.5, rimColor: 0xd8fff8, rimIntensity: 1, bloom: 0.5, warmth: 0.12, saturation: 1.15, }, cave: { phase: 'night', skyTop: 0x0a0718, skyHorizon: 0x1c1440, skyBottom: 0x140e30, fogColor: 0x120c2a, fogNear: 14, fogFar: 60, sunColor: 0x8a7aff, sunIntensity: 0.5, ambientColor: 0x5a4aa8, ambientIntensity: 0.6, groundColor: 0x1a1030, sun: 0, moon: 0, stars: 0, clouds: 0, rimColor: 0x9a8aff, rimIntensity: 0.9, bloom: 1.0, warmth: 0, saturation: 1.1, vignette: 0.55, darkness: 0.5, }, forest: { skyTop: 0x3aa8e8, skyHorizon: 0xf4ffd0, skyBottom: 0xb8f0c0, fogColor: 0xa8dcb0, fogNear: 26, fogFar: 110, sunColor: 0xfff0c0, sunIntensity: 2.4, ambientColor: 0xc8f0b8, ambientIntensity: 1.2, groundColor: 0x4a7a3a, sunX: 0.3, sunY: 0.88, clouds: 0.4, rimColor: 0xf0ffc8, rimIntensity: 1, bloom: 0.5, warmth: 0.3, saturation: 1.14, vignette: 0.32, }, castle: { phase: 'dusk', skyTop: 0x2a1a6a, skyHorizon: 0xc070c0, skyBottom: 0x6a3a9a, fogColor: 0x5a3a8a, fogNear: 28, fogFar: 110, sunColor: 0xffc8a0, sunIntensity: 1.7, ambientColor: 0x9a7ad8, ambientIntensity: 1.0, groundColor: 0x3a2a5a, sun: 0, moon: 1, sunX: 0.75, sunY: 0.78, sunDiscColor: 0xffe8c0, stars: 0.6, clouds: 0.45, cloudColor: 0x8a5ab8, rimColor: 0xd0a0ff, rimIntensity: 1.3, bloom: 0.75, warmth: 0.1, saturation: 1.12, vignette: 0.45, aurora: 0.35, auroraColor: 0xff7ad0, }, fire: { phase: 'dusk', skyTop: 0x3a0a1a, skyHorizon: 0xff7a2a, skyBottom: 0xc0301a, fogColor: 0x9a2a1a, fogNear: 24, fogFar: 100, sunColor: 0xffa060, sunIntensity: 2.2, ambientColor: 0xff8a6a, ambientIntensity: 0.95, groundColor: 0x5a1a10, sun: 0, moon: 0, stars: 0.1, clouds: 0.6, cloudColor: 0x5a2020, rimColor: 0xffa050, rimIntensity: 1.4, bloom: 1.1, warmth: 0.5, saturation: 1.15, vignette: 0.45, }, space: { phase: 'night', skyTop: 0x05041a, skyHorizon: 0x2a1060, skyBottom: 0x0e0a38, fogColor: 0x120c3a, fogNear: 40, fogFar: 180, sunColor: 0xe8e4ff, sunIntensity: 2.0, ambientColor: 0x7a6ae0, ambientIntensity: 0.8, groundColor: 0x201a50, sun: 0, moon: 1, sunX: 0.82, sunY: 0.74, sunDiscColor: 0xffe8a0, stars: 1.4, clouds: 0, rimColor: 0xa0b8ff, rimIntensity: 1.5, bloom: 0.95, warmth: -0.1, saturation: 1.15, vignette: 0.45, aurora: 0.6, auroraColor: 0x7a4aff, }, }; /** Ambiente completo a partir de un preset o de parámetros (sobre la base del preset `base`). */ export function resolveEnv(env: string | Partial, current?: EnvFull): EnvFull { if (typeof env === 'string') { const p = ENV_PRESETS[env]; if (!p) console.warn(`[render] preset de ambiente desconocido: ${env}`); return { ...BASE_ENV, ...(p ?? {}) }; } // parámetros sueltos: si traen `preset`, parten de él; si no, del ambiente actual const from = typeof env.preset === 'string' ? resolveEnv(env.preset) : current ?? BASE_ENV; return { ...from, ...env } as EnvFull; } const ca = new THREE.Color(); const cb = new THREE.Color(); const COLOR_SET = new Set(COLOR_KEYS); /** out = lerp(a, b, t) campo a campo (colores en lineal; lo no numérico salta al destino). */ export function lerpEnv(out: EnvFull, a: EnvFull, b: EnvFull, t: number): void { for (const k of Object.keys(b)) { const va = a[k]; const vb = b[k]; if (typeof vb !== 'number' || typeof va !== 'number') { out[k] = vb; } else if (COLOR_SET.has(k)) { ca.setHex(va); cb.setHex(vb); out[k] = ca.lerp(cb, t).getHex(); } else { out[k] = va + (vb - va) * t; } } }