From b49fcb01917120544c1ce57636528b2e36f00a61 Mon Sep 17 00:00:00 2001 From: Aevyrie Date: Mon, 6 Dec 2021 12:05:30 -0800 Subject: [PATCH 1/3] Check for NaN in `Camera::world_to_screen()` --- crates/bevy_render/src/camera/camera.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/bevy_render/src/camera/camera.rs b/crates/bevy_render/src/camera/camera.rs index ea600921edfce..9fda1ded07503 100644 --- a/crates/bevy_render/src/camera/camera.rs +++ b/crates/bevy_render/src/camera/camera.rs @@ -55,8 +55,8 @@ impl Camera { let world_to_ndc: Mat4 = self.projection_matrix * camera_transform.compute_matrix().inverse(); let ndc_space_coords: Vec3 = world_to_ndc.project_point3(world_position); - // NDC z-values outside of 0 < z < 1 are behind the camera and are thus not in screen space - if ndc_space_coords.z < 0.0 || ndc_space_coords.z > 1.0 { + // NDC z-values outside of 0 < z < 1 are outside the camera frustum and are thus not in screen space + if ndc_space_coords.z < 0.0 || ndc_space_coords.z > 1.0 || ndc_space_coords.z.is_nan() { return None; } // Once in NDC space, we can discard the z element and rescale x/y to fit the screen From c2dae68fc5496999d8a896f792c1a93697c81f02 Mon Sep 17 00:00:00 2001 From: Aevyrie Date: Mon, 6 Dec 2021 15:52:50 -0800 Subject: [PATCH 2/3] Add to pipelined cam, improve nan check --- crates/bevy_render/src/camera/camera.rs | 8 ++++++-- pipelined/bevy_render2/src/camera/camera.rs | 6 +++++- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/crates/bevy_render/src/camera/camera.rs b/crates/bevy_render/src/camera/camera.rs index 9fda1ded07503..f93581af7703e 100644 --- a/crates/bevy_render/src/camera/camera.rs +++ b/crates/bevy_render/src/camera/camera.rs @@ -56,12 +56,16 @@ impl Camera { self.projection_matrix * camera_transform.compute_matrix().inverse(); let ndc_space_coords: Vec3 = world_to_ndc.project_point3(world_position); // NDC z-values outside of 0 < z < 1 are outside the camera frustum and are thus not in screen space - if ndc_space_coords.z < 0.0 || ndc_space_coords.z > 1.0 || ndc_space_coords.z.is_nan() { + if ndc_space_coords.z < 0.0 || ndc_space_coords.z > 1.0 { return None; } // Once in NDC space, we can discard the z element and rescale x/y to fit the screen let screen_space_coords = (ndc_space_coords.truncate() + Vec2::ONE) / 2.0 * window_size; - Some(screen_space_coords) + if !screen_space_coords.is_nan() { + Some(screen_space_coords) + } else { + None + } } } diff --git a/pipelined/bevy_render2/src/camera/camera.rs b/pipelined/bevy_render2/src/camera/camera.rs index 7a05bbe9ff1b2..42f4820a5cc2c 100644 --- a/pipelined/bevy_render2/src/camera/camera.rs +++ b/pipelined/bevy_render2/src/camera/camera.rs @@ -60,7 +60,11 @@ impl Camera { } // Once in NDC space, we can discard the z element and rescale x/y to fit the screen let screen_space_coords = (ndc_space_coords.truncate() + Vec2::ONE) / 2.0 * window_size; - Some(screen_space_coords) + if !screen_space_coords.is_nan() { + Some(screen_space_coords) + } else { + None + } } } From 9c21567663f55d61c249c4cdcba6fe0f0f29517f Mon Sep 17 00:00:00 2001 From: Aevyrie Date: Tue, 7 Dec 2021 15:00:17 -0800 Subject: [PATCH 3/3] Correct comment text in pipelined --- pipelined/bevy_render2/src/camera/camera.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pipelined/bevy_render2/src/camera/camera.rs b/pipelined/bevy_render2/src/camera/camera.rs index 42f4820a5cc2c..6bcf544356c48 100644 --- a/pipelined/bevy_render2/src/camera/camera.rs +++ b/pipelined/bevy_render2/src/camera/camera.rs @@ -54,7 +54,7 @@ impl Camera { let world_to_ndc: Mat4 = self.projection_matrix * camera_transform.compute_matrix().inverse(); let ndc_space_coords: Vec3 = world_to_ndc.project_point3(world_position); - // NDC z-values outside of 0 < z < 1 are behind the camera and are thus not in screen space + // NDC z-values outside of 0 < z < 1 are outside the camera frustum and are thus not in screen space if ndc_space_coords.z < 0.0 || ndc_space_coords.z > 1.0 { return None; }