#version 120
#extension GL_EXT_gpu_shader4 : enable
#include "/lib/settings.glsl"

varying vec4 color;
varying vec2 lmtexcoord;
varying vec3 viewVector;
uniform float far;
uniform int frameCounter;
uniform sampler2D tex;
#include "/lib/noise.glsl"
#include "/lib/shadow_param.glsl"

//////////////////////////////VOID MAIN//////////////////////////////
//////////////////////////////VOID MAIN//////////////////////////////
//////////////////////////////VOID MAIN//////////////////////////////
//////////////////////////////VOID MAIN//////////////////////////////
//////////////////////////////VOID MAIN//////////////////////////////

float GetBorderFogMixFactor(in vec3 eyePlayerPos, in float far)
{

    float eyeDist = length(eyePlayerPos);
    float borderFogFactor = smoothstep(far * 0.75, far, eyeDist);
    borderFogFactor *= borderFogFactor * borderFogFactor;
    return (eyeDist > far * 0.75) ? borderFogFactor : 0.0;
}
void main()
{
    /* DRAWBUFFERS:0*/

#if defined DISTANT_HORIZONS && !defined SHADOW_DEBUG

    float factor = GetBorderFogMixFactor(viewVector, min(shadowDistance, far));

    float noise = noise_standard(gl_FragCoord.xy);

    if (factor > noise)
    {
        discard;
    }
#endif

#if SHADOW_FILTERING == 2
    vec4 color = texture(tex, lmtexcoord.xy) * color;
#ifdef SHADOW_DEBUG
    color.rgb *= vec3(1, 0.1, 0.1);
#endif
    gl_FragData[0] = color;
#else
    float alpha = texture(tex, lmtexcoord.xy).a;

    gl_FragData[0].a = alpha;
#endif
}
