unity 图片遮罩有锯齿_如何消除UGUI Mask遮罩的锯齿

一般的遮罩做法是在Image的节点前,增加一父节点Mask,然后通过Mask包含Image,就是用父Mask的遮罩Image

可程序的事件总不会那么顺畅的,反正就是不行

结果是锯齿严重:

具体原理不知道

或者可以通过RectMask2D解决

但是我选择了使用Shader,

结果:

代码如下:(需要RawImage才能使用mat + shader)

Shader "Custom/ImageMask"

{

Properties

{

[PerRendererData] _MainTex ("Sprite Texture", 2D) = "white" {}

_Mask ("Base (RGB)", 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

[Toggle(UNITY_UI_ALPHACLIP)] _UseUIAlphaClip ("Use Alpha Clip", Float) = 0

}

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"

#include "UnityUI.cginc"

#pragma multi_compile __ UNITY_UI_ALPHACLIP

struct a2v

{

fixed2 uv : TEXCOORD0;

half4 vertex : POSITION;

float4 color : COLOR;

};

fixed4 _Color;

struct v2f

{

fixed2 uv : TEXCOORD0;

half4 vertex : SV_POSITION;

float4 color : COLOR;

};

sampler2D _MainTex;

sampler2D _Mask;

v2f vert (a2v i)

{

v2f o;

o.vertex = UnityObjectToClipPos(i.vertex);

o.uv = i.uv;

o.color = i.color * _Color;

return o;

}

fixed4 frag (v2f i) : COLOR

{

half4 color = tex2D(_MainTex, i.uv) * i.color;

half4 mask = tex2D(_Mask, i.uv);

color.a *= mask.a;

return color;

}

ENDCG

}

}

}

你可能感兴趣的:(unity,图片遮罩有锯齿)