#include "fragmentVersionCentroid.h"

#if __VERSION__ >= 300
#ifndef BYPASS_PIXEL_SHADER
#if defined(TEXEL_AA) && defined(TEXEL_AA_FEATURE)
_centroid in vec2 uv0;
_centroid in vec2 uv1;

#else

_centroid in vec2 uv0;
_centroid in vec2 uv1;
#endif
#endif

#else

#ifndef BYPASS_PIXEL_SHADER
varying vec2 uv0;
varying vec2 uv1;
#endif
#endif

varying vec4 color;
varying vec3 wp;
varying vec3 p;

#ifdef FOG
varying vec4 fogColor;
#endif

#include "uniformPerFrameConstans.h"
#include "uniformShaderConstants.h"
#include "util.h"

uniform vec2 FOG_CONTROL;
uniform vec4 FOG_COLOR;
uniform float RENDER_DISTANCE;

LAYOUT_BINDING(0) uniform sampler2D TEXTURE_0;
LAYOUT_BINDING(1) uniform sampler2D TEXTURE_1;
LAYOUT_BINDING(2) uniform sampler2D TEXTURE_2;

void main() {
#ifdef BYPASS_PIXEL_SHADER
gl_FragColor = vec4(0, 0, 0, 0);
return;
#else

#if USE_TEXEL_AA
vec4 diffuse = texture2D_AA(TEXTURE_0, uv0);
#else
vec4 diffuse = texture2D(TEXTURE_0, uv0);
#endif

#ifdef SEASONS_FAR
diffuse.a = 1.0;
#endif

bool plnt = color.g > color.r && color.g > color.b && color.a != 0.0;
bool lvs = color.g > color.r && color.g > color.b && color.a == 0.0;

#if USE_ALPHA_TEST

#ifdef ALPHA_TO_COVERAGE
#define LVS_ALPHA_THRESHOLD 0.0
#define PLNT_ALPHA_THRESHOLD 0.001
#define ALPHA_THRESHOLD 0.001

#else

#define LVS_ALPHA_THRESHOLD 0.0
#define PLNT_ALPHA_THRESHOLD 0.001
#define ALPHA_THRESHOLD 0.001
#endif

if(lvs == true && plnt == false) {
if(diffuse.a < LVS_ALPHA_THRESHOLD) discard;
}

if(lvs == false && plnt == true) {
if(diffuse.a < PLNT_ALPHA_THRESHOLD) discard;
}

if(lvs == false && plnt == false) {
if(diffuse.a < ALPHA_THRESHOLD) discard;
}

#endif

vec4 inColor = color;

#if defined(BLEND)
diffuse.a *= inColor.a;
#endif

#if !defined(ALWAYS_LIT)
diffuse *= texture2D(TEXTURE_1, uv1);
#endif

diffuse.rgb *= vec3(1.15);

#ifdef BLEND
if(color.b > color.r && color.b > color.g) {
diffuse.rgb += vec3(0.15, 0.35, 0.9);
diffuse.a *= clamp(length(wp.xyz * 4.9)/RENDER_DISTANCE * 0.70, 0.50, 1.2);
}
#endif

#ifndef SEASONS
#if !USE_ALPHA_TEST && !defined(BLEND)
diffuse.a = inColor.a;
#endif

diffuse.rgb *= inColor.rgb;
#else
vec2 uv = inColor.xy;

diffuse.rgb *= inColor.aaa;
diffuse.a = 1.0;
#endif

float atmosfer = clamp(length(-wp * 0.5)/RENDER_DISTANCE * 0.8, 0.0, 0.9);

vec3 OF_Atmosfer = mix(diffuse.rgb, FOG_COLOR.rgb, atmosfer);

diffuse.rgb = OF_Atmosfer;

#ifdef FOG
diffuse.rgb = mix(diffuse.rgb, fogColor.rgb, fogColor.a);
#endif

gl_FragColor = diffuse;

#endif // BYPASS_PIXEL_SHADER
}
