• Advertisement
Sign in to follow this  

Texture bleeding/tearing with UV texture atlas mapping

This topic is 897 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

The following code maps the texture from a texture atlas to a UV. The problem is I'm having texture bleeding issues due to rounding (what I suspect anyway).
 
Does anyone know how to overcome this problem and have any suggestions? Here is the code I have, all images are point filtered and **no mipmapping** is used.
 
 
        
        for (int i = 0; i < mesh.uv.Length; i++)
        {
            UVs.Add(GetUVTextureFromAtlas(mesh.uv[i].x, mesh.uv[i].y, voxel, 0));
        }

        [...]

        Vector2 GetUVTextureFromAtlas(float x, float y, ushort voxel, Facing side)
        {
            Rect rect = GetVoxelTextureRect(voxel, side);
            float xout = UVLerp(rect.x, rect.x + rect.width, x);
            float yout = UVLerp(rect.y, rect.y + rect.height, y); 
            return new Vector2(xout, yout);
        }
        float UVLerp(float from, float to, float t)
        {
            float difference = to - from;
            return from + (difference * t);
        }
        Rect GetVoxelTextureRect(ushort voxel, Facing s)
        {
            int side = Convert.ToInt32(s);
            Rect rect = new Rect(VoxelArray[voxel].TextureAtlasPos[side].x,
                VoxelArray[voxel].TextureAtlasPos[side].y, 
                VoxelArray[voxel].TextureAtlasPos[side].width, 
                VoxelArray[voxel].TextureAtlasPos[side].height);
            return rect;
        }
 
Notice the tearing along the triangle lines of the mesh:
 
[attachment=29524:texturebleeding.png] Edited by GameObject

Share this post


Link to post
Share on other sites
Advertisement

This is usually solved by making texels around uv-borders extend/bleed over said edges - enough to make sure there are never any outer 'undefined' texel values involved in a filtering operation.

 

- If you're baking this texture, modelling apps often have an option to 'expand uv-borders'.

- If your texture comes with an alpha map, one quick trick is to place a slightly blurred duplicate of the texture underneath the image. Or write a script that for any texel with a zero-alpha value duplicates the RGB value of the nearest texel with a non-zero alpha value.

Share this post


Link to post
Share on other sites
How might I add 2 px or any pixel amount of padding into this when getting the texture from atlas and applying to UV? I've updated my original post to include the code when getting atlas pos. Just a little thrown off on when to add vs subtract for padding.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement