uniform vec3 skycolnight;
uniform vec3 skyColorNight;
uniform vec3 sunPosWorld;
uniform float lightRadiance;
uniform float nightblend5;
uniform float nightblend5Inv;
uniform float sunpow;
uniform vec3 rainColorMix;

vec3 fogAtmosphere2(vec3 rayDir, float rayL, float rayL2, inout vec3 transmittance)
{

    ///////////
    rayDir = normalize(rayDir);
    vec3 rsc = (fogColor + (fogColor - dot(fogColor, vec3(0.299, 0.587, 0.114)))) / 10000;

    //////////
    float b0 = 6371000.0 * rayDir.y;
    float t2 = -b0 + sqrt(b0 * b0 + 1284200000000.0);
    float costh = dot(rayDir, lightPos);

    vec2 opticalDepth = min(vec2(rayL, rayL2), t2);

    float h = 1.0 - 1.0 / (2.0 + t2 * t2 * 0.0000000000009);
    float densityR = h * h;
    float densityM = densityR * densityR * h;

    float phaseR = 0.12 * (1.0 + costh * costh);
    float term = 1.7225 - 1.7 * costh;

    float phaseM = (0.2775 / (12.5664 * sqrt(term) * term)) * 0.5;
    vec3 lightColor =
        lightRadiance * exp(-vec3(10.448, 19.435, 37.188) * pow(smoothstep(1.0, -0.4, lightPos.y), 8.0) * h) +
        skycolnight;

#ifdef OVERWORLD
    vec3 R = (1.0 - exp(-opticalDepth.x * densityR * vec3(0.000002901, 0.000006779, 0.00001655))) * phaseR * lightColor;
    vec3 M = (1.0 - exp(-opticalDepth.x * densityM * vec3(0.000007992, 0.000007992, 0.00000799))) * phaseM *
             lightColor * (1 - rainStrength);
    vec3 scattering = (R + M) * sunpow;

    transmittance = exp(-opticalDepth.y * densityR * vec3(0.000007836, 0.00001457625, 0.000027891));

    scattering =
        mix(scattering, dot(scattering + 0.01, vec3(0.299, 0.587, 0.114)) * skyColorNight * 10, nightblend5Inv * 0.9);
    scattering = mix(scattering, dot(scattering, vec3(0.299, 0.587, 0.114)) * rainColorMix, rainStrength);

#else
    vec3 R = (1.0 - exp(-opticalDepth.x * densityR * vec3(0.000002901, 0.000006779, 0.00001655))) * phaseR * lightColor;
    vec3 M = (1.0 - exp(-opticalDepth.x * densityM * vec3(0.000007992, 0.000007992, 0.00000799))) * phaseM * lightColor;
    vec3 scattering = (R + M) * sunpow;
    scattering = dot(scattering + 0.01, vec3(0.299, 0.587, 0.114)) * fogColor * 2;

    transmittance = exp(-opticalDepth.y * densityR * (1 - fogColor) / 20000);

#endif

    return scattering;
}

vec3 GetAtmosphere10(vec3 rayDir)
{
    vec3 transmitance = vec3(0.0);

    return fogAtmosphere2(rayDir, 3.402823466e38, 1.0, transmitance);
}
