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

#include "fragmentVersionCentroid.h"

#if __VERSION__ >= 300
	#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
#else
	#ifndef BYPASS_PIXEL_SHADER
		varying vec2 uv0;
		varying vec2 uv1;
	#endif
#endif

varying vec4 color;
varying vec3 chunkPos;

#ifdef FOG
varying vec4 fogColor;
#endif

#include "uniformShaderConstants.h"
#include "util.h"
#include "settings.h"

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

#if USE_ALPHA_TEST
	#ifdef ALPHA_TO_COVERAGE
	#define ALPHA_THRESHOLD 0.05
	#else
	#define ALPHA_THRESHOLD 0.5
	#endif
	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

#ifndef SEASONS
	#if !USE_ALPHA_TEST && !defined(BLEND)
		diffuse.a = inColor.a;
	#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

vec3 cp = fract (chunkPos.xyz);

	/*Main Chunks*/
	if      ((chunkPos.x < 0.0625 || chunkPos.x > 15.9375) && (chunkPos.z < 0.0625 || chunkPos.z > 15.9375)){
		diffuse.rgb *= vec3(0.0, 255.0, 0.0);

	}else if((chunkPos.x < 0.0625 || chunkPos.x > 15.9375) && (chunkPos.y < 0.0625 || chunkPos.y > 15.9375)){
		diffuse.rgb *= vec3(255.0, 0.0, 0.0);

	}else if((chunkPos.y < 0.0625 || chunkPos.y > 15.9375) && (chunkPos.z < 0.0625 || chunkPos.z > 15.9375)){
		diffuse.rgb *= vec3(0.0, 0.0, 255.0);

	}
	
	/*Block Drawer*/
	else if (
		((cp.x < 0.02 || cp.x > 0.98) && (cp.y < 0.02 || cp.y > 0.98)) ||
		((cp.x < 0.02 || cp.x > 0.98) && (cp.z < 0.02 || cp.z > 0.98)) ||
		((cp.y < 0.02 || cp.y > 0.98) && (cp.z < 0.02 || cp.z > 0.98))
	){
		if      ((chunkPos.y < 0.02 || chunkPos.y > 15.98) || (chunkPos.y < 0.02 || chunkPos.y > 15.98)){
			diffuse.rgb *= vec3(0.8, 0.8, 0.8);

		}else if((chunkPos.x < 0.02 || chunkPos.x > 15.98) || (chunkPos.x < 0.02 || chunkPos.x > 15.98)){
			diffuse.rgb *= vec3(0.8, 0.8, 0.8);

		}else if((chunkPos.z < 0.02 || chunkPos.z > 15.98) || (chunkPos.z < 0.02 || chunkPos.z > 15.98)){
			diffuse.rgb *= vec3(0.8, 0.8, 0.8);
		}
	}

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

	gl_FragColor = diffuse;
	
#endif // BYPASS_PIXEL_SHADER
}
