UnitySlot.cs 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022
  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. /**
  24. * The MIT License (MIT)
  25. *
  26. * Copyright (c) 2012-2017 DragonBones team and other contributors
  27. *
  28. * Permission is hereby granted, free of charge, to any person obtaining a copy of
  29. * this software and associated documentation files (the "Software"), to deal in
  30. * the Software without restriction, including without limitation the rights to
  31. * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  32. * the Software, and to permit persons to whom the Software is furnished to do so,
  33. * subject to the following conditions:
  34. *
  35. * The above copyright notice and this permission notice shall be included in all
  36. * copies or substantial portions of the Software.
  37. *
  38. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  39. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  40. * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  41. * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  42. * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  43. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  44. */
  45. using UnityEngine;
  46. using System.Collections.Generic;
  47. namespace DragonBones
  48. {
  49. /**
  50. * @language zh_CN
  51. * Unity 插槽。
  52. * @version DragonBones 3.0
  53. */
  54. public class UnitySlot : Slot
  55. {
  56. internal const float Z_OFFSET = 0.001f;
  57. private static readonly int[] TRIANGLES = { 0, 1, 2, 0, 2, 3 };
  58. private static Vector3 _helpVector3 = new Vector3();
  59. internal GameObject _renderDisplay;
  60. internal UnityUGUIDisplay _uiDisplay = null;
  61. internal MeshBuffer _meshBuffer;
  62. internal MeshRenderer _meshRenderer = null;
  63. internal MeshFilter _meshFilter = null;
  64. //combineMesh
  65. internal bool _isIgnoreCombineMesh;
  66. internal bool _isCombineMesh;
  67. internal int _sumMeshIndex = -1;
  68. internal int _verticeOrder = -1;
  69. internal int _verticeOffset = -1;
  70. internal UnityCombineMeshs _combineMesh = null;
  71. internal bool _isActive = false;
  72. private bool _skewed;
  73. private UnityArmatureComponent _proxy;
  74. private BlendMode _currentBlendMode;
  75. /**
  76. * @private
  77. */
  78. public UnitySlot()
  79. {
  80. }
  81. /**
  82. * @private
  83. */
  84. protected override void _OnClear()
  85. {
  86. base._OnClear();
  87. if (this._meshBuffer != null)
  88. {
  89. this._meshBuffer.Dispose();
  90. }
  91. this._skewed = false;
  92. this._proxy = null;
  93. this._renderDisplay = null;
  94. this._uiDisplay = null;
  95. this._meshBuffer = null;
  96. this._meshRenderer = null;
  97. this._meshFilter = null;
  98. this._isIgnoreCombineMesh = false;
  99. this._isCombineMesh = false;
  100. this._sumMeshIndex = -1;
  101. this._verticeOrder = -1;
  102. this._verticeOffset = -1;
  103. this._combineMesh = null;
  104. this._currentBlendMode = BlendMode.Normal;
  105. this._isActive = false;
  106. }
  107. /**
  108. * @private
  109. */
  110. protected override void _InitDisplay(object value, bool isRetain)
  111. {
  112. }
  113. /**
  114. * @private
  115. */
  116. protected override void _DisposeDisplay(object value, bool isRelease)
  117. {
  118. if (!isRelease)
  119. {
  120. UnityFactoryHelper.DestroyUnityObject(value as GameObject);
  121. }
  122. }
  123. /**
  124. * @private
  125. */
  126. protected override void _OnUpdateDisplay()
  127. {
  128. _renderDisplay = (_display != null ? _display : _rawDisplay) as GameObject;
  129. //
  130. _proxy = _armature.proxy as UnityArmatureComponent;
  131. if (_proxy.isUGUI)
  132. {
  133. _uiDisplay = _renderDisplay.GetComponent<UnityUGUIDisplay>();
  134. if (_uiDisplay == null)
  135. {
  136. _uiDisplay = _renderDisplay.AddComponent<UnityUGUIDisplay>();
  137. _uiDisplay.raycastTarget = false;
  138. }
  139. }
  140. else
  141. {
  142. _meshRenderer = _renderDisplay.GetComponent<MeshRenderer>();
  143. if (_meshRenderer == null)
  144. {
  145. _meshRenderer = _renderDisplay.AddComponent<MeshRenderer>();
  146. }
  147. //
  148. _meshFilter = _renderDisplay.GetComponent<MeshFilter>();
  149. if (_meshFilter == null && _renderDisplay.GetComponent<TextMesh>() == null)
  150. {
  151. _meshFilter = _renderDisplay.AddComponent<MeshFilter>();
  152. }
  153. }
  154. //init mesh
  155. if (this._meshBuffer == null)
  156. {
  157. this._meshBuffer = new MeshBuffer();
  158. this._meshBuffer.sharedMesh = MeshBuffer.GenerateMesh();
  159. this._meshBuffer.sharedMesh.name = this.name;
  160. }
  161. }
  162. /**
  163. * @private
  164. */
  165. protected override void _AddDisplay()
  166. {
  167. _proxy = _armature.proxy as UnityArmatureComponent;
  168. var container = _proxy;
  169. if (_renderDisplay.transform.parent != container.transform)
  170. {
  171. _renderDisplay.transform.SetParent(container.transform);
  172. _helpVector3.Set(0.0f, 0.0f, 0.0f);
  173. _SetZorder(_helpVector3);
  174. }
  175. }
  176. /**
  177. * @private
  178. */
  179. protected override void _ReplaceDisplay(object value)
  180. {
  181. var container = _proxy;
  182. var prevDisplay = value as GameObject;
  183. int index = prevDisplay.transform.GetSiblingIndex();
  184. prevDisplay.SetActive(false);
  185. _renderDisplay.hideFlags = HideFlags.None;
  186. _renderDisplay.transform.SetParent(container.transform);
  187. _renderDisplay.SetActive(true);
  188. _renderDisplay.transform.SetSiblingIndex(index);
  189. _SetZorder(prevDisplay.transform.localPosition);
  190. }
  191. /**
  192. * @private
  193. */
  194. protected override void _RemoveDisplay()
  195. {
  196. _renderDisplay.transform.parent = null;
  197. }
  198. /**
  199. * @private
  200. */
  201. protected override void _UpdateZOrder()
  202. {
  203. _SetZorder(this._renderDisplay.transform.localPosition);
  204. //
  205. if (this._childArmature != null || !this._isActive)
  206. {
  207. this._CombineMesh();
  208. }
  209. }
  210. /**
  211. * @internal
  212. */
  213. internal void _SetZorder(Vector3 zorderPos)
  214. {
  215. if (this._isCombineMesh)
  216. {
  217. var meshBuffer = this._combineMesh.meshBuffers[this._sumMeshIndex];
  218. meshBuffer.zorderDirty = true;
  219. }
  220. {
  221. zorderPos.z = -this._zOrder * (this._proxy._zSpace + Z_OFFSET);
  222. if (_renderDisplay != null)
  223. {
  224. _renderDisplay.transform.localPosition = zorderPos;
  225. _renderDisplay.transform.SetSiblingIndex(_zOrder);
  226. if (_proxy.isUGUI)
  227. {
  228. return;
  229. }
  230. if (_childArmature == null)
  231. {
  232. _meshRenderer.sortingLayerName = _proxy.sortingLayerName;
  233. if (_proxy.sortingMode == SortingMode.SortByOrder)
  234. {
  235. _meshRenderer.sortingOrder = _zOrder * UnityArmatureComponent.ORDER_SPACE;
  236. }
  237. else
  238. {
  239. _meshRenderer.sortingOrder = _proxy.sortingOrder;
  240. }
  241. }
  242. else
  243. {
  244. var childArmatureComp = childArmature.proxy as UnityArmatureComponent;
  245. childArmatureComp._sortingMode = _proxy._sortingMode;
  246. childArmatureComp._sortingLayerName = _proxy._sortingLayerName;
  247. if (_proxy._sortingMode == SortingMode.SortByOrder)
  248. {
  249. childArmatureComp.sortingOrder = _zOrder * UnityArmatureComponent.ORDER_SPACE;
  250. }
  251. else
  252. {
  253. childArmatureComp.sortingOrder = _proxy._sortingOrder;
  254. }
  255. }
  256. }
  257. }
  258. }
  259. public void DisallowCombineMesh()
  260. {
  261. this.CancelCombineMesh();
  262. this._isIgnoreCombineMesh = true;
  263. }
  264. internal void CancelCombineMesh()
  265. {
  266. if (this._isCombineMesh)
  267. {
  268. this._isCombineMesh = false;
  269. if (this._meshFilter != null)
  270. {
  271. this._meshFilter.sharedMesh = this._meshBuffer.sharedMesh;
  272. var isSkinnedMesh = this._deformVertices != null && this._deformVertices.verticesData != null && this._deformVertices.verticesData.weight != null;
  273. if (!isSkinnedMesh)
  274. {
  275. this._meshBuffer.rawVertextBuffers.CopyTo(this._meshBuffer.vertexBuffers, 0);
  276. }
  277. //
  278. this._meshBuffer.UpdateVertices();
  279. this._meshBuffer.UpdateColors();
  280. if (isSkinnedMesh)
  281. {
  282. this._UpdateMesh();
  283. this._IdentityTransform();
  284. }
  285. else
  286. {
  287. this._UpdateTransform();
  288. }
  289. }
  290. this._meshBuffer.enabled = true;
  291. }
  292. if (this._renderDisplay != null)
  293. {
  294. if (this._childArmature != null)
  295. {
  296. this._renderDisplay.SetActive(true);
  297. }
  298. else
  299. {
  300. this._renderDisplay.SetActive(this._isActive);
  301. }
  302. //
  303. this._renderDisplay.hideFlags = HideFlags.None;
  304. }
  305. //
  306. this._isCombineMesh = false;
  307. this._sumMeshIndex = -1;
  308. this._verticeOrder = -1;
  309. this._verticeOffset = -1;
  310. // this._combineMesh = null;
  311. }
  312. //
  313. private void _CombineMesh()
  314. {
  315. //引起合并的条件,Display改变,混合模式改变,Visible改变,Zorder改变
  316. //已经关闭合并,不再考虑
  317. if (this._isIgnoreCombineMesh || this._proxy.isUGUI)
  318. {
  319. return;
  320. }
  321. //已经合并过了,又触发合并,那么打断合并,用自己的网格数据还原
  322. if (this._isCombineMesh)
  323. {
  324. //已经合并过,除非满足一下情况,否则都不能再合并, TODO
  325. this.CancelCombineMesh();
  326. this._isIgnoreCombineMesh = true;
  327. }
  328. var combineMeshComp = this._proxy.GetComponent<UnityCombineMeshs>();
  329. //从来没有合并过,触发合并,那么尝试合并
  330. if (combineMeshComp != null)
  331. {
  332. combineMeshComp.dirty = true;
  333. }
  334. }
  335. /**
  336. * @private
  337. */
  338. internal override void _UpdateVisible()
  339. {
  340. this._renderDisplay.SetActive(this._parent.visible);
  341. if (this._isCombineMesh && !this._parent.visible)
  342. {
  343. this._CombineMesh();
  344. }
  345. }
  346. /**
  347. * @private
  348. */
  349. internal override void _UpdateBlendMode()
  350. {
  351. if (this._currentBlendMode == this._blendMode)
  352. {
  353. return;
  354. }
  355. if (this._childArmature == null)
  356. {
  357. if (this._uiDisplay != null)
  358. {
  359. this._uiDisplay.material = (this._textureData as UnityTextureData).GetMaterial(this._blendMode, true);
  360. }
  361. else
  362. {
  363. this._meshRenderer.sharedMaterial = (this._textureData as UnityTextureData).GetMaterial(this._blendMode);
  364. }
  365. this._meshBuffer.name = this._uiDisplay != null ? this._uiDisplay.material.name : this._meshRenderer.sharedMaterial.name;
  366. }
  367. else
  368. {
  369. foreach (var slot in _childArmature.GetSlots())
  370. {
  371. slot._blendMode = this._blendMode;
  372. slot._UpdateBlendMode();
  373. }
  374. }
  375. this._currentBlendMode = this._blendMode;
  376. this._CombineMesh();
  377. }
  378. /**
  379. * @private
  380. */
  381. protected override void _UpdateColor()
  382. {
  383. if (this._childArmature == null)
  384. {
  385. var proxyTrans = _proxy._colorTransform;
  386. if (this._isCombineMesh)
  387. {
  388. var meshBuffer = this._combineMesh.meshBuffers[this._sumMeshIndex];
  389. for (var i = 0; i < this._meshBuffer.vertexBuffers.Length; i++)
  390. {
  391. var index = this._verticeOffset + i;
  392. this._meshBuffer.color32Buffers[i].r = (byte)(_colorTransform.redMultiplier * proxyTrans.redMultiplier * 255);
  393. this._meshBuffer.color32Buffers[i].g = (byte)(_colorTransform.greenMultiplier * proxyTrans.greenMultiplier * 255);
  394. this._meshBuffer.color32Buffers[i].b = (byte)(_colorTransform.blueMultiplier * proxyTrans.blueMultiplier * 255);
  395. this._meshBuffer.color32Buffers[i].a = (byte)(_colorTransform.alphaMultiplier * proxyTrans.alphaMultiplier * 255);
  396. //
  397. meshBuffer.color32Buffers[index] = this._meshBuffer.color32Buffers[i];
  398. }
  399. meshBuffer.UpdateColors();
  400. }
  401. else if (this._meshBuffer.sharedMesh != null)
  402. {
  403. for (int i = 0, l = this._meshBuffer.sharedMesh.vertexCount; i < l; ++i)
  404. {
  405. this._meshBuffer.color32Buffers[i].r = (byte)(_colorTransform.redMultiplier * proxyTrans.redMultiplier * 255);
  406. this._meshBuffer.color32Buffers[i].g = (byte)(_colorTransform.greenMultiplier * proxyTrans.greenMultiplier * 255);
  407. this._meshBuffer.color32Buffers[i].b = (byte)(_colorTransform.blueMultiplier * proxyTrans.blueMultiplier * 255);
  408. this._meshBuffer.color32Buffers[i].a = (byte)(_colorTransform.alphaMultiplier * proxyTrans.alphaMultiplier * 255);
  409. }
  410. //
  411. this._meshBuffer.UpdateColors();
  412. }
  413. }
  414. else
  415. {
  416. //Set all childArmature color dirty
  417. (this._childArmature.proxy as UnityArmatureComponent).color = _colorTransform;
  418. }
  419. }
  420. /**
  421. * @private
  422. */
  423. protected override void _UpdateFrame()
  424. {
  425. var currentVerticesData = (this._deformVertices != null && this._display == this._meshDisplay) ? this._deformVertices.verticesData : null;
  426. var currentTextureData = this._textureData as UnityTextureData;
  427. this._meshBuffer.Clear();
  428. this._isActive = false;
  429. if (this._displayIndex >= 0 && this._display != null && currentTextureData != null)
  430. {
  431. var currentTextureAtlas = _proxy.isUGUI ? currentTextureAtlasData.uiTexture : currentTextureAtlasData.texture;
  432. if (currentTextureAtlas != null)
  433. {
  434. this._isActive = true;
  435. //
  436. var textureAtlasWidth = currentTextureAtlasData.width > 0.0f ? (int)currentTextureAtlasData.width : currentTextureAtlas.mainTexture.width;
  437. var textureAtlasHeight = currentTextureAtlasData.height > 0.0f ? (int)currentTextureAtlasData.height : currentTextureAtlas.mainTexture.height;
  438. var textureScale = _armature.armatureData.scale * currentTextureData.parent.scale;
  439. var sourceX = currentTextureData.region.x;
  440. var sourceY = currentTextureData.region.y;
  441. var sourceWidth = currentTextureData.region.width;
  442. var sourceHeight = currentTextureData.region.height;
  443. if (currentVerticesData != null)
  444. {
  445. var data = currentVerticesData.data;
  446. var meshOffset = currentVerticesData.offset;
  447. var intArray = data.intArray;
  448. var floatArray = data.floatArray;
  449. var vertexCount = intArray[meshOffset + (int)BinaryOffset.MeshVertexCount];
  450. var triangleCount = intArray[meshOffset + (int)BinaryOffset.MeshTriangleCount];
  451. int vertexOffset = intArray[meshOffset + (int)BinaryOffset.MeshFloatOffset];
  452. if (vertexOffset < 0)
  453. {
  454. vertexOffset += 65536; // Fixed out of bouds bug.
  455. }
  456. var uvOffset = vertexOffset + vertexCount * 2;
  457. if (this._meshBuffer.uvBuffers == null || this._meshBuffer.uvBuffers.Length != vertexCount)
  458. {
  459. this._meshBuffer.uvBuffers = new Vector2[vertexCount];
  460. }
  461. if (this._meshBuffer.rawVertextBuffers == null || this._meshBuffer.rawVertextBuffers.Length != vertexCount)
  462. {
  463. this._meshBuffer.rawVertextBuffers = new Vector3[vertexCount];
  464. this._meshBuffer.vertexBuffers = new Vector3[vertexCount];
  465. }
  466. this._meshBuffer.triangleBuffers = new int[triangleCount * 3];
  467. for (int i = 0, iV = vertexOffset, iU = uvOffset, l = vertexCount; i < l; ++i)
  468. {
  469. this._meshBuffer.uvBuffers[i].x = (sourceX + floatArray[iU++] * sourceWidth) / textureAtlasWidth;
  470. this._meshBuffer.uvBuffers[i].y = 1.0f - (sourceY + floatArray[iU++] * sourceHeight) / textureAtlasHeight;
  471. this._meshBuffer.rawVertextBuffers[i].x = floatArray[iV++] * textureScale;
  472. this._meshBuffer.rawVertextBuffers[i].y = floatArray[iV++] * textureScale;
  473. this._meshBuffer.vertexBuffers[i].x = this._meshBuffer.rawVertextBuffers[i].x;
  474. this._meshBuffer.vertexBuffers[i].y = this._meshBuffer.rawVertextBuffers[i].y;
  475. }
  476. for (int i = 0; i < triangleCount * 3; ++i)
  477. {
  478. this._meshBuffer.triangleBuffers[i] = intArray[meshOffset + (int)BinaryOffset.MeshVertexIndices + i];
  479. }
  480. var isSkinned = currentVerticesData.weight != null;
  481. if (isSkinned)
  482. {
  483. this._IdentityTransform();
  484. }
  485. }
  486. else
  487. {
  488. if (this._meshBuffer.rawVertextBuffers == null || this._meshBuffer.rawVertextBuffers.Length != 4)
  489. {
  490. this._meshBuffer.rawVertextBuffers = new Vector3[4];
  491. this._meshBuffer.vertexBuffers = new Vector3[4];
  492. }
  493. if (this._meshBuffer.uvBuffers == null || this._meshBuffer.uvBuffers.Length != this._meshBuffer.rawVertextBuffers.Length)
  494. {
  495. this._meshBuffer.uvBuffers = new Vector2[this._meshBuffer.rawVertextBuffers.Length];
  496. }
  497. // Normal texture.
  498. for (int i = 0, l = 4; i < l; ++i)
  499. {
  500. var u = 0.0f;
  501. var v = 0.0f;
  502. switch (i)
  503. {
  504. case 0:
  505. break;
  506. case 1:
  507. u = 1.0f;
  508. break;
  509. case 2:
  510. u = 1.0f;
  511. v = 1.0f;
  512. break;
  513. case 3:
  514. v = 1.0f;
  515. break;
  516. default:
  517. break;
  518. }
  519. var scaleWidth = sourceWidth * textureScale;
  520. var scaleHeight = sourceHeight * textureScale;
  521. var pivotX = _pivotX;
  522. var pivotY = _pivotY;
  523. if (currentTextureData.rotated)
  524. {
  525. var temp = scaleWidth;
  526. scaleWidth = scaleHeight;
  527. scaleHeight = temp;
  528. pivotX = scaleWidth - _pivotX;
  529. pivotY = scaleHeight - _pivotY;
  530. //uv
  531. this._meshBuffer.uvBuffers[i].x = (sourceX + (1.0f - v) * sourceWidth) / textureAtlasWidth;
  532. this._meshBuffer.uvBuffers[i].y = 1.0f - (sourceY + u * sourceHeight) / textureAtlasHeight;
  533. }
  534. else
  535. {
  536. //uv
  537. this._meshBuffer.uvBuffers[i].x = (sourceX + u * sourceWidth) / textureAtlasWidth;
  538. this._meshBuffer.uvBuffers[i].y = 1.0f - (sourceY + v * sourceHeight) / textureAtlasHeight;
  539. }
  540. //vertices
  541. this._meshBuffer.rawVertextBuffers[i].x = u * scaleWidth - pivotX;
  542. this._meshBuffer.rawVertextBuffers[i].y = (1.0f - v) * scaleHeight - pivotY;
  543. this._meshBuffer.vertexBuffers[i].x = this._meshBuffer.rawVertextBuffers[i].x;
  544. this._meshBuffer.vertexBuffers[i].y = this._meshBuffer.rawVertextBuffers[i].y;
  545. }
  546. this._meshBuffer.triangleBuffers = TRIANGLES;
  547. }
  548. if (_proxy.isUGUI)
  549. {
  550. this._uiDisplay.material = currentTextureAtlas;
  551. this._uiDisplay.texture = currentTextureAtlas.mainTexture;
  552. this._uiDisplay.sharedMesh = this._meshBuffer.sharedMesh;
  553. }
  554. else
  555. {
  556. this._meshFilter.sharedMesh = this._meshBuffer.sharedMesh;
  557. this._meshRenderer.sharedMaterial = currentTextureAtlas;
  558. }
  559. this._meshBuffer.name = currentTextureAtlas.name;
  560. this._meshBuffer.InitMesh();
  561. this._currentBlendMode = BlendMode.Normal;
  562. this._blendModeDirty = true;
  563. this._colorDirty = true;// Relpace texture will override blendMode and color.
  564. this._visibleDirty = true;
  565. this._CombineMesh();
  566. return;
  567. }
  568. }
  569. this._renderDisplay.SetActive(this._isActive);
  570. if (_proxy.isUGUI)
  571. {
  572. this._uiDisplay.material = null;
  573. this._uiDisplay.texture = null;
  574. this._uiDisplay.sharedMesh = null;
  575. }
  576. else
  577. {
  578. this._meshFilter.sharedMesh = null;
  579. this._meshRenderer.sharedMaterial = null;
  580. }
  581. _helpVector3.x = 0.0f;
  582. _helpVector3.y = 0.0f;
  583. _helpVector3.z = this._renderDisplay.transform.localPosition.z;
  584. this._renderDisplay.transform.localPosition = _helpVector3;
  585. if (this._isCombineMesh)
  586. {
  587. this._CombineMesh();
  588. }
  589. }
  590. protected override void _IdentityTransform()
  591. {
  592. var transform = this._renderDisplay.transform;
  593. transform.localPosition = new Vector3(0.0f, 0.0f, transform.localPosition.z);
  594. transform.localEulerAngles = Vector3.zero;
  595. transform.localScale = Vector3.one;
  596. }
  597. protected override void _UpdateMesh()
  598. {
  599. if (this._meshBuffer.sharedMesh == null || this._deformVertices == null)
  600. {
  601. return;
  602. }
  603. var scale = this._armature.armatureData.scale;
  604. var deformVertices = this._deformVertices.vertices;
  605. var bones = this._deformVertices.bones;
  606. var hasDeform = deformVertices.Count > 0;
  607. var verticesData = this._deformVertices.verticesData;
  608. var weightData = verticesData.weight;
  609. var data = verticesData.data;
  610. var intArray = data.intArray;
  611. var floatArray = data.floatArray;
  612. var vertextCount = intArray[verticesData.offset + (int)BinaryOffset.MeshVertexCount];
  613. if (weightData != null)
  614. {
  615. int weightFloatOffset = intArray[weightData.offset + 1/*(int)BinaryOffset.MeshWeightOffset*/];
  616. if (weightFloatOffset < 0)
  617. {
  618. weightFloatOffset += 65536; // Fixed out of bouds bug.
  619. }
  620. MeshBuffer meshBuffer = null;
  621. if (this._isCombineMesh)
  622. {
  623. meshBuffer = this._combineMesh.meshBuffers[this._sumMeshIndex];
  624. }
  625. int iB = weightData.offset + (int)BinaryOffset.WeigthBoneIndices + weightData.bones.Count, iV = weightFloatOffset, iF = 0;
  626. for (int i = 0; i < vertextCount; ++i)
  627. {
  628. var boneCount = intArray[iB++];
  629. float xG = 0.0f, yG = 0.0f;
  630. for (var j = 0; j < boneCount; ++j)
  631. {
  632. var boneIndex = intArray[iB++];
  633. var bone = bones[boneIndex];
  634. if (bone != null)
  635. {
  636. var matrix = bone.globalTransformMatrix;
  637. var weight = floatArray[iV++];
  638. var xL = floatArray[iV++] * scale;
  639. var yL = floatArray[iV++] * scale;
  640. if (hasDeform)
  641. {
  642. xL += deformVertices[iF++];
  643. yL += deformVertices[iF++];
  644. }
  645. xG += (matrix.a * xL + matrix.c * yL + matrix.tx) * weight;
  646. yG += (matrix.b * xL + matrix.d * yL + matrix.ty) * weight;
  647. }
  648. }
  649. this._meshBuffer.vertexBuffers[i].x = xG;
  650. this._meshBuffer.vertexBuffers[i].y = yG;
  651. if (meshBuffer != null)
  652. {
  653. meshBuffer.vertexBuffers[i + this._verticeOffset].x = xG;
  654. meshBuffer.vertexBuffers[i + this._verticeOffset].y = yG;
  655. }
  656. }
  657. if (meshBuffer != null)
  658. {
  659. meshBuffer.vertexDirty = true;
  660. }
  661. else
  662. {
  663. // if (this._meshRenderer && this._meshRenderer.enabled)
  664. {
  665. this._meshBuffer.UpdateVertices();
  666. }
  667. }
  668. }
  669. else if (deformVertices.Count > 0)
  670. {
  671. int vertexOffset = data.intArray[verticesData.offset + (int)BinaryOffset.MeshFloatOffset];
  672. if (vertexOffset < 0)
  673. {
  674. vertexOffset += 65536; // Fixed out of bouds bug.
  675. }
  676. //
  677. var a = globalTransformMatrix.a;
  678. var b = globalTransformMatrix.b;
  679. var c = globalTransformMatrix.c;
  680. var d = globalTransformMatrix.d;
  681. var tx = globalTransformMatrix.tx;
  682. var ty = globalTransformMatrix.ty;
  683. var index = 0;
  684. var rx = 0.0f;
  685. var ry = 0.0f;
  686. var vx = 0.0f;
  687. var vy = 0.0f;
  688. MeshBuffer meshBuffer = null;
  689. if (this._isCombineMesh)
  690. {
  691. meshBuffer = this._combineMesh.meshBuffers[this._sumMeshIndex];
  692. }
  693. for (int i = 0, iV = 0, iF = 0, l = vertextCount; i < l; ++i)
  694. {
  695. rx = (data.floatArray[vertexOffset + (iV++)] * scale + deformVertices[iF++]);
  696. ry = (data.floatArray[vertexOffset + (iV++)] * scale + deformVertices[iF++]);
  697. this._meshBuffer.rawVertextBuffers[i].x = rx;
  698. this._meshBuffer.rawVertextBuffers[i].y = -ry;
  699. this._meshBuffer.vertexBuffers[i].x = rx;
  700. this._meshBuffer.vertexBuffers[i].y = -ry;
  701. if (meshBuffer != null)
  702. {
  703. index = i + this._verticeOffset;
  704. vx = (rx * a + ry * c + tx);
  705. vy = (rx * b + ry * d + ty);
  706. meshBuffer.vertexBuffers[index].x = vx;
  707. meshBuffer.vertexBuffers[index].y = vy;
  708. }
  709. }
  710. if (meshBuffer != null)
  711. {
  712. meshBuffer.vertexDirty = true;
  713. }
  714. // else if (this._meshRenderer && this._meshRenderer.enabled)
  715. else
  716. {
  717. this._meshBuffer.UpdateVertices();
  718. }
  719. }
  720. }
  721. protected override void _UpdateTransform()
  722. {
  723. if (this._isCombineMesh)
  724. {
  725. var a = globalTransformMatrix.a;
  726. var b = globalTransformMatrix.b;
  727. var c = globalTransformMatrix.c;
  728. var d = globalTransformMatrix.d;
  729. var tx = globalTransformMatrix.tx;
  730. var ty = globalTransformMatrix.ty;
  731. var index = 0;
  732. var rx = 0.0f;
  733. var ry = 0.0f;
  734. var vx = 0.0f;
  735. var vy = 0.0f;
  736. var meshBuffer = this._combineMesh.meshBuffers[this._sumMeshIndex];
  737. for (int i = 0, l = this._meshBuffer.vertexBuffers.Length; i < l; i++)
  738. {
  739. index = i + this._verticeOffset;
  740. //vertices
  741. rx = this._meshBuffer.rawVertextBuffers[i].x;
  742. ry = -this._meshBuffer.rawVertextBuffers[i].y;
  743. vx = rx * a + ry * c + tx;
  744. vy = rx * b + ry * d + ty;
  745. this._meshBuffer.vertexBuffers[i].x = vx;
  746. this._meshBuffer.vertexBuffers[i].y = vy;
  747. meshBuffer.vertexBuffers[index].x = vx;
  748. meshBuffer.vertexBuffers[index].y = vy;
  749. }
  750. //
  751. meshBuffer.vertexDirty = true;
  752. }
  753. else
  754. {
  755. this.UpdateGlobalTransform(); // Update transform.
  756. //localPosition
  757. var flipX = _armature.flipX;
  758. var flipY = _armature.flipY;
  759. var transform = _renderDisplay.transform;
  760. _helpVector3.x = global.x;
  761. _helpVector3.y = global.y;
  762. _helpVector3.z = transform.localPosition.z;
  763. transform.localPosition = _helpVector3;
  764. //localEulerAngles
  765. if (_childArmature == null)
  766. {
  767. _helpVector3.x = flipY ? 180.0f : 0.0f;
  768. _helpVector3.y = flipX ? 180.0f : 0.0f;
  769. _helpVector3.z = global.rotation * TransformDB.RAD_DEG;
  770. }
  771. else
  772. {
  773. //If the childArmature is not null,
  774. //X, Y axis can not flip in the container of the childArmature container,
  775. //because after the flip, the Z value of the child slot is reversed,
  776. //showing the order is wrong, only in the child slot to deal with X, Y axis flip
  777. _helpVector3.x = 0.0f;
  778. _helpVector3.y = 0.0f;
  779. _helpVector3.z = global.rotation * TransformDB.RAD_DEG;
  780. //这里这样处理,是因为子骨架的插槽也要处理z值,那就在容器中反一下,子插槽再正过来
  781. if (flipX != flipY)
  782. {
  783. _helpVector3.z = -_helpVector3.z;
  784. }
  785. }
  786. if (flipX || flipY)
  787. {
  788. if (flipX && flipY)
  789. {
  790. _helpVector3.z += 180.0f;
  791. }
  792. else
  793. {
  794. if (flipX)
  795. {
  796. _helpVector3.z = 180.0f - _helpVector3.z;
  797. }
  798. else
  799. {
  800. _helpVector3.z = -_helpVector3.z;
  801. }
  802. }
  803. }
  804. transform.localEulerAngles = _helpVector3;
  805. //Modify mesh skew. // TODO child armature skew.
  806. if ((this._display == this._rawDisplay || this._display == this._meshDisplay) && this._meshBuffer.sharedMesh != null)
  807. {
  808. var skew = global.skew;
  809. var dSkew = skew;
  810. if (flipX && flipY)
  811. {
  812. dSkew = -skew + TransformDB.PI;
  813. }
  814. else if (!flipX && !flipY)
  815. {
  816. dSkew = -skew - TransformDB.PI;
  817. }
  818. var skewed = dSkew < -0.01f || 0.01f < dSkew;
  819. if (_skewed || skewed)
  820. {
  821. _skewed = skewed;
  822. var isPositive = global.scaleX >= 0.0f;
  823. var cos = Mathf.Cos(dSkew);
  824. var sin = Mathf.Sin(dSkew);
  825. var x = 0.0f;
  826. var y = 0.0f;
  827. for (int i = 0, l = this._meshBuffer.vertexBuffers.Length; i < l; ++i)
  828. {
  829. x = this._meshBuffer.rawVertextBuffers[i].x;
  830. y = this._meshBuffer.rawVertextBuffers[i].y;
  831. if (isPositive)
  832. {
  833. this._meshBuffer.vertexBuffers[i].x = x + y * sin;
  834. }
  835. else
  836. {
  837. this._meshBuffer.vertexBuffers[i].x = -x + y * sin;
  838. }
  839. this._meshBuffer.vertexBuffers[i].y = y * cos;
  840. }
  841. // if (this._meshRenderer && this._meshRenderer.enabled)
  842. {
  843. this._meshBuffer.UpdateVertices();
  844. }
  845. }
  846. }
  847. //localScale
  848. _helpVector3.x = global.scaleX;
  849. _helpVector3.y = global.scaleY;
  850. _helpVector3.z = 1.0f;
  851. transform.localScale = _helpVector3;
  852. }
  853. if (_childArmature != null)
  854. {
  855. _childArmature.flipX = _armature.flipX;
  856. _childArmature.flipY = _armature.flipY;
  857. }
  858. }
  859. public Mesh mesh
  860. {
  861. get
  862. {
  863. if (this._meshBuffer == null)
  864. {
  865. return null;
  866. }
  867. return this._meshBuffer.sharedMesh;
  868. }
  869. }
  870. public MeshRenderer meshRenderer
  871. {
  872. get { return this._meshRenderer; }
  873. }
  874. public UnityTextureAtlasData currentTextureAtlasData
  875. {
  876. get
  877. {
  878. if (this._textureData == null || this._textureData.parent == null)
  879. {
  880. return null;
  881. }
  882. return this._textureData.parent as UnityTextureAtlasData;
  883. }
  884. }
  885. public GameObject renderDisplay
  886. {
  887. get { return this._renderDisplay; }
  888. }
  889. public UnityArmatureComponent proxy
  890. {
  891. get { return this._proxy; }
  892. }
  893. public bool isIgnoreCombineMesh
  894. {
  895. get { return this._isIgnoreCombineMesh; }
  896. }
  897. }
  898. }