美文网首页
【Unity Shader入门精要学习】让画面动起来(一)

【Unity Shader入门精要学习】让画面动起来(一)

作者: 小王子称号发放NPC | 来源:发表于2019-10-29 10:15 被阅读0次

纹理动画

序列帧动画

Shader "Unlit/FrameAnimator"
{
    Properties
    {
        _Color("Color",Color)=(1,1,1,1)
        _MainTex ("Animator Texture", 2D) = "white" {}
        _HorizontalAmount("Horizon Amount",Float)=8
        _VerticalAmount("Vertical Amount",Float)=8
        _Speed("Playe Speed",Range(1,100))=30
    }
    SubShader
    {
        Tags { "RenderType"="Transparent" "Queue"="Transparent" "IgnoreProjector"="True"}

        Pass
        {
           // Tags { "LightMode"="ForwardBase" }

            ZWrite Off
            Blend SrcAlpha OneMinusSrcAlpha

            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag

            #include "UnityCG.cginc"

            sampler2D _MainTex;
            float4 _MainTex_ST;
            fixed4 _Color;
            float _HorizontalAmount;
            float _VerticalAmount;
            float _Speed;

            struct a2v
            {
                float4 vertex : POSITION;
                float2 uv : TEXCOORD0;
            };

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


            v2f vert (a2v v)
            {
                v2f o;
                o.pos = UnityObjectToClipPos(v.vertex);
                o.uv = TRANSFORM_TEX(v.uv, _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;

                /* 这样就好理解了,映射到(0,1)范围内
                float x = i.uv.x * (1/_HorizontalAmount);
                float y = i.uv.y * (1/_VerticalAmount);
                x = x + column * (1/_HorizontalAmount);
                y = y - row * (1/_VerticalAmount);
                */

                half2 uv = float2(i.uv.x/_HorizontalAmount,i.uv.y/_VerticalAmount);
               
                //竖直方向上Unity中是由下到上增加,而播放顺序是由上到下播放,是反过来的
                uv.x += column / _HorizontalAmount;
                uv.y -= row / _VerticalAmount;
                

                fixed4 col = tex2D(_MainTex, uv);
                col.rgb *= _Color;
                return col;
            }
            ENDCG
        }
    }
}
序列帧

滚动

Shader "Unlit/TextureAnimator"
{
    Properties
    {
        _BackGroundTexture ("BackGround Texture", 2D) = "white" {}
        _DetailTexture("Detail Texture",2D)="white"{}
        _ScrollX("BackGround Texture Scroll Speed",float)=1
        _ScrollX2("Detail Texture Scroll Speed",float)=1
        _Multiplier("Layer Multiplier", float) = 1
    }
    SubShader
    {
        Tags { "RenderType"="Opaque" }

        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag

            #include "UnityCG.cginc"

            sampler2D _BackGroundTexture;
            float4 _BackGroundTexture_ST;
            sampler2D _DetailTexture;
            float4 _DetailTexture_ST;
            float _ScrollX;
            float _ScrollX2;
            float _Multiplier;

            struct a2v
            {
                float4 vertex : POSITION;
                float2 uv : TEXCOORD0;
            };

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

            v2f vert (a2v v)
            {
                v2f o;
                o.pos = UnityObjectToClipPos(v.vertex);
                o.uv.xy = TRANSFORM_TEX(v.uv, _BackGroundTexture) + frac(float2(_ScrollX,0)*_Time.y);
                o.uv.zw = TRANSFORM_TEX(v.uv,_DetailTexture) + frac(float2(_ScrollX2,0)*_Time.y);
                return o;
            }

            fixed4 frag (v2f i) : SV_Target
            {
                // sample the texture
                fixed4 colBack = tex2D(_BackGroundTexture, i.uv.xy);
                fixed4 colDetail = tex2D(_DetailTexture,i.uv.zw);
                
                fixed4 color = lerp(colBack,colDetail,colDetail.a);
                color.rgb *= _Multiplier;

                return color;
            }
            ENDCG
        }
    }
}

相关文章

网友评论

      本文标题:【Unity Shader入门精要学习】让画面动起来(一)

      本文链接:https://www.haomeiwen.com/subject/ijklvctx.html