Created
June 15, 2022 10:18
-
-
Save nin-jat/681814ed859551768b71316e7d68e495 to your computer and use it in GitHub Desktop.
screen to ray to world orthographic projection
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// Convert the cursor's screen position to a camera ray for orthographic projection. | |
pub fn cursor_to_world_orthographic( | |
camera_world: &Transform, | |
camera_projection: &OrthographicProjection, | |
window: &Window, | |
) -> Option<Ray> { | |
// Get our cursor's position or return None if no cursor exists. | |
let cursor = cursor_normalised(window, &camera_projection.window_origin); | |
if cursor.is_none() { | |
return None; | |
} | |
let cursor_pos = cursor.unwrap(); | |
let mut position = camera_world.right() * cursor_pos.x * camera_projection.scale; | |
position += camera_world.up() * cursor_pos.y * camera_projection.scale; | |
Some(Ray { | |
origin: camera_world.translation + position, | |
direction: camera_world.forward(), | |
}) | |
} | |
/// The ray | |
pub struct Ray { | |
pub origin: Vec3, | |
pub direction: Vec3, | |
} | |
/// Find the intersection point of this ray and an infinite plane. | |
impl Ray { | |
pub fn intersect_plane(&self, normal: Vec3, origin: Vec3) -> Vec3 { | |
let origin = origin - self.origin; | |
let distance = origin.dot(normal) / self.direction.dot(normal); | |
self.origin + self.direction * distance | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment