/* 
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 sunVecM22, sunVec22, sunVec, upVec;

//Uniforms//
uniform int isEyeInWater;
uniform int worldTime;

uniform float blindFactor;
uniform float darknessFactor;
uniform float rainStrength;
uniform float shadowFade;
uniform float timeAngle, timeBrightness;

uniform ivec2 eyeBrightnessSmooth;

uniform vec3 cameraPosition;

uniform sampler2D colortex0;
uniform sampler2D colortex1;

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

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

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

//Program//
void main() {
    vec4 color = texture2D(colortex0, texCoord.xy);
	
	vec3 vl = texture2DLod(colortex1, texCoord.xy, 1.5).rgb;
	vl *= vl;

	#ifdef END
	vl *= endCol.rgb * 1.2;
	vl *= LIGHT_SHAFT_STRENGTH_END * shadowFade * (1.0 - max(blindFactor, darknessFactor));
	#endif

	#ifdef OVERWORLD
	vl *= lightCol * 0.25;

		#ifdef WEATHER_PERBIOME
		if (isEyeInWater == 0.0) {
		vl *= LIGHT_SHAFT_STRENGTH * 7.0 * (1.0 - rainStrength * 0.975 * (1.0 - isMesa - isDesert)) * shadowFade * (1.0 - max(blindFactor, darknessFactor));
		} else {
		vl *= LIGHT_SHAFT_STRENGTH * 7.0 * (1.0 - rainStrength * 0.975) * shadowFade * (1.0 - max(blindFactor, darknessFactor));
		}
		#else
		vl *= LIGHT_SHAFT_STRENGTH * 7.0 * (1.0 - rainStrength * 0.975) * shadowFade * (1.0 - max(blindFactor, darknessFactor));
		#endif

	if (isEyeInWater == 1.0) {
	vl *= 4.0;
	}
	#endif

	if (isEyeInWater == 3.0) {
	vl *= 0.0;
	}

	color.rgb += vl;

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

#endif

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

//Varyings//
varying vec2 texCoord;

varying vec3 sunVecM22, sunVec22, sunVec, upVec;

//Uniforms//
uniform float timeAngle;

uniform mat4 gbufferModelView;

//Program//
void main() {
	texCoord = gl_MultiTexCoord0.xy;
	
	gl_Position = ftransform();

	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);
}

#endif