Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Procedurally generate meshes only when needed by LoD Group

Discussion in 'Scripting' started by Caliber, Apr 25, 2021.

  1. Caliber

    Caliber

    Joined:
    Feb 11, 2018
    Posts:
    85
    I use procedurally generated meshes for terrain, and would like to use LoD Groups.
    But I'd prefer not to generate all LoD meshes at once, since the camera may never get close enough to some meshes to require LoD0 or Lod1.
    Is it possible to only generate a low res mesh at first, and then somehow have LODGroup request higher res meshes only when they are needed?
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,336
    Yes.

    Seriously, roll some code, give it a try. You know the criteria for when you need the higher LOD, see what you can come up with. Test small, iterate, measure, see what happens.

    But the answer is "Yes."
     
  3. Caliber

    Caliber

    Joined:
    Feb 11, 2018
    Posts:
    85
    I mean, if I had the source code to LODGroup and could alter it freely, that would be a simple task. But since I don't, I feel like I would pretty much have to write my own LoD manager to accomplish what I want, am I wrong?
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,336
    Wouldn't you just either put no mesh or perhaps a tiny stub mesh into LOD group things you haven't decided to generate yet? And when the Uniy LOD manager turns them on, you would make the mesh then, or else make it early enough that it doesn't cause a stutter? By now I imagine you have tried SOMETHING, right??
     
  5. Caliber

    Caliber

    Joined:
    Feb 11, 2018
    Posts:
    85
    Sure, no mesh, that part is obvious.

    This is the heart of the problem. How would I know that the LODGroup tried using a mesh that doesn't exist?
    I could attach a script that checks it every single frame in Update() on every single GameObject with a LODGroup, but that would be very ineffective.

    What I did do was actually write my own (very basic) LOD Manager, but I'd save myself a lot of work (and probably improve framerate) if I could solve this one issue with LODGroup.
     
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,336
    I hadn't fiddled with LOD in a while and I thought the LODGroup actually enabled/disabled the GameObjects so you could just hook the OnEnable() / OnDisable(), but this is not the case.

    It also appears to not manipulate the MeshRenderer's enabled status so that isn't useful to observe.

    However, you can hook this method to know when something is going to be considered for rendering:

    https://docs.unity3d.com/ScriptReference/MonoBehaviour.OnWillRenderObject.html

    I just tried it by putting that on the three LOD sub-objects and during running, only the ones involved are called.
     
  7. MDADigital

    MDADigital

    Joined:
    Apr 18, 2020
    Posts:
    2,198
    Im pretty sure it's handled natively to save interops. I wouldn't put a MonoBehavior with OnWillRender for thousands of objects. Depends on scale of things.
     
    Kurt-Dekker likes this.