void Simplify() { var rationAtLevel = 1f; var rScale = 1.0f / scale; for (var level = 0; level < recursionLevel; level++) { rationAtLevel *= ratio; var target = (GameObject)Instantiate(prefab); target.transform.parent = parent; var meshfilters = target.GetComponentsInChildren <MeshFilter>(); for (var i = 0; i < meshfilters.Length; i++) { var mesh = meshfilters[i].mesh; var vertices = mesh.vertices.Select((v) => v * scale).ToArray(); var simp = new Simplification(vertices, mesh.triangles); var targetFaceCount = (int)(rationAtLevel * mesh.triangles.Length / 3); while (targetFaceCount < simp.faceDb.FaceCount) { var edge = simp.costs.RemoveFront(); simp.CollapseEdge(edge); } Vector3[] outVertices; int[] outTriangles; simp.ToMesh(out outVertices, out outTriangles); Debug.Log(string.Format("Simplification step {0}/{1}, face {2}/{3}", i, meshfilters.Length, outTriangles.Length / 3, mesh.triangles.Length / 3)); mesh.Clear(); mesh.vertices = outVertices.Select((v) => v * rScale).ToArray(); mesh.triangles = outTriangles; mesh.RecalculateNormals(); } } }
void Simplify() { var rationAtLevel = 1f; var rScale = 1.0f / scale; for (var level = 0; level < recursionLevel; level++) { rationAtLevel *= ratio; var target = (GameObject)Instantiate(prefab); target.transform.parent = parent; var meshfilters = target.GetComponentsInChildren<MeshFilter>(); for (var i = 0; i < meshfilters.Length; i++) { var mesh = meshfilters[i].mesh; var vertices = mesh.vertices.Select((v) => v * scale).ToArray(); var simp = new Simplification(vertices, mesh.triangles); var targetFaceCount = (int)(rationAtLevel * mesh.triangles.Length / 3); while (targetFaceCount < simp.faceDb.FaceCount) { var edge = simp.costs.RemoveFront(); simp.CollapseEdge(edge); } Vector3[] outVertices; int[] outTriangles; simp.ToMesh(out outVertices, out outTriangles); Debug.Log(string.Format("Simplification step {0}/{1}, face {2}/{3}", i, meshfilters.Length, outTriangles.Length / 3, mesh.triangles.Length / 3)); mesh.Clear(); mesh.vertices = outVertices.Select((v) => v * rScale).ToArray(); mesh.triangles = outTriangles; mesh.RecalculateNormals(); } } }
void Update() { if (Input.GetKeyDown(KeyCode.Space)) { for (int i = 0; i < 1000; ++i) { var edgeCost = simp.costs.RemoveFront(); simp.CollapseEdge(edgeCost); } simp.ToMesh(out data.vertices, out data.triangles); if (go.mf.sharedMesh != null) { Destroy(go.mf.sharedMesh); } data.CalculateNormals(); if (Input.GetKey(KeyCode.LeftShift)) { data.SplitEdgesCalcSmoothness(); } mesh = data.CreateMesh(); go.mf.sharedMesh = mesh; } }
void UpdateMesh() { Vector3[] _outVertices; int[] _outTriangles; _simp.ToMesh(out _outVertices, out _outTriangles); _mesh.Clear(); _mesh.vertices = _outVertices; _mesh.triangles = _outTriangles; _mesh.RecalculateNormals(); }
void Reduction(System.Object targetEdgeCountObj) { try { var targetEdgeCount = (int)targetEdgeCountObj; while (targetEdgeCount < _simp.costs.Count) { CollapseAnEdge(); } _simp.ToMesh(out _outVertices, out _outTriangles); }finally { lock (this) { _reductionInProgress = false; } } }