[Solved] Stencil and Shadows (and Water)

Hello everyone.

I try to make a plane invisible (for AR) that can receive shadows but with holes where water (for a swimming pool should be deplayed).

I tried almost everything but it persist to not mix both.

My shader for the plane is :

Shader "Mask/Shadows"
{
    Properties
    {
        _ShadowIntensity("Shadow Intensity", Range(0, 1)) = 0.6
    }

    SubShader
    {
        Tags{ "Queue" = "AlphaTest" }
        Pass
        {
            Stencil{ Ref 1 Comp Equal Pass Replace }
            Tags{ "LightMode" = "ForwardBase" }
            Cull Back
            Blend SrcAlpha OneMinusSrcAlpha
            CGPROGRAM
                #pragma vertex vert
                #pragma fragment frag
                #pragma multi_compile_fwdbase
                #include "UnityCG.cginc"
                #include "AutoLight.cginc"
                uniform float _ShadowIntensity;

                struct v2f
                {
                    float4 pos : SV_POSITION;
                    LIGHTING_COORDS(0,1)
                };
                v2f vert(appdata_base v)
                {
                    v2f o;
                    o.pos = UnityObjectToClipPos(v.vertex);
                    TRANSFER_VERTEX_TO_FRAGMENT(o);

                    return o;
                }
                fixed4 frag(v2f i) : COLOR
                {
                    float attenuation = LIGHT_ATTENUATION(i);
                return fixed4(0,0,0,(1 - attenuation)*_ShadowIntensity);
                }
            ENDCG
        }
    }
    SubShader
    {
        Tags{ "RenderType" = "Opaque" }
        // Pass to render object as a shadow caster
        Pass
        {
            Name "ShadowCaster"
            Tags{ "LightMode" = "ShadowCaster" }
            Fog{ Mode Off }
            ZWrite On ZTest LEqual Cull Off
            Offset 1, 1
            CGPROGRAM
                #pragma vertex vert
                #pragma fragment frag
                #pragma multi_compile_shadowcaster
                #include "UnityCG.cginc"
                struct v2f
                {
                    V2F_SHADOW_CASTER;
                };
                v2f vert(appdata_base v)
                {
                    v2f o;
                    TRANSFER_SHADOW_CASTER(o)
                    return o;
                }
                float4 frag(v2f i) : SV_Target
                {
                    SHADOW_CASTER_FRAGMENT(i)
                }
            ENDCG
        }

        // Pass to render object as a shadow collector
        // note: editor needs this pass as it has a collector pass.
        Pass
        {
            Name "ShadowCollector"
            Tags{ "LightMode" = "ShadowCollector" }

            Fog{ Mode Off }
            ZWrite On ZTest LEqual
            CGPROGRAM
                #pragma vertex vert
                #pragma fragment frag
                #pragma multi_compile_shadowcollector
                #define SHADOW_COLLECTOR_PASS
                #include "UnityCG.cginc"
                struct appdata
                {
                    float4 vertex : POSITION;
                };
                struct v2f
                {
                    V2F_SHADOW_COLLECTOR;
                };
                v2f vert(appdata v)
                {
                    v2f o;
                    TRANSFER_SHADOW_COLLECTOR(o)
                        return o;
                }
                fixed4 frag(v2f i) : SV_Target
                {
                    SHADOW_COLLECTOR_FRAGMENT(i)
                }
            ENDCG
        }
    }
}

Image :

PS : I’m using Unity 2017

I didn’t quite get the question, but I think you want to create a shadow receiver that is not visible?! If so, take a look at this post: Transparent shadows - Changing queue from AlphaTest to Transparent

Yes but the invisible plan hide my water.
Here i have several planes.
One for the water (the Aqua Assets from the store). (y = -0.02)
One for the ground with stencil set at “1”. (y = -0.01 - RenderQueue = 1997)
Some small planes “water hole” what change the stencil from 1 to 0. (y=-0.01 - RenderQueue = 1998)
One last plan to hide everything behind when stencil = 1 (so out of water holes). (y = 0 -RenderQueue = 1999)

Stencil{ Ref 1 Comp Equal Pass Replace }

I don’t understand why my shadow plane with the same stencil don’t work and why only the water don’t display but the caustics and swimming pool does.

PS : I can’t use specific meshes because every objects are imported at runtime (including the skybox)

PSbis: I have the same problem with a textured plane…

Problem finally solved.

Just added a specific child camera to the main one to see shadows only.