Skip to content

LOD Generator API

Mattias Edlund edited this page Aug 18, 2021 · 3 revisions

About

The LOD Generator API assists in creating LODs for game objects with much ease. It will collect all meshes through MeshFilter and SkinnedMeshRenderer components on children of the game object, then simplify them using the Mesh Simplifier API, and lastly create a LODGroup component with all the desired LOD levels. You can read more about LODs in Unity here.

For the non-programmers out there, you are more likely interested in the LOD Generator Helper component.

API

You will find the API in the LODGenerator class of the UnityMeshSimplifier interface. The class is static, so no instance has to be created for this, since it doesn't maintain a state.

Static Methods

Type Name Summary
LODGroup GenerateLODs(LODGeneratorHelper generatorHelper) Generates the LODs and sets up a LOD Group for the LOD generator helper component.
LODGroup GenerateLODs(GameObject gameObject, LODLevel[] levels, bool autoCollectRenderers, SimplificationOptions simplificationOptions) Generates the LODs and sets up a LOD Group for the specified game object.
LODGroup GenerateLODs(GameObject gameObject, LODLevel[] levels, bool autoCollectRenderers, SimplificationOptions simplificationOptions, string saveAssetsPath)) Generates the LODs and sets up a LOD Group for the specified game object.
bool DestroyLODs(LODGeneratorHelper generatorHelper) Destroys the generated LODs and LOD Group for the LOD generator helper component.
bool DestroyLODs(GameObject gameObject) Destroys the generated LODs and LOD Group for the specified game object.

Structs

LOD Level

Type Name Summary
float ScreenRelativeTransitionHeight Gets or sets the screen relative height to use for the transition [0-1].
float FadeTransitionWidth Gets or sets the width of the cross-fade transition zone (proportion to the current LOD's whole length) [0-1]. Only used if it's not animated.
float Quality Gets or sets the quality of this level [0-1].
bool CombineMeshes Gets or sets if all renderers and meshes under this level should be combined into one, where possible.
bool CombineSubMeshes Gets or sets if all sub-meshes should be combined into one, where possible. This is only used if CombineMeshes is true. Important: This is not yet supported.
Renderer[] Renderers Gets or sets the renderers used in this level. These will have no purpose if automatic collection is used for the LOD generator.
SkinQuality SkinQuality Gets or sets the skin quality to use for renderers on this level.
ShadowCastingMode ShadowCastingMode Gets or sets the shadow casting mode for renderers on this level.
bool ReceiveShadows Gets or sets if renderers on this level should receive shadows.
MotionVectorGenerationMode MotionVectorGenerationMode Gets or sets the motion vector generation mode for renderers on this level.
bool SkinnedMotionVectors Gets or sets if renderers on this level should use skinned motion vectors.
LightProbeUsage LightProbeUsage Gets or sets the light probe usage for renderers on this level.
ReflectionProbeUsage ReflectionProbeUsage Gets or sets the reflection probe usage for renderers on this level.

Examples

Generate LODs for a game object at runtime

using UnityEngine;
using UnityMeshSimplifier;

public class AutoGenerateLODs: MonoBehaviour
{
    [SerializeField, Tooltip("The simplification options.")]
    private SimplificationOptions simplificationOptions = SimplificationOptions.Default;
    [SerializeField, Tooltip("If renderers should be automatically collected, otherwise they must be manually applied for each level.")]
    private bool autoCollectRenderers = true;
    [SerializeField, Tooltip("The LOD levels.")]
    private LODLevel[] levels = null;

    private void Start()
    {
        GenerateLODs();
    }

    private void Reset()
    {
        simplificationOptions = SimplificationOptions.Default;
        autoCollectRenderers = true;
        levels = new LODLevel[]
        {
            new LODLevel(0.5f, 1f)
            {
                CombineMeshes = false,
                CombineSubMeshes = false,
                SkinQuality = SkinQuality.Auto,
                ShadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.On,
                ReceiveShadows = true,
                SkinnedMotionVectors = true,
                LightProbeUsage = UnityEngine.Rendering.LightProbeUsage.BlendProbes,
                ReflectionProbeUsage = UnityEngine.Rendering.ReflectionProbeUsage.BlendProbes,
            },
            new LODLevel(0.17f, 0.65f)
            {
                CombineMeshes = true,
                CombineSubMeshes = false,
                SkinQuality = SkinQuality.Auto,
                ShadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.On,
                ReceiveShadows = true,
                SkinnedMotionVectors = true,
                LightProbeUsage = UnityEngine.Rendering.LightProbeUsage.BlendProbes,
                ReflectionProbeUsage = UnityEngine.Rendering.ReflectionProbeUsage.Simple
            },
            new LODLevel(0.02f, 0.4225f)
            {
                CombineMeshes = true,
                CombineSubMeshes = true,
                SkinQuality = SkinQuality.Bone2,
                ShadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.Off,
                ReceiveShadows = false,
                SkinnedMotionVectors = false,
                LightProbeUsage = UnityEngine.Rendering.LightProbeUsage.Off,
                ReflectionProbeUsage = UnityEngine.Rendering.ReflectionProbeUsage.Off
            }
        };
    }

    private void GenerateLODs()
    {
        LODGenerator.GenerateLODs(gameObject, levels, autoCollectRenderers, simplificationOptions);
    }
}