2020-01-19 03:58:25 +00:00
|
|
|
uniform sampler2D t_noise;
|
|
|
|
|
|
|
|
const float CLOUD_AVG_HEIGHT = 1025.0;
|
|
|
|
const float CLOUD_HEIGHT_MIN = CLOUD_AVG_HEIGHT - 50.0;
|
|
|
|
const float CLOUD_HEIGHT_MAX = CLOUD_AVG_HEIGHT + 50.0;
|
|
|
|
const float CLOUD_THRESHOLD = 0.3;
|
|
|
|
const float CLOUD_SCALE = 5.0;
|
2020-01-21 12:57:59 +00:00
|
|
|
const float CLOUD_DENSITY = 80.0;
|
2020-01-19 03:58:25 +00:00
|
|
|
|
|
|
|
float vsum(vec3 v) {
|
|
|
|
return v.x + v.y + v.z;
|
|
|
|
}
|
|
|
|
|
|
|
|
vec2 cloud_at(vec3 pos) {
|
2020-01-21 12:57:59 +00:00
|
|
|
vec2 scaled_pos = pos.xy / CLOUD_SCALE;
|
|
|
|
|
2020-01-19 03:58:25 +00:00
|
|
|
float tick_offs = 0.0
|
2020-01-21 12:57:59 +00:00
|
|
|
+ texture(t_noise, scaled_pos * 0.0005 - time_of_day.x * 0.00002).x * 0.5
|
|
|
|
+ texture(t_noise, scaled_pos * 0.000015).x * 5.0;
|
2020-01-19 03:58:25 +00:00
|
|
|
|
|
|
|
float value = (
|
|
|
|
0.0
|
2020-01-21 12:57:59 +00:00
|
|
|
+ texture(t_noise, scaled_pos * 0.0003 + tick_offs).x
|
|
|
|
+ texture(t_noise, scaled_pos * 0.0009 - tick_offs).x * 0.5
|
|
|
|
+ texture(t_noise, scaled_pos * 0.0025 - time_of_day.x * 0.0002).x * 0.25
|
|
|
|
+ texture(t_noise, scaled_pos * 0.008 + time_of_day.x * 0.0004).x * 0.15
|
|
|
|
+ texture(t_noise, scaled_pos * 0.02 + tick_offs + time_of_day.x * 0.0004).x * 0.1
|
2020-01-19 03:58:25 +00:00
|
|
|
) / 3.0;
|
|
|
|
|
|
|
|
float density = max((value - CLOUD_THRESHOLD) - abs(pos.z - CLOUD_AVG_HEIGHT) / 400.0, 0.0) * CLOUD_DENSITY;
|
|
|
|
|
2020-01-21 12:57:59 +00:00
|
|
|
const float SHADE_GRADIENT = 1.8 / (CLOUD_AVG_HEIGHT - CLOUD_HEIGHT_MIN);
|
|
|
|
float shade = ((pos.z - CLOUD_AVG_HEIGHT) * SHADE_GRADIENT + 0.5);
|
2020-01-19 03:58:25 +00:00
|
|
|
|
|
|
|
return vec2(shade, density / (1.0 + vsum(abs(pos - cam_pos.xyz)) / 5000));
|
|
|
|
}
|
|
|
|
|
|
|
|
vec4 get_cloud_color(vec3 dir, vec3 origin, float time_of_day, float max_dist, float quality) {
|
2020-01-21 12:57:59 +00:00
|
|
|
const int ITERS = 10;
|
|
|
|
const float INCR = 1.0 / ITERS;
|
2020-01-19 03:58:25 +00:00
|
|
|
|
|
|
|
float mind = (CLOUD_HEIGHT_MIN - origin.z) / dir.z;
|
|
|
|
float maxd = (CLOUD_HEIGHT_MAX - origin.z) / dir.z;
|
|
|
|
|
|
|
|
float start = max(min(mind, maxd), 0.0);
|
|
|
|
float delta = min(abs(mind - maxd), max_dist);
|
|
|
|
|
2020-01-31 10:12:57 +00:00
|
|
|
float fuzz = sin(texture(t_noise, dir.xz * 100000.0 + tick.x).x * 100.0) * INCR * delta * pow(abs(maxd - mind), 0.3) * 2.0;
|
2020-01-19 03:58:25 +00:00
|
|
|
|
|
|
|
float cloud_shade = 1.0;
|
|
|
|
float passthrough = 1.0;
|
2020-01-21 12:57:59 +00:00
|
|
|
if ((mind > 0.0 || maxd > 0.0) && start < max_dist) {
|
|
|
|
float dist = start;
|
|
|
|
for (int i = 0; i < ITERS; i ++) {
|
|
|
|
dist += fuzz * 0.01 * min(pow(dist * 0.005, 2.0), 1.0);
|
2020-01-19 03:58:25 +00:00
|
|
|
|
|
|
|
vec3 pos = origin + dir * min(dist, max_dist);
|
|
|
|
vec2 sample = cloud_at(pos);
|
|
|
|
|
2020-01-21 12:57:59 +00:00
|
|
|
float integral = sample.y * INCR;
|
|
|
|
passthrough *= 1.0 - integral;
|
2020-01-19 03:58:25 +00:00
|
|
|
cloud_shade = mix(cloud_shade, sample.x, passthrough * integral);
|
2020-01-21 12:57:59 +00:00
|
|
|
dist += INCR * delta;
|
2020-01-19 03:58:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
float total_density = 1.0 - passthrough / (1.0 + delta * 0.0001);
|
|
|
|
|
|
|
|
total_density = max(total_density - 1.0 / pow(max_dist, 0.25), 0.0); // Hack
|
|
|
|
|
|
|
|
return vec4(vec3(cloud_shade), total_density);
|
|
|
|
}
|