I've been working on a water shader for a project I'm currently working on and have been trying to work on the intersections between objects in the water to create a foam effect that radiates outwards. I'm very new to shaders so my code is probably horribly inefficient! But here it is:

Shader "Unlit/WaterShader"
{
Properties
{
_Color("Color", Color) = (1,1,1,1)
_IntersectColor("Intersection Color", Color) = (0,0,0,0)
_MinIntensity("Minimum intensity", Float) = 0.04
_MaxIntensity("Max Intensity", Float) = 0.4

}
{
Tags { "Queue"="Transparent" "RenderType"="Transparent" }
LOD 100

Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"

struct appdata
{
float4 vertex : POSITION;
float4 uv : TEXCOORD0;
};

struct v2f
{
float4 screenPos : TEXCOORD2;
float4 vertex  : SV_POSITION;

};

float4 _Color;
float4 _IntersectColor;
float _MinIntensity;
float _MaxIntensity;

v2f vert (appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.screenPos = ComputeScreenPos(o.vertex);
return o;
}

fixed4 frag (v2f i) : SV_Target
{
float linearDepth = LinearEyeDepth(currentDepth);
float depthDifference = linearDepth - i.screenPos.w;
if(depthDifference < _MinIntensity)
_Color = _IntersectColor;
return  _Color;
}
ENDCG
}
}
}


It works well enough :

But now Id like to maybe try increase the intensity of the intersection. I know increasing _MinIntensity doesn't really do what I want it to do, as it only shows more and more of the image submerged in water, which removes the effect of shoreline foam. I'd also like the outline of the objects in the water to spread out and fade out over time, I've tried using a sin wave for this but haven't been able to achieve the desired look.

• What specifically do you mean by "the intensity of the intersection"? What is your desired look? – DMGregory Mar 7 at 14:47
• Ideally id like the white to be thicker without showing whats under the water shader. I'll add another photo of the effect im looking for. – Jamie Hyland Mar 7 at 14:53
• Note that part of the difference in the example you've added is the shape of the submerged object. See how it flares outward toward the bottom? That's what's creating the lateral spread of the intersection highlight: the geometry just under the water continues to travel outward, rather than straight down as in your floating cube example. – DMGregory Mar 7 at 14:59
• So, its because im using very flat geometry that im not getting my desired look? – Jamie Hyland Mar 7 at 15:16
• Something worth experimenting with. Try using it with a mesh (or combination of meshes) that flares out as it sinks below the water line, and compare that with your target image. – DMGregory Mar 7 at 15:17