private static FinalVertex FindMatchingVertex(FinalVertex input, List <FinalVertex> vertices) { for (int i = 0; i < vertices.Count; i++) { FinalVertex compare = vertices[i]; if (compare.position == input.position && compare.normal == input.normal) { return(compare); } } return(null); }
private static Mesh BuildMesh2(List <PolygonGroup> polygonGroups) { int triangleCount = 0; List <int[]> tris = new List <int[]>(); Dictionary <Vector3, List <FinalVertex> > lookupPositions = new Dictionary <Vector3, List <FinalVertex> >(); List <Vector3> vertices = new List <Vector3>(); List <Vector3> normals = new List <Vector3>(); List <Vector2> uvCoords = new List <Vector2>(); for (int i = 0; i < polygonGroups.Count; i++) { PolygonGroup group = polygonGroups[i]; List <Polygon> polygons = group.polygons; int[] triangles = new int[polygons.Count * 3]; tris.Add(triangles); int count = 0; for (int j = 0; j < polygons.Count; j++) { for (int k = 0; k < polygons[j].vertices.Length; k++) { FinalVertex vertex = new FinalVertex(polygons[j].vertices[k], -1); FinalVertex match = null; List <FinalVertex> matchList; if (lookupPositions.TryGetValue(vertex.position, out matchList)) { match = FindMatchingVertex(vertex, matchList); } else { matchList = new List <FinalVertex>(); matchList.Add(vertex); lookupPositions.Add(vertex.position, matchList); } if (match != null) { triangles[count] = match.triangleIndex; } else { vertex.triangleIndex = triangleCount++; triangles[count] = vertex.triangleIndex; vertices.Add(vertex.position); normals.Add(vertex.normal); uvCoords.Add(vertex.uvCoord); } count++; } } } Mesh mesh = new Mesh(); mesh.subMeshCount = polygonGroups.Count; mesh.SetVertices(vertices); mesh.SetUVs(0, uvCoords); mesh.SetNormals(normals); for (int i = 0; i < polygonGroups.Count; i++) { mesh.SetTriangles(tris[i], i); } return(mesh); }
private static Mesh BuildMesh(List <Polygon> groupedPolygons) { if (groupedPolygons.Count == 0) { return(null); } FinalVertex[] subObjectVertices = new FinalVertex[groupedPolygons.Count * 3]; int[] triangles = new int[groupedPolygons.Count * 3]; int triangleCount = 0; int vertIdx = 0; for (int i = 0; i < groupedPolygons.Count; i++) { subObjectVertices[vertIdx++] = new FinalVertex(groupedPolygons[i].vertices[0], -1); subObjectVertices[vertIdx++] = new FinalVertex(groupedPolygons[i].vertices[1], -1); subObjectVertices[vertIdx++] = new FinalVertex(groupedPolygons[i].vertices[2], -1); } Dictionary <Vector3, List <FinalVertex> > lookupPositions = new Dictionary <Vector3, List <FinalVertex> >(); List <Vector3> vertices = new List <Vector3>(); List <Vector3> normals = new List <Vector3>(); List <Vector2> uvCoords = new List <Vector2>(); for (int i = 0; i < subObjectVertices.Length; i++) { FinalVertex vertex = subObjectVertices[i]; FinalVertex match = null; List <FinalVertex> matchList; if (lookupPositions.TryGetValue(vertex.position, out matchList)) { match = FindMatchingVertex(vertex, matchList); } else { matchList = new List <FinalVertex>(); matchList.Add(vertex); lookupPositions.Add(vertex.position, matchList); } if (match != null) { triangles[i] = match.triangleIndex; } else { vertex.triangleIndex = triangleCount++; triangles[i] = vertex.triangleIndex; vertices.Add(subObjectVertices[i].position); normals.Add(subObjectVertices[i].normal); uvCoords.Add(subObjectVertices[i].uvCoord); } } Mesh mesh = new Mesh(); mesh.SetVertices(vertices); mesh.SetUVs(0, uvCoords); mesh.SetNormals(normals); mesh.SetTriangles(triangles, 0); // = triangles; return(mesh); }