#version 450 // Body fragment: equirectangular colour textured by planetographic lon/lat, // shaded by the DEM-derived normal with a fixed sun direction, plus an // optional equatorial ring strip for the ringed giants. // // `inDisc` is -1..1 across the disc's bounding square (y down, Vulkan clip), // scaled by the ring extent for ringed bodies. Inside the unit circle the // orthographic hemisphere z and the sphere normal are recovered per fragment // in the view frame, the camera view rotation (bearing/pitch) is undone, and // the lon/lat then inverts through the same east/north/up basis the globe // math uses (`globe_point`/`globe_lonlat`). The colour texture is // equirectangular -180..180/-90..90. The DEM (same layout, RG-packed uint16, // packing pushed per body) perturbs the normal for relief shading. DEM-less // bodies push a 1x1 DEM with unit 0 and skip relief. // // Rings live in the body's equatorial plane. The ring point at a screen // pixel is solved in the centre-facing frame: undo the view rotation, then // require the point to lie in the equatorial plane (its centre-facing // coordinates dotted with the body pole are zero), which fixes its view // depth. Its centre-facing length is the equatorial radius. Outside the // disc the ring draws on its own; inside, a ring point nearer (in view // depth) than the sphere surface blends over the planet (the front arc), // and a farther one is hidden by the planet. Rings therefore roll and tip // with the disc under bearing/pitch instead of staying screen-aligned. // // Lighting is a single fixed sun from the east-equatorial direction: enough // to read relief, deliberately not a day/night terminator (no sun-position // input exists on this path yet). Limb darkening fakes the spherical falloff // the flat texture lacks. layout(location = 0) in vec2 inDisc; layout(location = 0) out vec4 outColor; layout(push_constant) uniform Push { mat4 tileToClip; // xy: camera centre lon/lat (degrees) — the visible-hemisphere centre. vec4 color; // x: DEM packing offset, y: DEM metres per packed unit (0 = no DEM), // z: ring inner radius, w: ring outer radius (planetary radii). vec4 line; // xy: camera bearing/pitch (degrees) for the view rotation, // z: DEM relief strength (normal nudge per metre), w: time (seconds). vec4 misc; // x: ring opacity (0 = no rings), y: quad extent in radii (vertex only). vec4 morph; } push; layout(set = 0, binding = 0) uniform sampler2D moonColor; layout(set = 0, binding = 1) uniform sampler2D moonDem; layout(set = 0, binding = 2) uniform sampler2D bodyRing; const float PI = 3.141592653589793; // Undo the camera view rotation (mirrors `globe_view_unrotate`): undo the // pitch tip about the screen-east axis, then undo the bearing roll about // the view axis. Both zero is the identity. vec3 viewUnrotate(vec3 v) { float vb = radians(push.misc.x); float vp = radians(push.misc.y); if (vb == 0.0 && vp == 0.0) return v; float y1 = cos(vp) * v.y + sin(vp) * v.z; float z1 = -sin(vp) * v.y + cos(vp) * v.z; return vec3(cos(vb) * v.x + sin(vb) * y1, -sin(vb) * v.x + cos(vb) * y1, z1); } // The height bytes are split across R and G, so filtering would blend the high // byte of one texel with another's and spike at every byte rollover. Fetch the // exact texel instead: longitude wraps, latitude clamps at the poles. float demMetres(vec2 uv) { ivec2 size = textureSize(moonDem, 0); ivec2 p = ivec2( int(fract(uv.x) * float(size.x)) % size.x, clamp(int(uv.y * float(size.y)), 0, size.y - 1)); vec4 t = texelFetch(moonDem, p, 0); return (round(t.r * 255.0) * 256.0 + round(t.g * 255.0) - push.line.x) * push.line.y; } // Ring sample at this screen point, or alpha 0 when the point misses the // ring annulus. `depth` returns the ring point's view depth (sphere-normal // z units) for the front/back test against the planet. vec4 ringSample(vec2 en, float cLatR, out float depth) { depth = 0.0; if (push.morph.x <= 0.0 || push.line.w <= push.line.z) return vec4(0.0); float sy = sin(cLatR); float cy = cos(cLatR); float vb = radians(push.misc.x); float vp = radians(push.misc.y); float cb = cos(vb); float sb = sin(vb); float cp = cos(vp); float sp = sin(vp); // Centre-facing point pc = viewUnrotate(vec3(en, depth)); the body pole // in that frame is (0, cy, sy), so the equatorial plane is // cy * pc.y + sy * pc.z == 0. pc.y/pc.z are linear in depth: // pc.y = -sb * en.x + cb * (cp * en.y + sp * depth) // pc.z = -sp * en.y + cp * depth // Solve for depth. Near a pole-on view the ring collapses to a line: // clamp the coefficient so the annulus stays finite (and thin). float coeff = cy * cb * sp + sy * cp; float denom = abs(coeff) < 0.03 ? (coeff < 0.0 ? -0.03 : 0.03) : coeff; float constant = cy * (-sb * en.x + cb * cp * en.y) - sy * sp * en.y; depth = -constant / denom; vec3 pc = viewUnrotate(vec3(en, depth)); float radius = length(pc); float u = (radius - push.line.z) / (push.line.w - push.line.z); if (u < 0.0 || u > 1.0) return vec4(0.0); vec4 ring = texture(bodyRing, vec2(u, 0.5)); return vec4(ring.rgb, ring.a * push.morph.x); } void main() { // Disc y-down to north-up. vec2 en = vec2(inDisc.x, -inDisc.y); float r2 = dot(en, en); float cLatR = radians(push.color.y); if (r2 > 1.0) { // Off the disc: only a ring can draw here. float ringDepth; vec4 ring = ringSample(en, cLatR, ringDepth); if (ring.a < 0.004) discard; gl_FragDepth = 1.0 - clamp(ringDepth, -1.0, 1.0) * 0.5; outColor = vec4(ring.rgb * 0.9, ring.a); return; } float z = sqrt(1.0 - r2); vec3 sphereNormal = vec3(en, z); // Nearer (larger z) wins LESS, matching the globe tiles' depth regime. gl_FragDepth = 1.0 - z * 0.5; // Undo the camera view rotation, then invert the east/north/up basis // (mirrors `globe_lonlat`). Shading keeps the view-frame normal above. vec3 basis = viewUnrotate(sphereNormal); float sy = sin(cLatR); float cy = cos(cLatR); float ey = cy * basis.y + sy * basis.z; float ez = -sy * basis.y + cy * basis.z; float lat = asin(clamp(ey, -1.0, 1.0)) * 180.0 / PI; float lon = push.color.x + atan(basis.x, ez) * 180.0 / PI; lon = mod(lon + 180.0, 360.0) - 180.0; vec2 uv = vec2((lon + 180.0) / 360.0, (90.0 - lat) / 180.0); vec3 albedo = texture(moonColor, uv).rgb; vec3 n = sphereNormal; if (push.line.y > 0.0) { // Forward differences one DEM texel east and south. Equirectangular // texels span equal angles on both axes, so the gradients share a scale. vec2 texel = 1.0 / vec2(textureSize(moonDem, 0)); float hC = demMetres(uv); float hX = demMetres(uv + vec2(texel.x, 0.0)); float hY = demMetres(uv + vec2(0.0, texel.y)); float relief = push.misc.z; // Rising east tilts the normal west; rising south (uv.y grows // southward) tilts it north. n = normalize(sphereNormal + vec3(-(hX - hC), hY - hC, 0.0) * relief); } vec3 sun = normalize(vec3(0.8, 0.25, 0.55)); float diff = clamp(dot(n, sun), 0.0, 1.0); float shade = (0.25 + 0.75 * diff) * (0.35 + 0.65 * z); vec3 col = albedo * shade; // The ring's front arc crosses the disc: blend it over the planet where // the ring point sits nearer than the sphere surface at this pixel. // Both depths are view-frame z, so the test survives the view rotation. float ringDepth; vec4 ring = ringSample(en, cLatR, ringDepth); if (ring.a >= 0.004 && ringDepth > z) { col = mix(col, ring.rgb * 0.9, clamp(ring.a, 0.0, 1.0)); } outColor = vec4(col, 1.0); }