Edge detection for a water shader is getting applyied indescriminetly across the whole mesh

Godot Version

4.3

Question

following along with a tutorial for a water shader (https://youtu.be/7L6ZUYj1hs8?si=lYCyQDsFNadkbZO3) and I’m at the part where he’s using edge detection to add like a beach foam effect on the edges. The problem is that instead of turning the edges white it’s turning the whole thing white even if its not intersecting anything.

Here’s all the relevent parts of my code

uniform float edgeScale = 0.1;
uniform float near = 1.0;
uniform float far = 100.0;
uniform vec3 edgeColor : source_color;

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

void fragment ()
{
...
// geeting edge depth
	float zDepth = calc_edge(texture(DEPTH_TEXTURE, SCREEN_UV).x);
	float zPos = calc_edge(FRAGCOORD.z);
	float zDif = zDepth - zPos;
...
	vec3 depth_color_adj = mix(edgeColor, color, step(edgeScale, zDif));
	ALBEDO = clamp(surfColor + depth_color_adj,vec3(0.0),vec3(1.0));
}

1 Like

FWIW, he uploaded it here for free: Godot Water Shader V1 by StayAtHomeDev You might download it and see if it works, and then use it to see where yours is different.

The point is apparently that the shaders were redesigned in 4.3.

That was a very cool read @tomcat thanks!

@APileOfSaltRocks from reading that, it looks like your line:

float zDif = zDepth - zPos;

needs to be changed to

float zDif = zPos - zDepth;
1 Like

Any luck with this? I’ve been messing with something similar myself but I can’t seem to wrap my head around it.