Skip to content

Commit

Permalink
Reuse ndc_to_world matrix in Camera::viewport_to_world (#6532)
Browse files Browse the repository at this point in the history
# Objective

Solve #6531.


Co-authored-by: devil-ira <justthecooldude@gmail.com>
  • Loading branch information
irate-devil and irate-devil committed Nov 10, 2022
1 parent 1914a3f commit 9b56b54
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions crates/bevy_render/src/camera/camera.rs
Original file line number Diff line number Diff line change
Expand Up @@ -236,11 +236,13 @@ impl Camera {
let target_size = self.logical_viewport_size()?;
let ndc = viewport_position * 2. / target_size - Vec2::ONE;

let world_near_plane = self.ndc_to_world(camera_transform, ndc.extend(1.))?;
// Using EPSILON because passing an ndc with Z = 0 returns NaNs.
let world_far_plane = self.ndc_to_world(camera_transform, ndc.extend(f32::EPSILON))?;
let ndc_to_world =
camera_transform.compute_matrix() * self.computed.projection_matrix.inverse();
let world_near_plane = ndc_to_world.project_point3(ndc.extend(1.));
// Using EPSILON because an ndc with Z = 0 returns NaNs.
let world_far_plane = ndc_to_world.project_point3(ndc.extend(f32::EPSILON));

Some(Ray {
(!world_near_plane.is_nan() && !world_far_plane.is_nan()).then_some(Ray {
origin: world_near_plane,
direction: (world_far_plane - world_near_plane).normalize(),
})
Expand Down

0 comments on commit 9b56b54

Please sign in to comment.