summaryrefslogtreecommitdiff
path: root/gfx
diff options
context:
space:
mode:
authorDan Goodliffe <dan@randomdan.homeip.net>2024-10-26 12:48:45 +0100
committerDan Goodliffe <dan@randomdan.homeip.net>2024-10-26 12:48:45 +0100
commit75c36fe3aa875f6377f9adec8ffedf112d6335cb (patch)
tree88e5647b2f4152d7c7a5885a86c0ec171a27b86f /gfx
parentImprove sun illumination based on angular size and astronomical twilight (diff)
downloadilt-75c36fe3aa875f6377f9adec8ffedf112d6335cb.tar.bz2
ilt-75c36fe3aa875f6377f9adec8ffedf112d6335cb.tar.xz
ilt-75c36fe3aa875f6377f9adec8ffedf112d6335cb.zip
Basic soft shadows
Diffstat (limited to 'gfx')
-rw-r--r--gfx/gl/shaders/directionalLight.fs25
1 files changed, 18 insertions, 7 deletions
diff --git a/gfx/gl/shaders/directionalLight.fs b/gfx/gl/shaders/directionalLight.fs
index 86447ec..cdf0389 100644
--- a/gfx/gl/shaders/directionalLight.fs
+++ b/gfx/gl/shaders/directionalLight.fs
@@ -17,24 +17,35 @@ uniform ivec3 lightPoint;
uniform mat4 lightViewProjection[MAX_MAPS];
uniform uint lightViewProjectionCount;
-const vec3 e1 = vec3(0, 0, 0), e2 = vec3(1, 1, 1);
+float
+getShadow(vec3 positionInLightSpace, float m, vec2 texelSize)
+{
+ float shadow = 0.0;
+ for (float x = -texelSize.x; x <= texelSize.x; x += texelSize.x) {
+ for (float y = -texelSize.y; y <= texelSize.y; y += texelSize.y) {
+ const float lightSpaceDepth = texture(shadowMap, vec3(positionInLightSpace.xy + vec2(x, y), m)).r;
+ shadow += step(positionInLightSpace.z, lightSpaceDepth + 0.001);
+ }
+ }
+ return shadow / 9.0;
+}
float
-insideShadowCube(vec3 v)
+insideShadowCube(vec3 v, vec2 texelSize)
{
- const vec3 s = step(e1, v) - step(e2, v);
+ const vec3 s = step(vec3(texelSize, 0), v) - step(vec3(1 - texelSize, 1), v);
return s.x * s.y * s.z;
}
float
isShaded(vec4 Position)
{
+ const vec2 texelSize = 1.0 / textureSize(shadowMap, 0).xy;
for (uint m = 0u; m < lightViewProjectionCount; m++) {
- const vec3 PositionInLightSpace = (lightViewProjection[m] * Position).xyz;
- const float inside = insideShadowCube(PositionInLightSpace);
+ const vec3 positionInLightSpace = (lightViewProjection[m] * Position).xyz;
+ const float inside = insideShadowCube(positionInLightSpace, texelSize);
if (inside > 0) {
- const float lightSpaceDepth = texture(shadowMap, vec3(PositionInLightSpace.xy, m)).r;
- return step(PositionInLightSpace.z, lightSpaceDepth + 0.001);
+ return getShadow(positionInLightSpace, m, texelSize);
}
}
return 1;