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

Modern-Apps.git / games / voxels / src / main / rust / shaders / cloud.frag

cloud.frag169 lines · 6.7 KiBRaw
1#version 450
2// Volumetric clouds as a real depth-tested layer at a fixed world-space altitude slab
3// [CLOUD_BOT, CLOUD_TOP]. Raymarched from the camera through the slab for ANY view direction, so you
4// can stand under them, fly up into them, or look down on them from above. Per-fragment gl_FragDepth
5// (entry point of the slab) lets terrain occlude clouds and clouds occlude terrain behind them.
6layout(location=0) in vec2 vNdc;
7
8layout(binding=0) uniform Ubo {
9 mat4 viewProj;
10 vec3 sunDir;
11 float time;
12 vec3 fogColor;
13 float fogDensity;
14 vec4 playerPos;
15 float dayFactor;
16 // Precipitation intensity, 0..1. Occupies what the other shaders leave as implicit std140 padding
17 // between dayFactor and the mat4 that follows, so the block layout is identical either way.
18 float rain;
19 float _padA;
20 float _padB;
21 mat4 invViewProj;
22 vec3 sunColor;
23 float cloudShadow;
24 vec3 ambientColor;
25 float _pad2;
26};
27
28layout(location=0) out vec4 outColor;
29
30const float PI = 3.14159265;
31
32// ---- Sky background colour (used as cloud ambient; matches sky.frag) -----------------------------
33vec3 skyColor(vec3 d, vec3 s) {
34 d = normalize(d);
35 s = normalize(s);
36 float mu = dot(d, s);
37 float h = clamp(d.y, 0.0, 1.0);
38 float sunUp = smoothstep(-0.10, 0.22, s.y);
39 vec3 betaR = vec3(0.19, 0.45, 1.0);
40 float phaseR = 0.75 * (1.0 + mu * mu);
41 float viewAir = 1.0 / (h * 0.85 + 0.15);
42 float sunAir = 1.0 / (max(s.y, 0.0) * 0.9 + 0.12);
43 vec3 sunTransmit = exp(-betaR * sunAir * 0.35);
44 vec3 day = betaR * phaseR * (0.10 + 0.30 * viewAir) * sunTransmit;
45 day = mix(day, vec3(dot(day, vec3(0.33)) + 0.30), (1.0 - h) * 0.35);
46 float low = 1.0 - sunUp;
47 vec3 warm = vec3(1.0, 0.42, 0.18);
48 float warmMix = low * (0.35 + 0.65 * max(mu, 0.0) * max(mu, 0.0)) * (1.0 - h * 0.6);
49 day = mix(day, warm, clamp(warmMix, 0.0, 0.8));
50 vec3 night = mix(vec3(0.015, 0.02, 0.05), vec3(0.03, 0.05, 0.10), h);
51 float twi = smoothstep(-0.22, 0.02, s.y) * (1.0 - sunUp);
52 night += vec3(0.18, 0.08, 0.10) * twi * (1.0 - h * 0.7);
53 return max(mix(night, day, sunUp), vec3(0.0));
54}
55
56// ---- Noise ---------------------------------------------------------------------------------------
57float hash(vec3 p) {
58 p = fract(p * 0.3183099 + 0.1);
59 p *= 17.0;
60 return fract(p.x * p.y * p.z * (p.x + p.y + p.z));
61}
62float vnoise(vec3 x) {
63 vec3 i = floor(x);
64 vec3 f = fract(x);
65 f = f * f * (3.0 - 2.0 * f);
66 return mix(mix(mix(hash(i + vec3(0,0,0)), hash(i + vec3(1,0,0)), f.x),
67 mix(hash(i + vec3(0,1,0)), hash(i + vec3(1,1,0)), f.x), f.y),
68 mix(mix(hash(i + vec3(0,0,1)), hash(i + vec3(1,0,1)), f.x),
69 mix(hash(i + vec3(0,1,1)), hash(i + vec3(1,1,1)), f.x), f.y), f.z);
70}
71float fbm2(vec3 p) { return 0.6 * vnoise(p) + 0.4 * vnoise(p * 2.03 + 1.7); }
72
73// ---- Cloud volume --------------------------------------------------------------------------------
74const float CLOUD_BOT = 120.0;
75const float CLOUD_TOP = 210.0;
76const float COVERAGE = 0.47;
77const float SIGMA_V = 0.12;
78const float SIGMA_L = 0.02;
79
80// Coverage is a parameter rather than a constant so rain can thicken the deck into a solid overcast.
81float cloudDensity(vec3 p, float cov) {
82 vec3 q = p * 0.0045 + vec3(time * 0.06, time * 0.04, time * 0.045);
83 q += 0.7 * (fbm2(q * 0.55) - 0.5);
84 float base = fbm2(q);
85 float d = smoothstep(1.0 - cov, 1.0 - cov * 0.25, base);
86 d -= 0.15 * fbm2(q * 3.0 + vec3(0.0, time * 0.2, 0.0) + 4.0);
87 float hn = (p.y - CLOUD_BOT) / (CLOUD_TOP - CLOUD_BOT);
88 float grad = smoothstep(0.0, 0.28, hn) * smoothstep(1.0, 0.5, hn);
89 return clamp(d, 0.0, 1.0) * grad;
90}
91float cloudDensityLow(vec3 p, float cov) {
92 vec3 q = p * 0.0045 + vec3(time * 0.06, time * 0.04, time * 0.045);
93 float base = fbm2(q);
94 float d = smoothstep(1.0 - cov, 1.0 - cov * 0.25, base);
95 float hn = (p.y - CLOUD_BOT) / (CLOUD_TOP - CLOUD_BOT);
96 float grad = smoothstep(0.0, 0.28, hn) * smoothstep(1.0, 0.5, hn);
97 return clamp(d, 0.0, 1.0) * grad;
98}
99
100float hgPhase(float mu, float g) {
101 float g2 = g * g;
102 return (1.0 - g2) / (4.0 * PI * pow(max(1.0 + g2 - 2.0 * g * mu, 1e-3), 1.5));
103}
104
105void main() {
106 vec4 np = invViewProj * vec4(vNdc, 0.0, 1.0);
107 vec4 fp = invViewProj * vec4(vNdc, 1.0, 1.0);
108 vec3 rd = normalize(fp.xyz / fp.w - np.xyz / np.w);
109 vec3 ro = playerPos.xyz;
110 vec3 s = normalize(sunDir);
111
112 // Intersect the ray with the fixed altitude slab for ANY direction (up, level, or down).
113 float t0, t1;
114 if (abs(rd.y) < 1e-4) {
115 if (ro.y < CLOUD_BOT || ro.y > CLOUD_TOP) discard; // level ray, outside the slab -> no clouds
116 t0 = 0.0; t1 = 4000.0;
117 } else {
118 float ta = (CLOUD_BOT - ro.y) / rd.y;
119 float tb = (CLOUD_TOP - ro.y) / rd.y;
120 t0 = max(min(ta, tb), 0.0);
121 t1 = max(ta, tb);
122 if (t1 <= 0.0) discard; // slab entirely behind the camera
123 }
124 t1 = min(t1, 6000.0);
125 if (t1 <= t0) discard;
126
127 // Depth of the slab entry point -> lets the depth test occlude clouds behind terrain and vice
128 // versa. (Writing gl_FragDepth disables early-z, but the discards above skip the cheap cases.)
129 vec3 entry = ro + rd * t0;
130 vec4 clip = viewProj * vec4(entry, 1.0);
131 gl_FragDepth = clamp(clip.z / clip.w, 0.0, 1.0);
132
133 const int STEPS = 14;
134 float stepLen = (t1 - t0) / float(STEPS);
135 float t = t0 + stepLen * hash(rd * 91.7);
136 float cov = clamp(COVERAGE + rain * 0.30, 0.0, 0.95);
137 float sigmaV = SIGMA_V * (1.0 + rain * 1.8);
138 vec3 sunCol = sunColor * (1.0 - rain * 0.55);
139 vec3 skyBg = skyColor(rd, s);
140 vec3 ambient = skyBg * 0.5 + ambientColor * 0.4;
141 float mu = dot(rd, s);
142 float phase = mix(hgPhase(mu, 0.70), hgPhase(mu, -0.20), 0.35);
143
144 float transmittance = 1.0;
145 vec3 scattered = vec3(0.0);
146 for (int i = 0; i < STEPS; i++) {
147 vec3 p = ro + rd * t;
148 float dens = cloudDensity(p, cov);
149 if (dens > 0.01) {
150 float od = 0.0;
151 float ls = 45.0;
152 for (int j = 0; j < 2; j++) {
153 od += cloudDensityLow(p + s * ls * (float(j) + 0.7), cov) * ls;
154 }
155 float lightT = exp(-od * SIGMA_L);
156 float powder = 1.0 - exp(-dens * 2.0);
157 vec3 lum = sunCol * lightT * (phase * 2.0 + 0.6) * powder + ambient;
158 float att = exp(-dens * stepLen * sigmaV);
159 scattered += transmittance * (1.0 - att) * lum;
160 transmittance *= att;
161 if (transmittance < 0.02) break;
162 }
163 t += stepLen;
164 }
165
166 float alpha = 1.0 - transmittance;
167 if (alpha < 0.003) discard; // nothing hit along this ray
168 outColor = vec4(scattered, alpha); // premultiplied: blend ONE, ONE_MINUS_SRC_ALPHA over the scene
169}