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 (IN-43792) ParallelCreateContactsJob bad performance

Discussion in 'Physics for ECS' started by mops398, Jun 12, 2023.

  1. mops398

    mops398

    Joined:
    Oct 20, 2017
    Posts:
    8
    Hi!

    We have a very strange performance issue in ParallelCreateContactsJob.
    (use com.unity.physics-1.0.10, editor 2023.2.0a17)

    In attached screenshots, you can see that colliders dont collide (2nd picture), only aabs overlapping (1st picture). But in results we have a 1ms ParallelCreateContactsJob in one thread(3rd picture).



    Could you explain please, is it normal? Can we reduce it somehow
     

    Attached Files:

    • 1.jpg
      1.jpg
      File size:
      150.6 KB
      Views:
      217
    • 2.jpg
      2.jpg
      File size:
      315.4 KB
      Views:
      188
    • 3.jpg
      3.jpg
      File size:
      145.7 KB
      Views:
      188
    • 2.jpg
      2.jpg
      File size:
      315.4 KB
      Views:
      44
  2. daniel-holz

    daniel-holz

    Unity Technologies

    Joined:
    Sep 17, 2021
    Posts:
    210
    Just for comparison's sake, can you try and use convex colliders for these two objects and check the time consumption?

    I assume these are the only two collider objects in this test scene, right?
     
  3. mops398

    mops398

    Joined:
    Oct 20, 2017
    Posts:
    8
    Thank for prompt answer!

    Yes, We have only these two colliders in scene. One of this (carrier) - convex hull, island - mesh collider.

    I tried change island collider to convex hull and this job executed for 0.1ms
     
  4. daniel-holz

    daniel-holz

    Unity Technologies

    Joined:
    Sep 17, 2021
    Posts:
    210
    Would you mind reporting this as a bug using our issue intake system and providing just this small scene snippet as a project so that we can reproduce this?
    There could be a problem with the BV tree in the mesh colliders which is potentially sensitive to object sizes, based on other feedback we have received. The size can lead to differences in time consumption based on this information.

    If you try scaling both objects up by a 100, and still use the mesh collider for the rock, do you get better results?
     
  5. mops398

    mops398

    Joined:
    Oct 20, 2017
    Posts:
    8
    Created a bug report CASE IN-43792. attached sample project.

    I tried to scale both object to 100 and is didnt have any performance effect
     
    daniel-holz likes this.
  6. daniel-holz

    daniel-holz

    Unity Technologies

    Joined:
    Sep 17, 2021
    Posts:
    210
    Hi @mops398 ,

    I am able to reproduce this issue and from my first investigation I can say that the shear size of the mesh you are using here is likely overwhelming the middle phase (a bounding volume hierarchy) used in meshes to find candidate triangles against which actual intersection tests need to be done in a collider pair.

    In the MeshCollider, the mesh has a way too high resolution to be useable for real-time.
    See here:
    upload_2023-8-10_15-34-41.png
    I understand that the intention here was to use LOD2 of this mesh, but that doesn't seem to work in the collider creation, leading to this excessive triangle count.

    When switching to a convex mesh, the LOD2 seems to be used, producing this outcome. That the performance issue then goes away makes sense.
    upload_2023-8-10_15-41-17.png

    I will have a look at why the correct data is not being passed into the MeshCollider here.
     
  7. daniel-holz

    daniel-holz

    Unity Technologies

    Joined:
    Sep 17, 2021
    Posts:
    210
    After further investigation, it appears that the issue could be at the authoring level (PhysicsShapeAuthoring), where rather than choosing the Custom Mesh instead of the render mesh present in the game object for collider creation, we choose both.

    Below, I replaced the Custom Mesh property with a simple box, and as you can see we still get a collider that contains the entire high resolution island mesh (from the Island_3 Mesh Filter component) now simply combined with the box (highlighted in the middle).

    upload_2023-8-10_15-48-1.png

    As a workaround, you can restructure your game object such that the game object that contains the PhysicsShape is a parent of the game object with the Mesh Filter for visualization.
    Setting the Custom Mesh to the LOD you want for the island will then create the expected low res physics mesh collider. Note that for this to work you still need to put a PhysicsShape side by side with the child game object that contains the Mesh Filter but deactivate the component. Otherwise the Mesh Filter of this game object will be picked up by the Physics Shape in the parent game object, leading to the same issue we had before.
    upload_2023-8-10_15-56-46.png
    upload_2023-8-10_15-56-5.png

    With this workaround, the performance issue disappears as you can see below.
    upload_2023-8-10_15-55-32.png
     
  8. daniel-holz

    daniel-holz

    Unity Technologies

    Joined:
    Sep 17, 2021
    Posts:
    210
    As an actual fix, which we will also put in the product, you can make the following modification to
    PhysicsShapeBakingSystem.cs
    located in your project.

    upload_2023-8-10_16-34-18.png