Unity Shader(九)压缩和混合纹理贴图

效果说明示例: 

 使用纹理贴图示例:

混合系数贴图示例:

Shader "Custom/BasicDiffuse"
{
	//属性
	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

		//编译指令,定义表面着色器和将要使用的光照模型Lambert(漫反射)
		#pragma surface surf Lambert

		//声明属性关联		
		float4 _MainTint;
		float4 _ColorA;
		float4 _ColorB;
		sampler2D _RTexture;
		sampler2D _GTexture;
		sampler2D _BTexture;
		sampler2D _ATexture;
		sampler2D _BlendTex;

		//表面着色器输入结构体
		struct Input
		{
			//纹理的UV坐标值
			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_BlendTex);
			float4 gTexData = tex2D(_GTexture, IN.uv_BlendTex);
			float4 bTexData = tex2D(_BTexture, IN.uv_BlendTex);
			float4 aTexData = tex2D(_ATexture, IN.uv_BlendTex);

			//使用线性插值对纹理进行混合
			float4 finalColor;
			finalColor = lerp(rTexData, gTexData, blendData.g);
			finalColor = lerp(finalColor, bTexData, blendData.g);
			finalColor = lerp(finalColor, aTexData, blendData.g);
			finalColor.a = 1.0;

			//将混合纹理与色调值相乘,并使用红色通道来确定两个不同地形将形成怎样的色调
			float4 terrainLayers = lerp(_ColorA, _ColorB, blendData.r);
			finalColor *= terrainLayers;
			finalColor = saturate(finalColor);

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

材质球设置

效果图:

lerp()函数

lerp(a,b,f) ---功能描述--->(1-f)*a+b*f

效果示例:

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值