Skip to main content

mln_engine/render/globe/
tile_mesh.rs

1use mln_style::constants::EXTENT;
2use mln_style::subdivision_granularity::SubdivisionGranularityExpression;
3use mln_style::tile_id::CanonicalTileID;
4
5use crate::render::globe::{NORTH_POLE_Y, SOUTH_POLE_Y};
6
7const EXTENT_STENCIL_BORDER: i32 = EXTENT / 128;
8
9#[derive(Clone, Copy, Debug, PartialEq, Eq)]
10pub(crate) struct TileMeshOptions {
11    /// Quads per axis; 1 is the plain tile quad. Capped so that every index
12    /// fits in 16 bits.
13    pub(crate) granularity: u32,
14    /// Adds a ring of quads `EXTENT / 128` units outside the tile, for
15    /// stencil masks.
16    pub(crate) generate_borders: bool,
17    /// Replaces the north border with geometry reaching the north pole
18    /// vertex.
19    pub(crate) extend_to_north_pole: bool,
20    pub(crate) extend_to_south_pole: bool,
21}
22
23impl Default for TileMeshOptions {
24    fn default() -> Self {
25        TileMeshOptions {
26            granularity: 1,
27            generate_borders: false,
28            extend_to_north_pole: false,
29            extend_to_south_pole: false,
30        }
31    }
32}
33
34/// A grid over a tile on the globe: its vertices' `x, y` in tile units,
35/// and its triangles, counter-clockwise in tile space, of 16-bit indexes.
36/// A vertex at [`NORTH_POLE_Y`] or [`SOUTH_POLE_Y`] is at the pole.
37#[derive(Clone, Debug, Default, PartialEq, Eq)]
38pub struct TileMesh {
39    pub vertices: Vec<[i16; 2]>,
40    pub triangles: Vec<[u16; 3]>,
41}
42
43/// The grid that draws the tile `canonical` on the globe, subdivided as
44/// `granularity` says at its zoom, reaching the poles where it touches
45/// them, and with a border outside the tile if `generate_borders`, for
46/// stencil masks.
47#[must_use]
48pub fn tile_mesh(
49    canonical: &CanonicalTileID,
50    generate_borders: bool,
51    granularity: SubdivisionGranularityExpression,
52) -> TileMesh {
53    create_tile_mesh(TileMeshOptions {
54        granularity: granularity.granularity_for_zoom_level(canonical.z),
55        generate_borders,
56        extend_to_north_pole: canonical.y == 0,
57        extend_to_south_pole: canonical.y == (1u32 << canonical.z) - 1,
58    })
59}
60
61/// A grid mesh covering one tile, optionally with borders and pole caps, as
62/// GL JS's `util/create_tile_mesh.ts` makes it. Triangles are
63/// counter-clockwise in tile space.
64#[must_use]
65pub(crate) fn create_tile_mesh(options: TileMeshOptions) -> TileMesh {
66    let extra_quads_x: i32 = if options.generate_borders { 2 } else { 0 };
67    let extra_quads_y = i32::from(options.extend_to_north_pole || options.generate_borders)
68        + i32::from(options.extend_to_south_pole || options.generate_borders);
69    // At most 256 vertices per axis, so every index fits in 16 bits.
70    let max_granularity = 255 - extra_quads_x.max(extra_quads_y);
71    let granularity = options.granularity.clamp(1, max_granularity as u32) as i32;
72    let quads_per_axis_x = granularity + extra_quads_x;
73    let quads_per_axis_y = granularity + extra_quads_y;
74    let vertices_per_axis_x = quads_per_axis_x + 1;
75    let vertices_per_axis_y = quads_per_axis_y + 1;
76    let offset_x = if options.generate_borders { -1 } else { 0 };
77    let offset_y = if options.generate_borders || options.extend_to_north_pole { -1 } else { 0 };
78    let end_x = granularity + i32::from(options.generate_borders);
79    let end_y = granularity + i32::from(options.generate_borders || options.extend_to_south_pole);
80
81    let mut mesh = TileMesh::default();
82    mesh.vertices.reserve(vertices_per_axis_x as usize * vertices_per_axis_y as usize);
83    for y in offset_y..=end_y {
84        for x in offset_x..=end_x {
85            let mut vx = x * EXTENT / granularity;
86            if x == -1 {
87                vx = -EXTENT_STENCIL_BORDER;
88            }
89            if x == granularity + 1 {
90                vx = EXTENT + EXTENT_STENCIL_BORDER;
91            }
92            let mut vy = y * EXTENT / granularity;
93            if y == -1 {
94                vy = if options.extend_to_north_pole { i32::from(NORTH_POLE_Y) } else { -EXTENT_STENCIL_BORDER };
95            }
96            if y == granularity + 1 {
97                vy =
98                    if options.extend_to_south_pole { i32::from(SOUTH_POLE_Y) } else { EXTENT + EXTENT_STENCIL_BORDER };
99            }
100            mesh.vertices.push([vx as i16, vy as i16]);
101        }
102    }
103
104    mesh.triangles.reserve(quads_per_axis_x as usize * quads_per_axis_y as usize * 2);
105    for y in 0..quads_per_axis_y {
106        for x in 0..quads_per_axis_x {
107            let v0 = (x + y * vertices_per_axis_x) as u16;
108            let v1 = ((x + 1) + y * vertices_per_axis_x) as u16;
109            let v2 = (x + (y + 1) * vertices_per_axis_x) as u16;
110            let v3 = ((x + 1) + (y + 1) * vertices_per_axis_x) as u16;
111            mesh.triangles.extend([[v0, v2, v1], [v1, v2, v3]]);
112        }
113    }
114    mesh
115}