/* 
OPAL Shaders by FENVEN
https://www.youtube.com/@minecraftfenven567
based on
BSL Shaders by Capt Tatsu 
https://bitslablab.com 
*/ 

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

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

//Varyings//
varying vec2 texCoord;

varying vec3 upVec, sunVec, sunVec22, sunVecM22;

varying vec4 color;

//Uniforms//
uniform int isEyeInWater;

uniform float nightVision;
uniform float rainStrength;
uniform float timeAngle, timeBrightness;
uniform float viewWidth, viewHeight;

uniform ivec2 eyeBrightnessSmooth;

uniform mat4 gbufferProjectionInverse;

uniform sampler2D texture;
uniform sampler2D gaux1;

//Common Variables//
float eBS = clamp(4.0 * eyeBrightnessSmooth.y / 240.0, 0.0, 1.0);
float sunVisibility  = clamp((dot( sunVec, upVec) + 0.05) * 10.0, 0.0, 1.0);
float moonVisibility = clamp((dot(-sunVec, upVec) + 0.05) * 10.0, 0.0, 1.0);
float sunVisibility22  = clamp((dot( sunVec22, upVec) + 0.05) * 10.0, 0.0, 1.0);
float moonVisibility22 = clamp((dot(-sunVec22, upVec) + 0.05) * 10.0, 0.0, 1.0);

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

//Includes//
#ifdef OVERWORLD
#include "/lib/color/lightColor.glsl"
#endif

//Program//
void main() {
	vec4 albedo = texture2D(texture, texCoord);

//add that to remove skybox
//	albedo.a = 0.0;

	#ifdef OVERWORLD
	albedo *= color;

	#ifdef SKYBOX_COLOR_TRANSITION
	albedo.rgb = pow(albedo.rgb,vec3(2.2)) * SKYBOX_BRIGHTNESS
	 * (1.0 + 4.5 * clamp((dot(sunVec22, upVec) + 0.04) * 8.0, 0.0, 1.0))		//makes sun brighter during the day
	 * albedo.a;
	#else
	albedo.rgb = pow(albedo.rgb,vec3(2.2)) * SKYBOX_BRIGHTNESS * albedo.a;
	#endif

//	#ifdef SKYBOX_COLOR_TRANSITION || HIDE_SUN_&_MOON
	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 VoU = clamp(dot(normalize(vec3(viewPos)), normalize(upVec)), 0.0, 1.0);
//	#endif

	//hides sun and moon under the horizon
	albedo *= sqrt(VoU);

	#ifdef SKYBOX_COLOR_TRANSITION
	albedo.rgb *= vec3(1.0, 1.0, 1.0) + vec3(2.0, -0.8, -0.9) * (1.0 - sqrt(clamp(VoU * 4.0, 0.0, 1.0)));	//slightly better red color
	albedo.rgb *= (0.4 + 0.6 * sqrt(sqrt(VoU)));
	#endif

	if (isEyeInWater == 1) albedo.rgb *= (1.0 + 2.0 * sunVisibility);

	#if CLOUDS == 1
	if (albedo.a > 0.0) {
		float cloudAlpha = texture2D(gaux1, gl_FragCoord.xy / vec2(viewWidth, viewHeight)).r;
		float alphaMult = 1.0 - 0.6 * rainStrength;
		albedo.a *= 1.0 - cloudAlpha / (alphaMult * alphaMult);
	}
	#endif

	#if CLOUDS == 2
	if (albedo.a > 0.0) {
		float cloudAlpha = texture2D(gaux1, gl_FragCoord.xy / vec2(viewWidth, viewHeight)).r;
		float alphaMult = 1.0 - 0.6 * rainStrength;
		albedo.a *= 1.0 - cloudAlpha / (alphaMult * alphaMult);
	}
	#endif
	
	#ifdef SKY_DESATURATION
	vec3 desat = GetLuminance(albedo.rgb) * pow(lightNight,vec3(1.6)) * 3.0;	//2.0 too desaturated ; 4.0 too blue ; 3.0 perfect
    	vec3 desatDay = GetLuminance(albedo.rgb) * pow(lightDay,vec3(1.6)) * 1.0;
	albedo.rgb = mix(desat, albedo.rgb, 1.0 - 0.6 * clamp((dot(-sunVec22, upVec) + 0.05) * 2.0, 0.0, 1.0));		//decrease value after "-" to increase mnoon brightness ; no higher than 6.0
	albedo.rgb = mix(desatDay, albedo.rgb, 1.0 - 0.6 * clamp((dot(sunVecM22, upVec) + 0.04) * 8.0, 0.0, 1.0));
	#endif
	#endif


	#ifdef END
	albedo.rgb = pow(albedo.rgb,vec3(2.2)) * SKYBOX_BRIGHTNESS_END * 0.0075;


	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 VoU = clamp(dot(normalize(vec3(viewPos)), normalize(upVec)), 0.0, 1.0);
	float VoL = clamp(dot(normalize(vec3(viewPos)), normalize(sunVec)), 0.0, 1.0);

	//Skybox Gradient
	albedo.rgb *= (1.0 + pow(VoL, 3.0));
	#endif
	
    /* DRAWBUFFERS:0 */
	gl_FragData[0] = albedo;
}

#endif

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

//Varyings//
varying vec2 texCoord;

varying vec3 sunVecM22, sunVec22, sunVec, upVec;

varying vec4 color;

//Uniforms//
uniform float timeAngle;

uniform mat4 gbufferModelView;

#ifdef TAA
uniform int frameCounter;

uniform float viewWidth;
uniform float viewHeight;
#include "/lib/util/jitter.glsl"
#endif

//Program//
void main() {
	texCoord = (gl_TextureMatrix[0] * gl_MultiTexCoord0).xy;

	color = gl_Color;
	
	const vec2 sunRotationData = vec2(cos(sunPathRotation * 0.01745329251994), -sin(sunPathRotation * 0.01745329251994));
	float ang = fract(timeAngle - 0.25);
	ang = (ang + (cos(ang * 3.14159265358979) * -0.5 + 0.5 - ang) / 3.0) * 6.28318530717959;
	sunVec = normalize((gbufferModelView * vec4(vec3(-sin(ang), cos(ang) * sunRotationData) * 2000.0, 1.0)).xyz);
	sunVec22 = normalize((gbufferModelView * vec4(vec3(-sin(ang), cos(ang) * sunRotationData + 0.22) * 2000.0, 1.0)).xyz);
	sunVecM22 = normalize((gbufferModelView * vec4(vec3(-sin(ang), cos(ang) * sunRotationData - 0.22) * 2000.0, 1.0)).xyz);

	upVec = normalize(gbufferModelView[1].xyz);
	
	gl_Position = ftransform();
	
	#ifdef TAA
	gl_Position.xy = TAAJitter(gl_Position.xy, gl_Position.w);
	#endif
}

#endif