Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / games / voxels / src / main / rust / shaders / sky.frag
sky.frag184 lines · 7.3 KiBRaw
| 1 | #version 450 |
| 2 | layout(location=0) in vec2 vNdc; |
| 3 | |
| 4 | layout(binding=0) uniform Ubo { |
| 5 | mat4 viewProj; |
| 6 | vec3 sunDir; |
| 7 | float time; |
| 8 | vec3 fogColor; |
| 9 | float fogDensity; |
| 10 | vec4 playerPos; |
| 11 | float dayFactor; |
| 12 | // Precipitation intensity, 0..1. Occupies what the other shaders leave as implicit std140 padding |
| 13 | // between dayFactor and the mat4 that follows, so the block layout is identical either way. |
| 14 | float rain; |
| 15 | float _padA; |
| 16 | float _padB; |
| 17 | mat4 invViewProj; |
| 18 | vec3 sunColor; |
| 19 | float cloudShadow; |
| 20 | vec3 ambientColor; |
| 21 | float _pad2; |
| 22 | }; |
| 23 | |
| 24 | layout(location=0) out vec4 outColor; |
| 25 | |
| 26 | const float PI = 3.14159265; |
| 27 | |
| 28 | // ---- Sky (Rayleigh) --------------------------------------------------------- |
| 29 | vec3 skyColor(vec3 d, vec3 s) { |
| 30 | d = normalize(d); |
| 31 | s = normalize(s); |
| 32 | float mu = dot(d, s); |
| 33 | float h = clamp(d.y, 0.0, 1.0); |
| 34 | float sunUp = smoothstep(-0.10, 0.22, s.y); |
| 35 | |
| 36 | vec3 betaR = vec3(0.19, 0.45, 1.0); |
| 37 | float phaseR = 0.75 * (1.0 + mu * mu); |
| 38 | float viewAir = 1.0 / (h * 0.85 + 0.15); |
| 39 | float sunAir = 1.0 / (max(s.y, 0.0) * 0.9 + 0.12); |
| 40 | vec3 sunTransmit = exp(-betaR * sunAir * 0.35); |
| 41 | |
| 42 | vec3 day = betaR * phaseR * (0.10 + 0.30 * viewAir) * sunTransmit; |
| 43 | day = mix(day, vec3(dot(day, vec3(0.33)) + 0.30), (1.0 - h) * 0.35); |
| 44 | |
| 45 | float low = 1.0 - sunUp; |
| 46 | vec3 warm = vec3(1.0, 0.42, 0.18); |
| 47 | float warmMix = low * (0.35 + 0.65 * max(mu, 0.0) * max(mu, 0.0)) * (1.0 - h * 0.6); |
| 48 | day = mix(day, warm, clamp(warmMix, 0.0, 0.8)); |
| 49 | |
| 50 | float glow = pow(max(mu, 0.0), 8.0) * 0.5 + pow(max(mu, 0.0), 350.0) * 4.0; |
| 51 | vec3 sunTint = mix(vec3(1.0, 0.5, 0.2), vec3(1.0, 0.96, 0.9), sunUp); |
| 52 | day += sunTint * glow; |
| 53 | |
| 54 | vec3 night = mix(vec3(0.015, 0.02, 0.05), vec3(0.03, 0.05, 0.10), h); |
| 55 | float twi = smoothstep(-0.22, 0.02, s.y) * (1.0 - sunUp); |
| 56 | night += vec3(0.18, 0.08, 0.10) * twi * (1.0 - h * 0.7); |
| 57 | |
| 58 | return max(mix(night, day, sunUp), vec3(0.0)); |
| 59 | } |
| 60 | |
| 61 | // ---- Noise (2-octave value noise fBm) --------------------------------------- |
| 62 | float hash(vec3 p) { |
| 63 | p = fract(p * 0.3183099 + 0.1); |
| 64 | p *= 17.0; |
| 65 | return fract(p.x * p.y * p.z * (p.x + p.y + p.z)); |
| 66 | } |
| 67 | float vnoise(vec3 x) { |
| 68 | vec3 i = floor(x); |
| 69 | vec3 f = fract(x); |
| 70 | f = f * f * (3.0 - 2.0 * f); |
| 71 | return mix(mix(mix(hash(i + vec3(0,0,0)), hash(i + vec3(1,0,0)), f.x), |
| 72 | mix(hash(i + vec3(0,1,0)), hash(i + vec3(1,1,0)), f.x), f.y), |
| 73 | mix(mix(hash(i + vec3(0,0,1)), hash(i + vec3(1,0,1)), f.x), |
| 74 | mix(hash(i + vec3(0,1,1)), hash(i + vec3(1,1,1)), f.x), f.y), f.z); |
| 75 | } |
| 76 | float fbm2(vec3 p) { return 0.6 * vnoise(p) + 0.4 * vnoise(p * 2.03 + 1.7); } |
| 77 | |
| 78 | // ---- Volumetric clouds ------------------------------------------------------ |
| 79 | const float CLOUD_BOT = 120.0; |
| 80 | const float CLOUD_TOP = 210.0; |
| 81 | const float COVERAGE = 0.47; // 0..1 |
| 82 | const float SIGMA_V = 0.12; // view-ray extinction per density-length |
| 83 | const float SIGMA_L = 0.02; // light-ray extinction per density-length |
| 84 | |
| 85 | float cloudDensity(vec3 p) { |
| 86 | // Fast wind drift (x,z) + moving vertical slice (y) so shapes race across and morph. |
| 87 | vec3 q = p * 0.0045 + vec3(time * 0.06, time * 0.04, time * 0.045); |
| 88 | // Domain warp: shift the sample position by a lower-frequency noise so clouds are billowy/rounded |
| 89 | // instead of grid-aligned rectangular lumps from the raw value noise (cheap 1-tap warp). |
| 90 | q += 0.7 * (fbm2(q * 0.55) - 0.5); |
| 91 | float base = fbm2(q); |
| 92 | float d = smoothstep(1.0 - COVERAGE, 1.0 - COVERAGE * 0.25, base); // softer, fluffier edges |
| 93 | d -= 0.15 * fbm2(q * 3.0 + vec3(0.0, time * 0.2, 0.0) + 4.0); // evolving wispy detail |
| 94 | float hn = (p.y - CLOUD_BOT) / (CLOUD_TOP - CLOUD_BOT); |
| 95 | float grad = smoothstep(0.0, 0.28, hn) * smoothstep(1.0, 0.5, hn); // rounder top/bottom |
| 96 | return clamp(d, 0.0, 1.0) * grad; |
| 97 | } |
| 98 | |
| 99 | // Cheap density for the sun-ward light march: base coverage only, no domain warp or detail octave. |
| 100 | // Self-shadowing is low-frequency, so dropping the fine noise here is nearly invisible but ~3x cheaper. |
| 101 | float cloudDensityLow(vec3 p) { |
| 102 | vec3 q = p * 0.0045 + vec3(time * 0.06, time * 0.04, time * 0.045); |
| 103 | float base = fbm2(q); |
| 104 | float d = smoothstep(1.0 - COVERAGE, 1.0 - COVERAGE * 0.25, base); |
| 105 | float hn = (p.y - CLOUD_BOT) / (CLOUD_TOP - CLOUD_BOT); |
| 106 | float grad = smoothstep(0.0, 0.28, hn) * smoothstep(1.0, 0.5, hn); |
| 107 | return clamp(d, 0.0, 1.0) * grad; |
| 108 | } |
| 109 | |
| 110 | float hgPhase(float mu, float g) { |
| 111 | float g2 = g * g; |
| 112 | return (1.0 - g2) / (4.0 * PI * pow(max(1.0 + g2 - 2.0 * g * mu, 1e-3), 1.5)); |
| 113 | } |
| 114 | |
| 115 | // rgb = in-scattered light, a = view-ray transmittance. |
| 116 | vec4 renderClouds(vec3 ro, vec3 rd, vec3 s, vec3 skyBg, float sunUp) { |
| 117 | if (rd.y <= 0.02) return vec4(0.0, 0.0, 0.0, 1.0); |
| 118 | float t0 = (CLOUD_BOT - ro.y) / rd.y; |
| 119 | float t1 = (CLOUD_TOP - ro.y) / rd.y; |
| 120 | if (t1 < t0) { float tmp = t0; t0 = t1; t1 = tmp; } |
| 121 | t0 = max(t0, 0.0); |
| 122 | t1 = min(t1, 7000.0); |
| 123 | if (t1 <= t0) return vec4(0.0, 0.0, 0.0, 1.0); |
| 124 | |
| 125 | const int STEPS = 14; |
| 126 | float stepLen = (t1 - t0) / float(STEPS); |
| 127 | float t = t0 + stepLen * hash(rd * 91.7); // jitter to hide banding |
| 128 | |
| 129 | vec3 sunCol = sunColor; // shared with terrain lighting |
| 130 | vec3 ambient = skyBg * 0.5 + ambientColor * 0.4; // sky backdrop + shared ambient |
| 131 | float mu = dot(normalize(rd), s); |
| 132 | float phase = mix(hgPhase(mu, 0.70), hgPhase(mu, -0.20), 0.35); // forward glow + soft back lobe |
| 133 | |
| 134 | float transmittance = 1.0; |
| 135 | vec3 scattered = vec3(0.0); |
| 136 | |
| 137 | for (int i = 0; i < STEPS; i++) { |
| 138 | vec3 p = ro + rd * t; |
| 139 | float dens = cloudDensity(p); |
| 140 | if (dens > 0.01) { |
| 141 | // Light march toward the sun for self-shadowing. |
| 142 | float od = 0.0; |
| 143 | float ls = 45.0; |
| 144 | for (int j = 0; j < 2; j++) { |
| 145 | od += cloudDensityLow(p + s * ls * (float(j) + 0.7)) * ls; |
| 146 | } |
| 147 | float lightT = exp(-od * SIGMA_L); |
| 148 | float powder = 1.0 - exp(-dens * 2.0); |
| 149 | // Direct sun (phase glow + multiscatter baseline) + sky ambient. |
| 150 | vec3 lum = sunCol * lightT * (phase * 2.0 + 0.6) * powder + ambient; |
| 151 | float att = exp(-dens * stepLen * SIGMA_V); |
| 152 | scattered += transmittance * (1.0 - att) * lum; |
| 153 | transmittance *= att; |
| 154 | if (transmittance < 0.02) break; |
| 155 | } |
| 156 | t += stepLen; |
| 157 | } |
| 158 | return vec4(scattered, transmittance); |
| 159 | } |
| 160 | |
| 161 | void main() { |
| 162 | vec4 np = invViewProj * vec4(vNdc, 0.0, 1.0); |
| 163 | vec4 fp = invViewProj * vec4(vNdc, 1.0, 1.0); |
| 164 | vec3 dir = normalize(fp.xyz / fp.w - np.xyz / np.w); |
| 165 | vec3 s = normalize(sunDir); |
| 166 | float sunUp = smoothstep(-0.10, 0.22, s.y); |
| 167 | |
| 168 | // Sky background only. Clouds are a separate depth-tested volumetric pass (cloud.frag) so they |
| 169 | // sit at a real world altitude and interact with terrain depth. |
| 170 | vec3 col = skyColor(dir, s); |
| 171 | |
| 172 | // Overcast: collapse the Rayleigh gradient toward a flat grey that dims with the sun, so a storm |
| 173 | // reads as heavy rather than merely desaturated. |
| 174 | if (rain > 0.0) { |
| 175 | float lum = dot(col, vec3(0.30, 0.59, 0.11)); |
| 176 | vec3 overcast = mix(vec3(0.020, 0.024, 0.030), vec3(0.30, 0.32, 0.35), sunUp); |
| 177 | overcast *= 0.55 + 0.45 * clamp(lum * 3.0, 0.0, 1.0); |
| 178 | col = mix(col, overcast, clamp(rain * 0.85, 0.0, 1.0)); |
| 179 | } |
| 180 | |
| 181 | if (playerPos.w > 0.5) { col = mix(col, vec3(0.05, 0.20, 0.32), 0.85); } // underwater |
| 182 | |
| 183 | outColor = vec4(col, 1.0); // linear HDR; tonemap in composite |
| 184 | } |