Exemple #1
0
        public static int[] RemoveDegenerateTriangles(this ProBuilderMesh mesh)
        {
            List <int> removed = new List <int>();

            MeshValidation.RemoveDegenerateTriangles(mesh, removed);
            return(removed.ToArray());
        }
Exemple #2
0
        /// <summary>
        /// Collapses all passed indexes to a single shared index.
        /// </summary>
        /// <remarks>
        /// Retains vertex normals.
        /// </remarks>
        /// <param name="mesh">Target mesh.</param>
        /// <param name="indexes">The indexes to merge to a single shared vertex.</param>
        /// <param name="collapseToFirst">If true, instead of merging all vertices to the average position, the vertices will be collapsed onto the first vertex position.</param>
        /// <returns>The first available local index created as a result of the merge. -1 if action is unsuccessfull.</returns>
        public static int MergeVertices(this ProBuilderMesh mesh, int[] indexes, bool collapseToFirst = false)
        {
            if (mesh == null)
            {
                throw new ArgumentNullException("mesh");
            }

            if (indexes == null)
            {
                throw new ArgumentNullException("indexes");
            }

            Vertex[] vertices = mesh.GetVertices();
            Vertex   cen      = collapseToFirst ? vertices[indexes[0]] : Vertex.Average(vertices, indexes);

            mesh.SetVerticesCoincident(indexes);
            UVEditing.SplitUVs(mesh, indexes);
            int sharedVertexHandle = mesh.GetSharedVertexHandle(indexes.First());

            mesh.SetSharedVertexValues(sharedVertexHandle, cen);

            SharedVertex merged         = mesh.sharedVerticesInternal[sharedVertexHandle];
            List <int>   removedIndexes = new List <int>();

            MeshValidation.RemoveDegenerateTriangles(mesh, removedIndexes);

            // get a non-deleted index to work with
            int ind = -1;

            for (int i = 0; i < merged.Count; i++)
            {
                if (!removedIndexes.Contains(merged[i]))
                {
                    ind = merged[i];
                }
            }

            int res = ind;

            for (int i = 0; i < removedIndexes.Count; i++)
            {
                if (ind > removedIndexes[i])
                {
                    res--;
                }
            }

            return(res);
        }