Shader播放序列帧

<1>代码

Shader "Tang/622/FrameAnim"{

	Properties{
		_MainTex("序列帧",2D) = "white" { }
		_Row("行",Float) = 8
	    _Col("列",Float) = 8
		_Speed("速度",Float) = 80
	}

	SubShader{
		Tags {"Queue" = "Transparent" "RenderType" = "Transparent"}
		Pass{
			Tags { "LightMode" = "ForwardBase" }
			ZWrite off
			cull off 
			Blend SrcAlpha OneMinusSrcAlpha 
			CGPROGRAM
			#pragma vertex vert 
			#pragma fragment frag 
			#include "UnityCG.cginc"

			struct a2v{
				float4 vertex:POSITION;
				float4 texcoord:TEXCOORD0;
			};
			struct v2f{
				float4 pos:SV_POSITION;
				float2 uv:TEXCOORD0;
			};
			sampler2D _MainTex;float4 _MainTex_ST;
			float _Row;
			float _Col;
			float _Speed;

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

			fixed4 frag(v2f i):SV_Target{
				//计算行列
				//思路
				//m*n个关键帧 一直循环
				//计算当前帧的起始uv.x和uv.y
				//最后把原始uv进行/Row/Col隐射 + 当前其实uv
				float row = 1/_Row;
				float col = 1/_Col;
				//限制到0-_Row*_Col 最大index
				float index = floor(_Time.y*_Speed)%(_Row*_Col);
				//限制到0-Row 左上方开始 uv.y从1开始
				float y = 1 - floor(index/_Col)*row;
				//限制到0-Col 左上开始 uv.x从0开始
				float x = (index+1) % _Row * col;
				x+= i.uv.x/_Row;
				y+= i.uv.y/_Col;
				fixed4 color = tex2D(_MainTex,float2(x,y));
				return color;				
			}

			ENDCG
		}
	}

	FallBack "Diffuse"
}

  

猜你喜欢

转载自www.cnblogs.com/cocotang/p/11060214.html