#include "fragmentVersionSimple.h"
#include "uniformMacro.h"
#include "uniformPerFrameConstants.h"

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

varying vec2 uv;
varying vec4 color;
varying vec4 worldPosition;
varying vec4 fogColor;

void main() {
vec4 albedo = texture2D(TEXTURE_0, uv);

#ifdef ALPHA_TEST
if (albedo.a < 0.5)
discard;
#endif

albedo.a *= color.a;

vec2 occlusionUV = worldPosition.xz;
vec4 occlusionTexture = texture2D(TEXTURE_1, occlusionUV);

#ifndef FLIP_OCCLUSION
#define OCCLUSION_OPERATOR <
#else
#define OCCLUSION_OPERATOR >
#endif

#define OCCLUSION_LUMINANCE occlusionTexture.r
#define OCCLUSION_HEIGHT_ONES occlusionTexture.g
#define OCCLUSION_HEIGHT_TENS occlusionTexture.b

float occlusion_height = OCCLUSION_HEIGHT_ONES + (OCCLUSION_HEIGHT_TENS * 255.0f) - (OCCLUSION_HEIGHT_OFFSET / 255.0f);

#ifndef NO_OCCLUSION
// clamp the uvs
if (occlusionUV.x >= 0.0 && occlusionUV.x <= 1.0 &&
occlusionUV.y >= 0.0 && occlusionUV.y <= 1.0 &&
worldPosition.y OCCLUSION_OPERATOR occlusion_height) {
albedo.a = 0.0;
}
#endif

float mixAmount = (worldPosition.y - occlusion_height)*25.0;
vec2 lightingUVs = vec2(OCCLUSION_LUMINANCE, 1.0);
lightingUVs.x = mix(lightingUVs.x, 0.0, mixAmount);

vec3 lighting = texture2D(TEXTURE_2, lightingUVs).rgb;
vec4 finalOutput = albedo;
finalOutput.rgb *= lighting.rgb;

//apply fog
gl_FragColor.rgb = mix(finalOutput.rgb, fogColor.rgb, fogColor.a);
gl_FragColor.a = finalOutput.a;
}
