Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Discussion Approach for vast amount of rock objects with colliders?

Discussion in 'World Building' started by NanushTol, Jun 20, 2023.

  1. NanushTol

    NanushTol

    Joined:
    Jan 9, 2018
    Posts:
    138
    I have a custom procedural terrain generator, and I'm debating with myself what would be the best approach to scatter rocks around the terrain.
    The requirements:
    - Object count: 100K +
    - Colliders are needed
    - Control rotation on all axis (so the terrain system trees prototypes are not an option)
    - With LODs
    - No need for light probes or light baking
    - Cast and receive shadows

    Option 1:
    I thought about instantiating prefabs with the colliders and a script that holds the information, and then using a manager to use GPU instancing with
    Graphics.DrawProcedural
    , with a compute shader to handle the camera culling, and in it also decide which object(rock) should have an active collider, and which LOD to choose.
    The objects will not have a mesh renderer or a mesh filter, but just a reference to the object different LODs

    Option 2:
    Instantiating the object and use
    StaticBatchingUtility
    , handling the activation of the colliders, the same way like option 1, letting unity handle the culling and LODs

    Does anyone knows of any pitfalls I might encounter with this options?
    Any other option that might be better ?
    An asset store tool that can make handle this ?
     
  2. jjejj87

    jjejj87

    Joined:
    Feb 2, 2013
    Posts:
    1,117
    I think 100k+ mesh colliders might be a problem, depends on how many and the number of collider triangles though. You might have to pool the colliders, or use LOD colliders to keep things performant. With both option 1 and 2 you will have lag spikes assuming that you are traversing the terrain at human speeds. I did a similar project a few years back and had to resolve to 250m x 250m terrain chunks and about 5 variants of rock forms. Had a lot of performance issues here and there. I sort of did make it work in the end but with many tweaks that I do not want to do again.

    I basically used chunked instancing grid that would show rocks within the camera frustrum, then for colliders I baked the 250m x 250m terrain with rocks, creating a separate heightmap (that had rocks with) for an invisible terrain. The best part of it was that it was literally lag free. Worst part was work flow and pain.

    Hope this helps!