TexturePacker

    TexturePacker uses multiple packing algorithms but the most important is based on . It also uses brute force, packing with numerous heuristics at various sizes and then choosing the most efficient result.

    If you prefer to pack your textures using a GUI, you can use Texture Packer GUI. If you are using Scene2d Skins, you probably already use and can use its user-friendly interface to add your textures to the Skin’s atlas.

    From source

    The TexturePacker class is in the gdx-tools project. It can be run from source via Eclipse:

    If you use gradle and the TexturePacker class is not found, add gdx-tools to your file.

    You can also run texturePacker as a gradle task if you make the following updates to your gradle files. First, you will need to update your ‘main’ build.gradle:

    1. buildscript {
    2. dependencies {
    3. // ... other dependencies trimmed ...
    4. classpath "com.badlogicgames.gdx:gdx-tools:$gdxVersion"
    5. }
    6. }

    If you want to use specific version, just replace the $gdxVersion variable with the version of your choice

    In this way, running ./gradlew texturePacker desktop:run will perform the texture packing before the desktop:run task is started. And if the textures have not changed, then all one has to do is omit the texturePacker argument.

    TexturePacker can also be run from the standalone nightly:

    1. java -cp runnable-texturepacker.jar com.badlogic.gdx.tools.texturepacker.TexturePacker inputDir [outputDir] [packFileName]
    2. // WINDOWS
    3. java -cp runnable-texturepacker.jar com.badlogic.gdx.tools.texturepacker.TexturePacker inputDir [outputDir] [packFileName]

    Note that TexturePacker runs significantly faster with Java 1.7+, especially when packing hundreds of input images.

    Directory structure

    TexturePacker can pack all images for an application in one shot. Given a directory, it recursively scans for image files. For each directory of images TexturePacker encounters, it packs the images on to a larger texture, called a page. If the images in a directory don’t fit on the max size of a single page, multiple pages will be used.

    Subdirectories are also useful to group images with related texture settings. Settings like runtime memory format (RGBA, RGB, etc) and filtering (nearest, linear, etc) are per texture. Images that need different per texture settings need to go on separate pages, so should be placed in separate subdirectories.

    To use subdirectories for organization without TexturePacker outputting a set of pages for each subdirectory, see the combineSubdirectories setting.

    To avoid subdirectory paths being used in image names in the atlas file, see the flattenPaths setting.

    Configuration

    Each directory may contain a “pack.json” file, which is a JSON representation of the TexturePacker.Settings class. Each subdirectory inherits all the settings from its parent directory. Any settings set in the subdirectory override those set in the parent directory.

    Below is a JSON example with every available setting and the default value for each. All settings do not need to be specified, any or all may be omitted. If a setting is not specified for a directory or any parent directory, the default value is used.

    Note that this is libgdx’s “minimal” JSON format, so double quotes are optional in most cases.

    Texture filter options

    Texture packer use the filters specified in the Texture.TextureFilter enum. The options for filterMin and filterMag are as following:
    Nearest: no filtering, no mipmaps
    Linear: filtering, no mipmaps
    MipMap & MipMapLinearLinear: filtering, smooth transition between mipmaps
    MipMapNearestNearest: no filtering, sharp switching between mipmaps
    MipMapLinearNearest: filtering, sharp switching between mipmaps
    MipMapNearestLinear: no filtering, smooth transition between mipmaps

    NinePatches

    If an image file name ends with “.9” just before the file extension, it is considered a ninepatch. See ninepatches. The image must have a 1px transparent border. The upper and left edge may optionally have one contiguous line of black pixels which denote the split information, ie what part of the ninepatch will stretch. The bottom and right edge may optionally have one contiguous line of black pixels which denote the padding information, ie how content on top of the NinePatch should be inset. When this image is packed, the 1px border is removed and the split and padding information stored in the pack file. TextureAtlas allows an instance of NinePatch to created for the region using the split information.

    If an image file name ends with underscore and then a number (eg animation_23.png), the number is considered the “index” and is stored separately. The image name is stored without the underscore and index. TextureAtlas allows a list of all images with the same name to be retrieved, ordered by index. This makes it easy to pack animations without losing the order of the frames.

    Packing

    The TexturePacker class is in gdx-tools.jar, which is in the extensions directory of the nightlies/releases zip files. You only need TexturePacker as a tool to process your image files for your application, you don’t need it as a dependency to run your application. To run the packer you need both gdx.jar and gdx-tools.jar. Note: gdx.jar must be in the same directory as gdx-tools.jar, for it to run without exceptions

    1. //*NIX (OS X/Linux)
    2. java -cp gdx.jar:gdx-tools.jar com.badlogic.gdx.tools.texturepacker.TexturePacker inputDir outputDir packFileName
    3. //WINDOWS
    4. java -cp gdx.jar;gdx-tools.jar com.badlogic.gdx.tools.texturepacker.TexturePacker inputDir outputDir packFileName

    inputDir is the root directory containing the images. outputDir is the output directory where the packed images will be placed. packFileName is the name of the pack file and the prefix used for the output packed image files.

    If outputDir is omitted, files will be placed in a new directory that is a sibling to inputDir with the suffix “-packed”. If packFileName is omitted, “pack” is used.

    While texture packing is intended to be a fully automated process, there has also been a nice UI contributed by Obli (though slightly out of date): TexturePacker GUI (check out ). There is also a commercial product at texturepacker.com which is completely unrelated to libgdx’s texture packer and has a UI, many features and nice documentation.

    Automatic packing

    During development it can be convenient to have the desktop application run TexturePacker before starting the game:

    Each time the game is run, all the images are packed. This can be especially convenient when giving a build to an artist, who can then try out new images without even knowing the game is using packed images. If many images are packed, the fast setting can be useful to avoid waiting.

    Note: When loading files from the classpath, Eclipse usually will not reflect changes to files that are updated externally. The project with the changed files must be manually refreshed in Eclipse. During development files can be loaded through the filesystem instead, where this is not an issue.

    TextureAtlas

    The TexturePacker output is a directory of page images and a text file that describes all the images packed on the pages. This shows how to use the images in an application:

    1. TextureAtlas atlas;
    2. atlas = new TextureAtlas(Gdx.files.internal("packedimages/pack.atlas"));
    3. AtlasRegion region = atlas.findRegion("imagename");
    4. Sprite sprite = atlas.createSprite("otherimagename");
    5. NinePatch patch = atlas.createPatch("patchimagename");

    TextureAtlas reads the pack file and loads all the page images. TextureAtlas.AtlasRegions can be retrieved, which are TextureRegions that provides extra information about the packed image, such as the frame index or any whitespace that was stripped. Sprites and NinePatches can also be created. If whitespace was stripped, the created Sprite will actually be a TextureAtlas.AtlasSprite, which allows the sprite to be used (mostly) as if whitespace was never stripped.

    Note that findRegion is not very fast, so the value returned should be stored rather than calling this method each frame. Also note that createSprite and createNinePatch allocate a new instance.

    TextureAtlas holds on to all the page textures, disposing the TextureAtlas will dispose all the page textures.