在之前3D项目上修改,可以应用在2D项目里面,处理了带有透明的图片。自己随便找一张噪声图就可以,颜色贴图不用的话,自己修改成一种颜色采样即可。贴上相应的shader代码:
Shader "UIEffect/Dissolve"
{
Properties
{
// _MainTex("Main Texture", 2D) = "defaulttexture" {}
_NoiseTex("噪声云图", 2D) = "defaulttexture" {}
_DissolveTex ("边缘色", 2D) = "defaulttexture" {}
_dissolve("消融值", Range(0, 1)) = 1
}
SubShader
{
Tags{
"Queue" = "Transparent"
"IgnoreProjector" = "True"
"RenderType" = "Transparent"
}
Cull Off ZWrite Off ZTest Always
Blend One OneMinusSrcAlpha
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};
struct v2f
{
float2 uv : TEXCOORD0;
float4 vertex : SV_POSITION;
};
v2f vert (appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = v.uv;
return o;
}
sampler2D _MainTex;
sampler2D _DissolveTex;
sampler2D _NoiseTex;
float _dissolve;
fixed4 frag (v2f i) : SV_Target
{
fixed4 MainCol = tex2D(_MainTex, i.uv);
MainCol.rgb *= MainCol.a;
// 随机阈值
fixed randValue = tex2D(_NoiseTex, i.uv).r;
float cutout = 0.6 - 1.2 * _dissolve + randValue;
clip(cutout - 0.5);
float weight = 1.0 - clamp(8 * cutout - 4, 0.0, 1.0);
float2 burnUV = float2(weight, 0);
fixed3 edgeColor = weight * tex2D(_DissolveTex, burnUV ).xyz;
edgeColor = step(0.05, MainCol.a) * edgeColor;
// // 需要剔除的部分
fixed3 finalCol = MainCol.rgb + edgeColor;
return fixed4(finalCol, MainCol.a);
}
ENDCG
}
}
}
`
![效果图](https://img-blog.csdnimg.cn/20200305172341975.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzM2NDYwNzMx,size_16,color_FFFFFF,t_70)