Made clouds render over terrain

This commit is contained in:
Joshua Barretto 2019-11-18 12:02:10 +00:00
parent 9d48cc8fb7
commit 2c2dd49aed
6 changed files with 21 additions and 19 deletions

View File

@ -39,8 +39,9 @@ void main() {
vec3 surf_color = illuminate(srgb_to_linear(model_col.rgb * f_col), light, diffuse_light, ambient_light); vec3 surf_color = illuminate(srgb_to_linear(model_col.rgb * f_col), light, diffuse_light, ambient_light);
float fog_level = fog(f_pos.xyz, focus_pos.xyz, medium.x); float fog_level = fog(f_pos.xyz, focus_pos.xyz, medium.x);
vec3 fog_color = get_sky_color(normalize(f_pos - cam_pos.xyz), time_of_day.x, f_pos, 0.5, true); vec4 clouds;
vec3 color = mix(surf_color, fog_color, fog_level); vec3 fog_color = get_sky_color(normalize(f_pos - cam_pos.xyz), time_of_day.x, f_pos, 0.5, true, clouds);
vec3 color = mix(mix(surf_color, fog_color, fog_level), clouds.rgb, clouds.a);
tgt_color = vec4(color, 1.0); tgt_color = vec4(color, 1.0);
} }

View File

@ -114,21 +114,21 @@ void main() {
vec3 surf_color = illuminate(srgb_to_linear(f_col), light, diffuse_light, ambient_light); vec3 surf_color = illuminate(srgb_to_linear(f_col), light, diffuse_light, ambient_light);
float fog_level = fog(f_pos.xyz, focus_pos.xyz, medium.x); float fog_level = fog(f_pos.xyz, focus_pos.xyz, medium.x);
vec3 fog_color = vec3(0); vec4 clouds;
if (fog_level > 0.0) vec3 fog_color = get_sky_color(normalize(f_pos - cam_pos.xyz), time_of_day.x, f_pos, 0.25, true, clouds);
fog_color = get_sky_color(normalize(f_pos - cam_pos.xyz), time_of_day.x, f_pos, 0.25, true);
vec3 reflect_ray_dir = reflect(cam_to_frag, norm); vec3 reflect_ray_dir = reflect(cam_to_frag, norm);
// Hack to prevent the reflection ray dipping below the horizon and creating weird blue spots in the water // Hack to prevent the reflection ray dipping below the horizon and creating weird blue spots in the water
reflect_ray_dir.z = max(reflect_ray_dir.z, 0.05); reflect_ray_dir.z = max(reflect_ray_dir.z, 0.05);
vec3 reflect_color = get_sky_color(reflect_ray_dir, time_of_day.x, vec3(-100000), 0.5, false) * f_light; vec4 _clouds;
vec3 reflect_color = get_sky_color(reflect_ray_dir, time_of_day.x, vec3(-100000), 0.5, false, _clouds) * f_light;
// Tint // Tint
reflect_color = mix(reflect_color, surf_color, 0.6); reflect_color = mix(reflect_color, surf_color, 0.6);
// 0 = 100% reflection, 1 = translucent water // 0 = 100% reflection, 1 = translucent water
float passthrough = pow(dot(faceforward(f_norm, f_norm, cam_to_frag), -cam_to_frag), 0.5); float passthrough = pow(dot(faceforward(f_norm, f_norm, cam_to_frag), -cam_to_frag), 0.5);
vec4 color = mix(vec4(reflect_color * 2.0, 1.0), vec4(surf_color, 4.0 / (4.0 + diffuse_light * 1.0)), passthrough); vec4 color = mix(vec4(reflect_color * 2.0, 1.0), vec4(surf_color, 1.0 / (1.0 + diffuse_light * 0.25)), passthrough);
tgt_color = mix(color, vec4(fog_color, 0.0), fog_level); tgt_color = mix(mix(color, vec4(fog_color, 0.0), fog_level), vec4(clouds.rgb, 0.0), clouds.a);
} }

View File

@ -185,7 +185,7 @@ vec4 get_cloud_color(vec3 dir, float time_of_day, float max_dist, float quality)
return vec4(vec3(cloud_shade), 1.0 - passthrough / (1.0 + min(delta, max_dist) * 0.0003)); return vec4(vec3(cloud_shade), 1.0 - passthrough / (1.0 + min(delta, max_dist) * 0.0003));
} }
vec3 get_sky_color(vec3 dir, float time_of_day, vec3 f_pos, float quality, bool with_stars) { vec3 get_sky_color(vec3 dir, float time_of_day, vec3 f_pos, float quality, bool with_stars, out vec4 clouds) {
// Sky color // Sky color
vec3 sun_dir = get_sun_dir(time_of_day); vec3 sun_dir = get_sun_dir(time_of_day);
vec3 moon_dir = get_moon_dir(time_of_day); vec3 moon_dir = get_moon_dir(time_of_day);
@ -261,10 +261,10 @@ vec3 get_sky_color(vec3 dir, float time_of_day, vec3 f_pos, float quality, bool
); );
// Approximate distance to fragment // Approximate distance to fragment
float f_dist = vsum(abs(cam_pos.xyz - f_pos)); float f_dist = distance(cam_pos.xyz, f_pos);
// Clouds // Clouds
vec4 clouds = get_cloud_color(dir, time_of_day, f_dist, quality); clouds = get_cloud_color(dir, time_of_day, f_dist, quality);
clouds.rgb *= get_sun_brightness(sun_dir) * (sun_halo * 2.5 + get_sun_color(sun_dir)) + get_moon_brightness(moon_dir) * (moon_halo * 20.5 + get_moon_color(moon_dir)); clouds.rgb *= get_sun_brightness(sun_dir) * (sun_halo * 2.5 + get_sun_color(sun_dir)) + get_moon_brightness(moon_dir) * (moon_halo * 20.5 + get_moon_color(moon_dir));
if (f_dist > 5000.0) { if (f_dist > 5000.0) {

View File

@ -13,5 +13,6 @@ uniform u_locals {
out vec4 tgt_color; out vec4 tgt_color;
void main() { void main() {
tgt_color = vec4(get_sky_color(normalize(f_pos), time_of_day.x, vec3(-100000), 1.0, true), 1.0); vec4 _clouds;
tgt_color = vec4(get_sky_color(normalize(f_pos), time_of_day.x, vec3(-100000), 1.0, true, _clouds), 1.0);
} }

View File

@ -27,8 +27,9 @@ void main() {
vec3 surf_color = illuminate(f_col, light, diffuse_light, ambient_light); vec3 surf_color = illuminate(f_col, light, diffuse_light, ambient_light);
float fog_level = fog(f_pos.xyz, focus_pos.xyz, medium.x); float fog_level = fog(f_pos.xyz, focus_pos.xyz, medium.x);
vec3 fog_color = get_sky_color(normalize(f_pos - cam_pos.xyz), time_of_day.x, f_pos, 0.5, true); vec4 clouds;
vec3 color = mix(surf_color, fog_color, fog_level); vec3 fog_color = get_sky_color(normalize(f_pos - cam_pos.xyz), time_of_day.x, f_pos, 0.5, true, clouds);
vec3 color = mix(mix(surf_color, fog_color, fog_level), clouds.rgb, clouds.a);
tgt_color = vec4(color, 1.0 - clamp((distance(focus_pos.xy, f_pos.xy) - (RENDER_DIST - FADE_DIST)) / FADE_DIST, 0, 1)); tgt_color = vec4(color, 1.0 - clamp((distance(focus_pos.xy, f_pos.xy) - (RENDER_DIST - FADE_DIST)) / FADE_DIST, 0, 1));
} }

View File

@ -20,7 +20,7 @@ out vec4 tgt_color;
void main() { void main() {
// First 3 normals are negative, next 3 are positive // First 3 normals are negative, next 3 are positive
vec3 normals[6] = vec3[]( vec3(-1,0,0), vec3(0,-1,0), vec3(0,0,-1), vec3(1,0,0), vec3(0,1,0), vec3(0,0,1) ); vec3 normals[6] = vec3[](vec3(-1,0,0), vec3(0,-1,0), vec3(0,0,-1), vec3(1,0,0), vec3(0,1,0), vec3(0,0,1));
// TODO: last 3 bits in v_pos_norm should be a number between 0 and 5, rather than 0-2 and a direction. // TODO: last 3 bits in v_pos_norm should be a number between 0 and 5, rather than 0-2 and a direction.
uint norm_axis = (f_pos_norm >> 30) & 0x3u; uint norm_axis = (f_pos_norm >> 30) & 0x3u;
@ -40,10 +40,9 @@ void main() {
vec3 surf_color = illuminate(srgb_to_linear(f_col), light, diffuse_light, ambient_light); vec3 surf_color = illuminate(srgb_to_linear(f_col), light, diffuse_light, ambient_light);
float fog_level = fog(f_pos.xyz, focus_pos.xyz, medium.x); float fog_level = fog(f_pos.xyz, focus_pos.xyz, medium.x);
vec3 fog_color = vec3(0); vec4 clouds;
if (fog_level > 0.0) vec3 fog_color = get_sky_color(normalize(f_pos - cam_pos.xyz), time_of_day.x, f_pos, 0.5, true, clouds);
fog_color = get_sky_color(normalize(f_pos - cam_pos.xyz), time_of_day.x, f_pos, 0.5, true); vec3 color = mix(mix(surf_color, fog_color, fog_level), clouds.rgb, clouds.a);
vec3 color = mix(surf_color, fog_color, fog_level);
tgt_color = vec4(color, 1.0); tgt_color = vec4(color, 1.0);
} }