Modern Apps git
git clone https://git.maproj.org/Modern-Apps.git
dependencies ▾

Modern-Apps.git / library / map / src / main / rust / shaders / road_surface.vert

road_surface.vert94 lines · 4.9 KiBRaw
1#version 450
2// Road surface: extrudes the carriageway centreline to its screen-space width and
3// hands the fragment stage an across-the-road coordinate.
4//
5// # Why this is not `line.vert` with another attribute
6//
7// A stroke has no across-the-road coordinate, so a marking cannot be placed on it —
8// which is the whole reason `tess::stroke` cannot draw a lane divider without
9// re-stroking the road once per divider. What the ribbon adds is `t`, a genuine
10// normalised across-coordinate that is exactly -1 at one kerb and +1 at the other,
11// so `road_surface.frag` can paint every marking as a function of `t` alone with no
12// extra geometry at all.
13//
14// It is a separate pipeline rather than a wider shared vertex format because
15// `TrafficMesh` and the route overlay both ride the 8-float line format and the line
16// pipeline; widening that to serve the roads layer would make every one of them pay
17// for four bytes they never read. `tile/geometry.rs` already states this tradeoff.
18//
19// # Width still lives here, not in the vertices
20//
21// Same invariant as `line.vert`, and for the same reason: a carriageway's width is a
22// screen measurement that ramps continuously with zoom, so baking it into vertices
23// would re-tessellate every road in every visible tile on every zoom step, while
24// panning, on the critical path. The tessellator emits the centreline point, the
25// join normal and `t`; the half-width arrives as a push constant and the offset is
26//
27// offset = normal * (t * halfWidth)
28//
29// `t` doubles as the extrusion multiplier and as the fragment stage's marking
30// coordinate, which is what keeps the vertex to seven floats.
31//
32// # Do not normalise `inNormal`
33//
34// It is the *miter* normal, not a unit vector: `tess::ribbon::join_normal` lengthens the
35// bisector by `1/cos(theta/2)`, clamped at `stroke::MITER_LIMIT`, and the taper scales it
36// again below 1 where a section abuts one of a different lane count. Unit length holds
37// only on a straight run.
38//
39// That extra length is load-bearing. It is what keeps the kerb at `|t| = 1` through a
40// bend; with a unit normal the carriageway pinches at every join and every marking slides
41// sideways with it, because a marking's position is a fraction of a width that is no
42// longer the width it should be. `normalize(inNormal)` compiles, warns about nothing, and
43// silently breaks the joins and the taper together.
44layout(location = 0) in vec2 inPosition;
45layout(location = 1) in vec2 inNormal;
46layout(location = 2) in float inT;
47layout(location = 3) in float inDistance;
48layout(location = 4) in float inZ;
49layout(location = 0) out float outT;
50layout(location = 1) out float outDistancePx;
51layout(push_constant) uniform Push {
52 mat4 tileToClip;
53 vec4 color;
54 // Repurposed for the ribbon; see the `Push` doc comment in `vulkan/pipeline.rs`,
55 // which is the contract the renderer fills in.
56 // x: carriageway half-width px, y: lane count, z: centre-line `t`, w: oneway flag.
57 vec4 line;
58 // x: tile span in px, y: 1 when this pass owns edge antialiasing,
59 // z: 1 when the centre line is yellow, w: the per-frame clock in seconds.
60 vec4 misc;
61 // MORPH_NONE on this path; z: the tile's world-px span (Dp), the
62 // tile-norm-height -> world-px scale for the draped `inZ` below.
63 vec4 morph;
64} push;
65
66// Mirrors `style::paint::MIN_HALF_WIDTH_PX`; must match `road_surface.frag`.
67const float MIN_HALF_WIDTH_PX = 0.5;
68
69void main() {
70 float wantHalfPx = push.line.x;
71 float tilePx = max(push.misc.x, 1.0);
72 // A carriageway thinner than a pixel falls between pixel centres and rasterises as
73 // stipple, so give it a pixel of geometry to land on and let the fragment stage take
74 // the difference back off as alpha. Identical to `line.vert`, so a hairline road at
75 // low zoom fades rather than snapping to full strength.
76 float halfWidthPx = max(wantHalfPx, MIN_HALF_WIDTH_PX);
77 // The offset is a pixel distance and the normal is in tile-local space, so dividing by
78 // the tile's pixel size converts one to the other. The normal's own length is a
79 // deliberate miter/taper factor and is meant to survive this — see the header.
80 vec2 offsetTile = inNormal * (inT * halfWidthPx / tilePx);
81 float worldHeight = inZ * push.morph.z;
82 // Same ortho/tilted split as `line.vert`: 0.0 at pitch 0 (byte-identical flat map),
83 // world-px height under tilt so the carriageway drapes onto the relief.
84 if (abs(push.tileToClip[2][3]) < 1e-6) {
85 gl_Position = push.tileToClip * vec4(inPosition + offsetTile, 0.0, 1.0);
86 } else {
87 gl_Position = push.tileToClip * vec4(inPosition + offsetTile, worldHeight, 1.0);
88 }
89 outT = inT;
90 // Distance along the carriageway in pixels, for the divider dash phase. Scaled here
91 // rather than in the tessellator for the same reason the width is: the attribute is
92 // tile-local so the geometry stays a function of the tile alone.
93 outDistancePx = inDistance * tilePx;
94}