libgdx has a new texture packer! The old one was slow and the code was absolutely horrible. Here’s a run down of the major differences:
- The new code is very nice and maintainable. Check it out.
- Much better packing and much faster. The packing algorithm is the maximal rectangles (MAXRECTS) algorithm by Jukka Jylänki here. He has done some great research! His work was the basis for the new packer, with lots of additions. Packing into a fixed size bin is pretty straightforward, but packing into the smallest size bin possible is quite a pain, especially if you want it to complete in a timely manner. The new libgdx packer uses a brute force binary search and supports packing into multiple bins.
- All existing settings are supported, except incremental. I may get around to that at some point. Some settings have been renamed for more flexibility, eg padding is now paddingX and paddingY. There is one new setting “fast”, which doesn’t pack quite as well (though it is still very good) but executes very fast. This can be useful when packing at the start of every run of your app.
- The underscore file name conventions are gone. File names can still have a numeric index, it is simply any numbers at end of the file name, before the file extension. To better control packing in various ways, a “pack.json” file can be placed in each input directory. This contains the JSON for the Settings object. Each directory inherits all settings from the parent directory and any property set here will override those. This also means it is easier to use, as you don’t have to write any Java code. Just wite your “pack.json” files and point the gdx-tools.jar at it. I will write proper documentation for it tomorrow and update this blog post.
- Files with “.9” before the file extension are considered a ninepatch. The pixels around the edge are read and stripped off before packing the image. The split information is stored in the pack file, enabling TextureAtlas to provide instances of NinePatch. The splits in the TextureAtlas file format are optional, so existing pack files are still compatible.
The new class is called TexturePacker2 and the old TexturePacker class is deprecated. It is committed in the “scene2d-new” branch, which will probably be merged in the next few days. These changes, especially the ninepatch support, will allow for the cleanup of the Skin class. It is currently a weird fork of TextureAtlas, storing both an atlas and styles. Probably it will be refactored to store only styles and TextureAtlas can be used for scene2d.ui.
Edit: The TexturePacker documentation is now up to date.