shader_type spatial;
render_mode blend_mix, depth_draw_always; 

global uniform float global_rain_intensity;
global uniform vec4 global_water_color = vec4(0.285, 0.534, 0.487, 1.0);
global uniform float global_snow_start_time = -1.0;
global uniform float global_snow_accumulation_speed = 0.005;
global uniform float global_snow_melt_time = -1.0;
global uniform float global_snow_melt_speed = 0.1;
global uniform float global_snow_amount = 0.0;
global uniform vec4 global_snow_color = vec4(0.92, 0.96, 1.0, 1.0);

//Water color
uniform vec4 deep_water_color : source_color = vec4(0.0, 0.1, 0.2, 1.0);
uniform vec4 shallow_water_color : source_color = vec4(0.0, 0.4, 0.7, 1.0);
uniform float beer_law_factor : hint_range(0.0, 10.0) = 5.0; 

group_uniforms Fake_Reflection_Proximity;
uniform sampler2D screen_texture : hint_screen_texture, filter_linear_mipmap;
uniform float reflection_strength : hint_range(0.0, 1.0) = 0.6; 
uniform float reflection_offset : hint_range(-0.5, 0.5) = 0.05; // Offset base
uniform float reflection_stretch : hint_range(-1.0, 1.0) = 0.1; 
uniform float max_reflection_distance : hint_range(0.1, 10.0) = 1.5;

//Ripples parameters
group_uniforms Ripple_Settings;
uniform vec4 ripple_color : source_color = vec4(1.0, 1.0, 1.0, 0.7);
uniform float ripple_density : hint_range(0.0, 1.0) = 0.05; 
uniform float ripple_scale : hint_range(0.5, 20.0) = 3.0; 
uniform float ripple_speed : hint_range(0.1, 5.0) = 0.8; 
uniform float ripple_thickness : hint_range(0.001, 0.1) = 0.02;

uniform sampler2D depth_texture : hint_depth_texture, filter_linear_mipmap;

varying vec2 world_pos_xz;
varying vec2 local_uv;

float get_snow_progress() {
	float snow_progress = clamp(global_snow_amount, 0.0, 1.0);

	if (global_snow_start_time >= 0.0) {
		float timed_progress = clamp((TIME - global_snow_start_time) * global_snow_accumulation_speed, 0.0, 1.0);
		snow_progress = max(snow_progress, timed_progress);
	}
	if (global_snow_melt_time >= 0.0) {
		float melt = clamp((TIME - global_snow_melt_time) * global_snow_melt_speed, 0.0, 1.0);
		snow_progress = min(snow_progress, 1.0 - melt);
	}

	return snow_progress;
}

void vertex() {
	world_pos_xz = (MODEL_MATRIX * vec4(VERTEX, 1.0)).xz;
	local_uv = UV;
}

void fragment() {
	float depth = texture(depth_texture, SCREEN_UV).r;
	vec3 ndc = vec3(SCREEN_UV * 2.0 - 1.0, depth);
	vec4 view = INV_PROJECTION_MATRIX * vec4(ndc, 1.0);
	view.xyz /= view.w;
	float linear_depth = -view.z;
	float depth_difference = linear_depth + VERTEX.z;

	float water_depth_gradient = exp(-depth_difference * beer_law_factor);
	vec4 base_water = mix(deep_water_color, shallow_water_color, water_depth_gradient);
	float rain_intensity = clamp(global_rain_intensity, 0.0, 1.0);
	base_water.rgb = mix(base_water.rgb, global_water_color.rgb, 0.7);

	vec2 pos = world_pos_xz * ripple_scale;
	vec2 cell = floor(pos);
	vec2 local_pos = fract(pos) - 0.5;
	float random_val = fract(sin(dot(cell, vec2(12.9898, 78.233))) * 43758.5453);
	float active_ripple_density = ripple_density * rain_intensity;
	float is_active_cell = step(1.0 - active_ripple_density, random_val);
	
	float ring = 0.0;
	if (is_active_cell > 0.0) {
		float local_time = fract(TIME * ripple_speed + random_val * 10.0);
		float dist = length(local_pos);
		ring = smoothstep(local_time - ripple_thickness, local_time, dist) 
		     - smoothstep(local_time, local_time + ripple_thickness, dist);
		ring *= (1.0 - local_time); 
	}
	ring *= rain_intensity;

	vec2 ref_uv = SCREEN_UV;
	
	ref_uv.y -= reflection_offset + (local_uv.y * reflection_stretch);
	ref_uv += ring * 0.01; 
	
	vec3 screen_ref = textureLod(screen_texture, ref_uv, 0.0).rgb;
	
	float ref_depth_raw = texture(depth_texture, ref_uv).r;
	vec3 ref_ndc = vec3(ref_uv * 2.0 - 1.0, ref_depth_raw);
	vec4 ref_view = INV_PROJECTION_MATRIX * vec4(ref_ndc, 1.0);
	ref_view.xyz /= ref_view.w;
	float ref_linear_depth = -ref_view.z;
	
	float distance_from_water = abs(ref_linear_depth - (-VERTEX.z));
	float reflection_mask = 1.0 - smoothstep(0.0, max_reflection_distance, distance_from_water);
	float is_sky = step(ref_depth_raw, 0.00001); // Protezione anti-cielo
	reflection_mask *= (1.0 - is_sky);

	float snow_progress = get_snow_progress();
	float active_snowfall = step(0.0, global_snow_start_time) * (1.0 - step(0.0, global_snow_melt_time));
	float reflection_snow_damping = max(smoothstep(0.0, 0.08, snow_progress), active_snowfall * 0.75);
	float effective_reflection_strength = reflection_strength * (1.0 - reflection_snow_damping * 0.85);

	vec3 final_rgb = mix(base_water.rgb, screen_ref, effective_reflection_strength * reflection_mask);
	final_rgb = mix(final_rgb, ripple_color.rgb, ring * ripple_color.a);
	float water_snow_amount = smoothstep(0.15, 1.0, snow_progress) * 0.18;
	final_rgb = mix(final_rgb, global_snow_color.rgb, water_snow_amount);

	ALBEDO = final_rgb;
	
	ALPHA = mix(0.98, 0.4, water_depth_gradient);
	ALPHA = clamp(ALPHA + (ring * ripple_color.a), 0.0, 1.0);

	ROUGHNESS = 1.0; 
	SPECULAR = 0.0;  
}
