Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / library / map / src / main / rust / shaders / road_surface_globe.vert
road_surface_globe.vert92 lines · 3.6 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. |
| 12 | layout(location = 0) in vec2 inPosition; |
| 13 | layout(location = 1) in vec2 inNormal; |
| 14 | layout(location = 2) in float inT; |
| 15 | layout(location = 3) in float inDistance; |
| 16 | layout(location = 4) in float inZ; |
| 17 | layout(location = 0) out float outT; |
| 18 | layout(location = 1) out float outDistancePx; |
| 19 | layout(location = 2) out float outZ; |
| 20 | layout(push_constant) uniform Push { |
| 21 | mat4 tileToClip; |
| 22 | vec4 color; |
| 23 | vec4 line; |
| 24 | vec4 misc; |
| 25 | vec4 morph; |
| 26 | } push; |
| 27 | |
| 28 | const float MIN_HALF_WIDTH_PX = 0.5; |
| 29 | const float PI = 3.141592653589793; |
| 30 | |
| 31 | // Camera view rotation for the globe: m[3]/m[7] of the globe matrix carry |
| 32 | // the bearing/pitch (degrees). Must match `camera::globe_view_rotate`. |
| 33 | vec3 globeView(vec3 pt) { |
| 34 | float vb = radians(push.tileToClip[0][3]); |
| 35 | float vp = radians(push.tileToClip[1][3]); |
| 36 | if (vb == 0.0 && vp == 0.0) return pt; |
| 37 | float x1 = cos(vb) * pt.x - sin(vb) * pt.y; |
| 38 | float y1 = sin(vb) * pt.x + cos(vb) * pt.y; |
| 39 | return vec3(x1, cos(vp) * y1 - sin(vp) * pt.z, sin(vp) * y1 + cos(vp) * pt.z); |
| 40 | } |
| 41 | |
| 42 | |
| 43 | void main() { |
| 44 | float ox = push.tileToClip[0][2]; |
| 45 | float oy = push.tileToClip[1][2]; |
| 46 | float span = push.tileToClip[2][2]; |
| 47 | // The globe's own px per tile unit: see `line_globe.vert`. |
| 48 | float n0 = PI - 2.0 * PI * (oy + inPosition.y * span); |
| 49 | float worldDp = max(push.tileToClip[3][2], 1e-6); |
| 50 | float tilePx = max(push.misc.x, 1.0) |
| 51 | * (2.0 * PI * push.morph.z * max(1.0 / cosh(n0), 0.05) / worldDp); |
| 52 | float wantHalfPx = push.line.x; |
| 53 | float halfWidthPx = max(wantHalfPx, MIN_HALF_WIDTH_PX); |
| 54 | vec2 offsetTile = inNormal * (inT * halfWidthPx / 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 | vec3 v = globeView(vec3(x, y, z)); |
| 80 | // Screen point y-up like the CPU anchor math (`halfH - y`): north renders up. |
| 81 | float cx = kx * (v.x * r) + (kx * hw - 1.0); |
| 82 | float cyy = -(ky * (v.y * r)) + (ky * hh - 1.0); |
| 83 | float depth = clamp(1.0 - v.z, 0.0, 1.0); |
| 84 | outT = inT; |
| 85 | outZ = v.z; |
| 86 | outDistancePx = inDistance * tilePx; |
| 87 | if (v.z < 0.0) { |
| 88 | gl_Position = vec4(cx, cyy, 1.0, 1.0); |
| 89 | } else { |
| 90 | gl_Position = vec4(cx, cyy, depth, 1.0); |
| 91 | } |
| 92 | } |