Shader——地形纹理混合

作者:追风剑情 发布于:2015-9-15 20:51 分类:Shader

函数 功能描述
lerp(a,b,f) 涉及线性插值(1-f)*a+b*f,在这里,a和b可以匹配为一个矢量或标题类型,f可以是一个标量或者与a、b同类型的矢量。

一、创建shader、material、Plane

Shader "Custom/TerrainBlend" {
	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 blendData = tex2D (_BlendTex, IN.uv_BlendTex);
			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);
			//使用lerp()函数(线性插值)对纹理进行混合。
			float4 finalColor;
			finalColor = lerp(rTexData, gTexData, blendData.g);
			finalColor = lerp(finalColor, bTexData, blendData.b);
			finalColor = lerp(finalColor, aTexData, blendData.a);
			finalColor.a = 1.0;
			
			o.Albedo = finalColor.rgb * _MainTint.rgb;
			o.Alpha = finalColor.a;
		}
		ENDCG
	} 
	FallBack "Diffuse"
}

t2.png

二、运行效果

t1.png

标签: Shader

Powered by emlog  蜀ICP备18021003号-1   sitemap

川公网安备 51019002001593号