Optimizing Unity's auto unwrapping

Geprüft mit Version: 5.5

-

Schwierigkeitsgrad: Fortgeschrittene Anfänger

When configuring unwrap parameters, the ideal outcome is to find a combination of values that results in the lowest number of Charts while having the least lightmap distortion. Remember that distortion is visualized as stretching of the checkerboard texture applied to objects in the Scene view when UV Charts draw mode is enabled.

The checkerboard pattern can be used to visualize lightmap texel distribution. Notice here that the checkerboard is relatively consistent across our model - meaning there is little lightmap distortion.

Stretching and warping of the checkerboard indicates that there is some inconsistency in the distribution of lightmap texels.

In this example, we will apply what we have learned about UV unwrapping to optimize precompute times on an object in our tutorial project.

Open the LightingTutorialStart Scene included in the example project.

Select one of the objects called HouseBig02 in the Hierarchy window. These are grouped under Environment > Structures > Houses.

Open the Lighting window (Window > Lighting) and select the Object tab.

Select Charting from the drop-down menu in the top left of the tab.

Expand the Preview area at the bottom of the tab. Note how many UV shells are displayed here and the number of corresponding colored Charts.

Image showing the Charting preview for the HouseBig02 object with default unwrap settings. This number of Charts can be easily reduced.

HouseBig02 is a fairly complex object within our Scene, which may justify the use of multiple Charts. However, we may still be able to reduce this number by modifying our unwrap settings.

The Object unwrap settings panel

There is no one-size fits all solution for unwrapping objects. Unity’s unwrapping algorithms attempt to make sensible decisions based on appropriate defaults, but with some user guidance, we may be able to achieve a more optimal result.

Select HouseBig02 in the Hierarchy window.

Open the Lighting window (Window > Lighting) and select the Object tab.

Lower the Auto UV Max Distance to its minimum value of 0.1.

The precompute process will begin. Following a brief calculation, we can see in the Preview area of the Lighting window that number of Charts has noticeably increased. Checking the Scene view there seems to be little distortion, which is good. Unfortunately, however, this many Charts for an object of this complexity is unacceptable. Remember that more Charts means longer precompute times and reduced performance at run time.

If HouseBig02 is not selected in the Hierarchy window, select it.

Navigate to the Lighting window (Window > Lighting) and select the Object tab.

Set the value of Auto UV Max Distance to 10.

We now have the opposite problem. The number of Charts has decreased, but at the cost of introducing unacceptable distortion to our lightmaps. We can observe this distortion in the UV Charts Scene draw mode. The distortion appears as streaking or stretching of the checkerboard overlay.

Selecting an Auto UV Max Distance which is too high can result in distortion of our lightmap UVs.

We know that Unity’s unwrapping algorithm attempts to minimise the number of Charts by merging UV shells together. The Auto UV Max Distance setting specifies the maximum distance, in worldspace, permitted between UV shells when they are considered for merging. If the shells are further apart than this value, they will not be combined.

When we decreased our Auto UV Max Distance, fewer UV shells were eligible for merging. This meant that more individual Charts remained at the end of the process. When we increased our Auto UV Max Distance, UV shells that were very far apart in worldspace were then able to be merged. This resulted in fewer Charts, but this aggressive merging also caused warped lightmap UVs and therefore more distortion in our lightmap output.

Note that with large objects it can sometimes be necessary to increase the Auto UV Max Distance in order to accommodate the size of faces within the mesh. Conversely, with smaller objects decreasing the Auto UV Max Distance might yield better results.

With the example of our object, HouseBig02, an Auto UV Max Distance value of 0.8 gives a good compromise between Chart count and distortion.

If HouseBig02 is not selected in the Hierarchy window, select it.

Navigate to the Lighting window (Window > Lighting) and select the Object tab.

Set the value of Auto UV Max Distance to 0.8.

Using a Auto UV Max Distance of 0.8 gives a good result with our example object. However, we can still improve this further.

Now we will look at the Auto UV Max Angle parameter. Like Auto UV Max Distance, this value affects the UV merging step of Unity’s unwrapping algorithms. In addition to being tested for distance before UV shells are combined, the unwrapper also tests the angle between adjacent mesh faces. Only edges with an angle below this value will be considered for merging.

A smaller Auto UV Max Angle means that fewer edges will pass the test. As a result, fewer UV shells will be merged and the number of Charts will increase. Conversely, a larger Auto UV Max Angle means that the merging algorithm will be more tolerant of angles between faces. More UV shells are likely to be merged, the result of which will likely be fewer UV shells and therefore fewer Charts. As with the Auto UV Max Distance, if unsuitable edges are combined due to overly wide tolerances, distortion may occur in the final lightmaps UVs.

If HouseBig02 is not selected in the Hierarchy window, select it.

Navigate to the Lighting window (Window > Lighting) and select the Object tab.

Set the value of Auto UV Max Angle to 0.

When we look at the Preview area of the Lighting window, we can see that the number of Charts created is unacceptably high. This is because we are only allowing a deviation of 0 degrees or less between faces before they are considered for merging. This almost certainly means that no UV shells will pass the test and therefore many Charts will remain following this merging process.

Without some tolerance, a low Auto UV Max Angle will result in an excessive number of Charts.

Next we will try increasing the Auto UV Max angle.

If HouseBig02 is not selected in the Hierarchy window, select it.

Navigate to the Lighting window (Window > Lighting) and select the Object tab.

Slowly increase the value of Auto UV Max Angle, observing effects of each change in the Preview area of the Lighting window.

We can see that the number of Charts decreases as the value goes up. This is because the angle permitted between mesh faces considered for merging is more tolerant. More UV shells pass the test and will be merged. Consequently, the number of lightmap UV shells is reduced and the number of corresponding Charts decreases.

Copy Component is an easy way to take your unwrap settings back to the main Scene.

The modest savings these settings give us can add up to much more significant optimisations when applied across a Scene of several hundred objects.

Unwrap settings: per-instance or per-Prefab?

Unwrap settings can be stored in a Prefab by applying them to any Static MeshRenderer component that lives within that Prefab’s hierarchy. Alternatively, unwrap settings can also be applied to individual GameObjects within the Scene. Unwrap settings applied to an instance of a Prefab in a Scene will override the settings stored within the Prefab. This way of working offers us some useful approaches to setting up Scene lighting.

It is often useful to configure a default unwrapping and lighting setup for your Prefabs. If the Prefabs are to be instantiated numerous times, it can save effort to have the unwrapping for that object preconfigured rather than having to specify this repeatedly throughout the Scene.

On the other hand, it is frequently desirable to configure lighting settings based on the context in which an object is used within the overall Scene. For example, if an instance of a Prefab will be seen up close within the playable area then it makes sense to use higher fidelity setting on that instance. If the instance of the Prefab is far away, then it would not make sense to use our lighting budget maintaining the same settings. Here, we would likely want to lower the quality of our lightmaps and use more aggressive stitching parameters in our unwrap.

The ability to configure default settings that suitable for most use cases and then override these settings in special cases can be very useful. In the case of this lighting tutorial, we will be applying our settings per-instance, in the Scene.

Applying unwrap settings throughout the Scene

It is time to start applying what we have learned about Unity’s automatic unwrapping algorithm to the remaining Static geometry within the LightingTutorialStart Scene. Working systematically, it should not take long to set up the Scene for significantly improved precompute times.

Open the LightingTutorialStart Scene included in the example project.

In the Hierarchy window, select the top GameObject found under Environment > Structures.

Copy the GameObject to an empty Scene.

At this point we should apply what we have learned about the unwrap parameters and feel free to experiment with different values. Remember to use the UV Charts Scene draw mode in combination with the Charting mode of the Preview area of the Lighting window. This will enable us to find the right balance between lightmap distortion and the number of Charts required.

The LightingTutorialOptimal Scene can be used as a reference in cases where we are unsure. Note that in some cases it may even be unnecessary to change the default unwrap values at all. The default values may actually provide the best outcome.

Experiment with different values for the unwrap parameters until a balance is reached between number of Charts and lightmap distortion.

Copy the settings, return to the LightingTutorialStart Scene and paste them into all other instances of the same GameObject. Multi-selecting all the corresponding objects in the hierarchy will enable you to use Paste Component Values to quickly apply the settings across multiple objects.

Repeat this process for every uniquely named GameObject in the Hierarchy view.

Once all of the GameObjects under the Structures group have been optimized, move onto the remaining Static geometry under the Rocks group. Working methodically in this way, it should take no longer than a few minutes to set the unwrap values for all of the Static geometry within your Scene. The outcome of this manual preparation will be faster iteration when lighting the Scene and better performance at run time.