/** * Cielo procedural a pantalla completa: degradado de 3 tonos, sol con halo, luna con cráteres suaves, * estrellas que titilan, nubes blandas que derivan y velo "aurora". Se dibuja antes de la escena * (SkyPass limpia color+profundidad y pinta el cielo; RenderPass dibuja encima sin limpiar). * El parallax usa la posición de la cámara para que el cielo se mueva un poco al avanzar. */ import * as THREE from 'three'; import { Pass, FullScreenQuad } from 'three/examples/jsm/postprocessing/Pass.js'; import type { EnvFull } from './env'; const vert = /* glsl */ ` varying vec2 vUv; void main() { vUv = uv; gl_Position = vec4( position.xy, 0.0, 1.0 ); } `; const frag = /* glsl */ ` precision highp float; varying vec2 vUv; uniform vec3 uTop, uHorizon, uBottom, uSunDisc, uSunColor, uCloud, uAurora; uniform float uSun, uMoon, uStars, uClouds, uAuroraAmt, uTime, uAspect; uniform vec2 uSunPos, uScroll; float hash( vec2 p ) { p = fract( p * vec2( 123.34, 456.21 ) ); p += dot( p, p + 45.32 ); return fract( p.x * p.y ); } float noise( vec2 p ) { vec2 i = floor( p ), f = fract( p ); f = f * f * ( 3.0 - 2.0 * f ); return mix( mix( hash( i ), hash( i + vec2( 1, 0 ) ), f.x ), mix( hash( i + vec2( 0, 1 ) ), hash( i + vec2( 1, 1 ) ), f.x ), f.y ); } float fbm( vec2 p ) { float v = 0.0, a = 0.5; for ( int i = 0; i < 4; i++ ) { v += a * noise( p ); p *= 2.03; a *= 0.5; } return v; } void main() { vec2 uv = vUv; float y = clamp( uv.y + uScroll.y * 0.006, 0.0, 1.0 ); // degradado: abajo → horizonte (≈0.3) → arriba, con curva suave vec3 col = mix( uBottom, uHorizon, smoothstep( 0.0, 0.32, y ) ); col = mix( col, uTop, smoothstep( 0.22, 1.0, y ) ); vec2 p = vec2( ( uv.x - 0.5 ) * uAspect, uv.y ); vec2 sp = vec2( ( uSunPos.x - 0.5 ) * uAspect, uSunPos.y ) - vec2( uScroll.x * 0.0015, 0.0 ); float d = length( p - sp ); // aurora / velo if ( uAuroraAmt > 0.0 ) { float band = fbm( vec2( p.x * 1.4 + uTime * 0.02, y * 3.0 - uTime * 0.03 ) ); float a = smoothstep( 0.35, 0.95, y ) * smoothstep( 0.35, 0.75, band ) * uAuroraAmt; col += uAurora * a * 0.6; } // estrellas (dos capas con parallax distinto) if ( uStars > 0.0 ) { for ( int l = 0; l < 2; l++ ) { float sc = l == 0 ? 90.0 : 150.0; vec2 q = ( p + vec2( uScroll.x * ( l == 0 ? 0.0012 : 0.0006 ), 0.0 ) ) * sc; vec2 id = floor( q ); float h = hash( id + float( l ) * 17.0 ); if ( h > 0.93 ) { vec2 c = id + 0.5 + ( vec2( hash( id + 3.1 ), hash( id + 7.7 ) ) - 0.5 ) * 0.6; float r = length( q - c ); float tw = 0.6 + 0.4 * sin( uTime * ( 1.5 + h * 4.0 ) + h * 60.0 ); float s = smoothstep( 0.22, 0.0, r ) * tw * ( l == 0 ? 1.0 : 0.6 ); s += smoothstep( 0.05, 0.0, abs( q.x - c.x ) ) * smoothstep( 0.6, 0.0, abs( q.y - c.y ) ) * tw * 0.3 * step( 0.985, h ); col += vec3( 1.0, 0.95, 0.85 ) * s * uStars * smoothstep( 0.2, 0.55, y ); } } } // sol: disco + halo cálido if ( uSun > 0.0 ) { float disc = smoothstep( 0.052, 0.046, d ); float halo = exp( -d * 7.0 ) * 0.55 + exp( -d * 2.2 ) * 0.25; col += uSunColor * halo * uSun; col = mix( col, uSunDisc * 2.2, disc * uSun ); } // luna con cráteres suaves y halo frío if ( uMoon > 0.0 ) { float disc = smoothstep( 0.05, 0.045, d ); vec2 mq = ( p - sp ) * 30.0; float cr = smoothstep( 0.55, 0.3, fbm( mq + 4.0 ) ); vec3 moon = uSunDisc * ( 1.25 - 0.25 * cr ) * ( 0.9 + 0.25 * smoothstep( 0.06, -0.06, ( p - sp ).x + ( p - sp ).y ) ); col += uSunDisc * ( exp( -d * 9.0 ) * 0.35 + exp( -d * 3.0 ) * 0.12 ) * uMoon; col = mix( col, moon, disc * uMoon ); } // nubes blandas en la franja media if ( uClouds > 0.0 ) { vec2 cq = vec2( p.x * 1.6 + uTime * 0.008 + uScroll.x * 0.004, y * 4.0 ); float n = fbm( cq * vec2( 1.0, 1.7 ) ); float band = smoothstep( 0.18, 0.45, y ) * smoothstep( 0.95, 0.55, y ); float c = smoothstep( 0.62 - uClouds * 0.2, 0.86 - uClouds * 0.15, n ) * band; float shade = smoothstep( 0.3, 0.9, fbm( cq * vec2( 1.0, 1.7 ) + vec2( 0.0, -0.08 ) ) ); vec3 cc = mix( uCloud * 0.82 + uBottom * 0.1, uCloud * 1.05, shade ); col = mix( col, cc, c * 0.85 ); } // grano mínimo para evitar bandas en los degradados col += ( hash( gl_FragCoord.xy + fract( uTime ) ) - 0.5 ) / 255.0; gl_FragColor = vec4( col, 1.0 ); } `; export class SkyPass extends Pass { readonly material: THREE.ShaderMaterial; private quad: FullScreenQuad; readonly uniforms = { uTop: { value: new THREE.Color() }, uHorizon: { value: new THREE.Color() }, uBottom: { value: new THREE.Color() }, uSunDisc: { value: new THREE.Color() }, uSunColor: { value: new THREE.Color() }, uCloud: { value: new THREE.Color() }, uAurora: { value: new THREE.Color() }, uSun: { value: 1 }, uMoon: { value: 0 }, uStars: { value: 0 }, uClouds: { value: 0.5 }, uAuroraAmt: { value: 0 }, uTime: { value: 0 }, uAspect: { value: 16 / 9 }, uSunPos: { value: new THREE.Vector2(0.8, 0.8) }, uScroll: { value: new THREE.Vector2() }, }; constructor() { super(); this.needsSwap = false; this.material = new THREE.ShaderMaterial({ uniforms: this.uniforms, vertexShader: vert, fragmentShader: frag, depthTest: false, depthWrite: false, }); this.quad = new FullScreenQuad(this.material); } /** Copia el ambiente (en lineal) a los uniformes. */ setEnv(e: EnvFull): void { const u = this.uniforms; u.uTop.value.setHex(e.skyTop); u.uHorizon.value.setHex(e.skyHorizon); u.uBottom.value.setHex(e.skyBottom); u.uSunDisc.value.setHex(e.sunDiscColor); u.uSunColor.value.setHex(e.sunColor); u.uCloud.value.setHex(e.cloudColor); u.uAurora.value.setHex(e.auroraColor); u.uSun.value = e.sun; u.uMoon.value = e.moon; u.uStars.value = e.stars; u.uClouds.value = e.clouds; u.uAuroraAmt.value = e.aurora; u.uSunPos.value.set(e.sunX, e.sunY); } update(dt: number, camX: number, camY: number, aspect: number): void { this.uniforms.uTime.value += dt; this.uniforms.uScroll.value.set(camX, camY); this.uniforms.uAspect.value = aspect; } render(renderer: THREE.WebGLRenderer, _w: THREE.WebGLRenderTarget, readBuffer: THREE.WebGLRenderTarget): void { renderer.setRenderTarget(this.renderToScreen ? null : readBuffer); renderer.clear(true, true, true); this.quad.render(renderer); } dispose(): void { this.material.dispose(); this.quad.dispose(); } }