Question by
neutralmouse01 · Dec 05, 2019 at 06:33 PM ·
2dshaderspritespriterenderermasking
Stencil Buffer not working with SpriteMask
Hi all,
I'm pretty new to Unity, so please be forgiving as I have no idea what I am doing here. I have a circular sprite inside of a larger rectangle sprite, which has a SpriteMask set up to keep the circle inside the rectangle. I also have the circle (which is semi-transparent) using a custom shader I found on Reddit a couple weeks ago (I can't find it right now, if I find it I will link it here) that stops it from blending alphas with other sprites. Here it is:
Shader "Custom/ShadowShader" {
Properties {
[PerRendererData] _MainTex ( "Sprite Texture", 2D ) = "white" {}
_Color ( "Tint", Color ) = ( 1, 1, 1, 1 )
[MaterialToggle] PixelSnap ("Pixel snap", Float) = 0
}
SubShader {
Tags { "Queue"="Transparent"
"IgnoreProjector" = "True"
"RenderType" = "TransparentCutout"
"PreviewType" = "Plane"
"CanUseSpriteAtlas" = "True" }
Cull Off
Lighting Off
ZWrite Off
Blend SrcAlpha OneMinusSrcAlpha
Pass {
Stencil {
Ref 4
Comp NotEqual
Pass Replace
}
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma multi_compile _ PIXELSNAP_ON
#include "UnityCG.cginc"
uniform sampler2D _MainTex;
struct v2f {
half4 pos : POSITION;
half2 uv : TEXCOORD0;
fixed4 color : COLOR;
};
fixed4 _Color;
v2f vert(appdata_img v) {
v2f o;
o.pos = UnityObjectToClipPos (v.vertex);
half2 uv = MultiplyUV( UNITY_MATRIX_TEXTURE0, v.texcoord );
o.uv = uv;
o.color = _Color;
return o;
}
half4 frag (v2f i) : COLOR {
half4 color = tex2D(_MainTex, i.uv);
if (color.a == 0.0)
discard;
color = _Color;
return color;
}
ENDCG
}
}
Fallback off
}
Here's where the problem arises, probably due to the stencil buffer. When I turn the SpriteMask on the rectangle off, this happens (the 2 circles are 2 different sprites): However, when I use the SpriteMask, the shader stops working: How can I fix this? Any help is appreciated!
screen-shot-2019-12-05-at-121744-pm.png
(14.5 kB)
screen-shot-2019-12-05-at-121721-pm.png
(14.9 kB)
Comment