- Home /
Get the triangles of a convex mesh collider
I would like to retrieve via script the mesh that is used for collisions of an object that has a convex mesh collider. If I try this on the particular object:
MeshCollider meshCol = GetComponent<MeshCollider>();
Mesh mesh = meshCol.sharedMesh;
I get the exactly same mesh that I can get from mesh filter component on the object, (or at least it has the same triangle count on the one I tested). The original object is not convex so I am quite sure this is not the convex mesh that is used for collisions. I needed this to approximate volume of an object using this aproach: link
But in my case the mesh triangle count is too high and it takes several seconds to finish calculating.
Is it even possible to get this convex mesh?
Answer by Bunny83 · Apr 25, 2021 at 01:09 AM
AFAIK Unity does not provide any way to read the convex hull that PhyX generates for the collision detection. Keep in mind that the convex hull is not meant as a simplification but just to make the shape convex. Yes, PhysX limits the number of triangles in the convex hull so the resulting shape usually has less triangles / faces than a huge complex mesh. Though as I said, triangle reduction is not really a goal of the convex hull.
Note that the mesh does not need to be convex in order to calculate the volume. It just needs to be a closed mesh. I explained why it works for non convex meshes as well over here.
Does your mesh actually change at runtime? If not you can easily pre-calculate the volume at edit time and just store it alongside the mesh.
How many triangles do we talk about when the volume calculation takes seconds? That must be millions... If not it's possible that you did some other performace critical mistake, like reading the vertices or triangles properties in an array instead of caching it in a local variable. Do you have a snippet of the code you're using? The code provided in the answer to the SO question has such a mistake in it. Even though they cache the vertices and triangle arrays, they still use mesh,triangle.length
in the for loop condition. This will create a new triangles array every for loop iteration.
mesh,triangle.length
thx, I actually had the same condition in my for loop, it works great now.
PhysX limits the number of triangles in the convex hull so the resulting shape usually has less triangles / faces than a huge complex mesh. Though as I said, triangle reduction is not really a goal of the convex hull.
Btw the reason I was trying to get the convex mesh of the collider was because I was under the impression it's face count is limited to 255 (my mesh filters are 40k triangles, so this I assumed would have been a big speed up). And yes this needed to be run at runtime, since the meshes change.
Your answer
Follow this Question
Related Questions
Editing Mesh Colliders? Creating a usable pot/cup 1 Answer
All colliders no longer work. 0 Answers
Terrain and character navigation 0 Answers
Colliders on this spider 1 Answer
Collision with Plane fails 0 Answers