Search Unity

Best practices for ECS + LOD groups created procedurally?

Discussion in 'Entity Component System' started by pbhogan, Mar 29, 2022.

  1. pbhogan

    pbhogan

    Joined:
    Aug 17, 2012
    Posts:
    384
    I'm hoping someone can point me at some best practices with this specific scenario:
    • I'm generating terrain procedurally.
    • Each terrain chunk is an entity.
    • For each terrain chunk, I want to have multiple LOD meshes.
    Currently, I have this working well without LOD. Initially the terrain chunk entity is created virtually empty. As data is generated asynchronously, it gets components added for a render mesh and physics collider. It's common to have "empty" terrain chunks in terms of rendering/physics (sky, or below ground).

    With a single mesh per chunk, the process is relatively simple and I can just use RenderMeshUtility.AddComponents() to add the appropriate rendering components. But to my knowledge, there is no such helper for setting up LOD groups.

    I could try to write a helper function myself (as I have had to for the physics components), however, it appears to be a rather complex setup of components for LOD groups and knowing which I need to create and what is created automatically, and maintaining the precise setup for it going forward is nontrivial.

    Another option I considered is creating a prefab entity from a GameObject with an LODGroup on it and then instantiating and populating the child meshes at runtime as procedural data comes in. Since the conversion to entity prefab is automatic, this is much easier to maintain. An additional advantage is easy authoring for tweaking things in the editor. But there are a few issues:
    • You can't leave empty meshes on MeshFilter or PhysicsShape. GameObject conversion will just skip converting those components. I can get around this with a degenerate mesh (a single triangle with vertices equal), though I'm unsure of potential issues this may cause and it seems kludgy.
    • Unlike before, every terrain chunk entity now has a full set of components instead of empty terrain chunks being, well, empty. Perhaps not a big performance hit, but not ideal.
    Does anyone have any recommendations for best practices working with LOD groups in cases where things need to be created procedurally?

    If anyone from Unity is reading this, I hope you consider some way to apply prefabs as templates that need elements filled in. I have no idea what that API would look like, but it would be super useful to instantiate a prefab with provided custom materials, meshes, etc. or even apply it to an existing entity where only the missing components are added.