[gd_scene load_steps=15 format=2]

[ext_resource path="res://ui/ui.tscn" type="PackedScene" id=1]
[ext_resource path="res://world/materials/outline.tres" type="Shader" id=2]
[ext_resource path="res://world/default_env.tres" type="Environment" id=3]
[ext_resource path="res://world/World3d.gd" type="Script" id=4]
[ext_resource path="res://utils/camera/CamBase.tscn" type="PackedScene" id=5]

[sub_resource type="PlaneMesh" id=1]
size = Vector2( 2000, 2000 )

[sub_resource type="Shader" id=2]
code = "shader_type spatial;
render_mode specular_phong, cull_disabled;

uniform float speed: hint_range(-1, 1) = 0.0;

uniform sampler2D noise1;
uniform sampler2D noise2;
uniform sampler2D normalmap: hint_normal;

uniform vec4 color : hint_color;
uniform vec4 deep_water: hint_color;

//depth-fade var
uniform float beer_law_factor = 2.0;
uniform float _distance = 0.0;

//foam var
uniform vec4 edge_color: hint_color;
uniform float edge_scale = 0.25;
uniform float near = 0.1;
uniform float far = 100.0f;

// wave var
uniform vec2 wave_strength = vec2(0.5, 0.25);
uniform vec2 wave_frequ = vec2(12.0, 12.0);
uniform vec2 time_factor = vec2(1.0, 2.0);


float waves(vec2 pos, float time) {
	return (wave_strength.y * sin(pos.y * wave_frequ.y + time * time_factor.y)) + (wave_strength.x * sin(pos.x * wave_frequ.x + time * time_factor.x));
}

void vertex() {
	VERTEX.y += waves(VERTEX.xy, TIME);
}

float rim(float depth) {
	depth = 2.0 * depth - 1.0;
	return near * far / (far + depth * (near - far));
}

float calc_depth_fade(float depth, mat4 projection_matrix, 
						vec4 fragcoord, float beer_factor, float __distance, vec3 vertex) {
	
	float scene_depth = depth;

	scene_depth = scene_depth * 2.0 - 1.0;
	scene_depth = projection_matrix[3][2] / (scene_depth + projection_matrix[2][2]);
	scene_depth = scene_depth + vertex.z; // z is negative
	
	// application of beers law
	scene_depth = exp(-scene_depth * beer_factor);
	
	float screen_depth = fragcoord.z;
	
	float depth_fade = (scene_depth - screen_depth) / __distance;
	
	depth_fade = clamp(depth_fade, 0.0, 1.0);
	
	return depth_fade;
}

void fragment() {
	float time = TIME * speed;
	
	vec3 n1 = texture(noise1, UV * 1.0 + time).rgb;
	vec3 n2 = texture(noise2, UV * 1.0 - time * 0.2).rgb;
	
	vec2 uv_movement = UV * 4.0;
	uv_movement += TIME * speed * 4.0;
	
	float sum = (n1.r + n2.r) - 1.0;
	
	float z_depth = rim(texture(DEPTH_TEXTURE, SCREEN_UV).x);
	float z_pos = rim(FRAGCOORD.z);
	float diff = z_depth - z_pos;
	
	// depth-fade
	float z_depth_fade = calc_depth_fade(texture(DEPTH_TEXTURE, SCREEN_UV).x, PROJECTION_MATRIX, FRAGCOORD, beer_law_factor, _distance, VERTEX);
	float z_fade = rim(FRAGCOORD.z);
	float fade_diff = z_depth_fade - z_fade;
	
	vec4 gradientcolor = mix(color, deep_water, z_depth_fade);
	
	vec2 displacement = vec2(sum * 0.1);
	diff += displacement.x * 70.0;
	
	vec4 col = mix(edge_color, gradientcolor, step(edge_scale, diff));
	
	vec4 alpha = texture(SCREEN_TEXTURE, SCREEN_UV + displacement);
	alpha = vec4(9.0);
	
	float fin = 0.0;
	if (sum > 0.0 && sum < 0.4) fin = 0.1;
	if (sum > 0.4 && sum < 0.8) fin = 0.0;
	if (sum > 0.8) fin = 1.0;
	
	// konvertier fin in vec3 um
	ALBEDO = vec3(fin) + mix(alpha.rgb, col.rgb, gradientcolor.a);
	
	NORMALMAP = texture(normalmap, uv_movement).rgb;
	
	ROUGHNESS = 0.1;
}"

[sub_resource type="OpenSimplexNoise" id=4]

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

[sub_resource type="OpenSimplexNoise" id=6]

[sub_resource type="NoiseTexture" id=7]
width = 2048
height = 2048
seamless = true
noise = SubResource( 6 )

[sub_resource type="ShaderMaterial" id=3]
render_priority = 1
shader = SubResource( 2 )
shader_param/speed = 0.003
shader_param/color = Color( 0.054902, 0.533333, 0.741176, 1 )
shader_param/deep_water = Color( 0.0518, 0.31561, 0.74, 1 )
shader_param/beer_law_factor = 2.0
shader_param/_distance = 0.0
shader_param/edge_color = Color( 1, 1, 1, 1 )
shader_param/edge_scale = 0.25
shader_param/near = 0.1
shader_param/far = 100.0
shader_param/wave_strength = Vector2( 0.5, 0.25 )
shader_param/wave_frequ = Vector2( 12, 12 )
shader_param/time_factor = Vector2( 1, 2 )
shader_param/noise1 = SubResource( 5 )
shader_param/noise2 = SubResource( 7 )

[sub_resource type="QuadMesh" id=8]
size = Vector2( 2, 2 )

[sub_resource type="ShaderMaterial" id=9]
shader = ExtResource( 2 )
shader_param/outline_mode = 3
shader_param/outline_intensity = 0.1
shader_param/_round = true
shader_param/outline_bias = 0.0
shader_param/outline_color = Color( 0, 0, 0, 1 )

[node name="Game" type="Node"]

[node name="UI" parent="." instance=ExtResource( 1 )]

[node name="Map" parent="UI" index="0"]
scale = Vector2( 0.25, 0.25 )

[node name="Cursor" parent="UI/Map" index="0"]
scale = Vector2( 4, 4 )
z_index = 3

[node name="World3d" type="Spatial" parent="."]
script = ExtResource( 4 )

[node name="Water" type="MeshInstance" parent="World3d"]
transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 1000, 0, 1000 )
mesh = SubResource( 1 )
material/0 = SubResource( 3 )

[node name="WorldEnvironment" type="WorldEnvironment" parent="World3d"]
environment = ExtResource( 3 )

[node name="CamBase" parent="World3d" instance=ExtResource( 5 )]

[node name="Camera" parent="World3d/CamBase" index="0"]
physics_interpolation_mode = 1
transform = Transform( 1, 0, 0, 0, 0.659983, -0.75128, 0, 0.75128, 0.659983, 0, -5.72205e-06, 6.618 )
fov = 55.0
zoom_sensibility = 1.436
height = 5.0

[node name="DirectionalLight" type="DirectionalLight" parent="World3d"]
transform = Transform( 0.971628, 0.168947, -0.16552, 0, 0.699825, 0.714314, 0.236516, -0.694047, 0.67997, 0, 1.41623, 14.8745 )
light_energy = 0.2
shadow_enabled = true

[node name="Outline" type="MeshInstance" parent="World3d"]
transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 6.29265, 7.41201, 29.3021 )
extra_cull_margin = 3653.19
mesh = SubResource( 8 )
material/0 = SubResource( 9 )

[connection signal="map_clicked" from="UI/Map" to="World3d/CamBase/Camera" method="_on_Map_map_clicked"]
[connection signal="camera_moved" from="World3d/CamBase/Camera" to="UI/Map/Cursor" method="_on_Camera_camera_moved"]

[editable path="UI"]
[editable path="UI/Map"]
[editable path="World3d/CamBase"]
