Search code examples
c#unity-game-enginetexturesuv-mappingtexture-packing

Why Texture2D.PackTextures() creates so much empty space in atlas?


I am creating a texture atlas by script. I use 6 images to do that and the code looks like this:

    atlasTextures = new Texture2D[6] {frontTexture, topTexture, backTexture, bottomTexture, leftTexture, rightTexture };

    Texture2D atlas = new Texture2D(1024, 1024);
    Rect[] UVs = atlas.PackTextures(atlasTextures, 2, 1024);

    GetComponent<Renderer>().material.mainTexture = atlas;

The result of packing looks like this:

texture

Question is, why this code produces so much empty space? Since I will always use only 6 textures, is it possible to make atlas a bit smaller?


Solution

  • Graphics hardware likes to process textures that have power-of-two dimensions, like 128x1024 (2^7 x 2^10). They can process other texture sizes, too, but its less efficient. That's why engines usually try to import or generate textures with power-of-two sizes, even if it leaves some texture space unused. It is up to the developer to decide when to override this.