You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

96 lines
2.7 KiB

  1. #if UNITY_2017
  2. using UnityEngine;
  3. using System.Collections.Generic;
  4. public static class SubMeshFix {
  5. private class Vertices {
  6. List<Vector3> verts = null;
  7. List<Vector2> uv1 = null;
  8. List<Vector2> uv2 = null;
  9. List<Vector2> uv3 = null;
  10. List<Vector2> uv4 = null;
  11. List<Vector3> normals = null;
  12. List<Vector4> tangents = null;
  13. List<Color32> colors = null;
  14. List<BoneWeight> boneWeights = null;
  15. public Vertices() {
  16. verts = new List<Vector3>();
  17. }
  18. public Vertices(Mesh aMesh) {
  19. verts = CreateList(aMesh.vertices);
  20. uv1 = CreateList(aMesh.uv);
  21. uv2 = CreateList(aMesh.uv2);
  22. uv3 = CreateList(aMesh.uv3);
  23. uv4 = CreateList(aMesh.uv4);
  24. normals = CreateList(aMesh.normals);
  25. tangents = CreateList(aMesh.tangents);
  26. colors = CreateList(aMesh.colors32);
  27. boneWeights = CreateList(aMesh.boneWeights);
  28. }
  29. private List<T> CreateList<T>(T[] aSource) {
  30. if (aSource == null || aSource.Length == 0)
  31. return null;
  32. return new List<T>(aSource);
  33. }
  34. private void Copy<T>(ref List<T> aDest, List<T> aSource, int aIndex) {
  35. if (aSource == null)
  36. return;
  37. if (aDest == null)
  38. aDest = new List<T>();
  39. aDest.Add(aSource[aIndex]);
  40. }
  41. public int Add(Vertices aOther, int aIndex) {
  42. int i = verts.Count;
  43. Copy(ref verts, aOther.verts, aIndex);
  44. Copy(ref uv1, aOther.uv1, aIndex);
  45. Copy(ref uv2, aOther.uv2, aIndex);
  46. Copy(ref uv3, aOther.uv3, aIndex);
  47. Copy(ref uv4, aOther.uv4, aIndex);
  48. Copy(ref normals, aOther.normals, aIndex);
  49. Copy(ref tangents, aOther.tangents, aIndex);
  50. Copy(ref colors, aOther.colors, aIndex);
  51. Copy(ref boneWeights, aOther.boneWeights, aIndex);
  52. return i;
  53. }
  54. public void AssignTo(Mesh aTarget) {
  55. aTarget.SetVertices(verts);
  56. if (uv1 != null) aTarget.SetUVs(0, uv1);
  57. if (uv2 != null) aTarget.SetUVs(1, uv2);
  58. if (uv3 != null) aTarget.SetUVs(2, uv3);
  59. if (uv4 != null) aTarget.SetUVs(3, uv4);
  60. if (normals != null) aTarget.SetNormals(normals);
  61. if (tangents != null) aTarget.SetTangents(tangents);
  62. if (colors != null) aTarget.SetColors(colors);
  63. if (boneWeights != null) aTarget.boneWeights = boneWeights.ToArray();
  64. }
  65. }
  66. public static Mesh GetSubmesh(this Mesh aMesh, int aSubMeshIndex) {
  67. if (aSubMeshIndex < 0 || aSubMeshIndex >= aMesh.subMeshCount)
  68. return null;
  69. int[] indices = aMesh.GetTriangles(aSubMeshIndex);
  70. Vertices source = new Vertices(aMesh);
  71. Vertices dest = new Vertices();
  72. Dictionary<int, int> map = new Dictionary<int, int>();
  73. int[] newIndices = new int[indices.Length];
  74. for (int i = 0; i < indices.Length; i++) {
  75. int o = indices[i];
  76. int n;
  77. if (!map.TryGetValue(o, out n)) {
  78. n = dest.Add(source, o);
  79. map.Add(o, n);
  80. }
  81. newIndices[i] = n;
  82. }
  83. Mesh m = new Mesh();
  84. dest.AssignTo(m);
  85. m.triangles = newIndices;
  86. return m;
  87. }
  88. }
  89. #endif