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

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

puck.frag84 lines · 3.6 KiBRaw
1#version 450
2// User puck: the bearing cone, the white rim and the blue dot, all from a distance and
3// an angle rather than from geometry.
4//
5// Tessellating this would need a circle, an annulus sector with round caps, and
6// per-vertex alpha the `fill` vertex format cannot carry. Solving it per fragment
7// instead gives exact circles at any density, the real radial falloff across the cone's
8// stroke, and antialiased edges that do not depend on MSAA.
9layout(location = 0) in vec2 inLocal;
10layout(location = 0) out vec4 outColor;
11layout(push_constant) uniform Push {
12 mat4 tileToClip;
13 // The dot and the cone. The rim's colour is a constant, below.
14 vec4 color;
15 // Radii in device px: x the rim, y the dot, z the cone's centreline, w half the
16 // cone's stroke width.
17 vec4 line;
18 // x: bearing in radians clockwise from north. y: 1 when there is a bearing at all,
19 // 0 when the fix carries no heading and the cone must not draw. z: the quad's radius
20 // in device px, which turns a local -1..1 coordinate into pixels.
21 vec4 misc;
22} push;
23
24// The rim is white in both palettes. It exists to separate the dot from whatever is
25// under it, and a rim that followed the basemap would stop doing that on one of them.
26const vec3 RIM = vec3(1.0);
27// Half the cone's 60 degree sweep.
28const float CONE_HALF_SWEEP = radians(30.0);
29const float PI = radians(180.0);
30
31// Source-over, on premultiplied colour.
32vec4 over(vec4 src, vec4 dst) {
33 return src + dst * (1.0 - src.a);
34}
35
36// 1 inside, 0 outside, with one pixel of falloff across the edge.
37float disc(float distancePx, float radiusPx) {
38 return clamp(radiusPx + 0.5 - distancePx, 0.0, 1.0);
39}
40
41void main() {
42 float quadPx = push.misc.z;
43 vec2 p = inLocal * quadPx;
44 float r = length(p);
45
46 vec4 acc = vec4(0.0);
47
48 if (push.misc.y > 0.5) {
49 float conePx = push.line.z;
50 float halfStrokePx = push.line.w;
51 // Clockwise from north, matching a compass bearing: clip y points down, so "up"
52 // is -y.
53 float offset = atan(p.x, -p.y) - push.misc.x;
54 // The shortest way round, so a cone pointing just west of north is two degrees
55 // from it rather than 358.
56 offset = mod(offset + PI, 2.0 * PI) - PI;
57 // Distance to the arc's centreline: straight across it inside the sweep, and to
58 // the nearer end point outside it, which is what gives the round caps.
59 float toCentreline;
60 if (abs(offset) <= CONE_HALF_SWEEP) {
61 toCentreline = abs(r - conePx);
62 } else {
63 float capAngle = push.misc.x + sign(offset) * CONE_HALF_SWEEP;
64 toCentreline = distance(p, conePx * vec2(sin(capAngle), -cos(capAngle)));
65 }
66 // The gradient is radial across the whole puck rather than across the stroke, so
67 // the arc is fully opaque at its inner edge and only ~71% at its outer one.
68 // Fading to zero across the stroke instead looks visibly flat.
69 float fade = 1.0 - clamp((r - 0.8 * quadPx) / (0.2 * quadPx), 0.0, 1.0);
70 float cone = disc(toCentreline, halfStrokePx) * fade;
71 acc = over(vec4(push.color.rgb * cone, cone), acc);
72 }
73
74 // Rim then dot, both over the cone, so the dot is never eaten by the cone's inner
75 // edge.
76 float rim = disc(r, push.line.x);
77 acc = over(vec4(RIM * rim, rim), acc);
78 float core = disc(r, push.line.y);
79 acc = over(vec4(push.color.rgb * core, core), acc);
80
81 if (acc.a <= 0.0) discard;
82 // Back to straight alpha: the pipeline blends src-alpha over one-minus-src-alpha.
83 outColor = vec4(acc.rgb / acc.a, acc.a * push.color.a);
84}