Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix viewport_to_world and world_to_viewport #6526

Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 7 additions & 2 deletions crates/bevy_render/src/camera/camera.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,10 @@ impl Camera {
}

// Once in NDC space, we can discard the z element and rescale x/y to fit the screen
Some((ndc_space_coords.truncate() + Vec2::ONE) / 2.0 * target_size)
let mut viewport_pos = (ndc_space_coords.truncate() + Vec2::ONE) / 2.0 * target_size;
// Viewport origin is at the top.
viewport_pos.y *= -1.;
Some(viewport_pos)
}

/// Returns a ray originating from the camera, that passes through everything beyond `viewport_position`.
Expand All @@ -234,7 +237,9 @@ impl Camera {
viewport_position: Vec2,
) -> Option<Ray> {
let target_size = self.logical_viewport_size()?;
let ndc = viewport_position * 2. / target_size - Vec2::ONE;
let mut ndc = viewport_position * 2. / target_size - Vec2::ONE;
// Viewport origin is at the top.
ndc.y *= -1.;

let world_near_plane = self.ndc_to_world(camera_transform, ndc.extend(1.))?;
// Using EPSILON because passing an ndc with Z = 0 returns NaNs.
Expand Down