- Home /
 
Creating a convex MESH (not collider)
I need to create a mesh around a group of other meshes (not necessarily one). As far as I can see, the simplest way would be to create a combined mesh (via script) of the meshes in question, add a Mesh Collider to the new mesh and make it Convex, then convert the convex colider to a mesh.
Now, I'm not even sure this is possible as I have found no references to anything like this on Google...
More details: I basically need a method to procedurally generate the Hull of a spaceship around it's hallways and rooms.
Take a look at convex hull algorithms simply (QuickHull for example)
Some resources from google :
https://people.csail.mit.edu/indyk/6.838-old/handouts/lec10.pdf http://box2d.org/files/GDC2014/DirkGregorius_ImplementingQuickHull.pdf
You can construct a mesh by scripting using the $$anonymous$$esh class in Unity : http://docs.unity3d.com/ScriptReference/$$anonymous$$esh.html
Well, this looks exactly like what I need, but unfortunately my coding skills are not quite there yet... Hard to believe this isn't an asset yet.
Answer by GLeBaTi · Feb 01, 2016 at 03:29 PM
I Found! 1) go to https://miconvexhull.codeplex.com/ 2) download and put "MIConvexHull" to unity project 3) Add class:
 public class Vertex : IVertex
 {
     public double[] Position { get; set; }
     public Vertex(double x, double y, double z)
     {
         Position = new double[3] { x, y, z };
     }
     public Vertex (Vector3 ver)
     {
         Position = new double[3] { ver.x, ver.y, ver.z };
     }
     public Vector3 ToVec()
     {
         return new Vector3((float)Position[0], (float)Position[1], (float)Position[2]);
     }
 }
 
               4) Use It:
     void CreateSelection(IEnumerable<Vector3> points)
     {
         selection = new GameObject("Selection");
         MeshFilter meshFilter = (MeshFilter)selection.AddComponent(typeof(MeshFilter));
 
         meshFilter.mesh = CreateMesh(points);
 
         MeshRenderer renderer = selection.AddComponent(typeof(MeshRenderer)) as MeshRenderer;
         renderer.material.shader = Shader.Find("Particles/Additive");
         Texture2D tex = new Texture2D(1, 1);
         tex.SetPixel(0, 0, Color.green);
         tex.Apply();
         renderer.material.mainTexture = tex;
         renderer.material.color = Color.green;
     }
 
     Mesh CreateMesh(IEnumerable<Vector3> stars)
     {
         Mesh m = new Mesh();
         m.name = "ScriptedMesh";
         List<int> triangles = new List<int>();
 
         var vertices = stars.Select(x => new Vertex(x)).ToList();
 
         var result = MIConvexHull.ConvexHull.Create(vertices);
         m.vertices = result.Points.Select(x => x.ToVec()).ToArray();
         var xxx = result.Points.ToList();
 
         foreach(var face in result.Faces)
         {
             triangles.Add(xxx.IndexOf(face.Vertices[0]));
             triangles.Add(xxx.IndexOf(face.Vertices[1]));
             triangles.Add(xxx.IndexOf(face.Vertices[2]));
         }
 
         m.triangles = triangles.ToArray();
         m.RecalculateNormals();
         return m;
     }
 
              I'm having trouble figuring out how to import $$anonymous$$IConvexHull into unity, could you explain how to do that in more detail? I downloaded the project, then copied the $$anonymous$$IConvexHull folder into my scripts folder, but a few code errors arose that I don't know how to fix.
1) copy only $$anonymous$$IConvexHull folder (not examples, asp, resources, sln, .ignore) Try to delete "Properties" folder and "$$anonymous$$IConvexHull.csproj" file
I've also problems running $$anonymous$$IConvexHull.
I've downlaoded at https://miconvexhull.codeplex.com/SourceControl/latest (https://miconvexhull.codeplex.com/SourceControl/latest#), extracted it and copied the folder "$$anonymous$$IConvexHull" (without the folder "Properties" and the file "$$anonymous$$IConvexHull.csproj"). I've also created a file Vertex.cs as you mentioned. But Unity is showing me some errors:
Assets/$$anonymous$$IConvexHull/Configuration.cs(125,87): error CS1750: Optional parameter value '0' cannot be converted to parameter type 'int?'
Assets/$$anonymous$$IConvexHull/Triangulation.cs(59,23): error CS0310: The type '$$anonymous$$IConvexHull.DefaultTriangulationCell' must have a public parameterless constructor in order to use it as parameter 'TCell' in the generic type or method '$$anonymous$$IConvexHull.ITriangulation'
Assets/$$anonymous$$IConvexHull/Triangulation.cs(71,23): error CS0310: The type '$$anonymous$$IConvexHull.DefaultTriangulationCell' must have a public parameterless constructor in order to use it as parameter 'TCell' in the generic type or method '$$anonymous$$IConvexHull.ITriangulation'
Assets/$$anonymous$$IConvexHull/Triangulation.cs(117,23): error CS0310: The type '$$anonymous$$IConvexHull.DefaultTriangulationCell' must have a public parameterless constructor in order to use it as parameter 'TCell' in the generic type or method '$$anonymous$$IConvexHull.Voronoi$$anonymous$$esh'
Assets/$$anonymous$$IConvexHull/Triangulation.cs(129,23): error CS0310: The type '$$anonymous$$IConvexHull.DefaultTriangulationCell' must have a public parameterless constructor in order to use it as parameter 'TCell' in the generic type or method '$$anonymous$$IConvexHull.Voronoi$$anonymous$$esh'
And some more but with the same kind of error for another row/line.
How did you managed to run $$anonymous$$IConvexHull in Unity?
EDIT: Found the reason. You have to copy the folder into to the Unity-project (top hierarchy) and not (as I did) in the Asset-folder.
this helps a lot!
If you want to use it. Just add to you script:
   public void _CreateConvex$$anonymous$$esh()
 {
    //------This is currently taken from this gameobject
     CreateSelection(GetComponent<$$anonymous$$eshFilter>().shared$$anonymous$$esh.vertices);
 }
                 MIConvexHull has been migrated from Codeplex to Github
Answer by Jack · Jan 19, 2017 at 08:23 AM
I did it, just delete Triangulation.cs and Triangulation Folder
Your answer
 
             Follow this Question
Related Questions
Building a prism (adding thickness) from non-convex mesh 0 Answers
How to cut a hole in a wall? 3 Answers
C# Proceducal Mesh terrain 2 Answers
Vertex Colors not smooth enough? 0 Answers