본문 바로가기

유니티/쉐이더

[Unity Shader] 쉐이더로 애니메이션 재생

 

결과 화면

Properties
{
    _MainTex("Texture", 2D) = "white" {}
    _XCount("Texture Count", Range(1, 10)) = 1
    _Speed("Speed", Range(0.1, 10)) = 1
}

SubShader
{
    Tags { "RenderType" = "Transparent" "Queue" = "Transparent" }
    LOD 200
    cull off

    CGPROGRAM
    #pragma surface surf Lambert noambient alpha:fade
    #pragma target 3.0


    sampler2D _MainTex;
    float _Speed;
    int _XCount;
    int _WaitCount;

    struct Input
    {
        float2 uv_MainTex;
    };

    void surf(Input IN, inout SurfaceOutput o)
    {
        float tile = floor(fmod(_Time.y * _Speed, _XCount);
        float uvX = (IN.uv_MainTex + tile) / _XCount;

        fixed4 c = tex2D(_MainTex, float2(uvX, IN.uv_MainTex.y));
        o.Emission = c.rgb;
        o.Alpha = c.a;
    }
    ENDCG
}

마지막 프레임 기다리기

최대 타일수를 _WaitCount 만큼 늘려주고

원래 타일 수를 초과하면 출력가능한만큼 줄여준다.