[gd_resource type="ShaderMaterial" load_steps=6 format=2]

[sub_resource type="Shader" id=1]
code = "shader_type canvas_item;

uniform sampler2D noise1 : hint_black;
uniform sampler2D noise2 : hint_black;

uniform vec2 deformation_speed_1 = vec2(1.0, 1.0);
uniform vec2 deformation_speed_2 = vec2(-2.0, -2.0);
uniform vec2 deformation_strength = vec2(1.0, 1.0);
uniform vec2 tile_factor = vec2(1.0,1.0);

uniform float water_level = 40.0;
uniform vec4 water_color: hint_color = vec4(0.0, 0.0, 1.0, 0.5);

uniform float surface_width = 2.0;
uniform vec4 surface_color: hint_color = vec4(1.0, 1.0, 1.0, 1.0);
uniform float surface_deformation_strength = 1.0;
uniform int surface_type = 0;

uniform float wave_speed = 1.0;
uniform float wave_distance_attenuation = 1.0;
uniform float wave_time_attenuation = 1.0;

uniform float impact_length_0 = 10.0;
uniform float impact_length_1 = 10.0;
uniform float impact_length_2 = 10.0;
uniform float impact_length_3 = 10.0;
uniform float impact_length_4 = 10.0;
uniform float impact_length_5 = 10.0;
uniform float impact_length_6 = 10.0;
uniform float impact_length_7 = 10.0;

uniform float impact_height_0 = 1.0;
uniform float impact_height_1 = 1.0;
uniform float impact_height_2 = 1.0;
uniform float impact_height_3 = 1.0;
uniform float impact_height_4 = 1.0;
uniform float impact_height_5 = 1.0;
uniform float impact_height_6 = 1.0;
uniform float impact_height_7 = 1.0;

uniform float impact_time_0 = -1;
uniform float impact_time_1 = -1;
uniform float impact_time_2 = -1;
uniform float impact_time_3 = -1;
uniform float impact_time_4 = -1;
uniform float impact_time_5 = -1;
uniform float impact_time_6 = -1;
uniform float impact_time_7 = -1;

uniform vec2 impact_pos_0 = vec2(-1, -1);
uniform vec2 impact_pos_1 = vec2(-1, -1);
uniform vec2 impact_pos_2 = vec2(-1, -1);
uniform vec2 impact_pos_3 = vec2(-1, -1);
uniform vec2 impact_pos_4 = vec2(-1, -1);
uniform vec2 impact_pos_5 = vec2(-1, -1);
uniform vec2 impact_pos_6 = vec2(-1, -1);
uniform vec2 impact_pos_7 = vec2(-1, -1);

uniform vec2 scale = vec2(1.0, 1.0);

uniform float current_time;

float add_wave_impact(vec2 pixel_pos, vec2 impact_pixel_pos, float time_since_impact, float impact_height, float impact_length) {
	if(impact_pixel_pos.y < 0.0) {
		return 0.0;
	}
	float dist = length(pixel_pos - impact_pixel_pos);
	float delay = dist / (wave_speed * 100.0);
	
	if(time_since_impact < delay) {
		return 0.0;
	}
	
	float amp = sin(wave_speed * 100.0 * (time_since_impact - delay) / impact_length);
	amp /= 1.0 + dist * wave_distance_attenuation * 0.01;
	amp *= exp(-time_since_impact * wave_time_attenuation);
	return - amp * impact_height;
}

void fragment() {
	vec2 u_pos_1 = UV * scale * tile_factor + TIME * deformation_speed_1 * 0.1;
	vec2 u_pos_2 = UV * scale * tile_factor + TIME * deformation_speed_2 * 0.1;
	vec2 offset = vec2(texture(noise1, u_pos_1).x, texture(noise2, u_pos_1).y ) - 0.5;
	vec2 pixel_pos = UV * scale / TEXTURE_PIXEL_SIZE;
	
	offset += add_wave_impact(pixel_pos, impact_pos_0, current_time - impact_time_0, impact_height_0, impact_length_0);
	offset += add_wave_impact(pixel_pos, impact_pos_1, current_time - impact_time_1, impact_height_1, impact_length_1);
	offset += add_wave_impact(pixel_pos, impact_pos_2, current_time - impact_time_2, impact_height_2, impact_length_2);
	offset += add_wave_impact(pixel_pos, impact_pos_3, current_time - impact_time_3, impact_height_3, impact_length_3);
	offset += add_wave_impact(pixel_pos, impact_pos_4, current_time - impact_time_4, impact_height_4, impact_length_4);
	offset += add_wave_impact(pixel_pos, impact_pos_5, current_time - impact_time_5, impact_height_5, impact_length_5);
	offset += add_wave_impact(pixel_pos, impact_pos_6, current_time - impact_time_6, impact_height_6, impact_length_6);
	offset += add_wave_impact(pixel_pos, impact_pos_7, current_time - impact_time_7, impact_height_7, impact_length_7);
	
	vec2 deformation = offset * deformation_strength * 0.01;
	vec2 real_pos = (UV * scale / TEXTURE_PIXEL_SIZE) + (deformation * 500.0 * surface_deformation_strength);
	if( real_pos.y < water_level) {
		return;
	} else if( real_pos.y < water_level + surface_width) {
		if(surface_type == 0) {
			COLOR = surface_color;
		} else {
			vec4 w_color = mix(textureLod(SCREEN_TEXTURE, SCREEN_UV + offset * deformation_strength * 0.015, 0.), vec4(water_color.rgb, 1.0), water_color.a);
			COLOR = mix(surface_color, w_color, (real_pos.y - water_level) / (surface_width));
		}
	} else {
		COLOR = mix(textureLod(SCREEN_TEXTURE, SCREEN_UV + offset * deformation_strength * 0.015, 0.), vec4(water_color.rgb, 1.0), water_color.a);
	}

}"

[sub_resource type="OpenSimplexNoise" id=2]

[sub_resource type="NoiseTexture" id=3]
seamless = true
noise = SubResource( 2 )

[sub_resource type="OpenSimplexNoise" id=4]
seed = 1

[sub_resource type="NoiseTexture" id=5]
seamless = true
noise = SubResource( 4 )

[resource]
shader = SubResource( 1 )
shader_param/deformation_speed_1 = Vector2( 1, 1 )
shader_param/deformation_speed_2 = Vector2( -2, -2 )
shader_param/deformation_strength = Vector2( 1, 1 )
shader_param/tile_factor = Vector2( 0.2, 0.2 )
shader_param/water_level = 20.0
shader_param/water_color = Color( 0.25098, 0.431373, 0.737255, 0.780392 )
shader_param/surface_width = 1.5
shader_param/surface_color = Color( 0, 0, 0, 0 )
shader_param/surface_deformation_strength = 1.0
shader_param/surface_type = 1
shader_param/wave_speed = 2.0
shader_param/wave_distance_attenuation = 1.0
shader_param/wave_time_attenuation = 1.0
shader_param/impact_length_0 = 10.0
shader_param/impact_length_1 = 10.0
shader_param/impact_length_2 = 10.0
shader_param/impact_length_3 = 10.0
shader_param/impact_length_4 = 10.0
shader_param/impact_length_5 = 10.0
shader_param/impact_length_6 = 10.0
shader_param/impact_length_7 = 10.0
shader_param/impact_height_0 = 1.0
shader_param/impact_height_1 = 1.0
shader_param/impact_height_2 = 1.0
shader_param/impact_height_3 = 1.0
shader_param/impact_height_4 = 1.0
shader_param/impact_height_5 = 1.0
shader_param/impact_height_6 = 1.0
shader_param/impact_height_7 = 1.0
shader_param/impact_time_0 = -1.0
shader_param/impact_time_1 = -1.0
shader_param/impact_time_2 = -1.0
shader_param/impact_time_3 = -1.0
shader_param/impact_time_4 = -1.0
shader_param/impact_time_5 = -1.0
shader_param/impact_time_6 = -1.0
shader_param/impact_time_7 = -1.0
shader_param/impact_pos_0 = Vector2( -1, -1 )
shader_param/impact_pos_1 = Vector2( -1, -1 )
shader_param/impact_pos_2 = Vector2( -1, -1 )
shader_param/impact_pos_3 = Vector2( -1, -1 )
shader_param/impact_pos_4 = Vector2( -1, -1 )
shader_param/impact_pos_5 = Vector2( -1, -1 )
shader_param/impact_pos_6 = Vector2( -1, -1 )
shader_param/impact_pos_7 = Vector2( -1, -1 )
shader_param/scale = Vector2( 3.96755, 2.50071 )
shader_param/current_time = 17953.2
shader_param/noise1 = SubResource( 3 )
shader_param/noise2 = SubResource( 5 )
