// __multiversion__
// This signals the loading code to prepend either #version 100 or #version 300 es as apropriate.

#include "fragmentVersionSimple.h"
#include "uniformMacro.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 highp vec4 fogColor;
varying highp vec4 fc;
varying highp vec2 fct;

#include "dect.h"

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

#ifdef SNOW
#define OCCLUSION_HEIGHT occlusionTexture.g
#define OCCLUSION_LUMINANCE occlusionTexture.r
#else
#define OCCLUSION_HEIGHT occlusionTexture.a
#define OCCLUSION_LUMINANCE occlusionTexture.b
#endif

	// clamp the uvs
#ifndef NO_OCCLUSION
	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);


    highp float chuva = Rain(fct);
	highp float v = 1.0;
	highp float t = 1.0;

if(Nether(fct)){//nether
highp vec3 La = vec3(0.5,0.1,-0.3);
	//nether do fumaça
	albedo.rgb += mix(vec3(0.0,0.0,0.0), mix(vec3(0.0,0.0,0.0), La * 6.0, pow(color.x,1.0)), clamp(fc.x -0.1, 0.0, 1.0));
	albedo.rgb *= mix(albedo.rgb, vec3(0.1,0.1,4.0), clamp(fc.y - (fc.x - 0.1), 0.0, 1.0));
	albedo.rgb += mix(vec3(0.0,0.0,0.0), (vec3(0.0,0.0,albedo.b) * 15.0) * pow(color.x,0.5), clamp(fc.y - (fc.x - 0.1), 0.0, 1.0));
	v *= 1.0;
}else{
if(Underwater(fct)){
	}else{
	v *= 0.7;
	t *= 0.3;
}}


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

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


