河流模拟是顶点动画最常见的应用之一,它的原理通常是使用正弦函数等来模拟水流的波动效果。
Shader "MyShader/Water"
{
Properties
{
_MainTex("Main Tex", 2D) = "white" {}
_Color("Color Tint", Color) = (1, 1, 1, 1)
_Magnitude("Distortion Magnitude", Float) = 1
_Frequency("Distortion Frequency", Float) = 1
_InvWaveLength("Distortion Inverse Wave Length", Float) = 10
_Speed("Speed", Float) = 0.5
}
SubShader
{
Tags {"Queue" = "Transparent" "IgnoreProjector" = "True" "RenderType" = "Transparent" "DisableBatching" = "True"}
Pass {
Tags { "LightMode" = "ForwardBase" }
ZWrite Off
Blend SrcAlpha OneMinusSrcAlpha
Cull Off
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
sampler2D _MainTex;
float4 _MainTex_ST;
fixed4 _Color;
float _Magnitude;
float _Frequency;
float _InvWaveLength;
float _Speed;
struct a2v
{
float4 vertex : POSITION;
float4 texcoord : TEXCOORD0;
};
struct v2f
{
float4 pos : SV_POSITION;
float2 uv : TEXCOORD0;
};
v2f vert(a2v v)
{
v2f o;
float4 offset;
offset.yzw = float3(0.0, 0.0, 0.0);
offset.x = sin(_Frequency * _Time.y + v.vertex.x * _InvWaveLength + v.vertex.y * _InvWaveLength + v.vertex.z * _InvWaveLength) * _Magnitude;
o.pos = UnityObjectToClipPos(v.vertex + offset);
o.uv = TRANSFORM_TEX(v.texcoord, _MainTex);
o.uv += float2(0.0, _Time.y * _Speed);
return o;
}
fixed4 frag(v2f i) : SV_Target
{
fixed4 c = tex2D(_MainTex, i.uv);
c.rgb *= _Color.rgb;
return c;
}
ENDCG
}
}
FallBack "Transparent/VertexLit"
}
其中,_MainTex 是河流纹理,_Color 用于控制整体颜色,_Magnitude 用于控制水流波动的幅度,_Frequency 用于控制波动频率,_InvWaveLength 用于控制波长的倒数,_Speed 用于控制河流的移动速度。
设置中,标签 DisableBatching 是否对该 SubShader 使用批处理。
一些SubShader在使用 unity 批处理功能时会出现问题,这时可以使用该标签来指明。而这些需要特殊处理的 shader 通常就是指包含了模型空间的顶点动画。这是因为,批处理会合并所有相关的模型,而这些模型各自的模型空间就会丢失。
我们首先计算顶点位移量,我们只希望对顶点的 x 方向进行位移,因此 yzw 的位移量被设置为0。
然后,我们利用_Frequency属性和内置的_Time.y变量来控制正弦函数的频率。为了让不同位置具有不同的位移,我们对上述结果加上了模型空间下的位置分量,并乘以_InvWaveLength来控制波长。最后,我们对结果值乘以_Magnitude属性来控制波动幅度,得到最终的位移。剩下的工作,我们只需要把位移量添加到顶点位置上,在进行正常的顶点变换即可。在上面的代码中,我们还进行了纹理动画,即使用_Time.y和_Speed来控制在水平方向上的纹理动画。
参考 我买的 unity shader 入门精要