4

I'm having trouble leading with LOD Group, because I want to know which is the current active LOD level that I see in the screen. I only can access to the percentage with

GameObject.GetComponent<LODGroup>().GetLODs()[size].screenRelativeTransitionHeight;

Someone knows how to solve this? Thanks in advance.

derHugo
  • 83,094
  • 9
  • 75
  • 115
Bitelchús
  • 65
  • 2
  • 6

5 Answers5

5

Searching through the answers answers.unity3d.com, I came to this: http://answers.unity3d.com/questions/684467/find-the-lod-step-which-is-currently-used.html

 LODGroup lodGroup = obj.GetComponent<LODGroup>();
 if (lodGroup != null)
 {
     Transform lodTransform = lodGroup.transform;
     foreach (Transform child in lodTransform)
     {
         var renderer = child.GetComponent<Renderer> ();
         if (renderer != null && renderer.isVisible)
         { 
             Debug.Log("This LODlevel is used: " + child.name); 
         }
     }
 }

You can find out which LOD level is currently active by looking into the names of children GameObjects Renderers which are currently visible (visible on the screen).

maros
  • 432
  • 3
  • 17
  • Nice one @maros. The only thing I would suggest is storing reference to the renderer instead of using GetComponent. It will make it more performant. – FullStackForger May 03 '17 at 14:03
4

I had the same issue, and finally found out a solution that really works. (The renderer.isVisible is not updated often enough to be reliable, unfortunately, and I did not want to add additionnal components on ever LOD subobjects.)

I uploaded the solution here: https://github.com/JulienHeijmans/EditorScripts/blob/master/Scripts/Utility/Editor/LODExtendedUtility.cs

It is mostly code that I took from here: https://github.com/Unity-Technologies/AutoLOD/blob/master/Scripts/Extensions/LODGroupExtensions.cs I just rmeoved what was not necessary, and addod other utility functions that I had to use often.

It has been made to be used as an in-editor utility script, but the math to get the currently visible LOD level is there.

JulienH
  • 61
  • 3
  • Julien, thanks for sharing your LODExtendedUtility script! The GetVisibleLOD method is very helpful for debugging things. – DizzyDoo Oct 31 '20 at 21:39
0

Optimized answer from @maros:

static public int GetCurrentLODIndex(LODGroup lodGroup)
{
    LOD[] lods = lodGroup.GetLODs();
    for (int i = 0; i < lods.Length; i++)
    {
        LOD lod = lods[i];
        if (lod.renderers.Length > 0 && lod.renderers[0].isVisible)
            return i;
    }
    return -1;
}

But it is not suitable if you need to use the same render in different LODs. For example, I use vfx renderer in LOD0 and LOD1, for LOD1 vfx is paused. So used the following code based on the answer from @JulienH:

using UnityEngine;

public class LODUtils
{
/// <summary>
/// Returns the currently visible LOD level of a specific LODGroup, from a specific camera.
/// </summary>
public static int GetCurrentLODIndex(LODGroup lodGroup, Camera camera)
{
    var lods = lodGroup.GetLODs();
    var relativeHeight = GetRelativeHeight(lodGroup, camera);

    int lodIndex = GetMaxLOD(lodGroup);
    for (var i = 0; i < lods.Length; i++)
    {
        var lod = lods[i];

        if (relativeHeight >= lod.screenRelativeTransitionHeight)
        {
            lodIndex = i;
            break;
        }
    }

    return lodIndex;
}

private static float GetRelativeHeight(LODGroup lodGroup, Camera camera)
{
    var distance = (lodGroup.transform.TransformPoint(lodGroup.localReferencePoint) - camera.transform.position).magnitude;
    return DistanceToRelativeHeight(camera, (distance / QualitySettings.lodBias), GetWorldSpaceSize(lodGroup));
}

private static float DistanceToRelativeHeight(Camera camera, float distance, float size)
{
    if (camera.orthographic)
        return size * 0.5F / camera.orthographicSize;

    var halfAngle = Mathf.Tan(Mathf.Deg2Rad * camera.fieldOfView * 0.5F);
    var relativeHeight = size * 0.5F / (distance * halfAngle);
    return relativeHeight;
}

private static int GetMaxLOD(LODGroup lodGroup)
{
    return lodGroup.lodCount - 1;
}

private static float GetWorldSpaceSize(LODGroup lodGroup)
{
    return GetWorldSpaceScale(lodGroup.transform) * lodGroup.size;
}

private static float GetWorldSpaceScale(Transform t)
{
    var scale = t.lossyScale;
    float largestAxis = Mathf.Abs(scale.x);
    largestAxis = Mathf.Max(largestAxis, Mathf.Abs(scale.y));
    largestAxis = Mathf.Max(largestAxis, Mathf.Abs(scale.z));
    return largestAxis;
}
}
Vladimir Pankov
  • 377
  • 3
  • 8
-2
private static int GetLODCurShowLevel(Camera cam, LODGroup lodGroup)
{
    //var inv_SceneViewCamHeight = 1.0f / (cam.pixelHeight - 6.0f);
    var inv_SceneViewCamHeight = 1.0f / (cam.pixelHeight);

    var lods = lodGroup.GetLODs();
    for (int lodIDX = 0; lodIDX < lods.Length; lodIDX++)
    {
        var lod = lods[lodIDX];
        var renderers = lod.renderers;
        for (int renderIDX = 0; renderIDX < renderers.Length; renderIDX++)
        {
            var renderer = renderers[renderIDX];

            // method1:
            //var heightInScreen = Mathf.Abs(cam.WorldToScreenPoint(renderer.bounds.max).y - cam.WorldToScreenPoint(renderer.bounds.min).y);

            // method2:
            var heightInScreen = GetHeightInScreen(cam, renderer);
            var ratioInScren = heightInScreen * inv_SceneViewCamHeight;
            if (ratioInScren > lod.screenRelativeTransitionHeight)
            {
                return lodIDX;
            }
        }
    }

    return -1;
}

private static float GetHeightInScreen(Camera cam, Renderer renderer)
{
    var min = renderer.bounds.min;
    var max = renderer.bounds.max;
    // F = Front
    var FTL = new Vector3(min.x, max.y, min.z);
    var FTR = new Vector3(max.x, max.y, min.z);
    var FBR = new Vector3(max.x, min.y, min.z);
    var FBL = new Vector3(min.x, min.y, min.z);

    // Back
    var BTL = new Vector3(min.x, max.y, max.z);
    var BTR = new Vector3(max.x, max.y, max.z);
    var BBR = new Vector3(max.x, min.y, max.z);
    var BBL = new Vector3(min.x, min.y, max.z);

    // to screen space pos
    FTL = cam.WorldToScreenPoint(FTL);
    FTR = cam.WorldToScreenPoint(FTR);
    FBR = cam.WorldToScreenPoint(FBR);
    FBL = cam.WorldToScreenPoint(FBL);

    BTL = cam.WorldToScreenPoint(BTL);
    BTR = cam.WorldToScreenPoint(BTR);
    BBR = cam.WorldToScreenPoint(BBR);
    BBL = cam.WorldToScreenPoint(BBL);

    var maxY = FTL.y;
    maxY = Mathf.Max(FTR.y, maxY);
    maxY = Mathf.Max(FBR.y, maxY);
    maxY = Mathf.Max(FBL.y, maxY);

    maxY = Mathf.Max(BTL.y, maxY);
    maxY = Mathf.Max(BTR.y, maxY);
    maxY = Mathf.Max(BBR.y, maxY);
    maxY = Mathf.Max(BBL.y, maxY);

    var minY = FTL.y;
    minY = Mathf.Min(FTR.y, minY);
    minY = Mathf.Min(FBR.y, minY);
    minY = Mathf.Min(FBL.y, minY);

    minY = Mathf.Min(BTL.y, minY);
    minY = Mathf.Min(BTR.y, minY);
    minY = Mathf.Min(BBR.y, minY);
    minY = Mathf.Min(BBL.y, minY);

    return maxY - minY;
}
jave lin
  • 1
  • 1
-2
public class TestingStoreLODGroupCurLODLevel : MonoBehaviour
{
    public bool[] lodVisibleArr;
    public int GetCurLOD()
    {
        if (lodVisibleArr != null)
        {
            var len = lodVisibleArr.Length;
            for (int i = 0; i < len; i++)
            {
                if (lodVisibleArr[i])
                {
                    return i;
                }
            }
        }
        return -1;
    }
}

public class TestingCheckVisibleChanged : MonoBehaviour
{
    public int belongLOD;
    public TestingStoreLODGroupCurLODLevel storeLOD;
    private void OnBecameInvisible()
    {
        if (storeLOD)
        {
            storeLOD.lodVisibleArr[belongLOD] = false;
        }
    }

    private void OnBecameVisible()
    {
        if (storeLOD)
        {
            storeLOD.lodVisibleArr[belongLOD] = true;
        }
    }
}

    private void SetupCheckLODInfo(GameObject go)
    {
        var lodGroup = go.GetComponent<LODGroup>();
        if (lodGroup == null)
        {
            return;
        }

        var storeLODComp = go.GetComponent<TestingStoreLODGroupCurLODLevel>();
        if (storeLODComp != null)
        {
            return;
        }

        storeLODComp = go.AddComponent<TestingStoreLODGroupCurLODLevel>();
        var lods = lodGroup.GetLODs();
        for (int lodIDX = 0; lodIDX < lods.Length; lodIDX++)
        {
            var lod = lods[lodIDX];
            var renderers = lod.renderers;
            for (int rendererIDX = 0; rendererIDX < renderers.Length; rendererIDX++)
            {
                var renderer = renderers[rendererIDX];
                var checkVisibleComp = renderer.gameObject.GetComponent<TestingCheckVisibleChanged>();
                if (checkVisibleComp == null)
                {
                    checkVisibleComp = renderer.gameObject.AddComponent<TestingCheckVisibleChanged>();
                }
                checkVisibleComp.belongLOD = lodIDX;
                checkVisibleComp.storeLOD = storeLODComp;
            }
        }

        storeLODComp.lodVisibleArr = new bool[lods.Length]; 
    }

    private void UnSetupCheckLODInfo(GameObject go)
    {
        var lodGroup = go.GetComponent<LODGroup>();
        if (lodGroup == null)
        {
            return;
        }
        var storeLODComp = go.GetComponent<TestingStoreLODGroupCurLODLevel>();
        if (storeLODComp != null)
        {
            GameObject.Destroy(storeLODComp);
        }

        var lods = lodGroup.GetLODs();
        for (int lodIDX = 0; lodIDX < lods.Length; lodIDX++)
        {
            var lod = lods[lodIDX];
            var renderers = lod.renderers;
            for (int rendererIDX = 0; rendererIDX < renderers.Length; rendererIDX++)
            {
                var renderer = renderers[rendererIDX];
                var checkVisibleComp = renderer.gameObject.GetComponent<TestingCheckVisibleChanged>();
                if (checkVisibleComp != null)
                {
                    GameObject.Destroy(checkVisibleComp);
                }
            }
        }
    }
jave lin
  • 1
  • 1
  • Please edit your answer to explain what you code does and how it solves the problem – BDL Jul 03 '21 at 13:00