MeshBuffer.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /**
  2. * The MIT License (MIT)
  3. *
  4. * Copyright (c) 2012-2017 DragonBones team and other contributors
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy of
  7. * this software and associated documentation files (the "Software"), to deal in
  8. * the Software without restriction, including without limitation the rights to
  9. * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  10. * the Software, and to permit persons to whom the Software is furnished to do so,
  11. * subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in all
  14. * copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  18. * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  19. * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  20. * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  21. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. using System;
  24. using System.Collections;
  25. using System.Collections.Generic;
  26. using UnityEngine;
  27. using DragonBones;
  28. namespace DragonBones
  29. {
  30. //[Serializable]
  31. public class MeshBuffer : IDisposable
  32. {
  33. public readonly List<UnitySlot> combineSlots = new List<UnitySlot>();
  34. public string name;
  35. public Mesh sharedMesh;
  36. public int vertexCount;
  37. public Vector3[] rawVertextBuffers;
  38. public Vector2[] uvBuffers;
  39. public Vector3[] vertexBuffers;
  40. public Color32[] color32Buffers;
  41. public int[] triangleBuffers;
  42. public bool vertexDirty;
  43. public bool zorderDirty;
  44. public bool enabled;
  45. public static Mesh GenerateMesh()
  46. {
  47. var mesh = new Mesh();
  48. mesh.hideFlags = HideFlags.DontSaveInEditor | HideFlags.DontSaveInBuild;
  49. mesh.MarkDynamic();
  50. return mesh;
  51. }
  52. private static int _OnSortSlots(Slot a, Slot b)
  53. {
  54. if(a._zOrder > b._zOrder)
  55. {
  56. return 1;
  57. }
  58. else if(a._zOrder < b._zOrder)
  59. {
  60. return -1;
  61. }
  62. return 0;
  63. }
  64. public void Dispose()
  65. {
  66. if (this.sharedMesh != null)
  67. {
  68. UnityFactoryHelper.DestroyUnityObject(this.sharedMesh);
  69. }
  70. this.combineSlots.Clear();
  71. this.name = string.Empty;
  72. this.sharedMesh = null;
  73. this.vertexCount = 0;
  74. this.rawVertextBuffers = null;
  75. this.uvBuffers = null;
  76. this.vertexBuffers = null;
  77. this.color32Buffers = null;
  78. this.vertexDirty = false;
  79. this.enabled = false;
  80. }
  81. public void Clear()
  82. {
  83. if (this.sharedMesh != null)
  84. {
  85. this.sharedMesh.Clear();
  86. this.sharedMesh.uv = null;
  87. this.sharedMesh.vertices = null;
  88. this.sharedMesh.normals = null;
  89. this.sharedMesh.triangles = null;
  90. this.sharedMesh.colors32 = null;
  91. }
  92. this.name = string.Empty;
  93. }
  94. public void CombineMeshes(CombineInstance[] combines)
  95. {
  96. if (this.sharedMesh == null)
  97. {
  98. this.sharedMesh = GenerateMesh();
  99. }
  100. this.sharedMesh.CombineMeshes(combines);
  101. //
  102. this.uvBuffers = this.sharedMesh.uv;
  103. this.rawVertextBuffers = this.sharedMesh.vertices;
  104. this.vertexBuffers = this.sharedMesh.vertices;
  105. this.color32Buffers = this.sharedMesh.colors32;
  106. this.triangleBuffers = this.sharedMesh.triangles;
  107. this.vertexCount = this.vertexBuffers.Length;
  108. //
  109. if (this.color32Buffers == null || this.color32Buffers.Length != this.vertexCount)
  110. {
  111. this.color32Buffers = new Color32[vertexCount];
  112. }
  113. }
  114. public void InitMesh()
  115. {
  116. if (this.vertexBuffers != null)
  117. {
  118. this.vertexCount = this.vertexBuffers.Length;
  119. }
  120. else
  121. {
  122. this.vertexCount = 0;
  123. }
  124. if (this.color32Buffers == null || this.color32Buffers.Length != this.vertexCount)
  125. {
  126. this.color32Buffers = new Color32[this.vertexCount];
  127. }
  128. this.sharedMesh.vertices = this.vertexBuffers;// Must set vertices before uvs.
  129. this.sharedMesh.uv = this.uvBuffers;
  130. this.sharedMesh.colors32 = this.color32Buffers;
  131. this.sharedMesh.triangles = this.triangleBuffers;
  132. this.sharedMesh.RecalculateBounds();
  133. this.enabled = true;
  134. }
  135. public void UpdateVertices()
  136. {
  137. this.sharedMesh.vertices = this.vertexBuffers;
  138. this.sharedMesh.RecalculateBounds();
  139. }
  140. public void UpdateColors()
  141. {
  142. this.sharedMesh.colors32 = this.color32Buffers;
  143. }
  144. public void UpdateOrder()
  145. {
  146. this.combineSlots.Sort(_OnSortSlots);
  147. var index = 0;
  148. var newVerticeIndex = 0;
  149. var oldVerticeOffset = 0;
  150. var newUVs = new Vector2[this.vertexCount];
  151. var newVertices = new Vector3[this.vertexCount];
  152. var newColors = new Color32[this.vertexCount];
  153. CombineInstance[] combines = new CombineInstance[this.combineSlots.Count];
  154. for (int i = 0; i < combineSlots.Count; i++)
  155. {
  156. var slot = combineSlots[i] as UnitySlot;
  157. oldVerticeOffset = slot._verticeOffset;
  158. //重新赋值
  159. slot._verticeOrder = i;
  160. slot._verticeOffset = newVerticeIndex;
  161. //
  162. CombineInstance com = new CombineInstance();
  163. slot._meshBuffer.InitMesh();
  164. com.mesh = slot._meshBuffer.sharedMesh;
  165. combines[i] = com;
  166. //
  167. var zspace = (slot._armature.proxy as UnityArmatureComponent).zSpace;
  168. for (int j = 0; j < slot._meshBuffer.vertexCount; j++)
  169. {
  170. index = oldVerticeOffset + j;
  171. newUVs[newVerticeIndex] = this.uvBuffers[index];
  172. newVertices[newVerticeIndex] = this.vertexBuffers[index];
  173. newColors[newVerticeIndex] = this.color32Buffers[index];
  174. newVertices[newVerticeIndex].z = -slot._verticeOrder * (zspace + UnitySlot.Z_OFFSET);
  175. newVerticeIndex++;
  176. }
  177. }
  178. //
  179. this.sharedMesh.Clear();
  180. this.sharedMesh.CombineMeshes(combines);
  181. //
  182. this.uvBuffers = newUVs;
  183. this.vertexBuffers = newVertices;
  184. this.color32Buffers = newColors;
  185. this.triangleBuffers = this.sharedMesh.triangles;
  186. this.InitMesh();
  187. }
  188. }
  189. }