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

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

symbol_globe.vert97 lines · 3.9 KiBRaw
1#version 450
2
3// Globe symbol vertex: label quads bent onto the sphere at their anchor.
4//
5// Same inputs as `symbol_billboard.vert` (tile-local position + uv + ground
6// anchor). The globe record path pushes the globe matrix (translation column =
7// centre lon/lat, third row = tile ox/oy/span) with `misc`/`morph` keeping
8// their flat meaning: `misc.x` is the tile's world-px span (Dp), `misc.yzw`
9// the halo rgb exactly as on the flat path, `line` the text size/halo/sdf/
10// billboard flag, and `morph` the globe half-viewport + radius (see above).
11// This shader bends the anchor through the CPU-matching `globe_point` basis
12// and hangs the glyph quad off it at its screen offset — upright labels on a
13// curved planet, with halo intact. Curved (line) labels carry each vertex as
14// sphere exactly as they lie flat on the plane. Screen y follows the CPU
15// anchor convention (`halfH - y`, north up).
16layout(location = 0) in vec2 inPosition;
17layout(location = 1) in vec2 inUv;
18layout(location = 2) in vec2 inAnchor;
19layout(location = 3) in float inAnchorH;
20layout(location = 0) out vec2 outUv;
21layout(location = 1) out float outZ;
22layout(push_constant) uniform Push {
23 mat4 tileToClip;
24 vec4 color;
25 vec4 line;
26 vec4 misc;
27 vec4 morph;
28} push;
29
30const float PI = 3.141592653589793;
31
32// Camera view rotation for the globe: m[3]/m[7] of the globe matrix carry
33// the bearing/pitch (degrees). Must match `camera::globe_view_rotate`.
34vec3 globeView(vec3 pt) {
35 float vb = radians(push.tileToClip[0][3]);
36 float vp = radians(push.tileToClip[1][3]);
37 if (vb == 0.0 && vp == 0.0) return pt;
38 float x1 = cos(vb) * pt.x - sin(vb) * pt.y;
39 float y1 = sin(vb) * pt.x + cos(vb) * pt.y;
40 return vec3(x1, cos(vp) * y1 - sin(vp) * pt.z, sin(vp) * y1 + cos(vp) * pt.z);
41}
42
43
44// Must match `camera::globe_point`.
45vec3 globeOf(float lon, float lat, float cLon, float cLat) {
46 float latR = radians(lat);
47 float dLon = radians(lon - cLon);
48 float cLatR = radians(cLat);
49 float sy = sin(cLatR);
50 float cy = cos(cLatR);
51 return vec3(
52 cos(latR) * sin(dLon),
53 cy * sin(latR) - sy * cos(latR) * cos(dLon),
54 sy * sin(latR) + cy * cos(latR) * cos(dLon));
55}
56
57void main() {
58 outUv = inUv;
59 float ox = push.tileToClip[0][2];
60 float oy = push.tileToClip[1][2];
61 float span = push.tileToClip[2][2];
62 // Anchor tile-local -> lon/lat.
63 float awx = ox + inAnchor.x * span;
64 float awy = oy + inAnchor.y * span;
65 float alon = awx * 360.0 - 180.0;
66 float an = PI - 2.0 * PI * awy;
67 float alat = atan((exp(an) - exp(-an)) * 0.5) * 180.0 / PI;
68 // Sphere basis from the matrix translation column (CPU-side centre).
69 float cLon = push.tileToClip[3][0];
70 float cLat = push.tileToClip[3][1];
71 vec3 a = globeOf(alon, alat, cLon, cLat);
72 float r = push.morph.z;
73 float kx = push.tileToClip[0][0] / max(r, 1e-6);
74 float ky = push.tileToClip[1][1] / max(r, 1e-6);
75 float hw = push.morph.x;
76 float hh = push.morph.y;
77 // Screen offset of this corner from its anchor. Labels are shaped in flat tile-local
78 // units, where one unit is `span * world` Dp (`world` rides in m[14]); the globe 2x2 is
79 // `k * r`, so the delta is scaled by `span * world / r` to keep the flat screen size.
80 float m00 = push.tileToClip[0][0];
81 float m01 = push.tileToClip[0][1];
82 float m10 = push.tileToClip[1][0];
83 float m11 = push.tileToClip[1][1];
84 vec2 dTile = (inPosition - inAnchor) * (span * push.tileToClip[3][2] / max(r, 1e-6));
85 vec2 off = vec2(m00 * dTile.x + m10 * dTile.y, m01 * dTile.x + m11 * dTile.y);
86 vec3 v = globeView(a);
87 float ax = kx * (v.x * r) + (kx * hw - 1.0);
88 float ay = -(ky * (v.y * r)) + (ky * hh - 1.0);
89 float depth = clamp(1.0 - v.z, 0.0, 1.0);
90 outZ = v.z;
91 if (v.z < 0.0) {
92 gl_Position = vec4(ax, ay, 1.0, 1.0);
93 } else {
94 // Tile +y and clip +y both point down the screen, so the offset adds unflipped.
95 gl_Position = vec4(ax + off.x, ay + off.y, depth, 1.0);
96 }
97}