TexturePacker v2

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.

-Nate

14 thoughts on “TexturePacker v2

  1. First of all, nice to hear you are improving texture packer tool.

    However, I hope the new texture packer is fast enough to don’t need incremental setting. We are using incremental a lot since we have a lot of assets and it is the only way to speed up adding a new asset or modifying an old one, and we use the incrementalFilePath and share the crc file in our git to avoid making another dev to regenerate all the assets.

    We also use a lot the underscore + number file convention for animations, hope it keeps assuming the region will be named all text before the underscore since that is how it is identified inside the game.

    I know using the new texture packer is optional, just wanted to share my concerns.

  2. Incremental may still be added in the future. I’m just tired at the moment and have other things I want to get to. :) I would like to hear how the speed of the new packer compares to the old one, as it sounds like you have a lot of assets. You might try with and without fast=true. Also it would be nice to know how much more space you save.

    The new packer considers any numbers at the end of the filename as the index, no underscore is needed. Regions can now be looked up in TextureAtlas by name with or without the numbers. This won’t affect you if you continue using the old packer. If you switch to the new packer and you have an image named “anim_123.png” then you can look it up by “anim_” or “anim_123″, which is slightly different behavior. Do you think this is acceptable?

  3. first of all nice job

    second, I don’t think removing the ability to have flags in the filename is a good idea. I think you may need to have some images that need to be in the same texture packed page to prevent swapping but with some custom configuration compared to the rest of the images in the same dir.
    Of course this would not apply to some properties like image format, output format, etc

  4. Spoke with arielsan, decided to leave the underscore for indexes.

    @Ruben, I think the underscore flags beyond indexes are a bit hacky. I’d prefer if that configuration was done in the settings object/JSON, if it is needed. However, I have a feeling that per file settings won’t be needed. Ninepatches are never rotated or ws stripped.

  5. Maxrects is an awesome algorithm, thanks for adding it, that’s a really nice job.

    Should I update the gui with that? I don’t know what are the plans about the latter, ie. if you want it to be included in libgdx official tools or not. Its main use is to let people preview their settings in real-time and play with different levels of zoom. In last update [1], I added support for multiple packs at once, so users can choose to manage all their packs from the gui instead of from the desktop project. It may even cope with the lack of incremental support by letting people quickly pack the atlas they want. I put some real love in polishing it to create a libgdx dedicated toolsuite :p

    [1] http://www.aurelienribon.com/blog/2012/06/texturepacker-gui-support-for-multiple-packs/

  6. The GUI looks great Obli! Packing can be completely automated, but if people want to play with settings, seeing the results realtime is nice. It should be easy to update the GUI to the new packer, it is very similar to the old one. It looks like you might start a new process to pack? You could do packing in a new thread in the same VM, just call TexturePacker2.process.

  7. You’re right, adding support for it should be quite easy. I think I’ll add an option to select the desired packer, for compatibility with existing projects. The choice will enable/disable the incremental option.

  8. Awesome work guys, really impressed by the refActoring!
    One question: are you going to update the tiled-preprocessor extension too?

  9. @Obli, output from the new packer should be compatible with old projects. The format is the same, just adds one optional section for ninepatches. True you would lose incremental. I will add that eventually.

    @Manuel, the TileD stuff was contributed, we haven’t found the time to update it. I’m afraid it is on the community to support it until we can get to it.

  10. Guys, I guess the Settings.rotation flag is still kinda bugged.
    Here’s what I got http://i.imgur.com/iVfWz.png

    Code I used:
    Settings settings = new Settings();
    settings.paddingX = 2;
    settings.paddingY = 2;
    settings.maxWidth = 1024;
    settings.maxHeight = 1024;
    settings.filterMin = TextureFilter.Linear;
    settings.filterMag = TextureFilter.Linear;
    settings.rotation = true;
    TexturePacker2.process(settings, INPUT_DIR, OUTPUT_TEST_DIR, PACK_FILE_NAME);

  11. Sako, can you please file an issue including a zip of the images from your screenshot? I need to be able to see the packing freak out like that so I can fix.

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>