How can you make a MeshRenderer mesh Convex at runtime (not Collider!)

Hi,

I’m looking for a way to render a Mesh at runtime, but as a convex version of the mesh.

Either by:

  • Converting the mesh at runtime to a
    convex version, then applying the
    converted mesh
  • Rendering the mesh as convex
    without converting the mesh at
    all?

The purpose here is to render a mesh to represent how a convex collider could look, but not actually being a collider. Instead rendering a normal mesh with convex settings so that a colider-less object can be shown in testing.

Well, Unity calculates a convex hull representation of the provided mesh for collider use. However those colliders have several limitations. Namely they are restricted to 256 vertices (at least they always had). As far as I know Unity does not provide any way to access the generated mesh data. Also the generated mesh data is not a “normal” mesh so there would be no UV coordinates as the generated geometry is for the physics system to detect collisions. Since we don’t know what approach Unity is using to calculate the convex hull, it’s essentially impossible to recreate exactly.

Even when you had your own convex hull algorithm and converting the mesh yourself, when using it as collider, Unity would still re-calculate the convex hull of your provided mesh which may not look the same as the mesh you provided. So in short: It’s not really possible to get access to the actually used collider.

If you want to “visualize” the collider, depending on the usecase you may use a crazy approach like “scanning” the collider with tons of raycasts, essentially collecting point cloud data and then simplify and visualize it :slight_smile: It’s probably the only “reliable” way at the moment if it needs to be accurate. The nice thing about this approach is, it can be as accurate as you want, just increase the ray count. Of course you can be smart about the scanning and specifically subdivide areas where the normals differ. Areas where the normals are equal belong to the same surface / triangle. That way you could “home in” to the edges and corners. For 99.9% of all usecases it would not be worth this efford. However if (for some reason) this really is required and needs to be correct, this would be a way.