- Home /
UI with no Ztest appearing behind geometry when masked.
So I have a world space canvas for a HUD which has a load of UI with a no ztest shader. All of the UI works and appears in front of all geometry unless I mask it.
I did have to follow the following to get the mask to work (clearMask gameobject in the screenshots below), as im in deferred rendering: http://forum.unity3d.com/threads/ugui-masking-blocked-by-background-meshes.270122/
Here's my hierarchy:
Here in the bottom left corner you can see the masked element being drawn behind geometry:
Here's the UI shader im using (applied to the HUDUI material): Shader "UI/Default_OverlayNoZTest" { Properties { _Color ("Main Color", Color) = (1,1,1,1) _MainTex ("Alpha (A)", 2D) = "white" {}
_StencilComp ("Stencil Comparison", Float) = 8
_Stencil ("Stencil ID", Float) = 0
_StencilOp ("Stencil Operation", Float) = 0
_StencilWriteMask ("Stencil Write Mask", Float) = 255
_StencilReadMask ("Stencil Read Mask", Float) = 255
_ColorMask ("Color Mask", Float) = 15
_Illum ("Illumin (A)", 2D) = "white" {}
}
SubShader
{
LOD 200
Tags
{
"Queue" = "Transparent"
"IgnoreProjector" = "True"
"RenderType" = "Transparent"
"PreviewType"="Plane"
"LightMode" = "Vertex"
}
Stencil
{
Ref [_Stencil]
Comp [_StencilComp]
Pass [_StencilOp]
ReadMask [_StencilReadMask]
WriteMask [_StencilWriteMask]
}
Cull Off
Lighting On
ZWrite Off
ZTest Off
Offset -1, -1
Blend SrcAlpha OneMinusSrcAlpha
ColorMask [_ColorMask]
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct appdata_t
{
float4 vertex : POSITION;
half4 color : COLOR;
float2 texcoord : TEXCOORD0;
};
struct v2f
{
float4 vertex : POSITION;
half4 color : COLOR;
float2 texcoord : TEXCOORD0;
float2 uvIllum : TEXCOORD1;
};
sampler2D _MainTex;
float4 _MainTex_ST;
fixed4 _Color;
float4 _Illum_ST;
v2f vert (appdata_t v)
{
v2f o;
o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
o.texcoord = TRANSFORM_TEX(v.texcoord, _MainTex);
o.color = v.color;
o.uvIllum = TRANSFORM_TEX(v.texcoord, _Illum);
#ifdef UNITY_HALF_TEXEL_OFFSET
o.vertex.xy += (_ScreenParams.zw-1.0)*float2(-1,1);
#endif
return o;
}
half4 frag (v2f i) : COLOR
{
half4 col = i.color;
col.a *= tex2D(_MainTex, i.texcoord).a;
col = col * _Color;
clip (col.a - 0.01);
return col;
}
ENDCG
}
}
}
Answer by romlel · Sep 25, 2017 at 08:44 PM
Hello,
Just in case someone else encountered this, use the custom shader on the masking graphics too. I guess that if the mask tests the zbuffer first, it won't never draw the "stencil hole" to display the text inside, so both need to use the customized shader
cheeks
Your answer
![](https://koobas.hobune.stream/wayback/20220611233625im_/https://answers.unity.com/themes/thub/images/avi.jpg)