Significantly improved underwater shaders

This commit is contained in:
Joshua Barretto 2022-01-21 00:42:20 +00:00
parent c2644d8e71
commit 5b606f0d50
8 changed files with 99 additions and 36 deletions

View File

@ -16,6 +16,9 @@
#define LIGHTING_DISTRIBUTION LIGHTING_DISTRIBUTION_BECKMANN
// Must come before includes
#define IS_POSTPROCESS
#include <globals.glsl>
// Note: The sampler uniform is declared here because it differs for MSAA
#include <anti-aliasing.glsl>

View File

@ -177,7 +177,13 @@ void main() {
// Squared to account for prior saturation.
float f_light = 1.0;// pow(f_light, 1.5);
vec3 reflect_color = get_sky_color(/*reflect_ray_dir*/beam_view_dir, time_of_day.x, f_pos, vec3(-100000), 0.125, true);
reflect_color = get_cloud_color(reflect_color, reflect_ray_dir, f_pos.xyz, time_of_day.x, 100000.0, 0.1);
vec3 ray_dir;
if (medium.x == 1) {
ray_dir = (cam_to_frag + norm) / 2;
} else {
ray_dir = reflect_ray_dir;
}
reflect_color = get_cloud_color(reflect_color, ray_dir, f_pos.xyz, time_of_day.x, 100000.0, 0.1);
reflect_color *= f_light;
// Prevent the sky affecting light when underground
@ -254,7 +260,7 @@ void main() {
// vec3 light, diffuse_light, ambient_light;
// vec3 light_frac = /*vec3(1.0);*/light_reflection_factor(f_norm/*vec3(0, 0, 1.0)*/, view_dir, vec3(0, 0, -1.0), vec3(1.0), vec3(R_s), alpha);
// 0 = 100% reflection, 1 = translucent water
float passthrough = /*pow(*/dot(faceforward(norm, norm, cam_to_frag/*view_dir*/), -cam_to_frag/*view_dir*/)/*, 0.5)*/;
float passthrough = max(dot(norm, -cam_to_frag), 0);
float max_light = 0.0;
max_light += get_sun_diffuse2(sun_info, moon_info, norm, /*time_of_day.x*/sun_view_dir, f_pos, mu, cam_attenuation, fluid_alt, k_a/* * (shade_frac * 0.5 + light_frac * 0.5)*/, vec3(k_d), /*vec3(f_light * point_shadow)*//*reflect_color*/k_s, alpha, f_norm, 1.0, emitted_light, reflected_light);
@ -324,7 +330,10 @@ void main() {
// vec4 color = mix(vec4(reflect_color, 1.0), vec4(surf_color, 1.0 / (1.0 + /*diffuse_light*/(/*f_light * point_shadow*/reflected_light_point/* + point_light*//*reflected_light*/))), passthrough);
// float log_cam = log(min(cam_attenuation.r, min(cam_attenuation.g, cam_attenuation.b)));
float min_refl = min(emitted_light.r, min(emitted_light.g, emitted_light.b));
float min_refl = 0.0;
if (medium.x != 1) {
min_refl = min(emitted_light.r, min(emitted_light.g, emitted_light.b));
}
vec4 color = vec4(surf_color, (1.0 - passthrough) * 1.0 / (1.0 + min_refl));// * (1.0 - /*log(1.0 + cam_attenuation)*//*cam_attenuation*/1.0 / (2.0 - log_cam)));
// vec4 color = vec4(surf_color, mix(1.0, 1.0 / (1.0 + /*0.25 * *//*diffuse_light*/(/*f_light * point_shadow*/reflected_light_point)), passthrough));
// vec4 color = vec4(surf_color, mix(1.0, length(cam_attenuation), passthrough));

View File

@ -229,39 +229,58 @@ vec3 get_cloud_color(vec3 surf_color, vec3 dir, vec3 origin, const float time_of
vec3 sun_color = get_sun_color();
vec3 moon_color = get_moon_color();
float cdist = max_dist;
float ldist = cdist;
// i is an emergency brake
float min_dist = clamp(max_dist / 4, 0.25, 24);
int i;
for (i = 0; cdist > min_dist && i < 250; i ++) {
ldist = cdist;
cdist = step_to_dist(trunc(dist_to_step(cdist - 0.25, quality)), quality);
// Clouds aren't visible underwater
#ifdef IS_POSTPROCESS
if (medium.x != 1) {
#endif
float cdist = max_dist;
float ldist = cdist;
// i is an emergency brake
float min_dist = clamp(max_dist / 4, 0.25, 24);
int i;
for (i = 0; cdist > min_dist && i < 250; i ++) {
ldist = cdist;
cdist = step_to_dist(trunc(dist_to_step(cdist - 0.25, quality)), quality);
vec3 emission;
float not_underground; // Used to prevent sunlight leaking underground
// `sample` is a reserved keyword
vec4 sample_ = cloud_at(origin + dir * ldist * splay, ldist, emission, not_underground);
vec3 emission;
float not_underground; // Used to prevent sunlight leaking underground
// `sample` is a reserved keyword
vec4 sample_ = cloud_at(origin + dir * ldist * splay, ldist, emission, not_underground);
vec2 density_integrals = max(sample_.zw, vec2(0));
vec2 density_integrals = max(sample_.zw, vec2(0));
float sun_access = max(sample_.x, 0);
float moon_access = max(sample_.y, 0);
float cloud_scatter_factor = density_integrals.x;
float global_scatter_factor = density_integrals.y;
float sun_access = max(sample_.x, 0);
float moon_access = max(sample_.y, 0);
float cloud_scatter_factor = density_integrals.x;
float global_scatter_factor = density_integrals.y;
float step = (ldist - cdist) * 0.01;
float cloud_darken = pow(1.0 / (1.0 + cloud_scatter_factor), step);
float global_darken = pow(1.0 / (1.0 + global_scatter_factor), step);
float step = (ldist - cdist) * 0.01;
float cloud_darken = pow(1.0 / (1.0 + cloud_scatter_factor), step);
float global_darken = pow(1.0 / (1.0 + global_scatter_factor), step);
surf_color =
// Attenuate light passing through the clouds
surf_color * cloud_darken * global_darken +
// Add the directed light light scattered into the camera by the clouds and the atmosphere (global illumination)
sun_color * sun_scatter * get_sun_brightness() * (sun_access * (1.0 - cloud_darken) /*+ sky_color * global_scatter_factor*/) +
moon_color * moon_scatter * get_moon_brightness() * (moon_access * (1.0 - cloud_darken) /*+ sky_color * global_scatter_factor*/) +
sky_light * (1.0 - global_darken) * not_underground +
emission * density_integrals.y * step;
surf_color =
// Attenuate light passing through the clouds
surf_color * cloud_darken * global_darken +
// Add the directed light light scattered into the camera by the clouds and the atmosphere (global illumination)
sun_color * sun_scatter * get_sun_brightness() * (sun_access * (1.0 - cloud_darken) /*+ sky_color * global_scatter_factor*/) +
moon_color * moon_scatter * get_moon_brightness() * (moon_access * (1.0 - cloud_darken) /*+ sky_color * global_scatter_factor*/) +
sky_light * (1.0 - global_darken) * not_underground +
emission * density_integrals.y * step;
}
#ifdef IS_POSTPROCESS
}
#endif
if (medium.x == 1) {
float f_alt = alt_at(cam_pos.xy);
float fluid_alt = max(cam_pos.z + 1, floor(f_alt + 1));
float water_dist = clamp((fluid_alt - cam_pos.z) / max(dir.z, 0), 0, max_dist);
//float fade = 1.0 - clamp(water_dist * 0.01, 0, 1);//pow(0.97, water_dist);
float fade = pow(0.98, water_dist);
surf_color.rgb = mix(vec3(0, 0.5, 1) * get_sun_brightness() / max(1.0, (fluid_alt - cam_pos.z) * 0.5 - dir.z * 5), surf_color.rgb, fade/*pow(fade, 4)*/);
}
// Apply point glow

View File

@ -559,9 +559,14 @@ vec3 get_sky_color(vec3 dir, float time_of_day, vec3 origin, vec3 f_pos, float q
#if (CLOUD_MODE == CLOUD_MODE_NONE)
vec3 sky_color = get_sky_light(dir, time_of_day, true);
#else
vec3 star_dir = normalize(sun_dir.xyz * dir.z + cross(sun_dir.xyz, vec3(0, 1, 0)) * dir.x + vec3(0, 1, 0) * dir.y);
float star = is_star_at(star_dir);
vec3 sky_color = vec3(0) + star;
vec3 sky_color;
if (medium.x == 1) {
sky_color = get_sky_light(dir, time_of_day, true);
} else {
vec3 star_dir = normalize(sun_dir.xyz * dir.z + cross(sun_dir.xyz, vec3(0, 1, 0)) * dir.x + vec3(0, 1, 0) * dir.y);
float star = is_star_at(star_dir);
sky_color = vec3(0) + star;
}
#endif
return sky_color + sun_light + moon_light;

View File

@ -191,6 +191,13 @@ void main() {
// return;
// }
#ifdef EXPERIMENTAL_UNDERWARPER
vec2 uv = uv;
if (medium.x == 1) {
uv += sin(uv.yx * 30 + tick.xx * 1.0) * 0.005;
}
#endif
vec4 aa_color = aa_apply(t_src_color, s_src_color, uv * screen_res.xy, screen_res.xy);
// Bloom

View File

@ -275,7 +275,9 @@ void main() {
emitted_light = vec3(1.0);
reflected_light = vec3(1.0);
max_light += get_sun_diffuse2(/*time_of_day.x, */sun_info, moon_info, f_norm, view_dir, f_pos, mu, cam_attenuation, fluid_alt, k_a/* * (shade_frac * 0.5 + light_frac * 0.5)*/, k_d, k_s, alpha, f_norm, 1.0, emitted_light, reflected_light);
float sun_diffuse = get_sun_diffuse2(/*time_of_day.x, */sun_info, moon_info, f_norm, view_dir, f_pos, mu, cam_attenuation, fluid_alt, k_a/* * (shade_frac * 0.5 + light_frac * 0.5)*/, k_d, k_s, alpha, f_norm, 1.0, emitted_light, reflected_light);
max_light += sun_diffuse;
// emitted_light *= f_light * point_shadow * max(shade_frac, MIN_SHADOW);
// reflected_light *= f_light * point_shadow * shade_frac;
@ -284,6 +286,20 @@ void main() {
reflected_light *= f_light;
max_light *= f_light;
#define EXPERIMENTAL_CAUSTICS
#ifdef EXPERIMENTAL_CAUSTICS
#if (FLUID_MODE == FLUID_MODE_SHINY)
if (faces_fluid) {
vec3 wpos = f_pos + focus_off.xyz;
vec3 spos = (wpos + wpos.z * vec3(sun_dir.xy, 0)) * 0.05;
reflected_light += max(1.0 - pow(abs(noise_3d(vec3(spos.xy, tick.x * 0.1 + dot(sin(wpos.xy * 0.2), vec2(1)) * 0.1)) - 0.5) * 10, 0.001), 0)
* 500
* cam_attenuation
* sun_diffuse;
}
#endif
#endif
// TODO: Apply AO after this
vec3 glow = glow_light(f_pos) * (pow(f_glow, 3) * 5 + pow(f_glow, 2.0) * 2);
reflected_light += glow * pow(max(dot(face_norm, f_norm), 0), 2);

View File

@ -427,4 +427,8 @@ pub enum ExperimentalShader {
CurvedWorld,
/// Adds extra detail to distant LoD (Level of Detail) terrain procedurally.
ProceduralLodDetail,
/// Add a warping effect when underwater.
Underwarper,
/// Add caustics to underwater terrain.
Caustics,
}

View File

@ -97,7 +97,7 @@ impl FluidPipeline {
},
depth_stencil: Some(wgpu::DepthStencilState {
format: wgpu::TextureFormat::Depth32Float,
depth_write_enabled: false,
depth_write_enabled: true,
depth_compare: wgpu::CompareFunction::GreaterEqual,
stencil: wgpu::StencilState {
front: wgpu::StencilFaceState::IGNORE,