With 3D games and applications on a wider range of devices than ever, it can be a challenge to deliver a consistently smooth experience. One tool to achieve this is intelligent optimization and display of visual assets through the use of LOD Groups.
In Unity, we use LOD groups to display a GameObject at various levels of detail (LOD) depending on its distance from the camera. When the GameObject is beyond a certain distance, it can be culled from the scene altogether.
An LOD group is made up of renderers — meshes or Sprites — that render the object in different ways depending on its distance from the camera. Typically, an LOD group is made up of single meshes of an entire object with successively lower resolutions. Another approach is to break a mesh into parts, showing fewer parts as the GameObject gets farther from the camera. In addition, Sprite renderers are useful for distant LODs, and in scenes with many characters or objects, such as large battlefields or forests.
If you have imported LOD groups from another application, such as 3dsMax or Maya, the renderers are already grouped, and you can skip the grouping step in Unity and continue with “Configuring LODs,” below.
To group LOD objects in Unity:
Name your renderers using “LOD” and sequential numbers at the end of the names, starting with LOD0 (or LOD00 if you will need two digits).
Create an empty GameObject by selecting Create Empty from the GameObject drop-down. Name it for your object. We’re using VegetationSmall07 from the 3D Game Kit, available in the Unity Asset Store.
Renderers used in an LOD group must be children of the LOD group’s GameObject. Drag your LOD Sprites or meshes from the Project window onto the GameObject in the Hierarchy. This will place them in the scene and parent them to your GameObject. (Figure 01)
Next, we’ll add the LOD group component to the parent GameObject, which will bring up the LOD Group Inspector. There, we will configure each LOD to appear when the camera is within a certain range of the GameObject. The percentages listed in the LOD Group Insector represent the amount of vertical space the GameObject takes up in the camera view. In our example below, once the GameObject takes up 60 percent or less of the camera view vertically, LOD 1 is displayed until it takes up 30 percent or less, and so on. We can also use the LOD group Inspector to set fades and animations for smoother transitions between LODs.
Select the parent object and add an LOD group component using either the Add Component button in the Inspector, or by selecting Rendering > LOD Group from the Component drop-down. The LOD group will be added to the GameObject’s Inspector.
Click LOD 0 to select it, and click the Add button. (Figure 02)
Double click the highest quality LOD Mesh Renderer to add it to LOD 0. (Figure 03)
You may wish to add more than one renderer to an LOD. Let’s say you’re setting up an LOD group for a tree. You can display the tree as one unified mesh at LOD 0, but separate a medium quality mesh into a mesh for the trunk and a Sprite for the leaves, showing both at LOD 1 and the entire tree as a Sprite at LOD 2. To add multiple renderers, use the Add button (Figure 04) for each LOD Renderer. The process is identical for mesh and Sprite LOD Renderers.
If you need more than the default 3 LODs, repeat adding LODs to fill out the bar. To add renderers to an LOD, use the Add button.
Drag the edges of the segments to change the LOD ranges.
To preview the transitions, either scroll with your mouse scroll wheel in Scene view, or click and drag the camera in the LOD Group Inspector. The camera will move in relation to the object. Adjust the thresholds to get the right level of detail at every distance from the camera.
The object can disappear or become very small at the lowest percentage. Adjust the Culled segment to set the threshold for it to vanish, or slide the Culled segment all the way off to the right if you want to keep the object visible at greatest distances.
Making Smoother LOD Transitions
As the camera moves, your object might “pop” from one level of detail to the next. You can set up fades to smooth out these transitions.
Select an LOD to configure the transition between it and the next lower LOD.
Select a Fade Mode:
None: Meshes are replaced (or culled) instantly.
Cross Fade: Select this mode to smoothly fade to the next lower LOD. If Animate Cross Fade is checked, the fade happens at the threshold between LODs and is nearly instant. Or, uncheck Animate Cross Fade to allow for manual control of the fade using the Fade Transition Width, explained below.
SpeedTree:SpeedTree Assets are imported with this mode. For non-SpeedTree Meshes, this mode behaves identically to None.
The Fade Transition Width can also cause the cross fade to the next LOD to begin sooner than the threshold between LODs, allowing for a smoother, longer transition. (Figure 05)
Recalculate Bounds: This button recalculates the bounding region for the LOD group. This is typically done automatically.
Recalculate Lightmap Scale: When using lightmapping, this button recalculates the lightmap scale to match the LOD percentages to aid in eliminating visual artifacts. For more information on Lightmapping, see the Unity manual.
Once configured, the LOD group works automatically as the distance between the GameObject and the camera changes. Your project should now run more efficiently, without needlessly rendering high-quality meshes when objects are far from the camera and high quality isn’t needed.