Unity Shader 之 uv動(dòng)畫

Unity Shader 之 uv動(dòng)畫

Unity Shader 內(nèi)置時(shí)間變量
名稱 類型 描述
_Time float4 t是自該場景加載開始所經(jīng)過的時(shí)間,4個(gè)分量分別是(t/20, t, 2t, 3t)
_SinTime float4 t是時(shí)間的正弦值,(t/8, t/4, t/2, t)
_CosTime float4 t是時(shí)間的余弦值,(t/8, t/4, t/2, t)
unity_DeltaTime float4 dt是時(shí)間增量,(dt, 1/dt, smoothDt, 1/smoothDt)
序列幀動(dòng)畫
Shader "Unity Shaders Book/Chapter 11/Image Sequence Animation" {
    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, 100)) = 30
    }
    SubShader {
        Tags {"Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent"}
        
        Pass {
            
            ZWrite Off
            Blend SrcAlpha OneMinusSrcAlpha
            
            CGPROGRAM
            
            #pragma vertex vert  
            #pragma fragment frag
            
            #include "UnityCG.cginc"
            
            fixed4 _Color;
            sampler2D _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;  
                //將頂點(diǎn)坐標(biāo)轉(zhuǎn)換到裁剪空間坐標(biāo)系并且
                o.pos = mul(UNITY_MATRIX_MVP, v.vertex);  
                //o.texcoord = v.texcoord.xy *_MainTex_ST.xy+_MainTex_ST.zw 
                //將紋理坐標(biāo)映射到頂點(diǎn)上以及zw偏移
                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);    // /運(yùn)算獲取當(dāng)前行
                float column = time - row * _HorizontalAmount;  // %運(yùn)算獲取當(dāng)前列
                
                //首先把原紋理坐標(biāo)i.uv按行數(shù)和列數(shù)進(jìn)行等分,然后使用當(dāng)前的行列進(jìn)行偏移
                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"
}
滾動(dòng)的背景
Shader "Unity Shaders Book/Chapter 11/Scrolling Background" {
    Properties {
        _MainTex ("Base Layer (RGB)", 2D) = "white" {}
        _DetailTex ("2nd Layer (RGB)", 2D) = "white" {}
        _ScrollX ("Base layer Scroll Speed", Float) = 1.0
        _Scroll2X ("2nd layer Scroll Speed", Float) = 1.0
        _Multiplier ("Layer Multiplier", Float) = 1
    }
    SubShader {
        Tags { "RenderType"="Opaque" "Queue"="Geometry"}
        
        Pass { 
            
            CGPROGRAM
            
            #pragma vertex vert
            #pragma fragment frag
            
            #include "UnityCG.cginc"
            
            sampler2D _MainTex;
            sampler2D _DetailTex;
            float4 _MainTex_ST;
            float4 _DetailTex_ST;
            float _ScrollX;
            float _Scroll2X;
            float _Multiplier;
            
            struct a2v {
                float4 vertex : POSITION;
                float4 texcoord : TEXCOORD0;
            };
            
            struct v2f {
                float4 pos : SV_POSITION;
                float4 uv : TEXCOORD0;
            };
            
            v2f vert (a2v v) {
                v2f o;
                //將頂點(diǎn)坐標(biāo)從模型空間轉(zhuǎn)換到裁剪空間
                o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
                ////將紋理坐標(biāo)映射到頂點(diǎn)上以及zw偏移,并用ScrollX對x軸坐標(biāo)進(jìn)行偏移
                o.uv.xy = TRANSFORM_TEX(v.texcoord, _MainTex) + frac(float2(_ScrollX, 0.0) * _Time.y);
                o.uv.zw = TRANSFORM_TEX(v.texcoord, _DetailTex) + frac(float2(_Scroll2X, 0.0) * _Time.y);
                
                return o;
            }
            
            fixed4 frag (v2f i) : SV_Target {
                //紋理采樣
                fixed4 firstLayer = tex2D(_MainTex, i.uv.xy);
                fixed4 secondLayer = tex2D(_DetailTex, i.uv.zw);
                //紋理混合
                fixed4 c = lerp(firstLayer, secondLayer, secondLayer.a);
                c.rgb *= _Multiplier;
                
                return c;
            }
            
            ENDCG
        }
    }
    FallBack "VertexLit"
}
頂點(diǎn)動(dòng)畫
Shader "Unity Shaders Book/Chapter 11/Water" {
    Properties {
        _MainTex ("Main Tex", 2D) = "white" {}
        _Color ("Color Tint", Color) = (1, 1, 1, 1)
        _Magnitude ("Distortion Magnitude", Float) = 1
        _Frequency ("Distortion Frequency", Float) = 1
        _InvWaveLength ("Distortion Inverse Wave Length", Float) = 10
        _Speed ("Speed", Float) = 0.5
    }
    SubShader {
        // Need to disable batching because of the vertex animation
        Tags {"Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent" "DisableBatching"="True"}
        
        Pass {
            
            ZWrite Off
            Blend SrcAlpha OneMinusSrcAlpha
            Cull Off
            
            CGPROGRAM  
            #pragma vertex vert 
            #pragma fragment frag
            
            #include "UnityCG.cginc" 
            
            sampler2D _MainTex;
            float4 _MainTex_ST;
            fixed4 _Color;
            float _Magnitude;
            float _Frequency;
            float _InvWaveLength;
            float _Speed;
            
            struct a2v {
                float4 vertex : POSITION;
                float4 texcoord : TEXCOORD0;
            };
            
            struct v2f {
                float4 pos : SV_POSITION;
                float2 uv : TEXCOORD0;
            };
            
            v2f vert(a2v v) {
                v2f o;
                
                //頂點(diǎn)偏移量,只對x偏移
                float4 offset;
                offset.yzw = float3(0.0, 0.0, 0.0);
                //Frequency控制頻率f
                //InvWaveLength控制波長L
                //Magnitude控制幅度k
                offset.x = sin(_Frequency * _Time.y + v.vertex.x * _InvWaveLength + v.vertex.y * _InvWaveLength + v.vertex.z * _InvWaveLength) * _Magnitude;
                o.pos = mul(UNITY_MATRIX_MVP, v.vertex + offset);
                
                //紋理采樣
                o.uv = TRANSFORM_TEX(v.texcoord, _MainTex);
                //對v偏移,橫向
                o.uv +=  float2(0.0, _Time.y * _Speed);
                
                return o;
            }
            
            fixed4 frag(v2f i) : SV_Target {
                fixed4 c = tex2D(_MainTex, i.uv);
                c.rgb *= _Color.rgb;
                
                return c;
            } 
            
            ENDCG
        }
    }
    FallBack "Transparent/VertexLit"
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容