Search code examples
javalibgdxtextures

Libgdx : Detect empty TextureRegion


I've been building a little game in Libgdx for a couple of days now and I wanted to implement some kind of dynamic sprite-analyzer.

I have a png-file that I split into TextureRegions and build a Animation out of. So I split up the Image in 64x64 Regions and iterate over them. Now I want to ignore every region that is completely transparent. How do I determine if one of these Regions actually contains any Pixels (not everything transparent). I'm just searching for something like textureRegion.isEmpty() or textureRegion.getTexture().isEmpty() Can someone help me out? :)


Solution

  • I would strongly advice against this. Since you can easily know which area's are completely transparent you can hard-code this. Or even, better use the transparent regions for other things that need yo drawn.

    If you do want to implement this you need to check the area pixel by pixel and which for obvious resons is not very efficient. If for some reasons i'm unaware about you need this then LibGDX has PixMap to help you out here to check the texture data of an image.

    Another more efficient way would be to add a known color on the first (or other known) pixel to check if it belongs to the animation. This way you do not need to iterate over the whole texture.