Search Unity

Slow ConvexCollider Creation

Discussion in 'Physics for ECS' started by Inter-Illusion, May 27, 2021.

  1. Inter-Illusion

    Inter-Illusion

    Joined:
    Jan 5, 2014
    Posts:
    598
    I'm creating some convexCollider out from dynamically generated meshes, but it turns out to be a very very slow process.
    Using the same mesh with an standard unity MeshCollider set to convex, it only takes 1-2ms while using DOTS Physics it takes 700-900ms

    Here is an example of the code I'm using for comparison:
    Code (csharp):
    1.  
    2. {
    3.     using var clock = new DebugClock("DOTS Create Convex");
    4.     MyCollider = ConvexCollider.Create(points, default, CollisionFilter.Default);
    5. }
    6.  
    7. MeshCollider c = transform.GetComponent<MeshCollider>();
    8. {
    9.     using var clock = new DebugClock("Unity Create Convex");
    10.     c.convex = true;
    11.     c.sharedMesh = mesh;
    12.     c.enabled = true;
    13. }
    14.  
    Here are some of the performance logs

    upload_2021-5-27_11-7-10.png

    When wrapping the ConvexCollider.Create into a job and enabling Burst, the numbers get better, but still, way off.

    Code (csharp):
    1.  
    2. NativeArray<BlobAssetReference<Unity.Physics.Collider>> col = new NativeArray<BlobAssetReference<Collider>>(1, Allocator.TempJob);
    3. {
    4.     using var clock = new DebugClock("DOTS Burst Create Convex");
    5.     var job = new JOB_BuildConvex
    6.     {
    7.         collider = col,
    8.         points = points
    9.     };
    10.     job.Schedule().Complete();
    11.     MyCollider = col[0];
    12. }
    13.  
    14. MeshCollider c = transform.GetComponent<MeshCollider>();
    15. {
    16.     using var clock = new DebugClock("Unity Create Convex");
    17.     c.convex = true;
    18.     c.sharedMesh = mesh;
    19.     c.enabled = true;
    20. }
    21.  
    22. .......
    23.  
    24. [BurstCompile]
    25. struct JOB_BuildConvex : IJob
    26. {
    27.     public NativeArray<BlobAssetReference<Unity.Physics.Collider>> collider;
    28.     public NativeArray<float3> points;
    29.    
    30.     public void Execute()
    31.     {
    32.         collider[0] = ConvexCollider.Create(points, default, CollisionFilter.Default);
    33.     }
    34. }
    35.  
    upload_2021-5-27_11-11-29.png

    Its there a way to speed up creating the convex colliders?
     
  2. Occuros

    Occuros

    Joined:
    Sep 4, 2018
    Posts:
    300
    It's the same for mesh collider creation.

    For us, it's actually so slow that we can't use ECS for anything procedurally generated. The best approach is currently to use Monobehaviours with bursted jobs.

    I wonder if that will be addressed in a future release?
     
  3. snacktime

    snacktime

    Joined:
    Apr 15, 2013
    Posts:
    3,356
    Convex is slowish.

    I think the simplest solution is likely to split the triangles into multiple arrays to get more parallelism, then at the end create a single compound from the resulting individual convex colliders, which is quite fast. Spread it over multiple frames if needed. Not simple per say but can't think of something better.

    Too bad they don't provide the collider vertices/triangles data from Physx colliders, or you could just bake using Physx and feed that to DOTS. Seems like a very practical solution to me although they probably don't want to create that type of dependency.