Advertisement
Guest User

Untitled

a guest
Apr 1st, 2022
24
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.16 KB | None | 0 0
  1. public static void WeldVertices(Mesh mesh, float threshold = 0.005f, float bucketStep = 3, bool recalculateNormals = true, bool ignoreNormals = false, bool printResults = true)
  2.     {
  3.         int oldCnt = mesh.vertices.Length;
  4.  
  5.         if(oldCnt == 0) return;
  6.  
  7.         mesh.RecalculateBounds();
  8.         if(recalculateNormals) mesh.RecalculateNormals();
  9.  
  10.         var vertBuffer = new List<Vector3>(8192);
  11.         var normalBuffer = new List<Vector3>(8192);
  12.         var colorBuffer = new List<Color>(8192);
  13.         var uvsBuffer = new List<Vector2>(8192);
  14.         var trisBuffer = new List<int>();
  15.         var indexBuffer = new List<int>();
  16.  
  17.         var vertices = mesh.vertices;
  18.         var colors = mesh.colors;
  19.         var normals = mesh.normals;
  20.         var uvs = mesh.uv;
  21.         int[] triangles = mesh.triangles;
  22.  
  23.         bool hasColors = colors.Length == vertices.Length;
  24.         bool hasUVs = uvs.Length == vertices.Length;
  25.         bool hasNormals = normals.Length == vertices.Length;
  26.         ignoreNormals |= !hasNormals;
  27.  
  28.         int newSize = 0;
  29.  
  30.         Vector3 min = mesh.bounds.min;
  31.         Vector3 max = mesh.bounds.max;
  32.  
  33.         int bucketSizeX = Mathf.FloorToInt ((max.x - min.x) / bucketStep) + 1;
  34.         int bucketSizeY = Mathf.FloorToInt ((max.y - min.y) / bucketStep) + 1;
  35.         int bucketSizeZ = Mathf.FloorToInt ((max.z - min.z) / bucketStep) + 1;
  36.  
  37.         int size = bucketSizeX*bucketSizeY*bucketSizeZ;
  38.         List<int>[] buckets = new List<int>[size];
  39.  
  40.         int x,y,z;
  41.         int tempIndex;
  42.         List<int> tempList;
  43.         for (int i = 0; i < oldCnt; i++)
  44.         {
  45.             x = Mathf.Max(0, Mathf.Min(Mathf.FloorToInt ((vertices[i].x - min.x) / bucketStep), bucketSizeX - 1));
  46.             y = Mathf.Max(0, Mathf.Min(Mathf.FloorToInt ((vertices[i].y - min.y) / bucketStep), bucketSizeY - 1));
  47.             z = Mathf.Max(0, Mathf.Min(Mathf.FloorToInt ((vertices[i].z - min.z) / bucketStep), bucketSizeZ - 1));
  48.  
  49.             tempIndex = x * bucketSizeZ * bucketSizeY + y * bucketSizeZ + z;
  50.  
  51.             if ((object)buckets[tempIndex] == null)
  52.             {
  53.                 tempList = new List<int>(5);
  54.                 buckets[tempIndex] = tempList;
  55.             } else tempList = buckets[tempIndex];
  56.  
  57.             for (int j = 0; j < tempList.Count; j++)
  58.             {
  59.                 tempIndex = tempList[j];
  60.                 if (Vector3.SqrMagnitude (vertBuffer[tempIndex] - vertices[i]) < threshold
  61.                                         && (!hasUVs || uvs[i] == uvsBuffer[tempIndex])
  62.                                         && (!hasColors || colors[i] == colorBuffer[tempIndex])
  63.                                         && (ignoreNormals || normals[i] == normalBuffer[tempIndex]))
  64.                 {
  65.                     indexBuffer.Add(tempList[j]);
  66.                     goto skip;
  67.                 }
  68.             }
  69.  
  70.             vertBuffer.Add(vertices[i]);
  71.             if(hasColors)
  72.                 colorBuffer.Add(colors[i]);
  73.             if(hasUVs)
  74.                 uvsBuffer.Add(uvs[i]);
  75.             if(!ignoreNormals)
  76.                 normalBuffer.Add(normals[i]);
  77.  
  78.             tempList.Add(newSize);
  79.             indexBuffer.Add(newSize);
  80.             newSize++;
  81.  
  82.             skip:;
  83.         }
  84.  
  85.         for (int i = 0; i < triangles.Length; i++) {
  86.             trisBuffer.Add(indexBuffer[triangles[i]]);
  87.         }
  88.  
  89.         mesh.Clear();
  90.  
  91.         mesh.SetVertices(vertBuffer);
  92.  
  93.         if(hasColors)
  94.             mesh.SetColors(colorBuffer);
  95.         if(hasUVs)
  96.             mesh.SetUVs(0, uvsBuffer);
  97.  
  98.         mesh.SetTriangles(trisBuffer, 0, true);
  99.        
  100.         if(!ignoreNormals) mesh.SetNormals(normalBuffer);
  101.         else mesh.RecalculateNormals();
  102.  
  103.         if(printResults) Debug.Log("Reduced " + mesh.name + " from " + vertices.Length + " to " + vertBuffer.Count);
  104.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement