Textures sampled show flickering gaps in the middle

vertex Shader:

varying vec2 vUv;

void main() {
   vUv = uv;
}

fragment Shader:

varying vec2 vUv;
uniform sampler2D spriteTexture;
uniform vec4 iconInfo; //In the spriteMap, use iconInfo's x and y components for the offset position, and z and w components for the width and height of the icon, respectively, to locate an icon

void main() {
    float xStart = iconInfo.x;
    float yStart = iconInfo.y;
    float width = iconInfo.z;
    float height = iconInfo.w;

    vec2 newUv = vec2(
       xStart + mod((width * vUv.x) * 10.0, width),
       yStart + mod((height * vUv.y) * 10.0, height)
   );

   gl_FragColor = texture2D(spriteTexture, newUv);
}

I want to achieve a texture repeat effect using the code above, but I’m encountering an issue where there are noticeable gaps and flickering between the tiles. The value of newUv ranges from 0 to 1, and I’m not sure why this effect occurs.

Could someone help explain why this phenomenon is occurring? Any assistance would be greatly appreciated.

This is the texture image of the spriteTexture. The iconInfo I am using points to the sprite located in the top-left corner of this image.

It could be texture bleeding.

Does it go away when:

  • the texture’s .minFilter is set to THREE.NearestFilter?
  • there is a safe area around the tile?
  • when sizes of tiles are powers of 2?
  • when mipmapping is disabled?

the texture’s .minFilter is set to THREE.NearestFilter
In this case, the problem will disappear, but I still expect to use LinearFilter.

Then try a safe area around the tile.

when mipmapping is disabled
This also works, although the edge effects are not great, it does prevent the issue of seam flickering. Why does disabling mipmap affect this result? I don’t have a deep understanding of texture sampling methods, so I would appreciate some explanation. Thank you!

For an in-depth explanation you may search the web. There are quite many pages about texture sampling, bleeding and how to counter-fight. It is a matter of sacrifice and balance - to gain something, you need to sacrifice something else (e.g. smoothness-vs-performance, or performance-vs-memory, or memory-vs-precision, or precision-vs-complexity, etc.)

In a nutshell, to smooth textures, the sampling takes the color of several texture pixels and calculates some intermediate color. At the boundary of a tile, the samples takes pixels from the tile and also pixels from the neighbouring tiles. This results in a wrong mixture of colors, because pixels of one tile affect the boundary pixels of another tile. When mipmaps are used, pixels are also taken from neighbouring levels.

1 Like

Add full copy of tiles or strips and chose centerd tile


image
Or try this

2 Likes