1. 程式人生 > >Unity 半透明shader以及擴充套件

Unity 半透明shader以及擴充套件

半透shader什麼時候用到呢

1.如果主角被東西擋住,攝像機要不拉近要不把擋住的物體半透

2.很多遊戲用到殘影,一般殘影都是半透的,然後慢慢透明

從左到右

1.使用標準材質球

2.使用半透材質球

3.使用外發光的半透材質球

4.使用內發光和外發光的材質球

shader比較簡單直接上程式碼

這些都是surface shader

1.半透shader

Shader "Snoopy/AlphaBase"
{
	Properties
	{
		_MainTex("Particle Texture", 2D) = "white" {}
		_Color("Color", Color) = (1,1,1,1)
	}
	
	SubShader
	{
		Tags{ "Queue" = "Transparent" "IgnoreProjector" = "True" "RenderType" = "Transparent" }

		//這裡加一個pass為了不會穿透疊自己的顏色,可以取消這個pass看看效果
		Pass
		{
			ZWrite On
			ColorMask 0
		}

		CGPROGRAM
		//使用alpha通道
		#pragma surface surf Lambert alpha

		struct Input
		{
			float2 uv_MainTex;
		};

		sampler2D _MainTex;
		fixed4 _Color;

		void surf(Input IN, inout SurfaceOutput o)
		{
			float4 col = tex2D(_MainTex, IN.uv_MainTex);
			o.Albedo = col.rgb * _Color.rgb;
			o.Alpha = _Color.a;
		}
		ENDCG
	}
	Fallback "VertexLit"
}

2.外發光的半透shader

Shader "Snoopy/AlphaRimBase"
{
	Properties
	{
		_MainTex("Particle Texture", 2D) = "white" {}
		_Color("Color", Color) = (1,1,1,1)
		_RimColor("Rim Color", Color) = (0.5,0.5,0.5,0.5)
		_RimPower("Rim Power", Range(0.0,5.0)) = 2.5
	}
	SubShader
	{
		Tags{ "Queue" = "Transparent" "IgnoreProjector" = "True" "RenderType" = "Transparent" }

		Pass
		{
			ZWrite On
			ColorMask 0
		}

		CGPROGRAM
		#pragma surface surf Lambert alpha

		struct Input
		{
			float3 viewDir;
			float2 uv_MainTex;
		};

		sampler2D _MainTex;
		float4 _Color;
		float4 _RimColor;
		float _RimPower;

		void surf(Input IN, inout SurfaceOutput o)
		{
			float4 col = tex2D(_MainTex, IN.uv_MainTex);
			o.Albedo = col.rgb * _Color.rgb;
			//邊緣高光
			half rim = 1.0 - saturate(dot(normalize(IN.viewDir), o.Normal));
			o.Emission = _RimColor.rgb * pow(rim, 6 - _RimPower);
			o.Alpha = _Color.a;
		}
		ENDCG
	}
	Fallback "VertexLit"
}

3.外發光和內發光的半透shader

Shader "Snoopy/AlphaRimPro"
{
	Properties
	{
		_MainTex ("Particle Texture", 2D) = "white" {}
		_RimColor ("Rim Color", Color) = (0.5,0.5,0.5,0.5)
		_InnerColor ("Inner Color", Color) = (0.5,0.5,0.5,0.5)
		_InnerColorPower ("Inner Color Power", Range(0.0,1.0)) = 0.5
		_RimPower ("Rim Power", Range(0.0,5.0)) = 2.5
		_AlphaPower ("Alpha Rim Power", Range(0.0,8.0)) = 4.0
		_AllPower ("All Power", Range(0.0, 10.0)) = 1.0
		_InnerAlphaBase ("Inner Alpha Base", Range(0.0, 1.0)) = 1.0
	}
	SubShader
	{
		Tags{ "Queue" = "Transparent" "IgnoreProjector" = "True" "RenderType" = "Transparent" }

		Pass
		{
			// 開啟深度寫入
			ZWrite On
			// 設定顏色通道的寫掩碼,0為不寫入任何顏色
			ColorMask 0
		}

		CGPROGRAM
		#pragma surface surf Lambert alpha
	
		struct Input
		{
			float3 viewDir;
			float2 uv_MainTex;
			INTERNAL_DATA
		};
	
		sampler2D _MainTex;
		float4 _RimColor;
		float _RimPower;
		float _AlphaPower;
		float _AlphaMin;
		float _InnerColorPower;
		float _AllPower;
		float4 _InnerColor;
		float _InnerAlphaBase;
		
		void surf (Input IN, inout SurfaceOutput o)
		{
			float4 col = tex2D (_MainTex, IN.uv_MainTex);
			o.Albedo = col.rgb;
			half rim = 1.0 - saturate(dot (normalize(IN.viewDir), o.Normal));
			o.Emission = _RimColor.rgb * pow (rim, _RimPower)*_AllPower+(_InnerColor.rgb*2*_InnerColorPower);
			o.Alpha = (_InnerAlphaBase + (pow (rim, _AlphaPower))*_AllPower) * col.a;
		}
		ENDCG
	}
Fallback "VertexLit"
} 

工程下載地址

連結:https://pan.baidu.com/s/11LTiIW0lIosNqokpucq6SQ 
提取碼:td8s