Search Unity

  1. Unity Asset Manager is now available in public beta. Try it out now and join the conversation here in the forums.
    Dismiss Notice

MeshCollider.Create freezes the game

Discussion in 'Physics Previews' started by sietse85, Jun 12, 2020.

  1. sietse85

    sietse85

    Joined:
    Feb 22, 2019
    Posts:
    99
    Hi all,

    I am struggling with this issue for a while now and i hope that anyone can point me into the right direction here.

    I am creating a mesh collider with code.
    Code (CSharp):
    1. Unity.Physics.MeshCollider.Create(tmpVerts, tmpTriangles);
    When this line is run, the game freezes up. For a terrain chunk that is 40x40 vertices in size this takes the game up to 28 seconds to create this mesh collider. I am wondering what i am doing wrong.
    tmpVerts and tmpTriangles are simple arrays of int3 and vector3.

    I can't figure out why this major freeze is happening. Do anyone of you have an idea? Any advice would be greatly appreciated.
     
  2. AlanMattano

    AlanMattano

    Joined:
    Aug 22, 2013
    Posts:
    1,501
    Did you try in an empty scene to isolate the problem?
    Is it inside a prefab?
     
  3. sietse85

    sietse85

    Joined:
    Feb 22, 2019
    Posts:
    99
    Here is the exact code i tried this with in a empty scene
    Code (CSharp):
    1.  void Start()
    2.     {
    3.         _world = World.DefaultGameObjectInjectionWorld;
    4.         terrainColliders = new Dictionary<Vector2Int, BlobAssetReference<Unity.Physics.Collider>>();
    5.         _meshGenerator = new MeshGenerator();
    6.         BlobAssetStore store = new BlobAssetStore();
    7.         _em = _world.EntityManager;
    8.         GameObjectConversionSettings settings = GameObjectConversionSettings.FromWorld(_world, store);
    9.         Entity e = _em.Instantiate(GameObjectConversionUtility.ConvertGameObjectHierarchy(chunk, settings));
    10.         GeneratedMeshData data = _meshGenerator.GenerateTerrainMesh(new Vector2Int(0, 0));
    11.  
    12.         NativeArray<float3> tmpVerts = new NativeArray<float3>(data.vertices.Length, Allocator.Temp);
    13.         for (int i = 0; i < tmpVerts.Length; i++)
    14.         {
    15.             tmpVerts[i] = data.vertices[i];
    16.         }
    17.  
    18.         NativeArray<int3> tmpTriangles = new NativeArray<int3>(data.triangles.Length, Allocator.Temp);
    19.  
    20.         for (int i = 0; i < data.triangles.Length / 3; i++)
    21.         {
    22.             tmpTriangles[i] = new int3(data.triangles[i * 3], data.triangles[i * 3 + 1],
    23.                 data.triangles[i * 3 + 2]);
    24.         }
    25.  
    26.         terrainColliders.Add(data.point, BlobAssetReference<Unity.Physics.Collider>.Null);
    27.         terrainColliders[data.point] = Unity.Physics.MeshCollider.Create(tmpVerts, tmpTriangles);
    28.  
    29.         _em.AddComponentData(e, new PhysicsCollider
    30.         {
    31.             Value = terrainColliders[data.point]
    32.         });
    33.  
    34.         _em.AddComponentData(e, new PhysicsDebugDisplayData
    35.         {
    36.             DrawColliders = 1,
    37.         });
    38.     }
    I tried an empty scene with only 1 chunk generated. The problem stays the same.
     
    Last edited: Jun 14, 2020
  4. petarmHavok

    petarmHavok

    Joined:
    Nov 20, 2018
    Posts:
    461
    Can you try if this freeze happens with Burst disabled?

    Your code should be slow (since it's not in a Burst compiled job), bot not this slow I suppose. I'm just wondering if there's a synchronous Burst compilation happening on some of the backend code. For example, our PhysicsShapeAuthoringEditor.cs has a synchronously compiled Burst job to create a mesh for display. That could be causing the freeze.

    Then again, if this is instantiated from code, it shouldn't happen. But please try and let me know.
     
    Last edited: Jun 15, 2020
  5. sietse85

    sietse85

    Joined:
    Feb 22, 2019
    Posts:
    99
    I tried it with burst disabled, no change. It still takes the same amount of time to generate the collider. What more can i try?
     
  6. petarmHavok

    petarmHavok

    Joined:
    Nov 20, 2018
    Posts:
    461
    Can you try wrapping this Create() in a BurstCompiled job and see the performance then?
     
  7. sietse85

    sietse85

    Joined:
    Feb 22, 2019
    Posts:
    99
    Code (CSharp):
    1. void Start()
    2.     {
    3.         _world = World.DefaultGameObjectInjectionWorld;
    4.         terrainColliders = new Dictionary<Vector2Int, BlobAssetReference<Unity.Physics.Collider>>();
    5.         _meshGenerator = new MeshGenerator();
    6.         BlobAssetStore store = new BlobAssetStore();
    7.         _em = _world.EntityManager;
    8.         GameObjectConversionSettings settings = GameObjectConversionSettings.FromWorld(_world, store);
    9.         e = _em.Instantiate(GameObjectConversionUtility.ConvertGameObjectHierarchy(chunk, settings));
    10.         GeneratedMeshData data = _meshGenerator.GenerateTerrainMesh(new Vector2Int(0, 0));
    11.  
    12.         NativeArray<float3> tmpVerts = new NativeArray<float3>(data.vertices.Length, Allocator.Temp);
    13.         for (int i = 0; i < tmpVerts.Length; i++)
    14.         {
    15.             tmpVerts[i] = data.vertices[i];
    16.         }
    17.  
    18.         NativeArray<int3> tmpTriangles = new NativeArray<int3>(data.triangles.Length, Allocator.Temp);
    19.  
    20.         for (int i = 0; i < data.triangles.Length / 3; i++)
    21.         {
    22.             tmpTriangles[i] = new int3(data.triangles[i * 3], data.triangles[i * 3 + 1],
    23.                 data.triangles[i * 3 + 2]);
    24.         }
    25.  
    26.         terrainColliders.Add(data.point, BlobAssetReference<Unity.Physics.Collider>.Null);
    27.        
    28.         createJob = new GenerateColliderJob
    29.         {
    30.             vertices = tmpVerts,
    31.             triangles = tmpTriangles,
    32.             point = data.point
    33.         };
    34.  
    35.         handle = createJob.Schedule();
    36.     }
    37.  
    38.     // Update is called once per frame
    39.     void Update()
    40.     {
    41.         if (!jobran)
    42.         {
    43.             handle.Complete();
    44.             terrainColliders[createJob.point] = createJob.collider;
    45.             _em.AddComponentData(e, new PhysicsCollider
    46.             {
    47.                 Value = terrainColliders[createJob.point]
    48.             });
    49.             _em.AddComponentData(e, new PhysicsDebugDisplayData
    50.             {
    51.                 DrawColliders = 1,
    52.             });
    53.             jobran = true;
    54.         }
    55.     }
    56. }
    57.  
    58.  
    59. struct GenerateColliderJob : IJobBurstScheduable
    60. {
    61.     public BlobAssetReference<Unity.Physics.Collider> collider;
    62.     public NativeArray<float3> vertices;
    63.     public NativeArray<int3> triangles;
    64.     public Vector2Int point;
    65.    
    66.     public void Execute()
    67.     {
    68.         collider = Unity.Physics.MeshCollider.Create(vertices, triangles);
    69.     }
    70. }
    Quickly coded this, but the result is still the same
     
  8. petarmHavok

    petarmHavok

    Joined:
    Nov 20, 2018
    Posts:
    461
    Is there you could provide vertices and triangles buffer so that I can give this a go locally and check?
     
  9. sietse85

    sietse85

    Joined:
    Feb 22, 2019
    Posts:
    99
    I think i found the issue ^^ never wait to long to use the debugger haha, was about to copy the values for you when i took notice.
     
    petarmHavok likes this.
  10. sietse85

    sietse85

    Joined:
    Feb 22, 2019
    Posts:
    99
    So fixed the triangle array size and all works smooth now :) thanks to you also @petarmHavok
     
  11. petarmHavok

    petarmHavok

    Joined:
    Nov 20, 2018
    Posts:
    461
    My pleasure, glad it was this simple! :)
     
    sietse85 likes this.