Invisible depth shader does not work when migrating to LWRP

Hi, I am moving a shader from the standard pipeline to LWRP and it is failing to work.

What the shader is supposed to do is render an invisible object in a render queue before all objects are rendered, but also write its depth to the buffer. This will make every object behind it invisible but will allow objects to render in front of it (from z testing).

Example of shader applied to cube (standard pipeline):

When I enable LWRP on the project this shader no longer works and I get this instead:

I don’t know what is happening here but if somebody with experience with ShaderLab and LWRP can explain why this is happening or how LWRP shader migrations are supposed to be handled, I would highly appreciate it.

For reference, here is the shader code I am using:

Shader "BlackStack/PortalShaderMod"
{
    SubShader
    {
        Tags{ "RenderType" = "Transparent" "Queue" = "Geometry-10" }
        ZTest Less
        ZWrite On
        ColorMask 0

        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag

            #include "UnityCG.cginc"

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

            struct v2f
            {
                float4 vertex : SV_POSITION;
            };

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

            fixed4 frag(v2f i, out float depth : SV_Depth) : SV_Target
            {
                return 0;
            }
        ENDCG
        }
    }
    FallBack "Hidden/InternalErrorShader"
}

I’m not really sure how your shader would have worked before hand in the Game view. Since Unity 5.0 that should have only worked in the Scene view.

The scene view rendering works like this:
Skybox > Opaque Queues > Transparent Queues
The game view works like this:
Opaque Queues > Skybox > Transparent Queues

The skybox only renders where the depth buffer has not yet been written to, so anything that writes to the depth during the opaque queue range will block the skybox. The only way I can think your above example worked is if you were looking at it from the scene view, or all your objects’ materials were in the transparent queue… which is not the case since they’re receiving shadows. AFAIK in the LWRP the scene and game view now actually match behaviors, which also matches the built in rendering paths’ game view.

1 Like

You are correct here, as I look deeper into this project that I worked on a while ago, I am noticing that the reason I am getting the images above is simply because of the fact that in LWRP, the scene and game view match behaviors. The “invisible” shader is something only seen in the scene view.

Here is a screenshot of the scene, camera preview, and the gameview:
5131523--507320--upload_2019-11-1_21-32-37.jpg

What is was happening there was that I was rendering with another camera in another location which draws in that space where the color mask is 0. So I think the issue I have here has to do with the two cameras not rendering together as expected, not the shader and the fact that I can’t see the skybox anymore.

Thank you so much for mentioning this, it really helped a lot. I would not have noticed this.

Yep, LWRP doesn’t support multi-camera rendering. None of the SRPs do currently. If this is something you need, you’ll need to modifying the LWRP code to add support for it yourself, or stick to the built in rendering paths.

1 Like

Yeah I am seeing that now. I’ll look into modifying LWRP since I got nothing but time for this hobby project. Thanks again.