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. Join us on March 30, 2023, between 5 am & 1 pm EST, in the Performance Profiling Dev Blitz Day 2023 - Q&A forum and Discord where you can connect with our teams behind the Memory and CPU Profilers.
    Dismiss Notice

Resolved Unity Physics runtime mesh collider creation and modification

Discussion in 'DOTS Dev Blitz Day 2022 - Q&A' started by pbhogan, Dec 8, 2022.

  1. pbhogan

    pbhogan

    Joined:
    Aug 17, 2012
    Posts:
    371
    Unity Entities Graphics has some great convenience features to enable runtime mesh creation / modification:
    1. RenderMeshUtility.AddComponents for runtime mesh creation
    2. MaterialMeshInfo (and RegisterMesh / RegisterMaterial calls) for efficiently changing meshes at runtime
    Are there any plans to add similar helpers and concepts for DOTS Physics?

    Procedural terrain is a good use case. There is the need to create meshes at runtime and, if the terrain is modifiable at runtime, then there is a need to efficiently update mesh colliders at runtime.

    I've rolled my own "PhysicsUtility.AddComponents"-style helpers for creating a mesh collider at runtime by looking at the physics internals, but this is obviously quite fragile. Specifically, taking a Mesh (or MeshDataArray), ColliderFilter and (physics) Material and building a BlobAssetReference<Collider>. It sure would be nice to have a supported helper API to rely on for this. I have no idea if it's possible to change a collider efficiently at runtime without building a whole new collider, but I'd love if there was a way to build the collider asynchronously and do a MaterialMeshInfo-style ID swap efficiently.
     
    scottjdaley likes this.
  2. IsaacsUnity

    IsaacsUnity

    Unity Technologies

    Joined:
    Mar 1, 2022
    Posts:
    65
    Thanks for the question! We currently can't modify a compound collider after creation as it is read-only, although we understand this feature can be valuable in some cases. Aside from this feedback, if this feature and use case is important to you, do share more on our public roadmap.
     
    daniel-holz likes this.
  3. daniel-holz

    daniel-holz

    Unity Technologies

    Joined:
    Sep 17, 2021
    Posts:
    30
    Did you have a look at Physics.MeshCollider.Create()?

    Code (CSharp):
    1. public static BlobAssetReference<Collider> Create(NativeArray<float3> vertices, NativeArray<int3> triangles, CollisionFilter filter, Material material)
    If it's creation of a mesh you are looking for, this function should do the job. It doesn't take a Mesh as input directly but it's used in the MeshBlobsJob (in BaseShapeBakingSystem.cs) for asynchronous creation of BlobAssetReference<Collider>'s.
    This job extracts the vertices and triangles for each mesh in a MeshDataArray in parallel and creates a BlobAssetReference<Collider> for them.

    If I understand correctly, for starters you would like an officially supported API in some utilities class which does exactly that, but for a single Mesh.

    Please let me know if my understanding is correct.
     
  4. pbhogan

    pbhogan

    Joined:
    Aug 17, 2012
    Posts:
    371
    Yes, that's correct.

    My own implementation uses Physics.MeshCollider.Create() under the hood, and it pretty much does what you describe... it gets the MeshData and builds the vertices and triangles NativeArrays in a job and passes it to Physics.MeshCollider.Create() in another job.

    It's not that any of that is particularly difficult, but there are some fiddly bits dealing with the correct index format of the MeshData to build the triangles, handle all the sub-meshes, wrap it in jobs, etc. I definitely had to go dig into the physics codebase to figure out exactly what to do. It just seems like there should be something in the general public API that takes a Mesh, MeshData or MeshDataArray (along with the CollisionFilter and Material) and spits out a BlobAssetReference<Collider> and just abstracts away the internal complexity of these fairly common operations.

    My wishlist would be to add:

    Code (CSharp):
    1.  
    2. public static BlobAssetReference<Collider> Create( Mesh mesh, CollisionFilter filter, Material material );
    3. public static BlobAssetReference<Collider> Create( MeshData meshData, CollisionFilter filter, Material material );
    4. public static BlobAssetReference<Collider> Create( MeshDataArray meshDataArray, CollisionFilter filter, Material material );
    5.  
    ... maybe along with some simple jobs to wrap the three burst-able calls (NativeArrays, MeshData and MeshDataArray). Again, something that is trivial to write, but is just another thing that many people will have to do.

    Finally, in my PhysicsUtility class, I have it all wrapped in a call that has RenderMeshUtility.AddComponents() vibes, taking an Entity and EntityManager and adding the correct components. But, that's just gravy. :)
     
    daniel-holz likes this.
  5. daniel-holz

    daniel-holz

    Unity Technologies

    Joined:
    Sep 17, 2021
    Posts:
    30
    That sounds good. I agree.
    I will relay that request internally.