Finding the concave hull of a rigidbody

I want a fast way to calculate the concave hull of a rigidbody. By that i mean take all of its colliders (which individually are convex) and compute the concave shape that they form together.

My only idea currently is to take a primitive sphere mesh using GameObject.CreatePrimitive that encompasses the object, and ray casting from each vertex inwards, then moving the vertex to that point (transforming it to local space).

This of course seems very expensive, and potentially not very accurate, not to mention that some of the ray casts may miss. I have tried casting 10s of thousands of rays with raycast commands using the jobs system and its suprisingly quick for the sheer number of them, and given I will likely only ever run this in the editor, I can tolerate it being a bit slow, but I cant help but feel theres a better solution.

EDIT: If possible, I would to have a perfect representation of the concave hull in a Mesh object (sphere and capsule colliders would of course need to be approximated, but convex meshs and box colliders wouldnt need to be).

Also, if possible I would like to be able to get the concave hull from the actuall mesh renderers. I dont even know where to begin for that.

Yes. For a start, you can enumerate the mesh vertices and cast a ray to each vertex. If you have primitive colliders already you should instead cast rays to each collider vertices.

For a box collider this would be defined by its bounds, and for capsule/sphere colliders you should use the bounds for a first early out test and then may have to calculate the size and location of the (half) sphere in order to find the closest/farthest point on the sphere (there are algorithms for that too) so that you can limit to one raycast per (half) sphere.

If you have a MeshCollider you can use its sharedMesh vertices.

There are concave hull (approximation) algorithms and repositories that can do this for you. Just two I found via google, can’t say anything about them and there’s definitely more (also look in the store):

1 Like