static ConnectFaceRebuildData InsertVertices(Face face, List <WingedEdge> edges, List <Vertex> vertices) { List <Edge> perimeter = WingedEdge.SortEdgesByAdjacency(face); List <Vertex> n_vertices = new List <Vertex>(); List <int> newVertexIndexes = new List <int>(); HashSet <Edge> affected = new HashSet <Edge>(edges.Select(x => x.edge.local)); for (int i = 0; i < perimeter.Count; i++) { n_vertices.Add(vertices[perimeter[i].a]); if (affected.Contains(perimeter[i])) { newVertexIndexes.Add(n_vertices.Count); n_vertices.Add(Vertex.Mix(vertices[perimeter[i].a], vertices[perimeter[i].b], .5f)); } } FaceRebuildData res = AppendElements.FaceWithVertices(n_vertices, false); res.face.textureGroup = face.textureGroup; res.face.uv = new AutoUnwrapSettings(face.uv); res.face.smoothingGroup = face.smoothingGroup; res.face.manualUV = face.manualUV; res.face.submeshIndex = face.submeshIndex; return(new ConnectFaceRebuildData(res, newVertexIndexes)); }
/// <summary> /// Insert a new vertex at the center of a face and connect the center of all edges to it. /// </summary> /// <param name="face"></param> /// <param name="edges"></param> /// <param name="vertices"></param> /// <returns></returns> static List <ConnectFaceRebuildData> ConnectEdgesInFace( Face face, List <WingedEdge> edges, List <Vertex> vertices) { List <Edge> perimeter = WingedEdge.SortEdgesByAdjacency(face); int splitCount = edges.Count; Vertex centroid = Vertex.Average(vertices, face.distinctIndexesInternal); List <List <Vertex> > n_vertices = ArrayUtility.Fill <List <Vertex> >(x => { return(new List <Vertex>()); }, splitCount); List <List <int> > n_indexes = ArrayUtility.Fill <List <int> >(x => { return(new List <int>()); }, splitCount); HashSet <Edge> edgesToSplit = new HashSet <Edge>(edges.Select(x => x.edge.local)); int index = 0; // creates two new polygon perimeter lines by stepping the current face perimeter and inserting new vertices where edges match for (int i = 0; i < perimeter.Count; i++) { n_vertices[index % splitCount].Add(vertices[perimeter[i].a]); if (edgesToSplit.Contains(perimeter[i])) { Vertex mix = Vertex.Mix(vertices[perimeter[i].a], vertices[perimeter[i].b], .5f); // split current poly line n_indexes[index].Add(n_vertices[index].Count); n_vertices[index].Add(mix); // add the centroid vertex n_indexes[index].Add(n_vertices[index].Count); n_vertices[index].Add(centroid); // advance the poly line index index = (index + 1) % splitCount; // then add the edge center vertex and move on n_vertices[index].Add(mix); } } List <ConnectFaceRebuildData> faces = new List <ConnectFaceRebuildData>(); for (int i = 0; i < n_vertices.Count; i++) { FaceRebuildData f = AppendElements.FaceWithVertices(n_vertices[i], false); if (f == null) { faces.Clear(); return(null); } faces.Add(new ConnectFaceRebuildData(f, n_indexes[i])); } return(faces); }
/// <summary> /// Accepts a face and set of edges to split on. /// </summary> /// <param name="face"></param> /// <param name="a"></param> /// <param name="b"></param> /// <param name="vertices"></param> /// <returns></returns> static List <ConnectFaceRebuildData> ConnectEdgesInFace( Face face, WingedEdge a, WingedEdge b, List <Vertex> vertices) { List <Edge> perimeter = WingedEdge.SortEdgesByAdjacency(face); List <Vertex>[] n_vertices = new List <Vertex>[2] { new List <Vertex>(), new List <Vertex>() }; List <int>[] n_indexes = new List <int>[2] { new List <int>(), new List <int>() }; int index = 0; // creates two new polygon perimeter lines by stepping the current face perimeter and inserting new vertices where edges match for (int i = 0; i < perimeter.Count; i++) { n_vertices[index % 2].Add(vertices[perimeter[i].a]); if (perimeter[i].Equals(a.edge.local) || perimeter[i].Equals(b.edge.local)) { Vertex mix = Vertex.Mix(vertices[perimeter[i].a], vertices[perimeter[i].b], .5f); n_indexes[index % 2].Add(n_vertices[index % 2].Count); n_vertices[index % 2].Add(mix); index++; n_indexes[index % 2].Add(n_vertices[index % 2].Count); n_vertices[index % 2].Add(mix); } } List <ConnectFaceRebuildData> faces = new List <ConnectFaceRebuildData>(); for (int i = 0; i < n_vertices.Length; i++) { FaceRebuildData f = AppendElements.FaceWithVertices(n_vertices[i], false); faces.Add(new ConnectFaceRebuildData(f, n_indexes[i])); } return(faces); }
/// <summary> /// Insert a number of new points to each edge. Points are evenly spaced out along the edge. /// </summary> /// <param name="mesh">The source mesh.</param> /// <param name="edges">The edges to split with points.</param> /// <param name="count">The number of new points to insert. Must be greater than 0.</param> /// <returns>The new edges created by inserting points.</returns> public static List <Edge> AppendVerticesToEdge(this ProBuilderMesh mesh, IList <Edge> edges, int count) { if (mesh == null) { throw new ArgumentNullException("mesh"); } if (edges == null) { throw new ArgumentNullException("edges"); } if (count < 1 || count > 512) { Log.Error("New edge vertex count is less than 1 or greater than 512."); return(null); } List <Vertex> vertices = new List <Vertex>(mesh.GetVertices()); Dictionary <int, int> lookup = mesh.sharedVertexLookup; Dictionary <int, int> lookupUV = mesh.sharedTextureLookup; List <int> indexesToDelete = new List <int>(); IEnumerable <Edge> commonEdges = EdgeUtility.GetSharedVertexHandleEdges(mesh, edges); List <Edge> distinctEdges = commonEdges.Distinct().ToList(); Dictionary <Face, FaceRebuildData> modifiedFaces = new Dictionary <Face, FaceRebuildData>(); int originalSharedIndexesCount = lookup.Count(); int sharedIndexesCount = originalSharedIndexesCount; foreach (Edge edge in distinctEdges) { Edge localEdge = EdgeUtility.GetEdgeWithSharedVertexHandles(mesh, edge); // Generate the new vertices that will be inserted on this edge List <Vertex> verticesToAppend = new List <Vertex>(count); for (int i = 0; i < count; i++) { verticesToAppend.Add(Vertex.Mix(vertices[localEdge.a], vertices[localEdge.b], (i + 1) / ((float)count + 1))); } List <SimpleTuple <Face, Edge> > adjacentFaces = ElementSelection.GetNeighborFaces(mesh, localEdge); Edge edgeLookUp = new Edge(lookup[localEdge.a], lookup[localEdge.b]); Edge e = new Edge(); // foreach face attached to common edge, append vertices foreach (SimpleTuple <Face, Edge> tup in adjacentFaces) { Face face = tup.item1; FaceRebuildData data; if (!modifiedFaces.TryGetValue(face, out data)) { data = new FaceRebuildData(); data.face = new Face(new int[0], face.submeshIndex, new AutoUnwrapSettings(face.uv), face.smoothingGroup, face.textureGroup, -1, face.manualUV); data.vertices = new List <Vertex>(ArrayUtility.ValuesWithIndexes(vertices, face.distinctIndexesInternal)); data.sharedIndexes = new List <int>(); data.sharedIndexesUV = new List <int>(); foreach (int i in face.distinctIndexesInternal) { int shared; if (lookup.TryGetValue(i, out shared)) { data.sharedIndexes.Add(shared); } if (lookupUV.TryGetValue(i, out shared)) { data.sharedIndexesUV.Add(shared); } } indexesToDelete.AddRange(face.distinctIndexesInternal); modifiedFaces.Add(face, data); //Ordering vertices in the new face List <Vertex> orderedVertices = new List <Vertex>(); List <int> orderedSharedIndexes = new List <int>(); List <int> orderedSharedUVIndexes = new List <int>(); List <Edge> peripheralEdges = WingedEdge.SortEdgesByAdjacency(face); for (int i = 0; i < peripheralEdges.Count; i++) { e.a = peripheralEdges[i].a; e.b = peripheralEdges[i].b; orderedVertices.Add(vertices[e.a]); int shared; if (lookup.TryGetValue(e.a, out shared)) { orderedSharedIndexes.Add(shared); } if (lookupUV.TryGetValue(i, out shared)) { data.sharedIndexesUV.Add(shared); } if (edgeLookUp.a == lookup[e.a] && edgeLookUp.b == lookup[e.b]) { for (int j = 0; j < count; j++) { orderedVertices.Add(verticesToAppend[j]); orderedSharedIndexes.Add(sharedIndexesCount + j); orderedSharedUVIndexes.Add(-1); } } else if (edgeLookUp.a == lookup[e.b] && edgeLookUp.b == lookup[e.a]) { for (int j = count - 1; j >= 0; j--) { orderedVertices.Add(verticesToAppend[j]); orderedSharedIndexes.Add(sharedIndexesCount + j); orderedSharedUVIndexes.Add(-1); } } } data.vertices = orderedVertices; data.sharedIndexes = orderedSharedIndexes; data.sharedIndexesUV = orderedSharedUVIndexes; } else { //Get ordered vertices in the existing face and add new ones List <Vertex> orderedVertices = data.vertices; List <int> orderedSharedIndexes = data.sharedIndexes; List <int> orderedSharedUVIndexes = data.sharedIndexesUV; for (int i = 0; i < orderedVertices.Count; i++) { Vertex edgeStart = orderedVertices[i]; int edgeStartIndex = vertices.IndexOf(edgeStart); Vertex edgeEnd = orderedVertices[(i + 1) % orderedVertices.Count]; int edgeEndIndex = vertices.IndexOf(edgeEnd); if (edgeStartIndex == -1 || edgeEndIndex == -1) { continue; } if (lookup[edgeStartIndex] == lookup[localEdge.a] && lookup[edgeEndIndex] == lookup[localEdge.b]) { orderedVertices.InsertRange(i + 1, verticesToAppend); for (int j = 0; j < count; j++) { orderedSharedIndexes.Insert(i + j + 1, sharedIndexesCount + j); orderedSharedUVIndexes.Add(-1); } } else if (lookup[edgeStartIndex] == lookup[localEdge.b] && lookup[edgeEndIndex] == lookup[localEdge.a]) { verticesToAppend.Reverse(); orderedVertices.InsertRange(i + 1, verticesToAppend); for (int j = count - 1; j >= 0; j--) { orderedSharedIndexes.Insert(i + 1, sharedIndexesCount + j); orderedSharedUVIndexes.Add(-1); } } } data.vertices = orderedVertices; data.sharedIndexes = orderedSharedIndexes; data.sharedIndexesUV = orderedSharedUVIndexes; } } sharedIndexesCount += count; } // now apply the changes List <Face> dic_face = modifiedFaces.Keys.ToList(); List <FaceRebuildData> dic_data = modifiedFaces.Values.ToList(); List <EdgeLookup> appendedEdges = new List <EdgeLookup>(); for (int i = 0; i < dic_face.Count; i++) { Face face = dic_face[i]; FaceRebuildData data = dic_data[i]; int vertexCount = vertices.Count; // triangulate and set new face indexes to end of current vertex list List <int> triangles; if (Triangulation.TriangulateVertices(data.vertices, out triangles, false)) { data.face = new Face(triangles); } else { continue; } data.face.ShiftIndexes(vertexCount); face.CopyFrom(data.face); for (int n = 0; n < data.vertices.Count; n++) { lookup.Add(vertexCount + n, data.sharedIndexes[n]); } if (data.sharedIndexesUV.Count == data.vertices.Count) { for (int n = 0; n < data.vertices.Count; n++) { lookupUV.Add(vertexCount + n, data.sharedIndexesUV[n]); } } vertices.AddRange(data.vertices); foreach (Edge e in face.edgesInternal) { EdgeLookup el = new EdgeLookup(new Edge(lookup[e.a], lookup[e.b]), e); if (el.common.a >= originalSharedIndexesCount || el.common.b >= originalSharedIndexesCount) { appendedEdges.Add(el); } } } indexesToDelete = indexesToDelete.Distinct().ToList(); int delCount = indexesToDelete.Count; var newEdges = appendedEdges.Distinct().Select(x => x.local - delCount).ToList(); mesh.SetVertices(vertices); mesh.SetSharedVertices(lookup); mesh.SetSharedTextures(lookupUV); mesh.DeleteVertices(indexesToDelete); return(newEdges); }
/// <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); }
static List <ConnectFaceRebuildData> ConnectIndexesPerFace( Face face, List <int> indexes, List <Vertex> vertices, Dictionary <int, int> lookup, int sharedIndexOffset) { if (indexes.Count < 3) { return(null); } List <Edge> perimeter = WingedEdge.SortEdgesByAdjacency(face); int splitCount = indexes.Count; List <List <Vertex> > n_vertices = ArrayUtility.Fill <List <Vertex> >(x => { return(new List <Vertex>()); }, splitCount); List <List <int> > n_sharedIndexes = ArrayUtility.Fill <List <int> >(x => { return(new List <int>()); }, splitCount); List <List <int> > n_indexes = ArrayUtility.Fill <List <int> >(x => { return(new List <int>()); }, splitCount); Vertex center = Vertex.Average(vertices, indexes); Vector3 nrm = Math.Normal(vertices, face.indexesInternal); int index = 0; for (int i = 0; i < perimeter.Count; i++) { int cur = perimeter[i].a; n_vertices[index].Add(vertices[cur]); n_sharedIndexes[index].Add(lookup[cur]); if (indexes.Contains(cur)) { n_indexes[index].Add(n_vertices[index].Count); n_vertices[index].Add(center); n_sharedIndexes[index].Add(sharedIndexOffset); index = (index + 1) % splitCount; n_indexes[index].Add(n_vertices[index].Count); n_vertices[index].Add(vertices[cur]); n_sharedIndexes[index].Add(lookup[cur]); } } List <ConnectFaceRebuildData> faces = new List <ConnectFaceRebuildData>(); for (int i = 0; i < n_vertices.Count; i++) { if (n_vertices[i].Count < 3) { continue; } FaceRebuildData f = AppendElements.FaceWithVertices(n_vertices[i], false); f.sharedIndexes = n_sharedIndexes[i]; Vector3 fn = Math.Normal(n_vertices[i], f.face.indexesInternal); if (Vector3.Dot(nrm, fn) < 0) { f.face.Reverse(); } faces.Add(new ConnectFaceRebuildData(f, n_indexes[i])); } return(faces); }
static List <ConnectFaceRebuildData> ConnectIndexesPerFace( Face face, int a, int b, List <Vertex> vertices, Dictionary <int, int> lookup) { List <Edge> perimeter = WingedEdge.SortEdgesByAdjacency(face); List <Vertex>[] n_vertices = new List <Vertex>[] { new List <Vertex>(), new List <Vertex>() }; List <int>[] n_sharedIndexes = new List <int>[] { new List <int>(), new List <int>() }; List <int>[] n_indexes = new List <int>[] { new List <int>(), new List <int>() }; int index = 0; for (int i = 0; i < perimeter.Count; i++) { // trying to connect two vertices that are already connected if (perimeter[i].Contains(a) && perimeter[i].Contains(b)) { return(null); } int cur = perimeter[i].a; n_vertices[index].Add(vertices[cur]); n_sharedIndexes[index].Add(lookup[cur]); if (cur == a || cur == b) { index = (index + 1) % 2; n_indexes[index].Add(n_vertices[index].Count); n_vertices[index].Add(vertices[cur]); n_sharedIndexes[index].Add(lookup[cur]); } } List <ConnectFaceRebuildData> faces = new List <ConnectFaceRebuildData>(); Vector3 nrm = Math.Normal(vertices, face.indexesInternal); for (int i = 0; i < n_vertices.Length; i++) { FaceRebuildData f = AppendElements.FaceWithVertices(n_vertices[i], false); f.sharedIndexes = n_sharedIndexes[i]; Vector3 fn = Math.Normal(n_vertices[i], f.face.indexesInternal); if (Vector3.Dot(nrm, fn) < 0) { f.face.Reverse(); } faces.Add(new ConnectFaceRebuildData(f, n_indexes[i])); } return(faces); }
/// <summary> /// Split a common index on a face into two vertices and slide each vertex backwards along it's feeding edge by distance. /// This method does not perform any input validation, so make sure edgeAndCommonIndex is distinct and all winged edges belong /// to the same face. ///<pre> /// `appendedVertices` is common index and a list of the new face indexes it was split into. /// /// _ _ _ _ _ _ _ /// | / /// | -> | /// | | /// </pre> /// </summary> /// <param name="vertices"></param> /// <param name="edgeAndCommonIndex"></param> /// <param name="distance"></param> /// <param name="appendedVertices"></param> /// <returns></returns> internal static FaceRebuildData ExplodeVertex( IList <Vertex> vertices, IList <SimpleTuple <WingedEdge, int> > edgeAndCommonIndex, float distance, out Dictionary <int, List <int> > appendedVertices) { Face face = edgeAndCommonIndex.FirstOrDefault().item1.face; List <Edge> perimeter = WingedEdge.SortEdgesByAdjacency(face); appendedVertices = new Dictionary <int, List <int> >(); Vector3 oldNormal = Math.Normal(vertices, face.indexesInternal); // store local and common index of split points Dictionary <int, int> toSplit = new Dictionary <int, int>(); foreach (SimpleTuple <WingedEdge, int> v in edgeAndCommonIndex) { if (v.item2 == v.item1.edge.common.a) { toSplit.Add(v.item1.edge.local.a, v.item2); } else { toSplit.Add(v.item1.edge.local.b, v.item2); } } int pc = perimeter.Count; List <Vertex> n_vertices = new List <Vertex>(); for (int i = 0; i < pc; i++) { int index = perimeter[i].b; // split this index into two if (toSplit.ContainsKey(index)) { // a --- b --- c Vertex a = vertices[perimeter[i].a]; Vertex b = vertices[perimeter[i].b]; Vertex c = vertices[perimeter[(i + 1) % pc].b]; Vertex leading_dir = a - b; Vertex following_dir = c - b; leading_dir.Normalize(); following_dir.Normalize(); Vertex leading_insert = vertices[index] + leading_dir * distance; Vertex following_insert = vertices[index] + following_dir * distance; appendedVertices.AddOrAppend(toSplit[index], n_vertices.Count); n_vertices.Add(leading_insert); appendedVertices.AddOrAppend(toSplit[index], n_vertices.Count); n_vertices.Add(following_insert); } else { n_vertices.Add(vertices[index]); } } List <int> triangles; if (Triangulation.TriangulateVertices(n_vertices, out triangles, false)) { FaceRebuildData data = new FaceRebuildData(); data.vertices = n_vertices; data.face = new Face(face); Vector3 newNormal = Math.Normal(n_vertices, triangles); if (Vector3.Dot(oldNormal, newNormal) < 0f) { triangles.Reverse(); } data.face.indexesInternal = triangles.ToArray(); return(data); } return(null); }