From 75c36fe3aa875f6377f9adec8ffedf112d6335cb Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sat, 26 Oct 2024 12:48:45 +0100 Subject: Basic soft shadows --- gfx/gl/shaders/directionalLight.fs | 25 ++++++++++++++++++------- 1 file 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; -- cgit v1.2.3