Shader_動畫序列幀

// Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'

Shader "Zombie/ZombieAnimation"
{
	Properties
	{
		_MainTex("Base(RGB)" ,2D) = ""{}
		_Row("Row",Int) = 1
		_Column("Column",Int) = 1
		_Speed("Speed",Range(0,60)) = 30
	}

		SubShader
		{
			tags{"Queue" = "Transparent" "RenderType" = "Transparent" "IgnoreProjector" = "True" "PreviewType" = "Plane" }
			Blend SrcAlpha OneMinusSrcAlpha
			ColorMask RGB
			ZWrite Off
			Cull Off
			Pass
			{
				CGPROGRAM
				#pragma vertex vert
				#pragma fragment frag
				#include "UnityCG.cginc"

				struct v2f
				{
					float4 pos:POSITION;
					float2 uv:TEXCOORD0;
				};

				sampler2D _MainTex;
				float4 _MainTex_ST;

				int _Row;
				int _Column;
				float _Speed;

				v2f vert(appdata_base v)
				{
					v2f o;
					o.pos = UnityObjectToClipPos(v.vertex);
					o.uv = TRANSFORM_TEX(v.texcoord,_MainTex);
					return o;
				}

				half4 frag(v2f IN) :COLOR
				{
					float2 uv = IN.uv;

					float cellX = uv.x / _Column;
					float cellY = uv.y / _Row;

					//Sprite總數
					int count = _Row * _Column;

					//在0到count-1 範圍內循環
					int SpriteIndex = fmod(_Time.w*_Speed,count);

					//當前Sprite所在行的下標
					int SpriteRowIndx = (SpriteIndex / _Column);

					//當前Sprite所在列的下標
					int SpriteColumnIndex = fmod(SpriteIndex,_Column);

					//因uv座標左下角爲(0,0),第一行爲最底下一行,爲了合乎我們常理,我們轉換到最上面一行爲第一行,eg:0,1,2-->2,1,0
					SpriteRowIndx = (_Row - 1) - fmod(SpriteRowIndx,_Row);

					//乘以1.0轉爲浮點數
					uv.x = cellX + SpriteColumnIndex * 1.0 / _Column;
					uv.y = cellY + SpriteRowIndx * 1.0 / _Row;

					half4 c = tex2D(_MainTex,uv);
					return c;
				}
				ENDCG
			}
		}
			FallBack "Diffuse"
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章