Modern Apps git
git clone https://git.maproj.org/Modern-Apps.git
translate ▾

Modern-Apps.git / library / map / src / main / rust / shaders / fill_globe.vert

fill_globe.vert88 lines · 3.8 KiBRaw
1#version 450
2
3// Globe fill: bends flat tile-local triangles onto the orthographic sphere.
4//
5// Same inputs as `fill.vert` (position-only tile-local 0..1 in `inPosition.xy`),
6// but the matrix is the globe matrix from `Camera::globe_tile_to_clip`, not the
7// flat tile matrix: its linear 2x2 carries the globe radius (Dp), its third
8// row carries the tile's normalised-world origin + span (ox, oy, span), and its
9// translation column carries the camera centre lon/lat (degrees, m[12]/m[13])
10// for the sphere basis. The vertex reconstructs its lon/lat from those,
11// projects through the CPU-matching `globe_point` basis, and emits clip + a
12// real depth (near-side nearer).
13//
14// `misc`/`morph` keep their flat meaning (tile px in `misc.x`, edge AA in
15// `misc.y`, tile fade in `morph.x`, draped-z scale in `morph.z`; `morph.y`
16// keeps the dash phase speed the line fragment reads) — the limb discard is
17// the only fragment difference from `fill.frag`, so opacity, fade and coverage
18// match the flat map exactly. Far-side vertices (z < 0) are emitted at the far
19// plane and the fragment stage discards wherever the interpolated z is negative,
20// so straddling triangles cut cleanly at the limb. The pipeline is depth-off
21// (layer order composites, as on the flat map), so the depth written below is
22// inert. Screen y follows the CPU anchor convention (`halfH - y`, north up).
23
24layout(location = 0) in vec3 inPosition;
25
26layout(push_constant) uniform Push {
27 mat4 tileToClip;
28 vec4 color;
29 vec4 line;
30 vec4 misc;
31 vec4 morph;
32} push;
33
34layout(location = 0) out float outZ;
35
36const float PI = 3.141592653589793;
37
38// `globe_tile_to_clip` packs (ox, oy, span) into m[2], m[6], m[10]: row 2 of
39// columns 0..2, i.e. `tileToClip[0][2]`, `[1][2]`, `[2][2]` in GLSL.
40// Translation column (m[12]/m[13]): centre lon/lat in degrees.
41
42void main() {
43 float ox = push.tileToClip[0][2];
44 float oy = push.tileToClip[1][2];
45 float span = push.tileToClip[2][2];
46 // Tile-local (u, v) -> normalised world (0..1 across the planet).
47 float wx = ox + inPosition.x * span;
48 float wy = oy + inPosition.y * span;
49 // Normalised world -> lon/lat (Web Mercator inverse, y down).
50 float lon = wx * 360.0 - 180.0;
51 float n = PI - 2.0 * PI * wy;
52 // atan(sinh(n)): wy is in [0,1] so n in [-PI, PI] and sinh is bounded.
53 float lat = atan((exp(n) - exp(-n)) * 0.5) * 180.0 / PI;
54 // Sphere basis from the matrix translation column (CPU-side centre).
55 float cLon = push.tileToClip[3][0];
56 float cLat = push.tileToClip[3][1];
57 float latR = radians(lat);
58 float dLon = radians(lon - cLon);
59 float cLatR = radians(cLat);
60 // Must match `camera::globe_point`.
61 float sy = sin(cLatR);
62 float cy = cos(cLatR);
63 float x = cos(latR) * sin(dLon);
64 float y = cy * sin(latR) - sy * cos(latR) * cos(dLon);
65 float z = sy * sin(latR) + cy * cos(latR) * cos(dLon);
66 // Globe radius (Dp) from the matrix scale: m[0] = kx * r with kx = 2/width.
67 // The record path pushes morph = (fade, halfW, r, halfH) on the globe branch.
68 float hw = push.morph.y;
69 float hh = push.morph.w;
70 float r = push.morph.z;
71 float kx = push.tileToClip[0][0] / max(r, 1e-6);
72 float ky = push.tileToClip[1][1] / max(r, 1e-6);
73 // Screen point in Dp: centre + sphere offset, y-up like the CPU anchor math
74 // (`halfH - y`), so north renders up.
75 float sx = x * r;
76 float sy2 = y * r;
77 float cx = kx * sx + (kx * hw - 1.0);
78 float cyy = -(ky * sy2) + (ky * hh - 1.0);
79 // Depth: sub-camera point (z=1) -> 0, limb (z=0) -> 1, so nearer wins LESS.
80 float depth = clamp(1.0 - z, 0.0, 1.0);
81 outZ = z;
82 if (z < 0.0) {
83 // Far side: emit at far depth so the triangle clips away.
84 gl_Position = vec4(cx, cyy, 1.0, 1.0);
85 } else {
86 gl_Position = vec4(cx, cyy, depth, 1.0);
87 }
88}