Advertisement
Outside_of_Awareness

Godot Water Shader

Feb 18th, 2024 (edited)
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. shader_type spatial;
  2.  
  3. uniform vec3 albedo : source_color;
  4. uniform vec3 albedo2 : source_color;
  5. uniform float metallic : hint_range(0.0, 1.0) = 0;
  6. uniform float roughness : hint_range(0.0, 1.0) = 0.02;
  7. uniform sampler2D wave;
  8. uniform sampler2D texture_normal;
  9. uniform sampler2D texture_normal2;
  10. uniform vec2 wave_direction = vec2(2.0,0.0);
  11. uniform vec2 wave_2_direction = vec2(0.0,1.0);
  12. uniform float time_scale : hint_range(0.0, 0.2, 0.005) = 0.025; // Rate of movement multiplied by TIME
  13. uniform float noise_scale = 10.0;
  14. uniform float height_scale = 0.01;
  15. // Depth Effect
  16. uniform vec4 color_deep : source_color;
  17. uniform vec4 color_shallow : source_color;
  18. uniform float beers_law = 2.0;
  19. uniform float depth_offset = -0.75;
  20. uniform sampler2D DEPTH_TEXTURE : hint_depth_texture, filter_linear_mipmap;
  21. uniform sampler2D SCREEN_TEXTURE : hint_screen_texture, filter_linear_mipmap;
  22. //Edge Detect Effect
  23. uniform float edge_scale = 0.1;
  24. uniform float near = 2.0;
  25. uniform float far = 100.0;
  26. uniform vec3 edge_color : source_color;
  27.  
  28. varying float height;
  29. varying vec3 world_pos;
  30.  
  31. float fresnel(float amount, vec3 normal, vec3 view)
  32. {
  33.     return pow((1.0 - clamp(dot(normalize(normal), normalize(view)), 0.0, 1.0 )), amount);
  34. }
  35. float edge(float depth) {
  36.     depth = 2.0 * depth - 1.0;
  37.     return near * far / (far + depth * (near - far));
  38. }
  39.  
  40. void vertex() {
  41.     // Called for every vertex the material is visible on.
  42.     world_pos = (MODEL_MATRIX * vec4(VERTEX, 1.0)).xyz;
  43.     height = texture(wave, world_pos.xz / noise_scale + TIME * time_scale).r;
  44.     VERTEX.y += height * height_scale;
  45.     }
  46.  
  47. void fragment() {
  48.     // Called for every pixel the material is visible on.
  49.     // Getting edge depth calc
  50. float z_depth = edge(texture(DEPTH_TEXTURE, SCREEN_UV).x);
  51. float z_pos = edge(FRAGCOORD.z);
  52. float z_dif = z_depth - z_pos;
  53. // Time calculations for wave (normal map) movement
  54. vec2 time = (TIME * wave_direction) * time_scale;
  55. vec2 time2 = (TIME * wave_2_direction) * time_scale;
  56. // Blend normal maps into one
  57. vec3 normal_blend = mix(texture(texture_normal,UV + TIME * time_scale).rgb, texture(texture_normal2,UV + time2).rgb, 0.5);
  58. // Calculate Fresnel
  59. float fresnel = fresnel(30.0, NORMAL, VIEW);
  60. vec3 surface_color = mix(albedo, albedo2, fresnel); // Interpolate albedo values by frensel
  61.    
  62.     float depth_texture = texture(DEPTH_TEXTURE, SCREEN_UV).r * 2.0 - 1.0;
  63.     float depth = PROJECTION_MATRIX[3][2] / (depth_texture + PROJECTION_MATRIX[2][2]);
  64.     float depth_blend = exp((depth+VERTEX.z + depth_offset) * -beers_law);
  65.     depth_blend = clamp(1.0 - depth_blend, 0.0, 1.0);  
  66.     float depth_blend_power = clamp(pow(depth_blend, 2.5), 0.0, 1.0);
  67.  
  68.     vec3 screen_color = textureLod(SCREEN_TEXTURE, SCREEN_UV, depth_blend_power * 2.5).rgb;
  69.     vec3 depth_color = mix(color_shallow.rgb, color_deep.rgb, depth_blend_power);
  70.     vec3 color = mix(screen_color * depth_color, depth_color * 0.25, depth_blend_power * 0.5);
  71.     vec3 depth_color_adj = mix(edge_color, color, step(edge_scale, z_dif));
  72.    
  73. ALBEDO = clamp(surface_color + depth_color_adj,vec3(0.0),vec3(1.0));
  74. METALLIC = metallic;
  75. ROUGHNESS = roughness;
  76. NORMAL_MAP = normal_blend;
  77. }
  78.  
  79. //void light() {
  80.     // Called for every pixel for every light affecting the material.
  81.     // Uncomment to replace the default light processing function with this one.
  82. //}
  83.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement