Skip to main content

mln_engine/camera/
projection.rs

1use geometry::Point;
2use glam::{DMat4, DVec2, DVec3, DVec4};
3use mln_style::constants;
4
5use crate::camera::vertical_perspective_projection::VerticalPerspectiveProjection;
6use crate::camera::view::View;
7use crate::geo::projection::Projection as WebMercator;
8use crate::tile::UnwrappedTileID;
9
10#[derive(Clone, Copy, Debug, Default, PartialEq)]
11pub struct ProjectionData {
12    pub main_matrix: DMat4,
13    pub tile_mercator_coords: DVec4,
14    pub clipping_plane: DVec4,
15    pub projection_transition: f64,
16    pub fallback_matrix: DMat4,
17    /// Clip-space Z shift for this drawable's layer, the same one the
18    /// Mercator matrix carries.
19    pub depth_offset: f64,
20    /// The layer's translation in tile units, added on the sphere; the
21    /// fallback matrix already carries it.
22    pub translate: DVec2,
23    /// The camera on the unit sphere's scale, for the line of sight to points
24    /// above the surface.
25    pub(crate) camera_position: DVec3,
26    /// On the globe, the point on the unit sphere under the middle of the
27    /// tile, which the shaders place the tile's geometry relative to; zero on
28    /// Mercator.
29    pub sphere_anchor: DVec3,
30    /// The sine and cosine of the anchor's longitude, then of its latitude,
31    /// in the angles of [`VerticalPerspectiveProjection::tile_coordinates_to_sphere`].
32    pub sphere_anchor_angles: DVec4,
33}
34
35/// A tile point in clip space, as the vertex shaders would place it.
36#[derive(Clone, Copy, Debug, Default, PartialEq)]
37pub struct ProjectedTilePoint {
38    pub point: Point<f64>,
39    pub signed_distance_from_camera: f64,
40    /// Behind the planet's horizon; never true on Mercator.
41    pub occluded: bool,
42}
43
44/// The tile-to-world matrix of a tile, the same on every projection.
45pub(crate) fn mercator_tile_matrix(tile_id: &UnwrappedTileID, scale: f64) -> DMat4 {
46    let tile_scale: u64 = 1u64 << tile_id.canonical.z;
47    let s = WebMercator::world_size(scale) / tile_scale as f64;
48    let x = (i64::from(tile_id.canonical.x) + i64::from(tile_id.wrap) * tile_scale as i64) as f64 * s;
49    let y = i64::from(tile_id.canonical.y) as f64 * s;
50    let unit = s / f64::from(constants::EXTENT);
51    DMat4::from_translation(DVec3::new(x, y, 0.0)) * DMat4::from_scale(DVec3::new(unit, unit, 1.0))
52}
53
54/// The Mercator extent of a tile, the same on every projection.
55#[must_use]
56pub(crate) fn mercator_tile_coords(tile_id: &UnwrappedTileID) -> DVec4 {
57    let tile_scale = (1u64 << tile_id.canonical.z) as f64;
58    DVec4::new(
59        f64::from(tile_id.canonical.x) / tile_scale,
60        f64::from(tile_id.canonical.y) / tile_scale,
61        1.0 / tile_scale / f64::from(constants::EXTENT),
62        1.0 / tile_scale / f64::from(constants::EXTENT),
63    )
64}
65
66/// How a [`View`] places tiles on the screen. Both variants
67/// project locations onto the same Mercator plane, so the tile-to-world matrix
68/// is `mercator_tile_matrix` on either.
69#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
70pub enum Projection {
71    #[default]
72    Mercator,
73    /// The globe, also while it hands over to or from Mercator.
74    VerticalPerspective,
75}
76
77impl Projection {
78    /// The per-tile projection contract, given the Mercator tile-to-clip
79    /// matrix the renderer already computed.
80    #[must_use]
81    pub fn projection_data(self, view: &View, tile_id: &UnwrappedTileID, mercator_matrix: &DMat4) -> ProjectionData {
82        match self {
83            Projection::Mercator => ProjectionData {
84                main_matrix: *mercator_matrix,
85                tile_mercator_coords: mercator_tile_coords(tile_id),
86                clipping_plane: DVec4::ZERO,
87                projection_transition: 0.0,
88                fallback_matrix: *mercator_matrix,
89                ..ProjectionData::default()
90            },
91            Projection::VerticalPerspective => {
92                VerticalPerspectiveProjection::projection_data(view, tile_id, mercator_matrix)
93            }
94        }
95    }
96
97    /// The clip-space position of a tile point, `elevation` meters above the
98    /// surface, computed the way the vertex shaders do it.
99    #[must_use]
100    pub(crate) fn project_tile_point(
101        self,
102        data: &ProjectionData,
103        tile_id: &UnwrappedTileID,
104        point: &Point<f64>,
105        elevation: f64,
106    ) -> ProjectedTilePoint {
107        match self {
108            Projection::Mercator => {
109                let pos = data.main_matrix * DVec4::new(point.x, point.y, elevation, 1.0);
110                ProjectedTilePoint {
111                    point: Point::new(pos.x / pos.w, pos.y / pos.w),
112                    signed_distance_from_camera: pos.w,
113                    occluded: false,
114                }
115            }
116            Projection::VerticalPerspective => {
117                VerticalPerspectiveProjection::project_tile_point(data, tile_id, point, elevation)
118            }
119        }
120    }
121
122    /// Where the vertex shaders draw a tile point: `project_tile_point`,
123    /// except while one projection hands over to another.
124    #[must_use]
125    pub(crate) fn drawn_tile_point(
126        self,
127        data: &ProjectionData,
128        tile_id: &UnwrappedTileID,
129        point: &Point<f64>,
130        elevation: f64,
131    ) -> ProjectedTilePoint {
132        match self {
133            Projection::Mercator => self.project_tile_point(data, tile_id, point, elevation),
134            Projection::VerticalPerspective => {
135                VerticalPerspectiveProjection::drawn_tile_point(data, tile_id, point, elevation)
136            }
137        }
138    }
139
140    /// Scale that keeps map-aligned circles and pitched text the size they
141    /// have on Mercator at the map center, blended with Mercator's 1 through
142    /// the projection transition as GL JS blends it.
143    #[must_use]
144    pub fn circle_radius_correction(self, view: &View) -> f64 {
145        match self {
146            Projection::Mercator => 1.0,
147            Projection::VerticalPerspective => VerticalPerspectiveProjection::circle_radius_correction(view),
148        }
149    }
150
151    /// Ratio of a screen pixel at the map center to one at the same zoom on
152    /// Mercator, blended the same way.
153    #[must_use]
154    pub fn pixel_scale(self, view: &View) -> f64 {
155        match self {
156            Projection::Mercator => 1.0,
157            Projection::VerticalPerspective => VerticalPerspectiveProjection::pixel_scale(view),
158        }
159    }
160
161    /// `circle_radius_correction` extended to a point away from the center,
162    /// by its latitude, blended the same way.
163    #[must_use]
164    pub fn pitched_text_correction(self, view: &View, point: &Point<f64>, tile_id: &UnwrappedTileID) -> f64 {
165        match self {
166            Projection::Mercator => 1.0,
167            Projection::VerticalPerspective => {
168                VerticalPerspectiveProjection::pitched_text_correction(view, point, tile_id)
169            }
170        }
171    }
172}