Kelsidavis-WoWee/assets/shaders/celestial.frag.glsl
Kelsi 83d7f26f33 Fix visible square behind sun by switching celestial to additive blending
Alpha blending caused faint quad edges to be visible against the sky
gradient. Additive blending correctly adds glow light without the
outline artifact. Edge fade starts earlier and discard threshold raised.
2026-02-23 07:40:51 -08:00

62 lines
2.1 KiB
GLSL

#version 450
layout(push_constant) uniform Push {
mat4 model;
vec4 celestialColor; // xyz = color, w = unused
float intensity;
float moonPhase;
float animTime;
} push;
layout(location = 0) in vec2 TexCoord;
layout(location = 0) out vec4 outColor;
float valueNoise(vec2 p) {
vec2 i = floor(p);
vec2 f = fract(p);
f = f * f * (3.0 - 2.0 * f);
float a = fract(sin(dot(i, vec2(127.1, 311.7))) * 43758.5453);
float b = fract(sin(dot(i + vec2(1.0, 0.0), vec2(127.1, 311.7))) * 43758.5453);
float c = fract(sin(dot(i + vec2(0.0, 1.0), vec2(127.1, 311.7))) * 43758.5453);
float d = fract(sin(dot(i + vec2(1.0, 1.0), vec2(127.1, 311.7))) * 43758.5453);
return mix(mix(a, b, f.x), mix(c, d, f.x), f.y);
}
void main() {
vec2 uv = TexCoord - 0.5;
float dist = length(uv);
// Hard disc with smooth edge
float disc = smoothstep(0.42, 0.35, dist);
// Soft glow that fades to zero well within quad bounds
// At dist=0.5 (quad edge), this should be negligible
float glow = exp(-dist * dist * 18.0) * 0.5;
// Combine disc and glow
float alpha = max(disc, glow) * push.intensity;
// Fade to zero well before quad edges
float edgeFade = 1.0 - smoothstep(0.35, 0.48, dist);
alpha *= edgeFade;
vec3 color = push.celestialColor.rgb;
// Animated haze/turbulence overlay for the sun disc
if (push.intensity > 0.5) {
float noise = valueNoise(uv * 8.0 + vec2(push.animTime * 0.3, push.animTime * 0.2));
float noise2 = valueNoise(uv * 16.0 - vec2(push.animTime * 0.5, push.animTime * 0.15));
float turbulence = (noise * 0.6 + noise2 * 0.4) * disc;
color += vec3(turbulence * 0.3, turbulence * 0.15, 0.0);
}
// Moon phase shadow (only applied when intensity < 0.5, i.e. for moons)
float phaseX = uv.x * 2.0 + push.moonPhase;
float phaseShadow = smoothstep(-0.1, 0.1, phaseX);
alpha *= mix(phaseShadow, 1.0, step(0.5, push.intensity));
if (alpha < 0.003) discard;
// Pre-multiply for additive blending: RGB is the light contribution
outColor = vec4(color * alpha, alpha);
}