Created
May 2, 2019 20:37
-
-
Save AndreaCatania/ed1cb67dfbcea0c6f342b8f332c46f79 to your computer and use it in GitHub Desktop.
Godot fluid shader using flex engine
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
shader_type spatial; | |
render_mode blend_mix, unshaded; | |
uniform float water_transparency = 0.1; | |
uniform vec4 water_color: hint_color; | |
uniform float water_distortion = 20; | |
uniform vec2 water_distortion_direction = vec2(0, 1); | |
vec4 blur(sampler2D tex, vec2 current_uv, vec2 pixel_size, int passes ){ | |
vec2 uv; | |
vec4 color = vec4(0.0); | |
int c = 0; | |
for(int i = 1; i <= passes; ++i){ | |
float f_i = float(i); | |
//float factor = 5.0 - pow((f_i / float(passes)), 0.3) * 5.0; | |
float factor = (f_i / float(passes)); | |
uv = current_uv + pixel_size * vec2(1, 0) * f_i; | |
if( uv.x >= 0.0 && uv.y >= 0.0 && uv.x <= 1.0 && uv.y <= 1.0 ){ | |
vec4 t = texture(tex, uv); | |
if(t.a >= 0.0){ | |
color += texture(tex, uv) * factor; | |
c++; | |
} | |
} | |
uv = current_uv + pixel_size * vec2(-1, 0) * f_i; | |
if( uv.x >= 0.0 && uv.y >= 0.0 && uv.x <= 1.0 && uv.y <= 1.0 ){ | |
vec4 t = texture(tex, uv); | |
if(t.a >= 0.0){ | |
color += texture(tex, uv) * factor; | |
c++; | |
} | |
} | |
uv = current_uv + pixel_size * vec2(0, 1) * f_i; | |
if( uv.x >= 0.0 && uv.y >= 0.0 && uv.x <= 1.0 && uv.y <= 1.0 ){ | |
vec4 t = texture(tex, uv); | |
if(t.a >= 0.0){ | |
color += texture(tex, uv) * factor; | |
c++; | |
} | |
} | |
uv = current_uv + pixel_size * vec2(0, -1) * f_i; | |
if( uv.x >= 0.0 && uv.y >= 0.0 && uv.x <= 1.0 && uv.y <= 1.0 ){ | |
vec4 t = texture(tex, uv); | |
if(t.a >= 0.0){ | |
color += texture(tex, uv) * factor; | |
c++; | |
} | |
} | |
} | |
if( c > 0 ) | |
return color / float(c); | |
return vec4(0); | |
} | |
void fragment(){ | |
/// Documentation: | |
/// | |
/// vec4 extracted_normal_and_depth = texture(FLUID_NORMAL_DEPTH_TEXTURE, FLUID_SCREEN_UV); | |
/// vec4 extracted_tickness = texture(FLUID_THICKNESS_TEXTURE, FLUID_SCREEN_UV); | |
/// FLUID_NORMAL = extracted_normal_and_depth.xyz; | |
/// FLUID_DEPTH = extracted_normal_and_depth.w; | |
/// FLUID_THICKNESS = extracted_tickness.x; | |
/// Note: | |
/// Before use these please blur | |
float clamped_fluid_thickness = min(FLUID_THICKNESS, 1.0); | |
vec4 normal_depth = blur(FLUID_NORMAL_DEPTH_TEXTURE, SCREEN_UV, SCREEN_PIXEL_SIZE, 5); | |
vec3 _normal = normal_depth.rgb; | |
float _depth = normal_depth.a; | |
vec2 uv = SCREEN_UV; | |
// Light distortion along plane | |
uv += SCREEN_PIXEL_SIZE * clamped_fluid_thickness * water_distortion_direction * water_distortion * 4.0; | |
// Light distortion along border | |
uv += SCREEN_PIXEL_SIZE * pow(1.0 - _normal.y, 4.) * water_distortion_direction * water_distortion; | |
vec3 light_color = vec3(1); | |
vec3 shadow_color = vec3(0); | |
vec3 color = texture(SCREEN_TEXTURE, uv).rgb; | |
color = mix(color, light_color, clamp(pow(_normal.y - 0.53, 0.4), 0, 1)); | |
color = mix(shadow_color, color, clamp(pow(_normal.y + 0.45, 0.4), 0, 1)); | |
color = mix(color, water_color.rgb, min(pow(_depth + 0.5, 7.0), 1.0)); | |
ALBEDO = color; | |
ALPHA = 1.0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment