Search Unity

Shifting a sprite's texture

Discussion in 'Shaders' started by jbjbjbborisb, Sep 24, 2015.

  1. jbjbjbborisb

    jbjbjbborisb

    Joined:
    Jun 8, 2014
    Posts:
    54
    Hi there,
    I have created a simple shader to shift the texture on a sprite.

    Code (CSharp):
    1. float4 frag(Fragment IN) : COLOR
    2.              {
    3.                 IN.uv_MainTex.x += _ScrollX;
    4.                 IN.uv_MainTex.y += _ScrollY;
    5.                 half4 c = tex2D (_MainTex, IN.uv_MainTex);  
    6.                 return c;
    7.              }
    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)?
     

    Attached Files:

  2. pixpusher2

    pixpusher2

    Joined:
    Oct 30, 2013
    Posts:
    121
    Have you tried setting the Wrap Mode to Repeat on the texture itself?
    wrapmode.jpg
     
  3. jbjbjbborisb

    jbjbjbborisb

    Joined:
    Jun 8, 2014
    Posts:
    54
    The sprite textures do not have this setting :-(

    I almost solved the problem by using the following shader code:
    Code (CSharp):
    1. float4 frag(Fragment IN) : COLOR
    2.              {
    3.                  IN.texcoord.x += _ScrollX;
    4.                 IN.texcoord.y += _ScrollY;
    5.            
    6.                  if(IN.texcoord.x >= 1.0f)
    7.                      IN.texcoord.x -= 1.0f;
    8.                  if(IN.texcoord.x < 0.0f)
    9.                      IN.texcoord.x += 1.0f;
    10.            
    11.                  if(IN.texcoord.y >= 1.0f)
    12.                      IN.texcoord.y -= 1.0f;
    13.                  if(IN.texcoord.y < 0.0f)
    14.                      IN.texcoord.y += 1.0f;
    15.                    
    16.                 half4 c = tex2D (_MainTex, IN.texcoord);  
    17.                 return c;
    18.              }
    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...
     

    Attached Files:

    • Seam.jpg
      Seam.jpg
      File size:
      46.5 KB
      Views:
      658
  4. karp505

    karp505

    Joined:
    Jul 24, 2014
    Posts:
    18
    try this instead:

    Code (CSharp):
    1. float mod (float x, float y){
    2.     return x - y * floor(x/y);
    3. }
    4.  
    5. float4 frag(Fragment IN) : COLOR
    6. {
    7.     IN.texcoord.x = mod(IN.texcoord.x + _ScrollX, 1.0);
    8.     IN.texcoord.y = mod(IN.texcoord.y + _ScrollY, 1.0);
    9.            
    10.     half4 c = tex2D (_MainTex, IN.texcoord);
    11.     return c;
    12. }
     
  5. jbjbjbborisb

    jbjbjbborisb

    Joined:
    Jun 8, 2014
    Posts:
    54
    Definitely more elegant :)
    But your code seems to do the same thing as mine (the seam still exists).
     
  6. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,339
    This is an artifact of mipmapping. If you can turn off mipmaps this should go away.
     
  7. jbjbjbborisb

    jbjbjbborisb

    Joined:
    Jun 8, 2014
    Posts:
    54
    Yep, that solved it.
    Thanks a bunch :)