Unity Shader 纹理动画-序列帧动画

Unity Shader系列文章:Unity Shader目录-初级篇

Unity Shader系列文章:Unity Shader目录-中级篇

效果:
使用序列帧动画来实现爆炸效果

原图:


爆炸图片

Shader代码:

// 序列帧动画
Shader "Custom/ImageSequenceAnimation"
{
    Properties
    {
        _Color ("Color Tint", Color) = (1, 1, 1, 1)
        _MainTex ("Image Sequence", 2D) = "white" { }// 序列帧图片
        _HorizontalAmount ("Horizontal Amount", Float) = 4 // 行数
        _VerticalAmount ("Vertical Amount", Float) = 4 // 列数
        _Speed ("Speed", Range(1, 100)) = 40 // 数量
    }

    SubShader
    {
        Tags { "RenderType" = "Transparent" "IgnoreProjector" = "True" "Queue" = "Transparent" }

        pass
        {
            Tags { "LightMode" = "ForwardBase" }

            ZWrite Off
            Blend One OneMinusSrcAlpha

            CGPROGRAM

            #pragma vertex vert
            #pragma fragment frag

            #include "UnityCG.cginc"

            fixed4 _Color;
            sampler2D _MainTex;
            // _MainTex纹理的缩放和偏移系数
            float4 _MainTex_ST;
            float _HorizontalAmount;
            float _VerticalAmount;
            float _Speed;

            // 应用传递给顶点着色器的数据
            struct a2v
            {
                float4 vertex: POSITION; // 语义:模型顶点坐标
                float2 texcoord: TEXCOORD0; // 语义:模型第一组纹理坐标
            };

            // 顶点着色器传递给片元着色器的数据
            struct v2f
            {
                float4 pos: SV_POSITION; // 语义:裁剪空间顶点坐标
                float2 uv: TEXCOORD0;
            };

            // 顶点着色器
            v2f vert(a2v v)
            {
                v2f o;

                // 将顶点坐标从模型空间变换到裁剪空间
                // 等价于o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
                o.pos = UnityObjectToClipPos(v.vertex);

                // 计算uv
                o.uv = TRANSFORM_TEX(v.texcoord, _MainTex);

                return o;
            }

            // 片元着色器
            fixed4 frag(v2f i): SV_TARGET
            {
                float time = floor(_Time.y * _Speed);
                float row = floor(time / _HorizontalAmount);
                float column = time - row * _HorizontalAmount;

                half2 uv = i.uv + half2(column, -row);
                uv.x /= _HorizontalAmount;
                uv.y /= _VerticalAmount;
                
                fixed4 color = tex2D(_MainTex, uv);
                color.rgb *= _Color;

                return color;
            }

            ENDCG

        }
    }
    FallBack "Transparent/VertexLit"
}

你可能感兴趣的:(Unity Shader 纹理动画-序列帧动画)