特效序列帧动画,可指定开始和结束点

 直接贴代码:

Shader "James/FX/MeshFrame"
{
    Properties
    {
        [Enum(Add, 1, Blend, 10)] _DstBlend ("Blend Mode", Float) = 1
        _MainTex ("Main Tex", 2D) = "black" {}
        _XCell ("X Cell", Float) = 1
        _YCell ("Y Cell", Float) = 1
        _Speed ("Speed", Float) = 1
        _Start ("Start", Float) = 0
        _End   ("End", Float) = 1
        _MaskColor ("Color", Color) = (1, 1, 1, 1)
    }
    
    SubShader
    {
        LOD 200

        Tags
        {
            "Queue" = "Transparent"
            "IgnoreProjector" = "True"
            "RenderType" = "Transparent"
        }
        
        Pass
        {
            Cull Off
            Lighting Off
            ZWrite Off
            Fog { Mode Off }
            Blend SrcAlpha [_DstBlend]
            
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            #include "UnityCG.cginc"

            struct appdata_t
            {
                float4 vertex : POSITION;
                float2 texcoord : TEXCOORD0;
            };
    
            struct v2f
            {
                float4 vertex : SV_POSITION;
                half2 texcoord : TEXCOORD0;
                half2 uvMain  : TEXCOORD1;
            };

            sampler2D _MainTex;
            float4 _MainTex_ST;
            fixed _XCell, _YCell, _Speed, _Start, _End;
            fixed4 _MaskColor;
            
            float2 FrameUV(float2 uv)
            {
                float time = _Time.y * _Speed;
                time = fmod(time, _XCell * _YCell);
                time = _Start + (time * (_End - _Start + 1) / (_XCell * _YCell));

                float col = floor(time / _XCell);
                float row = floor(time - col * _XCell);
 
                float sourceX = 1.0 / _XCell;
                float sourceY = 1.0 / _YCell;
              
                uv.x *= sourceX;
                uv.y *= sourceY;

                uv.x += row * sourceX;
                uv.y = 1 - col * sourceY - uv.y;
                return uv;
            }

            v2f vert (appdata_t v)
            {
                v2f o;
                o.vertex = UnityObjectToClipPos(v.vertex);
                o.uvMain = FrameUV(v.texcoord);

                return o;
            }

            fixed4 frag (v2f IN) : SV_Target
            {
                float4 finalColor = tex2D(_MainTex, IN.uvMain);
                return finalColor * _MaskColor;
            }
            ENDCG
        }
    }
}

Start和End表示序列帧的显示范围,从0开始计数,最大值为XCell * YCell - 1。

 运行效果如下:

说明:图片来自百度图片搜索

posted @ 2018-11-06 11:22  斯芬克斯  阅读(973)  评论(0编辑  收藏  举报