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

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

road_surface.frag161 lines · 8.7 KiBRaw
1#version 450
2// Road surface fragment: asphalt, then the markings painted onto it.
3//
4// Everything here is a function of `t` — the across-the-road coordinate, exactly -1 at
5// one kerb and +1 at the other — and of the distance along the carriageway. There is no
6// marking geometry at all. That is the point of the ribbon: the old `lane_fans` path
7// re-stroked a road's entire geometry once per divider, so an eight-lane road paid seven
8// extra full strokes to draw seven hairlines. Here they cost arithmetic.
9//
10// # Which coverage terms are gated by `misc.y`, and which are not
11//
12// `misc.y` says whether this pass owns edge antialiasing — it is 0 under MSAA, where the
13// rasteriser already resolves partial coverage and a second coverage term on top fades a
14// diagonal road twice (`line.frag` documents the z6 case that forced this).
15//
16// That reasoning applies to the **carriageway silhouette** and to nothing else. A marking
17// is painted *inside* a fully covered triangle, so the rasteriser sees no edge there and
18// resolves nothing: MSAA cannot antialias a lane divider. Its coverage term is therefore
19// unconditional. Gating the markings on `misc.y` too would leave every marking hard-edged
20// on the MSAA path, and a hard-edged sub-pixel line crawls and flickers as it drifts
21// across pixel centres while panning — far more visible than the road edge ever was.
22//
23// For the same reason every marking edge is derived in **pixels**, never in `t`. A fixed
24// width in `t` is a fixed fraction of the road, so it would grow and shrink with zoom and
25// go sub-pixel at exactly the zoom where the aliasing is worst.
26//
27// # Why markings fade out rather than shrink to nothing
28//
29// Marking widths are proportional to the lane (a real marking is a real width on the
30// ground), floored so they stay rasterisable. Left alone, that floor is what a low zoom
31// converges to: at a few pixels per lane the floors of the edge lines, the dividers and
32// the centre line together are wider than the road, and a motorway renders as a white
33// smear. So markings ramp off once a lane is too narrow to read one on, and the asphalt
34// is left plain — which is also what the road looked like before any of this existed.
35layout(location = 0) in float inT;
36layout(location = 1) in float inDistancePx;
37layout(location = 0) out vec4 outColor;
38layout(push_constant) uniform Push {
39 mat4 tileToClip;
40 // The carriageway's asphalt colour.
41 vec4 color;
42 // Repurposed for the ribbon; see the `Push` doc comment in `vulkan/pipeline.rs`,
43 // which is the contract the renderer fills in.
44 // x: carriageway half-width px, y: lane count, z: centre-line `t`, w: oneway flag.
45 vec4 line;
46 // x: tile span in px, y: 1 when the pass is single-sampled and this shader owns the
47 // carriageway's edge antialiasing, z: 1 when the centre line is yellow,
48 // w: the per-frame clock in seconds.
49 vec4 misc;
50 vec4 morph;
51} push;
52
53// Mirrors `style::paint::MIN_HALF_WIDTH_PX`; must match `road_surface.vert`.
54const float MIN_HALF_WIDTH_PX = 0.5;
55
56// A `line.z` below this means "paint no markings on this ribbon at all". A real centre-line
57// `t` is an across-road coordinate bounded to [-1, +1], so no road can reach it; the
58// renderer pushes exactly -2.0 (`pipeline::NO_MARKINGS`). The threshold sits halfway
59// between the two so neither side depends on the exact value.
60//
61// It exists for the lane connectors through a junction. Real road paint stops at the stop
62// line and resumes on the far side — an intersection is not marked out into lanes — so a
63// connector carrying edge lines is not merely noisy, it is wrong. And it is *only* noise:
64// a connector is drawn in the same colour as the carriageway it continues, so where it
65// overlies the road its asphalt contributes nothing and its markings are the entire
66// visible ribbon. Twelve of those per junction read as a tangle of hairlines rather than
67// as road surface.
68const float NO_MARKINGS_BELOW = -1.5;
69
70// A painted marking is about 0.15 m in a lane about 3.5 m wide. Held as a fraction of the
71// lane rather than an absolute width so it stays a ground measurement across zooms.
72const float MARK_WIDTH_LANES = 0.045;
73// Below half a pixel a marking falls between pixel centres and stipples, so it is widened
74// to something the rasteriser can hit and the fade below takes it away instead.
75const float MIN_MARK_HALF_PX = 0.5;
76// The lane widths between which markings ramp in. Under the lower bound a lane is a few
77// pixels across and its markings would be most of it.
78const float MARK_FADE_START_PX = 4.0;
79const float MARK_FADE_FULL_PX = 10.0;
80// Divider dash and gap, in lane widths: roughly the 3 m line and 9 m gap that is the
81// common standard. Tied to the lane so the dash is a ground length, like the widths.
82const float DASH_ON_LANES = 0.9;
83const float DASH_OFF_LANES = 2.6;
84// Markings are painted, not lit, so they are a flat off-white rather than pure white,
85// which reads as a blown highlight against the asphalt.
86const vec3 MARK_WHITE = vec3(0.92, 0.92, 0.90);
87const vec3 MARK_YELLOW = vec3(0.95, 0.76, 0.18);
88
89// The 1px box filter `line.frag` uses, over a signed pixel distance from a band's centre.
90// Shared rather than a smoothstep so a marking edge and a road edge fade identically.
91float band(float distancePx, float halfPx) {
92 return clamp(halfPx - distancePx + 0.5, 0.0, 1.0);
93}
94
95void main() {
96 float wantHalfPx = push.line.x;
97 float halfWidthPx = max(wantHalfPx, MIN_HALF_WIDTH_PX);
98 // Hand back the width the vertex shader added to make the quad rasterisable, so a
99 // hairline road at low zoom draws faint rather than solid.
100 float coverage = clamp(wantHalfPx / halfWidthPx, 0.0, 1.0);
101 // `t` interpolates linearly across the ribbon, so this is the same signed
102 // one-unit-per-screen-pixel measure `line.frag` calls `inEdgePx`.
103 float edgePx = inT * halfWidthPx;
104 coverage *= mix(1.0, band(abs(edgePx), halfWidthPx), push.misc.y);
105 if (coverage <= 0.0) discard;
106
107 float lanes = max(push.line.y, 1.0);
108 float lanePx = 2.0 * halfWidthPx / lanes;
109 float legible =
110 clamp((lanePx - MARK_FADE_START_PX) / (MARK_FADE_FULL_PX - MARK_FADE_START_PX), 0.0, 1.0);
111
112 vec3 rgb = push.color.rgb;
113 if (legible > 0.0 && push.line.z > NO_MARKINGS_BELOW) {
114 float markHalfPx = max(lanePx * MARK_WIDTH_LANES * 0.5, MIN_MARK_HALF_PX);
115 bool oneway = push.line.w >= 0.5;
116 float centreT = push.line.z;
117
118 // Lane-index space: 0 at one kerb, `lanes` at the other, so a lane boundary is an
119 // integer and the nearest one is a rounding away.
120 float lane = (inT * 0.5 + 0.5) * lanes;
121 float boundary = round(lane);
122 float boundaryT = boundary / lanes * 2.0 - 1.0;
123 // Boundary 0 and boundary `lanes` are the kerbs, which carry the solid edge line
124 // rather than a divider.
125 bool interior = boundary > 0.5 && boundary < lanes - 0.5;
126 // The boundary the opposing traffic meets at is the centre line, not a divider —
127 // otherwise a white dash is painted straight over the yellow. Half a lane in `t`
128 // is `1 / lanes`, so exactly one boundary can match. Identifying the boundary is
129 // the one thing done in `t`; its edges are still placed in pixels below.
130 bool isCentre = !oneway && abs(boundaryT - centreT) < 1.0 / lanes;
131
132 float dashed = 0.0;
133 if (interior && !isCentre) {
134 float on = DASH_ON_LANES * lanePx;
135 float period = on + DASH_OFF_LANES * lanePx;
136 float along = mod(inDistancePx, period);
137 // Soft at both ends of the dash for the same reason the sides are soft: a hard
138 // dash end is a sub-pixel edge that crawls when the map pans.
139 dashed = clamp(min(along, on - along) + 0.5, 0.0, 1.0);
140 dashed *= band(abs(lane - boundary) * lanePx, markHalfPx);
141 }
142 rgb = mix(rgb, MARK_WHITE, dashed * legible);
143
144 // The edge lines sit flush inside the kerb, so their outer side lands on the rim
145 // of the carriageway and the road reads as having a painted border rather than a
146 // white line floating near one.
147 float edgeCentrePx = max(halfWidthPx - markHalfPx, 0.0);
148 float edgeLine = band(abs(abs(edgePx) - edgeCentrePx), markHalfPx);
149 rgb = mix(rgb, MARK_WHITE, edgeLine * legible);
150
151 // The centre line last, so it wins wherever it overlaps. A one-way carriageway has
152 // no opposing traffic and so has no centre line at all — not a white one.
153 if (!oneway) {
154 float centreLine = band(abs(edgePx - centreT * halfWidthPx), markHalfPx);
155 vec3 centreRgb = push.misc.z >= 0.5 ? MARK_YELLOW : MARK_WHITE;
156 rgb = mix(rgb, centreRgb, centreLine * legible);
157 }
158 }
159
160 outColor = vec4(rgb, push.color.a * coverage);
161}