chenxin
2020-12-25 adb0dae8a82a7eabb4e686bc0e83c8859bf6445f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
 
Shader "MyShader/ImageSequenceAnimation"
{
    Properties
    {
        _Color("Color Tint",Color) = (1,1,1,1)
        _MainTex("Image Sequence", 2D) = "white" {}
        _HorizontalAmount("Horizontal Amount",Float) = 8
        _VerticalAmount("Vertical Amount",Float) = 8
        _Speed("Speed",Range(1,150)) = 30
        _ChangeTime("ChangeTime",Float) = 0
 
    }
    SubShader
    {
        Tags{"Queue" = "Transparent" "IgnoreProjector" = "True" "RenderType" = "Transparent"}
        Pass
        {
            Tags{"LightMode" = "ForwardBase"}
 
            ZWrite Off
            Blend SrcAlpha OneMinusSrcAlpha
 
            CGPROGRAM
      // 两条非常重要的编译指令
            #pragma vertex vert// 定点着色器
            #pragma fragment frag// 片元着色器
            #include "Lighting.cginc"
 
            fixed4 _Color;
            sampler2D _MainTex;
            float4 _MainTex_ST;
            float _HorizontalAmount;
            float _VerticalAmount;
            float _Speed;
            float _ChangeTime;
 
 
            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 = UnityObjectToClipPos(v.vertex);
                o.uv = TRANSFORM_TEX(v.texcoord, _MainTex);
                return o;
            }
 
            fixed4 frag(v2f i) :SV_Target
            {
                //_Time    float4    Time (t/20, t, t*2, t*3), use to animate things inside the shaders.
                //_SinTime    float4    Sine of time: (t/8, t/4, t/2, t).
                //_CosTime    float4    Cosine of time: (t/8, t/4, t/2, t).
                //unity_DeltaTime    float4    Delta time: (dt, 1/dt, smoothDt, 1/smoothDt).
                //_ProjectionParams    float4    x is 1.0 (or –1.0 if currently rendering with a flipped projection matrix), y is the camera’s near plane, z is the camera’s far plane and w is 1/FarPlane.
                //_ScreenParams    float4    x is the current render target width in pixels, y is the current render target height in pixels, z is 1.0 + 1.0/width and w is 1.0 + 1.0/height.
                //这里的 t 得到的时间单位是——秒 
 
                float time = floor((_Time.y - _ChangeTime) * _Speed);//对输入参数向下取整。例如 floor(float(1.3)) 返回的值为 1.0;但是 floor(float(-1.3))返回的值为-2.0。
                float row = floor(time / _HorizontalAmount);//行
                float column = time - row * _HorizontalAmount;//列
                //--
                    //half2 uv = float2(i.uv.x / _HorizontalAmount, i.uv.y / _VerticalAmount);
                    //uv.x += column / _HorizontalAmount;
                    //uv.y -= row / _VerticalAmount;
                //    --
 
                //--
                half2 uv = i.uv + half2(column, -row);
                uv.x /= _HorizontalAmount;
                uv.y /= _VerticalAmount;
                //--
 
                fixed4 c = tex2D(_MainTex, uv);
                c.rgb *= _Color;
 
                return c;
            }
 
            ENDCG
        }
    }
    Fallback "Transparent/VertexLit"
}