public void MakeBoxHull()
        {
            var timer = System.Diagnostics.Stopwatch.StartNew();

            Debug.Log($"[CraftHull] MakeBoxHull 8 verts to process");
            var min     = bounds.min - boundsOrigin;
            var max     = bounds.max - boundsOrigin;
            var rawMesh = new RawMesh(8);

            rawMesh.AddVertex(min);
            rawMesh.AddVertex(new Vector3(min.x, min.y, max.z));
            rawMesh.AddVertex(new Vector3(min.x, max.y, min.z));
            rawMesh.AddVertex(new Vector3(min.x, max.y, max.z));
            rawMesh.AddVertex(new Vector3(max.x, min.y, min.z));
            rawMesh.AddVertex(new Vector3(max.x, min.y, max.z));
            rawMesh.AddVertex(new Vector3(max.x, max.y, min.z));
            rawMesh.AddVertex(max);

            var hull      = new Quickhull(rawMesh);
            var hullFaces = hull.GetHull();

            Debug.Log($"[CraftHull] MakeBoxHull {hullFaces.Count} hull faces");
            hullMeshes = hullFaces.CreateMesh();

            timer.Stop();
            Debug.Log($"[CraftHull] MakeBoxHull {timer.ElapsedMilliseconds}ms");
        }
Ejemplo n.º 2
0
 public FaceSet(RawMesh mesh, params Triangle [] faces)
 {
     this.mesh = mesh;
     for (int i = 0; i < faces.Length; i++)
     {
         faces[i].Pull();
         this.faces.AddFirst(faces[i].Node);
     }
 }
Ejemplo n.º 3
0
 public Quickhull(RawMesh mesh)
 {
     if (dump_points)
     {
         Debug.Log($"[Quickhull] points_path: {points_path}");
         var file = File.Open($"{points_path}/quickhull-points.bin",
                              FileMode.Create);
         var bw = new BinaryWriter(file);
         mesh.Write(bw);
         bw.Close();
     }
     this.mesh = mesh;
 }
Ejemplo n.º 4
0
 public Triangle(RawMesh mesh, int a, int b, int c)
 {
     node.Value = this;
     this.mesh  = mesh;
     edges[0]   = new Edge(mesh, a, b);
     edges[1]   = new Edge(mesh, b, c);
     edges[2]   = new Edge(mesh, c, a);
     redges[0]  = new Edge(mesh, b, a);
     redges[1]  = new Edge(mesh, c, b);
     redges[2]  = new Edge(mesh, a, c);
     this.a     = mesh.verts[a];
     this.b     = mesh.verts[b];
     this.c     = mesh.verts[c];
     n          = Vector3.Cross(edges[0].vect, edges[1].vect).normalized;
 }
Ejemplo n.º 5
0
 public Edge(RawMesh mesh, int a, int b)
 {
     this.mesh = mesh;
     this.a    = a;
     this.b    = b;
 }
        public void BuildConvexHull(Vessel craftVessel)
        {
            var timer = System.Diagnostics.Stopwatch.StartNew();

            var meshFilters          = craftVessel.GetComponentsInChildren <MeshFilter> (false);
            var skinnedMeshRenderers = craftVessel.GetComponentsInChildren <SkinnedMeshRenderer> (false);

            int numVerts = 0;

            for (int i = 0; i < meshFilters.Length; i++)
            {
                if (!IsActive(meshFilters[i]))
                {
                    continue;
                }
                numVerts += meshFilters[i].sharedMesh.vertices.Length;
            }
            for (int i = 0; i < skinnedMeshRenderers.Length; i++)
            {
                if (!skinnedMeshRenderers[i].enabled)
                {
                    continue;
                }
                numVerts += skinnedMeshRenderers[i].sharedMesh.vertices.Length;
            }

            Debug.Log($"[CraftHull] BuildConvexHull {numVerts} verts to process");
            var rawMesh   = new RawMesh(numVerts);
            var rootXform = craftVessel.parts[0].localRoot.transform.worldToLocalMatrix;

            for (int i = 0; i < meshFilters.Length; i++)
            {
                var mf = meshFilters[i];
                if (!IsActive(mf))
                {
                    continue;
                }
                var xform = rootXform * mf.transform.localToWorldMatrix;
                rawMesh.AppendMesh(mf.sharedMesh, xform);
            }

            var m = new Mesh();

            for (int i = 0; i < skinnedMeshRenderers.Length; i++)
            {
                var smr = skinnedMeshRenderers[i];
                if (!smr.enabled)
                {
                    continue;
                }
                var xform = rootXform * smr.transform.localToWorldMatrix;
                BakeMesh(smr, m);
                rawMesh.AppendMesh(m, xform);
            }
            UnityEngine.Object.Destroy(m);

            var hull      = new Quickhull(rawMesh);
            var hullFaces = hull.GetHull();

            hullError = hull.error;
            Debug.Log($"[CraftHull] BuildConvexHull hullError:{hullError}");
            Debug.Log($"[CraftHull] BuildConvexHull {hullFaces.Count} hull faces");
            hullMeshes = hullFaces.CreateMesh();

            timer.Stop();
            Debug.Log($"[CraftHull] BuildConvexHull {timer.ElapsedMilliseconds}ms");
        }