3

I would like to scroll a tile which is part of a texture atlas like it can be done with a single quad and texture wrap mode set to repeat.

Can this be done? I hope it's clear what I would like to achieve.

Here is my shader code which I use to sample tiles from my texture atlas, but I can't figure out how to do the wrapping so that one tile repeats itself over time.

uniform float time
void main() {
  float texId = textureId/32.0;
  vec2 tileOffset = vec2(fract(texId)*32.0,floor(texId));
  vec2 offset = invTileCount * tileOffset;
  vec2 texCord = fract(vUv) * invAtlasSize + offset;
  vec4 texelColor = texture2D( uTex, texCord );
}

1 Answers1

2

Shouldn´t this already do it? Assuming vUv to cover the whole 0-1 range for each tile. The fract() will make it wrap and then it is scaled and moved to actually fit your tile.

uniform float time
void main()
{
  float texId = textureId/32.0;
  vec2 tileOffset = vec2(fract(texId)*32.0,floor(texId));
  vec2 offset = invTileCount * tileOffset;
  vec2 texCord = fract(vUv+time) * invAtlasSize + offset;
  vec4 texelColor = texture2D( uTex, texCord );
}

The problematic part will be to handle the texture interpolation and mipmapping correctly if you need it. textureGrad() might be helpful, or you just implement it completely yourself using texelFetch().

Slin
  • 306
  • 1
  • 3