Shader "Custom/UVAnimation"
{
Properties
{
// 設置紋理1以及紋理2
_MainTex ("Texture", 2D) = "white" {}
_SubTex ("SubTexture", 2D) = "white" {}
}
SubShader
{
Tags { "RenderType"="Opaque" }
LOD 100
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};
struct v2f
{
float2 uv : TEXCOORD0;
float4 vertex : SV_POSITION;
};
// 要在片元中使用,就必須要在這裏定義,得和Properties裏面同樣的名字
sampler2D _SubTex;
sampler2D _MainTex;
float4 _MainTex_ST;
v2f vert (appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = TRANSFORM_TEX(v.uv, _MainTex);
return o;
}
fixed4 frag (v2f i) : SV_Target
{
// 定義一個紋理偏移量,這個偏移量用來偏移紋理,產生紋理移動的效果
float2 uvoffset = float2(0,0);
//將x和y方向的偏移量設置成根據時間的延時,紋理時二維的,只有u,v兩個方向
uvoffset.x = _Time.y;
uvoffset.y = _Time.y;
// 將紋理的座標加上偏移量
fixed4 subcol = tex2D(_SubTex, i.uv+uvoffset);
// sample the texture
fixed4 col = tex2D(_MainTex, i.uv);
fixed4 ResColor = subcol + col;
return ResColor;
}
ENDCG
}
}
}
該效果可以應用於一些水面流動的效果,或者光線透過水照射在魚兒身上的效果
Tip:記得將紋理的循環模式修改成repeat模式