Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Detecting LOD changes?

Discussion in 'Scripting' started by mbaske, Oct 20, 2019.

  1. mbaske

    mbaske

    Joined:
    Dec 31, 2017
    Posts:
    473
    Hi - As far as I know, one can detect the current LOD of a gameobject by checking which of its renderers are visible. Is there a way to get notified of an LOD change, perhaps via an event? I want to procedurally generate meshes for different LODs, but only if and when they're actually needed. And I'd like to avoid constant checks for renderer visibility within Update(). Thanks!
     
  2. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,605
    If you are procedurally generating the meshes, why do you need access to the LOD?
    I assume you are generating a procedural world based on chunks (since otherwise you could most likely pre-generate the meshes and LOD levels and use the default LOD system)? If so, then you probably want to keep track of the meshes anyways, to disable far away chunks. So you'd already check their distance, which would enable you to decide based on that whether you need a new LOD version.

    Am i missing the point completely?
    As for your main question, i dont know, but i believe there is no such thing as an LOD change event. To my knowledge, LOD indices are calculated for each camera separately that views the object (which makes sense, since one object could have different LOD for different cameras). But again, take that with a grain of salt since i'm not sure.
     
  3. mbaske

    mbaske

    Joined:
    Dec 31, 2017
    Posts:
    473
    Thanks Yoreki. I am actually using the default LOD system. It's just that my close-up meshes are large and expensive to generate, so I'd rather not create everything up front. Anyway, I just noticed there's a MonoBehaviour.OnBecameVisible() method, I'll give that a try.
     
  4. Wilhelm_LAS

    Wilhelm_LAS

    Joined:
    Aug 18, 2020
    Posts:
    52
    Bump.
    OnBecameVisible() and OnBecameInvisible() calls are not ordered.
    Imagine you have 2 Renderers A and B.

    Lets say, A became visible when you press play first time and OnBecameVisible() is called on the A.
    Now if you move away, it will call OnBecameVisible() at B.
    After that it will call OnBecameInvisible() at A.
    So it goes: OnVisible(A)>OnVisible(B)>OnInvisible(A)

    I wan't to manage single object inside that visibility methods. I don't wan't to use LODGroup.GetLODs() and create unnecessary cycle for that.

    How could i achieve OnVisible(A) > OnInvisible(A) > OnVisible(B)?