/** * Creates a new mesh using only the @src positions, normals, and a new color array. */ public static Mesh CreateOverlayMesh(z_Mesh src) { Mesh m = new Mesh(); m.name = "Overlay Mesh: " + src.name; m.vertices = src.vertices; m.normals = src.normals; m.colors = z_Util.Fill <Color>(new Color(0f, 0f, 0f, 0f), m.vertexCount); m.subMeshCount = src.subMeshCount; for (int i = 0; i < src.subMeshCount; i++) { if (src.GetTopology(i) == MeshTopology.Triangles) { int[] tris = src.GetIndices(i); int[] lines = new int[tris.Length * 2]; int index = 0; for (int n = 0; n < tris.Length; n += 3) { lines[index++] = tris[n + 0]; lines[index++] = tris[n + 1]; lines[index++] = tris[n + 1]; lines[index++] = tris[n + 2]; lines[index++] = tris[n + 2]; lines[index++] = tris[n + 0]; } m.SetIndices(lines, MeshTopology.Lines, i); } else { m.SetIndices(src.GetIndices(i), src.GetTopology(i), i); } } return(m); }
/** * Returns a dictionary where each z_Edge is mapped to a list of triangle indices that share that edge. * To translate triangle list to vertex indices, multiply by 3 and take those indices (ex, triangles[index+{0,1,2}]) */ public static Dictionary <z_Edge, List <int> > GetAdjacentTriangles(z_Mesh m) { int len = m.GetTriangles().Length; if (len % 3 != 0 || len / 3 == m.vertexCount) { return(new Dictionary <z_Edge, List <int> >()); } Dictionary <z_Edge, List <int> > lookup = null; // @todo - should add some checks to make sure triangle structure hasn't changed if (adjacentTrianglesCache.TryGetValue(m, out lookup)) { return(lookup); } int smc = m.subMeshCount; lookup = new Dictionary <z_Edge, List <int> >(); List <int> connections; for (int n = 0; n < smc; n++) { int[] tris = m.GetIndices(n); for (int i = 0; i < tris.Length; i += 3) { int index = i / 3; z_Edge a = new z_Edge(tris[i], tris[i + 1]); z_Edge b = new z_Edge(tris[i + 1], tris[i + 2]); z_Edge c = new z_Edge(tris[i + 2], tris[i]); if (lookup.TryGetValue(a, out connections)) { connections.Add(index); } else { lookup.Add(a, new List <int>() { index }); } if (lookup.TryGetValue(b, out connections)) { connections.Add(index); } else { lookup.Add(b, new List <int>() { index }); } if (lookup.TryGetValue(c, out connections)) { connections.Add(index); } else { lookup.Add(c, new List <int>() { index }); } } } adjacentTrianglesCache.Add(m, lookup); return(lookup); }