Skip to main content

Tech Art Bits: Basic texture packer with PIL

One of the most common parts of any pipeline is a texture packing process of some sort. This is usually the result of the complexities of a shader, requiring artists to adhere to a rigid packing spec to maximize performance and pack masks as efficiently as possible.

In any production of any scale, 3D art production will involve some amount of texture packing. There are many ways to perform texture packing, and not all of them require some kind of pipeline solution. However, once the packing spec reaches a certain level of complexity, a measure of control over the packing process becomes desirable.

In this article, we will build a simple texture packer (which works with .TGA files and most other common file formats), that could lay the foundation of building a more complex one on your own, without requiring any expensive software licenses to do so.

Building the base class

Implementing a simple Mask Packer

Now that we have a simple base class, we can get to work on the "meat and potatoes". Fortunately, there are a few utilities we can use that come free with the PIL.Image class, which will make it very easy for us to create a simple packer that takes four input images, and just packs them respectively into the R, G, B and A channels.

Note: Take special notice of how the base class does not specify specific inputs, but leaves the input as "None". This is done for reasons we will see when we implement the second, more complex example.

And that's all we need for a simple packer! To illustrate how you could expand this, and why the base class is useful to have around, let's create one final example, a slightly more complex packer designed to use of the numpy module, to take the input images and procedurally generate a combination of them as an output channel.

The example given here is rather simple, but of course you can feel free to make this as complex as you need, according to your needs.

And that about wraps it up for our simple texture packer using numpy and PIL! there isn't much to it, but you can see how with only a little additional code you could add things like config files, validation, and more complex generated channels that the artist wouldn't have to create themselves.


Comments