Skip to content

Commit

Permalink
move z control to screen method
Browse files Browse the repository at this point in the history
  • Loading branch information
mockersf committed May 2, 2022
1 parent 051e502 commit bfb15b3
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions crates/bevy_render/src/camera/camera.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down

0 comments on commit bfb15b3

Please sign in to comment.