Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / library / map / src / main / rust / shaders / symbol_globe.vert
symbol_globe.vert84 lines · 3.4 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). |
| 16 | layout(location = 0) in vec2 inPosition; |
| 17 | layout(location = 1) in vec2 inUv; |
| 18 | layout(location = 2) in vec2 inAnchor; |
| 19 | layout(location = 3) in float inAnchorH; |
| 20 | layout(location = 0) out vec2 outUv; |
| 21 | layout(location = 1) out float outZ; |
| 22 | layout(push_constant) uniform Push { |
| 23 | mat4 tileToClip; |
| 24 | vec4 color; |
| 25 | vec4 line; |
| 26 | vec4 misc; |
| 27 | vec4 morph; |
| 28 | } push; |
| 29 | |
| 30 | const float PI = 3.141592653589793; |
| 31 | |
| 32 | // Must match `camera::globe_point`. |
| 33 | vec3 globeOf(float lon, float lat, float cLon, float cLat) { |
| 34 | float latR = radians(lat); |
| 35 | float dLon = radians(lon - cLon); |
| 36 | float cLatR = radians(cLat); |
| 37 | float sy = sin(cLatR); |
| 38 | float cy = cos(cLatR); |
| 39 | return vec3( |
| 40 | cos(latR) * sin(dLon), |
| 41 | cy * sin(latR) - sy * cos(latR) * cos(dLon), |
| 42 | sy * sin(latR) + cy * cos(latR) * cos(dLon)); |
| 43 | } |
| 44 | |
| 45 | void main() { |
| 46 | outUv = inUv; |
| 47 | float ox = push.tileToClip[0][2]; |
| 48 | float oy = push.tileToClip[1][2]; |
| 49 | float span = push.tileToClip[2][2]; |
| 50 | // Anchor tile-local -> lon/lat. |
| 51 | float awx = ox + inAnchor.x * span; |
| 52 | float awy = oy + inAnchor.y * span; |
| 53 | float alon = awx * 360.0 - 180.0; |
| 54 | float an = PI - 2.0 * PI * awy; |
| 55 | float alat = atan((exp(an) - exp(-an)) * 0.5) * 180.0 / PI; |
| 56 | // Sphere basis from the matrix translation column (CPU-side centre). |
| 57 | float cLon = push.tileToClip[3][0]; |
| 58 | float cLat = push.tileToClip[3][1]; |
| 59 | vec3 a = globeOf(alon, alat, cLon, cLat); |
| 60 | float r = push.morph.z; |
| 61 | float kx = push.tileToClip[0][0] / max(r, 1e-6); |
| 62 | float ky = push.tileToClip[1][1] / max(r, 1e-6); |
| 63 | float hw = push.morph.x; |
| 64 | float hh = push.morph.y; |
| 65 | // Screen offset of this corner from its anchor. Labels are shaped in flat tile-local |
| 66 | // units, where one unit is `span * world` Dp (`world` rides in m[14]); the globe 2x2 is |
| 67 | // `k * r`, so the delta is scaled by `span * world / r` to keep the flat screen size. |
| 68 | float m00 = push.tileToClip[0][0]; |
| 69 | float m01 = push.tileToClip[0][1]; |
| 70 | float m10 = push.tileToClip[1][0]; |
| 71 | float m11 = push.tileToClip[1][1]; |
| 72 | vec2 dTile = (inPosition - inAnchor) * (span * push.tileToClip[3][2] / max(r, 1e-6)); |
| 73 | vec2 off = vec2(m00 * dTile.x + m10 * dTile.y, m01 * dTile.x + m11 * dTile.y); |
| 74 | float ax = kx * (a.x * r) + (kx * hw - 1.0); |
| 75 | float ay = -(ky * (a.y * r)) + (ky * hh - 1.0); |
| 76 | float depth = clamp(1.0 - a.z, 0.0, 1.0); |
| 77 | outZ = a.z; |
| 78 | if (a.z < 0.0) { |
| 79 | gl_Position = vec4(ax, ay, 1.0, 1.0); |
| 80 | } else { |
| 81 | // Tile +y and clip +y both point down the screen, so the offset adds unflipped. |
| 82 | gl_Position = vec4(ax + off.x, ay + off.y, depth, 1.0); |
| 83 | } |
| 84 | } |