Categories
Development Game Creation

Shader intersection radiate outwards

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

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

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

            sampler2D cameraDepthTexture;

            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;
            }

            sampler2D _CameraDepthTexture;

            fixed4 frag (v2f i) : SV_Target
            {
                float currentDepth = tex2Dproj(_CameraDepthTexture, UNITY_PROJ_COORD(i.screenPos)).r;
                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.

Leave a Reply

Your email address will not be published. Required fields are marked *