Skip to content

Instantly share code, notes, and snippets.

@BanchouBoo
Created May 24, 2019 17:52
Show Gist options
  • Save BanchouBoo/3a219148b29dbac4747321476c0df501 to your computer and use it in GitHub Desktop.
Save BanchouBoo/3a219148b29dbac4747321476c0df501 to your computer and use it in GitHub Desktop.
shader_type spatial;
uniform sampler2D noise;
uniform float noise_cutoff = 0.7;
uniform vec4 depth_gradient_shallow : hint_color = vec4(0.325, 0.807, 0.971, 0.725);
uniform vec4 depth_gradient_deep : hint_color = vec4(0.086, 0.407, 1, 0.749);
uniform vec4 foam_color : hint_color = vec4(1.0, 1.0, 1.0, 1.0);
uniform float depth_max_distance = 1.0;
uniform float foam_min_distance = 0.04;
uniform float foam_max_distance = 0.4;
uniform vec2 surface_noise_scroll = vec2(0.03);
uniform sampler2D surface_distortion_tex;
uniform float surface_distortion_amount = 0.05;
void fragment() {
float depth = texture(DEPTH_TEXTURE, SCREEN_UV, 0.0).r;
depth = depth * 2.0 - 1.0;
depth = PROJECTION_MATRIX[3][2] / (depth + PROJECTION_MATRIX[2][2]);
depth += VERTEX.z;
float normal_dot = clamp(dot(NORMAL, VIEW), 0, 1);
float depth_diff = depth - FRAGCOORD.w;
float water_depth_diff = clamp(depth_diff / depth_max_distance, 0, 1);
float foam_distance = mix(foam_max_distance, foam_min_distance, normal_dot);
float foam_depth_difference = clamp(depth_diff / foam_distance, 0, 1);
vec4 water_color = mix(depth_gradient_shallow, depth_gradient_deep, water_depth_diff);
vec2 noise_uv = UV;
vec2 distortion_sample = (texture(surface_distortion_tex, UV).xy * 2.0 - 1.0) * surface_distortion_amount;
noise_uv += (TIME * surface_noise_scroll) + distortion_sample;
float cutoff = foam_depth_difference * noise_cutoff;
float surface_noise_sample = texture(noise, noise_uv).r;
float surface_noise = smoothstep(cutoff - 0.01, cutoff + 0.01, surface_noise_sample);
vec4 surface_noise_color = foam_color;
surface_noise_color.a *= surface_noise;
ALBEDO = (surface_noise_color.rgb * surface_noise_color.a) + (water_color.rgb * (1.0 - surface_noise_color.a));
ALPHA = surface_noise_color.a + water_color.a * (1.0 - surface_noise_color.a);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment