直接上代码吧
//顶点动画
Shader "Custome/Chapter11/Water" {
Properties {
_MainTex("Base Layer (RGB)",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 {
// Pass for ambient light & first pixel light (directional light)
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 v2f {
float4 pos : POSITION;
float2 uv : TEXCOORD0;
};
v2f vert (appdata_base v) {
v2f o;
float4 offset;
offset.yzw = float3(0.0,0.0,0.0);
//注意这里水流图片是竖直的,所以他的边是x
//如果是横躺着的图片,则竖直的是Y。
offset.x = _Magnitude*
sin(_Frequency*_Time.y + v.vertex.x * _InvWaveLength + v.vertex.y*_InvWaveLength + v.vertex.z*_InvWaveLength);
o.pos = UnityObjectToClipPos(v.vertex + offset);
o.uv = TRANSFORM_TEX(v.texcoord, _MainTex);
//因为我们的水流图片是竖直的,所以为了让它表现出
//水流流速很快我们对其进行y+time;
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"
}
sin(_Frequency*_Time.y + v.vertex.x * _InvWaveLength + v.vertex.y*_InvWaveLength + v.vertex.z*_InvWaveLength);
由上图可知,这段代码中我们可以省去Y,X轴分量,他也可以很好的运行。