Trying to write a 2d mask shader with stencil
I am new in shaders and trying to write a mask shader could works fine both in UI/SpriteRender Components.
.
.
[Expect ]
The right side is I'd like to, but it shows the left side as result. .
.
[Code]
Shader "Test/Stencil Mask" {
Properties {
[PerRendererData] _MainTex ("Texture", 2D) = "white" {}
_StencilRef ("Mask Reference", Range(0, 255)) = 1
}
SubShader {
Tags {
"Queue"="Transparent"
"RenderType"="TransparentCutout"
"IgnoreProjector"="True"
"PreviewType"="Plane"
}
Stencil {
Ref [_StencilRef]
Comp NotEqual
Pass Replace
}
Cull Off Lighting Off ZWrite Off
Fog { Mode Off }
Blend SrcAlpha OneMinusSrcAlpha
Pass {
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
sampler2D _MainTex;
float4 _MainTex_ST;
struct a2v {
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
float4 color : COLOR;
};
struct v2f {
float4 vertex : SV_POSITION;
float2 uv : TEXCOORD0;
fixed4 color : COLOR;
};
v2f vert (a2v v) {
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = TRANSFORM_TEX(v.uv, _MainTex);
o.color = v.color;
return o;
}
fixed4 frag (v2f f) : SV_Target {
fixed4 col = tex2D(_MainTex, f.uv) * f.color;
if (col.a == 0.0)
discard;
return col;
}
ENDCG
}
}
}
.
.
[Analysis]
I know this is because the second and the third images are not passed for the stencil test, but I have know idea how to modify the stecil settings.
.
I think maybe need a way can clear the pixel that have stored in stencill buffer, am I right?
snapshot.jpg
(231.2 kB)
Comment
Your answer
Follow this Question
Related Questions
What is the best approach to mask players vision? 1 Answer
Invisible transition mask 0 Answers
Making a shader ignore masks 0 Answers
Inverse Sprite Maskin 0 Answers
Hide objects inside 3d volume 0 Answers