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

float GetLogarithmicDepth(float dist) {
	return (far * (dist - near)) / (dist * (far - near));
}

float GetLinearDepth2(float depth) {
    return 2.0 * near * far / (far + near - (2.0 * depth - 1.0) * (far - near));
}

vec4 DistortShadow(vec4 shadowpos, float distortFactor) {
	shadowpos.xy *= 1.0 / distortFactor;
	shadowpos.z = shadowpos.z * SHADOW_Z_DISTORTION;
	shadowpos = shadowpos * 0.5 + 0.5;

	return shadowpos;
}

vec4 GetWorldSpace(float shadowdepth, vec2 texCoord) {
	vec4 viewPos = gbufferProjectionInverse * (vec4(texCoord, shadowdepth, 1.0) * 2.0 - 1.0);
	viewPos /= viewPos.w;

	vec4 wpos = gbufferModelViewInverse * viewPos;
	wpos /= wpos.w;
	
	return wpos;
}

vec4 GetShadowSpace(vec4 wpos) {
	wpos = shadowModelView * wpos;
	wpos = shadowProjection * wpos;
	wpos /= wpos.w;
	
	float distb = sqrt(wpos.x * wpos.x + wpos.y * wpos.y);
	float distortFactor = 1.0 - shadowMapBias + distb * shadowMapBias;
	wpos = DistortShadow(wpos,distortFactor);
	
	return wpos;
}

//Light shafts from Robobo1221 (modified)
vec3 GetLightShafts(float pixeldepth0, float pixeldepth1, vec3 color, float dither) {
	vec3 vl = vec3(0.0);

	dither = TemporalDither(dither);

	vec3 screenPos = vec3(texCoord, pixeldepth0);
	vec4 viewPos = gbufferProjectionInverse * (vec4(screenPos, 1.0) * 2.0 - 1.0);
		 viewPos /= viewPos.w;
	
	vec3 lightVec = sunVec * ((timeAngle < 0.5325 || timeAngle > 0.9675) ? 1.0 : -1.0);
	float VoL = dot(normalize(viewPos.xyz), lightVec);

	#ifdef OVERWORLD
	float visfactor = mix(LIGHT_SHAFT_MORNING_FALLOFF, LIGHT_SHAFT_DAY_FALLOFF, timeBrightness);
		  visfactor = mix(LIGHT_SHAFT_NIGHT_FALLOFF, visfactor, sunVisibility);
		  visfactor*= mix(1.0, LIGHT_SHAFT_WEATHER_FALLOFF, rainStrength) * 0.1;
		  visfactor = min(visfactor, 0.999);

	float invvisfactor = 1.0 - visfactor;

	float visibility = clamp(VoL * 0.5 + 0.5, 0.0, 1.0);
		  visibility = visfactor / (1.0 - invvisfactor * visibility) - visfactor;
		  visibility = clamp(visibility * 1.015 / invvisfactor - 0.015, 0.0, 1.0);
		  visibility = mix(1.0, visibility, 0.03125 * eBS + 0.96875);
	#endif
	
	#ifdef END
	VoL = pow(VoL * 0.5 + 0.5, 24.0) * 0.85 + 0.15;
	float visibility = VoL * END_VL_SHARPNESS;
	#endif

	visibility *= VL_VISIBILITY_SCALE * float(pixeldepth0 > AO_HAND_DEPTH_THRESHOLD);

	if (visibility > 0.0) {
		float maxDist = VL_MAX_DISTANCE;
		
		float depth0 = GetLinearDepth2(pixeldepth0);
		float depth1 = GetLinearDepth2(pixeldepth1);
		vec4 worldposition = vec4(0.0);
		vec4 shadowposition = vec4(0.0);
		
		vec3 watercol = mix(vec3(1.0),
							waterColor.rgb / (waterColor.a * waterColor.a),
							pow(waterAlpha, 0.25));
		
		for(int i = 0; i < VL_SAMPLE_COUNT; i++) {
			float minDist = (i + dither) * VL_STEP_SIZE;
			if (minDist >= maxDist) break;

			if (depth1 < minDist || (depth0 < minDist && color == vec3(0.0))) {
				break;
			}

			worldposition = GetWorldSpace(GetLogarithmicDepth(minDist), texCoord.st);
			shadowposition = GetShadowSpace(worldposition);
			shadowposition.z += SHADOW_BIAS_VL / shadowMapResolution;

			if (length(shadowposition.xy * 2.0 - 1.0) < 1.0) {
				float shadow0 = clamp((texture2D(shadowtex0, shadowposition.xy).r - shadowposition.z) * SHADOW_COMPARISON_SCALE + 0.5, 0.0, 1.0);
				
				vec3 shadowCol = vec3(0.0);
				#ifdef SHADOW_COLOR
				if (shadow0 < 1.0) {
					float shadow1 = clamp((texture2D(shadowtex1, shadowposition.xy).r - shadowposition.z) * SHADOW_COMPARISON_SCALE + 0.5, 0.0, 1.0);
					if (shadow1 > 0.0) {
						shadowCol = texture2D(shadowcolor0, shadowposition.xy).rgb;
						shadowCol *= shadowCol * shadow1;
						#ifdef WATER_CAUSTICS
						shadowCol *= 16.0 - 15.0 * (1.0 - (1.0 - shadow0) * (1.0 - shadow0));
						#endif
					}
				}
				#endif

				shadow0 *= shadow0;
				shadowCol *= shadowCol;
				
				vec3 shadow = clamp(shadowCol * (1.0 - shadow0) + shadow0, vec3(0.0), vec3(16.0));

				if (depth0 < minDist) shadow *= color;

				#ifdef END
				vec3 npos = worldposition.xyz + cameraPosition.xyz + vec3(frametime * 2.0, 0, 0);
				float n3da = texture2D(noisetex, npos.xz / 384.0 + floor(npos.y / 2.0) * 0.35).r;
				float n3db = texture2D(noisetex, npos.xz / 384.0 + floor(npos.y / 2.0 + 1.0) * 0.35).r;
				float noise = mix(n3da, n3db, fract(npos.y / 2.0));
				noise = pow(sin(noise * 28.0 + frametime * 2.0) * 0.35 + 0.4, 1.5);
				shadow *= noise;
				#endif
				
				vl += shadow;
			}
		}
		
		vl *= clamp((cameraPosition.y + DEEP_UNDERGROUND_Y_NEW) / DEEP_UNDERGROUND_RANGE, 0.0, 1.0);

		#ifdef UNDERGROUND_SKY
		vl *= mix(clamp((cameraPosition.y - UNDERGROUND_SKY_START) / UNDERGROUND_SKY_RANGE, 0.0, 1.0), 1.0, eBS);
		#endif

		vl = sqrt(vl * visibility);
		if(dot(vl, vl) > 0.0) vl += (dither - 0.25) / VL_DITHER_SCALE;
	}
	
	#ifdef OVERWORLD
	if (isEyeInWater == 0) {
		// Enhanced time-of-day light shaft coloring
		// Golden hour: warm tint when sun is near horizon
		float sunHeight = clamp(timeBrightness, 0.0, 1.0);
		float goldenHour = pow(1.0 - sunHeight, 2.0) * sunVisibility;

		// Blue hour: cool tint during twilight (derived from low sun visibility)
		float blueHour = (1.0 - sunVisibility) * (1.0 - sunVisibility) * 0.5;

		vec3 shaftColor = lightCol;
		// Warm golden tint near sunrise/sunset
		shaftColor = mix(shaftColor, shaftColor * vec3(1.3, 1.05, 0.7), goldenHour * 0.6);
		// Cool blue tint during twilight
		shaftColor = mix(shaftColor, shaftColor * vec3(0.7, 0.8, 1.3), blueHour * 0.4);

		vl *= pow(shaftColor, vec3(1.0 - VoL * 0.25));
	} else {
		vl *= waterColorSqrt.rgb * 8.0;
	}
	#endif

	return vl;
}