UnityCombineMeshs.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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.Collections;
  24. using System.Collections.Generic;
  25. using UnityEngine;
  26. using DragonBones;
  27. namespace DragonBones
  28. {
  29. [DisallowMultipleComponent]
  30. [ExecuteInEditMode]
  31. [RequireComponent(typeof(UnityArmatureComponent))]
  32. public class UnityCombineMeshs : MonoBehaviour
  33. {
  34. [HideInInspector]
  35. public List<string> slotNames = new List<string>();
  36. [HideInInspector]
  37. public MeshBuffer[] meshBuffers;
  38. [HideInInspector]
  39. public bool dirty = false;
  40. private UnityArmatureComponent _unityArmature;
  41. private int _subSlotCount;
  42. private int _verticeOffset;
  43. private bool _isCanCombineMesh = false;
  44. private void Start()
  45. {
  46. this._unityArmature = GetComponent<UnityArmatureComponent>();
  47. this._isCanCombineMesh = true;
  48. this.dirty = true;
  49. }
  50. private void OnDestroy()
  51. {
  52. if (this._unityArmature != null)
  53. {
  54. this.RestoreArmature(this._unityArmature._armature);
  55. }
  56. if (this.meshBuffers != null)
  57. {
  58. for (var i = 0; i < this.meshBuffers.Length; i++)
  59. {
  60. var meshBuffer = this.meshBuffers[i];
  61. meshBuffer.Dispose();
  62. }
  63. }
  64. this.meshBuffers = null;
  65. this.dirty = false;
  66. this._unityArmature = null;
  67. this._subSlotCount = 0;
  68. this._verticeOffset = -1;
  69. this._isCanCombineMesh = false;
  70. }
  71. private void RestoreArmature(Armature armature)
  72. {
  73. if (armature == null)
  74. {
  75. return;
  76. }
  77. //
  78. foreach (UnitySlot slot in armature.GetSlots())
  79. {
  80. if (slot.childArmature == null)
  81. {
  82. slot.CancelCombineMesh();
  83. }
  84. }
  85. }
  86. private void LateUpdate()
  87. {
  88. if (this.dirty)
  89. {
  90. this.BeginCombineMesh();
  91. this.dirty = false;
  92. }
  93. if (this.meshBuffers == null)
  94. {
  95. return;
  96. }
  97. for (var i = 0; i < this.meshBuffers.Length; i++)
  98. {
  99. var meshBuffer = this.meshBuffers[i];
  100. if (meshBuffer.zorderDirty)
  101. {
  102. meshBuffer.UpdateOrder();
  103. meshBuffer.zorderDirty = false;
  104. }
  105. else if (meshBuffer.vertexDirty)
  106. {
  107. meshBuffer.UpdateVertices();
  108. meshBuffer.vertexDirty = false;
  109. }
  110. }
  111. }
  112. public void BeginCombineMesh()
  113. {
  114. if (!this._isCanCombineMesh || _unityArmature.isUGUI)
  115. {
  116. return;
  117. }
  118. //
  119. this._verticeOffset = 0;
  120. this._subSlotCount = 0;
  121. this.slotNames.Clear();
  122. //
  123. if (this.meshBuffers != null)
  124. {
  125. for (var i = 0; i < this.meshBuffers.Length; i++)
  126. {
  127. var meshBuffer = this.meshBuffers[i];
  128. meshBuffer.Dispose();
  129. }
  130. this.meshBuffers = null;
  131. }
  132. List<CombineMeshInfo> combineSlots = new List<CombineMeshInfo>();
  133. //
  134. this.CollectMesh(this._unityArmature.armature, combineSlots);
  135. //
  136. //先合并
  137. this.meshBuffers = new MeshBuffer[combineSlots.Count];
  138. for (var i = 0; i < combineSlots.Count; i++)
  139. {
  140. var combineSlot = combineSlots[i];
  141. //
  142. var proxySlot = combineSlot.proxySlot;
  143. MeshBuffer meshBuffer = new MeshBuffer();
  144. meshBuffer.name = proxySlot._meshBuffer.name;
  145. meshBuffer.sharedMesh = MeshBuffer.GenerateMesh();
  146. meshBuffer.sharedMesh.Clear();
  147. meshBuffer.CombineMeshes(combineSlot.combines.ToArray());
  148. meshBuffer.vertexDirty = true;
  149. //
  150. proxySlot._meshFilter.sharedMesh = meshBuffer.sharedMesh;
  151. this.meshBuffers[i] = meshBuffer;
  152. //
  153. this._verticeOffset = 0;
  154. for (int j = 0; j < combineSlot.slots.Count; j++)
  155. {
  156. var slot = combineSlot.slots[j];
  157. slot._isCombineMesh = true;
  158. slot._sumMeshIndex = i;
  159. slot._verticeOrder = j;
  160. slot._verticeOffset = this._verticeOffset;
  161. slot._combineMesh = this;
  162. slot._meshBuffer.enabled = false;
  163. if (slot._renderDisplay != null)
  164. {
  165. slot._renderDisplay.SetActive(false);
  166. slot._renderDisplay.hideFlags = HideFlags.HideInHierarchy;
  167. var transform = slot._renderDisplay.transform;
  168. transform.localPosition = new Vector3(0.0f, 0.0f, transform.localPosition.z);
  169. transform.localEulerAngles = Vector3.zero;
  170. transform.localScale = Vector3.one;
  171. }
  172. //
  173. if(slot._deformVertices != null)
  174. {
  175. slot._deformVertices.verticesDirty = true;
  176. }
  177. slot._transformDirty = true;
  178. slot.Update(-1);
  179. //
  180. meshBuffer.combineSlots.Add(slot);
  181. this.slotNames.Add(slot.name);
  182. this._verticeOffset += slot._meshBuffer.vertexBuffers.Length;
  183. this._subSlotCount++;
  184. }
  185. //被合并的显示
  186. if (proxySlot._renderDisplay != null)
  187. {
  188. proxySlot._renderDisplay.SetActive(true);
  189. proxySlot._renderDisplay.hideFlags = HideFlags.None;
  190. }
  191. }
  192. }
  193. public void CollectMesh(Armature armature, List<CombineMeshInfo> combineSlots)
  194. {
  195. if (armature == null)
  196. {
  197. return;
  198. }
  199. var slots = new List<Slot>(armature.GetSlots());
  200. if (slots.Count == 0)
  201. {
  202. return;
  203. }
  204. //
  205. var isBreakCombineMesh = false;
  206. var isSameMaterial = false;
  207. var isChildAramture = false;
  208. UnitySlot slotMeshProxy = null;
  209. GameObject slotDisplay = null;
  210. for (var i = 0; i < slots.Count; i++)
  211. {
  212. var slot = slots[i] as UnitySlot;
  213. slot.CancelCombineMesh();
  214. isChildAramture = slot.childArmature != null;
  215. slotDisplay = slot.renderDisplay;
  216. if (slotMeshProxy != null)
  217. {
  218. if (slot._meshBuffer.name == string.Empty)
  219. {
  220. isSameMaterial = true;
  221. }
  222. else
  223. {
  224. isSameMaterial = slotMeshProxy._meshBuffer.name == slot._meshBuffer.name;
  225. }
  226. }
  227. else
  228. {
  229. isSameMaterial = slotMeshProxy == null;
  230. }
  231. //先检查这个slot会不会打断网格合并
  232. isBreakCombineMesh = isChildAramture ||
  233. slot._isIgnoreCombineMesh ||
  234. slot._blendMode != BlendMode.Normal ||
  235. !isSameMaterial;
  236. //如果会打断,那么先合并一次
  237. if (isBreakCombineMesh)
  238. {
  239. if (combineSlots.Count > 0)
  240. {
  241. if (combineSlots[combineSlots.Count - 1].combines.Count == 1)
  242. {
  243. combineSlots.RemoveAt(combineSlots.Count - 1);
  244. }
  245. }
  246. slotMeshProxy = null;
  247. }
  248. //
  249. if (slotMeshProxy == null && !isBreakCombineMesh && slotDisplay != null && slotDisplay.activeSelf)
  250. {
  251. CombineMeshInfo combineSlot = new CombineMeshInfo();
  252. combineSlot.proxySlot = slot;
  253. combineSlot.combines = new List<CombineInstance>();
  254. combineSlot.slots = new List<UnitySlot>();
  255. combineSlots.Add(combineSlot);
  256. slotMeshProxy = slot;
  257. }
  258. //如果不会合并,检查一下是否是子骨架
  259. if (isChildAramture)
  260. {
  261. continue;
  262. }
  263. if (slotMeshProxy != null && slotDisplay != null && slotDisplay.activeSelf && !slot._isIgnoreCombineMesh)
  264. {
  265. var parentTransfrom = (slot._armature.proxy as UnityArmatureComponent).transform;
  266. CombineInstance com = new CombineInstance();
  267. com.mesh = slot._meshBuffer.sharedMesh;
  268. com.transform = slotMeshProxy._renderDisplay.transform.worldToLocalMatrix * slotDisplay.transform.localToWorldMatrix;
  269. combineSlots[combineSlots.Count - 1].combines.Add(com);
  270. combineSlots[combineSlots.Count - 1].slots.Add(slot);
  271. }
  272. if (i != slots.Count - 1)
  273. {
  274. continue;
  275. }
  276. //
  277. if (combineSlots.Count > 0)
  278. {
  279. if (combineSlots[combineSlots.Count - 1].combines.Count == 1)
  280. {
  281. combineSlots.RemoveAt(combineSlots.Count - 1);
  282. }
  283. }
  284. slotMeshProxy = null;
  285. }
  286. }
  287. }
  288. public struct CombineMeshInfo
  289. {
  290. public UnitySlot proxySlot;
  291. public List<CombineInstance> combines;
  292. public List<UnitySlot> slots;
  293. }
  294. }