Unity Shader 学习笔记(十一) 混合纹理Shader实例

Unity Shader 学习笔记(十一) 混合纹理Shader实例

Shader "Custom/Blend" {
	Properties {

		_MainTint ("Diffuse Tint",Color) = (1,1,1,1)

		_ColorA ("Terrain Color A",Color) = (1,1,1,1)
		_ColorB ("Terrain Color B",Color) = (1,1,1,1)
		
		_RTexture ("Red Channel Texture", 2D) = "" {}
		_GTexture ("Green Channel Texture", 2D) = "" {}
		_BTexture ("Blue Channel Texture", 2D) = "" {}
		_ATexture ("Alpha Channel Texture", 2D) = "" {}

		_BlendTex ("Blend Texture", 2D) = "" {}
	}
	SubShader {
		Tags { "RenderType"="Opaque" }
		LOD 200
		
		CGPROGRAM
		#pragma surface surf Lambert

		float4 _MainTint;
		float4 _ColorA;
		float4 _ColorB;

		sampler2D _RTexture;
		sampler2D _GTexture;
		sampler2D _BTexture;
		sampler2D _ATexture;
		
		sampler2D _BlendTex;

		struct Input {
			float2 uv_RTexture;
			float2 uv_GTexture;
			float2 uv_BTexture;
			float2 uv_ATexture;
			float2 uv_BlendTex;
		};

		void surf (Input IN, inout SurfaceOutput o) {
			
			float4 rTexData = tex2D(_RTexture,IN.uv_RTexture);
			float4 gTexData = tex2D(_GTexture,IN.uv_GTexture);
			float4 bTexData = tex2D(_BTexture,IN.uv_BTexture);
			float4 aTexData = tex2D(_ATexture,IN.uv_ATexture);
			float4 blendData = tex2D(_BlendTex,IN.uv_BlendTex);

			float4 finalColor;
			//根据blendData.g 将 RTexture 和 GTexture 混合
			finalColor = lerp(rTexData,gTexData,blendData.g);
			//根据blendData.b 将 BTexture 混合
			finalColor = lerp(finalColor,bTexData,blendData.b);
			//根据blendData.a 将 ATexture 混合

			finalColor = lerp(finalColor,aTexData,blendData.a);
			//根据blendData.r 将 ColorA,ColorB 混合
			float4 terrainLayers = lerp(_ColorA,_ColorB,blendData.r);
			finalColor *= terrainLayers;

			//防止越界 如果 x 小于 0 ,返回 0 ;如果 x 大于 1 ,返回 1 ;否则,返回 x
			finalColor = saturate(finalColor);

			
			o.Albedo = finalColor.rgb * _MainTint.rgb;
			o.Alpha = finalColor.a;
		}
		ENDCG
	} 
	FallBack "Diffuse"
}


你可能感兴趣的:(Unity,Shader)