IEnumerator method_1_optimize_coroutine() { Stopwatch systemTimer = new Stopwatch(); float nextTime = 500; systemTimer.Start(); //Build graph first meshFilter.mesh.Optimize(); int vertexCount = meshFilter.sharedMesh.vertexCount; int triangleCount = meshFilter.sharedMesh.triangles.Length; int[] triangles = meshFilter.sharedMesh.triangles; _triangle = triangles; vertices = meshFilter.mesh.vertices; // print("vertex count" + vertexCount); // print("triangle count" + triangleCount); edgeGraph = new EdgeGraph(vertexCount, true); edgeGraph.InitEdgeGraphDict(vertexCount); for (int i = 0; i < triangleCount / 3; i++) { percentage = (float)i / (float)triangleCount / 6; int triangleIndex = i * 3; Triangle newTriangle = new Triangle(); newTriangle.vertex1 = vertices[triangles[triangleIndex]]; newTriangle.vertex2 = vertices[triangles[triangleIndex + 1]]; newTriangle.vertex3 = vertices[triangles[triangleIndex + 2]]; edgeGraph.addTriangle(vertices[triangles[triangleIndex]], newTriangle); edgeGraph.addTriangle(vertices[triangles[triangleIndex + 1]], newTriangle); edgeGraph.addTriangle(vertices[triangles[triangleIndex + 2]], newTriangle); if (systemTimer.ElapsedMilliseconds > nextTime) { nextTime = systemTimer.ElapsedMilliseconds + 500; yield return(new WaitForEndOfFrame()); } } //Build normal graph for (int i = 0; i < vertexCount; i++) { percentage = ((float)i / (float)vertexCount) / 4 + 0.5f; //Get Vertex Vector3 vertex = vertices[i]; Vector3 averageNormal = edgeGraph.caculateVertexAverageNoraml(vertex); edgeGraph.addNormal(vertex, averageNormal); if (systemTimer.ElapsedMilliseconds > nextTime) { nextTime = systemTimer.ElapsedMilliseconds + 500; yield return(new WaitForEndOfFrame()); } } if (path == "") { path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop); } edgeGraph.OutputDictGraph(path + "/" + graphName + extension); //clear origin data edgeCollection.Clear(); // retrive every triangle (triangleCount / 3 for (int i = 0; i < triangleCount; i += 3) { percentage = (float)i / (float)triangleCount / 2 + 0.5f; int triangleIndex = i; int vert_0 = triangles[triangleIndex]; int vert_1 = triangles[triangleIndex + 1]; int vert_2 = triangles[triangleIndex + 2]; Triangle currTriangle = new Triangle(); currTriangle.vertex1 = vertices[vert_0]; currTriangle.vertex2 = vertices[vert_1]; currTriangle.vertex3 = vertices[vert_2]; Vector3 centerNormal = GetTriangleNormal(currTriangle.toArray()); Triangle[] vert_0_adjacentTriList = edgeGraph.getAdjacentTriangleArray(vertices[vert_0]); Triangle[] vert_1_adjacentTriList = edgeGraph.getAdjacentTriangleArray(vertices[vert_1]); Triangle[] vert_2_adjacentTriList = edgeGraph.getAdjacentTriangleArray(vertices[vert_2]); //Edge 0 - 1 Triangle adjacentTriangle1 = FindSameTriangleInGivenArrays(vert_0_adjacentTriList, vert_1_adjacentTriList, currTriangle); if (adjacentTriangle1 != null) { Vector3 adjacentEdgeNormal = GetTriangleNormal( new Vector3[] { adjacentTriangle1.vertex1, adjacentTriangle1.vertex2, adjacentTriangle1.vertex3 } ); float dot_result = Vector3.Dot(adjacentEdgeNormal, centerNormal); // print("adjacentEdgeNormal"+adjacentEdgeNormal); bool isEdge = dot_result <= Mathf.Cos(angle * Mathf.Deg2Rad); if (isEdge) { print(dot_result + " cos : " + Mathf.Cos(angle * Mathf.PI / 180)); edgeCollection.AddEdge(currTriangle.vertex1, currTriangle.vertex2); } } //Edge 0 - 2 Triangle adjacentTriangle2 = FindSameTriangleInGivenArrays(vert_0_adjacentTriList, vert_2_adjacentTriList, currTriangle); if (adjacentTriangle2 != null) { Vector3 adjacentEdgeNormal = GetTriangleNormal( new Vector3[] { adjacentTriangle2.vertex1, adjacentTriangle2.vertex2, adjacentTriangle2.vertex3 } ); float dot_result = Vector3.Dot(adjacentEdgeNormal, centerNormal); // print("adjacentEdgeNormal"+adjacentEdgeNormal); bool isEdge = dot_result <= Mathf.Cos(angle * Mathf.Deg2Rad); if (isEdge) { // print (dot_result + " cos : " + Mathf.Cos(angle * Mathf.PI / 180)); // edgeCollection.AddEdge(triangleVertices[0], triangleVertices[1]); edgeCollection.AddEdge(currTriangle.vertex1, currTriangle.vertex3); } } //Edge 1 - 2 Triangle adjacentTriangle3 = FindSameTriangleInGivenArrays(vert_1_adjacentTriList, vert_2_adjacentTriList, currTriangle); if (adjacentTriangle3 != null) { Vector3 adjacentEdgeNormal = GetTriangleNormal( new Vector3[] { adjacentTriangle3.vertex1, adjacentTriangle3.vertex2, adjacentTriangle3.vertex3 } ); float dot_result = Vector3.Dot(adjacentEdgeNormal, centerNormal); // print("adjacentEdgeNormal"+adjacentEdgeNormal); bool isEdge = dot_result <= Mathf.Cos(angle * Mathf.Deg2Rad); if (isEdge) { edgeCollection.AddEdge(currTriangle.vertex2, currTriangle.vertex3); } } // // if over run time pass for end of frame if (systemTimer.ElapsedMilliseconds > nextTime) { nextTime = systemTimer.ElapsedMilliseconds + 500; yield return(new WaitForEndOfFrame()); } } List <Vector3> _normals = new List <Vector3>(); foreach (var edgeVertex in edgeCollection.vertices) { _normals.Add(edgeGraph.getVertexAverageNormal(edgeVertex)); if (systemTimer.ElapsedMilliseconds > nextTime) { nextTime = systemTimer.ElapsedMilliseconds + 500; yield return(new WaitForEndOfFrame()); } } edgeCollection.AddNormal(_normals); if (fileName == "") { fileName = transform.parent.gameObject.name + extension; } edgeCollection.SaveFile(path + "/" + fileName); systemTimer.Stop(); print("vertices count : " + mesh.vertexCount + " triangle count : " + mesh.triangles.Length); print(systemTimer.Elapsed.Minutes + ", " + systemTimer.Elapsed.Seconds + ", " + systemTimer.Elapsed.Milliseconds); yield return(new WaitForEndOfFrame()); }
// public EdgeRawData rawData; public SimplifyModelData(EdgeRawData _rawData) { // rawData = _rawData; edgeGraph = new EdgeGraph(_rawData.getVertexNumber, false); }
IEnumerator method_2_optimize_coroutine() { Stopwatch systemTimer = new Stopwatch(); float nextTime = 500; systemTimer.Start(); //Build graph first meshFilter.mesh.Optimize(); int vertexCount = meshFilter.sharedMesh.vertexCount; int triangleCount = meshFilter.sharedMesh.triangles.Length; int[] triangles = meshFilter.sharedMesh.triangles; _triangle = triangles; vertices = meshFilter.mesh.vertices; // print("vertex count" + vertexCount); // print("triangle count" + triangleCount); edgeGraph = new EdgeGraph(vertexCount, true); edgeGraph.InitEdgeGraphDict(vertexCount); for (int i = 0; i < triangleCount / 3; i++) { percentage = (float)i / (float)triangleCount / 6; int triangleIndex = i * 3; Triangle newTriangle = new Triangle(); newTriangle.vertex1 = vertices[triangles[triangleIndex]]; newTriangle.vertex2 = vertices[triangles[triangleIndex + 1]]; newTriangle.vertex3 = vertices[triangles[triangleIndex + 2]]; edgeGraph.addTriangle(vertices[triangles[triangleIndex]], newTriangle); edgeGraph.addTriangle(vertices[triangles[triangleIndex + 1]], newTriangle); edgeGraph.addTriangle(vertices[triangles[triangleIndex + 2]], newTriangle); if (systemTimer.ElapsedMilliseconds > nextTime) { nextTime = systemTimer.ElapsedMilliseconds + 500; yield return(new WaitForEndOfFrame()); } } if (path == "") { path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop); } edgeGraph.OutputDictGraph(path + "/" + graphName + extension); //clear origin data edgeCollection.Clear(); //Build normal graph for (int i = 0; i < vertexCount; i++) { percentage = ((float)i / (float)vertexCount) / 4 + 0.5f; //Get Vertex Vector3 vertex = vertices[i]; Vector3 averageNormal = edgeGraph.caculateVertexAverageNoraml(vertex); edgeGraph.addNormal(vertex, averageNormal); if (systemTimer.ElapsedMilliseconds > nextTime) { nextTime = systemTimer.ElapsedMilliseconds + 500; yield return(new WaitForEndOfFrame()); } } for (int i = 0; i < vertexCount; i++) { percentage = ((float)i / (float)vertexCount) / 4 + 0.75f; Vector3 vertex = vertices[i]; Vector3 averageNormal = Vector3.zero; List <Triangle> adjacentTriangle = edgeGraph.getAdjacentTriangleList(vertex); Vector3 normal = edgeGraph.getVertexAverageNormal(vertex); //check each adjacent triangle of vertex foreach (var triangle in adjacentTriangle) { //find common edge Vector3[] commonEdge = triangle.edgeExcludeVertex(vertex); Triangle adjacentCell = null; //find adjacent triangle if (commonEdge != null) { Triangle[] vert0_adjacentTriangleList = edgeGraph.getAdjacentTriangleArray(commonEdge[0]); Triangle[] vert1_adjacentTriangleList = edgeGraph.getAdjacentTriangleArray(commonEdge[1]); adjacentCell = FindSameTriangleInGivenArrays( vert0_adjacentTriangleList, vert1_adjacentTriangleList, triangle ); } else { print("vertex not in the edge"); continue; } //compare 2 normal (if so add edge to list) float dotProduct = Vector3.Dot(adjacentCell.normal.normalized, normal.normalized); if (dotProduct < Mathf.Cos(angle * Mathf.Deg2Rad)) { edgeCollection.AddEdge(commonEdge[0], commonEdge[1]); } } if (systemTimer.ElapsedMilliseconds > nextTime) { nextTime = systemTimer.ElapsedMilliseconds + 500; yield return(new WaitForEndOfFrame()); } } edgeGraph = EdgeGraph.ConvertEdgeRawDataToGraph(edgeCollection, true); }