From bfb15b3e7b420b3e9feca79230803e70d32b9799 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois?= Date: Sat, 5 Mar 2022 22:45:54 +0100 Subject: [PATCH] move z control to screen method --- crates/bevy_render/src/camera/camera.rs | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/crates/bevy_render/src/camera/camera.rs b/crates/bevy_render/src/camera/camera.rs index 758db280247f0..a7089585fee76 100644 --- a/crates/bevy_render/src/camera/camera.rs +++ b/crates/bevy_render/src/camera/camera.rs @@ -132,6 +132,11 @@ impl Camera { let window_size = self.target.get_logical_size(windows, images)?; if let Some(ndc_space_coords) = self.world_to_ndc(camera_transform, 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 { + 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) @@ -150,10 +155,6 @@ 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 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; - } if !ndc_space_coords.is_nan() { Some(ndc_space_coords)