UI mask doesn't work with custom sprite material
Hi. I use two shader for stencil mask. the mask are:
Shader "Sprites/Stencil Mask"
{
Properties
{
[PerRendererData] _MainTex("Sprite Texture", 2D) = "white" {}
_Color("Tint", Color) = (1,1,1,1)
[MaterialToggle] PixelSnap("Pixel snap", Float) = 0
// required for UI.Mask
_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
}
SubShader
{
Tags
{
"Queue" = "Transparent"
"IgnoreProjector" = "True"
"RenderType" = "Transparent"
"PreviewType" = "Plane"
"CanUseSpriteAtlas" = "True"
}
Cull Off
Lighting Off
ZWrite Off
Fog{ Mode Off }
Blend One OneMinusSrcAlpha
Pass
{
Stencil
{
Ref 1
Comp always
Pass replace
}
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma multi_compile DUMMY PIXELSNAP_ON
#include "UnityCG.cginc"
struct appdata_t
{
float4 vertex : POSITION;
float4 color : COLOR;
float2 texcoord : TEXCOORD0;
};
struct v2f
{
float4 vertex : SV_POSITION;
fixed4 color : COLOR;
half2 texcoord : TEXCOORD0;
};
fixed4 _Color;
v2f vert(appdata_t IN)
{
v2f OUT;
OUT.vertex = mul(UNITY_MATRIX_MVP, IN.vertex);
OUT.texcoord = IN.texcoord;
OUT.color = IN.color * _Color;
#ifdef PIXELSNAP_ON
OUT.vertex = UnityPixelSnap(OUT.vertex);
#endif
return OUT;
}
sampler2D _MainTex;
fixed4 frag(v2f IN) : SV_Target
{
fixed4 c = tex2D(_MainTex, IN.texcoord) * IN.color;
if (c.a<0.001) discard; //Most IMPORTANT working Code
c.rgb *= c.a;
return c;
}
ENDCG
}
}
}
and the draw in mask are:
Shader "Sprites/Stencil Draw In Mask"
{
Properties
{
[PerRendererData] _MainTex("Sprite Texture", 2D) = "white" {}
_Color("Tint", Color) = (1,1,1,1)
[MaterialToggle] PixelSnap("Pixel snap", Float) = 0
// required for UI.Mask
_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
}
SubShader
{
Tags
{
"Queue" = "Transparent+1"
"IgnoreProjector" = "True"
"RenderType" = "Transparent"
"PreviewType" = "Plane"
"CanUseSpriteAtlas" = "True"
}
Cull Off
Lighting Off
ZWrite Off
Fog{ Mode Off }
Blend One OneMinusSrcAlpha
Pass
{
Stencil
{
Ref 1
Comp Equal
}
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma multi_compile DUMMY PIXELSNAP_ON
#include "UnityCG.cginc"
struct appdata_t
{
float4 vertex : POSITION;
float4 color : COLOR;
float2 texcoord : TEXCOORD0;
};
struct v2f
{
float4 vertex : SV_POSITION;
fixed4 color : COLOR;
half2 texcoord : TEXCOORD0;
};
fixed4 _Color;
v2f vert(appdata_t IN)
{
v2f OUT;
OUT.vertex = mul(UNITY_MATRIX_MVP, IN.vertex);
OUT.texcoord = IN.texcoord;
OUT.color = IN.color * _Color;
#ifdef PIXELSNAP_ON
OUT.vertex = UnityPixelSnap(OUT.vertex);
#endif
return OUT;
}
sampler2D _MainTex;
fixed4 frag(v2f IN) : SV_Target
{
fixed4 c = tex2D(_MainTex, IN.texcoord) * IN.color;
c.rgb *= c.a;
return c;
}
ENDCG
}
}
}
it works great. but the problem is that this material doesn't work with UI Mask component.
I think this is a related work: UI mask with shader but i can't fix my shader. any help?
Answer by zac_tangible · Nov 09, 2016 at 06:08 PM
Unity identifies a clip rectangle to pass to graphics, you need to properly handle that information. Note, this will not work with a SpriteRenderer, you need to be using a Graphics component of some sort (ie Image)
//scope variables
#include "UnityUI.cginc"
float4 _ClipRect;
struct v2f
{
float4 vertex : SV_POSITION;
fixed4 color : COLOR;
half2 texcoord : TEXCOORD0;
float4 worldPosition : TEXCOORD1; //we need to pass world pos to the fragment shader for clipping
};
//vertex shader
o.worldPosition = i.vertex;
o.pos = UnityObjectToClipPos(o.worldPosition);
//fragment shader
output_color.a *= UnityGet2DClipping(i.worldPosition.xy, _ClipRect);
Works perfect, however under Unity 2018, I had to add also
inline float UnityGet2DClipping(in float2 position, in float4 clipRect) { float2 inside = step(clipRect.xy, position.xy) step(position.xy, clipRect.zw); return inside.x inside.y; }
otherwise Unity was not able to find that function. Thanks!
Answer by crocodilepp · Sep 12, 2017 at 06:19 AM
Ran into exactly the same issue for my project and finally found this thread Masked UI Element's Shader Not Updating. Using image.materialForRendering.SetFloat to change shader property at runtime perfectly solved the problem.
Answer by johnstablencsoft · May 12, 2017 at 12:15 PM
If you want to modify the material AFTER mask is applied, you need to get the Mask component, and call GetModifiedMaterial().
Mask myMask = _image.GetComponent<Mask>();
Material modifiedMat = myMask.GetModifiedMaterial( _image.material );
modifiedMat.SetFloat( "_SomeProp", 3.0f );