使用纹理可以使得Shaders更快速,也更逼真;使用的同时,需要控制纹理数目,避免过多加载纹理的运行消耗
下面逐步说明使用纹理制作流行水面的效果:
1.定义 X方向以及Y方向的速度变量(因为UV 是二维的),控制纹理的流动速度
Properties
{
_MainTint("Diffuse Tint",Color) = (1,1,1,1)
_MainTex("Base (RGB)",2D) = "White"{}
_ScrollXSpeed ("X Scroll Speed", Range(0,10)) = 2
_ScrollYSpeed( "Y Scroll Speed", Range(0,10)) = 2
}
2. 在CGPROGRAM 段 中添加 变量,这样可以访问到1中定义的 properties。
fixed4 _MainTint;
fixed _ScrollXSpeed;
fixed _ScrollYSpeed;
sampler2D _MainTex;
3. 修改 surface function,使用内置_Time 时间变量,控制Texture 的流动
void surf (Input IN, inout SurfaceOutput o)
{
//Create a separate variable to store out uvs
//before ve pass them to the tex2D() function
fixed2 scrolledUV = IN.uv_MainTex;
// Create variables that store the individual x and y
// components for the uvs scaled by time
fixed xScrollValue = _ScrollXSpeed * _Time;
fixed yScrollValue = _ScrollYSpeed * _Time;
//Apply the final uv offset
scrolledUV += fixed2(xScrollValue, yScrollValue);
// Apply textures and tint
half4 c = tex2D(_MainTex, scrolledUV);
o.Albedo = c.rgb * _MainTint;
o.Alpha = C.a;
}
解释:
Input 结构体中,定义了 float2 uv_MainTex, 所以在surf 函数中,首先把存储到scrolledUV 中。 这样,可以
通过控制speed,在后面给 ScrolledUV offset, 关于 _Time ,float4 变量,常使用来控制动画。