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