参考链接:http://www.cnblogs.com/Esfog/p/DissolveShader.html
效果图:
从颜色变化来说,有三种,一种是纹理颜色,一种是纹理与黑边的混合颜色,一种是透明(用discard处理,不绘制像素)。还需要一张纹理(任意),根据它的任意一个通道来控制哪部分先消失。例如上面的效果图用的就是这张纹理的r通道:
如果用的是这张纹理(也就是模型自身的纹理)的r通道,会更加酷:
溶解效果代码如下:
Shader "Esfog/Dissolve" { Properties { _MainTex ("Base (RGB)", 2D) = "white" {} _NoiseTex ("NoiseTex (R)",2D) = "white"{} _DissolveSpeed ("DissolveSpeed (Second)",Float) = 1 _EdgeWidth("EdgeWidth",Range(0,0.5)) = 0.1 _EdgeColor("EdgeColor",Color) = (1,1,1,1) _StartTime("StartTime",Float) = 0 } SubShader { Tags { "RenderType"="Opaque" } Pass { CGPROGRAM #pragma vertex vert_img #pragma fragment frag #include "UnityCG.cginc" uniform sampler2D _MainTex; uniform sampler2D _NoiseTex; uniform float _DissolveSpeed; uniform float _EdgeWidth; uniform float4 _EdgeColor; uniform float _StartTime; float4 frag(v2f_img i):COLOR { float DissolveFactor = saturate((_Time.y - _StartTime) / _DissolveSpeed); float noiseValue = tex2D(_NoiseTex,i.uv).r; if(noiseValue <= DissolveFactor) { discard; } float4 texColor = tex2D(_MainTex,i.uv); float EdgeFactor = saturate((noiseValue - DissolveFactor)/(_EdgeWidth*DissolveFactor)); float4 BlendColor = texColor * _EdgeColor; return lerp(texColor,BlendColor,1 - EdgeFactor); } ENDCG } } FallBack Off }
Shader "lyh/Show" { Properties { _MainTex ("Base (RGB)", 2D) = "white" {} _NoiseTex ("NoiseTex (R)",2D) = "white"{} _DissolveSpeed ("DissolveSpeed (Second)",Float) = 1 _EdgeWidth("EdgeWidth",Range(0,0.5)) = 0.1 _EdgeColor("EdgeColor",Color) = (1,1,1,1) _StartTime("StartTime",Float) = 0 } SubShader { Tags { "RenderType"="Opaque" } Pass { CGPROGRAM #pragma vertex vert_img #pragma fragment frag #include "UnityCG.cginc" uniform sampler2D _MainTex; uniform sampler2D _NoiseTex; uniform float _DissolveSpeed; uniform float _EdgeWidth; uniform float4 _EdgeColor; uniform float _StartTime; float4 frag(v2f_img i):COLOR { float DissolveFactor = saturate((_Time.y - _StartTime) / _DissolveSpeed); float noiseValue = tex2D(_NoiseTex,i.uv).r; float4 texColor = tex2D(_MainTex,i.uv); float EdgeFactor = saturate((noiseValue - DissolveFactor)/(_EdgeWidth*DissolveFactor)); float4 BlendColor = texColor * _EdgeColor; clip(1 - EdgeFactor - 0.01); return lerp(BlendColor,texColor,1 - EdgeFactor); } ENDCG } } FallBack Off }
material.SetFloat("_StartTime", Time.realtimeSinceStartup);