Can I catch "Gu::ConvexMesh::loadConvexHull: convex hull init failed!" error?

We are experimenting with Fracturing and Destruction extension and while working on our model, we are getting this “Gu::ConvexMesh::loadConvexHull: convex hull init failed!” error (see the stack below). I can step into the where the error happens and it is at the line where we are setting convex flag to true.

MeshCollider cldr = chunk.gameObject.AddComponent<MeshCollider>();
cldr.convex = true;

Ideally, I would like to catch that error with some information on what object it occurs, so I can provide a meaningful message to the content creator. However, there are no return values here obviously and the exception is not thrown.

Does anyone know about any possible solutions or workarounds to the problem?

The full stack:

Gu::ConvexMesh::loadConvexHull: convex hull init failed! Try to use the PxConvexFlag::eINFLATE_CONVEX flag. (see PxToolkit::createConvexMeshSafe)
UnityEngine.MeshCollider:set_convex(Boolean)
UltimateFracturing.Fracturer:ComputeChunkColliders(FracturedObject, ProgressDelegate) (at Assets/Ultimate Game Tools/Fracturing/Scripts/Fracture.cs:1446)
UltimateFracturing.Fracturer:FractureToChunksBSPMultipleInteriors(FracturedObject, Boolean, List`1&, ProgressDelegate) (at Assets/Destruction Testing/Scripts/FractureAlgorithms.cs:627)
UltimateFracturing.Fracturer:ACEFracture(FracturedObject, Boolean, List`1&, ProgressDelegate) (at Assets/Destruction Testing/Scripts/FractureAlgorithms.cs:19)
UltimateFracturing.Fracturer:FractureToChunks(FracturedObject, Boolean, List`1&, ProgressDelegate) (at Assets/Ultimate Game Tools/Fracturing/Scripts/Fracture.cs:196)
FracturedObjectEditor:ComputeChunks(FracturedObject) (at Assets/Destruction Testing/Scripts/Editor/FracturedInspectorExtension.cs:122)
FracturedObjectEditor:FixedInspectorGUI() (at Assets/Destruction Testing/Scripts/Editor/FracturedInspectorExtension.cs:514)
FracturedObjectEditor:OnInspectorGUI() (at Assets/Ultimate Game Tools/Fracturing/Editor/FracturedObjectEditor.cs:335)
UnityEditor.DockArea:OnGUI()

As far as i know you can’t “catch” the error since it doesn’t seem to be an exception, but a warning / error from native C++ land.

However there are only two reasons why Unity / Physx might fail to initialize the convex hull:

  • The mesh doesn’t have any volume. So any flat mesh like a single quad or triangle will cause that error.
  • The mesh is too complex so the 255 triangles aren’t enough to construct the convex hull.

Note: In most cases it’s reason No1. No2 is quite rare. The convex hull for most meshes has way less triangles than the original mesh. However if the mesh has itself a lot vertices on the convex hull itself this error can happen as well.

So to check No1, just make sure your mesh’s triangles don’t lay in the same plane. For that you can use Unity’s Plane struct. Just use the corners of the first triangle to create a Plane. Now you can simply iterate the remaining vertices and do a distance check with the Plane. You need at least one that has a significant distance from the initial plane so the mesh does have some “depth”.

There’s no real solution for No2 besides to use a custom convex hull algorithm