Manual: AtlasGenerator

Documentation and discussion for Draconus Texture Utils available in the Unity Asset Store.
snlehton
Crew
Crew
Posts: 16
Joined: Tue Nov 04, 2014 1:59 pm

Manual: AtlasGenerator

Postby snlehton » Tue Jan 27, 2015 11:48 am

Manual: AtlasGenerator
TextureUtils: Slicing | Pixel Operations |Normal Maps | Misc
AtlasGenerator | PSDReader |API Scripting Reference

Features

AtlasGenerator is not only used to generate atlases, but bitmap fonts and sprite sheets too. List of features include:

  • Two operation modes: MaxRects and SpriteSheets
  • Extensive parameters for controlling the size of the atlas texture: min-max size, square, power of two, edge increment
  • Alpha trimming
  • Support for multi-layered atlases
  • Export TexturePacker JSON definition
  • Bitmap fonts
  • Export BMFont definition


Basic Usage

To create new atlas, you first need to instantiate a new AtlasGenerator (optionally with some parameters):

Code: Select all

AtlasGenerator ag = new AtlasGenerator();


Next, we add all our textures to the atlas generator:

Code: Select all

foreach (var texture in textures)
    ag.AddAtlasRect(texture, texture.name); // second parameter is the id for the rectangle


Then, we need to generate the layout (notice that this can fail if the generator is unable to find proper atlas size with the given parameters)

Code: Select all

if (!ag.GenerateLayout())
{
    Debug.LogError("Generating atlas failed!");
    return;
}


Now we have generated the atlas layout (packed rectangles or sprite sheet), and to get the actual atlas texture, we need to generate it, too:

Code: Select all

Texture2D atlasTexture = ag.GenerateTexture();


The reason why the layout and texture generation are separated is that when generating multilayered atlases we only need to generate the layout once.



Multi-layered Atlases

To generate multi-layered atlases, the process is a bit more convoluted. By default when adding rectangles to the AtlasGenerator, the texture for each rectangle is stored and used when you call GenerateTexture. For any other layer/texture, you need to provide new set of textures.

Let's imagine that we have a definition of sprite, which contains all the layers we want to generate atlases for.

Code: Select all

public class Sprite
{
    public string id;
    public Texture2D[] layers;
}


Where the sprites come from is up to the implementation. It could be slicing png's, psd's or individual textures for each layer for each sprite.

Let's assume we have an array of sprites with 3 texture layers. Each sprite also has an unique id. We're going to use the layer 0 to define the rectangles for the atlas generator.

Code: Select all

Sprite[] sprites = ...;
foreach (var sprite in sprites)
    ag.AddAtlasRect(sprite.layers[0], sprite.id);

if (!ag.GenerateLayout())
    return;


To generate the texture layers we're going to use a special TextureProvider delegate to provide textures of correct layer for the AtlasGenerator:

Code: Select all

Texture2D[] atlasTextures = new Texture2D[3];
for (int i = 0; i < 3; i++)
{
    atlasTexture[i] = ag.GenerateTexture((id) =>
    {
        foreach (var sprite in sprites)
            if (sprite.name == id)
                return sprite.layers[i];
            return null;
    }, TextureUtils.colorBlackTransparent, false);
}




Generating TexturePacker JSON

After you have generated an atlas layout, you can then save it in JSON format. NOTE: you DO NOT need to generate the atlas texture, only layout!)

Code: Select all

string json = ag.GenerateTexturePackerJSON();
System.IO.File.WriteAllText("Assets/MyAtlas.txt", json);


This TexturePacker definition is then ready to be loaded into a third-party extension such as NGUI, DaikonForge, Futile and so on.



Bitmap fonts

(coming soon)

====================================================================================
TextureUtils: Slicing | Pixel Operations |Normal Maps | Misc
AtlasGenerator | PSDReader |API Scripting Reference

Return to “DTU - Draconus Texture Utils”

Who is online

Users browsing this forum: No registered users and 1 guest