0
\$\begingroup\$

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 : Objects immersed in water shader

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.

enter image description here

\$\endgroup\$
  • \$\begingroup\$ What specifically do you mean by "the intensity of the intersection"? What is your desired look? \$\endgroup\$ – DMGregory Mar 7 at 14:47
  • \$\begingroup\$ 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. \$\endgroup\$ – Jamie Hyland Mar 7 at 14:53
  • \$\begingroup\$ 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. \$\endgroup\$ – DMGregory Mar 7 at 14:59
  • \$\begingroup\$ So, its because im using very flat geometry that im not getting my desired look? \$\endgroup\$ – Jamie Hyland Mar 7 at 15:16
  • \$\begingroup\$ 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. \$\endgroup\$ – DMGregory Mar 7 at 15:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.