[UnityShader2]表面着色器实例(三)

一、访问顶点颜色

Shader "Cookbook/Chapter07/SimpleVertexColor" 
{
	Properties 
	{
		_MainTint("Global Color Tint", Color) = (1,1,1,1)
	}
	
	SubShader 
	{
		Tags { "RenderType"="Opaque" }
		LOD 200
		
		CGPROGRAM
		#pragma surface surf Lambert vertex:vert

		float4 _MainTint;

		struct Input 
		{
			float2 uv_MainTex;
			float4 vertColor;
		};
		
		void vert(inout appdata_full v, out Input o)
		{
			UNITY_INITIALIZE_OUTPUT(Input, o);
			o.vertColor = v.color;
		}

		void surf (Input IN, inout SurfaceOutput o) 
		{
			o.Albedo = IN.vertColor.rgb * _MainTint.rgb;
		}
		ENDCG
	} 
	FallBack "Diffuse"
}

[UnityShader2]表面着色器实例(三)_第1张图片


二、顶点动画

Shader "Cookbook/Chapter07/VertexAnimation" 
{
	Properties 
	{
		_MainTex ("Base (RGB)", 2D) = "white" {}
		_tintAmount ("Tint Amount", Range(0,1)) = 0.5
		_ColorA ("Color A", Color) = (1,1,1,1)
		_ColorB ("Color B", Color) = (1,1,1,1)
		_Speed ("Wave Speed", Range(0.1, 80)) = 5
		_Frequency ("Wave Frequency", Range(0, 5)) = 2
		_Amplitude ("Wave Amplitude", Range(-1, 1)) = 1
	}
	
	SubShader 
	{
		Tags { "RenderType"="Opaque" }
		LOD 200
		
		CGPROGRAM
		#pragma surface surf Lambert vertex:vert

		sampler2D _MainTex;
		float4 _ColorA;
		float4 _ColorB;
		float _tintAmount;
		float _Speed;
		float _Frequency;
		float _Amplitude;
		float _OffsetVal;

		struct Input 
		{
			float2 uv_MainTex;
			float3 vertColor;
		};
		
		void vert(inout appdata_full v, out Input o)
		{
			UNITY_INITIALIZE_OUTPUT(Input, o);

			float time = _Time * _Speed;
			float waveValueA = sin(time + v.vertex.x * _Frequency) * _Amplitude;
			
			v.vertex.xyz = float3(v.vertex.x, v.vertex.y + waveValueA, v.vertex.z);
			v.normal = normalize(float3(v.normal.x + waveValueA, v.normal.y, v.normal.z));
			o.vertColor = float3(waveValueA,waveValueA,waveValueA);
		}

		void surf (Input IN, inout SurfaceOutput o) 
		{
			half4 c = tex2D (_MainTex, IN.uv_MainTex);
			float3 tintColor = lerp(_ColorA, _ColorB, IN.vertColor).rgb; 
			
			o.Albedo = c.rgb * (tintColor * _tintAmount);
			o.Alpha = c.a;
		}
		ENDCG
	} 
	FallBack "Diffuse"
}

[UnityShader2]表面着色器实例(三)_第2张图片


三、顶点颜色混合

Shader "Cookbook/Chapter07/VertexHeightMapBlend" 
{
	Properties 
	{
		_MainTex ("Base (RGB)", 2D) = "white" {}
		_SecondaryTex ("Secondary Texture", 2D) = "white"{}
		_HeightMap ("HeightMap", 2D) = "white" {}
		_Value ("Value", Range(1, 20)) = 3
		_Switch ("Swtich", Float) = 0
	}
	
	SubShader 
	{
		Tags { "RenderType"="Opaque" }
		LOD 200
		
		CGPROGRAM
		#pragma surface surf Lambert vertex:vert

		sampler2D _MainTex;
		sampler2D _SecondaryTex;
		sampler2D _HeightMap;
		float _Value;
		float _Switch;

		struct Input 
		{
			float2 uv_MainTex;
			float2 uv_SecondaryTex;
			float3 vertColor;
		};
		
		void vert (inout appdata_full v, out Input o)
		{
			UNITY_INITIALIZE_OUTPUT(Input, o);
			o.vertColor = v.color.rgb;
		}

		void surf (Input IN, inout SurfaceOutput o) 
		{
			//Sample our textures
			half4 base = tex2D (_MainTex, IN.uv_MainTex);
			half4 secondTex = tex2D (_SecondaryTex, IN.uv_SecondaryTex);
			float4 height = tex2D(_HeightMap, IN.uv_MainTex);
			
			//perform blending
			float redChannel = 1-IN.vertColor.r;
			float rHeight = height.r * redChannel;
			float invertHeight = 1-height.r;
			float finalHeight = (invertHeight * redChannel)*4;
			float finalBlend = saturate(rHeight + finalHeight + pow(IN.vertColor.b, _Value));
			
			//Lets create more detail in how the vertex colors
			//blend.  Either very hard edge or very soft.
			float hardness = ((1-IN.vertColor.g) * _Value)+1;
			finalBlend = pow(finalBlend, hardness);
			
			//Produce the final color
			float3 finalColor = lerp(base, secondTex, finalBlend);
			
			if(_Switch == 0)
			{
				o.Albedo = finalColor;
			}
			else
			{
				o.Albedo = IN.vertColor;
			}
			o.Alpha = base.a;
		}
		ENDCG
	} 
	FallBack "Diffuse"
}

[UnityShader2]表面着色器实例(三)_第3张图片

你可能感兴趣的:(表面着色器)