- Home /
Aliasing When Using UI Mask
I've just used the Mask from the new UI for the first time, using a rounded rect as the mask image. The image being masked has really bad aliasing on its corners, where the mask image is rounded with smooth antialiasing.
Is this supposed to happen? Is there a way to get smooth curves with a UI mask?
I'm having the same problem. Did you find any solution?
Problem is that the mask is a bitmap so either 1 or 0 and your rounded corner image probably uses some various level of transparency.
Also, if the image is not used with the exact dimensions it gets reshaped and some extra little pixels seem to get added wrong.
I had a similar issue with circle and could only $$anonymous$$imized the problem using bigger resolution images.
Answer by alexandre-fiset · May 22, 2015 at 07:06 PM
I made a little shader that allows you to use a mask to hide part of a sprite smoothly.
Create a new shader and past the code below.
Create a new material and choose Ui > Mask as the shader.
Assign the new material to your sprite.
Assign your mask to the Mask Texture slot in your material.
Voilà!
It is a bit harder on the system than the built-in solution, but it works.
Shader "UI/Mask"
{
Properties
{
[PerRendererData] _MainTex ("Sprite Texture", 2D) = "white" {}
_MaskTex ("Mask Texture", 2D) = "white" {}
_Color ("Tint", Color) = (1,1,1,1)
_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"
}
Stencil
{
Ref [_Stencil]
Comp [_StencilComp]
Pass [_StencilOp]
ReadMask [_StencilReadMask]
WriteMask [_StencilWriteMask]
}
Cull Off
Lighting Off
ZWrite Off
ZTest [unity_GUIZTestMode]
Blend SrcAlpha OneMinusSrcAlpha
ColorMask [_ColorMask]
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#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;
#ifdef UNITY_HALF_TEXEL_OFFSET
OUT.vertex.xy += (_ScreenParams.zw-1.0)*float2(-1,1);
#endif
OUT.color = IN.color * _Color;
return OUT;
}
sampler2D _MainTex;
sampler2D _MaskTex;
fixed4 frag(v2f IN) : SV_Target
{
half4 color = tex2D(_MainTex, IN.texcoord) * IN.color;
half4 mask = tex2D(_MaskTex, IN.texcoord);
color.a *= mask.r;
clip (color.a - 0.01);
return color;
}
ENDCG
}
}
}
I tried to implement this shader, but it did not seem to work in Unity 5.3. Do you know if this still works? @alexandre-fiset
Yeah, it works in Unity 5.4.2. Note that you should set your mask texture type to "Texture" in inspector or else it won't work.
doesnt work unless im setting it up wrong
It works nice, you can increase the smoothness/clip more from the edge by changing the value you subtract in line 90 "clip (color.a - 0.01);".
Answer by Fbear5 · Apr 04, 2016 at 01:52 PM
If you uncheck "Generate Mip Maps" on the sprite of mask it also helps a little :)
Answer by kurtdog · Jan 21, 2018 at 02:15 AM
Hey @alexandre-fiset
Thanks for the awesome shader!! It doesn't seem to support alpha transparency though. See the attached picture as viewed with the UI/Mask shader compared to how it should look. Would you be willing to adjust the shader to support transparency?
Appreciate it, Kurt