Unity Shader播放序列幀動畫


 1 Shader "LordShader/AnimateSprite" {
 2     Properties {
 3         _MainTint ("Diffuse Color", Color) = (1,1,1,1)            //顏色屬性,可以在u3d inspector面板控制該變量
 4         _MainTex("Base (RGB)",2D) = "white" {}  //貼圖
 5         _TexWidth("Sheet Width",float) = 0.0    //貼圖寬度像素值
 6         _SpriteFrameNum("Sprite Frame Counts",float) = 9.0  //總幀數
 7         _Speed("Speed ",Range(0.01,32)) = 12    //播放速度
 8     }
 9     SubShader {
10         Tags { "RenderType"="Opaque" }
11         LOD 200
12         
13         CGPROGRAM    
14         #pragma surface surf Lambert
15 
16         fixed4 _MainTint;            //主顏色
17         sampler2D _MainTex;            //主貼圖
18         float _TexWidth;            //貼圖寬度像素值
19         float _SpriteFrameNum;        //動畫幀數
20         float _Speed;                //播放速度
21         float  _TimeValue;            //從腳本傳遞過來的數
22 
23         struct Input {
24             float2 uv_MainTex;
25         };
26 
27         void surf (Input IN, inout SurfaceOutput o) {
28             float2 spriteUV = IN.uv_MainTex;
29             float uAddPerFrame = 1 / _SpriteFrameNum;         //每一幀U值的增量
30             
31             //獲取一個0 1 2 3 循環的值 
32             //fmod 返回 x/y 的余數(取模)。如果 y 為 0 ,結果不可預料
33             float timeVal = fmod(_Time.y * _Speed,_SpriteFrameNum);  //進行取余數操作 得到當前要顯示的圖片的下標
34             timeVal = ceil(timeVal);
35             
36             //float timeVal = _TimeValue;        //_TimeValue直接通過腳本傳遞 material.SetFloat("_TimeValue",timeVal);    
37             float xValue = spriteUV.x;            //UV坐標中的X坐標(0到9)
38             xValue *= uAddPerFrame;                //把UV值指定到第一張小圖的范圍 注意
39 
40             xValue += timeVal * uAddPerFrame; //每次執行把圖片切下一張小圖,累加u的增量值
41             spriteUV = float2(xValue,spriteUV.y);        
42             fixed4 c = tex2D (_MainTex, spriteUV) * _MainTint;
43             o.Albedo = c.rgb * _MainTint;
44             o.Alpha = c.a;
45         }
46         ENDCG
47     } 
48     FallBack "Diffuse"
49 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM