Skip to main content

graphics_core/math/
mod.rs

1//! Viewport-space coordinate math and conversions.
2//!
3//! All math functions in this module are host-neutral and work in terms of normalized
4//! or pixel viewport coordinates, without assuming a specific browser or OS window environment.
5
6use glam::{Mat4, Vec2, Vec3, Vec4};
7
8/// A 3D ray defined by an origin and a normalized direction.
9#[derive(Clone, Copy, Debug, PartialEq)]
10pub struct Ray3D {
11    pub origin: Vec3,
12    pub direction: Vec3,
13}
14
15impl Ray3D {
16    pub fn new(origin: Vec3, direction: Vec3) -> Self {
17        Self {
18            origin,
19            direction: direction.normalize_or_zero(),
20        }
21    }
22}
23
24/// Host-neutral viewport rectangle in physical or logical pixels.
25#[derive(Clone, Copy, Debug, PartialEq)]
26pub struct ViewportRect {
27    pub x: f32,
28    pub y: f32,
29    pub width: f32,
30    pub height: f32,
31}
32
33impl ViewportRect {
34    pub fn new(x: f32, y: f32, width: f32, height: f32) -> Self {
35        Self {
36            x,
37            y,
38            width,
39            height,
40        }
41    }
42
43    /// Converts a pixel coordinate within this viewport into Normalized Device Coordinates (NDC).
44    /// NDC X: [-1.0, 1.0], Y: [-1.0, 1.0] (with Y-up).
45    pub fn pixel_to_ndc(&self, pixel_pt: Vec2) -> Vec2 {
46        let rel_x = (pixel_pt.x - self.x) / self.width.max(1.0);
47        let rel_y = (pixel_pt.y - self.y) / self.height.max(1.0);
48        Vec2::new(rel_x * 2.0 - 1.0, 1.0 - rel_y * 2.0)
49    }
50
51    /// Converts an NDC point back into viewport pixel coordinates.
52    pub fn ndc_to_pixel(&self, ndc_pt: Vec2) -> Vec2 {
53        let rel_x = (ndc_pt.x + 1.0) * 0.5;
54        let rel_y = (1.0 - ndc_pt.y) * 0.5;
55        Vec2::new(self.x + rel_x * self.width, self.y + rel_y * self.height)
56    }
57}
58
59/// Transforms a 2D viewport pixel point into 2D world coordinates.
60pub fn viewport_to_world_2d(
61    viewport_pt: Vec2,
62    viewport: ViewportRect,
63    inv_view_proj: Mat4,
64) -> Vec2 {
65    let ndc = viewport.pixel_to_ndc(viewport_pt);
66    let world_pos = inv_view_proj.project_point3(Vec3::new(ndc.x, ndc.y, 0.0));
67    Vec2::new(world_pos.x, world_pos.y)
68}
69
70/// Transforms a 2D world point into viewport pixel coordinates.
71pub fn world_to_viewport_2d(world_pt: Vec2, viewport: ViewportRect, view_proj: Mat4) -> Vec2 {
72    let clip = view_proj * Vec4::new(world_pt.x, world_pt.y, 0.0, 1.0);
73    if clip.w.abs() < 1e-6 {
74        return Vec2::ZERO;
75    }
76    let ndc = Vec2::new(clip.x / clip.w, clip.y / clip.w);
77    viewport.ndc_to_pixel(ndc)
78}
79
80/// Unprojects a 2D viewport point into a 3D ray for object picking.
81pub fn viewport_to_ray(viewport_pt: Vec2, viewport: ViewportRect, inv_view_proj: Mat4) -> Ray3D {
82    let ndc = viewport.pixel_to_ndc(viewport_pt);
83    // Near plane (NDC z = 0.0 in WGPU standard [0, 1] depth range)
84    let p_near = inv_view_proj.project_point3(Vec3::new(ndc.x, ndc.y, 0.0));
85    // Far plane (NDC z = 1.0 in WGPU)
86    let p_far = inv_view_proj.project_point3(Vec3::new(ndc.x, ndc.y, 1.0));
87    let dir = p_far - p_near;
88    Ray3D::new(p_near, dir)
89}
90
91#[cfg(test)]
92mod tests {
93    use super::*;
94
95    #[test]
96    fn test_viewport_ndc_roundtrip() {
97        let vp = ViewportRect::new(0.0, 0.0, 800.0, 600.0);
98        let pt = Vec2::new(400.0, 300.0); // Center of viewport
99        let ndc = vp.pixel_to_ndc(pt);
100        assert!((ndc.x - 0.0).abs() < 1e-5);
101        assert!((ndc.y - 0.0).abs() < 1e-5);
102
103        let back = vp.ndc_to_pixel(ndc);
104        assert!((back.x - pt.x).abs() < 1e-5);
105        assert!((back.y - pt.y).abs() < 1e-5);
106    }
107}