今天看了雨松momo的,http://www.xuanyusong.com/archives/4408。在ugui下实现新手引导的遮罩效果。将点击位置,及ui的宽高最大值传递给shader显示遮罩。
顺手在ngui里实现了。shader如下:
Shader "Unlit/Transparent Colored Cycle"
{
Properties
{
_MainTex ("Base (RGB), Alpha (A)", 2D) = "black" {}
_Grey("Luminance Factor",Range(0,1))=0
_Center("Center", vector) = (0, 0, 0, 0)
_Silder ("_Silder", Range (0,1000)) = 1000 // sliders
}
SubShader
{
LOD 100
Tags
{
"Queue" = "Transparent"
"IgnoreProjector" = "True"
"RenderType" = "Transparent"
"RenderTarget" = "oriented"
}
Cull Off
Lighting Off
ZWrite Off
Fog { Mode Off }
Offset -1, -1
Blend SrcAlpha OneMinusSrcAlpha
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct appdata_t
{
float4 vertex : POSITION;
float2 texcoord : TEXCOORD0;
fixed4 color : COLOR;
};
struct v2f
{
float4 vertex : SV_POSITION;
half2 texcoord : TEXCOORD0;
fixed4 color : COLOR;
fixed grey:TEXCOORD1;
float4 worldPosition : TEXCOORD2;
};
sampler2D _MainTex;
float4 _MainTex_ST;
fixed _Grey;
//-------------------add----------------------
float _Silder;
float2 _Center;
//-------------------add----------------------
v2f vert (appdata_t v)
{
v2f o;
o.worldPosition=v.vertex;
o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
o.texcoord = v.texcoord;
o.color = v.color;
o.grey=dot(v.color,fixed4(1,1,1,0));
return o;
}
fixed4 frag (v2f i) : COLOR
{
fixed4 col;
col = tex2D(_MainTex, i.texcoord);
if(i.grey==0){
fixed3 grey= Luminance(col.rgb);
col.rgb=lerp(grey,col.rgb,_Grey);
col.a=col.a*i.color.a;
}
else col=col*i.color;
//-------------------add----------------------
col.a*=(distance(i.worldPosition.xy,_Center.xy) > _Silder);
col.rgb*= col.a;
//-------------------add----------------------
return col;
}
ENDCG
}
}
SubShader
{
LOD 100
Tags
{
"Queue" = "Transparent"
"IgnoreProjector" = "True"
"RenderType" = "Transparent"
}
Pass
{
Cull Off
Lighting Off
ZWrite Off
Fog { Mode Off }
Offset -1, -1
ColorMask RGB
Blend SrcAlpha OneMinusSrcAlpha
ColorMaterial AmbientAndDiffuse
SetTexture [_MainTex]
{
Combine Texture * Primary
}
}
}
}
ps:友情附送,灰度效果,及灰度与原图的插值控制,从而实现灰度效果下的低彩效果(参见王者荣耀英雄卡片)。
新建一个材质,选择该shader后,给uitexture.uitexture获得material后,设置参数,如果无效果很正常,记得MarkAsChanged();试试。