Added all NPC Idle animations, Overworld water changes,

This commit is contained in:
Pal
2025-09-17 04:12:21 -07:00
parent faa9bcd66b
commit 94c6c212ca
157 changed files with 2099 additions and 1447 deletions

View File

@@ -1,244 +1,385 @@
// The following shader is used in order to simulate a simple ocean using Gerstner waves.
// This shader can be added in a plane mesh. For a more detailed ocean, increase the width and depth subdivison.
// Note 1: On larger planes ex. 500x500, increasing the subdivision above 1000 comes at great performance cost
// Note 2: Special thanks to @ninetailsrabbit for fixing a caustics projection issue!
shader_type spatial;
render_mode world_vertex_coords, cull_disabled;
uniform sampler2D screen : hint_screen_texture, filter_linear_mipmap_anisotropic, repeat_disable;
group_uniforms colours;
uniform vec3 surfacecolour : source_color;
uniform vec3 volumecolour : source_color;
// Set render modes: always draw depth and disable backface culling
render_mode depth_draw_always, cull_disabled;
group_uniforms material;
uniform float AirIOR = 1.0;
uniform float IOR = 1.33;
// Uniforms for screen and depth textures
uniform sampler2D SCREEN_TEXTURE : hint_screen_texture, filter_linear_mipmap;
uniform sampler2D DEPTH_TEXTURE : hint_depth_texture, filter_linear_mipmap;
group_uniforms textures;
uniform vec2 sampler1speed = vec2(0.02, 0.0);
uniform vec2 sampler2speed = vec2(0.0, 0.02);
uniform float samplermix : hint_range(0.0, 1.0, 0.1) = 0.5;
uniform vec2 samplerscale = vec2(0.1);
uniform sampler2D normal1tex : filter_linear_mipmap_anisotropic, hint_normal;
uniform sampler2D normal2tex : filter_linear_mipmap_anisotropic, hint_normal;
uniform float normalstrength : hint_range(0.0, 5.0, 0.01) = 1.0;
uniform sampler2D height1tex : filter_linear_mipmap_anisotropic;
uniform sampler2D height2tex : filter_linear_mipmap_anisotropic;
uniform float heightstrength : hint_range(0.0, 5.0, 0.01) = 0.12;
uniform sampler2D edge1tex : filter_linear_mipmap_anisotropic;
uniform sampler2D edge2tex : filter_linear_mipmap_anisotropic;
// Group uniforms for wave parameters
group_uniforms Waves;
// Each wave is defined by a vec4: direction (x,y), amplitude, frequency
uniform vec4 wave_1 = vec4(0.3, 4.0, 0.2, 0.6);
uniform vec4 wave_2 = vec4(-0.26, -0.19, 0.01, 0.47);
uniform vec4 wave_3 = vec4(-7.67, 5.63, 0.1, 0.38);
uniform vec4 wave_4 = vec4(-0.42, -1.63, 0.1, 0.28);
uniform vec4 wave_5 = vec4(1.66, 0.07, 0.15, 1.81);
uniform vec4 wave_6 = vec4(1.20, 1.14, 0.01, 0.33);
uniform vec4 wave_7 = vec4(-1.6, 7.3, 0.11, 0.73);
uniform vec4 wave_8 = vec4(-0.42, -1.63, 0.15, 1.52);
varying vec2 position;
varying vec3 wposition;
// Uniforms for time factor, noise zoom, and noise amplitude
uniform float time_factor = 2.5;
uniform float noise_zoom = 2.0;
uniform float noise_amp = 1.0;
group_uniforms refraction;
// Group uniforms for water colors
group_uniforms Water_colours;
uniform vec3 base_water_color:source_color;
uniform vec3 fresnel_water_color:source_color;
uniform vec4 deep_water_color : source_color;
uniform vec4 shallow_water_color : source_color;
uniform float refrationamount : hint_range(0.0, 1.0, 0.01);
uniform bool fog_underwater;
// Group uniforms for depth-related parameters
group_uniforms Depth;
uniform float beers_law = 0.5;
uniform float depth_offset = -1.2;
uniform float near = 7.0;
uniform float far = 10000.0;
group_uniforms edge;
// Group uniforms for edge detection and foam effects
group_uniforms Edge_Detection;
uniform float edge_texture_scale = 3.5;
uniform float edge_texture_offset = 1.0;
uniform float edge_texture_speed = 0.1;
uniform float edge_foam_intensity = 2.0;
uniform float edge_fade_start = -3.0;
uniform float edge_fade_end = 6.6;
uniform sampler2D edge_foam_texture;
uniform float edge_size : hint_range(0.01, 0.5, 0.01) = 0.1;
uniform bool foam_or_fade = false;
// Group uniforms for wave peak effects
group_uniforms WavePeakEffect;
uniform float peak_height_threshold = 1.0;
uniform vec3 peak_color = vec3(1.0, 1.0, 1.0);
uniform float peak_intensity = 1.0;
uniform sampler2D foam_texture;
uniform float foam_intensity = 1.0;
uniform float foam_scale = 1.0;
uniform sampler2D DEPTH_TEXTURE : hint_depth_texture, filter_linear_mipmap, repeat_disable;
// Group uniforms for surface details
group_uniforms Surface_details;
uniform float metallic = 0.6;
uniform float roughness = 0.045;
uniform float uv_scale_text_a = 0.1;
uniform vec2 uv_speed_text_a = vec2(0.42, 0.3);
uniform float uv_scale_text_b = 0.6;
uniform vec2 uv_speed_text_b = vec2(0.15, 0.1);
uniform float normal_strength = 1.0;
uniform float uv_sampler_scale = 0.3;
uniform float blend_factor = 0.28;
uniform float perturbation_strength = 1.0; // Adjust this value to tile/de-tile the oceans surface.
uniform float perturbation_time = 0.3; // Offset perturbation_time for patern variation
uniform sampler2D normalmap_a;
uniform sampler2D normalmap_b;
uniform sampler2D uv_sampler;
uniform sampler2DArray caustic_sampler : hint_default_black;
group_uniforms screen_space_reflection;
uniform float num_caustic_layers = 16.0; // <<< IMPORTANT: DOUBLE CHECK THIS against your Texture2DArray's actual slices!
uniform float caustic_distortion_strength = 0.001; // Keep this value within a range of 0.001 to 0.009
uniform float far_clip = 50.0;
uniform int steps : hint_range(64, 1024, 16) = 512;
uniform float ssr_screen_fade : hint_range(0.01, 0.5, 0.01) = 0.05;
float schlickfresnel(float ior1, float ior2, vec3 view, vec3 norm) {
float incident = dot(view, norm);
float reflectance = pow(((ior2 - ior1)/(ior2 + ior1)), 2.0);
float fresnelincident = reflectance + (1.0 - reflectance) * pow(1.0 - cos(incident), 5.0);
return(fresnelincident / incident);
// Fresnel function to calculate the reflection/refraction effect
float fresnel(float amount, vec3 normal, vec3 view) {
return pow((1.0 - clamp(dot(normalize(normal), normalize(view)), 0.0, 1.0)), amount);
}
// Function to calculate edge depth
float edge(float depth) {
depth = 2.0 * depth - 1.0;
return near * far / (far - depth * (near - far));
}
// Function to calculate dynamic amplitude based on position and time
float dynamic_amplitude(vec2 pos, float time) {
return 1.0 + 0.5 * sin(time + length(pos) * 0.1);
}
// Hash function for noise generation
float hash(vec2 p) {
return fract(sin(dot(p * 17.17, vec2(14.91, 67.31))) * 4791.9511);
}
// 2D noise function
float noise(vec2 x) {
vec2 p = floor(x);
vec2 f = fract(x);
f = f * f * (3.0 - 2.0 * f);
vec2 a = vec2(1.0, 0.0);
return mix(mix(hash(p + a.yy), hash(p + a.xy), f.x),
mix(hash(p + a.yx), hash(p + a.xx), f.x), f.y);
}
// Fractional Brownian Motion (fBM) function for generating complex noise
float fbm(vec2 x) {
float height = 0.0;
float amplitude = 0.5;
float frequency = 3.0;
for (int i = 0; i < 6; i++) {
height += noise(x * frequency) * amplitude;
amplitude *= 0.5;
frequency *= 2.0;
}
return height;
}
// Structure to hold wave results: displacement, tangent, binormal, and normal
struct WaveResult {
vec3 displacement;
vec3 tangent;
vec3 binormal;
vec3 normal;
};
// Gerstner wave function to calculate wave displacement and normals
WaveResult gerstner_wave(vec4 params, vec2 pos, float time) {
float steepness = params.z * dynamic_amplitude(pos, time);
float wavelength = params.w;
float k = 2.0 * PI / wavelength;
float c = sqrt(9.81 / k);
vec2 d = normalize(params.xy);
float f = k * (dot(d, pos.xy) - c * time);
float a = steepness / k;
vec3 displacement = vec3(d.x * (a * cos(f)), a * sin(f), d.y * (a * cos(f)));
vec3 tangent = vec3(1.0 - d.x * d.x * steepness * sin(f), steepness * cos(f), -d.x * d.y * steepness * sin(f));
vec3 binormal = vec3(-d.x * d.y * steepness * sin(f), steepness * cos(f), 1.0 - d.y * d.y * steepness * sin(f));
vec3 normal = normalize(cross(tangent, binormal));
return WaveResult(displacement, tangent, binormal, normal);
}
// Function to combine multiple Gerstner waves
WaveResult wave(vec2 pos, float time) {
WaveResult waveResult;
waveResult.displacement = vec3(0.0);
waveResult.tangent = vec3(1.0, 0.0, 0.0);
waveResult.binormal = vec3(0.0, 0.0, 1.0);
waveResult.normal = vec3(0.0, 1.0, 0.0);
WaveResult wr;
wr = gerstner_wave(wave_1, pos, time);
waveResult.displacement += wr.displacement;
waveResult.tangent += wr.tangent;
waveResult.binormal += wr.binormal;
waveResult.normal += wr.normal;
wr = gerstner_wave(wave_2, pos, time);
waveResult.displacement += wr.displacement;
waveResult.tangent += wr.tangent;
waveResult.binormal += wr.binormal;
waveResult.normal += wr.normal;
wr = gerstner_wave(wave_3, pos, time);
waveResult.displacement += wr.displacement;
waveResult.tangent += wr.tangent;
waveResult.binormal += wr.binormal;
waveResult.normal += wr.normal;
wr = gerstner_wave(wave_4, pos, time);
waveResult.displacement += wr.displacement;
waveResult.tangent += wr.tangent;
waveResult.binormal += wr.binormal;
waveResult.normal += wr.normal;
wr = gerstner_wave(wave_5, pos, time);
waveResult.displacement += wr.displacement;
waveResult.tangent += wr.tangent;
waveResult.binormal += wr.binormal;
waveResult.normal += wr.normal;
wr = gerstner_wave(wave_6, pos, time);
waveResult.displacement += wr.displacement;
waveResult.tangent += wr.tangent;
waveResult.binormal += wr.binormal;
waveResult.normal += wr.normal;
wr = gerstner_wave(wave_7, pos, time);
waveResult.displacement += wr.displacement;
waveResult.tangent += wr.tangent;
waveResult.binormal += wr.binormal;
waveResult.normal += wr.normal;
wr = gerstner_wave(wave_8, pos, time);
waveResult.displacement += wr.displacement;
waveResult.tangent += wr.tangent;
waveResult.binormal += wr.binormal;
waveResult.normal += wr.normal;
// Add noise to the wave displacement for more natural look
waveResult.displacement.y += fbm(pos.xy * (noise_zoom / 50.0)) * noise_amp;
return waveResult;
}
// Varying variables to pass data from vertex to fragment shader
varying float height;
varying vec3 world_position;
varying mat3 tbn_matrix;
varying mat4 inv_mvp;
// Vertex shader function
void vertex() {
position = VERTEX.xz;
UV = VERTEX.xz * samplerscale + (sampler1speed * TIME);
UV2 = VERTEX.xz * samplerscale + (sampler2speed * TIME);
float height = mix(texture(height1tex, UV),texture(height2tex, UV2),samplermix).x;
VERTEX.y += (height - 0.5) * heightstrength;
wposition = VERTEX;
// Called for every vertex the material is visible on.
// Calculate time based on the global TIME variable and time_factor
float time = TIME / time_factor;
// Calculate wave displacement and normals
WaveResult waveResult = wave(VERTEX.xz, time);
// Apply wave displacement to the vertex position
VERTEX += waveResult.displacement;
// Store the height of the wave displacement
height = waveResult.displacement.y;
// Transform normals, tangents, and binormals to world space
vec3 n = normalize((MODELVIEW_MATRIX * vec4(waveResult.normal, 0.0)).xyz);
vec3 t = normalize((MODELVIEW_MATRIX * vec4(waveResult.tangent.xyz, 0.0)).xyz);
vec3 b = normalize((MODELVIEW_MATRIX * vec4((cross(waveResult.normal, waveResult.tangent.xyz)), 0.0)).xyz);
// Calculate world position of the vertex
world_position = (MODEL_MATRIX * vec4(VERTEX, 1.0)).xyz;
// Create TBN matrix for normal mapping
tbn_matrix = mat3(t, b, n);
// Calculate inverse MVP matrix for screen space transformations
inv_mvp = inverse(PROJECTION_MATRIX * MODELVIEW_MATRIX);
}
float snells_window(vec3 normal, vec3 view, float ior) {
float cos_theta = dot(normal, view);
return step(sqrt(1.0 - cos_theta * cos_theta) * ior, 1.0);
// 2D Random hash function
float random(vec2 p) {
return fract(sin(dot(p, vec2(127.1, 311.7))) * 43758.5453123);
}
float linear_depth(float nonlinear_depth, mat4 inv_projection_matrix) {
return 1.0 / (nonlinear_depth * inv_projection_matrix[2].w + inv_projection_matrix[3].w);
// Smooth noise function
float smooth_noise(vec2 p) {
vec2 i = floor(p);
vec2 f = fract(p);
vec2 u = f * f * (3.0 - 2.0 * f);
return mix(
mix(random(i + vec2(0.0, 0.0)), random(i + vec2(1.0, 0.0)), u.x),
mix(random(i + vec2(0.0, 1.0)), random(i + vec2(1.0, 1.0)), u.x),
u.y
);
}
float nonlinear_depth(float linear_depth, mat4 inv_projection_matrix) {
return (1.0 / linear_depth - inv_projection_matrix[3].w) / inv_projection_matrix[2].w;
// Layered noise for detiling
float layered_noise(vec2 p, float scale) {
float n = 0.0;
n += smooth_noise(p * scale) * 0.5;
n += smooth_noise(p * scale * 2.0) * 0.25;
n += smooth_noise(p * scale * 4.0) * 0.125;
return n;
}
vec2 view2uv(vec3 position_view_space, mat4 proj_m)
{
vec4 position_clip_space = proj_m * vec4(position_view_space.xyz, 1.0);
vec2 position_ndc = position_clip_space.xy / position_clip_space.w;
return position_ndc.xy * 0.5 + 0.5;
// Perturb UV coordinates for detiling
vec2 perturb_uv(vec2 uv, vec2 world_pos, float time, float strength) {
// Use world position to generate unique noise patterns
vec2 noise_offset = vec2(
layered_noise(world_pos * 0.3 + time * 0.06, 1.0),
layered_noise(world_pos * 0.3 + time * 0.06 + vec2(10.0), 1.0)
);
// Apply subtle distortion to UVs
return uv + noise_offset * strength;
}
float remap(float x, float min1, float max1, float min2, float max2) {
return ((x - min1) / (max1 - min1) + min2) * (max2 - min2);
}
float remap1(float x, float min1, float max1) {
return (x - min1) / (max1 - min1);
}
float edge_fade(vec2 uv, float size) {
float x1 = clamp(remap1(uv.x, 0.0, size), 0.0, 1.0);
float x2 = clamp(remap1(uv.x, 1.0, 1.0 - size), 0.0, 1.0);
float y1 = clamp(remap1(uv.y, 0.0, size), 0.0, 1.0);
float y2 = clamp(remap1(uv.y, 1.0, 1.0 - size), 0.0, 1.0);
return x1*x2*y1*y2;
}
// Fragment shader function
void fragment() {
// Calculate UV coordinates based on world position
vec2 uv = world_position.xz;
vec3 onorm = NORMAL;
// Sample UV offset texture
vec2 uv_offset = texture(uv_sampler, uv * uv_sampler_scale).rg;
vec2 normmap = mix(texture(normal1tex, UV),texture(normal2tex, UV2),samplermix).xy;
NORMAL += TANGENT * (normmap.x - 0.5) * normalstrength;
NORMAL += BINORMAL * (normmap.y - 0.5) * normalstrength;
// Calculate base UV coordinates for normal maps
vec2 base_uv_a = (uv + uv_speed_text_a * TIME + uv_offset) * uv_scale_text_a;
vec2 base_uv_b = (uv + uv_speed_text_b * TIME + uv_offset) * uv_scale_text_b;
vec3 wnorm = (vec4(NORMAL, 0.0) * VIEW_MATRIX).xyz;
vec3 wview = (vec4(VIEW, 0.0) * VIEW_MATRIX).xyz;
if (FRONT_FACING) {
// Apply noise-based perturbation to UVs
vec2 animated_uv_a = perturb_uv(base_uv_a, world_position.xz, TIME, perturbation_strength);
vec2 animated_uv_b = perturb_uv(base_uv_b, world_position.xz, TIME + 0.0, perturbation_time);
ROUGHNESS = 0.0;
METALLIC = 1.0;
SPECULAR = 0.0;
// Sample normal maps
vec3 normal_sample_a = texture(normalmap_a, animated_uv_a).rgb;
vec3 normal_sample_b = texture(normalmap_b, animated_uv_b).rgb;
float fres = schlickfresnel(AirIOR, IOR, VIEW, NORMAL);
ALBEDO = surfacecolour * fres;
// REFRACTION
float lineardepth = linear_depth(texture(DEPTH_TEXTURE, SCREEN_UV).r, INV_PROJECTION_MATRIX);
float selfdepth = -VERTEX.z;
float depth_diff = lineardepth - selfdepth;
vec3 tanx = BINORMAL * (normmap.x - 0.5) * normalstrength;
vec3 tany = TANGENT * (normmap.y - 0.5) * normalstrength;
vec2 refracted_uv = SCREEN_UV + (tanx + tany).xy * refrationamount * depth_diff / lineardepth;
float newdepth = linear_depth(texture(DEPTH_TEXTURE, refracted_uv).r, INV_PROJECTION_MATRIX);
//float selfdepth = 1.0/(1.0 + 2.0 * distance(wposition, CAMERA_POSITION_WORLD));
vec3 newvolcolour = mix(volumecolour, vec3(1.0), clamp(1.0 / (depth_diff * 1.0), 0.0, 1.0));
EMISSION = newvolcolour * texture(screen, refracted_uv).rgb;
// Normalize normal samples and combine them
normal_sample_a = normalize(normal_sample_a * 2.0 - 1.0);
normal_sample_b = normalize(normal_sample_b * 2.0 - 1.0);
vec3 combined_normal = normalize(mix(normal_sample_a, normal_sample_b, blend_factor));
if (newdepth < selfdepth) {
EMISSION = newvolcolour * texture(screen, SCREEN_UV).rgb;
}
// SSR
vec3 reflected = -reflect(VIEW, NORMAL);
vec3 pos = VERTEX;
int curstep = 0;
bool finished = false;
vec2 uv;
float currentdepth;
while (curstep < steps) {
float step_scale = float(curstep + 1) / float(steps);
float step_dist = step_scale * step_scale * far_clip;
pos += reflected * step_dist;
curstep += 1;
currentdepth = -pos.z;
uv = view2uv(pos, PROJECTION_MATRIX);
if (!(uv.x < 1.0 && uv.y < 1.0 && uv.x > 0.0 && uv.y > 0.0)) {
break;
}
float testdepth = linear_depth(texture(DEPTH_TEXTURE, uv).r, INV_PROJECTION_MATRIX);
if (testdepth < currentdepth) {
finished = true;
break;
}
}
if (finished && currentdepth < far_clip * 0.99) {
ALBEDO *= 1.0 - edge_fade(uv, ssr_screen_fade);
METALLIC *= 1.0 - edge_fade(uv, ssr_screen_fade);
EMISSION += texture(screen, uv).xyz * schlickfresnel(1.0, 1.33, VIEW, NORMAL) * edge_fade(uv, ssr_screen_fade);
}
// EDGE EFFECT
float distfromedge = depth_diff * dot(normalize(NORMAL), normalize(-VERTEX)) / VIEW.z;
if (distfromedge < edge_size) {
distfromedge /= edge_size;
if (foam_or_fade) {
ALPHA = distfromedge;
} else {
float edgetex = mix(texture(edge1tex, UV).r, texture(edge2tex, UV2).r, samplermix);
if (edgetex > distfromedge) {
ALBEDO = vec3(1.0);
ROUGHNESS = 1.0;
METALLIC = 1.0;
EMISSION = vec3(0.0);
NORMAL = onorm;
}
}
}
// Perturb the normal using the TBN matrix
vec3 perturbed_normal = normalize(tbn_matrix * (combined_normal * normal_strength));
} else {
// SNELLS WINDOW
float window = snells_window(wnorm, wview, IOR);
// Sample depth texture
float depth_raw = texture(DEPTH_TEXTURE, SCREEN_UV).r;
float depth = PROJECTION_MATRIX[3][2] / (depth_raw + PROJECTION_MATRIX[2][2]);
// Calculate the distance from the camera to the water surface
float camera_depth = INV_VIEW_MATRIX[3].y - world_position.y;
if (camera_depth < 0.0) { // Camera is underwater
// Map the depth to a range where deeper = positive beers_law, closer = negative beers_law
float depth_factor = smoothstep(-10.0, 0.0, camera_depth); // Adjust -10.0 for the depth range
ALPHA -= depth_factor * 0.3;
}
// Calculate depth blend factor using Beer's law
float depth_blend = exp((depth + VERTEX.z + depth_offset) * -beers_law);
depth_blend = clamp(1.0 - depth_blend, 0.0, 1.0);
float depth_blend_power = clamp(pow(depth_blend, 2.5), 0.0, 1.0);
// Sample screen color and blend it with depth color
vec3 screen_color = textureLod(SCREEN_TEXTURE, SCREEN_UV, depth_blend_power * 2.5).rgb;
vec3 depth_color = mix(shallow_water_color.rgb, deep_water_color.rgb, depth_blend_power);
vec3 color = mix(screen_color * depth_color, depth_color * 0.25, depth_blend_power * 0.5);
// Calculate depth difference for edge detection
float z_depth = edge(texture(DEPTH_TEXTURE, SCREEN_UV).x);
float z_pos = edge(FRAGCOORD.z);
float z_dif = z_depth - z_pos;
// Calculate caustic effect
vec4 caustic_screenPos = vec4(SCREEN_UV * 2.0 - 1.0, depth_raw, 1.0);
vec4 caustic_localPos = inv_mvp * caustic_screenPos;
caustic_localPos = vec4(caustic_localPos.xyz / caustic_localPos.w, caustic_localPos.w);
vec2 caustic_Uv = caustic_localPos.xz / vec2(1024.0) + 0.5;
caustic_Uv += perturbed_normal.xz * caustic_distortion_strength;
if (window > 0.5) {
ROUGHNESS = 1.0;
METALLIC = 1.0;
ALBEDO = vec3(0.0);
SPECULAR = 0.0;
float linear_depth = 1.0 / (texture(DEPTH_TEXTURE, SCREEN_UV).r * INV_PROJECTION_MATRIX[2].w + INV_PROJECTION_MATRIX[3].w);
float selfdepth = 1.0 / (FRAGCOORD.z * INV_PROJECTION_MATRIX[2].w + INV_PROJECTION_MATRIX[3].w);
float depth_diff = linear_depth - selfdepth;
vec3 tanx = BINORMAL * (normmap.x - 0.5) * normalstrength;
vec3 tany = TANGENT * (normmap.y - 0.5) * normalstrength;
float newdepth = 1.0 / (texture(DEPTH_TEXTURE, SCREEN_UV + (tanx + tany).xy * refrationamount).r * INV_PROJECTION_MATRIX[2].w + INV_PROJECTION_MATRIX[3].w);
//float selfdepth = 1.0/(1.0 + 2.0 * distance(wposition, CAMERA_POSITION_WORLD));
vec3 newvolcolour = mix(volumecolour, vec3(1.0), clamp(1.0 / (selfdepth * 1.0), 0.0, 1.0));
if (!fog_underwater) {
newvolcolour = vec3(1.0);
}
EMISSION = newvolcolour * texture(screen, SCREEN_UV + (tanx + tany).xy * refrationamount).rgb;
} else {
ALBEDO = surfacecolour;
ROUGHNESS = 0.0;
METALLIC = 1.0;
// SSR
vec3 reflected = -reflect(VIEW, NORMAL);
vec3 pos = VERTEX;
int curstep = 0;
bool finished = false;
vec2 uv;
float currentdepth;
while (curstep < steps) {
float step_scale = float(curstep + 1) / float(steps);
float step_dist = step_scale * step_scale * far_clip;
pos += reflected * step_dist;
curstep += 1;
currentdepth = -pos.z;
uv = view2uv(pos, PROJECTION_MATRIX);
if (!(uv.x < 1.0 && uv.y < 1.0 && uv.x > 0.0 && uv.y > 0.0)) {
break;
}
float testdepth = linear_depth(texture(DEPTH_TEXTURE, uv).r, INV_PROJECTION_MATRIX);
if (testdepth < currentdepth) {
finished = true;
break;
}
}
if (finished && currentdepth < far_clip * 0.99) {
ALBEDO *= 1.0 - edge_fade(uv, ssr_screen_fade);
METALLIC *= 1.0 - edge_fade(uv, ssr_screen_fade);
EMISSION += texture(screen, uv).xyz * schlickfresnel(1.0, 1.33, VIEW, NORMAL) * edge_fade(uv, ssr_screen_fade);
}
}
float caustic_layer_index = floor(mod(TIME * 26.0, num_caustic_layers)); // Use floor for integer index
}
}
vec4 caustic_color = texture(caustic_sampler, vec3(caustic_Uv * 660.0, caustic_layer_index));
float caustic_intensity_multiplier = (1.0 - depth_blend_power) * 6.0;
color *= 1.0 + pow(caustic_color.r, 1.50) * caustic_intensity_multiplier;
// Calculate fresnel effect
float fresnel = fresnel(5.0, NORMAL, VIEW);
vec3 surface_color = mix(base_water_color, fresnel_water_color, fresnel);
// Calculate edge foam effect
vec2 edge_uv = world_position.xz * edge_texture_scale + edge_texture_offset + TIME * edge_texture_speed;
float edge_fade = smoothstep(edge_fade_start, edge_fade_end, z_dif);
vec3 depth_color_adj = mix(texture(edge_foam_texture, edge_uv).rgb * edge_foam_intensity, color, edge_fade);
// Apply peak color effect based on height with noise
float peak_factor = smoothstep(peak_height_threshold, peak_height_threshold + 0.2, height);
float noise_factor = fbm(world_position.xz * 0.1 + TIME * 0.1);
peak_factor = peak_factor * noise_factor;
vec3 final_color = mix(surface_color, peak_color * peak_intensity, peak_factor);
// Sample the foam texture and blend it with the final color
vec2 foam_uv = world_position.xz * foam_scale + TIME * 0.1;
float foam_sample = texture(foam_texture, foam_uv).r;
float foam_blend_factor = smoothstep(0.0, 1.0, peak_factor) * foam_sample * foam_intensity;
final_color = mix(final_color, vec3(1.0), foam_blend_factor);
// Set the final color, metallic, roughness, and normal
ALBEDO = clamp(final_color + depth_color_adj, vec3(0.0), vec3(1.0));
METALLIC = metallic;
ROUGHNESS = roughness;
NORMAL = perturbed_normal;
}