I am struggling to find a way to make a transparent surface occlude opaque meshes behind it for an AR app. Often times, this is used to make "floating door" animations, holes in the ground, etc where a surrounding transparent mesh occludes anything that is not viewable through a hole in the mesh, and is a commonly used trick in Unity. It becomes an issue when trying to do it in URP. Above Occlusion Cube Below Occlusion Cube Below Occlusion Cube with pill highlighted
The closest thing I've found is this shader. It seems to be capturing the depth information correctly (?), but it is unable to render the cube transparent. It has eliminated the "smear" issue that others seem to be having with other transparent occlusion shaders in URP. I feel like maybe I'm missing something with the render layers, but I am not experienced enough in them to understand. Ideally, the top side view would just show the top half of the capsule, and the bottom side view would show nothing
Shader "Custom/InvisibleOcclusionShaderURP"
{
Properties
{
[HDR] _BaseColor("Base Color", Color) = (1, 1, 1, 1)
_MainTex("Texture", 2D) = "white" {}
}
SubShader
{
Tags {"Queue" = "Transparent" "RenderType" = "Transparent"}
Pass
{
Stencil
{
Ref 1
Comp always
Pass replace
}
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma multi_compile_fog
#include "UnityCG.cginc"
struct appdata_t
{
float4 vertex : POSITION;
float2 texcoord : TEXCOORD0;
};
struct v2f
{
float2 texcoord : TEXCOORD0;
float4 vertex : SV_POSITION;
};
sampler2D _MainTex;
float4 _MainTex_ST;
v2f vert(appdata_t v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.texcoord = TRANSFORM_TEX(v.texcoord, _MainTex);
return o;
}
half4 frag(v2f i) : SV_Target
{
// Make the object invisible, but still write to the depth buffer.
return half4(0, 0, 0, 0);
}
ENDCG
}
}
}