Texture Bleeding Fix

Any community contributions to libgdx go here! Some may get included in the core API when permission is granted.

Texture Bleeding Fix

Postby awilki01 » Thu Dec 11, 2014 1:55 am

I have been facing an issue with the OrthogonalTiledMapRenderer class rendering a tiled map that exhibits vertical texture bleeding lines on some test machines. My partner I'm working with in making a game asked the question in stack overflow here: http://stackoverflow.com/questions/27391911/white-vertical-lines-and-jittery-horizontal-lines-in-tile-map-movement

The response was gold. I copied the OrthogonalTiledMapRenderer into my own class and added the following method:

Code: Select all
public static void fixBleeding(TextureRegion region) {
    float fix = 0.01f;

    float x = region.getRegionX();
    float y = region.getRegionY();
    float width = region.getRegionWidth();
    float height = region.getRegionHeight();
    float invTexWidth = 1f / region.getTexture().getWidth();
    float invTexHeight = 1f / region.getTexture().getHeight();
    region.setRegion((x + fix) * invTexWidth, (y + fix) * invTexHeight, (x + width - fix) * invTexWidth, (y + height - fix) * invTexHeight);
}


I also added the following in the renderTileLayer method after the "TextureRegion region = tile.getTextureRegion();" line:

Code: Select all
fixBleeding(region);


The texture bleeding issue resolved on our test machines.

I would love to contribute this to the GDX project, but to be entirely honest, I don't know how. Can you all point me in the right direction? I've forked the project, but I really don't know what I'm doing when it comes to contributing to an open source project.
awilki01
 
Posts: 8
Joined: Tue Oct 14, 2014 2:59 am

Re: Texture Bleeding Fix

Postby BurningHand » Thu Dec 11, 2014 3:32 am

Unfortunately, it doesn't always work, I've implemented this (not exactly, but similar) in there before and found it only works sometimes. As for your code, you probably should not do that in renderTileLayer but instead do it once after loading.
IRC: nexsoftware / mobidevelop; GitHub: MobiDevelop;
BurningHand
 
Posts: 2812
Joined: Mon Oct 25, 2010 4:35 am

Re: Texture Bleeding Fix

Postby Magnesus » Thu Dec 11, 2014 7:56 am

Aren't you taking pixels of sorrounding regions though which might contain weird random stuff or just empty space? My solution to get rid of such lines (although I don't use TiledMap) is to multiply the width&height of the sprite/actor by about 5% making all tiles a bit larger so they overlap a bit.
Magnesus
 
Posts: 1663
Joined: Sun Sep 25, 2011 3:50 pm

Re: Texture Bleeding Fix

Postby vedioboy » Sat Jun 20, 2015 3:28 am

Omg thank you so much for this awilki01. It works great, just teh solution I was looking for and it doesn't require any special padding. For people interested in a one-liner, I made one here:
Code: Select all
 region.setRegion((region.getRegionX() + 0.01f) * 1f / region.getTexture().getWidth(), (region.getRegionY() + 0.01f) * 1f / region.getTexture().getHeight(), (region.getRegionX() + region.getRegionWidth() - 0.01f) * 1f / region.getTexture().getWidth(), (region.getRegionY() + region.getRegionHeight() - 0.01f) * 1f / region.getTexture().getHeight());
vedioboy
 
Posts: 8
Joined: Mon Mar 23, 2015 10:12 pm

Re: Texture Bleeding Fix

Postby j3nda » Sat Jan 12, 2019 3:30 pm

it exists more elegant way how to fix it, instead of call fixBleeding() on each renderTileLayer()!

Code: Select all
public class GulaOrthogonalTiledMapRenderer
extends OrthogonalTiledMapRenderer
{

   public GulaOrthogonalTiledMapRenderer(TiledMap map)
   {
      super(fixTilesPixelBleeding(map));
   }

   private static TiledMap fixTilesPixelBleeding(TiledMap tiledMap)
   {
      for(MapLayer layer : tiledMap.getLayers())
      {
         if (!layer.isVisible())
         {
            continue;
         }
         if (layer instanceof TiledMapTileLayer)
         {
            fixTilePixelBleeding((TiledMapTileLayer) layer);
         }
      }
      return tiledMap;
   }

   private static void fixTilePixelBleeding(TiledMapTileLayer layer)
   {
      for(int x = 0; x < layer.getWidth(); x++)
      {
         for(int y = 0; y < layer.getHeight(); y++)
         {
            Cell cell = layer.getCell(x, y);
            if (cell != null)
            {
               fixPixelBleeding(cell.getTile().getTextureRegion());
            }
         }
      }
   }

   private static void fixPixelBleeding(TextureRegion region)
   {
      float fix = 0.01f;

      float x = region.getRegionX();
      float y = region.getRegionY();
      float width = region.getRegionWidth();
      float height = region.getRegionHeight();
      float invTexWidth = 1f / region.getTexture().getWidth();
      float invTexHeight = 1f / region.getTexture().getHeight();

      region.setRegion(
         (x + fix) * invTexWidth,
         (y + fix) * invTexHeight,
         (x + width - fix) * invTexWidth,
         (y + height - fix) * invTexHeight
      );
   }
}
j3nda
 
Posts: 2
Joined: Tue Mar 29, 2016 4:29 pm


Return to Libgdx Contributions

Who is online

Users browsing this forum: No registered users and 1 guest