首先我想指出,
someMat.Lerp();
不是 你问题的解决方案。
material Lerp()
函数用于更改颜色,同时保留原始着色器和纹理。由于您希望lerp纹理,因此情况并非如此。
此外,我认为在Unity中没有内置的方法来lerp纹理。但似乎可以使用着色器来规避此问题。经过短暂的谷歌之旅,我找到了这个UnityScript解决方案
source,其中包含一个简单而漂亮的着色器实现解决方案的示例。
着色器和相关代码示例也可以在下面看到。
public void Update ()
{
changeCount = changeCount - 0.05;
textureObject.renderer.material.SetFloat( "_Blend", changeCount );
if(changeCount <= 0) {
triggerChange = false;
changeCount = 1.0;
textureObject.renderer.material.SetTexture ("_Texture2", newTexture);
textureObject.renderer.material.SetFloat( "_Blend", 1);
}
}
}
在博客中给出的示例着色器为:
。
Shader "TextureChange" {
Properties {
_Blend ("Blend", Range (0, 1) ) = 0.5
_Color ("Main Color", Color) = (1,1,1,1)
_MainTex ("Texture 1", 2D) = "white" {}
_Texture2 ("Texture 2", 2D) = ""
_BumpMap ("Normalmap", 2D) = "bump" {}
}
SubShader {
Tags { "RenderType"="Opaque" }
LOD 300
Pass {
SetTexture[_MainTex]
SetTexture[_Texture2] {
ConstantColor (0,0,0, [_Blend])
Combine texture Lerp(constant) previous
}
}
CGPROGRAM
#pragma surface surf Lambert
sampler2D _MainTex;
sampler2D _BumpMap;
fixed4 _Color;
sampler2D _Texture2;
float _Blend;
struct Input {
float2 uv_MainTex;
float2 uv_BumpMap;
float2 uv_Texture2;
};
void surf (Input IN, inout SurfaceOutput o) {
fixed4 t1 = tex2D(_MainTex, IN.uv_MainTex) * _Color;
fixed4 t2 = tex2D (_Texture2, IN.uv_MainTex) * _Color;
o.Albedo = lerp(t1, t2, _Blend);
o.Normal = UnpackNormal(tex2D(_BumpMap, IN.uv_BumpMap));
}
ENDCG
}
FallBack "Diffuse"
}