简单的纹理扰动效果

2023/11 15 23:11
Shader "Unlit/NoiseTexture"
{
    Properties
    {
        _MainTex ("MainTexture", 2d) = "white"{}
        _WarpTex ("NoiseTexture", 2d) = "white"{}
        _WaveValue ("_WaveValue", range(0, 20)) = 10
        _WaveSpeed ("_WaveSpeed", range(0, 100)) = 10
    }
    SubShader 
    {
        Pass 
        {
            Name "FORWARD"
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            #include "UnityCG.cginc"
            sampler2D _MainTex;
            sampler2D _WarpTex;    
            float4 _WarpTex_ST;
            half _WaveValue;
            half _WaveSpeed;
            struct VertexInput 
            {
                float4 vertex : POSITION;
                float2 uv : TEXCOORD0;
            };

            struct VertexOutput 
            {
                float4 pos : SV_POSITION;
                float2 uv0 : TEXCOORD0;
                float2 uv1 : TEXCOORD1;
            };

            VertexOutput vert (VertexInput v) 
            {
                VertexOutput o = (VertexOutput)0;
                o.pos = UnityObjectToClipPos( v.vertex);
                o.uv0 = v.uv;
                o.uv1 = TRANSFORM_TEX(v.uv, _WarpTex);
                o.uv1.x = o.uv1.x + frac(_Time.x * _WaveSpeed / 100);
                o.uv1.y = o.uv1.y + frac(_Time.y * _WaveSpeed / 100);
                return o;
            }

            half4 frag(VertexOutput i) : COLOR 
            {
                float3 offset = tex2D(_WarpTex, i.uv1).rgb / _WaveValue;
                return tex2D(_MainTex, i.uv0 + offset.xy);
            }
            ENDCG
        }
    }
}