Exemplo n.º 1
0
        public void Evaluate(int SpreadMax)
        {
            if (this.FPinInVector.PinIsChanged)
            {
                List <Point3d> points = new List <Point3d>();

                #region Initialize List

                for (int i = 0; i < this.FPinInVector.SliceCount; i++)
                {
                    double x, y, z;
                    this.FPinInVector.GetValue3D(i, out x, out y, out z);
                    points.Add(new Point3d(x, y, z));
                }
                #endregion

                #region Compute Hull
                QuickHull3D q = new QuickHull3D();
                q.build(points.ToArray());
                Point3d[] vertices = q.getVertices();
                int[][]   faces    = q.getFaces();
                #endregion

                #region Output
                this.FPinOutVertices.SliceCount = vertices.Length;
                for (int i = 0; i < vertices.Length; i++)
                {
                    this.FPinOutVertices.SetValue3D(i, vertices[i].x, vertices[i].y, vertices[i].z);
                }


                this.FPinOutIndices.SliceCount = faces.Length * 3;
                int idx = 0;
                for (int i = 0; i < faces.Length; i++)
                {
                    this.FPinOutIndices.SetValue(idx, faces[i][0]);
                    this.FPinOutIndices.SetValue(idx + 1, faces[i][1]);
                    this.FPinOutIndices.SetValue(idx + 2, faces[i][2]);
                    idx += 3;
                }
                #endregion
            }
        }
Exemplo n.º 2
0
        private void GenerateConvexHull(Hull hull, Vector3[] meshVertices, int[] meshIndices, Mesh destMesh)
        {
            // Generate array of input points

            int totalFaces = hull.selectedFaces.Count;

            Point3d[] inputPoints = new Point3d[totalFaces * 3];

            for (int i = 0; i < hull.selectedFaces.Count; i++)
            {
                int faceIndex = hull.selectedFaces[i];

                Vector3 p0 = meshVertices[meshIndices[faceIndex * 3]];
                Vector3 p1 = meshVertices[meshIndices[faceIndex * 3 + 1]];
                Vector3 p2 = meshVertices[meshIndices[faceIndex * 3 + 2]];

                inputPoints[i * 3]     = new Point3d(p0.x, p0.y, p0.z);
                inputPoints[i * 3 + 1] = new Point3d(p1.x, p1.y, p1.z);
                inputPoints[i * 3 + 2] = new Point3d(p2.x, p2.y, p2.z);
            }

            // Calculate the convex hull

            QuickHull3D qHull = new QuickHull3D();

            try
            {
                qHull.build(inputPoints);
            }
            catch (System.Exception)
            {
                Debug.LogError("Could not generate hull for " + this.name + "'s '" + hull.name + "' (input " + inputPoints.Length + " points)");
            }

            // Get calculated hull vertices and indices

            Point3d[] hullVertices    = qHull.getVertices();
            int[][]   hullFaceIndices = qHull.getFaces();

            hull.numColliderFaces = hullFaceIndices.Length;

            Debug.Log("Calculated collider for '" + hull.name + "' has " + hullFaceIndices.Length + " faces");
            if (hullFaceIndices.Length >= 256)
            {
                hull.hasColliderError = true;
                return;
            }

            // Convert to dest vertices

            Vector3[] destVertices = new Vector3[hullVertices.Length];
            for (int i = 0; i < destVertices.Length; i++)
            {
                destVertices[i] = new Vector3((float)hullVertices[i].x, (float)hullVertices[i].y, (float)hullVertices[i].z);
            }

            // Convert to dest incices

            List <int> destIndices = new List <int>();

            for (int i = 0; i < hullFaceIndices.Length; i++)
            {
                int faceVerts = hullFaceIndices[i].Length;
                for (int j = 1; j < faceVerts - 1; j++)
                {
                    destIndices.Add(hullFaceIndices[i][0]);
                    destIndices.Add(hullFaceIndices[i][j]);
                    destIndices.Add(hullFaceIndices[i][j + 1]);
                }
            }

            int[] destIndicesArray = new int[destIndices.Count];
            for (int i = 0; i < destIndices.Count; i++)
            {
                destIndicesArray[i] = destIndices[i];
            }

            // Push to collision mesh

            hull.collisionMesh.vertices  = destVertices;
            hull.collisionMesh.triangles = destIndicesArray;
            hull.collisionMesh.RecalculateBounds();
        }