Search Unity

Bad performance with lots of static colliders

Discussion in 'Physics for ECS' started by Romulas, Apr 16, 2020.

  1. Romulas

    Romulas

    Joined:
    Oct 5, 2012
    Posts:
    3
    Hello,

    I am using 2020.1.0b2.3333 and Unity Physics 0.3.1
    In the scene I have 20k static boxes with 9 TerrainColliders. Terrain sizes are 129x129. I thought this wouldn't be a lot to handle.

    First issue is BoundingVolumeHierarchy:BuildFirstNLevelsJob takes very long to finish. All safety checks are off. If I remove the boxes I have 3ms per frame rather than 38ms. So maybe I should combine the box colliders somehow?

    How do you create scenes with lots of static colliders?
     
  2. Romulas

    Romulas

    Joined:
    Oct 5, 2012
    Posts:
    3
    Well, CompoundCollider works. :)
     
    petarmHavok likes this.
  3. petarmHavok

    petarmHavok

    Joined:
    Nov 20, 2018
    Posts:
    461
    Was just about to suggest that, group your static geometry that doesn't change to a compound collider.
     
  4. rz_0lento

    rz_0lento

    Joined:
    Oct 8, 2013
    Posts:
    2,361
    I'm curious, it's not possible to do this automatically when the collider is marked static? Having to assemble static colliders manually as compound feels counter intuitive.

    Is there some use case where this wouldn't be preferred? Like do we have limitations on what we can add to and remove from compound colliders at runtime?
     
    KwahuNashoba and Romulas like this.
  5. petarmHavok

    petarmHavok

    Joined:
    Nov 20, 2018
    Posts:
    461
    You mean something like all static colliders being linked to a compound automatically? No, that isn't available and there is a good reason - if you change a single static body in the scene, your compound collider would have to be instantiated again, built from scratch. Adding/removing instances in a compound collider is not supported and results in rebuilding of the whole thing. And there could be a lot to rebuild.

    Our usual guideline is as follows - add all static colliders that never change to a single compound collider, and keep static colliders that change (come and go) separate. That will optimize performance of the collision world building algorithms.
     
    rz_0lento likes this.
  6. steveeHavok

    steveeHavok

    Joined:
    Mar 19, 2019
    Posts:
    481
    There is a partial automatic optimization. Back in 0.2.3 we added the following:

    Implicitly static shapes (i.e. those without a PhysicsBodyAuthoring or Rigidbody) in a hierarchy under a GameObject with StaticOptimizeEntity are now converted into a single compound PhysicsCollider on the entity with the Static tag. If your queries or contact events need to know about data associated with the entities from which these leaf shapes were created, you need to explicitly add PhysicsBodyAuthoring components with static motion type, in order to prevent them from becoming part of the compound.
     
    rz_0lento likes this.
  7. Romulas

    Romulas

    Joined:
    Oct 5, 2012
    Posts:
    3
    I think if we are talking about performance by default, then this compound collider optimization should be default for all static colliders, providing us collision events with an entity id at the same time.
    Making a huge scene and not knowing where you collide, sounds bad.
     
  8. rz_0lento

    rz_0lento

    Joined:
    Oct 8, 2013
    Posts:
    2,361
    I can totally understand the logic behind it not happening by default but I don't agree either.

    If you actually need to add/remove static collisions at runtime, then you can setup the compound collision yourself or conf that separately, that should be the exception and not the other way around IMHO.