Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / library / map / src / main / rust / shaders / moon.frag
moon.frag84 lines · 3.4 KiBRaw
| 1 | #version 450 |
| 2 | |
| 3 | // Moon fragment: LROC color textured by selenographic lon/lat, shaded by the |
| 4 | // LDEM-derived normal with a fixed sun direction. |
| 5 | // |
| 6 | // `inDisc` is -1..1 across the disc's bounding square (y down, Vulkan clip). |
| 7 | // Outside the unit circle is discarded; inside, the orthographic hemisphere z |
| 8 | // and the sphere normal are recovered per fragment, and the lon/lat inverts |
| 9 | // through the same east/north/up basis the globe math uses |
| 10 | // (`globe_point`/`globe_lonlat`). The color texture is equirectangular |
| 11 | // -180..180/-90..90. The DEM (same layout, RG-packed uint16 half-metres + |
| 12 | // 20000) perturbs the normal for relief shading. |
| 13 | // |
| 14 | // Lighting is a single fixed sun from the east-equatorial direction: enough to |
| 15 | // read relief, deliberately not a day/night terminator (no sun-position input |
| 16 | // exists on this path yet). Limb darkening fakes the spherical falloff the |
| 17 | // flat texture lacks. |
| 18 | |
| 19 | layout(location = 0) in vec2 inDisc; |
| 20 | layout(location = 0) out vec4 outColor; |
| 21 | |
| 22 | layout(push_constant) uniform Push { |
| 23 | mat4 tileToClip; |
| 24 | // xy: camera centre lon/lat (degrees) — the visible-hemisphere centre. |
| 25 | vec4 color; |
| 26 | vec4 line; |
| 27 | // z: DEM relief strength (normal nudge per metre). |
| 28 | vec4 misc; |
| 29 | vec4 morph; |
| 30 | } push; |
| 31 | layout(set = 0, binding = 0) uniform sampler2D moonColor; |
| 32 | layout(set = 0, binding = 1) uniform sampler2D moonDem; |
| 33 | |
| 34 | const float PI = 3.141592653589793; |
| 35 | |
| 36 | // The height bytes are split across R and G, so filtering would blend the high |
| 37 | // byte of one texel with another's and spike at every byte rollover. Fetch the |
| 38 | // exact texel instead: longitude wraps, latitude clamps at the poles. |
| 39 | float demMetres(vec2 uv) { |
| 40 | ivec2 size = textureSize(moonDem, 0); |
| 41 | ivec2 p = ivec2( |
| 42 | int(fract(uv.x) * float(size.x)) % size.x, |
| 43 | clamp(int(uv.y * float(size.y)), 0, size.y - 1)); |
| 44 | vec4 t = texelFetch(moonDem, p, 0); |
| 45 | return (round(t.r * 255.0) * 256.0 + round(t.g * 255.0) - 20000.0) * 0.5; |
| 46 | } |
| 47 | |
| 48 | void main() { |
| 49 | // Disc y-down to north-up. |
| 50 | vec2 en = vec2(inDisc.x, -inDisc.y); |
| 51 | float r2 = dot(en, en); |
| 52 | if (r2 > 1.0) discard; |
| 53 | float z = sqrt(1.0 - r2); |
| 54 | vec3 sphereNormal = vec3(en, z); |
| 55 | // Nearer (larger z) wins LESS, matching the globe tiles' depth regime. |
| 56 | gl_FragDepth = 1.0 - z * 0.5; |
| 57 | |
| 58 | // Invert the east/north/up basis (mirrors `globe_lonlat`). |
| 59 | float cLatR = radians(push.color.y); |
| 60 | float sy = sin(cLatR); |
| 61 | float cy = cos(cLatR); |
| 62 | float ey = cy * en.y + sy * z; |
| 63 | float ez = -sy * en.y + cy * z; |
| 64 | float lat = asin(clamp(ey, -1.0, 1.0)) * 180.0 / PI; |
| 65 | float lon = push.color.x + atan(en.x, ez) * 180.0 / PI; |
| 66 | lon = mod(lon + 180.0, 360.0) - 180.0; |
| 67 | vec2 uv = vec2((lon + 180.0) / 360.0, (90.0 - lat) / 180.0); |
| 68 | vec3 albedo = texture(moonColor, uv).rgb; |
| 69 | |
| 70 | // Forward differences one DEM texel east and south. Equirectangular texels |
| 71 | // span equal angles on both axes, so the two gradients share one scale. |
| 72 | vec2 texel = 1.0 / vec2(textureSize(moonDem, 0)); |
| 73 | float hC = demMetres(uv); |
| 74 | float hX = demMetres(uv + vec2(texel.x, 0.0)); |
| 75 | float hY = demMetres(uv + vec2(0.0, texel.y)); |
| 76 | float relief = push.misc.z; |
| 77 | // Rising east tilts the normal west; rising south (uv.y grows southward) |
| 78 | // tilts it north. |
| 79 | vec3 n = normalize(sphereNormal + vec3(-(hX - hC), hY - hC, 0.0) * relief); |
| 80 | vec3 sun = normalize(vec3(0.8, 0.25, 0.55)); |
| 81 | float diff = clamp(dot(n, sun), 0.0, 1.0); |
| 82 | float shade = (0.25 + 0.75 * diff) * (0.35 + 0.65 * z); |
| 83 | outColor = vec4(albedo * shade, 1.0); |
| 84 | } |