88 lines
3.2 KiB
Plaintext
88 lines
3.2 KiB
Plaintext
shader_type spatial;
|
|
render_mode blend_mix, depth_draw_always;
|
|
|
|
//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;
|
|
|
|
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);
|
|
|
|
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 is_active_cell = step(1.0 - 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);
|
|
}
|
|
|
|
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);
|
|
|
|
vec3 final_rgb = mix(base_water.rgb, screen_ref, reflection_strength * reflection_mask);
|
|
final_rgb = mix(final_rgb, ripple_color.rgb, ring * ripple_color.a);
|
|
|
|
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;
|
|
} |