//////////////////////////////////////////////////////////////////////////////
//    ##      #####   ######   ######     ##     ####     #######  ##  ##   //
//   ####    ##   ##  # ## #    ##  ##   ####     ##       ##   #  ##  ##   //
//  ##  ##   #          ##      ##  ##  ##  ##    ##       ## #     ####    //
//  ##  ##    #####     ##      #####   ##  ##    ##       ####      ##     //
//  ######        ##    ##      ## ##   ######    ##   #   ## #     ####    //
//  ##  ##   ##   ##    ##      ##  ##  ##  ##    ##  ##   ##   #  ##  ##   //
//  ##  ##    #####    ####    #### ##  ##  ##   #######  #######  ##  ##   //
//--------------------------------------------------------------------------//
//--------------------------------------------------------------------------//
//      #####   ##   ##    ##     #####    #######  ######    #####         //
//     ##   ##  ##   ##   ####     ## ##    ##   #   ##  ##  ##   ##        //
//     #        ##   ##  ##  ##    ##  ##   ## #     ##  ##  #              //
//      #####   #######  ##  ##    ##  ##   ####     #####    #####         //
//          ##  ##   ##  ######    ##  ##   ## #     ## ##        ##        //
//          ##  ##   ##  ######    ##  ##   ## #     ## ##        ##        //
//     ##   ##  ##   ##  ##  ##    ## ##    ##   #   ##  ##  ##   ##        //
//      #####   ##   ##  ##  ##   #####    #######  #### ##   #####         //
/////(BSL Shaders Edit)//////////////////////////////////////By LexBoosT//////

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

//Varyings//

varying vec2 texCoord;
varying vec3 sunVec, upVec;

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

//Uniforms//
uniform int isEyeInWater;
uniform int worldTime;
uniform float frameTimeCounter;
uniform float blindFactor;
uniform float rainStrengthS;
uniform float screenBrightness;
uniform float shadowFade;
uniform float timeAngle, timeBrightness, moonBrightness;
uniform float viewWidth, viewHeight;

uniform ivec2 eyeBrightnessSmooth;

uniform sampler2D colortex0;
uniform sampler2D colortex1;

uniform mat4 gbufferProjectionInverse;

//Optifine Constants//
const bool colortex1MipmapEnabled = true;

//Common Variables//
float eBS = eyeBrightnessSmooth.y / 240.0;
float eBS2 = clamp(pow(eBS,4.0),0.03,0.1);
float eBS3 = clamp((eyeBrightnessSmooth.y-220)/15.0,1.0,1.0);
float sunVisibility  = clamp(dot( sunVec,upVec) + 0.0625, 0.0, 0.125) * 8.0;
float moonVisibility = clamp(dot( -sunVec,upVec) + 0.0625, 0.0, 0.125) * 8.0;
float screenBrightness2 = clamp(screenBrightness, 0.0, 1.0);
float rainStrengthSp2 = rainStrengthS * rainStrengthS;
float lightShaftTime = pow(abs(sunVisibility - 0.5) * 2.0, 10.0);

//Includes//
#include "/lib/color/dimensionColor.glsl"

//Program//
void main(){
	vec4 color = texture2D(colortex0,texCoord.xy);

	#if defined (END) && defined LIGHT_SHAFT
	vec3 vl = texture2DLod(colortex1, texCoord.xy, 1.5).rgb;
	vl *= vl;
	#else
	float lod = 1.0;

	#ifndef MC_GL_RENDERER_GEFORCE
		if (fract(viewHeight / 2.0) > 0.25 || fract(viewWidth / 2.0) > 0.25) 
			lod = 0.0;
	#endif

	float offset = 1.0;
		vec3 vl1 = texture2DLod(colortex1, texCoord.xy + vec2( 0.0,  offset / viewHeight), lod).rgb;
		vec3 vl2 = texture2DLod(colortex1, texCoord.xy + vec2( 0.0, -offset / viewHeight), lod).rgb;
		vec3 vl3 = texture2DLod(colortex1, texCoord.xy + vec2( offset / viewWidth,   0.0), lod).rgb;
		vec3 vl4 = texture2DLod(colortex1, texCoord.xy + vec2(-offset / viewWidth,   0.0), lod).rgb;
		vec3 finalVl = (vl1 + vl2 + vl3 + vl4) * 0.25;
		vec3 vl = finalVl;

		vl *= vl;
	#endif

	#if defined (OVERWORLD) && defined LIGHT_SHAFT

	if (isEyeInWater == 0) {
		float lightShaftEndurance = LIGHTSHAFT_ENDURANCE;
		if ((lightShaftEndurance < 5.40 || lightShaftEndurance > 5.60) && rainStrengthS < 1.0) {
			vec4 screenPos = vec4(gl_FragCoord.xy / vec2(viewWidth, viewHeight), gl_FragCoord.z, 1.0);
			vec4 viewPos = gbufferProjectionInverse * (screenPos * 2.0 - 1.0);
			viewPos /= viewPos.w;
			vec3 nViewPos = normalize(viewPos.xyz);
			
			float NdotU = dot(nViewPos, upVec);
			NdotU = max(NdotU, 0.0);
			NdotU = 1.0 - NdotU;
			if (NdotU > 0.5) NdotU = smoothstep(0.0, 1.0, NdotU);
			
			#if DIRECTION_LIGHTSHAFT == 3
			#endif
			#if DIRECTION_LIGHTSHAFT == 2
			NdotU *= NdotU;
			#endif
			#if DIRECTION_LIGHTSHAFT == 1
			NdotU *= NdotU;
			NdotU *= NdotU;
			#endif
			#if DIRECTION_LIGHTSHAFT == 0
			NdotU *= NdotU;
			NdotU *= NdotU;
			NdotU *= NdotU;
			#endif
			
			NdotU = mix(NdotU, 1.0, rainStrengthSp2);

			vl *= pow2(NdotU);
		}
		
		#if defined OVERWORLD && defined LIGHT_SHAFT && defined SHINY_SUNRISE
		vec3 dayLightCol = lightCol2 * lightCol2 * lightCol2 ;
		#else
		vec3 dayLightCol = lightCol * lightCol * lightCol * lightCol;
		#endif
		
		vec3 nightLightCol =lightCol * 1.5;
		vl *= mix(nightLightCol, dayLightCol, sunVisibility);
		
		vl *= mix(1.0, LIGHT_SHAFT_NOON_MULTIPLIER * 0.75, timeBrightness*timeBrightness);
		vl *= mix(LIGHT_SHAFT_NIGHT_MULTIPLIER * 10.0 * (0.35 - moonBrightness * 0.15), 2.0, sunVisibility);
		vl *= mix(1.0, LIGHT_SHAFT_RAIN_MULTIPLIER * 0.5, rainStrengthSp2);


	} else vl *= length(lightCol) * 0.2 * LIGHT_SHAFT_UNDERWATER_MULTIPLIER  * (1.0 - rainStrengthS * 0.85);
	#endif

	
	#if defined (END) && defined LIGHT_SHAFT
	vl *= endCol3.rgb * 0.025;
	#endif
	
	#if defined (OVERWORLD) && defined LIGHT_SHAFT
	vl *= LIGHT_SHAFT_STRENGTH / 3.0 * (1.0 - rainStrengthS * eBS * 0.875) * shadowFade * (1 + isEyeInWater * 1.5 * eBS3) * (1.0 - blindFactor);
	#else
	vl *= LIGHT_SHAFT_STRENGTH_END * shadowFade * (1.0 - blindFactor);
	
	float vlFactor = (1.0 - min((timeBrightness)*2.0, 0.75));
	vlFactor = mix(vlFactor, 0.05, rainStrengthS);
	if (isEyeInWater == 1) vlFactor = 3.0;
	vl *= vlFactor * 1.15;
	#endif

	
	color.rgb += vl * lightShaftTime;
	
	
	/*DRAWBUFFERS:0*/
	gl_FragData[0]=color;
}

#endif

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

//Uniforms//
uniform float timeAngle;

uniform mat4 gbufferModelView;

//Program//
void main(){
	texCoord=gl_MultiTexCoord0.xy;
	
	gl_Position=ftransform();
	
	const vec2 sunRotationData=vec2(cos(sunPathRotation*.01745329251994),-sin(sunPathRotation*.01745329251994));
	float ang=fract(timeAngle-.25);
	ang=(ang+(cos(ang*3.14159265358979)*-.5+.5-ang)/3.)*6.28318530717959;
	sunVec=normalize((gbufferModelView*vec4(vec3(-sin(ang),cos(ang)*sunRotationData)*2000.,1.)).xyz);
	
	upVec=normalize(gbufferModelView[1].xyz);
}

#endif