Shifting a sprite's texture

Hi there,
I have created a simple shader to shift the texture on a sprite.

float4 frag(Fragment IN) : COLOR
             {
                IN.uv_MainTex.x += _ScrollX;
                IN.uv_MainTex.y += _ScrollY;
                half4 c = tex2D (_MainTex, IN.uv_MainTex);  
                return c;
             }

The result can be seen below.
The texture is shifted allright, but at the borders no wrap around occurs. Instead the pixels seem to be duplicated.

Is there a way to fix this (i.e. make the texture repeat)?

2309569--155602--Shader.jpg

Have you tried setting the Wrap Mode to Repeat on the texture itself?
2309960--155615--wrapmode.jpg

The sprite textures do not have this setting :frowning:

I almost solved the problem by using the following shader code:

float4 frag(Fragment IN) : COLOR
             {
                 IN.texcoord.x += _ScrollX;
                IN.texcoord.y += _ScrollY;
            
                 if(IN.texcoord.x >= 1.0f)
                     IN.texcoord.x -= 1.0f;
                 if(IN.texcoord.x < 0.0f)
                     IN.texcoord.x += 1.0f;
            
                 if(IN.texcoord.y >= 1.0f)
                     IN.texcoord.y -= 1.0f;
                 if(IN.texcoord.y < 0.0f)
                     IN.texcoord.y += 1.0f;
                    
                half4 c = tex2D (_MainTex, IN.texcoord);  
                return c;
             }

Only one problem remains: There is a small but ugly seam where the wrap-around occurs (see screenshot).
I would be very thankful for any advice on how to get rid of this…

2310104--155633--Seam.jpg

try this instead:

float mod (float x, float y){
    return x - y * floor(x/y);
}

float4 frag(Fragment IN) : COLOR
{
    IN.texcoord.x = mod(IN.texcoord.x + _ScrollX, 1.0);
    IN.texcoord.y = mod(IN.texcoord.y + _ScrollY, 1.0);
            
    half4 c = tex2D (_MainTex, IN.texcoord);
    return c;
}

Definitely more elegant :slight_smile:
But your code seems to do the same thing as mine (the seam still exists).

This is an artifact of mipmapping. If you can turn off mipmaps this should go away.

Yep, that solved it.
Thanks a bunch :slight_smile: