Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / library / map / src / main / rust / shaders / road_surface_globe.frag
road_surface_globe.frag84 lines · 3.2 KiBRaw
| 1 | #version 450 |
| 2 | // Globe ribbon fragment: `road_surface.frag` plus the limb discard. |
| 3 | // |
| 4 | // `road_surface.frag` is shared verbatim for the markings math — this file is |
| 5 | // a copy with one varying added (`inZ`, the unit-sphere z) and the two early |
| 6 | // discards. Kept as a separate file rather than a `#ifdef` so the flat |
| 7 | // pipeline compiles bit-identical shaders whether or not the globe exists. |
| 8 | layout(location = 0) in float inT; |
| 9 | layout(location = 1) in float inDistancePx; |
| 10 | layout(location = 2) in float inZ; |
| 11 | layout(location = 0) out vec4 outColor; |
| 12 | layout(push_constant) uniform Push { |
| 13 | mat4 tileToClip; |
| 14 | vec4 color; |
| 15 | vec4 line; |
| 16 | vec4 misc; |
| 17 | vec4 morph; |
| 18 | } push; |
| 19 | |
| 20 | const float MIN_HALF_WIDTH_PX = 0.5; |
| 21 | const float NO_MARKINGS_BELOW = -1.5; |
| 22 | const float MARK_WIDTH_LANES = 0.045; |
| 23 | const float MIN_MARK_HALF_PX = 0.5; |
| 24 | const float MARK_FADE_START_PX = 4.0; |
| 25 | const float MARK_FADE_FULL_PX = 10.0; |
| 26 | const float DASH_ON_LANES = 0.9; |
| 27 | const float DASH_OFF_LANES = 2.6; |
| 28 | const vec3 MARK_WHITE = vec3(0.92, 0.92, 0.90); |
| 29 | const vec3 MARK_YELLOW = vec3(0.95, 0.76, 0.18); |
| 30 | |
| 31 | float band(float distancePx, float halfPx) { |
| 32 | return clamp(halfPx - distancePx + 0.5, 0.0, 1.0); |
| 33 | } |
| 34 | |
| 35 | void main() { |
| 36 | if (inZ < 0.0) discard; |
| 37 | float wantHalfPx = push.line.x; |
| 38 | float halfWidthPx = max(wantHalfPx, MIN_HALF_WIDTH_PX); |
| 39 | float coverage = clamp(wantHalfPx / halfWidthPx, 0.0, 1.0); |
| 40 | float edgePx = inT * halfWidthPx; |
| 41 | coverage *= mix(1.0, band(abs(edgePx), halfWidthPx), push.misc.y); |
| 42 | coverage *= clamp(inZ * 8.0, 0.0, 1.0); |
| 43 | if (coverage <= 0.0) discard; |
| 44 | |
| 45 | float lanes = max(push.line.y, 1.0); |
| 46 | float lanePx = 2.0 * halfWidthPx / lanes; |
| 47 | float legible = |
| 48 | clamp((lanePx - MARK_FADE_START_PX) / (MARK_FADE_FULL_PX - MARK_FADE_START_PX), 0.0, 1.0); |
| 49 | |
| 50 | vec3 rgb = push.color.rgb; |
| 51 | if (legible > 0.0 && push.line.z > NO_MARKINGS_BELOW) { |
| 52 | float markHalfPx = max(lanePx * MARK_WIDTH_LANES * 0.5, MIN_MARK_HALF_PX); |
| 53 | bool oneway = push.line.w >= 0.5; |
| 54 | float centreT = push.line.z; |
| 55 | |
| 56 | float lane = (inT * 0.5 + 0.5) * lanes; |
| 57 | float boundary = round(lane); |
| 58 | float boundaryT = boundary / lanes * 2.0 - 1.0; |
| 59 | bool interior = boundary > 0.5 && boundary < lanes - 0.5; |
| 60 | bool isCentre = !oneway && abs(boundaryT - centreT) < 1.0 / lanes; |
| 61 | |
| 62 | float dashed = 0.0; |
| 63 | if (interior && !isCentre) { |
| 64 | float on = DASH_ON_LANES * lanePx; |
| 65 | float period = on + DASH_OFF_LANES * lanePx; |
| 66 | float along = mod(inDistancePx, period); |
| 67 | dashed = clamp(min(along, on - along) + 0.5, 0.0, 1.0); |
| 68 | dashed *= band(abs(lane - boundary) * lanePx, markHalfPx); |
| 69 | } |
| 70 | rgb = mix(rgb, MARK_WHITE, dashed * legible); |
| 71 | |
| 72 | float edgeCentrePx = max(halfWidthPx - markHalfPx, 0.0); |
| 73 | float edgeLine = band(abs(abs(edgePx) - edgeCentrePx), markHalfPx); |
| 74 | rgb = mix(rgb, MARK_WHITE, edgeLine * legible); |
| 75 | |
| 76 | if (!oneway) { |
| 77 | float centreLine = band(abs(edgePx - centreT * halfWidthPx), markHalfPx); |
| 78 | vec3 centreRgb = push.misc.z >= 0.5 ? MARK_YELLOW : MARK_WHITE; |
| 79 | rgb = mix(rgb, centreRgb, centreLine * legible); |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | outColor = vec4(rgb, push.color.a * coverage); |
| 84 | } |