

const vec3 SR = vec3(0.000002901, 0.000006779, 0.00001655);
const vec3 SM = vec3(0.000007992, 0.000007992, 0.00000799);
const vec3 SL = vec3(10.44800000, 19.43500000, 37.1880000);

/////////////////////////

vec3 fogAtmosphereALT(vec3 rayDir, float rayL, float rayL2, inout vec3 transmittance, float fogFactor)
{
#ifdef VANILLA_SKY_COLOR
    vec3 SR2 = mix(pow(skyColor.rgb, vec3(3.0)) * 0.00003, SR, 0.0);
#else
    vec3 SR2 = SR;
#endif

#ifdef NETHER

    float lightRadiance = 0.05;

    vec3 lightPos = vec3(0.0, 1.0, 0);
    SR2 = mix(pow(sunLight, vec3(4.0)) * 0.1, SR, 0.0);
#endif
#ifdef END
    float lightRadiance = 0.005;

    vec3 lightPos = vec3(0.0, 1.0, 0);
    SR2 = mix(pow(sunLight, vec3(3.0)) * 0.1, SR, 0.0);

#endif
#ifdef OVERWORLD
    #ifndef IS_IRIS
    vec3 sunpos2 = mat3(gbufferModelViewInverse) * shadowLightPosition + gbufferModelViewInverse[3].xyz;
    vec3 lightPos = normalize(sunpos2);
    #endif
#endif

    rayDir = normalize(rayDir);
    float t2 = -(6371000.0 * rayDir.y) + sqrt(40589641000000.0 * rayDir.y * rayDir.y + 1284200000000.0);
    float CT = dot(rayDir, lightPos);
    float PR = 0.12 * (1.0 + CT * CT);
    float term = 1.7225 - 1.7 * CT;

    vec2 OD = min(vec2(rayL, rayL2), t2);
    float h = (1.0 - 1.0 / (2.0 + t2 * t2 * 0.0000000000009));
    float DR = h * h;
    float DM = h * h * h * h * h * rainStrengthInv;
    float PM = (0.2775 / (12.5664 * sqrt(term) * term)) * 0.5;
    float t = clamp((lightPos.y - 1.0) / (-1.4), 0.0, 1.0);
    float LPS = pow(t * t * (3.0 - 2.0 * t), 8.0) * h * SunsetMult;
    vec3 LC = lightRadiance * exp(-SL * LPS);
    vec3 R = (1.0 - exp(-OD.x * DR * SR2)) * PR * LC;

    vec3 M = (1.0 - exp(-OD.x * DM * SM)) * PM * LC;
    vec3 S = (R + M) * sunpow;

    transmittance = exp(-OD.y * DR * vec3(0.000007836, 0.00001457625, 0.000027891));

    if (fogFactor > 0.0)
    {
        // 2nd sample (all the way to atmosphere exit), used for fog fade.
        vec3 R2 = (1.0 - exp(-t2 * DR * SR)) * PR * LC;
        vec3 M2 = (1.0 - exp(-t2 * DM * SM)) * PM * LC;
        vec3 S2 = (mix(R, R2, 0.5) + M2) * sunpow;
        vec3 transmittance2 = exp(-t2 * DR * vec3(0.000007836, 0.00001457625, 0.000027891));

        S2 *= mix(vec3(0.8, 0.9, 1.0), vec3(1, 1, 1), (fogFactor * fogFactor)); // Fog color test
        S = mix(S, S2, fogFactor);

        transmittance.xyz = mix(transmittance.xyz, transmittance2, fogFactor);
    }

#ifdef OVERWORLD
    vec3 TS = S * 16.0 + 0.01;
    float DP = dot(TS, vec3(0.299, 0.587, 0.114));
    float DP2 = dot(S, vec3(0.299, 0.587, 0.114));

    vec3 RC = mix(vec3(0.4, 0.5, 0.6), vec3(0.016, 0.05, 0.066), (1 - nightblend5));
    vec3 NC = vec3(0.04, 0.1, 0.11);

    S = mix(S, DP * NC, (1 - nightblend5));
    S = mix(S, DP2 * RC, rainStrength * (nightblend5));

    transmittance = mix(transmittance, transmittance * RC, rainStrength * 0.5);

#endif

    return S;
}
vec3 fogAtmosphereALT2(
    vec3 rayDir,
    float rayL,
    float rayL2,
    inout vec3 transmittance,
    float fogFactor,
    float shadow)
{
    // Constants
    const vec3 SR = vec3(2.901e-6, 6.779e-6, 1.655e-5);
    const vec3 SM = vec3(7.992e-6, 7.992e-6, 7.99e-6);
    const vec3 SL = vec3(10.448, 19.435, 37.188);
    const vec3 TC = vec3(7.836e-6, 1.457625e-5, 2.7891e-5);
    const vec3 GRAY = vec3(0.299, 0.587, 0.114);

// Light properties
#ifdef END
    const float LR = 0.001;
    const vec3 LP = vec3(0.0, 1.0, 0.0);
#elif defined NETHER
    const float LR = 0.5;
    const vec3 LP = vec3(0.0);
#else
    // Default light position if needed
    float LR = lightRadiance;
    vec3 LP = lightPos;
#endif

    rayDir = normalize(rayDir);

    // Precomputed constants
    float rdY = rayDir.y;
    float t2 = -6371000.0 * rdY + sqrt(4.0589641e13 * rdY * rdY + 1.2842e12);

    float CT = dot(rayDir, LP);
    float PR = 0.12 * (1.0 + CT * CT);
    float term = 1.7225 - 1.7 * CT;

    float OD_x = min(rayL, t2);
    float OD_y = min(rayL2, t2);

    float h = 1.0 - 1.0 / (2.0 + t2 * t2 * 9e-13);
    float h2 = h * h;
    float DR = h2;
    float DM = h2 * h2 * h * rainStrengthInv; // h^5 * rainStrengthInv
    float PM = 0.2775 / (12.5664 * sqrt(term) * term) * 0.5;

    float ti = clamp((LP.y - 1.0) * -0.7142857, 0.0, 1.0); // Equivalent to (LP.y -1)/-1.4
    float tSmooth = ti * ti * (3.0 - 2.0 * ti);
    float LPS = pow(tSmooth, 8.0) * h * SunsetMult;

    vec2 atten = vec2(shadow + 0.3, shadow);
    vec3 LC = LR * exp(-SL * LPS);

    // Exponentials
    vec3 e_SR = exp(-OD_x * DR * SR);
    vec3 e_SM = exp(-OD_x * DM * SM);

    // Scattering
    vec3 S = ((1.0 - e_SR) * PR * LC * atten.x + (1.0 - e_SM) * PM * LC * atten.y) * sunpow;

#ifdef OVERWORLD
    vec3 TS = S * 16.0 + 0.01;
    float DP = dot(TS, GRAY);
    float DP2 = dot(S, GRAY);

    vec3 RC = mix(vec3(0.4, 0.5, 0.6), vec3(0.016, 0.05, 0.066), 1.0 - nightblend5);
    vec3 NC = vec3(0.04, 0.1, 0.11);

    transmittance = exp(-OD_y * DR * TC);

    if (fogFactor > 0.0)
    {
        vec3 e_SR_t2 = exp(-t2 * DR * SR);
        vec3 e_SM_t2 = exp(-t2 * DR * SM);
        vec3 S2 = (0.5 * (1.0 - e_SR_t2) * PR * LC * atten.x + (1.0 - e_SM_t2) * PM * LC * atten.y) * sunpow;
        transmittance = mix(transmittance, exp(-t2 * DR * TC), fogFactor);
        S = mix(S, S2 * mix(vec3(0.8, 0.9, 1.0), vec3(1.0), fogFactor * fogFactor), fogFactor);
    }

    S = mix(S, DP * NC, 1.0 - nightblend5);
    S = mix(S, DP2 * RC, rainStrength * nightblend5);
    transmittance = mix(transmittance, transmittance * RC, rainStrength * 0.5);

#else
    #ifdef END
    S *= 10.0;
    float dotS = dot(S, GRAY);
    S = dotS * vec3(0.184, 0.169, 0.212) * 2.0 + S * 0.1;
    transmittance = exp(-OD_y * DR * (1.0 - vec3(0.184, 0.169, 0.212)) / 20000.0) + exp(-OD_y * DR * TC) * 0.2;
    #else
    transmittance = exp(-vec3(OD_y * DR) * TC);
    S = (1.0 - exp(-OD_x * DR * SR)) * PR * fogColor * 0.5;
    #endif
#endif

    return S;
}
