Hey, my name is Alexander and I will be making a tool that can generate texture atlases. In these blog post series about the texture atlas, I will be sharing how I will be creating this awsum tool. At the end of this blog post series we will make this tool open-source and available for download!
Because we won’t be the only ones who will be using this tool, this tool will be usable in the XNA content pipeline and as a stand-alone application.
The tree enigma
In our game we have a problem. We will have to draw over 100 different kinds of overlapping trees in several depth layers. It can happen that the trees are ordered like this:
Layer 1: 1,2,3,2,1,2,3,2,1,etc.
Layer 2: 1,3,2,1,3,2,1,2,3,etc.
Because the trees overlap and the order in which they are drawn is important, the order of the draw operation can’t be changed. This can become problematic really quickly, because we have to switch sprites for every tree and that cause will a lot of small draw calls. In our example this will be one tree per draw call, because we can only use one tree sprite texture per draw call. Each draw call has overhead, an excess of computation and memory usage. Which means that a lot of these small draw calls will simply decimate our performance!
If only we had some way to batch more textures together, so we wouldn’t have to keep switching textures that trigger all these potential smaller draw calls. This is exactly what a texture atlas are for!
The texture atlas
A texture atlas is an image that is compiled out of a set of textures, often of various sizes. By putting all these textures on to one image, we can draw multiple objects from the same sheet by choosing a region to draw, without having to literally change texture. So aside from an image we need a descriptive file that can be read by the engine to choose a region of the texture atlas that corresponds to the original texture. For our tool this descriptive xml file will at least contain the position and the origin of each texture in the texture atlas.
How to build a texture atlas
A texture atlas can be made by an artist, but this is very labour intensive. Whenever you want to add, remove or change a texture, the artist would potentially have to remake the whole texture atlas and change a lot of entries in the descriptive file, read by the game engine. Fortunately this tedious process can be automated and this is exactly what we want to do for our texture atlas tool!
My next blog post will be about rectangle packing algorithms, the algorithms that actually decide where to place all the textures in the texture atlas as fast and accurate as possible.