Ejemplo n.º 1
0
        /**
         *	Removes triangles that occupy the same space, and point to the same vertices.
         */
        public static int[] RemoveDuplicateTriangles(this pb_Object pb)
        {
            pb_IntArray[]  sharedIndices = pb.sharedIndices;
            Vector3[]      v             = pb.vertices;
            List <pb_Face> del           = new List <pb_Face>();

            int[] removedIndices;

            List <pb_Face> f = new List <pb_Face>();

            foreach (pb_Face face in pb.faces)
            {
                List <int> tris = new List <int>();

                int[] ind = face.indices;
                for (int i = 0; i < ind.Length; i += 3)
                {
                    int[] s = new int[3]
                    {
                        sharedIndices.IndexOf(ind[i + 0]),
                        sharedIndices.IndexOf(ind[i + 1]),
                        sharedIndices.IndexOf(ind[i + 2])
                    };

                    float area = pb_Math.TriangleArea(v[ind[i + 0]], v[ind[i + 1]], v[ind[i + 2]]);

                    if ((s[0] == s[1] || s[0] == s[2] || s[1] == s[2]) || area <= 0)
                    {
                        // don't include this face in the reconstruct
                        ;
                    }
                    else
                    {
                        tris.Add(ind[i + 0]);
                        tris.Add(ind[i + 1]);
                        tris.Add(ind[i + 2]);
                    }
                }

                if (tris.Count > 0)
                {
                    face.SetIndices(tris.ToArray());
                    face.RebuildCaches();

                    f.Add(face);
                }
                else
                {
                    del.Add(face);
                }
            }

            pb.SetFaces(f.ToArray());

            removedIndices = pb.RemoveUnusedVertices();

            return(removedIndices);
        }
Ejemplo n.º 2
0
        /**
         * Merge all faces into a sigle face.
         */
        public static pb_Face MergeFaces(this pb_Object pb, pb_Face[] faces)
        {
            List <int> collectedIndices = new List <int>(faces[0].indices);

            for (int i = 1; i < faces.Length; i++)
            {
                collectedIndices.AddRange(faces[i].indices);
            }

            pb_Face mergedFace = new pb_Face(collectedIndices.ToArray(),
                                             faces[0].material,
                                             faces[0].uv,
                                             faces[0].smoothingGroup,
                                             faces[0].textureGroup,
                                             faces[0].elementGroup,
                                             faces[0].manualUV);

            pb_Face[] rebuiltFaces = new pb_Face[pb.faces.Length - faces.Length + 1];

            int n = 0;

            foreach (pb_Face f in pb.faces)
            {
                if (System.Array.IndexOf(faces, f) < 0)
                {
                    rebuiltFaces[n++] = f;
                }
            }

            rebuiltFaces[n] = mergedFace;

            pb.SetFaces(rebuiltFaces);

            // merge vertices that are on top of one another now that they share a face
            Dictionary <int, int> shared = new Dictionary <int, int>();

            for (int i = 0; i < mergedFace.indices.Length; i++)
            {
                int sharedIndex = pb.sharedIndices.IndexOf(mergedFace.indices[i]);

                if (shared.ContainsKey(sharedIndex))
                {
                    mergedFace.indices[i] = shared[sharedIndex];
                }
                else
                {
                    shared.Add(sharedIndex, mergedFace.indices[i]);
                }
            }

            pb.RemoveUnusedVertices();

            return(mergedFace);
        }