Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static void WeldVertices(Mesh mesh, float threshold = 0.005f, float bucketStep = 3, bool recalculateNormals = true, bool ignoreNormals = false, bool printResults = true)
- {
- int oldCnt = mesh.vertices.Length;
- if(oldCnt == 0) return;
- mesh.RecalculateBounds();
- if(recalculateNormals) mesh.RecalculateNormals();
- var vertBuffer = new List<Vector3>(8192);
- var normalBuffer = new List<Vector3>(8192);
- var colorBuffer = new List<Color>(8192);
- var uvsBuffer = new List<Vector2>(8192);
- var trisBuffer = new List<int>();
- var indexBuffer = new List<int>();
- var vertices = mesh.vertices;
- var colors = mesh.colors;
- var normals = mesh.normals;
- var uvs = mesh.uv;
- int[] triangles = mesh.triangles;
- bool hasColors = colors.Length == vertices.Length;
- bool hasUVs = uvs.Length == vertices.Length;
- bool hasNormals = normals.Length == vertices.Length;
- ignoreNormals |= !hasNormals;
- int newSize = 0;
- Vector3 min = mesh.bounds.min;
- Vector3 max = mesh.bounds.max;
- int bucketSizeX = Mathf.FloorToInt ((max.x - min.x) / bucketStep) + 1;
- int bucketSizeY = Mathf.FloorToInt ((max.y - min.y) / bucketStep) + 1;
- int bucketSizeZ = Mathf.FloorToInt ((max.z - min.z) / bucketStep) + 1;
- int size = bucketSizeX*bucketSizeY*bucketSizeZ;
- List<int>[] buckets = new List<int>[size];
- int x,y,z;
- int tempIndex;
- List<int> tempList;
- for (int i = 0; i < oldCnt; i++)
- {
- x = Mathf.Max(0, Mathf.Min(Mathf.FloorToInt ((vertices[i].x - min.x) / bucketStep), bucketSizeX - 1));
- y = Mathf.Max(0, Mathf.Min(Mathf.FloorToInt ((vertices[i].y - min.y) / bucketStep), bucketSizeY - 1));
- z = Mathf.Max(0, Mathf.Min(Mathf.FloorToInt ((vertices[i].z - min.z) / bucketStep), bucketSizeZ - 1));
- tempIndex = x * bucketSizeZ * bucketSizeY + y * bucketSizeZ + z;
- if ((object)buckets[tempIndex] == null)
- {
- tempList = new List<int>(5);
- buckets[tempIndex] = tempList;
- } else tempList = buckets[tempIndex];
- for (int j = 0; j < tempList.Count; j++)
- {
- tempIndex = tempList[j];
- if (Vector3.SqrMagnitude (vertBuffer[tempIndex] - vertices[i]) < threshold
- && (!hasUVs || uvs[i] == uvsBuffer[tempIndex])
- && (!hasColors || colors[i] == colorBuffer[tempIndex])
- && (ignoreNormals || normals[i] == normalBuffer[tempIndex]))
- {
- indexBuffer.Add(tempList[j]);
- goto skip;
- }
- }
- vertBuffer.Add(vertices[i]);
- if(hasColors)
- colorBuffer.Add(colors[i]);
- if(hasUVs)
- uvsBuffer.Add(uvs[i]);
- if(!ignoreNormals)
- normalBuffer.Add(normals[i]);
- tempList.Add(newSize);
- indexBuffer.Add(newSize);
- newSize++;
- skip:;
- }
- for (int i = 0; i < triangles.Length; i++) {
- trisBuffer.Add(indexBuffer[triangles[i]]);
- }
- mesh.Clear();
- mesh.SetVertices(vertBuffer);
- if(hasColors)
- mesh.SetColors(colorBuffer);
- if(hasUVs)
- mesh.SetUVs(0, uvsBuffer);
- mesh.SetTriangles(trisBuffer, 0, true);
- if(!ignoreNormals) mesh.SetNormals(normalBuffer);
- else mesh.RecalculateNormals();
- if(printResults) Debug.Log("Reduced " + mesh.name + " from " + vertices.Length + " to " + vertBuffer.Count);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement