From a694e337f30b3320c45b6164cb31fe65c35e6bc9 Mon Sep 17 00:00:00 2001
From: Dan Goodliffe <dan@randomdan.homeip.net>
Date: Sun, 18 Aug 2024 13:11:46 +0100
Subject: Offset shaded position to reduce chance of self-shadowing

---
 gfx/gl/shaders/directionalLight.fs | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

(limited to 'gfx')

diff --git a/gfx/gl/shaders/directionalLight.fs b/gfx/gl/shaders/directionalLight.fs
index 24457b8..86447ec 100644
--- a/gfx/gl/shaders/directionalLight.fs
+++ b/gfx/gl/shaders/directionalLight.fs
@@ -34,10 +34,10 @@ isShaded(vec4 Position)
 		const float inside = insideShadowCube(PositionInLightSpace);
 		if (inside > 0) {
 			const float lightSpaceDepth = texture(shadowMap, vec3(PositionInLightSpace.xy, m)).r;
-			return step(lightSpaceDepth, PositionInLightSpace.z);
+			return step(PositionInLightSpace.z, lightSpaceDepth + 0.001);
 		}
 	}
-	return 0;
+	return 1;
 }
 
 void
@@ -46,5 +46,5 @@ main()
 	const vec4 Position = vec4(texture(gPosition, TexCoords).xyz - lightPoint, 1);
 	const vec3 Normal = texture(gNormal, TexCoords).rgb;
 	const float shaded = isShaded(Position);
-	FragColor = (1 - shaded) * max(dot(-lightDirection, Normal) * lightColour, 0);
+	FragColor = shaded * max(dot(-lightDirection, Normal) * lightColour, 0);
 }
-- 
cgit v1.2.3