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

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

//Varyings//
varying vec2 texCoord;

varying vec3 upVec, sunVec;

varying vec4 color;

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

//Uniforms//
uniform sampler2D noisetex;
uniform float nightVision;
uniform float rainStrengthS;
uniform float screenBrightness;
uniform float timeAngle, timeBrightness;
uniform float viewWidth, viewHeight;
uniform float eyeAltitude;
uniform int worldTime;

uniform ivec2 eyeBrightnessSmooth;

uniform mat4 gbufferProjectionInverse;

uniform vec3 skyColor;
uniform vec3 fogColor;

uniform sampler2D texture;
uniform sampler2D gaux1;

uniform float frameTimeCounter;

uniform vec3 cameraPosition;

uniform mat4 gbufferModelViewInverse;

#if MC_VERSION >= 11700 && defined OVERWORLD
uniform int renderStage;
#endif

//Common Variables//

float eBS = eyeBrightnessSmooth.y / 255.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);


//Common Functions//
float GetLuminance(vec3 color){
	return dot(color,vec3(0.299, 0.587, 0.114));
}

//Includes//
#include "/lib/util/dither.glsl"

#if (defined OVERWORLD || defined END) && defined CLOUD || defined END_CLOUDS || defined END_STARS
#include "/lib/color/lightColor.glsl"
#endif

#ifdef AURORA
#include "/lib/atmospherics/aurora.glsl"
#endif

#if (defined OVERWORLD || defined END)
#include "/lib/color/endColor.glsl"
#endif

#if defined END && defined CLOUDS
#include "/lib/color/skyColor.glsl"
#include "/lib/atmospherics/endclouds.glsl"
#include "/lib/atmospherics/sky.glsl"
#endif

#if defined END && defined END_STARS
#include "/lib/atmospherics/endstars.glsl"
#endif

#if defined END && defined SHOOTING_STARS_END
#include "/lib/atmospherics/shootingstars.glsl"
#endif

//Program//
void main(){

	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;

	float dither = Bayer128(gl_FragCoord.xy);

	#if defined OVERWORLD && !defined ROUND_SUN_MOON
	vec4 albedo = texture2D(texture, texCoord.xy);
	vec3 nViewPos = normalize(viewPos.xyz);
	float NdotU = dot(nViewPos, upVec);

		#if MC_VERSION >= 11700
				if (renderStage > 3)
		#endif

		albedo.a *= clamp((NdotU+0.02)*10, 0.0, 1.0);
		albedo *= color;
		albedo.rgb = pow(albedo.rgb, vec3(2.2 + sunVisibility * 2.2)) * (1.0 + sunVisibility * 4.0) * SKYBOX_BRIGHTNESS * albedo.a;
		#else
	vec4 albedo = vec4(0.0, 0.0, 0.0, 1.0);
	#endif

		#ifdef END
			albedo = vec4(0.0, 0.0, 0.0, 1.0);
		#endif

	#if defined CLOUDS && !defined END
		if (albedo.a > 0.0){
			float cloudAlpha = texture2D(gaux1, gl_FragCoord.xy / vec2(viewWidth, viewHeight)).r;
			float alphaMult = 1.0 - 0.6 * rainStrengthS;
			albedo.a *= 1.0 - cloudAlpha / (alphaMult * alphaMult);
		}
	#endif
	
	#ifdef END
	float cloudMask = 0.0;
	#ifdef CLOUDS 
	#ifdef END_CLOUDS
		if (1.0362 < length(color) && length(color) < 1.0363) {
		vec4 cloud = DrawEndCloud(viewPos.xyz, dither, endCol2.rgb);
		cloudMask = min((cloud.a / (CLOUD_OPACITY * 2.0)), 0.25) + (cloud.a / (CLOUD_OPACITY * 2.0)) * 0.5;
		albedo.rgb = mix(albedo.rgb, cloud.rgb, cloud.a);
		}
	#endif
	#endif
	#endif
	
	#if defined END && defined END_STARS
		albedo.rgb = mix(albedo.rgb,DrawEndStars(albedo.rgb, viewPos.xyz),1.0-cloudMask);
	#endif

	#ifdef END
		#ifdef ENDER_SKY_COLOR_CUSTOM
			albedo.rgb += endColCustom.rgb;
		#else
			albedo.rgb += endCol.rgb * 0.055;
		#endif
	#endif

	#if defined END && defined SHOOTING_STARS_END
		albedo.rgb = mix(albedo.rgb,DrawShootingStar(albedo.rgb, viewPos.xyz, 1.0,dither),1.0-cloudMask);
		#if NUM_SHOOTING_STARS >= 2
		albedo.rgb = mix(albedo.rgb,DrawShootingStar(albedo.rgb, viewPos.xyz, 1.2,dither),1.0-cloudMask);
		#if NUM_SHOOTING_STARS >= 3
		albedo.rgb = mix(albedo.rgb,DrawShootingStar(albedo.rgb, viewPos.xyz, 1.4,dither),1.0-cloudMask);
		#if NUM_SHOOTING_STARS >= 4
		albedo.rgb = mix(albedo.rgb,DrawShootingStar(albedo.rgb, viewPos.xyz, 1.6,dither),1.0-cloudMask);
		#endif
		#endif
		#endif
	#endif

	/* DRAWBUFFERS:0 */
	gl_FragData[0]=albedo;
}

#endif

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

//Uniforms//
uniform float timeAngle;
uniform float frameTimeCounter;
uniform int worldTime;
uniform mat4 gbufferModelView;

#if AA > 0
uniform int frameCounter;
uniform float viewWidth,viewHeight;
#endif


#include "/lib/util/moonrot.glsl"

//Includes//
#if AA > 0
#include "/lib/util/jitter.glsl"
#endif

//Program//
void main(){

	texCoord=(gl_TextureMatrix[0]*gl_MultiTexCoord0).xy;
	
	color=gl_Color;
	
	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;
	vec3 usunvec = vec3(-sin(ang),cos(ang)*sunRotationData);
	sunVec=normalize((gbufferModelView*vec4(usunvec*2000.,1.)).xyz);
	
	upVec=normalize(gbufferModelView[1].xyz);
	
	#if AA > 0
	gl_Position.xy=TAAJitter(gl_Position.xy,gl_Position.w);
	#endif
}

#endif