這里通過 “是否丟棄像素”的2種方法,寫2個shader,效果是一樣的,也提到了,丟棄某個像素的3種方式。
是否丟棄:
1.通過腳本控制shader變量判斷當前是否丟棄像素,需要額外腳本;
2.shader根據當前時間控制是否丟棄某個像素,不需要額外腳本。
丟棄方法:
1.通過clip函數進行丟棄像素;
2.通過discard丟棄像素;
3.通過設置alpha變量為0丟棄像素。
1)DissolveOne.shader通過腳本控制
Shader "Unlit/DissolveOne" { Properties { //外部腳本控制_Value來控制是否丟棄某個像素 _MainTex ("Texture", 2D) = "white" {} _DissolveTex("Texture", 2D) = "white" {} _DissSize("DissSize", Range(0, 1)) = 0.1 //溶解閾值,小於閾值才屬於溶解帶 _DissColor("DissColor", Color) = (1,0,0,1)//溶解帶的漸變顏色,與_AddColor配合形成漸變色 _AddColor("AddColor", Color) = (1,1,0,1) _Value("Value", Range(0,1)) = 0.5 //這個屬性其實不用開放出來的,通過腳本控制就好,但我想看效果卻懶得寫腳本,就這樣吧 } SubShader { Tags { "RenderType"="Opaque" } LOD 100 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; }; sampler2D _MainTex; sampler2D _DissolveTex; float4 _MainTex_ST; half _Value;//腳本控制的變量 half _DissSize; half4 _DissColor, _AddColor; v2f vert (appdata v) { v2f o; o.vertex = mul(UNITY_MATRIX_MVP, v.vertex); o.uv = TRANSFORM_TEX(v.uv, _MainTex); return o; } fixed4 frag (v2f i) : SV_Target { fixed4 col = tex2D(_MainTex, i.uv); float dissolveValue = tex2D(_DissolveTex, i.uv).r; float clipValue = dissolveValue - _Value; clip(clipValue);// 如果clipValue<0則拋棄當前像素, //clip is equivalent to (if(clipValue<0)discard;),但好像好像discard需要更高一點的硬件支持 //也可以通過設置col.a = 0拋棄當前像素:if(clipValue<0)col.a = 0; if (clipValue > 0 && clipValue < _DissSize) { //溶解帶漸變 half4 dissolveColor = lerp(_DissColor, _AddColor, clipValue / _DissSize)* 2; col *= dissolveColor; } return col; } ENDCG } } }
2)DissolveTwo.shader根據當前時間控制是否丟棄某個像素
Shader "Unlit/DissolveTwo" { Properties { //shader根據當前時間控制是否丟棄某個像素 _MainTex("Texture", 2D) = "white" {} _DissolveTex("Texture", 2D) = "white" {} _DissSize("DissSize", Range(0, 1)) = 0.1 //溶解閾值,小於閾值才屬於溶解帶 _DissColor("DissColor", Color) = (1,0,0,1)//溶解帶的漸變顏色,與_AddColor配合形成漸變色 _AddColor("AddColor", Color) = (1,1,0,1) _DissSpeed("DissSpeed", Float) = 1 } SubShader { Tags { "RenderType"="Opaque" } LOD 100 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; }; sampler2D _MainTex; sampler2D _DissolveTex; float4 _MainTex_ST; half _DissSize; half4 _DissColor, _AddColor; half _DissSpeed; v2f vert (appdata v) { v2f o; o.vertex = mul(UNITY_MATRIX_MVP, v.vertex); o.uv = TRANSFORM_TEX(v.uv, _MainTex); return o; } fixed4 frag (v2f i) : SV_Target { // sample the texture fixed4 col = tex2D(_MainTex, i.uv); //因為這個溶解過程很快,所以為了看效果,可以改成:saturate(_Time.y % _DissSpeed / _DissSpeed),重復溶解 float dissolveFactor = saturate(_Time.y / _DissSpeed); float dissolveValue = tex2D(_DissolveTex, i.uv).r; float delta = dissolveValue - dissolveFactor; if (delta < 0) { discard; } if (delta >= 0 && delta < _DissSize) { float leftFactor = delta / _DissSize; half4 dissolveColor = lerp(_DissColor, _AddColor, leftFactor) * 2; col *= dissolveColor; } return col; } ENDCG } } }
兩者溶解效果是一樣的:

參考文章:
1.http://blog.csdn.net/u011047171/article/details/46873217
2.http://www.cnblogs.com/Esfog/p/DissolveShader.html
