Optimizing LibGDX Game Assets with TexturePacker

LibGDX requires texture dimesnions to be powers of two, necessitating manual resizing of arbitrary images. This becomes inefficient when managing numerous assets, cmopounded by the need to extract sub-images using TextureRegion.

The LibGDX toolset includes TexturePacker (in com.badlogic.gdx.tools.imagepacker) to automate atlas generation. It combines multiple images into a single texture while producing a descriptor file mapping original filenames to coordinates.

Packing Process

Place source images in a dedicated directory. Execute packing programmatically:

PackingConfig cfg = new PackingConfig();
cfg.enableAliases = true;
TexturePacker.process(cfg, "input_dir", "output_dir");

This generates:

  1. A merged PNG containing all source textures
  2. A descriptor file (.pack) storing region metadata

Rename the descriptor freely, but update its internal reference if renaming the merged image.

Atlas Utilization

Copy both files to project assets. Load using TextureAtlas:

public class GameScene implements ApplicationListener {
    private Stage uiStage;
    private TextureAtlas assetBundle;

    @Override
    public void create() {
        uiStage = new Stage();
        assetBundle = new TextureAtlas(
            Gdx.files.internal("assets/game_atlas.pack")
        );

        Image character = new Image(
            assetBundle.findRegion("player_idle")
        );
        character.setScale(0.75f);
        character.setPosition(100, 200);
        uiStage.addActor(character);

        Image obstacle = new Image(
            assetBundle.findRegion("rock_formation")
        );
        obstacle.setPosition(300, 150);
        uiStage.addActor(obstacle);
    }

    @Override
    public void render() {
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
        uiStage.act(Gdx.graphics.getDeltaTime());
        uiStage.draw();
    }

    @Override
    public void dispose() {
        uiStage.dispose();
        assetBundle.dispose();
    }
}

Retrieve regions via findRegion() using original filenames. Group frequently co-loaded assets in single atlases, keeping dimensions under 1024x1024 pixels.

Tags: libgdx texturepacker asset pipeline game development java

Posted on Fri, 02 Oct 2026 16:16:00 +0000 by MrJW