/*
BSLex (BSL Edit) by LexBoosT
*/

//Settings//
#include "/lib/settings.glsl"

//Fragment Shader///////////////////////////////////////////////////////////////////////////////////
#ifdef FSH

//Varyings//
varying float mat;

varying vec2 texCoord;

varying vec4 color;

//Uniforms//
uniform int blockEntityId;

uniform sampler2D tex;

//Program//
void main(){
	#if MC_VERSION>=11300
	if(blockEntityId==10250)discard;
	#endif
	
	vec4 albedo=texture2D(tex,texCoord.xy);
	albedo.rgb*=color.rgb;
	
	float premult=float(mat>.98&&mat<1.02);
	float ice=float(mat>1.48&&mat<1.52);
	float water=float(mat>1.98&&mat<2.02);
	if(water>.5||albedo.a<.01)discard;
	
	#ifdef SHADOW_COLOR
	albedo.rgb=mix(vec3(1.),albedo.rgb,1.-pow(1.-albedo.a,3.));
	albedo.rgb*=1.-pow(albedo.a,64.);
	#else
	if((premult>.5&&albedo.a<.98))albedo.a=0.;
	#endif
	
	gl_FragData[0] = clamp(albedo, vec4(0.0), vec4(1.0));
}

#endif

//Vertex Shader/////////////////////////////////////////////////////////////////////////////////////
#ifdef VSH

//Varyings//
varying float mat;

varying vec2 texCoord,lmCoord;

#ifdef SHADOW_BACKFACE_CULLING
varying vec3 normal;
#endif

varying vec4 color;
varying vec4 position;

//Uniforms//
uniform int worldTime;

uniform float frameTimeCounter;

uniform vec3 cameraPosition;

uniform mat4 gbufferModelView,gbufferModelViewInverse;
uniform mat4 shadowProjection,shadowProjectionInverse;
uniform mat4 shadowModelView,shadowModelViewInverse;

//Attributes//
attribute vec4 mc_Entity;
attribute vec4 mc_midTexCoord;

//Common Variables//
#ifdef WORLD_TIME_ANIMATION
float frametime=float(worldTime)*.05*ANIMATION_SPEED;
#else
float frametime=frameTimeCounter*ANIMATION_SPEED;
#endif

//Includes//
#include "/lib/vertex/waving.glsl"

#ifdef WORLD_CURVATURE
#include "/lib/vertex/worldCurvature.glsl"
#endif

//Program//
void main(){
	texCoord=gl_MultiTexCoord0.xy;
	color=gl_Color;
	
	lmCoord=(gl_TextureMatrix[1]*gl_MultiTexCoord1).xy;
	lmCoord=clamp((lmCoord-.03125)*1.06667,0.,1.);
	
	mat=0;
	if(mc_Entity.x==10301)mat=1.5;
	if(mc_Entity.x==10300||mc_Entity.x==10249||mc_Entity.x==10252)mat=2;
	if(mc_Entity.x==10100||mc_Entity.x==10105||mc_Entity.x==10102||mc_Entity.x==10103||mc_Entity.x==10104)mat=3;
	
	vec4 position=shadowModelViewInverse*shadowProjectionInverse*ftransform();
	
	float istopv=gl_MultiTexCoord0.t<mc_midTexCoord.t?1.:0.;
	float skylight=(gl_TextureMatrix[1]*gl_MultiTexCoord1).y;
	skylight=clamp((skylight-.03125)*1.06667,0.,1.);
	position.xyz=WavingBlocks(position.xyz,istopv,skylight);
	
	#ifdef WORLD_CURVATURE
	position.y-=WorldCurvature(position.xz);
	#endif
	
	#ifdef MOUVEMENT_CAM
	position+=vec4(.03*sin(frameTimeCounter*3.),.015*cos(frameTimeCounter*4.),0.,0.)*gbufferModelView;
	#endif
	
	gl_Position=shadowProjection*shadowModelView*position;
	
	#ifdef SHADOW_BACKFACE_CULLING
	normal=gl_NormalMatrix*gl_Normal;
	if(normal.z<0.){
		gl_Position=vec4(-1.);
		return;
	}
	#endif
	
	float dist=sqrt(gl_Position.x*gl_Position.x+gl_Position.y*gl_Position.y);
	float distortFactor=dist*shadowMapBias+(1.-shadowMapBias);
	
	gl_Position.xy*=1./distortFactor;
	gl_Position.z=gl_Position.z*.2;
}

#endif