In this article, we will explain how to implement UV animations in Unity3d with shader to modify the texture UV coordinates to scroll the map.
First look at the effect diagram
UV animation is a commonly used rendering technique, often used to describe the flow of water, flashing lights and so on. The principle of implementation is to dynamically modify the map's UV coordinates, so that the surface of the object to produce changes. Using different algorithmic models can achieve many very complex effects, the following is the shader code:Shader "Unlit/myshader/uvaim"
{
Properties
{
_maintex ("Texture", 2D) = "White" {}
_scrolly ("Scroll speed", Float) = 1.0
}
Subshader
{
Tags {"Rendertype" = "Opaque"}
LOD 100
Pass
{
Cgprogram
#pragma vertex vert
#pragma fragment Frag
Make fog work
#pragma multi_compile_fog
#include "Unitycg.cginc"
float _scrolly;
struct AppData
{
FLOAT4 vertex:position;
FLOAT2 uv:texcoord0;
};
struct V2F
{
FLOAT2 uv:texcoord0;
Unity_fog_coords (1)
FLOAT4 vertex:sv_position;
};
Sampler2d _maintex;
FLOAT4 _maintex_st;
v2f Vert (AppData v)
{
v2f o;
O.vertex = Unityobjecttoclippos (V.vertex);
O.UV = Transform_tex (V.uv, _maintex) + frac (float2 (0, _scrolly) * _time.y);
Unity_transfer_fog (O,o.vertex);
return o;
}
Fixed4 Frag (v2f i): Sv_target
{
Sample the texture
Fixed4 col = tex2d (_maintex, I.UV);
Apply Fog
Unity_apply_fog (I.fogcoord, col);
return col;
}
Endcg
}
}
Fallback "Vertexlit"
}