- Home /
Generating a convex hull
Hello wonderful unity community,
I'm looking something that would generate a convex mesh with less than 255 triangles for an arbitrary high ploy concave mesh like this in Unity. The convex mesh would be used for a mesh collider.
Answer by mathmos_ · Feb 05, 2013 at 03:03 PM
Hi there. So I finally found a solution. Basically I just take 64 random polygons from the original mesh, feed those to the mesh collider and enable 'convex'. It doesn't have to be 64, but that's what works for me.
using UnityEngine;
using System.Collections;
public class SimpleConvex {
Mesh mesh;
// Use this for initialization
public SimpleConvex(Mesh mesh) {
this.mesh = mesh;
}
public Mesh BuildSimplifiedConvexMesh()
{
Debug.Log(mesh.triangles.Length/3 + " tris");
SplitMeshBuilder builder = new SplitMeshBuilder();
for (int i = 0; i < 64; i++)
{
int index = Random.Range(0, mesh.triangles.Length/3) * 3;
Vector3[] triangle = new Vector3[]{mesh.vertices[mesh.triangles[index]], mesh.vertices[mesh.triangles[index + 1]], mesh.vertices[mesh.triangles[index + 2]]};
Vector2[] uvs = new Vector2[]{mesh.uv[mesh.triangles[index]], mesh.uv[mesh.triangles[index + 1]], mesh.uv[mesh.triangles[index + 2]]};
builder.AddTriangleToMesh(triangle, uvs);
}
Mesh polygonSoup = builder.Build();
Debug.Log(polygonSoup.triangles.Length/3 + " tris");
return polygonSoup;
}
}
The MeshBuilder something I made to ease mesh construction. You can make your own or I can post mine if you guys want it.
Hope it helps.
Hello,
I'm new to unity and trying to do exactly the same thing. Could you post the "Split$$anonymous$$eshBuilder" class code please? I'll be really thankful !
Sure. $$anonymous$$eshBuilder.cs
Looks like I altered the mesh builder a little since I posted this code, so you also need to pass the normals now. Their not really important, unless you need to render the mesh, so just an array of three dummy vectors, like Vector3.Up.
Let me know how it works out.
Hi, I think this is what I need to, could you explain what to to do with this script. I have a complex object with too many polygons and I want to add a convex mesh collider to use the On$$anonymous$$ouseDown function on it. Thanks!
Honestly, I would not use this code now. The randomness makes it too unreliable, but if you want to use it, just generate a mesh by feeding the constructor your original mesh, then use BuildSimplifiedConvex$$anonymous$$esh() to get your new mesh and assign that to meshfilter of an gameobject with a meshfilter and a meshcollider. But you should investigate other solutions, this one is too hacky imo.
Answer by Wolfram · Jan 14, 2013 at 10:27 AM
Not sure what you're trying to do, but Unity creates such a collider automatically if you tick "Convex" in the MeshCollider.
EDIT:
As Unity (version dependently?) apparently sometimes has problems finding such a convex hull with
This configurable algorithm generates fast and accurate approximations for the convex hull.
For example, using "`-t -n.01`", I was able to construct a 236 triangles convex hull for a 69451 triangles Stanford bunny in 3 seconds. (Note: uses Wavefront .obj, a trivial 3D file format. The only caveat: face indices start at 1, not 0).
Right, if you need the mesh only for the meshcollider just set convex to true after you assigned a mesh.
This only works if the original mesh has 255 triangles or less. I'm looking for something that would generate a low poly convex hull for any concave or convex high poly mesh.
I believe that was the case some time in the past, but now it should work for any mesh size. Tested with 3.5.7.
I just tested it with a 4968 tri Stanford bunny in Unity version 4.0.0f7 and I got the following error message: "The hull has more than 255 polygons. This is invalid. UnityEditor.DockArea:OnGUI()"
Hm, sounds like a regression bug to me, tried various models with 5000-55000 tris. Tried collisions with Rigidbodies as well.
When/what exactly prints that error, for what are you using the $$anonymous$$eshCollider?
Answer by ivarboms · Jan 17, 2013 at 02:49 PM
Unity will automatically create a convex hull of the mesh when you tick 'Convex' in a MeshCollider. Unity is able to create convex hulls from meshes with more than 255 triangles, but with certain kinds of meshes (i.e. highly tessellated spheres), the convex hull it generates contains more than 255 polygons. This is the reason you get error messages.
Unfortunately, Unity gives you no control over convex hull generation, so the only option is using external tools to generate a simplified mesh (or even a convex hull if you have tools which support that) with simpler shapes, and using this as the MeshCollider's mesh.
Your answer
![](https://koobas.hobune.stream/wayback/20220613090921im_/https://answers.unity.com/themes/thub/images/avi.jpg)