Fix shadowmap sampling outside of frustum

This commit is contained in:
Lauri Räsänen 2023-11-04 19:29:55 +02:00
parent 5e0001e2e1
commit 5afa12a236
2 changed files with 21 additions and 3 deletions

View file

@ -1 +1,3 @@
const PI = 3.14159; const PI = 3.14159;
const INV_SQRT_2 = 0.70710678118654752440; // 1 / sqrt(2)
const INV_SQRT_3 = 0.57735026918962576451; // 1 / sqrt(3)

View file

@ -125,8 +125,24 @@ fn fs_main(vert: VertexOutput) -> @location(0) vec4<f32> {
var total_radiance: vec3<f32>; var total_radiance: vec3<f32>;
var in_light = 0.0; var in_light = 0.0;
for (var i: i32 = 0; i < 1; i++) { for (var i: i32 = 0; i < 6; i++) {
in_light = sample_direct_light(i, light.matrices[i] * vert.world_position); let light_coords = light.matrices[i] * vert.world_position;
let light_dir = normalize(light_coords.xyz);
let bias = 0.005;
// z can never be smaller than this inside 90 degree frustum
if (light_dir.z < INV_SQRT_3 - bias) {
continue;
}
// x and y can never be larger than this inside frustum
if (abs(light_dir.y) > INV_SQRT_2 + bias) {
continue;
}
if (abs(light_dir.x) > INV_SQRT_2 + bias) {
continue;
}
in_light = sample_direct_light(i, light_coords);
if (in_light > 0.0) { if (in_light > 0.0) {
break; break;
} }