Forcing DepthMask Shader to clear alpha buffer

Working on a project that shares the final image of the unity renderer via Spout as a transparent texture (with a seperate depth texture) into a different enviroment, where it gets used as an augmented reality layer on top of an image feed.

My current problem is, that i’m using a simple depth mask shader:

Shader "Custom/DepthOnly" {
    SubShader {
        Tags {
            "Queue" = "Geometry-10"
        }
        Fog { Mode Off }

        Pass {
            Tags { "LightMode" = "Deferred" } // otherwise does not work in deferred path
            Lighting Off
            Cull Back
            ZWrite On
            ColorMask 0
        }
    }
}

that uses the depth buffer to cut hole for a real object (on the image feed). It nearly works but since that shader is at queue position 1990, unity defaults to write alpha 1 into the alpha buffer which results into a rendertexture that shows the clear color/skybox of the camera.

Does anyone here know a way to force that opaque shader to write or even overwrite the alpha buffer with zeros? Or maybe a different way by using a commandbuffer?

EDIT: also adding a fragment shader that writes 0 into the alpha channel does not seem to work. Seems like Unity defaults to fully opaque depending on the render queue position…

Try writing 0 in the alpha after the background has been drawn. So somewhere in the transparent queue.

The alpha simply gets overwritten by the background, which is drawn between the opaque and transparent queues.

The other way would be to use your own skybox shader that skips writing to the alpha channel.

Thank you for your answer! I couldn’t get the skybox approach to work… while it didn’t clear the background in the editor window at all, it still wrote those pixel into the final alpha channel.

At least I somehow fumbled this shader together, that more or less does what I need. Sadly only using forward rendering…

Shader "Occluder" {

  SubShader {
    Tags {"Queue" = "Geometry" }

    Blend SrcAlpha OneMinusSrcAlpha
    ZWrite On

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

      fixed4 vert(float4 pos : POSITION) : SV_POSITION {
        return UnityObjectToClipPos(pos);
      }

      fixed4 frag() : COLOR {
        return 0;
      }

      ENDCG
    }
  }

}