From b6ac3d7da780fed9b5d3c65b5b3f94e229bdfa57 Mon Sep 17 00:00:00 2001 From: robtfm <50659922+robtfm@users.noreply.github.com> Date: Fri, 13 Jan 2023 01:16:20 +0000 Subject: [PATCH] fix spot dir nan again (#7176) # Objective fix error with shadow shader's spotlight direction calculation when direction.y ~= 0 fixes #7152 ## Solution same as #6167: in shadows.wgsl, clamp 1-x^2-z^2 to >= 0 so that we can safely sqrt it --- crates/bevy_pbr/src/render/shadows.wgsl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/bevy_pbr/src/render/shadows.wgsl b/crates/bevy_pbr/src/render/shadows.wgsl index 70c2ed3383a2e..34f4c3b627f48 100644 --- a/crates/bevy_pbr/src/render/shadows.wgsl +++ b/crates/bevy_pbr/src/render/shadows.wgsl @@ -49,7 +49,7 @@ fn fetch_spot_shadow(light_id: u32, frag_position: vec4, surface_normal: ve // construct the light view matrix var spot_dir = vec3((*light).light_custom_data.x, 0.0, (*light).light_custom_data.y); // reconstruct spot dir from x/z and y-direction flag - spot_dir.y = sqrt(1.0 - spot_dir.x * spot_dir.x - spot_dir.z * spot_dir.z); + spot_dir.y = sqrt(max(0.0, 1.0 - spot_dir.x * spot_dir.x - spot_dir.z * spot_dir.z)); if (((*light).flags & POINT_LIGHT_FLAGS_SPOT_LIGHT_Y_NEGATIVE) != 0u) { spot_dir.y = -spot_dir.y; }