Unity ShaderLab: 2D/3D Wave Flag Shader(飘动旗帜)的实现

个人实现的第一个在实际项目中使用的shader。

  • 2D Wave Flag Shader
    效果图:
    Unity ShaderLab: 2D/3D Wave Flag Shader(飘动旗帜)的实现_第1张图片
Shader "Custom/WaveFlag2D"
{

Properties
{
    _Color ("Main Color", Color) = (1,1,1,1)
    _MainTex ("Texture", 2D) = "white" { }
    _WaveX ("Wave x", Range(0, 1))  = 0.1
    _WaveZ ("Wave z", Range(0, 1)) = 0.1
    _WindSpeed("Wind Speed", Range(50,200)) = 100
}

SubShader
{
    Pass
    {
        CULL Off

        CGPROGRAM
        #pragma vertex vert
        #pragma fragment frag
        #include "UnityCG.cginc"
        #include "AutoLight.cginc"

        float4 _Color;
        sampler2D _MainTex;
        fixed _WaveX;
        fixed _WaveZ;
        float _WindSpeed;

        struct a2v {
            float4 vertex : POSITION;
            float4 texcoord : TEXCOORD0;
        };

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

        v2f vert (a2v v) {
            v2f o;

            float angle= _Time * _WindSpeed;

            if(v.vertex.x < 5)
            {
                v.vertex.z = v.vertex.z + sin(v.vertex.x + angle) * _WaveX ;
                v.vertex.x = v.vertex.x + sin(v.vertex.z + angle) * _WaveZ;
            }

            v.vertex.z -=  (v.vertex.x) * 0.4;

            o.pos = mul( UNITY_MATRIX_MVP, v.vertex );
            o.uv = v.texcoord;

            return o;
        }

        float4 frag (v2f i) : COLOR
        {
            half4 color = saturate(tex2D(_MainTex, i.uv)) * 0.8;
            return color;
        }

        ENDCG

    }
}
Fallback "VertexLit"
}
  • 3D Wave Flag Shader
    效果图:
    Unity ShaderLab: 2D/3D Wave Flag Shader(飘动旗帜)的实现_第2张图片

Shader "Custom/WaveFlag3D"
{

Properties
{
    _Color ("Main Color", Color) = (1,1,1,1)
    _MainTex ("Texture", 2D) = "white" { }
    _WaveY ("Wave y", Range(0, 1))  = 0.1
    _WindSpeed("Wind Speed", Range(50,200)) = 100
}

SubShader
{
    Pass
    {
        CULL Off

        CGPROGRAM
        #pragma vertex vert
        #pragma fragment frag
        #include "UnityCG.cginc"
        #include "AutoLight.cginc"

        float4 _Color;
        sampler2D _MainTex;
        fixed _WaveY;
        float _WindSpeed;

        struct a2v {
            float4 vertex : POSITION;
            float4 texcoord : TEXCOORD0;
        };

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

        v2f vert (a2v v) {
            v2f o;

            float angle= _Time * _WindSpeed;

            if(v.vertex.z < 5)
            v.vertex.y = v.vertex.y + sin(v.vertex.z + v.vertex.x + angle) * _WaveY;

            v.vertex.x = v.vertex.x  + v.vertex.z * 0.4;

            o.pos = mul( UNITY_MATRIX_MVP, v.vertex );
            o.uv = v.texcoord;
            return o;
        }

        float4 frag (v2f i) : COLOR
        {
            half4 color = saturate(tex2D(_MainTex, i.uv.xy));
            return color;
        }

        ENDCG
    }
}
Fallback "VertexLit"
}

你可能感兴趣的:(Unity,Shader)