/* __   ______________
  / /  /  _/_  __/ __/
 / /___/ /  / / / _/
/____/___/ /_/ /___/

Aurora Astral 1.0 - get_sky.glsl
Sky render. - Renderização do céu. 
*/

vec3 sky_color;

#if AA_TYPE > 0
    float dither = shifted_dither13(gl_FragCoord.xy);
#else
    float dither = dither13(gl_FragCoord.xy);
#endif

dither = (dither - .5) * 0.03125;

#if ((COLOR_SCHEME == 8 && SIMPLE_SKY == 0) || COLOR_SCHEME == 12 || COLOR_SCHEME == 13) && !defined UNKNOWN_DIM
    vec4 fragpos = gbufferProjectionInverse * (vec4(gl_FragCoord.xy * vec2(pixel_size_x, pixel_size_y) / RENDER_SCALE, gl_FragCoord.z, 1.0) * 2.0 - 1.0);
    vec3 nfragpos = normalize(fragpos.xyz);
    float n_u = clamp(dot(nfragpos, up_vec) + 0.1 + dither, 0.0, 1.0);

    float raw_blend = pow(n_u, 0.5); // Sky height
    float blend_initial = mix(0.0, 1.0, raw_blend);

    float transition1_start = 0.0; // Horizon start
    float transition_mid_point = 0.6; // Mid sky max
    float transition2_end = 1.0; // Mid sky end

    // - COLOR INTERPOLATIONS - //
    
    #include "/src/current_sky_color.glsl"
    current_low_sky_color = xyz_to_rgb(current_low_sky_color);
    current_mid_sky_color = xyz_to_rgb(current_mid_sky_color);
    current_hi_sky_color = xyz_to_rgb(current_hi_sky_color);

    float t1 = smoothstep(transition_mid_point, transition2_end, blend_initial + (final_sun_factor * day_blend_float(0.0, 0.0, 0.1)));
    float t2 = smoothstep(transition1_start, transition_mid_point, blend_initial - day_blend_float(0.05, 0.1, 0.05) - (final_sun_factor * day_blend_float(0.05, 0.05, 0.0)));

    vec3 temp_sky_color = mix(current_mid_sky_color * biome_sky, current_hi_sky_color * biome_sky, t1);
    sky_color = mix(current_low_sky_color * biome_sky_low, temp_sky_color, t2);

    // Atmospheric scattering enhancement for layered cinematic skies.
    #ifdef PREPARE_SHADER
    {
        vec3 viewDir = normalize((gbufferModelViewInverse * vec4(nfragpos, 0.0)).xyz);
        vec3 sunDir = normalize((gbufferModelViewInverse * vec4(normalize(sunPosition), 0.0)).xyz);
        
        // Sun-view angle for Mie scattering (sun glow)
        float cos_theta = dot(viewDir, sunDir);
        
        // Henyey-Greenstein phase function for sun glow
        float g = 0.76;
        float g2 = g * g;
        float mie_phase = (1.0 - g2) / (4.0 * 3.14159 * pow(1.0 + g2 - 2.0 * g * cos_theta, 1.5));
        
        // Rayleigh phase
        float rayleigh_phase = 0.75 * (1.0 + cos_theta * cos_theta);
        
        // Rayleigh scattering color (blue wavelength scatter more)
        vec3 rayleigh_color = vec3(0.18, 0.34, 0.56) * rayleigh_phase
            #if COLOR_SCHEME == 13
                * 0.045;
            #else
                * 0.08;
            #endif
        
        // Mie (sun glow) warm color near sun
        vec3 mie_color = vec3(1.0, 0.85, 0.6) * mie_phase
            #if COLOR_SCHEME == 13
                * 0.085;
            #else
                * 0.15;
            #endif
        
        // Height-based atmospheric density
        float height_factor = clamp(viewDir.y + 0.1, 0.0, 1.0);
        float atmosphere_density = exp(-height_factor * 2.0);
        
        // Combine: stronger at horizon, weaker at zenith
        vec3 scatter = (rayleigh_color + mie_color) * atmosphere_density;
        
        // Day/night fade: only apply during daytime
        float scatter_strength = light_mix * (1.0 - rainStrength * 0.8);
        sky_color += scatter * scatter_strength;
    }
    #endif

    // Aurora Astral's authored four-stage sky. Aurora's original day_blend uses
    // one shared colour for both dawn and dusk; this coherent time branch lets
    // morning, noon and sunset carry distinct cosmic identities. The artistic
    // visibility is exactly zero through the midnight window, preserving the
    // existing night gradient without any grade or luminance change.
    float astralTime = clamp(day_moment, 0.0, 1.0);
    vec3 astralLow;
    vec3 astralMid;
    vec3 astralHigh;
    float astralSkyVisibility = 1.0;

    const vec3 astralDawnLow = vec3(0.46, 0.30, 0.30);
    const vec3 astralDawnMid = vec3(0.24, 0.18, 0.30);
    const vec3 astralDawnHigh = vec3(0.080, 0.100, 0.220);
    const vec3 astralDayLow = vec3(0.47, 0.65, 0.76);
    const vec3 astralDayMid = vec3(0.16, 0.33, 0.50);
    const vec3 astralDayHigh = vec3(0.075, 0.22, 0.42);
    const vec3 astralDuskLow = vec3(0.70, 0.34, 0.22);
    const vec3 astralDuskMid = vec3(0.28, 0.13, 0.25);
    const vec3 astralDuskHigh = vec3(0.070, 0.070, 0.190);

    if (astralTime < 0.25) {
        float morningProgress = smoothstep(0.015, 0.235, astralTime);
        astralLow = mix(astralDawnLow, astralDayLow, morningProgress);
        astralMid = mix(astralDawnMid, astralDayMid, morningProgress);
        astralHigh = mix(astralDawnHigh, astralDayHigh, morningProgress);
    } else if (astralTime < 0.50) {
        float eveningProgress = smoothstep(0.27, 0.495, astralTime);
        astralLow = mix(astralDayLow, astralDuskLow, eveningProgress);
        astralMid = mix(astralDayMid, astralDuskMid, eveningProgress);
        astralHigh = mix(astralDayHigh, astralDuskHigh, eveningProgress);
    } else if (astralTime < 0.66) {
        astralLow = astralDuskLow;
        astralMid = astralDuskMid;
        astralHigh = astralDuskHigh;
        astralSkyVisibility = 1.0
            - smoothstep(0.50, 0.66, astralTime);
    } else if (astralTime < 0.90) {
        astralLow = astralDawnLow;
        astralMid = astralDawnMid;
        astralHigh = astralDawnHigh;
        astralSkyVisibility = 0.0;
    } else {
        astralLow = astralDawnLow;
        astralMid = astralDawnMid;
        astralHigh = astralDawnHigh;
        astralSkyVisibility = smoothstep(0.90, 0.995, astralTime);
    }

    float astralMidBlend = smoothstep(0.02, 0.62, blend_initial);
    float astralHighBlend = smoothstep(0.52, 1.0, blend_initial);
    vec3 astralGradient = mix(astralLow, astralMid, astralMidBlend);
    astralGradient = mix(astralGradient, astralHigh, astralHighBlend);
    float astralWeatherClarity = 1.0 - rainStrength * 0.72;
    #if COLOR_SCHEME == 13
        float astralGradientStrength = 0.44;
    #else
        float astralGradientStrength = 0.70;
    #endif
    sky_color = mix(
        sky_color,
        astralGradient,
        astralSkyVisibility * astralWeatherClarity * astralGradientStrength
    );
#elif COLOR_SCHEME == 11 // Vanilla
    vec4 fragpos =
        gbufferProjectionInverse *
        (vec4(gl_FragCoord.xy * vec2(pixel_size_x, pixel_size_y) / RENDER_SCALE, gl_FragCoord.z, 1.0) * 2.0 - 1.0);
    vec3 nfragpos = normalize(fragpos.xyz);
    float n_u = clamp(dot(nfragpos, up_vec) - 0.1 + dither, 0.0, 1.0);
    
    float raw_blend = pow(n_u, 0.22); // Sky height
    float blend_initial = mix(0.0, 1.0, raw_blend);

    float transition1_start = 0.0; // Horizon start
    float transition_mid_point = 0.65; // Mid sky max
    float transition2_end = 1.0; // Mid sky end

    // - COLOR INTERPOLATIONS - //
    
    #include "/src/current_sky_color.glsl"
    current_low_sky_color = xyz_to_rgb(current_low_sky_color);
    current_hi_sky_color = xyz_to_rgb(current_hi_sky_color);

    float t2 = smoothstep(transition1_start, transition_mid_point, blend_initial - 0.2 - (final_sun_factor * day_blend_float(0.05, 0.05, 0.05)));

    sky_color = mix(current_low_sky_color, current_hi_sky_color, t2);
#else // Using legacy color interpolation.
    vec4 fragpos =
        gbufferProjectionInverse *
        (vec4(gl_FragCoord.xy * vec2(pixel_size_x, pixel_size_y) / RENDER_SCALE, gl_FragCoord.z, 1.0) * 2.0 - 1.0);
    vec3 nfragpos = normalize(fragpos.xyz);
    float n_u = clamp(dot(nfragpos, up_vec) + dither, 0.0, 1.0);
    
    sky_color = mix(low_sky_color, hi_sky_color, smoothstep(0.0, 1.0, pow(n_u, 0.333)));
    sky_color = xyz_to_rgb(sky_color);
#endif

#ifdef GBUFFER_SKYBASIC
    vec4 background_color = vec4(sky_color, 1.0);
#endif

#ifdef PREPARE_SHADER
    vec3 block_color = sky_color;
#endif
