Search code examples
unity-game-engineglslshaderhlslcg

Shader and Texture Scrolling depending on Direction


I am trying to scroll a texture using its uv in Unity but I don't get the result I need.

The aim is to have two components, the speed and the direction. I would like to define the direction in normalized values and the speed should influence the velocity of the scrolling according to the direction.

If I change the speed at runtime, I don't want to have some hiccups but maybe this should not be handled but the GPU.

How can I do that in a better way, maybe using matrix ?

Here is an example but the result is not as good as expected.

uv.xy = uv.xy + frac(_Time.y * float2(_Direction.x, _Direction.y));

Solution

  • I hope I correctly understand your question. What about defining three variables in properties:

    fixed _ScrollXSpeed;
    fixed _ScrollYSpeed;
    sampler2D _Texture
    

    then in surf() you sholud do this:

    void surf(Input IN, inout SurfaceOutput o)
    {
        fixed2 scrolledUV = IN._Texture;
    
        fixed xScrollValue = _ScrollXSpeed * _Time;
        fixed yScrollValue = _ScrollYSpeed * _Time;
    
        //Apply offset
        scrolledUV += fixed2(xScrollValue, yScrollValue);
    
        half4 c = tex2D(_Texture, scrolledUV);
        o.Albedo = c.rgb;
        o.Alpha = c.a;
    }