Improved the shadows with a larger texture, PCF for the edges, and only calculating the shadows once.

This commit is contained in:
Zed A. Shaw 2026-09-21 14:49:59 -04:00
parent 621c355876
commit 4747d297e8
4 changed files with 49 additions and 25 deletions

View file

@ -16,17 +16,31 @@ float ShadowCalculation(vec4 fragPosLightSpace, float bias)
{
// perform perspective divide
vec3 projCoords = fragPosLightSpace.xyz / fragPosLightSpace.w;
// transform to [0,1] range
projCoords = projCoords * 0.5 + 0.5;
// avoid the frustrum
if(projCoords.z > 1.0) return 0.0;
// get closest depth value from light's perspective (using [0,1] range fragPosLight as coords)
float closestDepth = texture(shadowMap, projCoords.xy).r;
// get depth of current fragment from light's perspective
float currentDepth = projCoords.z;
// check whether current frag pos is in shadow
// PCF
float shadow = 0.0;
vec2 texelSize = 1.0 / textureSize(shadowMap, 0);
for(int x = -1; x <= 1; ++x) {
for(int y = -1; y <= 1; ++y) {
float pcfDepth = texture(shadowMap, projCoords.xy + vec2(x, y) * texelSize).r;
shadow += currentDepth - bias > pcfDepth ? 1.0 : 0.0;
}
}
float shadow = currentDepth - bias > closestDepth ? 1.0 : 0.0;
return shadow;
return shadow / 9.0;
}
void main()