1. 程式人生 > >大光頭的shader學習之路-自己寫的一個消融效果

大光頭的shader學習之路-自己寫的一個消融效果

原理很簡單,設定一張黑白的遮罩圖,根據遮罩圖的某個非alpha通道的顏色從深到淺,逐漸設定透明和溶解的顏色。

直接貼程式碼:


Shader "MyShader/DissMask" {
	Properties {
		_MainTex ("Albedo (RGB)", 2D) = "white" {}
		_MaskTex("Mask Image",2D) = "white"{}
		_MainColor("Main Color", Color) = (1,1,1,1)
		_LightColor("Light Color", Color) = (1,1,1,1)
		_DissFactor("Diss Factor ",Range(0,1)) = 0
		_Cutoff("_Cutoff ",Range(0,1)) = 0
	}
	SubShader {
		Tags { "RenderType"="Transparent" }
		LOD 200
		Cull off
			//ZWrite Off
		CGPROGRAM
		// Physically based Standard lighting model, and enable shadows on all light types
		#pragma surface surf Lambert alphatest:_Cutoff

		sampler2D _MainTex;
		sampler2D _MaskTex;

		struct Input {
			float2 uv_MainTex;
			float2 uv_MaskTex;
		};

		fixed4 _MainColor;
		fixed4 _LightColor;
		fixed _DissFactor;

		void surf (Input IN, inout SurfaceOutput o) {
			// Albedo comes from a texture tinted by color
			fixed4 c = tex2D (_MainTex, IN.uv_MainTex);
			fixed4 m = tex2D(_MaskTex, IN.uv_MaskTex);

			fixed color = m.r;
			if (color < _DissFactor)
			{
				o.Alpha = 0;
			}else
			{
				if (color-_DissFactor<0.1 && _DissFactor!=0)
				{
					fixed temp = (color - _DissFactor) * 10;
					o.Albedo = (_MainColor.rgb * temp + _LightColor.rgb *(1-temp));
					o.Alpha = temp;
				}
				else
				{
					o.Albedo = c.rgb;
					o.Alpha = c.a;
				}
			}
		}
		ENDCG
	}
	FallBack "Diffuse"
}

上面程式碼在處理消融顏色的時候,感覺效果並不太理想。又不知道怎麼修改,希望大神給點建議。

重點記錄一下新學到的內容,在剛開始#program是這麼寫的

因為是三維消融,所以要把雙面渲染開啟,這樣就可以透過前面透明的位置看到後面的內容

開啟雙面渲染比較簡單,只要把CULL屬性關閉就好了

在剛開始#pragma是這麼寫的

#pragma surface surf Lambert alpha:blend

但是這樣寫會再渲染時產生深度錯亂的問題

具體原因請看這篇文章

http://blog.csdn.net/candycat1992/article/details/41599167

於是,把#pragma修改為這樣子

#pragma surface surf Lambert alphatest:_Cutoff

==========================================================================

晚上睡覺的時候想到一種很普遍的需求,從上往下或者從下往上消失。這就需要根據頂點座標來改變畫素透明度


沒什麼技術含量,改了一下程式碼

Shader "MyShader/DissMask" {
	Properties {
		_MainTex ("Albedo (RGB)", 2D) = "white" {}
		_MaskTex("Mask Image",2D) = "white"{}
		_MainColor("Main Color", Color) = (1,1,1,1)
		_LightColor("Light Color", Color) = (1,1,1,1)
		_DissFactor("Diss Factor ",Range(-2,2)) = 0
		_Trace("Trace Range",Range(0,0.5))=0
		_Cutoff("_Cutoff ",Range(0,1)) = 0
	}
	SubShader {
		Tags { "RenderType"="Transparent" }
		LOD 200
		Cull off
			//ZWrite Off
		CGPROGRAM
		// Physically based Standard lighting model, and enable shadows on all light types
		#pragma surface surf Lambert alphatest:_Cutoff vertex:vert

		sampler2D _MainTex;
		sampler2D _MaskTex;

		struct Input {
			float2 uv_MainTex;
			float2 uv_MaskTex;
			fixed4 vertColor;
		};

		fixed4 _MainColor;
		fixed4 _LightColor;
		fixed _DissFactor;
		fixed _Trace;

		void vert(inout appdata_full v, out Input o)
		{
			UNITY_INITIALIZE_OUTPUT(Input, o);

			o.vertColor = v.color; 
			if (v.vertex.z<_DissFactor)
			{
				o.vertColor.a = 0;
			}
			else
			{
				if (v.vertex.z < _DissFactor + _Trace)
				{
					o.vertColor.a = 0.5;
				}
			}
		}

		void surf (Input IN, inout SurfaceOutput o) {
			// Albedo comes from a texture tinted by color
			fixed4 c = tex2D (_MainTex, IN.uv_MainTex);
			fixed4 m = tex2D(_MaskTex, IN.uv_MaskTex);

			fixed color = m.r;

			if (IN.vertColor.a==0.5)
			{
				o.Albedo = (c.rgb+ m.r*_MainColor+(1-m.r)*_LightColor)/2;
				o.Alpha = 1;
			}
			else
			{
				o.Albedo = c.rgb;
				o.Alpha = IN.vertColor.a;
			}
		}

		ENDCG
	}
	FallBack "Diffuse"
}

一分錢的過渡效果,調了好長時間都不滿意,就先這樣吧