今天给大家看下,UV动画,极坐标是如何实现的,算法也比较简单,老规矩,先上效果图:
资源贴图源文件:
极坐标完整算法:
atan2 可以查阅下资料,这里不展开讲解了 ,
float2 Remap = (i.uv*2.0+-1.0);
float2 Uv = Remap.rg;
//UV动画播放 6.28318530718 是2π;
float2 UvAnimation = float2(((_Speed*time.g)+length(Remap)),((atan2(Uv.r,Uv.g)/6.28318530718)+0.5));
源码奉上:
Shader "Unlit/PolarCoordinates" {
Properties {
_MainTex ("MainTex", 2D) = "white" {}
_Speed ("Speed", Float ) = 0
}
SubShader {
Tags {
"RenderType"="Opaque"
}
LOD 100
Pass {
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
#pragma multi_compile_fog
sampler2D _MainTex;
float _Speed;
struct a2v {
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};
struct v2f {
float4 pos : SV_POSITION;
float2 uv : TEXCOORD0;
UNITY_FOG_COORDS(1)
};
v2f vert(a2v v) {
v2f o;
o.uv = v.uv;
o.pos = UnityObjectToClipPos( v.vertex );
UNITY_TRANSFER_FOG(o,o.pos);
return o;
}
float4 frag(v2f i) : SV_Target {
float4 time = _Time;
//修改uv坐标
float2 Remap = (i.uv*2.0+-1.0);
float2 Uv = Remap.rg;
//UV动画播放 6.28318530718 是2π;
float2 UvAnimation = float2(((_Speed*time.g)+length(Remap)),((atan2(Uv.r,Uv.g)/6.28318530718)+0.5));
half4 col= tex2D(_MainTex,UvAnimation);
UNITY_APPLY_FOG(i.fogCoord, col);
return col;
}
ENDCG
}
}
FallBack "Diffuse"
}