Unity【LOD Group】- About the use and summary of LOD in performance optimization

LOD is a content often considered in performance optimization. This article is divided into the following parts to introduce the related content of LOD:

content

1. What is LOD:

2. How to use LOD:

Third, the disadvantages of using LOD:

Fourth, the use of LOD precautions:


1. What is LOD:

LOD technology (level of detail) is called multi-level detail. Its principle is that model objects display models with different levels of detail in the scene according to the distance from the camera. When fading away, a less detailed model is displayed, saving performance overhead.

2. How to use LOD:

LOD is implemented in Unity through the LOD Group component. For the container model shown in the figure, we have prepared four Mesh meshes with different levels of detail:

Create a new empty object and add the LOD Group component, which is divided into 3 layers by default. The last Culled layer refers to the culling layer, which will not render any models:

Here we need four levels, and insert one by selecting a level and right-clicking/Insert Before:

Use Add to add Mesh meshes to be rendered at different levels:

Preview in the scene after adding:

In addition to setting the LOD in the Inspector view panel, it can also be set in the code. The SetLODs function is provided in the LOD Group class:

//
// 摘要:
//     Set the LODs for the LOD group. This will remove any existing LODs configured
//     on the LODGroup.
//
// 参数:
//   lods:
//     The LODs to use for this group.
[MethodImpl(MethodImplOptions.InternalCall)]
[FreeFunction("SetLODs_Binding", HasExplicitThis = true)]
public extern void SetLODs(LOD[] lods);

 Test code:

using UnityEngine;

public class LODExample : MonoBehaviour
{
    private void Start()
    {
        LODGroup group = gameObject.AddComponent<LODGroup>();
        LOD[] lods = new LOD[4];
        for (int i = 0; i < lods.Length; i++)
        {
            lods[i] = new LOD(1 - (i + 1) * .2f, new Renderer[] { transform.GetChild(i).GetComponent<Renderer>() });
        }
        group.SetLODs(lods);
        group.RecalculateBounds();
    }
}

Third, the disadvantages of using LOD :

The disadvantages are also obvious. The first is to increase the workload of modeling colleagues. It is necessary to prepare models with different levels of detail. Of course, there are many plug-ins for automatic surface reduction, such as the Mesh Simplify plug-in in the resource store, but the program surface reduction will destroy more or less. The original appearance of the model, the ideal situation is that the modeler manually reduces the surface. In addition, a large number of model files will not only increase the size of the package body , but also greatly increase the memory consumption during runtime . Therefore, there is an evaluation of space for time for LOD. Of course, whether to use LOD technology in the end depends on the specific situation. Performance optimization It is nothing more than a trade-off between CPU, GPU and memory.

Fourth, the use of LOD precautions:

Only the model with the highest level of detail will participate in the baking of static lighting. As shown in the figure, when LOD0 on the container object transitions to LOD1, it will turn black, because LOD1 does not participate in static lighting baking.

If we want a model with a lower level of detail to look normal, we need to place a Light Probe Group or Light Probe around it to get indirect lighting during the baking process.

For details, please refer to the official documentation: https://docs.unity.cn/2017.2/Documentation/Manual/LODForBakedGI.html

  Welcome to the public account "Contemporary Wild Programmer"

Guess you like

Origin blog.csdn.net/qq_42139931/article/details/123680854