UV Mapping, Texture Baking and Lightmaps – ShiVa Engine

UV Mapping, Texture Baking and Lightmaps

How can you cut down on drawcalls with texture baking? How can you save disk space with multiple UV sets? How can you use pre-rendered lightmaps and still retain the ability to edit individual surfaces? Let's find out!

The Setup

Consider the following simple test scene. It consists of 3 pillars and a ground plane. We want every object to look different.

There are essentially 5 ways to get this scene into ShiVa:

1. Export every object separately and assemble the scene in ShiVa.
2. Export the whole object as a group, so you retain the individual components for later modification in ShiVa.
3. Combine everything into one mesh. Unrwap everything into a single UV. Assign a single material to the entire structure.
4. Combine everything into one mesh. Unrwap everything into a single UV. Assign different materials to every element.
5. Combine everything into one mesh. Unrwap everything into separate UVs. Assign different materials to every element.

Every method has its pros and cons, and one method is not necessarily better than the other. In a nutshell:

Option 1 is good for simple scenes with lots of reused and reusable objects. Imagine a large hallway with lots of pillars. But it takes time in ShiVa to reassemble, and you are potentially producing a lot of drawcalls. Option 2 is the same as 1, but saves you the reassembly process at the cost of flexibility.

You are most likely very familiar with options 1 and 2. This article is going to investigate options 3, 4 and 5, which are less used.

Single UV, single material

This method is optimal for single large objects, characters, objects with one surface type, and mobile devices since it is very efficient and produces only 1 drawcall. But you will end up with large texture sizes, so your game file size will be larger than average.

All objects are combined into a single mesh, and then unwrapped into a single UV map:

The single UV map does not limit you to just a single surface, but you will have to draw all your diffuse colors into a single texture.

Import the model and texture into SHiVa, and it will look something like this:

Now you can start refining your scene, for instance by placing lights, rendering static lightmaps, etc:

Bake everything

You can go so far to bake everything into this single map, including the light map from a DCC renderer. The map from this scene...

... will come out as baked map like this...

... can be mapped as a regular texture in ShiVa...

... or can be pre-multiplied with your diffuse map:

Of course, this is a very inflexible approach. You will not be able to shift the texture around, adjust scaling, alter individual surface properties or lighting after the fact:

Single UV, multiple materials

This method allows for fine tuning of individual surfaces, but has potentially the same problems as the single material method with none of its benefits: you keep the big texture file sizes, and the object is no longer a single draw call. Use only if you want to keep lighting (AO and Lightmap) all in the same UV set.

Speaking of UVs, here is what they look like. It is essentially the same as before, but the selected box only has its own surfaces inside its own material.

With this setup, it is now possible to adjust surface properties for the pillars and ground independently from each other:

You can even use a completely different texture, like in this picture where we switched the wood texture and set the scale modifier to 1x...

.. and here it is at 3x, all without changing the baked ground plane:

But of course, you lose the (shared) baked lightmap on the pillar in the process.

Separate UVs, separate materials

This approach is a filesize-optimized version of the previous one. By giving every individual component the most available UV space on its own, you can dramatically cut down on file sizes. The UV for each pillar looks like this:

Instead of one large map, you will generate many small ones, so you trade drawcalls for disk space:

Multiple UV sets for Lightmaps

There is a way to decouple lightmaps from the diffuse texture(s) of an object using multiple UV sets. We already have a tutorial on this topic on our blog. You can easily combine UVs of different models with tools like Ultimate Unwrap 3D. Just export the UV from model A and import it into model B as UV set 2:

However there is a drawback. The UV coordinates have to match the object. You cannot import merge the UVs of approach 3 (single material, single UV) with approach 5 (multiple materials, multiple UVs) without severe distortions:

This means that you are limited to one surface texture per object if you wish to use the 2nd UV set.

With the help of the 2nd UV set, you can freely scale and move the diffuse map around without losing any baked lighting information:

In essence, the 2nd UV map approach is the pinnacle of saving texture space at the cost of drawcalls. Both lightmap and (tiling) diffuse texture can be very small and still look very rich, if your device can handle the drawcalls, since every surface will need its own individual material.