Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / library / map / src / main / rust / shaders / line.frag
line.frag73 lines · 3.7 KiBRaw
| 1 | #version 450 |
| 2 | // Line fragment: edge coverage, then the dash pattern. |
| 3 | // |
| 4 | // `outEdgePx` is a signed pixel distance from the band's centreline, and `misc.y` says |
| 5 | // whether this pass has to antialias the edge itself. Under MSAA it does not: the |
| 6 | // rasteriser already resolves partial coverage, and a second coverage term on top fades |
| 7 | // a diagonal road twice - at z6 a 2.9px highway crossing at an angle kept one pixel of |
| 8 | // strength against three for the near-vertical stretches of the same road. Without MSAA |
| 9 | // the box filter is the only thing standing between a diagonal road and a staircase, so |
| 10 | // it stays for that case. |
| 11 | // |
| 12 | // The sub-pixel fade is separate and always applies: `line.vert` widens anything thinner |
| 13 | // than MIN_HALF_WIDTH_PX so the rasteriser can hit it, and the alpha here gives that width |
| 14 | // back. MSAA cannot do this - the geometry really is a pixel wide by then. |
| 15 | // |
| 16 | // `dashOn`/`dashOff` are dash and gap lengths in **line widths**, as the style spec |
| 17 | // defines `line-dasharray`, converted to pixels here. |
| 18 | // |
| 19 | // The degenerate `[2, 0]` pattern has to be special-cased. `boundaries_country` |
| 20 | // produces it through a `step` expression - a zero-length gap means "solid" - and a |
| 21 | // naive `mod(distance, on + off) < on` renders it solid only by accident of `off` |
| 22 | // being exactly zero. Any drift in the period, or a driver that evaluates |
| 23 | // `mod(x, x)` as `x` rather than `0`, turns the whole border into stipple. So a |
| 24 | // non-positive gap short-circuits before any arithmetic happens. |
| 25 | layout(location = 0) in float inDistancePx; |
| 26 | layout(location = 1) in float inEdgePx; |
| 27 | layout(location = 0) out vec4 outColor; |
| 28 | layout(push_constant) uniform Push { |
| 29 | mat4 tileToClip; |
| 30 | vec4 color; |
| 31 | vec4 line; |
| 32 | // x: tile span in px, y: 1 when the pass is single-sampled and this shader owns |
| 33 | // edge antialiasing, 0 when MSAA is resolving it. w: the per-frame clock in seconds. |
| 34 | vec4 misc; |
| 35 | // y: dash phase speed in px/s (WS-B). 0 for every static line, so the dash pattern |
| 36 | // is byte-identical; only the traffic and route draws set it. |
| 37 | vec4 morph; |
| 38 | } push; |
| 39 | |
| 40 | // Mirrors `style::paint::MIN_HALF_WIDTH_PX`; must match `line.vert`. |
| 41 | const float MIN_HALF_WIDTH_PX = 0.5; |
| 42 | |
| 43 | void main() { |
| 44 | float wantHalfPx = push.line.x; |
| 45 | float halfWidthPx = max(wantHalfPx, MIN_HALF_WIDTH_PX); |
| 46 | // Hand back the width the vertex shader added to make the quad rasterisable, so |
| 47 | // a 0.3px road draws as a faint pixel rather than a solid one. |
| 48 | float coverage = clamp(wantHalfPx / halfWidthPx, 0.0, 1.0); |
| 49 | // Box filter over the pixel, only when nothing else is doing it. |
| 50 | float box = clamp(halfWidthPx - abs(inEdgePx) + 0.5, 0.0, 1.0); |
| 51 | coverage *= mix(1.0, box, push.misc.y); |
| 52 | if (coverage <= 0.0) discard; |
| 53 | |
| 54 | float dashOn = push.line.z; |
| 55 | float dashOff = push.line.w; |
| 56 | // A non-positive gap is a solid line, checked first so [2, 0] never reaches the |
| 57 | // modulo at all. |
| 58 | if (dashOff > 0.0) { |
| 59 | float width = max(halfWidthPx * 2.0, 1.0); |
| 60 | float on = dashOn * width; |
| 61 | float off = dashOff * width; |
| 62 | float period = on + off; |
| 63 | // Scroll the dash by a clock-driven phase: `morph.y` is a per-draw phase speed |
| 64 | // in px/s and `misc.w` is the per-frame clock in seconds. Both are 0 for every |
| 65 | // static line (morph.y defaults to 0; the clock starts at 0), so `phase` is 0 |
| 66 | // and `mod(inDistancePx - phase, period)` collapses to the un-phased test — |
| 67 | // static roads, rail and borders are byte-identical. Traffic and route set |
| 68 | // morph.y to make their dashes travel. |
| 69 | float phase = push.misc.w * push.morph.y; |
| 70 | if (period > 0.0 && mod(inDistancePx - phase, period) > on) discard; |
| 71 | } |
| 72 | outColor = vec4(push.color.rgb, push.color.a * coverage); |
| 73 | } |