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

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

road_surface_globe.vert79 lines · 3.1 KiBRaw
1#version 450
2// Globe ribbon: `road_surface.vert` extrusion, then the globe bend.
3//
4// Same contract as `line_globe.vert` (which see for the push layout): the
5// carriageway width/offsets are screen measurements resolved in tile-local
6// space exactly as the flat ribbon does, and only the final transform bends
7// onto the sphere. Markings readouts (`outT`, `outDistancePx`) pass through
8// untouched — `road_surface.frag` logic is shared with the limb discard added
9// (see `road_surface_globe.frag`). `misc`/`morph` keep their flat meaning:
10// tile px in `misc.x`, edge AA in `misc.y`, yellow-centre flag in `misc.z`,
11// and `morph` is (fade, halfW, radius, halfH) on the globe branch.
12layout(location = 0) in vec2 inPosition;
13layout(location = 1) in vec2 inNormal;
14layout(location = 2) in float inT;
15layout(location = 3) in float inDistance;
16layout(location = 4) in float inZ;
17layout(location = 0) out float outT;
18layout(location = 1) out float outDistancePx;
19layout(location = 2) out float outZ;
20layout(push_constant) uniform Push {
21 mat4 tileToClip;
22 vec4 color;
23 vec4 line;
24 vec4 misc;
25 vec4 morph;
26} push;
27
28const float MIN_HALF_WIDTH_PX = 0.5;
29const float PI = 3.141592653589793;
30
31void main() {
32 float ox = push.tileToClip[0][2];
33 float oy = push.tileToClip[1][2];
34 float span = push.tileToClip[2][2];
35 // The globe's own px per tile unit: see `line_globe.vert`.
36 float n0 = PI - 2.0 * PI * (oy + inPosition.y * span);
37 float worldDp = max(push.tileToClip[3][2], 1e-6);
38 float tilePx = max(push.misc.x, 1.0)
39 * (2.0 * PI * push.morph.z * max(1.0 / cosh(n0), 0.05) / worldDp);
40 float wantHalfPx = push.line.x;
41 float halfWidthPx = max(wantHalfPx, MIN_HALF_WIDTH_PX);
42 vec2 offsetTile = inNormal * (inT * halfWidthPx / tilePx);
43 vec2 bent = inPosition + offsetTile;
44
45 float wx = ox + bent.x * span;
46 float wy = oy + bent.y * span;
47 float lon = wx * 360.0 - 180.0;
48 float n = PI - 2.0 * PI * wy;
49 float lat = atan((exp(n) - exp(-n)) * 0.5) * 180.0 / PI;
50 // Sphere basis from the matrix translation column (CPU-side centre).
51 float cLon = push.tileToClip[3][0];
52 float cLat = push.tileToClip[3][1];
53 float latR = radians(lat);
54 float dLon = radians(lon - cLon);
55 float cLatR = radians(cLat);
56 // Must match `camera::globe_point`.
57 float sy = sin(cLatR);
58 float cy = cos(cLatR);
59 float x = cos(latR) * sin(dLon);
60 float y = cy * sin(latR) - sy * cos(latR) * cos(dLon);
61 float z = sy * sin(latR) + cy * cos(latR) * cos(dLon);
62 float r = push.morph.z;
63 float kx = push.tileToClip[0][0] / max(r, 1e-6);
64 float ky = push.tileToClip[1][1] / max(r, 1e-6);
65 float hw = push.morph.y;
66 float hh = push.morph.w;
67 // Screen point y-up like the CPU anchor math (`halfH - y`): north renders up.
68 float cx = kx * (x * r) + (kx * hw - 1.0);
69 float cyy = -(ky * (y * r)) + (ky * hh - 1.0);
70 float depth = clamp(1.0 - z, 0.0, 1.0);
71 outT = inT;
72 outZ = z;
73 outDistancePx = inDistance * tilePx;
74 if (z < 0.0) {
75 gl_Position = vec4(cx, cyy, 1.0, 1.0);
76 } else {
77 gl_Position = vec4(cx, cyy, depth, 1.0);
78 }
79}