Unwrapping and Chart reduction

Geprüft mit Version: 5.5

-

Schwierigkeitsgrad: Fortgeschrittene Anfänger

Now that we have excluded suitable objects from the lighting precompute, we can begin the work of reducing the number of remaining Charts.

Charts are created to encompass the UV lightmap coordinates of a Static Mesh Renderer. The number of Charts that an object requires is therefore largely determined by the number of UV shells (pieces) needed to unwrap the object in question. Unwrapping is always a tradeoff between distortion of the texel distribution on geometry faces, and the number of shells required for sufficient texture coverage.

Unity’s sophisticated unwrapping algorithms will often achieve good results without our intervention; however, sometimes we need to provide a little guidance. It is therefore important that we understand the theory and process behind automatic unwrapping.

Consider the following illustrations:

An unwrap without distortion, but requiring multiple UV shells.

UV coordinates which result in a single UV shell, but yield unacceptable texture distortion.

An ideal result, with no texture distortion and a single UV shell.

In the images above, we show three different examples of how the UVs on an object can be projected or unwrapped.

In the first image, we can see a result which is largely free of distortion. The checker pattern we are using as a texture is mapped in such a way that each tile of the texture remains square in aspect. If we imagine that this checkerboard was a lightmap (an ‘image’ of the lighting applied to the object), we would get a result that looked visually correct and without distortion. However, we require six UV shells to get this coverage. As a result, we would get six Charts using Unity’s Precomputed Realtime GI system. Regardless of the size of the object, each Chart would require a minimum of 4x4 texels, meaning that this object would use at least 96 texels irrespective of resolution.

In the second image, we have a different problem. The object’s UV texture coordinates have been projected so that we have one complete UV shell covering all faces. While this would be optimal in terms of the number of Charts required to cover the object, the visual results would be unacceptably distorted. We would likely see ‘smearing’ of textures applied across the surface of the object. The faces of the object also overlap in UV texture space - meaning that if we we were referencing a lightmap, the lighting from one side of the object would be displayed on opposing faces. Clearly this method of unwrapping the object will not give us an acceptable result.

The third image shows an example of an ideal unwrap. We have a result which is free of distortion: the tiles of our checkerboard texture have remained square in aspect. We have also succeeded in covering all faces of the object while only outputting one UV shell. This has been achieved by joining, or stitching UV edges which correspond to contiguous geometry edges on the model.

If we think about this programmatically, what have we done to achieve an ideal unwrap? First, we have made our UV projections orthographically onto the object in order to create our individual shells. We have then evaluated these shells for edges which are shared in the geometry of the corresponding object. Where shared edges are found, we have moved and stitched those shells to the partner edges of their neighbouring shell. This is what Unity’s unwrapping algorithms attempt to do automatically with Static geometry.

Visualising Charts

Before working to optimize our unwrapping and lightmap Charts, we need a way to visualize them in the Editor. Charts are generated during the unwrapping stage of the mesh import pipeline. For Precomputed Realtime GI, these Charts are then packed into atlasses during the Geometry stage of the precompute process. This is to ensure that they don’t overlap. We can only see a preview of our Charts once the Geometry stage of the precompute has completed and the visualisation data has been saved.

If you are working with version control as part of a team, please be aware that this information is cached locally. This means that a precompute must be carried out on your local machine before the various diagnostic draw modes become available.

UV Charts draw mode representing the different Charts used in the Scene as colored regions and lightmap resolution as a checkerboard overlay.

One way to quickly visualize your Charts is to use the UV Charts draw mode in the Scene view.

In the top left of the Scene view, use the Draw Mode drop-down menu to select UV Charts.

Using this mode, Charts can be seen as different-colored panels overlaid with a checkerboard texture representing the corresponding lightmap resolution. If you have Auto mode enabled (Window > Lighting > Auto), changes to unwrap parameters will be calculated automatically and the Scene view will be updated with the results.

The Charting mode of the object Preview window showing Charts as colored squares and lightmap UVs as light blue wireframe.

It can be easy to miss Charts in the Scene view when working with complex objects. By using the Preview pane in the Lighting window, we can view all the Charts used by an individual object. This can help us to more accurately assess the unwrapping of these objects, which will help us to reduce Chart counts in our Scene.

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

Select the object that we wish to view in the Hierarchy window.

In the top left of the Preview pane of the Lighting window, choose Charting from the drop-down menu.

The number of Charts used by that object will be represented by colored squares overlaid with the corresponding UV coordinates in light blue.

Unwrapping parameters explained

There are a number of settings that we can tweak to optimize UV unwrapping. All of these settings are per-object. We can access these settings through the Lighting window, as follows:

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

Select the object that we wish to view in the Hierarchy window.

Auto UV Max Distance

Unity’s unwrapping algorithms attempt to simplify lightmap UVs by moving shells and stitching UV edges together. UV shells will only be considered for this operation if shells are within the distance defined by Auto UV Max Distance. This range is defined in Unity’s world space units. Remember that in our tutorial Scene we are assuming that a unit is 1 meter.

The Auto UV Max Distance setting found in the Object tab of the Lighting window.

In many cases the default value of 0.5 units will give acceptable results. For particularly large objects with large faces, it may be necessary to increase this value. This is to prevent suitable candidate UVs from being excluded from selection by the stitching algorithm.

Increasing the Auto UV Max Distance will often reduce the number of Charts required by the selected object. Decreasing this value is useful in situations where there is visible stretching of lightmap texels and we may actually need more Charts in order to get the required texel coverage. The results of these changes can be easily assessed using the checkerboard overlay in the UV Charts Scene draw mode. Finding the right balance can often require a little experimentation.

Auto UV Max Angle

Lighting UV shells are also evaluated for stitching based on the angle between neighbouring faces of the corresponding mesh. Auto UV Max Angle defines the maximum angle permitted between faces sharing a UV edge and is calculated using the internal angle. If the angle between the backfaces is greater than this amount, these UV shells will not be considered for stitching.

The Auto Max Angle settings can be found within the Object tab of the Lighting window.

Increasing this value will make it more likely that lighting UVs will be combined by Unity’s unwrapping algorithm. Auto UV Max Angle can therefore can be a good way to reduce the number of Charts required by a selected object. However, sometimes stretched lightmaps can occur when this tolerance is too relaxed. Decreasing the Auto UV Max Angle will make the unwrapper less likely to combine UV edges, which will result in more Charts but less distortion. Again, using the checkerboard overlay in the UV Charts Scene draw mode is a good way of assessing the suitability of the values we are using.

Preserve UVs

In some cases it may be impossible to get an ideal unwrap using the auto unwrapper. We may end up with too many Charts or unacceptable distortion in our lightmaps (visualized as stretched checkering when in GI Charts draw mode). In these cases it may be necessary to manually author UVs in the UV01 channel of our model file. This is work that needs to be done while in our chosen content creation package.

If this is the case, we can use the Preserve UVs option to force Unity’s unwrapping algorithms to preserve the shape of the UV shells defined in your model’s UV01 channel.

The Preserve UVs option is useful when it is desirable to keep your manual lighting UVs.

It is important to note that these shells will always be be repacked to save lightmap space. It is the manner in which the shells are unwrapped, individually, which will be preserved rather than the shell’s position within the lightmap.

We must be cautious when using this approach. In cases where our lightmap UVs contain a large number of UV shells, this option can actually increase precompute times. This is because the UV merging step offered by Unity’s auto unwrapper is bypassed and our manual UV layout is preserved. Remember that the desired outcome is as few UV shells - and therefore as few Charts - as possible, while keeping an acceptable level of distortion.

Ignore Normals

In certain cases the mesh importer may decide to split geometry. This will also affect Charts. For example, if a mesh has an extremely high triangle count it may be more performant for Unity to split it into separate sub-meshes. Often this is for hardware specific requirements, such as reducing the number of triangles in each draw call. Where these splits occur is decided based on areas where there is large variance in the normal angle between adjacent mesh faces, such as hard edges . Splitting meshes in this way happens during the mesh import process for your models. Charts can potentially be split during this process as edges that fall within a Chart may be separated, resulting in multiple shells which in turn will require additional Charts.

The Ignore Normals checkbox prevents charts being split during the import pipeline.

Sometimes it is not desirable to split your Charts in this way. The resulting increase in the number of Charts may be detrimental to precompute times, and lighting seams can cause unwanted visual artefacts in the resulting lightmaps. In these cases, enabling the Ignore Normals checkbox will prevent the Charts being split for Precomputed Realtime GI lighting.

Note that only Precomputed Realtime GI is affected by this option. Splits in the selected meshes are still preserved for other uses in Unity.

Faster iteration in larger Scenes

Complex Scenes can contain hundreds - even thousands - of Static objects. Generating the Chart atlases for all these objects can result in slow lighting precomputes and this can negatively affect the speed with which we are able to iterate on our Scenes.

When experimenting with unwrap settings for objects, sometimes it can be useful to isolate an object of interest in an empty Scene where we can iterate quickly with minimal precompute times. The unwrap settings we determine can then be taken back and applied to other objects of that type in our original Scene. Working in this way can add up to big time savings when preparing Scenes for lighting.

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.

Copy this object to the clipboard by pressing Ctrl+C (Cmd+C on Mac).

Create a new Scene by pressing Ctrl+N (Cmd+N on Mac).

If prompted to save your changes, select Yes if you are happy with your current Scene progress or No if you wish to discard it.

In the newly created Scene, paste HouseBig02 from the clipboard by pressing Ctrl+V (Cmd+V on Mac).

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

Enable Auto precompute mode by enabling the Auto checkbox.

Now select the Object tab.

In the top left of the Object tab, select Charting from the drop-down menu.