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

//Settings//

//#define DEBUG_SKYTEXTURED

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

#include "/settings/globalSettings.glsl"

//Varyings//

#if (defined END || (defined OVERWORLD && VANILLA_SKYBOX > 0))
varying vec2 texCoord;
varying vec3 sunVec, upVec;
varying vec4 color;
#endif

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

	//Uniforms//
	uniform sampler2D noisetex;
	uniform float nightVision;
	uniform float rainStrengthS;
	uniform float screenBrightness;
	uniform float viewWidth, viewHeight;

	#ifdef UNDERGROUND_SKY
	uniform float isEyeInCave;
	#endif

	uniform ivec2 eyeBrightnessSmooth;

	uniform mat4 gbufferProjectionInverse;

	uniform vec3 skyColor;
	uniform vec3 fogColor;

	uniform sampler2D texture;
	uniform sampler2D gaux1;

	uniform vec3 cameraPosition;

	uniform mat4 gbufferModelViewInverse;

	uniform int renderStage;

	#ifndef MC_RENDER_STAGE_SUN
	#define MC_RENDER_STAGE_SUN 1
	#endif

	#ifndef MC_RENDER_STAGE_MOON
	#define MC_RENDER_STAGE_MOON 1
	#endif

	//Common Variables//

	#if (defined OVERWORLD && VANILLA_SKYBOX > 0)
	float eBS              =eyeBrightnessSmooth.y / 240.0;
	float sunVisibility    =clamp00125(dot( sunVec,upVec) + 0.0625) * 8.0;
	float moonVisibility   =clamp00125(dot( -sunVec,upVec) + 0.0625) * 8.0;
	#endif

	float screenBrightness2  = clamp01(screenBrightness);

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

	//Includes//

	#if (defined OVERWORLD && VANILLA_SKYBOX > 0)
		#include "/lib/color/lightColor.glsl"
	#endif

	//Program//
	void main(){

		#if (defined OVERWORLD && VANILLA_SKYBOX > 0)
			vec4 albedo    =texture2D(texture, texCoord.xy);
			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);

				#ifdef HORIZON_SUN_MOON

				float NdotU = dot(nViewPos, upVec);

					#if MC_VERSION >= 11700
						if (renderStage > 3)
					#endif
					albedo.a *= clamp01((NdotU + 0.02) * 10.0);
				#endif

				albedo *= color;

				albedo.rgb = pow(albedo.rgb, vec3(2.2 + sunVisibility * 2.2)) * (1.0 + sunVisibility * 4.0) * SKYBOX_BRIGHTNESS * albedo.a;

				#if REALISTIC_CLOUDS == 1
					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

				#if ROUND_SUN_MOON > 0
					if (renderStage == MC_RENDER_STAGE_SUN || renderStage == MC_RENDER_STAGE_MOON) {
						albedo *= 0.0;
					}
				#endif

				#ifdef UNDERGROUND_SKY
					albedo.rgb *= 1.0 - isEyeInCave;
				#endif
		#else

			vec4 albedo = vec4(0.0);

		#endif

		/*DRAWBUFFERS:0*/

		#ifdef DEBUG_SKYTEXTURED
            gl_FragData[0]=vec4(0.0667, 0.0, 1.0, 0.75);
		#else
			gl_FragData[0]=clamp01(albedo);
		#endif
	}

#endif

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

	//Uniforms//
	#if (defined OVERWORLD && VANILLA_SKYBOX > 0)
		#if AA > 1
			uniform int frameCounter;
			uniform float viewWidth, viewHeight;
			#include "/lib/util/jitter.glsl"
		#endif
	#endif

	//Program//
	void main(){
		#if (defined END || (defined OVERWORLD && VANILLA_SKYBOX > 0))

			texCoord=(gl_TextureMatrix[0]*gl_MultiTexCoord0).xy;

			color=gl_Color;

			gl_Position=ftransform();

		#endif

		#if (defined OVERWORLD && VANILLA_SKYBOX > 0)
			upVec = normalize(gbufferModelView[1].xyz);
			vec2 sunRotationData = getSunRotationData();
			vec3 usunvec = getUsunvec(sunRotationData);
			sunVec = getSunVec(usunvec, sunRotationData);

			#if AA > 1
				gl_Position.xy=TAAJitter(gl_Position.xy, gl_Position.w);
			#endif

		#else
				#if !defined END
					vec4 color = vec4(0.0);
					gl_Position = color;
				#endif
		#endif
	}

#endif