序列帧动画.shader

时间:2022-05-31 04:00:58

序列帧动画.shader
由于gif丢帧了。。图片也不是很好看。。肯定不是我代码的问题。。
序列帧动画.shader

Shader "Custom/AnimationShader"
{
    Properties
    {
        _Color("Base Color", Color) = (1,1,1,1)
        _MainTex("Base(RGB)", 2D) = "white" {}
    }

    SubShader
    {
        tags{"Queue" = "Transparent" "RenderType" = "Transparent" "IgnoreProjector" = "True"}
        Blend SrcAlpha OneMinusSrcAlpha

        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            #include "UnityCG.cginc"

            float4 _Color;
            sampler2D _MainTex;

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

            struct appdata{
                float4 vertex : POSITION;
                float4 texcoord : TEXCOORD;
            };

            v2f vert(appdata v)
            {
                v2f o;
                o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
                o.uv = v.texcoord;
                return o;
            }

            float2 AnimationUV(float2 uv){
                //图片上的帧数
                float textureCount = 12;
                //通过时间更新图片
                float index = floor(_Time.z * 100 / textureCount);
                //图片停留时间--播放速度。其实可以设为变量方便调整
                float sourceX = 1.0 / 12.0;
                //更改UV的大小,是的物体上只有一帧
                uv.x *= sourceX;
                //更换帧
                uv.x += index * sourceX;
                return uv;
            }

            half4 frag(v2f i):COLOR
            {
                half4 c = tex2D(_MainTex , AnimationUV(i.uv.xy)) * _Color;
                return c;
            }

            ENDCG
        }
    }
}