From fe459005c950cbeb7dc1b742610655b54992f009 Mon Sep 17 00:00:00 2001
From: Xonk <XonkDev@gmail.com>
Date: Sun, 23 Jun 2024 21:43:15 -0400
Subject: [PATCH] fix blocklight and ambient light interactions with
 rtao/ssgi/gtao. fix atmosphere haze self absorbance.  fix cloud lighting
 during nausea.  fix the issue were floodfill handheld lights cannot be
 toggled off.  fix hardocded emission writing to the wrong buffer. fix
 lighting on translucents/particles in the end/nether

---
 shaders/dimensions/DH_solid.fsh            |   4 +-
 shaders/dimensions/all_particles.fsh       |  12 ++-
 shaders/dimensions/all_solid.fsh           |   9 +-
 shaders/dimensions/all_translucent.fsh     |  23 ++---
 shaders/dimensions/composite.fsh           |   2 +-
 shaders/dimensions/composite.vsh           |   4 +
 shaders/dimensions/composite1.fsh          | 108 +++++++++++----------
 shaders/dimensions/composite2.fsh          |   2 +
 shaders/dimensions/composite3.fsh          |   2 +-
 shaders/dimensions/deferred.vsh            |   2 +-
 shaders/lib/diffuse_lighting.glsl          |  98 ++++++-------------
 shaders/lib/indirect_lighting_effects.glsl |  49 ++++------
 shaders/lib/overworld_fog.glsl             |  18 +++-
 shaders/lib/settings.glsl                  |   2 +-
 shaders/lib/volumetricClouds.glsl          |   2 +-
 shaders/shaders.properties                 |   4 +-
 16 files changed, 153 insertions(+), 188 deletions(-)

diff --git a/shaders/dimensions/DH_solid.fsh b/shaders/dimensions/DH_solid.fsh
index afd0197..ad1136c 100644
--- a/shaders/dimensions/DH_solid.fsh
+++ b/shaders/dimensions/DH_solid.fsh
@@ -113,14 +113,14 @@ void main() {
         }
     #endif
 
-    vec3 normals = viewToWorld(normals_and_materials.xyz);
+    vec3 normals = (normals_and_materials.xyz);
     float materials = normals_and_materials.a;
 	vec2 PackLightmaps = lightmapCoords;
 
     // PackLightmaps.y *= 1.05;
     PackLightmaps = min(max(PackLightmaps,0.0)*1.05,1.0);
     
-    vec4 data1 = clamp( encode(normals.xyz, PackLightmaps), 0.0, 1.0);
+    vec4 data1 = clamp( encode(normals, PackLightmaps), 0.0, 1.0);
     
     // alpha is material masks, set it to 0.65 to make a DH LODs mask. 
     vec4 Albedo = vec4(gcolor.rgb, 1.0);
diff --git a/shaders/dimensions/all_particles.fsh b/shaders/dimensions/all_particles.fsh
index 07720b9..eb05a66 100644
--- a/shaders/dimensions/all_particles.fsh
+++ b/shaders/dimensions/all_particles.fsh
@@ -425,32 +425,34 @@ void main() {
 			#endif
 
 			AmbientLightColor = averageSkyCol_Clouds / 30.0;
+
 			#ifdef IS_IRIS
 				AmbientLightColor *= 2.5;
 			#else
 				AmbientLightColor *= 0.5;
 			#endif
-
+			
+			Indirect_lighting = doIndirectLighting(AmbientLightColor, MinimumLightColor, lightmap.y);
 		#endif
 		
 		#ifdef NETHER_SHADER
-			AmbientLightColor = skyCloudsFromTexLOD2(vec3(0.0,1.0,0.0), colortex4, 6).rgb / 30.0;
+			Indirect_lighting = skyCloudsFromTexLOD2(vec3(0.0,1.0,0.0), colortex4, 6).rgb / 30.0;
 		#endif
 
 		#ifdef END_SHADER
-			AmbientLightColor = vec3(0.3,0.6,1.0) * 0.5;
+			Indirect_lighting = vec3(0.3,0.6,1.0) * 0.5;
 		#endif
 
+	///////////////////////// BLOCKLIGHT LIGHTING OR LPV LIGHTING OR FLOODFILL COLORED LIGHTING
 		#ifdef IS_LPV_ENABLED
 			vec3 lpvPos = GetLpvPosition(feetPlayerPos);
 		#else
 			const vec3 lpvPos = vec3(0.0);
 		#endif
 
-		Indirect_lighting = DoAmbientLightColor(feetPlayerPos, lpvPos, AmbientLightColor, MinimumLightColor, Torch_Color, clamp(lightmap.xy,0,1), exposure);
+		Indirect_lighting += doBlockLightLighting( vec3(TORCH_R,TORCH_G,TORCH_B), lightmap.x, exposure, feetPlayerPos, lpvPos);
 
 		#ifdef LINES
-
 			gl_FragData[0].rgb = (Indirect_lighting + Direct_lighting) * toLinear(color.rgb);
 
 			if(SELECTION_BOX > 0) gl_FragData[0].rgba = vec4(toLinear(vec3(SELECT_BOX_COL_R, SELECT_BOX_COL_G, SELECT_BOX_COL_B)), 1.0);
diff --git a/shaders/dimensions/all_solid.fsh b/shaders/dimensions/all_solid.fsh
index a86d147..832f8cb 100644
--- a/shaders/dimensions/all_solid.fsh
+++ b/shaders/dimensions/all_solid.fsh
@@ -503,7 +503,7 @@ void main() {
 
 		#if EMISSIVE_TYPE == 2
 			gl_FragData[1].a = SpecularTex.a;
-			if(SpecularTex.a <= 0.0) gl_FragData[2].a = EMISSIVE;
+			if(SpecularTex.a <= 0.0) gl_FragData[1].a = EMISSIVE;
 		#endif
 
 		#if EMISSIVE_TYPE == 3		
@@ -526,13 +526,6 @@ void main() {
 		#if SSS_TYPE == 3		
 			gl_FragData[1].b = SpecularTex.b;
 		#endif
-		
-		// #ifndef ENTITIES
-		// 	if(PORTAL > 0){
-		// 		gl_FragData[2].rgb = vec3(0);
-		// 		gl_FragData[2].a = clamp(ENDPORTAL_EFFECT * 0.9, 0,0.9);
-		// 	}
-		// #endif
 	#endif
 
 	// hit glow effect...
diff --git a/shaders/dimensions/all_translucent.fsh b/shaders/dimensions/all_translucent.fsh
index e5ff096..d8a8edb 100644
--- a/shaders/dimensions/all_translucent.fsh
+++ b/shaders/dimensions/all_translucent.fsh
@@ -217,7 +217,7 @@ vec3 viewToWorld(vec3 viewPos) {
     vec4 pos;
     pos.xyz = viewPos;
     pos.w = 0.0;
-    pos = gbufferModelViewInverse * pos;
+    pos = gbufferModelViewInverse * pos ;
     return pos.xyz;
 }
 
@@ -629,19 +629,12 @@ if (gl_FragCoord.x * texelSize.x < 1.0  && gl_FragCoord.y * texelSize.y < 1.0 )
 		
 		float skylight = max(pow(viewToWorld(flatnormal).y*0.5+0.5,0.1) + SkylightDir, 0.2);
 		AmbientLightColor *= skylight;
+		
+		Indirect_lighting = doIndirectLighting(AmbientLightColor, MinimumLightColor, lightmap.y);
 	#endif
 
 	#ifdef NETHER_SHADER
-		// vec3 AmbientLightColor = skyCloudsFromTexLOD2(worldSpaceNormal, colortex4, 6).rgb / 15.0;
-		
-		// vec3 up 	= skyCloudsFromTexLOD2(vec3( 0, 1, 0), colortex4, 6).rgb/ 30.0;
-		// vec3 down 	= skyCloudsFromTexLOD2(vec3( 0,-1, 0), colortex4, 6).rgb/ 30.0;
-
-		// up   *= pow( max( worldSpaceNormal.y, 0), 2);
-		// down *= pow( max(-worldSpaceNormal.y, 0), 2);
-		// AmbientLightColor += up + down;
-		
-		vec3 AmbientLightColor = vec3(0.1);
+		Indirect_lighting = skyCloudsFromTexLOD2(worldSpaceNormal, colortex4, 6).rgb / 30.0 ;
 	#endif
 
 	#ifdef END_SHADER
@@ -658,14 +651,12 @@ if (gl_FragCoord.x * texelSize.x < 1.0  && gl_FragCoord.y * texelSize.y < 1.0 )
 
 		Direct_lighting = lightColors * endFogPhase(lightPos) * NdotL;
 
-		// vec3 AmbientLightColor = vec3(0.5,0.75,1.0) * 0.9 + 0.1;
-		// AmbientLightColor *= clamp(1.5 + dot(worldSpaceNormal, normalize(feetPlayerPos))*0.5,0,2);
-
 		vec3 AmbientLightColor = vec3(0.3,0.6,1.0) * 0.5;
 			
-		AmbientLightColor = AmbientLightColor + 0.7 * AmbientLightColor * dot(worldSpaceNormal, normalize(feetPlayerPos));
+		Indirect_lighting = AmbientLightColor + 0.7 * AmbientLightColor * dot(worldSpaceNormal, normalize(feetPlayerPos));
 	#endif
 
+	///////////////////////// BLOCKLIGHT LIGHTING OR LPV LIGHTING OR FLOODFILL COLORED LIGHTING
 	#ifdef IS_LPV_ENABLED
 		vec3 normalOffset = vec3(0.0);
 
@@ -684,7 +675,7 @@ if (gl_FragCoord.x * texelSize.x < 1.0  && gl_FragCoord.y * texelSize.y < 1.0 )
 		const vec3 lpvPos = vec3(0.0);
 	#endif
 
-	Indirect_lighting = DoAmbientLightColor(feetPlayerPos, lpvPos, AmbientLightColor, MinimumLightColor, vec3(TORCH_R,TORCH_G,TORCH_B), lightmap.xy, exposure);
+	Indirect_lighting += doBlockLightLighting( vec3(TORCH_R,TORCH_G,TORCH_B), lightmap.x, exposure, feetPlayerPos, lpvPos);
 	
 	vec3 FinalColor = (Indirect_lighting + Direct_lighting) * Albedo;
 
diff --git a/shaders/dimensions/composite.fsh b/shaders/dimensions/composite.fsh
index cb67a0e..ebe5d79 100644
--- a/shaders/dimensions/composite.fsh
+++ b/shaders/dimensions/composite.fsh
@@ -440,7 +440,7 @@ void main() {
 		vec3 FlatNormals = texture2D(colortex15,texcoord).rgb * 2.0 - 1.0;
 		
 		if(z >= 1.0){
-			FlatNormals = worldToView(normal);
+			FlatNormals = normal;
 		}
 
 		vec2 SSAO_SSS = SSAO(viewPos, FlatNormals, hand, isLeaf, noise);
diff --git a/shaders/dimensions/composite.vsh b/shaders/dimensions/composite.vsh
index 1555ac9..772c571 100644
--- a/shaders/dimensions/composite.vsh
+++ b/shaders/dimensions/composite.vsh
@@ -6,9 +6,13 @@ flat varying vec3 WsunVec;
 uniform sampler2D colortex4;
 
 uniform int frameCounter;
+
 uniform float sunElevation;
 uniform vec3 sunPosition;
 uniform mat4 gbufferModelViewInverse;
+
+
+
 #include "/lib/util.glsl"
 #include "/lib/res_params.glsl"
 
diff --git a/shaders/dimensions/composite1.fsh b/shaders/dimensions/composite1.fsh
index 7e80549..d16dfde 100644
--- a/shaders/dimensions/composite1.fsh
+++ b/shaders/dimensions/composite1.fsh
@@ -809,8 +809,9 @@ void main() {
 		normalAndAO.a = clamp(pow(normalAndAO.a*5,4),0,1);
 
 		if(isDHrange){
+			FlatNormals = normal;
+			normal = viewToWorld(normal);
 			slopednormal = normal;
-			FlatNormals = worldToView(normal);
 		}
 
 
@@ -899,6 +900,8 @@ void main() {
 			bool inShadowmapBounds = false;
 		#endif
 
+		MinimumLightColor = MinimumLightColor + 0.7 * MinimumLightColor * dot(slopednormal, feetPlayerPos_normalized);
+
 	////////////////////////////////////////////////////////////////////////////////////////////
 	///////////////////////////////////	    FILTER STUFF      //////////////////////////////////
 	////////////////////////////////////////////////////////////////////////////////////////////
@@ -1023,7 +1026,7 @@ void main() {
 		Shadows = ComputeShadowMap(projectedShadowPosition, distortFactor, noise_2, filteredShadow.x, flatNormNdotL, shadowMapFalloff, DirectLightColor, ShadowAlpha, LabSSS > 0.0);
 
 		// transition to fallback lightmap shadow mask.
-		Shadows = mix(isWater ? 1.0 : LM_shadowMapFallback, Shadows, shadowMapFalloff);
+		Shadows = mix(isWater ? lightLeakFix : LM_shadowMapFallback, Shadows, shadowMapFalloff);
 
 		#ifdef OLD_LIGHTLEAK_FIX
 			if (isEyeInWater == 0) Shadows *= lightLeakFix; // light leak fix
@@ -1080,29 +1083,29 @@ void main() {
 	/////////////////////////////	INDIRECT LIGHTING 	/////////////////////////////
 	/////////////////////////////////////////////////////////////////////////////////
 
-		#if defined OVERWORLD_SHADER && (indirect_effect == 0 || indirect_effect == 1)
+		#if defined OVERWORLD_SHADER 
 			Indirect_lighting = AmbientLightColor;
-			
-			float allDirections = dot(abs(slopednormal),vec3(1.0));
-			vec3 ambientcoefs = slopednormal / allDirections;
-			float SkylightDir = ambientcoefs.y*1.5;
-			
-			if(isGrass) SkylightDir = 1.5;
 
-			float skylight = max(pow(viewToWorld(FlatNormals).y*0.5+0.5,0.1) + SkylightDir, 0.2 + (1-lightmap.y)*0.8*0) ;
+			float skylight = 1.0;
+			#if indirect_effect != 2
+				float SkylightDir = (slopednormal / dot(abs(slopednormal),vec3(1.0))).y*1.5;
+				if(isGrass) SkylightDir = 1.5;
 
-			#if indirect_effect == 1
-				skylight =  min(skylight, mix(0.95, 2.5, pow(1-pow(1-SSAO_SSS.x, 0.5),2.0)	));
+				skylight = max(pow(viewToWorld(FlatNormals).y*0.5+0.5,0.1) + SkylightDir, 0.2 + (1-lightmap.y)*0.8);
+
+				#if indirect_effect == 1
+					skylight =  min(skylight, mix(0.95, 2.5, pow(1-pow(1-SSAO_SSS.x, 0.5),2.0)	));
+				#endif
 			#endif
-
-			Indirect_lighting *= skylight;
+			
+			Indirect_lighting = doIndirectLighting(AmbientLightColor * skylight, MinimumLightColor, lightmap.y);
 		#endif
 
 		#ifdef NETHER_SHADER
 			Indirect_lighting = skyCloudsFromTexLOD2(normal, colortex4, 6).rgb / 30.0;
 			vec3 up = skyCloudsFromTexLOD2(vec3(0.0,1.0,0.0), colortex4, 6).rgb / 30.0;
 			
-			Indirect_lighting =  mix(up, Indirect_lighting,  clamp(pow(1.0-pow(1.0-SSAO_SSS.x, 0.5),2.0),0.0,1.0));
+			Indirect_lighting = mix(up, Indirect_lighting,  clamp(pow(1.0-pow(1.0-SSAO_SSS.x, 0.5),2.0),0.0,1.0));
 
 			AmbientLightColor = Indirect_lighting / 5.0;
 		#endif
@@ -1112,28 +1115,7 @@ void main() {
 			
 			Indirect_lighting = Indirect_lighting + 0.7*mix(-Indirect_lighting, Indirect_lighting * dot(slopednormal, feetPlayerPos_normalized), clamp(pow(1.0-pow(1.0-SSAO_SSS.x, 0.5),2.0),0.0,1.0));
 		#endif
-	
-		#ifdef IS_LPV_ENABLED
-			vec3 normalOffset = vec3(0.0);
 
-			if (any(greaterThan(abs(FlatNormals), vec3(1.0e-6))))
-				normalOffset = 0.5*viewToWorld(FlatNormals);
-
-			#if LPV_NORMAL_STRENGTH > 0
-				vec3 texNormalOffset = -normalOffset + slopednormal;
-				normalOffset = mix(normalOffset, texNormalOffset, (LPV_NORMAL_STRENGTH*0.01));
-			#endif
-
-			vec3 lpvPos = GetLpvPosition(feetPlayerPos) + normalOffset;
-		#else
-			const vec3 lpvPos = vec3(0.0);
-		#endif
-
-		// little highlight effect just to make caves look a little less flat.
-		MinimumLightColor = MinimumLightColor + MinimumLightColor*dot(feetPlayerPos_normalized, slopednormal)*0.7;
-
-		Indirect_lighting = DoAmbientLightColor(feetPlayerPos, lpvPos, Indirect_lighting, MinimumLightColor, vec3(TORCH_R,TORCH_G,TORCH_B) , lightmap.xy, exposure);
-		
 		#ifdef OVERWORLD_SHADER
 			Indirect_lighting += LightningFlashLighting;
 		#endif
@@ -1166,29 +1148,56 @@ void main() {
 			Indirect_lighting *= AO;
 		#endif
 
-		// GTAO
+		// GTAO... this is so dumb but whatevverrr
 		#if indirect_effect == 2
-			vec2 r2 = fract(R2_samples((frameCounter%40000) + frameCounter*2) + bnoise);
-			Indirect_lighting = AmbientLightColor/2.5;
-			
-			AO = ambient_occlusion(vec3(texcoord/RENDER_SCALE-TAA_Offset*texelSize*0.5,z), viewPos, worldToView(slopednormal), r2) * vec3(1.0);
+			float vanillaAO_curve = pow(1.0 - vanilla_AO*vanilla_AO,5.0);
 
-			Indirect_lighting *= AO;
+			vec2 r2 = fract(R2_samples((frameCounter%40000) + frameCounter*2) + bnoise);
+			float GTAO =  !hand ? ambient_occlusion(vec3(texcoord/RENDER_SCALE-TAA_Offset*texelSize*0.5, z), viewPos, worldToView(slopednormal), r2) : 1.0;
+			AO = vec3(min(vanillaAO_curve,GTAO));
+			
+			vec3 IndirectColor = Indirect_lighting;
+
+			float SkylightDir = (slopednormal / dot(abs(slopednormal),vec3(1.0))).y*1.5;
+			if(isGrass) SkylightDir = 1.5;
+
+			skylight = max(pow(viewToWorld(FlatNormals).y*0.5+0.5,0.1) + SkylightDir, 0.2 + (1-lightmap.y)*0.8);
+			skylight =  min(skylight, mix(0.95, 2.5, pow(1-pow(1-GTAO, 0.5),2.0)	));
+
+			IndirectColor *= skylight;
+
+			Indirect_lighting = doIndirectLighting(IndirectColor, lightmap.y) * AO;
 		#endif
 
 		// RTAO and/or SSGI
 		#if indirect_effect == 3 || indirect_effect == 4
-			
-			// Indirect_lighting = AmbientLightColor;
-
-
-			ApplySSRT(Indirect_lighting, viewPos, normal, vec3(bnoise, noise_2), 		feetPlayerPos, lpvPos, exposure, lightmap.xy, AmbientLightColor*2.5, vec3(TORCH_R,TORCH_G,TORCH_B), isGrass, hand);
+			if(!hand) Indirect_lighting = ApplySSRT(viewPos, normal, vec3(bnoise, noise_2), AmbientLightColor, MinimumLightColor, lightmap.y, isGrass, isDHrange);
 		#endif
 
 		#if defined END_SHADER
 			Direct_lighting *= AO;
 		#endif
 
+	///////////////////////// BLOCKLIGHT LIGHTING OR LPV LIGHTING OR FLOODFILL COLORED LIGHTING
+	
+		#ifdef IS_LPV_ENABLED
+			vec3 normalOffset = vec3(0.0);
+
+			if (any(greaterThan(abs(FlatNormals), vec3(1.0e-6))))
+				normalOffset = 0.5*viewToWorld(FlatNormals);
+
+			#if LPV_NORMAL_STRENGTH > 0
+				vec3 texNormalOffset = -normalOffset + slopednormal;
+				normalOffset = mix(normalOffset, texNormalOffset, (LPV_NORMAL_STRENGTH*0.01));
+			#endif
+
+			vec3 lpvPos = GetLpvPosition(feetPlayerPos) + normalOffset;
+		#else
+			const vec3 lpvPos = vec3(0.0);
+		#endif	
+
+		Indirect_lighting += doBlockLightLighting( vec3(TORCH_R,TORCH_G,TORCH_B), lightmap.x, exposure, feetPlayerPos, lpvPos);
+
 	////////////////////////////////////////////////////////////////////////////////
 	/////////////////////////	SUB SURFACE SCATTERING	////////////////////////////
 	////////////////////////////////////////////////////////////////////////////////
@@ -1237,9 +1246,8 @@ void main() {
 
 			
 			Direct_SSS = SubsurfaceScattering_sun(albedo, ShadowBlockerDepth, sunSSS_density, clamp(dot(feetPlayerPos_normalized, WsunVec),0.0,1.0), SSS_shadow, shadowMapFalloff2);
-			// Direct_SSS = vec3(1.0);
 
-			if (isEyeInWater == 0) Direct_SSS *= lightLeakFix;
+			Direct_SSS *= lightLeakFix;
 
 			#ifndef SCREENSPACE_CONTACT_SHADOWS
 				Direct_SSS = mix(vec3(0.0), Direct_SSS, shadowMapFalloff2);
@@ -1298,7 +1306,7 @@ void main() {
 	#endif
 	#if DEBUG_VIEW == debug_NORMALS
 		if(swappedDepth >= 1.0) Direct_lighting = vec3(1.0);
-		gl_FragData[0].rgb = normalize(worldToView(normal));
+		gl_FragData[0].rgb = normal ;
 	#endif
 	#if DEBUG_VIEW == debug_SPECULAR
 		if(swappedDepth >= 1.0) Direct_lighting = vec3(1.0);
diff --git a/shaders/dimensions/composite2.fsh b/shaders/dimensions/composite2.fsh
index fd3b3d1..20a5f23 100644
--- a/shaders/dimensions/composite2.fsh
+++ b/shaders/dimensions/composite2.fsh
@@ -70,6 +70,8 @@ float linearizeDepthFast(const in float depth, const in float near, const in flo
     return (near * far) / (depth * (near - far) + far);
 }
 
+#define IS_LPV_ENABLED
+
 #if defined LPV_VL_FOG_ILLUMINATION && defined IS_LPV_ENABLED
 	
 	flat varying float exposure;
diff --git a/shaders/dimensions/composite3.fsh b/shaders/dimensions/composite3.fsh
index 2a586fb..53af1dc 100644
--- a/shaders/dimensions/composite3.fsh
+++ b/shaders/dimensions/composite3.fsh
@@ -434,7 +434,7 @@ void main() {
 ////// --------------- BLEND FOG INTO SCENE
 //////////// apply VL fog over opaque and translucents
 
-  color *= vl.a*cloudAlpha;
+  color *= vl.a*cloudAlpha ;
   color += vl.rgb;
   bloomyFogMult *= mix(vl.a,vl.a*0.5 + 0.5, rainStrength);
   
diff --git a/shaders/dimensions/deferred.vsh b/shaders/dimensions/deferred.vsh
index 567572a..de0da17 100644
--- a/shaders/dimensions/deferred.vsh
+++ b/shaders/dimensions/deferred.vsh
@@ -148,7 +148,7 @@ void main() {
 	// maximum control of color and luminance
 	// vec3 minimumlight =  vec3(0.5,0.75,1.0) * (MIN_LIGHT_AMOUNT*0.001 + nightVision);
 	vec3 minimumlight =  vec3(0.5,0.75,1.0) * nightVision;
-	averageSkyCol_Clouds = max(	normalize(averageSkyCol_Clouds) * min(luma(averageSkyCol_Clouds) * 3.0,2.5) * (1.0-rainStrength*0.3), minimumlight);
+	averageSkyCol_Clouds = max(	normalize(averageSkyCol_Clouds) * min(luma(averageSkyCol_Clouds) * 3.0,2.5) * (1.0-rainStrength*0.7), minimumlight);
 	averageSkyCol = max(averageSkyCol * PLANET_GROUND_BRIGHTNESS, minimumlight);
 
 ////////////////////////////////////////
diff --git a/shaders/lib/diffuse_lighting.glsl b/shaders/lib/diffuse_lighting.glsl
index 29e37fc..6044852 100644
--- a/shaders/lib/diffuse_lighting.glsl
+++ b/shaders/lib/diffuse_lighting.glsl
@@ -21,92 +21,56 @@
     }
 #endif
 
-vec3 DoAmbientLightColor(
-    vec3 playerPos,
-    vec3 lpvPos,
-    vec3 SkyColor,
-    vec3 MinimumColor,
-    vec3 TorchColor, 
-    vec2 Lightmap,
-    float Exposure
+vec3 doBlockLightLighting(
+    vec3 lightColor, float lightmap, float exposureValue,
+    vec3 playerPos, vec3 lpvPos
 ){
-	// Lightmap = vec2(0.0,1.0);
-    // float LightLevelZero = clamp(pow(eyeBrightnessSmooth.y/240. + Lightmap.y,2.0) ,0.0,1.0);
 
-    // do sky lighting.
-    float skyLM = (pow(Lightmap.y,15.0)*2.0 + pow(Lightmap.y,2.5))*0.5;
-    vec3 MinimumLight = MinimumColor * max(MIN_LIGHT_AMOUNT*0.01, nightVision * 0.1);
-    vec3 IndirectLight = max(SkyColor * ambient_brightness * skyLM * 0.7,     MinimumLight); 
+    float lightmapCurve = pow(1.0-sqrt(1.0-clamp(lightmap,0.0,1.0)),2.0) * 2.0;
     
-    // do torch lighting
-    float TorchLM = pow(1.0-sqrt(1.0-clamp(Lightmap.x,0.0,1.0)),2.0) * 2.0;
-    float TorchBrightness_autoAdjust = mix(1.0, 30.0,  clamp(exp(-10.0*Exposure),0.0,1.0));
-    
-    vec3 TorchLight = TorchColor * TorchLM * TORCH_AMOUNT;
+    vec3 blockLight = lightColor * lightmapCurve; //;
     
     #if defined IS_LPV_ENABLED && defined MC_GL_EXT_shader_image_load_store
         vec4 lpvSample = SampleLpvLinear(lpvPos);
-        vec3 LpvTorchLight = GetLpvBlockLight(lpvSample);
+        vec3 lpvBlockLight = GetLpvBlockLight(lpvSample);
 
-        // i gotchu
-        float fadeLength = 10.0; // in blocks
+        // create a smooth falloff at the edges of the voxel volume.
+        float fadeLength = 10.0; // in meters
         vec3 cubicRadius = clamp( min(((LpvSize3-1.0) - lpvPos)/fadeLength,      lpvPos/fadeLength) ,0.0,1.0);
-        float LpvFadeF = cubicRadius.x*cubicRadius.y*cubicRadius.z;
-
-        LpvFadeF = 1.0 - pow(1.0-pow(LpvFadeF,1.5),3.0); // make it nice and soft :)
+        float voxelRangeFalloff = cubicRadius.x*cubicRadius.y*cubicRadius.z;
+        voxelRangeFalloff = 1.0 - pow(1.0-pow(voxelRangeFalloff,1.5),3.0);
         
-        TorchLight = mix(TorchLight, LpvTorchLight/5.0, LpvFadeF);
+        // outside the voxel volume, lerp to vanilla lighting as a fallback
+        blockLight = mix(blockLight, lpvBlockLight/5.0, voxelRangeFalloff);
 
-        const vec3 normal = vec3(0.0); // TODO
+        #ifdef Hand_Held_lights
+            // create handheld lightsources
+            const vec3 normal = vec3(0.0); // TODO
 
-        if (heldItemId > 0)
-            TorchLight += GetHandLight(heldItemId, playerPos, normal);
+                if (heldItemId > 0)
+                blockLight += GetHandLight(heldItemId, playerPos, normal);
 
-        if (heldItemId2 > 0)
-            TorchLight += GetHandLight(heldItemId2, playerPos, normal);
+                if (heldItemId2 > 0)
+                blockLight += GetHandLight(heldItemId2, playerPos, normal);
+        #endif
     #endif
 
-    return IndirectLight + TorchLight * TorchBrightness_autoAdjust;
+    // try to make blocklight have consistent visiblity in different light levels.
+    float autoBrightness = mix(1.0, 30.0,  clamp(exp(-10.0*exposureValue),0.0,1.0));
+    blockLight *= autoBrightness;
+    
+    return blockLight * TORCH_AMOUNT;
 }
 
-
-// this is dumb, and i plan to remove it eventually...
-vec4 RT_AmbientLight(
-    vec3 playerPos,
-    vec3 lpvPos,
-    float Exposure,
-    vec2 Lightmap,
-    vec3 TorchColor
+vec3 doIndirectLighting(
+    vec3 lightColor, vec3 minimumLightColor, float lightmap
 ){
-    float skyLM = (pow(Lightmap.y,15.0)*2.0 + pow(Lightmap.y,2.5))*0.5;
 
+    float lightmapCurve = (pow(lightmap,15.0)*2.0 + pow(lightmap,2.5))*0.5;
 
-    // do torch lighting
-    float TorchLM = pow(1.0-sqrt(1.0-clamp(Lightmap.x,0.0,1.0)),2.0) * 2.0;
-    float TorchBrightness_autoAdjust = mix(1.0, 30.0,  clamp(exp(-10.0*Exposure),0.0,1.0)) ;
-    vec3 TorchLight = TorchColor * TorchLM * TORCH_AMOUNT  ;
-    
-    #if defined IS_LPV_ENABLED && defined MC_GL_EXT_shader_image_load_store
-        vec4 lpvSample = SampleLpvLinear(lpvPos);
-        vec3 LpvTorchLight = GetLpvBlockLight(lpvSample);
+    vec3 indirectLight = lightColor * lightmapCurve * ambient_brightness * 0.7; 
 
-        // i gotchu
-        float fadeLength = 10.0; // in blocks
-        vec3 cubicRadius = clamp( min(((LpvSize3-1.0) - lpvPos)/fadeLength,      lpvPos/fadeLength) ,0.0,1.0);
-        float LpvFadeF = cubicRadius.x*cubicRadius.y*cubicRadius.z;
+    indirectLight += minimumLightColor * max(MIN_LIGHT_AMOUNT*0.01, nightVision * 0.1);
 
-        LpvFadeF = 1.0 - pow(1.0-pow(LpvFadeF,1.5),3.0); // make it nice and soft :)
-        
-        TorchLight = mix(TorchLight,LpvTorchLight/5.0,   LpvFadeF);
-
-        const vec3 normal = vec3(0.0); // TODO
-
-        if (heldItemId > 0)
-            TorchLight += GetHandLight(heldItemId, playerPos, normal);
-
-        if (heldItemId2 > 0)
-            TorchLight += GetHandLight(heldItemId2, playerPos, normal);
-    #endif
-
-    return vec4(TorchLight, skyLM);
+    return indirectLight;
 }
\ No newline at end of file
diff --git a/shaders/lib/indirect_lighting_effects.glsl b/shaders/lib/indirect_lighting_effects.glsl
index a66f32a..21d8487 100644
--- a/shaders/lib/indirect_lighting_effects.glsl
+++ b/shaders/lib/indirect_lighting_effects.glsl
@@ -291,22 +291,17 @@ vec3 RT(vec3 dir, vec3 position, float noise, float stepsizes, bool hand){
 	return vec3(1.1);
 }
 
-void ApplySSRT(
-	inout vec3 lighting, 
+vec3 ApplySSRT(
 	vec3 viewPos,
 	vec3 normal,
 	vec3 noise,
 	
-	vec3 playerPos,
-    vec3 lpvPos,
-    float Exposure,
-	vec2 lightmaps, 
-
-	vec3 skylightcolor, 
-	vec3 torchcolor, 
+	vec3 indirectLightColor,
+	vec3 minLightColor,
+	float lightmap, 
 
 	bool isGrass,
-	bool hand
+	bool isLOD
 ){
 	int nrays = RAY_COUNT;
 
@@ -318,9 +313,7 @@ void ApplySSRT(
 	vec3 occlusion2 = vec3(0.0);
 	vec3 skycontribution2 = vec3(0.0);
 
-	// rgb = torch color * lightmap. a = sky lightmap.
-	vec4 Lighting = RT_AmbientLight(playerPos, lpvPos, Exposure, lightmaps, torchcolor);
-    skylightcolor = max(skylightcolor * ambient_brightness * Lighting.a,  vec3(1.0) * (MIN_LIGHT_AMOUNT*0.01 + nightVision)); 
+	vec3 ambientColor = doIndirectLighting(indirectLightColor * 2.5, minLightColor, lightmap);
 
 	for (int i = 0; i < nrays; i++){
 		int seed = (frameCounter%40000)*nrays+i;
@@ -330,33 +323,32 @@ void ApplySSRT(
 		#ifdef HQ_SSGI
 			vec3 rayHit = rayTrace_GI( mat3(gbufferModelView) * rayDir, viewPos, noise.z, 50.); // ssr rt
 		#else
-			vec3 rayHit = RT(mat3(gbufferModelView)*rayDir, viewPos, noise.z, 30., hand);  // choc sspt 
+			vec3 rayHit = RT(mat3(gbufferModelView)*rayDir, viewPos, noise.z, 30., isLOD);  // choc sspt 
 		#endif
 
 		#ifdef SKY_CONTRIBUTION_IN_SSRT
 			#ifdef OVERWORLD_SHADER
 				if(isGrass) rayDir.y = clamp(rayDir.y +  0.5,-1,1);
 
-				// rayDir.y = mix(-1.0, rayDir.y, lightmaps.y*lightmaps.y);
-				skycontribution = pow(skyCloudsFromTexLOD(rayDir, colortex4, 0).rgb/30, vec3(0.7)) *2.5 * ambient_brightness * Lighting.a  + Lighting.rgb;
+				skycontribution = mix(ambientColor,  pow(skyCloudsFromTexLOD(rayDir, colortex4, 0).rgb/30, vec3(0.7)), lightmap);
 			#else
-				skycontribution = (pow(skyCloudsFromTexLOD2(rayDir, colortex4, 6).rgb / 30.0,vec3(0.7))/2.5 * ambient_brightness) * Lighting.a + skylightcolor*(1-Lighting.a)  + Lighting.rgb;
+				skycontribution = pow(skyCloudsFromTexLOD2(rayDir, colortex4, 6).rgb / 30.0,vec3(0.7));
 			#endif
 
 			
-   			skycontribution = max(skycontribution,  vec3(1.0) * (MIN_LIGHT_AMOUNT*0.01 + nightVision));
+   			skycontribution = max(skycontribution,  minLightColor * max(MIN_LIGHT_AMOUNT*0.01, nightVision * 0.1));
 		#else
 
 			#ifdef OVERWORLD_SHADER
 				if(isGrass) rayDir.y = clamp(rayDir.y +  0.25,-1,1);
 			
-				skycontribution = skylightcolor * (max(rayDir.y,pow(1.0-lightmaps.y,2))*0.95+0.05) + Lighting.rgb;
+				skycontribution = ambientColor * (max(rayDir.y,pow(1.0-lightmap,2))*0.95+0.05);
 			#else
-				skycontribution = skylightcolor + Lighting.rgb;
+				skycontribution = ambientColor;
 			#endif
 
 			#if indirect_effect == 4
-				skycontribution2 = skylightcolor + Lighting.rgb;
+				skycontribution2 = ambientColor;
 			#endif
 
 		#endif
@@ -370,7 +362,6 @@ void ApplySSRT(
 				
 				if (previousPosition.x > 0.0 && previousPosition.y > 0.0 && previousPosition.x < 1.0 && previousPosition.x < 1.0){
 					radiance += texture2D(colortex5, previousPosition.xy).rgb * GI_Strength + skycontribution;
-		
 				} else{
 					radiance += skycontribution;
 				}
@@ -389,14 +380,14 @@ void ApplySSRT(
 			radiance += skycontribution;
 		}
 	}
+
+	if(isLOD) return max(radiance/nrays, 0.0);
 	
 	occlusion *= AO_Strength;
-	
-	#if indirect_effect == 4
-		lighting = max(radiance/nrays - max(occlusion, occlusion2*0.5)/nrays, 0.0);
-	#else
-		lighting = max(radiance/nrays - occlusion/nrays, 0.0);
-	#endif
 
-	if(hand) lighting = skylightcolor/1.5;
+	#if indirect_effect == 4
+		return max(radiance/nrays - max(occlusion, occlusion2*0.5)/nrays, 0.0);
+	#else
+		return max(radiance/nrays - occlusion/nrays, 0.0);
+	#endif
 }
\ No newline at end of file
diff --git a/shaders/lib/overworld_fog.glsl b/shaders/lib/overworld_fog.glsl
index e928505..abdb1e8 100644
--- a/shaders/lib/overworld_fog.glsl
+++ b/shaders/lib/overworld_fog.glsl
@@ -112,6 +112,7 @@ vec4 GetVolumetricFog(
 	
 	vec3 color = vec3(0.0);
 	float absorbance = 1.0;
+	float AtmosphereAbsorbance = 1.0;
 
 	float lightleakfix = 1.0 - caveDetection;
 	float lightleakfix2 = pow(clamp(eyeBrightnessSmooth.y/240. ,0.0,1.0),3.0);
@@ -235,19 +236,28 @@ vec4 GetVolumetricFog(
 			absorbance *= fogVolumeCoeff;
 
 		//------ ATMOSPHERE HAZE EFFECT
+			#if defined CloudLayer0 && defined VOLUMETRIC_CLOUDS
+				float cloudPlaneCutoff = clamp((CloudLayer0_height +  max(eyeAltitude-(CloudLayer0_height-100),0)) - progressW.y,0.0,1.0);
+			#else
+				float cloudPlaneCutoff = 1.0;
+			#endif
+
 			// just air
-			vec2 airCoef = exp2(-max(progressW.y-SEA_LEVEL,0.0)/vec2(8.0e3, 1.2e3)*vec2(6.,7.0)) * (24.0 * atmosphereMult) * Haze_amount * clamp((CloudLayer0_height +  max(eyeAltitude-(CloudLayer0_height-100),0)) - progressW.y,0.0,1.0);
+			vec2 airCoef = exp2(-max(progressW.y-SEA_LEVEL,0.0)/vec2(8.0e3, 1.2e3)*vec2(6.,7.0)) * (24.0 * atmosphereMult) * Haze_amount * cloudPlaneCutoff;
 
 			// Pbr for air, yolo mix between mie and rayleigh for water droplets
 			vec3 rL = rC*airCoef.x;
 			vec3 m =  mC*(airCoef.y+densityVol*300.0);
 
 			// calculate the atmosphere haze seperately and purely additive to color, do not contribute to absorbtion.
-			vec3 Atmosphere = LightSourcePhased * sh * (rayL*rL + sunPhase*m) + (AveragedAmbientColor*0.7) * (rL+m) * lightleakfix2;
-			color += (Atmosphere - Atmosphere*exp(-(rL+m)*dd*dL_alternate)) / (rL+m+1e-6) * absorbance;	
+			vec3 atmosphereVolumeCoeff = exp(-(rL+m)*dd*dL_alternate);
+			
+			vec3 Atmosphere = (LightSourcePhased * sh * (rayL*rL + sunPhase*m) + AveragedAmbientColor * (rL+m)) * lightleakfix2;
+			color += (Atmosphere - Atmosphere * atmosphereVolumeCoeff) / (rL+m+1e-6) * AtmosphereAbsorbance * absorbance;
+			AtmosphereAbsorbance *= dot(atmosphereVolumeCoeff, vec3(0.33333));
 			
 		//------ LPV FOG EFFECT
-			#if defined LPV_VL_FOG_ILLUMINATION && defined EXCLUDE_WRITE_TO_LUT
+			#if defined LPV_VL_FOG_ILLUMINATION && defined EXCLUDE_WRITE_TO_LUT 
 				color += LPV_FOG_ILLUMINATION(progressW-cameraPosition, dd, dL) * TorchBrightness_autoAdjust * absorbance;
 			#endif
 
diff --git a/shaders/lib/settings.glsl b/shaders/lib/settings.glsl
index 71d9a37..d64c9dd 100644
--- a/shaders/lib/settings.glsl
+++ b/shaders/lib/settings.glsl
@@ -699,7 +699,7 @@ const vec3 aerochrome_color = mix(vec3(1.0, 0.0, 0.0), vec3(0.715, 0.303, 0.631)
 #define SELECT_BOX_COL_G 0.0 // [0.0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0]
 #define SELECT_BOX_COL_B 0.0 // [0.0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0]
 
-// #define OLD_CAVE_DETECTION
+#define OLD_CAVE_DETECTION
 
 ///////////////////////////////////////////
 // ----- DISTANT HORIZONS SETTINGS ----- //
diff --git a/shaders/lib/volumetricClouds.glsl b/shaders/lib/volumetricClouds.glsl
index 98fe3ab..350b652 100644
--- a/shaders/lib/volumetricClouds.glsl
+++ b/shaders/lib/volumetricClouds.glsl
@@ -467,7 +467,7 @@ vec4 renderClouds(
 		dV_Sun *= lightCol.a;
 	#endif
 	
-	float SdotV = dot(mat3(gbufferModelView)*WsunVec, normalize(FragPosition)) ;
+	float SdotV = dot(WsunVec, normalize(mat3(gbufferModelViewInverse)*FragPosition + gbufferModelViewInverse[3].xyz));
 
 	float mieDay = phaseg(SdotV, 0.85) + phaseg(SdotV, 0.75);
 	float mieDayMulti = (phaseg(SdotV, 0.35) + phaseg(-SdotV, 0.35) * 0.5) ;
diff --git a/shaders/shaders.properties b/shaders/shaders.properties
index 4d6b6fa..c5cb7f2 100644
--- a/shaders/shaders.properties
+++ b/shaders/shaders.properties
@@ -482,9 +482,9 @@ uniform.float.noPuddleAreas = smooth(if(in(biome, 3, 4, 16, 37, 39, 48, 49, 31,
 #endif
 
 #if defined OLD_CAVE_DETECTION
-    variable.bool.inTheDarkTimer = smooth(if(eyeBrightness.y < 0.01, 1.0, 0.0), 30.0, 1.0) > 0.99
+    variable.bool.inTheDarkTimer = smooth(if(eyeBrightness.y < 0.01, 1.0, 0.0), 10.0, 0.0) > 0.99
 
-    uniform.float.caveDetection = smooth(if(inTheDarkTimer, 1.0, 0.0), 10.0, 0.5)
+    uniform.float.caveDetection = smooth(if(inTheDarkTimer, 1.0, 0.0), 3.0, 1.0)
 #else
     # the idea is to modulate the mood value between 0 - 1
     # then check if the modulated value is non-zero. when it goes back to zero, it will have a very slowly deteriorating value.