public MeshSorter(Vector3[] vertices, Vector2[] uvs, Color[] colors, int[] indices, Transform space, Transform[] spheres) { this.vertices = vertices; this.uvs = uvs; this.colors = colors; var patches = new List<Patch>(); patches.Add(new Patch(this, new Triangle(indices[0], indices[1], indices[2]))); Patch activePatch = patches[0]; for(int i = 3, n = indices.Length; i < n; i += 3) { var newPatch = new Patch(this, new Triangle(indices[i], indices[i+1], indices[i+2])); if(!activePatch.Merge(newPatch)) { patches.Add(newPatch); activePatch = newPatch; } } staticPatchColors = new Color[vertices.Length]; var staticIndices = new List<int>(); int patchIdx = 0; foreach(var p in patches) { foreach(var t in p.triangles) { staticIndices.Add(t.i0); staticIndices.Add(t.i1); staticIndices.Add(t.i2); //SetDebugColor(patches.Count, patchIdx, t.i0, null); //SetDebugColor(patches.Count, patchIdx, t.i1, null); //SetDebugColor(patches.Count, patchIdx, t.i2, null); } //Debug.Log(string.Format("Indices in patch {0}: {1}", patchIdx, p.triangles.Count * 3)); ++patchIdx; } staticPatchIndices = staticIndices.ToArray(); //Debug.Log(string.Format("Merged {2} triangles to {0} patches in {1} iterations (tried {3} - out {4} indices).", patches.Count, mergeIterations, indices.Length/3, mergeTests, staticIndices.Count)); sortingList = new uint[patches.Count]; sortablePatches = new SortablePatch[patches.Count]; for(int i = 0, n = patches.Count; i < n; ++i) { var p = patches[i]; var c = Vector3.zero; var l = float.MaxValue; foreach(var idx in p.indices) { var v = vertices[idx]; c += v; #if _DISABLED foreach(var s in spheres) { l = Mathf.Min(l, Vector3.Distance(space.TransformPoint(v), s.position) - s.localScale.x); } #else l = Mathf.Min(l, space.TransformPoint(v).y - space.position.y); #endif } c /= (float)p.indices.Count; var patchIndices = new int[p.triangles.Count * 3]; var pIdx = 0; foreach(var t in p.triangles) { patchIndices[pIdx++] = t.i0; patchIndices[pIdx++] = t.i1; patchIndices[pIdx++] = t.i2; } //Debug.Log(string.Format("Patch {0}: Layer: {1} Centroid: {2}", patchIdx, l, c)); sortablePatches[i] = new SortablePatch(patchIndices, c, l); } }
public MeshSorter(Vector3[] vertices, Vector2[] uvs, Color[] colors, int[] indices, Transform space, Transform[] spheres) { this.vertices = vertices; this.uvs = uvs; this.colors = colors; var patches = new List<Patch>(); patches.Add(new Patch(this, new Triangle(indices[0], indices[1], indices[2]))); Patch activePatch = patches[0]; for(int i = 3, n = indices.Length; i < n; i += 3) { var newPatch = new Patch(this, new Triangle(indices[i], indices[i+1], indices[i+2])); if(!activePatch.Merge(newPatch)) { patches.Add(newPatch); activePatch = newPatch; } } //Debug.Log(string.Format("{0} patches after initial add.", patches.Count)); //int mergeIterations = 0, mergeTests = 0; // Usually don't need to search for more patches /*for(;;) { bool didMerge = false; for(int i = 0, n = patches.Count - 1; i < n; ++i) { var p0 = patches[i]; for(int j = i + 1; j <= n; ++j) { var p1 = patches[j]; if(p0.Merge(p1)) { patches[j] = patches[n]; patches.RemoveAt(n); didMerge = true; --n; } ++mergeTests; } ++i; } ++mergeIterations; if(!didMerge) break; }*/ staticPatchColors = new Color[vertices.Length]; var staticIndices = new List<int>(); int patchIdx = 0; foreach(var p in patches) { foreach(var t in p.triangles) { staticIndices.Add(t.i0); staticIndices.Add(t.i1); staticIndices.Add(t.i2); //SetDebugColor(patches.Count, patchIdx, t.i0, null); //SetDebugColor(patches.Count, patchIdx, t.i1, null); //SetDebugColor(patches.Count, patchIdx, t.i2, null); } //Debug.Log(string.Format("Indices in patch {0}: {1}", patchIdx, p.triangles.Count * 3)); ++patchIdx; } staticPatchIndices = staticIndices.ToArray(); //Debug.Log(string.Format("Merged {2} triangles to {0} patches in {1} iterations (tried {3} - out {4} indices).", patches.Count, mergeIterations, indices.Length/3, mergeTests, staticIndices.Count)); sortingList = new uint[patches.Count]; sortablePatches = new SortablePatch[patches.Count]; for(int i = 0, n = patches.Count; i < n; ++i) { var p = patches[i]; var c = Vector3.zero; var l = float.MaxValue; foreach(var idx in p.indices) { var v = vertices[idx]; c += v; #if _DISABLED foreach(var s in spheres) { l = Mathf.Min(l, Vector3.Distance(space.TransformPoint(v), s.position) - s.localScale.x); } #else l = Mathf.Min(l, space.TransformPoint(v).y - space.position.y); #endif } c /= (float)p.indices.Count; var patchIndices = new int[p.triangles.Count * 3]; var pIdx = 0; foreach(var t in p.triangles) { patchIndices[pIdx++] = t.i0; patchIndices[pIdx++] = t.i1; patchIndices[pIdx++] = t.i2; } //Debug.Log(string.Format("Patch {0}: Layer: {1} Centroid: {2}", patchIdx, l, c)); sortablePatches[i] = new SortablePatch(patchIndices, c, l); } }