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

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

moon.frag171 lines · 7.7 KiBRaw
1#version 450
2
3// Body fragment: equirectangular colour textured by planetographic lon/lat,
4// shaded by the DEM-derived normal with a fixed sun direction, plus an
5// optional equatorial ring strip for the ringed giants.
6//
7// `inDisc` is -1..1 across the disc's bounding square (y down, Vulkan clip),
8// scaled by the ring extent for ringed bodies. Inside the unit circle the
9// orthographic hemisphere z and the sphere normal are recovered per fragment
10// in the view frame, the camera view rotation (bearing/pitch) is undone, and
11// the lon/lat then inverts through the same east/north/up basis the globe
12// math uses (`globe_point`/`globe_lonlat`). The colour texture is
13// equirectangular -180..180/-90..90. The DEM (same layout, RG-packed uint16,
14// packing pushed per body) perturbs the normal for relief shading. DEM-less
15// bodies push a 1x1 DEM with unit 0 and skip relief.
16//
17// Rings live in the body's equatorial plane. The ring point at a screen
18// pixel is solved in the centre-facing frame: undo the view rotation, then
19// require the point to lie in the equatorial plane (its centre-facing
20// coordinates dotted with the body pole are zero), which fixes its view
21// depth. Its centre-facing length is the equatorial radius. Outside the
22// disc the ring draws on its own; inside, a ring point nearer (in view
23// depth) than the sphere surface blends over the planet (the front arc),
24// and a farther one is hidden by the planet. Rings therefore roll and tip
25// with the disc under bearing/pitch instead of staying screen-aligned.
26//
27// Lighting is a single fixed sun from the east-equatorial direction: enough
28// to read relief, deliberately not a day/night terminator (no sun-position
29// input exists on this path yet). Limb darkening fakes the spherical falloff
30// the flat texture lacks.
31
32layout(location = 0) in vec2 inDisc;
33layout(location = 0) out vec4 outColor;
34
35layout(push_constant) uniform Push {
36 mat4 tileToClip;
37 // xy: camera centre lon/lat (degrees) — the visible-hemisphere centre.
38 vec4 color;
39 // x: DEM packing offset, y: DEM metres per packed unit (0 = no DEM),
40 // z: ring inner radius, w: ring outer radius (planetary radii).
41 vec4 line;
42 // xy: camera bearing/pitch (degrees) for the view rotation,
43 // z: DEM relief strength (normal nudge per metre), w: time (seconds).
44 vec4 misc;
45 // x: ring opacity (0 = no rings), y: quad extent in radii (vertex only).
46 vec4 morph;
47} push;
48layout(set = 0, binding = 0) uniform sampler2D moonColor;
49layout(set = 0, binding = 1) uniform sampler2D moonDem;
50layout(set = 0, binding = 2) uniform sampler2D bodyRing;
51
52const float PI = 3.141592653589793;
53
54// Undo the camera view rotation (mirrors `globe_view_unrotate`): undo the
55// pitch tip about the screen-east axis, then undo the bearing roll about
56// the view axis. Both zero is the identity.
57vec3 viewUnrotate(vec3 v) {
58 float vb = radians(push.misc.x);
59 float vp = radians(push.misc.y);
60 if (vb == 0.0 && vp == 0.0) return v;
61 float y1 = cos(vp) * v.y + sin(vp) * v.z;
62 float z1 = -sin(vp) * v.y + cos(vp) * v.z;
63 return vec3(cos(vb) * v.x + sin(vb) * y1, -sin(vb) * v.x + cos(vb) * y1, z1);
64}
65
66// The height bytes are split across R and G, so filtering would blend the high
67// byte of one texel with another's and spike at every byte rollover. Fetch the
68// exact texel instead: longitude wraps, latitude clamps at the poles.
69float demMetres(vec2 uv) {
70 ivec2 size = textureSize(moonDem, 0);
71 ivec2 p = ivec2(
72 int(fract(uv.x) * float(size.x)) % size.x,
73 clamp(int(uv.y * float(size.y)), 0, size.y - 1));
74 vec4 t = texelFetch(moonDem, p, 0);
75 return (round(t.r * 255.0) * 256.0 + round(t.g * 255.0) - push.line.x) * push.line.y;
76}
77
78// Ring sample at this screen point, or alpha 0 when the point misses the
79// ring annulus. `depth` returns the ring point's view depth (sphere-normal
80// z units) for the front/back test against the planet.
81vec4 ringSample(vec2 en, float cLatR, out float depth) {
82 depth = 0.0;
83 if (push.morph.x <= 0.0 || push.line.w <= push.line.z) return vec4(0.0);
84 float sy = sin(cLatR);
85 float cy = cos(cLatR);
86 float vb = radians(push.misc.x);
87 float vp = radians(push.misc.y);
88 float cb = cos(vb);
89 float sb = sin(vb);
90 float cp = cos(vp);
91 float sp = sin(vp);
92 // Centre-facing point pc = viewUnrotate(vec3(en, depth)); the body pole
93 // in that frame is (0, cy, sy), so the equatorial plane is
94 // cy * pc.y + sy * pc.z == 0. pc.y/pc.z are linear in depth:
95 // pc.y = -sb * en.x + cb * (cp * en.y + sp * depth)
96 // pc.z = -sp * en.y + cp * depth
97 // Solve for depth. Near a pole-on view the ring collapses to a line:
98 // clamp the coefficient so the annulus stays finite (and thin).
99 float coeff = cy * cb * sp + sy * cp;
100 float denom = abs(coeff) < 0.03 ? (coeff < 0.0 ? -0.03 : 0.03) : coeff;
101 float constant = cy * (-sb * en.x + cb * cp * en.y) - sy * sp * en.y;
102 depth = -constant / denom;
103 vec3 pc = viewUnrotate(vec3(en, depth));
104 float radius = length(pc);
105 float u = (radius - push.line.z) / (push.line.w - push.line.z);
106 if (u < 0.0 || u > 1.0) return vec4(0.0);
107 vec4 ring = texture(bodyRing, vec2(u, 0.5));
108 return vec4(ring.rgb, ring.a * push.morph.x);
109}
110
111void main() {
112 // Disc y-down to north-up.
113 vec2 en = vec2(inDisc.x, -inDisc.y);
114 float r2 = dot(en, en);
115 float cLatR = radians(push.color.y);
116
117 if (r2 > 1.0) {
118 // Off the disc: only a ring can draw here.
119 float ringDepth;
120 vec4 ring = ringSample(en, cLatR, ringDepth);
121 if (ring.a < 0.004) discard;
122 gl_FragDepth = 1.0 - clamp(ringDepth, -1.0, 1.0) * 0.5;
123 outColor = vec4(ring.rgb * 0.9, ring.a);
124 return;
125 }
126 float z = sqrt(1.0 - r2);
127 vec3 sphereNormal = vec3(en, z);
128 // Nearer (larger z) wins LESS, matching the globe tiles' depth regime.
129 gl_FragDepth = 1.0 - z * 0.5;
130
131 // Undo the camera view rotation, then invert the east/north/up basis
132 // (mirrors `globe_lonlat`). Shading keeps the view-frame normal above.
133 vec3 basis = viewUnrotate(sphereNormal);
134 float sy = sin(cLatR);
135 float cy = cos(cLatR);
136 float ey = cy * basis.y + sy * basis.z;
137 float ez = -sy * basis.y + cy * basis.z;
138 float lat = asin(clamp(ey, -1.0, 1.0)) * 180.0 / PI;
139 float lon = push.color.x + atan(basis.x, ez) * 180.0 / PI;
140 lon = mod(lon + 180.0, 360.0) - 180.0;
141 vec2 uv = vec2((lon + 180.0) / 360.0, (90.0 - lat) / 180.0);
142 vec3 albedo = texture(moonColor, uv).rgb;
143
144 vec3 n = sphereNormal;
145 if (push.line.y > 0.0) {
146 // Forward differences one DEM texel east and south. Equirectangular
147 // texels span equal angles on both axes, so the gradients share a scale.
148 vec2 texel = 1.0 / vec2(textureSize(moonDem, 0));
149 float hC = demMetres(uv);
150 float hX = demMetres(uv + vec2(texel.x, 0.0));
151 float hY = demMetres(uv + vec2(0.0, texel.y));
152 float relief = push.misc.z;
153 // Rising east tilts the normal west; rising south (uv.y grows
154 // southward) tilts it north.
155 n = normalize(sphereNormal + vec3(-(hX - hC), hY - hC, 0.0) * relief);
156 }
157 vec3 sun = normalize(vec3(0.8, 0.25, 0.55));
158 float diff = clamp(dot(n, sun), 0.0, 1.0);
159 float shade = (0.25 + 0.75 * diff) * (0.35 + 0.65 * z);
160 vec3 col = albedo * shade;
161
162 // The ring's front arc crosses the disc: blend it over the planet where
163 // the ring point sits nearer than the sphere surface at this pixel.
164 // Both depths are view-frame z, so the test survives the view rotation.
165 float ringDepth;
166 vec4 ring = ringSample(en, cLatR, ringDepth);
167 if (ring.a >= 0.004 && ringDepth > z) {
168 col = mix(col, ring.rgb * 0.9, clamp(ring.a, 0.0, 1.0));
169 }
170 outColor = vec4(col, 1.0);
171}