Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / library / map / src / main / rust / shaders / line.vert
line.vert84 lines · 4.4 KiBRaw
| 1 | #version 450 |
| 2 | // Line: extrudes the stroked centreline to its screen-space width. |
| 3 | // |
| 4 | // The tessellator emits geometry that is a function of the tile alone - position, |
| 5 | // the unit normal of the join, and two multipliers - and the width arrives as a |
| 6 | // push constant. That is deliberate: a stroke's width is a screen measurement that |
| 7 | // changes continuously with zoom, so baking it into vertices would re-tessellate |
| 8 | // every road in every visible tile on every zoom step, while panning, on the |
| 9 | // critical path. |
| 10 | // |
| 11 | // offset = normal * (offsetMul * gapHalf + widthMul * halfWidth + lateral) |
| 12 | // |
| 13 | // `offsetMul` is 0 for a plain stroke and ±1 for the two bands of a casing; |
| 14 | // `widthMul` is ±1 for a plain stroke's two edges and 0 / ±2 for a casing band's |
| 15 | // inner and outer edges - a band spans a full width, not a half width. See |
| 16 | // `tess::stroke`. |
| 17 | // |
| 18 | // `lateral` shifts the whole band sideways, which is how transit routes sharing |
| 19 | // one track fan out into parallel coloured lines. It is a push constant for the |
| 20 | // same reason the width is: it is a screen measurement that ramps with zoom, and |
| 21 | // baking it into vertices would re-tessellate the layer on every zoom step. |
| 22 | // |
| 23 | // `inZ` is the draped ground height in tile-normalised units (metres over the tile's |
| 24 | // ground width — 0.0 where the tile carries no heightmap). `morph.z` carries the |
| 25 | // tile's world-px span (Dp) for this frame; multiplying the two recovers the world-px |
| 26 | // height the perspective matrix expects, exactly as `building.vert` scales its heights. |
| 27 | layout(location = 0) in vec2 inPosition; |
| 28 | layout(location = 1) in vec2 inNormal; |
| 29 | layout(location = 2) in vec2 inExtrude; |
| 30 | layout(location = 3) in float inDistance; |
| 31 | layout(location = 4) in float inZ; |
| 32 | layout(location = 0) out float outDistancePx; |
| 33 | layout(location = 1) out float outEdgePx; |
| 34 | layout(push_constant) uniform Push { |
| 35 | mat4 tileToClip; |
| 36 | vec4 color; |
| 37 | vec4 line; |
| 38 | vec4 misc; |
| 39 | // x: per-tile opacity/morph factor. y: dash phase speed (px/s). z: the tile's |
| 40 | // world-px span (Dp), the tile-norm-height -> world-px scale. w reserved. |
| 41 | vec4 morph; |
| 42 | } push; |
| 43 | |
| 44 | // Mirrors `style::paint::MIN_HALF_WIDTH_PX`, which GLSL cannot include. |
| 45 | // `the_shader_floor_matches_the_style_constant` pins the two together. |
| 46 | const float MIN_HALF_WIDTH_PX = 0.5; |
| 47 | |
| 48 | void main() { |
| 49 | float wantHalfPx = push.line.x; |
| 50 | float gapHalfPx = push.line.y; |
| 51 | float tilePx = max(push.misc.x, 1.0); |
| 52 | float lateralPx = push.misc.z; |
| 53 | // A quad thinner than a pixel falls between pixel centres and rasterises as |
| 54 | // stipple, so give it a pixel of geometry to land on. The fragment shader takes |
| 55 | // the difference back off as alpha, which is how a thin road fades instead of |
| 56 | // snapping to full strength. |
| 57 | float halfWidthPx = max(wantHalfPx, MIN_HALF_WIDTH_PX); |
| 58 | // The offset is a pixel distance and the normal is a unit vector in tile-local |
| 59 | // space, so dividing by the tile's pixel size converts one to the other. |
| 60 | float offsetPx = inExtrude.x * gapHalfPx + inExtrude.y * halfWidthPx + lateralPx; |
| 61 | // Where this band's centreline sits: 0 for a plain stroke, and |
| 62 | // gapHalf + halfWidth out on the offset side for a casing band. The lateral |
| 63 | // shift has to be here too, or `outEdgePx` is measured from the old centre and |
| 64 | // `line.frag`'s box filter fades the whole line instead of just its edges. |
| 65 | float centrePx = inExtrude.x * gapHalfPx + sign(inExtrude.x) * halfWidthPx + lateralPx; |
| 66 | // Signed distance from that centre, which spans -halfWidth..+halfWidth across |
| 67 | // every band and so interpolates one unit per screen pixel. |
| 68 | outEdgePx = offsetPx - centrePx; |
| 69 | vec2 offsetTile = inNormal * (offsetPx / tilePx); |
| 70 | float worldHeight = inZ * push.morph.z; |
| 71 | // The height column's w term (tileToClip[2][3]) is 0 on the ortho fast-path (pitch 0) |
| 72 | // and -cos(pitch) once tilted — the same test the building shader uses. At pitch 0 |
| 73 | // feed 0.0 exactly as before, so the flat map is byte-identical with or without a DEM; |
| 74 | // tilted, feed the world-px height so the stroke drapes onto the relief. Route and |
| 75 | // traffic overlays ride this pipeline with undraped z=0 and a zero span, so they stay |
| 76 | // flat by construction. |
| 77 | if (abs(push.tileToClip[2][3]) < 1e-6) { |
| 78 | gl_Position = push.tileToClip * vec4(inPosition + offsetTile, 0.0, 1.0); |
| 79 | } else { |
| 80 | gl_Position = push.tileToClip * vec4(inPosition + offsetTile, worldHeight, 1.0); |
| 81 | } |
| 82 | // Distance along the line in pixels, for the dash pattern. |
| 83 | outDistancePx = inDistance * tilePx; |
| 84 | } |