UV序列幀動畫
阿新 • • 發佈:2018-01-13
sub sam ble parent image png ons custom 分享圖片
通過上圖實現一個火焰的序列幀動畫(使用UV實現美術只需要出一張圖,而不用每個火焰狀態出一張圖,節省了內存)。
shader如下:
Shader "Custom/UVAnimation" { Properties { _Color("Base Color", Color) = (1,1,1,1) _MainTex("Base(RGB)", 2D) = "white" {} _AnimationSpeed("AnimationSpeed", float) = 100 //動畫的播放速度 _FrameCount("FrameCount",float) = 12//這個動畫共幾幀 } SubShader { tags{"Queue" = "Transparent" "RenderType" = "Transparent" "IgnoreProjector" = "True"} Blend SrcAlpha OneMinusSrcAlpha Pass { CGPROGRAM #pragma vertex vert #pragmafragment frag #include "UnityCG.cginc" float4 _Color; sampler2D _MainTex; float _AnimationSpeed; float _FrameCount; struct v2f { float4 pos:POSITION; float2 uv:TEXCOORD0; };//處理動畫邏輯 float2 moveUV(float2 vertUV) { float index = frac(_Time.x / _FrameCount * _AnimationSpeed); float2 uvScale = float2(1 / _FrameCount, 1); for(int i = 1; i < _FrameCount + 1; i++) { if(index <= uvScale.x * i){ return vertUV * uvScale + float2( (i-1) * uvScale.x, 0.0);//計算UV的位移量 } } return vertUV * uvScale + float2((_FrameCount - 1) * uvScale.x, 0.0); } v2f vert(appdata_base v) { v2f o; o.pos = mul(UNITY_MATRIX_MVP, v.vertex); o.uv = moveUV(v.texcoord.xy); return o; } half4 frag(v2f i):COLOR { half4 c = tex2D(_MainTex , i.uv) * _Color; return c; } ENDCG } } }
UV序列幀動畫