? ? ? ? ?我們都知道在游戲中,對于物體被溶解的效果滋恬,如降落傘溶解聊训、紙張溶解銷毀等,還是挺常見的恢氯,今天我和大家一起學(xué)習(xí)一下带斑,如何簡單實(shí)現(xiàn)一個(gè)溶解效果的Shader。
?1勋拟、新建Shader-Dissolve.shader文件勋磕,代碼如下:
Shader?"Custom/Shader-Dissolve"?{
????Properties?{
????????_MainTex?("Albedo?(RGB)",?2D)?=?"white"?{}
????????_DissolveTex("Sub(RGB)",2D)="white"{}
????????_EdgeColor("Main?Color",Color)=(1,1,1,1)
????????_EdgeValue("Dissolve?Range",Range(0,1))=0.1
????????_AddColor?("Add?Color",Color)=(1,1,1,1)
????????_UserColorValue("UserColorSpeed",Range(0,1))=0.2?
????}
????SubShader
????{
??????Pass
??????{
???????CGPROGRAM
???????#include?"UnityCG.cginc"?//引用unity?自帶的一些方法
???????#pragma?vertex?vert?//定義頂點(diǎn)著色器函數(shù)
???????#pragma?fragment?frag?//定義片元著色器函數(shù)
???????sampler2D?_MainTex;
???????float4?_MainColor;
???????sampler2D?_DissolveTex;
???????float4?_EdgeColor;
???????float4??_AddColor;
???????float?_EdgeValue;
???????float?_UserColorValue;
? ? ? ?float4?_DissColor;
? ? ? ?struct?a2v?//表示這個(gè)結(jié)構(gòu)體?是需要用到頂點(diǎn)函數(shù)的
???????{
?????????float4?position:POSITION;
?????????float2?uv:TEXCOORD0;
? ? ? ?};
? ? ? ?struct?v2f//表示頂點(diǎn)函數(shù)的返回值作為參數(shù)傳遞
???????{
????????float4?sv_position:SV_POSITION;
????????float2?sv_uv:TEXCOORD0;
???????};
???????v2f?vert?(a2v?i)//頂點(diǎn)函數(shù)
???????{
?????????v2f?o;
?????????o.sv_position?=?UnityObjectToClipPos(i.position);
?????????o.sv_uv?=i.uv;
?????????return?o;
???????}
???????float4?frag(v2f?v):SV_TARGET
???????{
?????????float4?mainTex=?tex2D?(_MainTex,v.sv_uv);
?????????float?dissolveValue=tex2D?(_DissolveTex,v.sv_uv).r;
?????????float?clipValue?=dissolveValue-_UserColorValue;
?????????clip(clipValue);
?????????if(clipValue>0?&&?clipValue<_EdgeValue)
?????????{
???????????float4?colors?=lerp(_DissColor,_AddColor,(clipValue/_EdgeValue)*2);
???????????mainTex=colors;
?????????}
?????????return?mainTex;
???????}
???????ENDCG
???????}
????}
}
2、最后測試效果敢靡,如圖示: