Skip to content

Commit

Permalink
fix Orthographic Picking (#29)
Browse files Browse the repository at this point in the history
* fix Camera::position_at_uv_coordinates Orthographic

* Use screen2ray for orthographic picking

---------

Co-authored-by: Asger Nyman Christiansen <asgernyman@gmail.com>
  • Loading branch information
paul2t and asny authored Feb 15, 2024
1 parent 8c7097c commit 268236a
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions src/camera.rs
Original file line number Diff line number Diff line change
Expand Up @@ -375,10 +375,10 @@ impl Camera {
///
pub fn position_at_uv_coordinates(&self, coords: impl Into<UvCoordinate>) -> Vec3 {
match self.projection_type() {
ProjectionType::Orthographic { height } => {
let width = height * self.viewport.aspect();
ProjectionType::Orthographic { .. } => {
let coords = coords.into();
self.position() + vec3((coords.u - 0.5) * width, (coords.v - 0.5) * height, 0.0)
let screen_pos = vec4(2. * coords.u - 1., 2. * coords.v - 1.0, -1.0, 1.);
(self.screen2ray * screen_pos).truncate()
}
ProjectionType::Perspective { .. } => *self.position(),
}
Expand Down Expand Up @@ -549,7 +549,9 @@ impl Camera {

fn update_screen2ray(&mut self) {
let mut v = self.view;
v[3] = vec4(0.0, 0.0, 0.0, 1.0);
if let ProjectionType::Perspective { .. } = self.projection_type {
v[3] = vec4(0.0, 0.0, 0.0, 1.0);
}
self.screen2ray = (self.projection * v).invert().unwrap();
}

Expand Down

0 comments on commit 268236a

Please sign in to comment.