water2D_material.tres 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. [gd_resource type="ShaderMaterial" load_steps=6 format=2]
  2. [sub_resource type="Shader" id=1]
  3. code = "shader_type canvas_item;
  4. uniform sampler2D noise1 : hint_black;
  5. uniform sampler2D noise2 : hint_black;
  6. uniform vec2 deformation_speed_1 = vec2(1.0, 1.0);
  7. uniform vec2 deformation_speed_2 = vec2(-2.0, -2.0);
  8. uniform vec2 deformation_strength = vec2(1.0, 1.0);
  9. uniform vec2 tile_factor = vec2(1.0,1.0);
  10. uniform float water_level = 40.0;
  11. uniform vec4 water_color: hint_color = vec4(0.0, 0.0, 1.0, 0.5);
  12. uniform float surface_width = 2.0;
  13. uniform vec4 surface_color: hint_color = vec4(1.0, 1.0, 1.0, 1.0);
  14. uniform float surface_deformation_strength = 1.0;
  15. uniform int surface_type = 0;
  16. uniform float wave_speed = 1.0;
  17. uniform float wave_distance_attenuation = 1.0;
  18. uniform float wave_time_attenuation = 1.0;
  19. uniform float impact_length_0 = 10.0;
  20. uniform float impact_length_1 = 10.0;
  21. uniform float impact_length_2 = 10.0;
  22. uniform float impact_length_3 = 10.0;
  23. uniform float impact_length_4 = 10.0;
  24. uniform float impact_length_5 = 10.0;
  25. uniform float impact_length_6 = 10.0;
  26. uniform float impact_length_7 = 10.0;
  27. uniform float impact_height_0 = 1.0;
  28. uniform float impact_height_1 = 1.0;
  29. uniform float impact_height_2 = 1.0;
  30. uniform float impact_height_3 = 1.0;
  31. uniform float impact_height_4 = 1.0;
  32. uniform float impact_height_5 = 1.0;
  33. uniform float impact_height_6 = 1.0;
  34. uniform float impact_height_7 = 1.0;
  35. uniform float impact_time_0 = -1;
  36. uniform float impact_time_1 = -1;
  37. uniform float impact_time_2 = -1;
  38. uniform float impact_time_3 = -1;
  39. uniform float impact_time_4 = -1;
  40. uniform float impact_time_5 = -1;
  41. uniform float impact_time_6 = -1;
  42. uniform float impact_time_7 = -1;
  43. uniform vec2 impact_pos_0 = vec2(-1, -1);
  44. uniform vec2 impact_pos_1 = vec2(-1, -1);
  45. uniform vec2 impact_pos_2 = vec2(-1, -1);
  46. uniform vec2 impact_pos_3 = vec2(-1, -1);
  47. uniform vec2 impact_pos_4 = vec2(-1, -1);
  48. uniform vec2 impact_pos_5 = vec2(-1, -1);
  49. uniform vec2 impact_pos_6 = vec2(-1, -1);
  50. uniform vec2 impact_pos_7 = vec2(-1, -1);
  51. uniform vec2 scale = vec2(1.0, 1.0);
  52. uniform float current_time;
  53. float add_wave_impact(vec2 pixel_pos, vec2 impact_pixel_pos, float time_since_impact, float impact_height, float impact_length) {
  54. if(impact_pixel_pos.y < 0.0) {
  55. return 0.0;
  56. }
  57. float dist = length(pixel_pos - impact_pixel_pos);
  58. float delay = dist / (wave_speed * 100.0);
  59. if(time_since_impact < delay) {
  60. return 0.0;
  61. }
  62. float amp = sin(wave_speed * 100.0 * (time_since_impact - delay) / impact_length);
  63. amp /= 1.0 + dist * wave_distance_attenuation * 0.01;
  64. amp *= exp(-time_since_impact * wave_time_attenuation);
  65. return - amp * impact_height;
  66. }
  67. void fragment() {
  68. vec2 u_pos_1 = UV * scale * tile_factor + TIME * deformation_speed_1 * 0.1;
  69. vec2 u_pos_2 = UV * scale * tile_factor + TIME * deformation_speed_2 * 0.1;
  70. vec2 offset = vec2(texture(noise1, u_pos_1).x, texture(noise2, u_pos_1).y ) - 0.5;
  71. vec2 pixel_pos = UV * scale / TEXTURE_PIXEL_SIZE;
  72. offset += add_wave_impact(pixel_pos, impact_pos_0, current_time - impact_time_0, impact_height_0, impact_length_0);
  73. offset += add_wave_impact(pixel_pos, impact_pos_1, current_time - impact_time_1, impact_height_1, impact_length_1);
  74. offset += add_wave_impact(pixel_pos, impact_pos_2, current_time - impact_time_2, impact_height_2, impact_length_2);
  75. offset += add_wave_impact(pixel_pos, impact_pos_3, current_time - impact_time_3, impact_height_3, impact_length_3);
  76. offset += add_wave_impact(pixel_pos, impact_pos_4, current_time - impact_time_4, impact_height_4, impact_length_4);
  77. offset += add_wave_impact(pixel_pos, impact_pos_5, current_time - impact_time_5, impact_height_5, impact_length_5);
  78. offset += add_wave_impact(pixel_pos, impact_pos_6, current_time - impact_time_6, impact_height_6, impact_length_6);
  79. offset += add_wave_impact(pixel_pos, impact_pos_7, current_time - impact_time_7, impact_height_7, impact_length_7);
  80. vec2 deformation = offset * deformation_strength * 0.01;
  81. vec2 real_pos = (UV * scale / TEXTURE_PIXEL_SIZE) + (deformation * 500.0 * surface_deformation_strength);
  82. if( real_pos.y < water_level) {
  83. return;
  84. } else if( real_pos.y < water_level + surface_width) {
  85. if(surface_type == 0) {
  86. COLOR = surface_color;
  87. } else {
  88. vec4 w_color = mix(textureLod(SCREEN_TEXTURE, SCREEN_UV + offset * deformation_strength * 0.015, 0.), vec4(water_color.rgb, 1.0), water_color.a);
  89. COLOR = mix(surface_color, w_color, (real_pos.y - water_level) / (surface_width));
  90. }
  91. } else {
  92. COLOR = mix(textureLod(SCREEN_TEXTURE, SCREEN_UV + offset * deformation_strength * 0.015, 0.), vec4(water_color.rgb, 1.0), water_color.a);
  93. }
  94. }"
  95. [sub_resource type="OpenSimplexNoise" id=2]
  96. [sub_resource type="NoiseTexture" id=3]
  97. seamless = true
  98. noise = SubResource( 2 )
  99. [sub_resource type="OpenSimplexNoise" id=4]
  100. seed = 1
  101. [sub_resource type="NoiseTexture" id=5]
  102. seamless = true
  103. noise = SubResource( 4 )
  104. [resource]
  105. shader = SubResource( 1 )
  106. shader_param/deformation_speed_1 = Vector2( 1, 1 )
  107. shader_param/deformation_speed_2 = Vector2( -2, -2 )
  108. shader_param/deformation_strength = Vector2( 1, 1 )
  109. shader_param/tile_factor = Vector2( 0.2, 0.2 )
  110. shader_param/water_level = 20.0
  111. shader_param/water_color = Color( 0.25098, 0.431373, 0.737255, 0.780392 )
  112. shader_param/surface_width = 1.5
  113. shader_param/surface_color = Color( 0, 0, 0, 0 )
  114. shader_param/surface_deformation_strength = 1.0
  115. shader_param/surface_type = 1
  116. shader_param/wave_speed = 2.0
  117. shader_param/wave_distance_attenuation = 1.0
  118. shader_param/wave_time_attenuation = 1.0
  119. shader_param/impact_length_0 = 10.0
  120. shader_param/impact_length_1 = 10.0
  121. shader_param/impact_length_2 = 10.0
  122. shader_param/impact_length_3 = 10.0
  123. shader_param/impact_length_4 = 10.0
  124. shader_param/impact_length_5 = 10.0
  125. shader_param/impact_length_6 = 10.0
  126. shader_param/impact_length_7 = 10.0
  127. shader_param/impact_height_0 = 1.0
  128. shader_param/impact_height_1 = 1.0
  129. shader_param/impact_height_2 = 1.0
  130. shader_param/impact_height_3 = 1.0
  131. shader_param/impact_height_4 = 1.0
  132. shader_param/impact_height_5 = 1.0
  133. shader_param/impact_height_6 = 1.0
  134. shader_param/impact_height_7 = 1.0
  135. shader_param/impact_time_0 = -1.0
  136. shader_param/impact_time_1 = -1.0
  137. shader_param/impact_time_2 = -1.0
  138. shader_param/impact_time_3 = -1.0
  139. shader_param/impact_time_4 = -1.0
  140. shader_param/impact_time_5 = -1.0
  141. shader_param/impact_time_6 = -1.0
  142. shader_param/impact_time_7 = -1.0
  143. shader_param/impact_pos_0 = Vector2( -1, -1 )
  144. shader_param/impact_pos_1 = Vector2( -1, -1 )
  145. shader_param/impact_pos_2 = Vector2( -1, -1 )
  146. shader_param/impact_pos_3 = Vector2( -1, -1 )
  147. shader_param/impact_pos_4 = Vector2( -1, -1 )
  148. shader_param/impact_pos_5 = Vector2( -1, -1 )
  149. shader_param/impact_pos_6 = Vector2( -1, -1 )
  150. shader_param/impact_pos_7 = Vector2( -1, -1 )
  151. shader_param/scale = Vector2( 3.96755, 2.50071 )
  152. shader_param/current_time = 17953.2
  153. shader_param/noise1 = SubResource( 3 )
  154. shader_param/noise2 = SubResource( 5 )