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

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

line_globe.vert103 lines · 4.4 KiBRaw
1#version 450
2// Globe line: same screen-space extrusion as `line.vert`, then the globe bend.
3//
4// The offset math (casing bands, lateral shift, sub-pixel floor) is verbatim
5// from `line.vert` — a width is a screen measurement either way. The difference
6// is the final transform: instead of the flat tile matrix, the extruded
7// tile-local point is bent onto the orthographic sphere by the same
8// lon/lat -> unit-sphere -> clip derivation `fill_globe.vert` uses. Draped
9// height (`inZ`) is ignored on the globe: relief at globe zoom is sub-pixel.
10//
11// Push layout: identical block to the flat path. `tileToClip` is the globe
12// matrix (2x2 = globe scale, row 2 = tile ox/oy/span, translation column =
13// centre lon/lat degrees), `misc.xyzw` keep their flat meaning (tile px,
14// edge AA, lateral shift, clock). `morph` is (fade, halfW, radius, halfH) on
15// the globe branch, as `record.rs` pushes it.
16layout(location = 0) in vec2 inPosition;
17layout(location = 1) in vec2 inNormal;
18layout(location = 2) in vec2 inExtrude;
19layout(location = 3) in float inDistance;
20layout(location = 4) in float inZ;
21layout(location = 0) out float outDistancePx;
22layout(location = 1) out float outEdgePx;
23layout(location = 2) out float outZ;
24layout(push_constant) uniform Push {
25 mat4 tileToClip;
26 vec4 color;
27 vec4 line;
28 vec4 misc;
29 vec4 morph;
30} push;
31
32const float MIN_HALF_WIDTH_PX = 0.5;
33const float PI = 3.141592653589793;
34
35// Camera view rotation for the globe: m[3]/m[7] of the globe matrix carry
36// the bearing/pitch (degrees). Must match `camera::globe_view_rotate`.
37vec3 globeView(vec3 pt) {
38 float vb = radians(push.tileToClip[0][3]);
39 float vp = radians(push.tileToClip[1][3]);
40 if (vb == 0.0 && vp == 0.0) return pt;
41 float x1 = cos(vb) * pt.x - sin(vb) * pt.y;
42 float y1 = sin(vb) * pt.x + cos(vb) * pt.y;
43 return vec3(x1, cos(vp) * y1 - sin(vp) * pt.z, sin(vp) * y1 + cos(vp) * pt.z);
44}
45
46
47void main() {
48 float ox = push.tileToClip[0][2];
49 float oy = push.tileToClip[1][2];
50 float span = push.tileToClip[2][2];
51 // `misc.x` is the flat tile's px span (span * world). On the ball one tile unit spans
52 // 2 * pi * r * cos(lat) * span Dp in both directions (Mercator is conformal), so widths
53 // and dash lengths convert through that span instead. `world` (Dp) rides in the globe
54 // matrix's m[14]; cos(lat) = 1 / cosh(n) for the Mercator n of the unbent position.
55 float n0 = PI - 2.0 * PI * (oy + inPosition.y * span);
56 float worldDp = max(push.tileToClip[3][2], 1e-6);
57 float tilePx = max(push.misc.x, 1.0)
58 * (2.0 * PI * push.morph.z * max(1.0 / cosh(n0), 0.05) / worldDp);
59 float wantHalfPx = push.line.x;
60 float gapHalfPx = push.line.y;
61 float lateralPx = push.misc.z;
62 float halfWidthPx = max(wantHalfPx, MIN_HALF_WIDTH_PX);
63 float offsetPx = inExtrude.x * gapHalfPx + inExtrude.y * halfWidthPx + lateralPx;
64 float centrePx = inExtrude.x * gapHalfPx + sign(inExtrude.x) * halfWidthPx + lateralPx;
65 outEdgePx = offsetPx - centrePx;
66 vec2 offsetTile = inNormal * (offsetPx / tilePx);
67 vec2 bent = inPosition + offsetTile;
68
69 float wx = ox + bent.x * span;
70 float wy = oy + bent.y * span;
71 float lon = wx * 360.0 - 180.0;
72 float n = PI - 2.0 * PI * wy;
73 float lat = atan((exp(n) - exp(-n)) * 0.5) * 180.0 / PI;
74 // Sphere basis from the matrix translation column (CPU-side centre).
75 float cLon = push.tileToClip[3][0];
76 float cLat = push.tileToClip[3][1];
77 float latR = radians(lat);
78 float dLon = radians(lon - cLon);
79 float cLatR = radians(cLat);
80 // Must match `camera::globe_point`.
81 float sy = sin(cLatR);
82 float cy = cos(cLatR);
83 float x = cos(latR) * sin(dLon);
84 float y = cy * sin(latR) - sy * cos(latR) * cos(dLon);
85 float z = sy * sin(latR) + cy * cos(latR) * cos(dLon);
86 float r = push.morph.z;
87 float kx = push.tileToClip[0][0] / max(r, 1e-6);
88 float ky = push.tileToClip[1][1] / max(r, 1e-6);
89 float hw = push.morph.y;
90 float hh = push.morph.w;
91 vec3 v = globeView(vec3(x, y, z));
92 // Screen point y-up like the CPU anchor math (`halfH - y`): north renders up.
93 float cx = kx * (v.x * r) + (kx * hw - 1.0);
94 float cyy = -(ky * (v.y * r)) + (ky * hh - 1.0);
95 float depth = clamp(1.0 - v.z, 0.0, 1.0);
96 outZ = v.z;
97 outDistancePx = inDistance * tilePx;
98 if (v.z < 0.0) {
99 gl_Position = vec4(cx, cyy, 1.0, 1.0);
100 } else {
101 gl_Position = vec4(cx, cyy, depth, 1.0);
102 }
103}