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

// To use centroid sampling we need to have version 300 es shaders, which requires changing:
// attribute to in
// varying to out when in vertex shaders or in when in fragment shaders
// defining an out vec4 FragColor and replacing uses of gl_FragColor with FragColor
// texture2D to texture
#if __VERSION__ >= 300

	// version 300 code

	#ifdef MSAA_FRAMEBUFFER_ENABLED
		#define _centroid centroid
	#else
		#define _centroid
	#endif

	#ifndef BYPASS_PIXEL_SHADER
		#if defined(TEXEL_AA) && defined(TEXEL_AA_FEATURE)
			_centroid in highp vec2 uv0;
			_centroid in highp vec2 uv1;
		#else
			_centroid in vec2 uv0;
			_centroid in vec2 uv1;
		#endif
	#endif

	#define varying in
	#define texture2D texture
	out vec4 FragColor;
	#define gl_FragColor FragColor
#else

	// version 100 code

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

varying vec4 color;

#ifdef LOW_PRECISION
varying vec4 fogColor;
varying vec4 fogCoIor;
varying float moreFog;
#endif

#ifdef NEAR_WATER
varying float cameraDist;
#endif

#include "shaders/util.h"

uniform sampler2D TEXTURE_0;
uniform sampler2D TEXTURE_1;
uniform sampler2D TEXTURE_2;
uniform highp float TIME; 
uniform vec2 FOG_CONTROL;
uniform vec4 FOG_COLOR;


#define enableMaps

float filmc(float x) {
float A = 0.42;
float B = 0.05;
float C = 0.15;
float D = 0.55;
float E = 0.05;
float F = 0.35;

return ((x * (A * x + C * B) + D * E) / (x * (A * x + B) + D * F)) - E / F;
}

vec3 Hyra(vec3 clr) {
float W = 1.3 / 1.0;
#ifdef enableMaps
float Luma = dot(clr, vec3(0.0, 0.3, 0.3));
vec3 Chroma = clr - Luma;
clr = (Chroma * 1.70) + Luma;
clr = vec3(filmc(clr.r), filmc(clr.g), filmc(clr.b)) / filmc(W);
#endif
return clr;
}


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

#if !defined(TEXEL_AA) || !defined(TEXEL_AA_FEATURE)
	vec4 diffuse = texture2D( TEXTURE_0, uv0 );
#else
	vec4 diffuse = texture2D_AA(TEXTURE_0, uv0 );
#endif

	vec4 inColor = color;

  float uY = uv1.y;
   vec3 shadowColor = vec3(0.965);
   vec3 Torch = vec3(0.75,0.21,0.0);

#ifdef SEASONS_FAR
	diffuse.a = 1.0;
	inColor.b = 1.0;
#endif

#ifdef ALPHA_TEST
	#ifdef ALPHA_TO_COVERAGE
		float alphaThreshold = .05;
	#else
		float alphaThreshold = .5;
	#endif
	if(diffuse.a < alphaThreshold)
		discard;
#endif
	
	diffuse = diffuse * texture2D( TEXTURE_1, uv1 );
 	
if((uY <= 0.8830)){
diffuse.rgb*= shadowColor;}
if((color.a <= 0.1)){
diffuse.rgb*= shadowColor;
diffuse.rgb*= 1.3;
diffuse.rgb*= color.g*3.1;}
if((uY <= 0.8820)){
diffuse.rgb*= shadowColor;}
if((uY <= 0.8810)){
diffuse.rgb*= shadowColor;}
if((uY <= 0.8800)){
diffuse.rgb*= shadowColor;}
if((uY <= 0.8790)){
diffuse.rgb*= shadowColor;}
if((uY <= 0.8780)){
diffuse.rgb*= shadowColor;}

diffuse.rgb += diffuse.rgb*Torch*uv1.x*0.79;

float c = color.r;
if(color.a<=0.1){ c = color.g*1.999; }
if(c<0.638){ diffuse.rgb *= 0.7; }

#ifndef SEASONS

#if !defined(ALPHA_TEST) && !defined(BLEND)
	diffuse.a = inColor.a;
#elif defined(BLEND)
	diffuse.a *= inColor.a;
	#ifdef NEAR_WATER
		float alphaFadeOut = clamp(cameraDist, 0.0, 1.0);
		diffuse.a = mix(diffuse.a, 1.0, alphaFadeOut);
	#endif
#endif	
	
	diffuse.rgb *= inColor.rgb;
#else
	vec2 uv = inColor.xy;
	diffuse.rgb *= mix(vec3(1.0,1.0,1.0), texture2D( TEXTURE_2, uv).rgb*2.0, inColor.b);
	diffuse.rgb *= inColor.aaa;
	diffuse.a = 1.0;
#endif

diffuse.rgb = Hyra(diffuse.rgb)*vec3(1.22);

#ifdef LOW_PRECISION
	diffuse.rgb = mix( diffuse.rgb, fogColor.rgb, fogColor.a ); 
diffuse.rgb = mix( diffuse.rgb, fogCoIor.rgb, moreFog );
#endif

	gl_FragColor = diffuse;

#endif // BYPASS_PIXEL_SHADER
}
