0

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?

Łukasz Motyczka
  • 1,169
  • 2
  • 13
  • 35
  • 1
    I guess your images are about 256x256 and you create a 1024x1024 atlas so no wonder. Make it a multiple of your size. 768x768 would save you some already. Don't know if 512x768 will work too. – Gunnar B. Mar 26 '16 at 11:53
  • Every of these textures are 167x167. I changed dimensions to Texture2D(512, 512) and to PackTextures(atlasTextures, 2, 256); And it is smaller. Do you know what the thirdparameter in PackTextures() do? – Łukasz Motyczka Mar 26 '16 at 12:15
  • That is the maximum size the atlas can have. – Gunnar B. Mar 26 '16 at 12:56

1 Answers1

1

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.

Thomas Hilbert
  • 3,559
  • 2
  • 13
  • 33
  • ok so one more question, how does it work if my textures are 167x167 and they can be packed in the atlas with dimensions 256x256 (`Debug.Log(atlas.width.ToString() + " " + atlas.height.ToString());` gives my 256 256? – Łukasz Motyczka Mar 26 '16 at 14:26
  • Well if you mean by that, that all 6 of your textures are contained in that 256x256 atlas, then obviously they must have been downsized. If only one is contained in that atlas, then it's in there with its original size of 167x167, leaving some space of the atlas unused. – Thomas Hilbert Mar 26 '16 at 14:32
  • All fit in this atlas. Probably some downsizing is going on. Ok thanks for help guys :) – Łukasz Motyczka Mar 26 '16 at 14:33