Shader2.0-一個簡單的uv動畫:旋轉
阿新 • • 發佈:2018-11-15
旋轉公式為矩陣的旋轉公式,具體可以自行百度,這裡博主不在詳細解釋。
Shader "Hidden/TestStruct" { Properties { _MainTex ("Texture", 2D) = "white" {} _Speed("Speed",float)=1 } SubShader { Pass { CGPROGRAM #pragma vertex vert #pragma fragment frag #include "UnityCG.cginc" float _Speed; sampler2D _MainTex; struct appdata { float4 vertex : POSITION; float2 uv : TEXCOORD0; }; struct v2f { float2 uv : TEXCOORD0; float4 vertex : SV_POSITION; }; v2f vert (appdata v) { v2f o; o.vertex = UnityObjectToClipPos(v.vertex); o.uv = v.uv; return o; } fixed4 frag (v2f i) : SV_Target { float2 tempUV = i.uv; //平移到原點位置 tempUV -= float2(0.5, 0.5); //旋轉 float angle = _Time.x*_Speed; float2 finalUV = 0; finalUV.x = tempUV.x*cos(angle) - tempUV.y*sin(angle); finalUV.y = tempUV.x*sin(angle) + tempUV.y*cos(angle); //平移回圖片原位置 finalUV+= float2(0.5, 0.5); return tex2D(_MainTex, finalUV); } ENDCG } } }