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)?
Have you tried setting the Wrap Mode to Repeat on the texture itself?
The sprite textures do not have this setting
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…
karp505
September 24, 2015, 3:04pm
4
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
But your code seems to do the same thing as mine (the seam still exists).
bgolus
September 24, 2015, 7:28pm
6
This is an artifact of mipmapping. If you can turn off mipmaps this should go away.
Yep, that solved it.
Thanks a bunch