uniform float screenBrightness;
uniform float shadowblend;
#ifndef OVERWORLD

vec3 calcAmbientLight(vec3 wNormal, vec2 lightmap, vec3 ambientUp, vec3 ambientDown, vec3 ambientRight,
                      vec3 ambientLeft, vec3 ambientB, vec3 ambientF, vec3 RSML, float diffuseSun, vec3 torch)
{
    vec3 ambientCoefs = wNormal / (abs(wNormal.x) + abs(wNormal.y) + abs(wNormal.z));

    vec3 clampedCoefs = clamp(ambientCoefs, 0.0, 1.0);
    vec3 clampedCoefsNeg = clamp(-ambientCoefs, 0.0, 1.0);

    float skymap = 0;

#ifndef OVERWORLD
    skymap += 5;
#endif

    vec3 ambientCombined =
        (ambientUp * clampedCoefs.y + ambientDown * clampedCoefsNeg.y + ambientRight * clampedCoefs.x +
         ambientLeft * clampedCoefsNeg.x + ambientB * clampedCoefs.z + ambientF * clampedCoefsNeg.z) *
            skymap +
        RSML;

    vec3 diffuseLight = diffuseSun * vec3(0.01, 0.002, 0.003) + ambientCombined;

    float darknessFactor = darknessLightFactor * darknessLightFactor * darknessLightFactor;

    diffuseLight *= (1.0 - 6.0 * darknessFactor);

    vec3 light = max(diffuseLight + torch + (vec3(0.002, 0.005, 0.006) * (1.0 + screenBrightness * 2.0)), 0.001);

    light *= 1.0 - darknessLightFactor;

    return vec3(light);
}
#else
vec3 calcAmbientLight(vec3 wNormal, vec2 lightmap, vec3 ambientUp, vec3 ambientDown, vec3 ambientRight,
                      vec3 ambientLeft, vec3 ambientB, vec3 ambientF, vec3 RSML, float diffuseSun, vec3 torch)
{
    vec3 ambientCoefs = wNormal / dot(abs(wNormal), vec3(1.0));
    vec3 clampedCoefs = clamp(ambientCoefs, 0.0, 1.0);
    vec3 clampedCoefsNeg = clamp(-ambientCoefs, 0.0, 1.0);

    float skymap = lightmap.y;

    vec3 ambientCombined =
        (ambientUp * clampedCoefs.y + ambientDown * clampedCoefsNeg.y + ambientRight * clampedCoefs.x +
         ambientLeft * clampedCoefsNeg.x + ambientB * clampedCoefs.z + ambientF * clampedCoefsNeg.z) *
            skymap +
        RSML;
    vec3 ambientCombined2 =
        (ambientUp * clampedCoefs.y + ambientDown * clampedCoefsNeg.y + ambientDown * clampedCoefs.x +
         ambientDown * clampedCoefsNeg.x + ambientDown * clampedCoefs.z + ambientDown * clampedCoefsNeg.z) *
            skymap +
        RSML;
    // vec3 bounce = (diffuseSun + sunElevation * sunElevation) * (sunLight * 0.05);

    ambientCombined = mix(ambientCombined2, ambientCombined, shadowblend);

    vec3 diffuseLight = diffuseSun * sunLight + ambientCombined;

    float darknessFactor = darknessLightFactor * darknessLightFactor * darknessLightFactor;

    diffuseLight *= (1.0 - 6.0 * darknessFactor);

    vec3 light = max(diffuseLight + torch + (vec3(0.002, 0.005, 0.006) * (1.0 + screenBrightness * 2.0)), 0.001);

    light *= 1.0 - darknessLightFactor;

    return vec3(light);
}

#endif

vec3 calcAmbientLight(vec3 Normal, vec2 lightmap, vec3 ambientUp, float diffuseSun, vec3 torch)
{

    vec3 ambient = calcAmbientLight(Normal, lightmap.xy, ambientUp, ambientUp, ambientUp, ambientUp, ambientUp,
                                    ambientUp, vec3(0.6 * ambientUp.b * lightmap.y), diffuseSun, torch);

    return ambient;
}