Optimizing Unity's auto unwrapping

Checked with version: 5.5

-

Difficulty: Intermediate

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.

description

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.

description

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.

description

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.

description

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.

description

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.

description

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.

description

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.

description

Increasing the Auto UV Max Angle can help reduce Charts - particularly on objects with rounded surfaces.

An Auto UV Max Angle of 93 yields good results with our example object, HouseBig02.

  • 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 93

Notice how the number of Charts is reduced in the Preview area of the Lighting window following a brief precompute.

description

The Charting preview for HouseBig02 following some optimisation of its unwrap settings.

At this stage, our optimized unwrap settings can be taken back into the working Scene.

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

  • In the Inspector panel, locate the MeshRenderer component and click the cog icon in the top right of its display.

  • Click Copy Component.

  • Open the LightingTutorialStart Scene.

  • Find all the objects in the Hierarchy named HouseBig02.

  • Hold down Ctrl (Cmd on the Mac) and click each of the HouseBig02 GameObjects in the Hierarchy window to select them all.

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

  • Select the the cog icon in the top right.

  • Choose Paste Component Values to apply the settings you previously copied.

description

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.

Graphics

  1. Introduction to Lighting and Rendering
  2. Choosing a Lighting Technique
  3. The Precompute Process
  4. Choosing a Rendering Path
  5. Choosing a Color Space
  6. High Dynamic Range (HDR)
  7. Reflections
  8. Ambient Lighting
  9. Light Types
  10. Emissive Materials
  11. Light Probes
  1. Introduction to Precomputed Realtime GI
  2. Realtime Resolution
  3. Understanding Charts
  4. Starting the precompute process
  5. Probe lighting
  6. Unwrapping and Chart reduction
  7. Optimizing Unity's auto unwrapping
  8. Understanding Clusters
  9. Fine tuning with Lightmap Parameters
  10. Summary - Precomputed Realtime GI
  1. Lighting Overview
  2. Lights
  3. Materials
  4. The Standard Shader
  5. Textures
  6. Using Skyboxes
  7. A Gentle Introduction to Shaders
  8. Using detail textures for extra realism close-up
  9. Frame Debugger
  1. Cameras
  2. Image Effects: Overview
  1. Meshes
  2. Mesh Renderers and Mesh Filters
  1. Where to Start?
  2. Preparing Unity Render Settings
  3. Lighting Strategy
  4. Modeling
  5. Standard Shader/Material PBS and texturing
  6. Lighting and Setup
  7. Understanding Post Process Features
  8. Dynamically Lit Objects
  9. Sample Project File
  1. Using Cameras
  2. Using Lights
  3. Fun with Lasers!
  4. The Particle System
  5. Cinematic Explosions - PIT
  6. Cinematic Composition - PIT
  7. Image Effects: Overview
  8. Fun with Explosions!
  9. Exploring the Blacksmith Environment
  1. Turning it up to 11: Making Unity 5 look Awesome!
  1. Substance - Introduction
  2. Substance - Understanding PBR
  3. Substance - Working with PBR in Unity
  4. Substance - Using Substance materials in Unity
  5. Substance - Optimization for Substance materials
  6. Substance - Creating rock shapes
  7. Substance - Creating rock material, Pt 1
  8. Substance - Creating rock material, Pt 2
  9. Substance - Creating the dirt ground material
  10. Substance - Creating the rock ground material, Pt 1
  11. Substance - Creating the rock ground material, Pt 2
  12. Substance - Publishing the Substance
  13. Substance - Creating a blocking scene
  14. Substance - Creating the ground model
  15. Substance - Modelling the rock assets
  16. Substance - Texturing the upper body
  17. Substance - Exporting textures from Substance Painter
  18. Substance - Creating a scene in Unity, Pt 1
  19. Substance - Creating a scene in Unity, Pt 2
  1. Introduction and Goals
  2. Flame Particles Overview
  3. Particle Emission and Color
  4. Adding Movement To Particles With Noise
  5. Creating The Ember Particles
  6. Adding Lighting To Particles
  7. Creating Sparks With Particle Trails
  8. Particle Question and Answers
  1. Introduction and Goals
  2. Adding a Second Camera
  3. Adding Minimap Icons
  4. Render Textures and UI
  5. Adding A UI Mask and Border Image
  6. Questions and Answers
  1. Session Introduction
  2. Rendering In Unity
  3. Anatomy Of An Unlit Shader
  4. The Vertex Function
  5. The Fragment Function and Color Tint
  6. Making A Transparent Shader
  7. Displacing Vertices and Clipping Pixels
  8. Questions and Answers
  1. Introduction and Session Goals
  2. The VideoPlayer Component
  3. Texturing Objects With Video
  4. Playing and Pausing
  5. Playing A New Clip
  6. Displaying Current Time and Clip
  7. Animated Playhead
  8. Questions and Answers
  1. Overview and Goals
  2. Tonemapping and Color Grading
  3. Camera Movement & Animation
  4. Post Processing Volumes
  5. Timeline & Cinemachine
  6. Questions and Answers