/// <summary>
        /// Split any shared vertices so that this face may be moved independently of the main object.
        /// </summary>
        /// <param name="mesh">The source mesh.</param>
        /// <param name="faces">The faces to split from the mesh.</param>
        /// <param name="deleteSourceFaces">Whether or not to delete the faces on the source geometry which were detached.</param>
        /// <returns>The faces created forming the detached face group.</returns>
        public static List <Face> DetachFaces(this ProBuilderMesh mesh, IEnumerable <Face> faces, bool deleteSourceFaces)
        {
            if (mesh == null)
            {
                throw new System.ArgumentNullException("mesh");
            }

            if (faces == null)
            {
                throw new System.ArgumentNullException("faces");
            }

            List <Vertex> vertices          = new List <Vertex>(mesh.GetVertices());
            int           sharedIndexOffset = mesh.sharedVerticesInternal.Length;
            var           lookup            = mesh.sharedVertexLookup;

            List <FaceRebuildData> detached = new List <FaceRebuildData>();

            foreach (Face face in faces)
            {
                FaceRebuildData data = new FaceRebuildData();
                data.vertices      = new List <Vertex>();
                data.sharedIndexes = new List <int>();
                data.face          = new Face(face);

                Dictionary <int, int> match = new Dictionary <int, int>();
                int[] indexes = new int[face.indexesInternal.Length];

                for (int i = 0; i < face.indexesInternal.Length; i++)
                {
                    int local;

                    if (match.TryGetValue(face.indexesInternal[i], out local))
                    {
                        indexes[i] = local;
                    }
                    else
                    {
                        local      = data.vertices.Count;
                        indexes[i] = local;
                        match.Add(face.indexesInternal[i], local);
                        data.vertices.Add(vertices[face.indexesInternal[i]]);
                        data.sharedIndexes.Add(lookup[face.indexesInternal[i]] + sharedIndexOffset);
                    }
                }

                data.face.indexesInternal = indexes.ToArray();
                detached.Add(data);
            }

            FaceRebuildData.Apply(detached, mesh, vertices);
            if (deleteSourceFaces)
            {
                mesh.DeleteFaces(faces);
            }

            mesh.ToMesh();

            return(detached.Select(x => x.face).ToList());
        }
示例#2
0
        /// <summary>
        /// Convert a selection of faces from n-gons to triangles.
        /// <br />
        /// If a face is successfully converted to triangles, each new triangle is created as a separate face and the original face is deleted.
        /// </summary>
        /// <param name="mesh">The target mesh.</param>
        /// <param name="faces">The faces to convert from quads to triangles.</param>
        /// <returns>Any new triangle faces created by breaking faces into individual triangles.</returns>
        public static Face[] ToTriangles(this ProBuilderMesh mesh, IList <Face> faces)
        {
            if (mesh == null)
            {
                throw new System.ArgumentNullException("mesh");
            }

            if (faces == null)
            {
                throw new System.ArgumentNullException("faces");
            }

            List <Vertex>         vertices = new List <Vertex>(mesh.GetVertices());
            Dictionary <int, int> lookup   = mesh.sharedVertexLookup;

            List <FaceRebuildData> rebuild = new List <FaceRebuildData>();

            foreach (Face face in faces)
            {
                List <FaceRebuildData> res = BreakFaceIntoTris(face, vertices, lookup);
                rebuild.AddRange(res);
            }

            FaceRebuildData.Apply(rebuild, mesh, vertices, null);
            mesh.DeleteFaces(faces);
            mesh.ToMesh();

            return(rebuild.Select(x => x.face).ToArray());
        }
        /// <summary>
        /// Duplicate and reverse the winding direction for each face.
        /// </summary>
        /// <param name="mesh">The target mesh.</param>
        /// <param name="faces">The faces to duplicate, reverse triangle winding order, and append to mesh.</param>
        public static void DuplicateAndFlip(this ProBuilderMesh mesh, Face[] faces)
        {
            if (mesh == null)
            {
                throw new ArgumentNullException("mesh");
            }

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

            List <FaceRebuildData> rebuild  = new List <FaceRebuildData>();
            List <Vertex>          vertices = new List <Vertex>(mesh.GetVertices());
            Dictionary <int, int>  lookup   = mesh.sharedVertexLookup;

            foreach (Face face in faces)
            {
                FaceRebuildData data = new FaceRebuildData();

                data.vertices      = new List <Vertex>();
                data.face          = new Face(face);
                data.sharedIndexes = new List <int>();

                Dictionary <int, int> map = new Dictionary <int, int>();
                int len = data.face.indexesInternal.Length;

                for (int i = 0; i < len; i++)
                {
                    if (map.ContainsKey(face.indexesInternal[i]))
                    {
                        continue;
                    }

                    map.Add(face.indexesInternal[i], map.Count);
                    data.vertices.Add(vertices[face.indexesInternal[i]]);
                    data.sharedIndexes.Add(lookup[face.indexesInternal[i]]);
                }

                int[] tris = new int[len];

                for (var i = 0; i < len; i++)
                {
                    tris[len - (i + 1)] = map[data.face[i]];
                }

                data.face.SetIndexes(tris);

                rebuild.Add(data);
            }

            FaceRebuildData.Apply(rebuild, mesh, vertices);
        }
        /// <summary>
        /// Create a new face connecting existing vertices.
        /// </summary>
        /// <param name="mesh">The source mesh.</param>
        /// <param name="indexes">The indexes of the vertices to join with the new polygon.</param>
        /// <param name="unordered">Are the indexes in an ordered path (false), or not (true)? If indexes are not ordered this function will treat the polygon as a convex shape. Ordered paths will be triangulated allowing concave shapes.</param>
        /// <returns>The new face created if the action was successfull, null if action failed.</returns>
        public static Face CreatePolygon(this ProBuilderMesh mesh, IList <int> indexes, bool unordered)
        {
            if (mesh == null)
            {
                throw new ArgumentNullException("mesh");
            }

            SharedVertex[]        sharedIndexes  = mesh.sharedVerticesInternal;
            Dictionary <int, int> lookup         = mesh.sharedVertexLookup;
            HashSet <int>         common         = mesh.GetSharedVertexHandles(indexes);
            List <Vertex>         vertices       = new List <Vertex>(mesh.GetVertices());
            List <Vertex>         appendVertices = new List <Vertex>();

            foreach (int i in common)
            {
                int index = sharedIndexes[i][0];
                appendVertices.Add(new Vertex(vertices[index]));
            }

            FaceRebuildData data = FaceWithVertices(appendVertices, unordered);

            if (data != null)
            {
                data.sharedIndexes = common.ToList();
                List <Face> faces = new List <Face>(mesh.facesInternal);
                FaceRebuildData.Apply(new FaceRebuildData[] { data }, vertices, faces, lookup, null);
                mesh.SetVertices(vertices);
                mesh.faces = faces;
                mesh.SetSharedVertices(lookup);

                return(data.face);
            }

            const string insufficientPoints = "Too Few Unique Points Selected";
            const string badWinding         = "Points not ordered correctly";

            Log.Info(unordered ? insufficientPoints : badWinding);

            return(null);
        }
示例#5
0
        /// <summary>
        /// Apply a bevel to a set of edges.
        /// </summary>
        /// <param name="mesh">Target mesh.</param>
        /// <param name="edges">A set of edges to apply bevelling to.</param>
        /// <param name="amount">A value from 0 (bevel not at all) to 1 (bevel entire face).</param>
        /// <returns>The new faces created to form the bevel.</returns>
        public static List <Face> BevelEdges(ProBuilderMesh mesh, IList <Edge> edges, float amount)
        {
            if (mesh == null)
            {
                throw new ArgumentNullException("mesh");
            }

            Dictionary <int, int>  lookup      = mesh.sharedVertexLookup;
            List <Vertex>          vertices    = new List <Vertex>(mesh.GetVertices());
            List <EdgeLookup>      m_edges     = EdgeLookup.GetEdgeLookup(edges, lookup).Distinct().ToList();
            List <WingedEdge>      wings       = WingedEdge.GetWingedEdges(mesh);
            List <FaceRebuildData> appendFaces = new List <FaceRebuildData>();

            Dictionary <Face, List <int> > ignore = new Dictionary <Face, List <int> >();
            HashSet <int> slide   = new HashSet <int>();
            int           beveled = 0;

            Dictionary <int, List <SimpleTuple <FaceRebuildData, List <int> > > > holes = new Dictionary <int, List <SimpleTuple <FaceRebuildData, List <int> > > >();

            // test every edge that will be moved along to make sure the bevel distance is appropriate.  if it's not, adjust the max bevel amount
            // to suit.
            Dictionary <int, List <WingedEdge> > spokes = WingedEdge.GetSpokes(wings);
            HashSet <int> tested_common = new HashSet <int>();

            foreach (EdgeLookup e in m_edges)
            {
                if (tested_common.Add(e.common.a))
                {
                    foreach (WingedEdge w in spokes[e.common.a])
                    {
                        Edge le = w.edge.local;
                        amount = Mathf.Min(Vector3.Distance(vertices[le.a].position, vertices[le.b].position) - .001f, amount);
                    }
                }

                if (tested_common.Add(e.common.b))
                {
                    foreach (WingedEdge w in spokes[e.common.b])
                    {
                        Edge le = w.edge.local;
                        amount = Mathf.Min(Vector3.Distance(vertices[le.a].position, vertices[le.b].position) - .001f, amount);
                    }
                }
            }

            if (amount < .001f)
            {
                Log.Info("Bevel Distance > Available Surface");
                return(null);
            }

            // iterate selected edges and move each leading edge back along it's direction
            // storing information about adjacent faces in the process
            foreach (EdgeLookup lup in m_edges)
            {
                WingedEdge we = wings.FirstOrDefault(x => x.edge.Equals(lup));

                if (we == null || we.opposite == null)
                {
                    continue;
                }

                beveled++;

                ignore.AddOrAppend(we.face, we.edge.common.a);
                ignore.AddOrAppend(we.face, we.edge.common.b);
                ignore.AddOrAppend(we.opposite.face, we.edge.common.a);
                ignore.AddOrAppend(we.opposite.face, we.edge.common.b);

                // after initial slides go back and split indirect triangles at the intersecting index into two vertices
                slide.Add(we.edge.common.a);
                slide.Add(we.edge.common.b);

                SlideEdge(vertices, we, amount);
                SlideEdge(vertices, we.opposite, amount);

                appendFaces.AddRange(GetBridgeFaces(vertices, we, we.opposite, holes));
            }

            if (beveled < 1)
            {
                Log.Info("Cannot Bevel Open Edges");
                return(null);
            }

            // grab the "createdFaces" array now so that the selection returned is just the bridged faces
            // then add holes later
            var createdFaces = new List <Face>(appendFaces.Select(x => x.face));

            Dictionary <Face, List <SimpleTuple <WingedEdge, int> > > sorted = new Dictionary <Face, List <SimpleTuple <WingedEdge, int> > >();

            // sort the adjacent but affected faces into winged edge groups where each group contains a set of
            // unique winged edges pointing to the same face
            foreach (int c in slide)
            {
                IEnumerable <WingedEdge> matches = wings.Where(x => x.edge.common.Contains(c) && !(ignore.ContainsKey(x.face) && ignore[x.face].Contains(c)));

                HashSet <Face> used = new HashSet <Face>();

                foreach (WingedEdge match in matches)
                {
                    if (!used.Add(match.face))
                    {
                        continue;
                    }

                    sorted.AddOrAppend(match.face, new SimpleTuple <WingedEdge, int>(match, c));
                }
            }

            // now go through those sorted faces and apply the vertex exploding, keeping track of any holes created
            foreach (KeyValuePair <Face, List <SimpleTuple <WingedEdge, int> > > kvp in sorted)
            {
                // common index & list of vertices it was split into
                Dictionary <int, List <int> > appended;

                FaceRebuildData f = VertexEditing.ExplodeVertex(vertices, kvp.Value, amount, out appended);

                if (f == null)
                {
                    continue;
                }

                appendFaces.Add(f);

                foreach (var apv in appended)
                {
                    // organize holes by new face so that later we can compare the winding of the new face to the hole face
                    // holes are sorted by key: common index value: face, vertex list
                    holes.AddOrAppend(apv.Key, new SimpleTuple <FaceRebuildData, List <int> >(f, apv.Value));
                }
            }

            FaceRebuildData.Apply(appendFaces, mesh, vertices);
            int removed = mesh.DeleteFaces(sorted.Keys).Length;

            mesh.sharedTextures = new SharedVertex[0];
            mesh.sharedVertices = SharedVertex.GetSharedVerticesWithPositions(mesh.positionsInternal);

            // @todo don't rebuild indexes, keep 'em cached
            SharedVertex[] sharedIndexes = mesh.sharedVerticesInternal;
            lookup = mesh.sharedVertexLookup;
            List <HashSet <int> > holesCommonIndexes = new List <HashSet <int> >();

            // offset the indexes of holes and cull any potential holes that are less than 3 indexes (not a hole :)
            foreach (KeyValuePair <int, List <SimpleTuple <FaceRebuildData, List <int> > > > hole in holes)
            {
                // less than 3 indexes in hole path; ain't a hole
                if (hole.Value.Sum(x => x.item2.Count) < 3)
                {
                    continue;
                }

                HashSet <int> holeCommon = new HashSet <int>();

                foreach (SimpleTuple <FaceRebuildData, List <int> > path in hole.Value)
                {
                    int offset = path.item1.Offset() - removed;

                    for (int i = 0; i < path.item2.Count; i++)
                    {
                        holeCommon.Add(lookup[path.item2[i] + offset]);
                    }
                }

                holesCommonIndexes.Add(holeCommon);
            }

            List <WingedEdge> modified = WingedEdge.GetWingedEdges(mesh, appendFaces.Select(x => x.face));

            // now go through the holes and create faces for them
            vertices = new List <Vertex>(mesh.GetVertices());

            List <FaceRebuildData> holeFaces = new List <FaceRebuildData>();

            foreach (HashSet <int> h in holesCommonIndexes)
            {
                // even if a set of hole indexes made it past the initial culling, the distinct part
                // may have reduced the index count
                if (h.Count < 3)
                {
                    continue;
                }
                // skip sorting the path if it's just a triangle
                if (h.Count < 4)
                {
                    List <Vertex> v = new List <Vertex>(mesh.GetVertices(h.Select(x => sharedIndexes[x][0]).ToList()));
                    holeFaces.Add(AppendElements.FaceWithVertices(v));
                }
                // if this hole has > 3 indexes, it needs a tent pole triangulation, which requires sorting into the perimeter order
                else
                {
                    List <int>    holePath = WingedEdge.SortCommonIndexesByAdjacency(modified, h);
                    List <Vertex> v        = new List <Vertex>(mesh.GetVertices(holePath.Select(x => sharedIndexes[x][0]).ToList()));
                    holeFaces.AddRange(AppendElements.TentCapWithVertices(v));
                }
            }

            FaceRebuildData.Apply(holeFaces, mesh, vertices);
            mesh.sharedVertices = SharedVertex.GetSharedVerticesWithPositions(mesh.positionsInternal);

            // go through new faces and conform hole normals
            // get a hash of just the adjacent and bridge faces
            // HashSet<pb_Face> adjacent = new HashSet<pb_Face>(appendFaces.Select(x => x.face));
            // and also just the filled holes
            HashSet <Face> newHoles = new HashSet <Face>(holeFaces.Select(x => x.face));

            // now append filled holes to the full list of added faces
            appendFaces.AddRange(holeFaces);

            List <WingedEdge> allNewFaceEdges = WingedEdge.GetWingedEdges(mesh, appendFaces.Select(x => x.face));

            for (int i = 0; i < allNewFaceEdges.Count && newHoles.Count > 0; i++)
            {
                WingedEdge wing = allNewFaceEdges[i];

                if (newHoles.Contains(wing.face))
                {
                    newHoles.Remove(wing.face);

                    // find first edge whose opposite face isn't a filled hole* then
                    // conform normal by that.
                    // *or is a filled hole but has already been conformed
                    using (var it = new WingedEdgeEnumerator(wing))
                    {
                        while (it.MoveNext())
                        {
                            var w = it.Current;

                            if (!newHoles.Contains(w.opposite.face))
                            {
                                w.face.submeshIndex = w.opposite.face.submeshIndex;
                                w.face.uv           = new AutoUnwrapSettings(w.opposite.face.uv);
                                SurfaceTopology.ConformOppositeNormal(w.opposite);
                                break;
                            }
                        }
                    }
                }
            }

            mesh.ToMesh();

            return(createdFaces);
        }
        /// <summary>
        /// Add a set of points to a face and retriangulate. Points are added to the nearest edge.
        /// </summary>
        /// <param name="mesh">The source mesh.</param>
        /// <param name="face">The face to append points to.</param>
        /// <param name="points">Points to added to the face.</param>
        /// <returns>The face created by appending the points.</returns>
        public static Face AppendVerticesToFace(this ProBuilderMesh mesh, Face face, Vector3[] points)
        {
            if (mesh == null)
            {
                throw new ArgumentNullException("mesh");
            }

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

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

            List <Vertex>         vertices = mesh.GetVertices().ToList();
            List <Face>           faces    = new List <Face>(mesh.facesInternal);
            Dictionary <int, int> lookup   = mesh.sharedVertexLookup;
            Dictionary <int, int> lookupUV = null;

            if (mesh.sharedTextures != null)
            {
                lookupUV = new Dictionary <int, int>();
                SharedVertex.GetSharedVertexLookup(mesh.sharedTextures, lookupUV);
            }

            List <Edge> wound = WingedEdge.SortEdgesByAdjacency(face);

            List <Vertex> n_vertices = new List <Vertex>();
            List <int>    n_shared   = new List <int>();
            List <int>    n_sharedUV = lookupUV != null ? new List <int>() : null;

            for (int i = 0; i < wound.Count; i++)
            {
                n_vertices.Add(vertices[wound[i].a]);
                n_shared.Add(lookup[wound[i].a]);

                if (lookupUV != null)
                {
                    int uv;

                    if (lookupUV.TryGetValue(wound[i].a, out uv))
                    {
                        n_sharedUV.Add(uv);
                    }
                    else
                    {
                        n_sharedUV.Add(-1);
                    }
                }
            }

            // now insert the new points on the nearest edge
            for (int i = 0; i < points.Length; i++)
            {
                int     index = -1;
                float   best  = Mathf.Infinity;
                Vector3 p     = points[i];
                int     vc    = n_vertices.Count;

                for (int n = 0; n < vc; n++)
                {
                    Vector3 v = n_vertices[n].position;
                    Vector3 w = n_vertices[(n + 1) % vc].position;

                    float dist = Math.DistancePointLineSegment(p, v, w);

                    if (dist < best)
                    {
                        best  = dist;
                        index = n;
                    }
                }

                Vertex left = n_vertices[index], right = n_vertices[(index + 1) % vc];

                float x = (p - left.position).sqrMagnitude;
                float y = (p - right.position).sqrMagnitude;

                Vertex insert = Vertex.Mix(left, right, x / (x + y));

                n_vertices.Insert((index + 1) % vc, insert);
                n_shared.Insert((index + 1) % vc, -1);
                if (n_sharedUV != null)
                {
                    n_sharedUV.Insert((index + 1) % vc, -1);
                }
            }

            List <int> triangles;

            try
            {
                Triangulation.TriangulateVertices(n_vertices, out triangles, false);
            }
            catch
            {
                Debug.Log("Failed triangulating face after appending vertices.");
                return(null);
            }

            FaceRebuildData data = new FaceRebuildData();

            data.face = new Face(triangles.ToArray(), face.submeshIndex, new AutoUnwrapSettings(face.uv),
                                 face.smoothingGroup, face.textureGroup, -1, face.manualUV);
            data.vertices        = n_vertices;
            data.sharedIndexes   = n_shared;
            data.sharedIndexesUV = n_sharedUV;

            FaceRebuildData.Apply(new List <FaceRebuildData>()
            {
                data
            },
                                  vertices,
                                  faces,
                                  lookup,
                                  lookupUV);

            var newFace = data.face;

            mesh.SetVertices(vertices);
            mesh.faces = faces;
            mesh.SetSharedVertices(lookup);
            mesh.SetSharedTextures(lookupUV);

            // check old normal and make sure this new face is pointing the same direction
            Vector3 oldNrm = Math.Normal(mesh, face);
            Vector3 newNrm = Math.Normal(mesh, newFace);

            if (Vector3.Dot(oldNrm, newNrm) < 0)
            {
                newFace.Reverse();
            }

            mesh.DeleteFace(face);

            return(newFace);
        }
示例#7
0
        /// <summary>
        /// Inserts edges connecting a list of indices.
        ///
        /// This is the equivalent of the [Connect Edges](../manual/Edge_Connect.html) action.
        /// </summary>
        /// <param name="mesh">The target mesh.</param>
        /// <param name="indexes">A list of indices (corresponding to the <see cref="ProBuilderMesh.positions"/> array) to connect to the new edges.</param>
        /// <returns>A new array containing the indices of the newly connected positions. This method rebuilds the `indexes` array because it might modify the ordering of the original array.</returns>
        public static int[] Connect(this ProBuilderMesh mesh, IList <int> indexes)
        {
            if (mesh == null)
            {
                throw new ArgumentNullException("mesh");
            }

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

            int sharedIndexOffset        = mesh.sharedVerticesInternal.Length;
            Dictionary <int, int> lookup = mesh.sharedVertexLookup;

            HashSet <int> distinct = new HashSet <int>(indexes.Select(x => lookup[x]));
            HashSet <int> affected = new HashSet <int>();

            foreach (int i in distinct)
            {
                affected.UnionWith(mesh.sharedVerticesInternal[i].arrayInternal);
            }

            Dictionary <Face, List <int> > splits = new Dictionary <Face, List <int> >();
            List <Vertex> vertices = new List <Vertex>(mesh.GetVertices());

            foreach (Face face in mesh.facesInternal)
            {
                int[] f = face.distinctIndexesInternal;

                for (int i = 0; i < f.Length; i++)
                {
                    if (affected.Contains(f[i]))
                    {
                        splits.AddOrAppend(face, f[i]);
                    }
                }
            }

            List <ConnectFaceRebuildData> appendFaces = new List <ConnectFaceRebuildData>();
            List <Face>   successfulSplits            = new List <Face>();
            HashSet <int> usedTextureGroups           = new HashSet <int>(mesh.facesInternal.Select(x => x.textureGroup));
            int           newTextureGroupIndex        = 1;

            foreach (KeyValuePair <Face, List <int> > split in splits)
            {
                Face face = split.Key;

                List <ConnectFaceRebuildData> res = split.Value.Count == 2 ?
                                                    ConnectIndexesPerFace(face, split.Value[0], split.Value[1], vertices, lookup) :
                                                    ConnectIndexesPerFace(face, split.Value, vertices, lookup, sharedIndexOffset++);

                if (res == null)
                {
                    continue;
                }

                if (face.textureGroup < 0)
                {
                    while (usedTextureGroups.Contains(newTextureGroupIndex))
                    {
                        newTextureGroupIndex++;
                    }

                    usedTextureGroups.Add(newTextureGroupIndex);
                }

                foreach (ConnectFaceRebuildData c in res)
                {
                    c.faceRebuildData.face.textureGroup   = face.textureGroup < 0 ? newTextureGroupIndex : face.textureGroup;
                    c.faceRebuildData.face.uv             = new AutoUnwrapSettings(face.uv);
                    c.faceRebuildData.face.smoothingGroup = face.smoothingGroup;
                    c.faceRebuildData.face.manualUV       = face.manualUV;
                    c.faceRebuildData.face.submeshIndex   = face.submeshIndex;
                }

                successfulSplits.Add(face);
                appendFaces.AddRange(res);
            }

            FaceRebuildData.Apply(appendFaces.Select(x => x.faceRebuildData), mesh, vertices, null);
            int removedVertexCount = mesh.DeleteFaces(successfulSplits).Length;

            lookup = mesh.sharedVertexLookup;

            HashSet <int> newVertexIndexes = new HashSet <int>();

            for (int i = 0; i < appendFaces.Count; i++)
            {
                for (int n = 0; n < appendFaces[i].newVertexIndexes.Count; n++)
                {
                    newVertexIndexes.Add(lookup[appendFaces[i].newVertexIndexes[n] + (appendFaces[i].faceRebuildData.Offset() - removedVertexCount)]);
                }
            }

            mesh.ToMesh();

            return(newVertexIndexes.Select(x => mesh.sharedVerticesInternal[x][0]).ToArray());
        }
示例#8
0
        /// <summary>
        /// Inserts new edges connecting the passed edges, optionally restricting new edge insertion to faces in faceMask.
        /// </summary>
        /// <param name="mesh"></param>
        /// <param name="edges"></param>
        /// <param name="addedFaces"></param>
        /// <param name="connections"></param>
        /// <param name="returnFaces"></param>
        /// <param name="returnEdges"></param>
        /// <param name="faceMask"></param>
        /// <returns></returns>
        internal static ActionResult Connect(
            this ProBuilderMesh mesh,
            IEnumerable <Edge> edges,
            out Face[] addedFaces,
            out Edge[] connections,
            bool returnFaces        = false,
            bool returnEdges        = false,
            HashSet <Face> faceMask = null)
        {
            Dictionary <int, int> lookup        = mesh.sharedVertexLookup;
            Dictionary <int, int> lookupUV      = mesh.sharedTextureLookup;
            HashSet <EdgeLookup>  distinctEdges = new HashSet <EdgeLookup>(EdgeLookup.GetEdgeLookup(edges, lookup));
            List <WingedEdge>     wings         = WingedEdge.GetWingedEdges(mesh);

            // map each edge to a face so that we have a list of all touched faces with their to-be-subdivided edges
            Dictionary <Face, List <WingedEdge> > touched = new Dictionary <Face, List <WingedEdge> >();

            foreach (WingedEdge wing in wings)
            {
                if (distinctEdges.Contains(wing.edge))
                {
                    List <WingedEdge> faceEdges;
                    if (touched.TryGetValue(wing.face, out faceEdges))
                    {
                        faceEdges.Add(wing);
                    }
                    else
                    {
                        touched.Add(wing.face, new List <WingedEdge>()
                        {
                            wing
                        });
                    }
                }
            }

            Dictionary <Face, List <WingedEdge> > affected = new Dictionary <Face, List <WingedEdge> >();

            // weed out edges that won't actually connect to other edges (if you don't play ya' can't stay)
            foreach (KeyValuePair <Face, List <WingedEdge> > kvp in touched)
            {
                if (kvp.Value.Count <= 1)
                {
                    WingedEdge opp = kvp.Value[0].opposite;

                    if (opp == null)
                    {
                        continue;
                    }

                    List <WingedEdge> opp_list;

                    if (!touched.TryGetValue(opp.face, out opp_list))
                    {
                        continue;
                    }

                    if (opp_list.Count <= 1)
                    {
                        continue;
                    }
                }

                affected.Add(kvp.Key, kvp.Value);
            }

            List <Vertex> vertices = new List <Vertex>(mesh.GetVertices());
            List <ConnectFaceRebuildData> results = new List <ConnectFaceRebuildData>();
            // just the faces that where connected with > 1 edge
            List <Face> connectedFaces = new List <Face>();

            HashSet <int> usedTextureGroups    = new HashSet <int>(mesh.facesInternal.Select(x => x.textureGroup));
            int           newTextureGroupIndex = 1;

            // do the splits
            foreach (KeyValuePair <Face, List <WingedEdge> > split in affected)
            {
                Face face = split.Key;
                List <WingedEdge> targetEdges = split.Value;
                int     inserts = targetEdges.Count;
                Vector3 nrm     = Math.Normal(vertices, face.indexesInternal);

                if (inserts == 1 || (faceMask != null && !faceMask.Contains(face)))
                {
                    ConnectFaceRebuildData c;

                    if (InsertVertices(face, targetEdges, vertices, out c))
                    {
                        Vector3 fn = Math.Normal(c.faceRebuildData.vertices, c.faceRebuildData.face.indexesInternal);

                        if (Vector3.Dot(nrm, fn) < 0)
                        {
                            c.faceRebuildData.face.Reverse();
                        }

                        results.Add(c);
                    }
                }
                else if (inserts > 1)
                {
                    List <ConnectFaceRebuildData> res = inserts == 2 ?
                                                        ConnectEdgesInFace(face, targetEdges[0], targetEdges[1], vertices) :
                                                        ConnectEdgesInFace(face, targetEdges, vertices);

                    if (face.textureGroup < 0)
                    {
                        while (usedTextureGroups.Contains(newTextureGroupIndex))
                        {
                            newTextureGroupIndex++;
                        }

                        usedTextureGroups.Add(newTextureGroupIndex);
                    }


                    if (res == null)
                    {
                        connections = null;
                        addedFaces  = null;
                        return(new ActionResult(ActionResult.Status.Failure, "Unable to connect faces"));
                    }
                    else
                    {
                        foreach (ConnectFaceRebuildData c in res)
                        {
                            connectedFaces.Add(c.faceRebuildData.face);

                            Vector3 fn = Math.Normal(c.faceRebuildData.vertices,
                                                     c.faceRebuildData.face.indexesInternal);

                            if (Vector3.Dot(nrm, fn) < 0)
                            {
                                c.faceRebuildData.face.Reverse();
                            }

                            c.faceRebuildData.face.textureGroup =
                                face.textureGroup < 0 ? newTextureGroupIndex : face.textureGroup;
                            c.faceRebuildData.face.uv             = new AutoUnwrapSettings(face.uv);
                            c.faceRebuildData.face.submeshIndex   = face.submeshIndex;
                            c.faceRebuildData.face.smoothingGroup = face.smoothingGroup;
                            c.faceRebuildData.face.manualUV       = face.manualUV;
                        }

                        results.AddRange(res);
                    }
                }
            }

            FaceRebuildData.Apply(results.Select(x => x.faceRebuildData), mesh, vertices, null);

            mesh.sharedTextures = new SharedVertex[0];
            int removedVertexCount = mesh.DeleteFaces(affected.Keys).Length;

            mesh.sharedVertices = SharedVertex.GetSharedVerticesWithPositions(mesh.positionsInternal);
            mesh.ToMesh();

            // figure out where the new edges where inserted
            if (returnEdges)
            {
                // offset the newVertexIndexes by whatever the FaceRebuildData did so we can search for the new edges by index
                var appended = new HashSet <int>();

                for (int n = 0; n < results.Count; n++)
                {
                    for (int i = 0; i < results[n].newVertexIndexes.Count; i++)
                    {
                        appended.Add((results[n].newVertexIndexes[i] + results[n].faceRebuildData.Offset()) - removedVertexCount);
                    }
                }

                Dictionary <int, int>    lup          = mesh.sharedVertexLookup;
                IEnumerable <Edge>       newEdges     = results.SelectMany(x => x.faceRebuildData.face.edgesInternal).Where(x => appended.Contains(x.a) && appended.Contains(x.b));
                IEnumerable <EdgeLookup> distNewEdges = EdgeLookup.GetEdgeLookup(newEdges, lup);

                connections = distNewEdges.Distinct().Select(x => x.local).ToArray();
            }
            else
            {
                connections = null;
            }

            if (returnFaces)
            {
                addedFaces = connectedFaces.ToArray();
            }
            else
            {
                addedFaces = null;
            }

            return(new ActionResult(ActionResult.Status.Success, string.Format("Connected {0} Edges", results.Count / 2)));
        }