Skip to main content

mln_engine/render/
tile_matrix.rs

1//! The matrices and projections of a layer's draws of a tile, which their
2//! uniforms hold.
3
4use geometry::Point;
5use glam::{DMat4, DVec2, DVec3, Vec2};
6use mln_gfx::shaders::layer_ubo::ProjectionUBO;
7use mln_style::constants::EXTENT;
8use mln_style::types::TranslateAnchorType;
9
10use crate::camera::{ProjectionData, View};
11use crate::draw::cast_matrix;
12use crate::draw::draws::LayerFrame;
13use crate::geo::constants::M2PI;
14use crate::render::paint_parameters::TransformParameters;
15use crate::render::tile::RenderTile;
16use crate::tile::UnwrappedTileID;
17
18/// The matrix that draws geometry of the tile `tile_id` in the frame of
19/// `frame`: translated by `translation`, anchored as `anchor` says, in
20/// tile units or viewport pixels, from `origin` in the tile where it has
21/// one, and projected with the near-clipped or pixel-aligned projection
22/// matrix where those say so.
23#[expect(
24    clippy::too_many_arguments,
25    reason = "the tile, the frame and the translation, origin, clipping and alignment options of its matrix"
26)]
27pub(crate) fn tile_matrix(
28    tile_id: &UnwrappedTileID,
29    frame: &LayerFrame<'_>,
30    translation: [f32; 2],
31    anchor: TranslateAnchorType,
32    near_clipped: bool,
33    in_viewport_pixel_units: bool,
34    origin: Option<Point<f64>>,
35    aligned: bool,
36) -> DMat4 {
37    tile_matrix_for(
38        tile_id,
39        frame.view,
40        frame.transform_params,
41        translation,
42        anchor,
43        origin,
44        near_clipped,
45        in_viewport_pixel_units,
46        aligned,
47    )
48}
49
50/// The projection data for this tile, of geometry at `origin` in it: the
51/// tile matrix plus the per-tile projection fields.
52#[expect(
53    clippy::too_many_arguments,
54    reason = "the tile, the frame and the translation, origin, clipping and alignment options of its projection"
55)]
56#[must_use]
57pub fn projection_data(
58    tile_id: &UnwrappedTileID,
59    frame: &LayerFrame<'_>,
60    translation: [f32; 2],
61    anchor: TranslateAnchorType,
62    near_clipped: bool,
63    in_viewport_pixel_units: bool,
64    origin: Option<Point<f64>>,
65    aligned: bool,
66) -> ProjectionData {
67    let mut data = frame.view.projection_data_for_matrix(
68        tile_id,
69        &tile_matrix(tile_id, frame, translation, anchor, near_clipped, in_viewport_pixel_units, origin, aligned),
70    );
71    if !in_viewport_pixel_units {
72        let translate = RenderTile::tile_unit_translation(tile_id, translation, anchor, frame.view);
73        data.translate = DVec2::new(f64::from(translate[0]), f64::from(translate[1]));
74    }
75    // A draw's depth range keeps the layers apart, so the shader's depth
76    // offset stays 0.
77    data
78}
79
80/// The block that a program reads `data`, a tile's projection, from.
81#[must_use]
82pub fn to_projection_ubo(data: &ProjectionData) -> ProjectionUBO {
83    // The anchor's offset goes into the matrix in f64, where the camera's
84    // large translation cancels it exactly; the anchor is zero on Mercator.
85    let anchor = data.sphere_anchor;
86    let plane = data.clipping_plane;
87    ProjectionUBO {
88        matrix: cast_matrix(&(data.main_matrix * DMat4::from_translation(anchor))),
89        fallback_matrix: cast_matrix(&data.fallback_matrix),
90        tile_mercator_coords: data.tile_mercator_coords.as_vec4().to_array(),
91        clipping_plane: data.clipping_plane.as_vec4().to_array(),
92        projection_transition: data.projection_transition as f32,
93        depth_offset: data.depth_offset as f32,
94        translate: data.translate.as_vec2().to_array(),
95        anchor: anchor.extend(anchor.dot(plane.truncate()) + plane.w).as_vec4().to_array(),
96        anchor_angles: data.sphere_anchor_angles.as_vec4().to_array(),
97    }
98}
99
100/// Radians per pixel on the sphere for a tile.
101/// `latitude_scale` is the cosine of the center latitude on the globe and 1
102/// on Mercator.
103#[must_use]
104pub fn globe_extrude_scale(tile_id: &UnwrappedTileID, zoom: f32, latitude_scale: f64) -> f32 {
105    (f64::from(tile_id.pixels_to_tile_units(1.0, zoom)) / (f64::from(EXTENT) * (1u64 << tile_id.canonical.z) as f64)
106        * M2PI
107        * latitude_scale) as f32
108}
109
110/// [`tile_matrix`] in `view`, projected with the
111/// matrices of `transform_params`.
112#[expect(
113    clippy::too_many_arguments,
114    reason = "the tile, the transform and the translation, origin, clipping and alignment options of its matrix"
115)]
116pub(crate) fn tile_matrix_for(
117    tile_id: &UnwrappedTileID,
118    view: &View,
119    transform_params: &TransformParameters,
120    translation: [f32; 2],
121    anchor: TranslateAnchorType,
122    origin: Option<Point<f64>>,
123    near_clipped: bool,
124    in_viewport_pixel_units: bool,
125    aligned: bool,
126) -> DMat4 {
127    let mut tile_matrix = view.tile_matrix(tile_id);
128    if let Some(origin) = origin {
129        tile_matrix *= DMat4::from_translation(DVec3::new(origin.x, origin.y, 0.0));
130    }
131    let tile_matrix = with_projection_matrix(&tile_matrix, transform_params, near_clipped, aligned);
132    translate_vtx_matrix(tile_id, &tile_matrix, translation, anchor, view, in_viewport_pixel_units)
133}
134
135/// `matrix` projected with the frame's projection matrix: the pixel-aligned
136/// one if `aligned`, else the near-clipped one if `near_clipped`.
137pub(crate) fn with_projection_matrix(
138    matrix: &DMat4,
139    transform_params: &TransformParameters,
140    near_clipped: bool,
141    aligned: bool,
142) -> DMat4 {
143    // The near-clipped matrix moves the near plane further out, for the
144    // depth buffer's precision.
145    let proj_matrix_ref = if aligned {
146        &transform_params.aligned_proj_matrix
147    } else if near_clipped {
148        &transform_params.near_clipped_proj_matrix
149    } else {
150        &transform_params.proj_matrix
151    };
152    *proj_matrix_ref * *matrix
153}
154
155pub(crate) fn translate_vtx_matrix(
156    id: &UnwrappedTileID,
157    tile_matrix: &DMat4,
158    translation: [f32; 2],
159    anchor: TranslateAnchorType,
160    state: &View,
161    in_viewport_pixel_units: bool,
162) -> DMat4 {
163    if translation[0] == 0.0 && translation[1] == 0.0 {
164        return *tile_matrix;
165    }
166
167    let angle: f32 = if in_viewport_pixel_units {
168        if anchor == TranslateAnchorType::Map { state.angle() as f32 } else { 0.0 }
169    } else if anchor == TranslateAnchorType::Viewport {
170        (-state.angle()) as f32
171    } else {
172        0.0
173    };
174
175    let translate = Vec2::from_angle(angle).rotate(Vec2::new(translation[0], translation[1]));
176
177    let offset = if in_viewport_pixel_units {
178        DVec3::new(f64::from(translate.x), f64::from(translate.y), 0.0)
179    } else {
180        let zoom = state.zoom() as f32;
181        DVec3::new(
182            f64::from(id.pixels_to_tile_units(translate.x, zoom)),
183            f64::from(id.pixels_to_tile_units(translate.y, zoom)),
184            0.0,
185        )
186    };
187    *tile_matrix * DMat4::from_translation(offset)
188}