Improving performance with LOD groups

Optimization principal 5: Minimize the number of draw calls.

Detailed geometry and high-resolution texture maps can be a double-edged sword: they can deliver a better visual experience, but they can impact negatively on the game's performance. LOD groups address this issue by replacing high-quality objects by simplified versions whenever that object takes up a smaller portion of the screen than necessary for a high-quality version to make a significant difference.

In this recipe, we will use a LOD group to create a game object featuring two different levels of detail: a high-quality version for whenever the object takes up more than 50 percent of the screen and a low-quality version for the times it takes up less than that amount. We would like to thank Carl Callewaert, from Unity, for his demonstration of the LOD Group functionality, which has informed this recipe in many ways.

Improving performance with LOD groups

Getting ready

For this recipe, we have prepared two prefabs for the high- and low-quality versions of the game object. They share the same dimensions and transform settings (position, rotation, and scale), so that they can replace each other seamlessly. Both prefabs are contained within the package named LODGroup, available in the 1362_11_16 folder.

How to do it...

To create a LOD group, follow these steps:

  1. Import the LODGroup package into your project.
  2. From the Project view, inside the LOD folder, drag the batt-high prefab into the Hierarchy view. Then, do the same for the batt-low prefab. Make sure that they are placed at the same Position (X: 0; Y: 0; Z: 0).
  3. From the Create drop-down menu in the Hierarchy view, create a new empty game object (Create | Create Empty). Rename it battLOD.
  4. Add the LODGroup component to battLOD (menu Component | Rendering | LODGroup).
  5. Select the battLOD object, and, from the Inspector view, LODGroup component, right-click on LOD 2 and delete it (since we'll have only two different LODs: LOD 0 and LOD 1), as shown in the following screnshot:
    How to do it...
  6. Select the LOD 0 area, click on the Add button, and select the batt-high game object from the list. A message about reparenting objects will appear. Select Yes, Reparent.
    How to do it...
  7. Select the LOD 1 section, click on Add, and select the batt-low object. Again, chose Yes, Reparent when prompted.
    How to do it...
  8. Drag the limits of the LOD renderers to set them as: LOD 0: 100%, LOD 1: 50%, Culled: 1%. That will make Unity render bat-high whenever it occupies 51 percent to 100 percent of the screen space, batt-low when 2 percent to 50 percent, and will not render anything if 1 percent or less.
    How to do it...
  9. Move the scene's camera toward the battLOD object and back. You will notice how Unity swaps between the high- and low-definition LOD renderer as it occupies more or less than 50 percent of the screen's space.

How it works...

Once we have populated the LOD renderers with the appropriate models, the LODGroup component will select and display the right renderer based on how much of the screen's percentage the object takes up, or even display nothing at all.

There's more...

Some details you don't want to miss:

Adding more LOD renderers

You can add more LOD renderers by right-clicking on an existing LOD renderer and selecting Insert Before from the context menu.

Fading LOD transitions

In case you want to minimize the popping that occurs when renderers are swapped, you can try changing the parameter Fade Mode from None to Percentage or Crossfade.

See also

Refer to the next recipe in this chapter for more information

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.36.221