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

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

line_globe.vert90 lines · 3.9 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
35void main() {
36 float ox = push.tileToClip[0][2];
37 float oy = push.tileToClip[1][2];
38 float span = push.tileToClip[2][2];
39 // `misc.x` is the flat tile's px span (span * world). On the ball one tile unit spans
40 // 2 * pi * r * cos(lat) * span Dp in both directions (Mercator is conformal), so widths
41 // and dash lengths convert through that span instead. `world` (Dp) rides in the globe
42 // matrix's m[14]; cos(lat) = 1 / cosh(n) for the Mercator n of the unbent position.
43 float n0 = PI - 2.0 * PI * (oy + inPosition.y * span);
44 float worldDp = max(push.tileToClip[3][2], 1e-6);
45 float tilePx = max(push.misc.x, 1.0)
46 * (2.0 * PI * push.morph.z * max(1.0 / cosh(n0), 0.05) / worldDp);
47 float wantHalfPx = push.line.x;
48 float gapHalfPx = push.line.y;
49 float lateralPx = push.misc.z;
50 float halfWidthPx = max(wantHalfPx, MIN_HALF_WIDTH_PX);
51 float offsetPx = inExtrude.x * gapHalfPx + inExtrude.y * halfWidthPx + lateralPx;
52 float centrePx = inExtrude.x * gapHalfPx + sign(inExtrude.x) * halfWidthPx + lateralPx;
53 outEdgePx = offsetPx - centrePx;
54 vec2 offsetTile = inNormal * (offsetPx / tilePx);
55 vec2 bent = inPosition + offsetTile;
56
57 float wx = ox + bent.x * span;
58 float wy = oy + bent.y * span;
59 float lon = wx * 360.0 - 180.0;
60 float n = PI - 2.0 * PI * wy;
61 float lat = atan((exp(n) - exp(-n)) * 0.5) * 180.0 / PI;
62 // Sphere basis from the matrix translation column (CPU-side centre).
63 float cLon = push.tileToClip[3][0];
64 float cLat = push.tileToClip[3][1];
65 float latR = radians(lat);
66 float dLon = radians(lon - cLon);
67 float cLatR = radians(cLat);
68 // Must match `camera::globe_point`.
69 float sy = sin(cLatR);
70 float cy = cos(cLatR);
71 float x = cos(latR) * sin(dLon);
72 float y = cy * sin(latR) - sy * cos(latR) * cos(dLon);
73 float z = sy * sin(latR) + cy * cos(latR) * cos(dLon);
74 float r = push.morph.z;
75 float kx = push.tileToClip[0][0] / max(r, 1e-6);
76 float ky = push.tileToClip[1][1] / max(r, 1e-6);
77 float hw = push.morph.y;
78 float hh = push.morph.w;
79 // Screen point y-up like the CPU anchor math (`halfH - y`): north renders up.
80 float cx = kx * (x * r) + (kx * hw - 1.0);
81 float cyy = -(ky * (y * r)) + (ky * hh - 1.0);
82 float depth = clamp(1.0 - z, 0.0, 1.0);
83 outZ = z;
84 outDistancePx = inDistance * tilePx;
85 if (z < 0.0) {
86 gl_Position = vec4(cx, cyy, 1.0, 1.0);
87 } else {
88 gl_Position = vec4(cx, cyy, depth, 1.0);
89 }
90}