Bone.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648
  1. using System;
  2. using System.Collections.Generic;
  3. namespace DragonBones
  4. {
  5. /**
  6. * @private
  7. */
  8. internal enum BoneTransformDirty
  9. {
  10. None = 0,
  11. Self = 1,
  12. All = 2
  13. }
  14. /**
  15. * @language zh_CN
  16. * 骨骼,一个骨架中可以包含多个骨骼,骨骼以树状结构组成骨架。
  17. * 骨骼在骨骼动画体系中是最重要的逻辑单元之一,负责动画中的平移旋转缩放的实现。
  18. * @see DragonBones.BoneData
  19. * @see DragonBones.Armature
  20. * @see DragonBones.Slot
  21. * @version DragonBones 3.0
  22. */
  23. public class Bone : TransformObject
  24. {
  25. /**
  26. * @language zh_CN
  27. * 是否继承父骨骼的平移。
  28. * @version DragonBones 3.0
  29. */
  30. public bool inheritTranslation;
  31. /**
  32. * @language zh_CN
  33. * 是否继承父骨骼的旋转。
  34. * @version DragonBones 3.0
  35. */
  36. public bool inheritRotation;
  37. /**
  38. * @language zh_CN
  39. * 是否继承父骨骼的缩放。
  40. * @version DragonBones 4.5
  41. */
  42. public bool inheritScale;
  43. /**
  44. * @private
  45. */
  46. public bool ikBendPositive;
  47. /**
  48. * @language zh_CN
  49. * 骨骼长度。
  50. * @version DragonBones 4.5
  51. */
  52. public float length;
  53. /**
  54. * @private
  55. */
  56. public float ikWeight;
  57. /**
  58. * @private
  59. */
  60. internal BoneTransformDirty _transformDirty;
  61. private bool _visible;
  62. private int _cachedFrameIndex;
  63. private uint _ikChain;
  64. private int _ikChainIndex;
  65. /**
  66. * @private
  67. */
  68. internal int _updateState;
  69. /**
  70. * @private
  71. */
  72. internal int _blendLayer;
  73. /**
  74. * @private
  75. */
  76. internal float _blendLeftWeight;
  77. /**
  78. * @private
  79. */
  80. internal float _blendTotalWeight;
  81. /**
  82. * @private
  83. */
  84. internal readonly Transform _animationPose = new Transform();
  85. private readonly List<Bone> _bones = new List<Bone>();
  86. private readonly List<Slot> _slots = new List<Slot>();
  87. private BoneData _boneData;
  88. private Bone _ik;
  89. /**
  90. * @private
  91. */
  92. internal List<int> _cachedFrameIndices;
  93. /**
  94. * @private
  95. */
  96. public Bone()
  97. {
  98. }
  99. /**
  100. * @private
  101. */
  102. protected override void _onClear()
  103. {
  104. base._onClear();
  105. inheritTranslation = false;
  106. inheritRotation = false;
  107. inheritScale = false;
  108. ikBendPositive = false;
  109. length = 0.0f;
  110. ikWeight = 0.0f;
  111. _transformDirty = BoneTransformDirty.None;
  112. _visible = true;
  113. _cachedFrameIndex = -1;
  114. _ikChain = 0;
  115. _ikChainIndex = 0;
  116. _updateState = -1;
  117. _blendLayer = 0;
  118. _blendLeftWeight = 0.0f;
  119. _blendTotalWeight = 0.0f;
  120. _animationPose.Identity();
  121. _bones.Clear();
  122. _slots.Clear();
  123. _boneData = null;
  124. _ik = null;
  125. _cachedFrameIndices = null;
  126. }
  127. /**
  128. * @private
  129. */
  130. private void _updateGlobalTransformMatrix()
  131. {
  132. global.x = origin.x + offset.x + _animationPose.x;
  133. global.y = origin.y + offset.y + _animationPose.y;
  134. global.skewX = origin.skewX + offset.skewX + _animationPose.skewX;
  135. global.skewY = origin.skewY + offset.skewY + _animationPose.skewY;
  136. global.scaleX = origin.scaleX * offset.scaleX * _animationPose.scaleX;
  137. global.scaleY = origin.scaleY * offset.scaleY * _animationPose.scaleY;
  138. if (_parent != null)
  139. {
  140. var parentRotation = _parent.global.skewY; // Only inherit skew y.
  141. var parentMatrix = _parent.globalTransformMatrix;
  142. if (inheritScale)
  143. {
  144. if (!inheritRotation)
  145. {
  146. global.skewX -= parentRotation;
  147. global.skewY -= parentRotation;
  148. }
  149. global.ToMatrix(globalTransformMatrix);
  150. globalTransformMatrix.Concat(parentMatrix);
  151. if (!inheritTranslation)
  152. {
  153. globalTransformMatrix.tx = global.x;
  154. globalTransformMatrix.ty = global.y;
  155. }
  156. global.FromMatrix(globalTransformMatrix);
  157. }
  158. else
  159. {
  160. if (inheritTranslation)
  161. {
  162. var x = global.x;
  163. var y = global.y;
  164. global.x = parentMatrix.a * x + parentMatrix.c * y + parentMatrix.tx;
  165. global.y = parentMatrix.d * y + parentMatrix.b * x + parentMatrix.ty;
  166. }
  167. if (inheritRotation)
  168. {
  169. global.skewX += parentRotation;
  170. global.skewY += parentRotation;
  171. }
  172. global.ToMatrix(globalTransformMatrix);
  173. }
  174. }
  175. else
  176. {
  177. global.ToMatrix(globalTransformMatrix);
  178. }
  179. if (_ik != null && _ikChainIndex == _ikChain && ikWeight > 0.0f)
  180. {
  181. if (inheritTranslation && _ikChain > 0 && _parent != null)
  182. {
  183. _computeIKB();
  184. }
  185. else
  186. {
  187. _computeIKA();
  188. }
  189. }
  190. }
  191. /**
  192. * @private
  193. */
  194. private void _computeIKA()
  195. {
  196. var ikGlobal = _ik.global;
  197. var x = globalTransformMatrix.a * length;
  198. var y = globalTransformMatrix.b * length;
  199. var ikRadian =
  200. (float)(
  201. Math.Atan2(ikGlobal.y - global.y, ikGlobal.x - global.x) +
  202. offset.skewY -
  203. global.skewY * 2.0f +
  204. Math.Atan2(y, x)
  205. ) * ikWeight; // Support offset.
  206. global.skewX += ikRadian;
  207. global.skewY += ikRadian;
  208. global.ToMatrix(globalTransformMatrix);
  209. }
  210. /**
  211. * @private
  212. */
  213. private void _computeIKB()
  214. {
  215. var parentGlobal = _parent.global;
  216. var ikGlobal = _ik.global;
  217. var x = globalTransformMatrix.a * length;
  218. var y = globalTransformMatrix.b * length;
  219. var lLL = x * x + y * y;
  220. var lL = (float)Math.Sqrt(lLL);
  221. var dX = global.x - parentGlobal.x;
  222. var dY = global.y - parentGlobal.y;
  223. var lPP = dX * dX + dY * dY;
  224. var lP = (float)Math.Sqrt(lPP);
  225. dX = ikGlobal.x - parentGlobal.x;
  226. dY = ikGlobal.y - parentGlobal.y;
  227. var lTT = dX * dX + dY * dY;
  228. var lT = (float)Math.Sqrt(lTT);
  229. var ikRadianA = 0.0f;
  230. if (lL + lP <= lT || lT + lL <= lP || lT + lP <= lL)
  231. {
  232. ikRadianA = (float)Math.Atan2(ikGlobal.y - parentGlobal.y, ikGlobal.x - parentGlobal.x) + _parent.offset.skewY; // Support offset.
  233. if (lL + lP <= lT)
  234. {
  235. }
  236. else if (lP < lL)
  237. {
  238. ikRadianA += DragonBones.PI;
  239. }
  240. }
  241. else
  242. {
  243. var h = (lPP - lLL + lTT) / (2.0f * lTT);
  244. var r = (float)Math.Sqrt(lPP - h * h * lTT) / lT;
  245. var hX = parentGlobal.x + (dX * h);
  246. var hY = parentGlobal.y + (dY * h);
  247. var rX = -dY * r;
  248. var rY = dX * r;
  249. if (ikBendPositive)
  250. {
  251. global.x = hX - rX;
  252. global.y = hY - rY;
  253. }
  254. else
  255. {
  256. global.x = hX + rX;
  257. global.y = hY + rY;
  258. }
  259. ikRadianA = (float)Math.Atan2(global.y - parentGlobal.y, global.x - parentGlobal.x) + _parent.offset.skewY; // Support offset.
  260. }
  261. ikRadianA = (ikRadianA - parentGlobal.skewY) * ikWeight;
  262. parentGlobal.skewX += ikRadianA;
  263. parentGlobal.skewY += ikRadianA;
  264. parentGlobal.ToMatrix(_parent.globalTransformMatrix);
  265. _parent._transformDirty = BoneTransformDirty.Self;
  266. global.x = parentGlobal.x + (float)Math.Cos(parentGlobal.skewY) * lP;
  267. global.y = parentGlobal.y + (float)Math.Sin(parentGlobal.skewY) * lP;
  268. var ikRadianB =
  269. (float)(
  270. Math.Atan2(ikGlobal.y - global.y, ikGlobal.x - global.x) + offset.skewY -
  271. global.skewY * 2 + Math.Atan2(y, x)
  272. ) * ikWeight; // Support offset.
  273. global.skewX += ikRadianB;
  274. global.skewY += ikRadianB;
  275. global.ToMatrix(globalTransformMatrix);
  276. }
  277. /**
  278. * @private
  279. */
  280. internal void _init(BoneData boneData)
  281. {
  282. if (_boneData != null) {
  283. return;
  284. }
  285. _boneData = boneData;
  286. inheritTranslation = _boneData.inheritTranslation;
  287. inheritRotation = _boneData.inheritRotation;
  288. inheritScale = _boneData.inheritScale;
  289. length = _boneData.length;
  290. name = _boneData.name;
  291. origin =_boneData.transform;
  292. }
  293. /**
  294. * @private
  295. */
  296. internal override void _setArmature(Armature value)
  297. {
  298. if (_armature == value)
  299. {
  300. return;
  301. }
  302. _ik = null;
  303. List<Slot> oldSlots = null;
  304. List<Bone> oldBones = null;
  305. if (_armature != null)
  306. {
  307. oldSlots = GetSlots();
  308. oldBones = GetBones();
  309. _armature._removeBoneFromBoneList(this);
  310. }
  311. _armature = value;
  312. if (_armature != null)
  313. {
  314. _armature._addBoneToBoneList(this);
  315. }
  316. if (oldSlots != null)
  317. {
  318. foreach (var slot in oldSlots)
  319. {
  320. if (slot.parent == this)
  321. {
  322. slot._setArmature(_armature);
  323. }
  324. }
  325. }
  326. if (oldBones != null)
  327. {
  328. foreach (var bone in oldBones)
  329. {
  330. if (bone.parent == this)
  331. {
  332. bone._setArmature(_armature);
  333. }
  334. }
  335. }
  336. }
  337. /**
  338. * @private
  339. */
  340. internal void _setIK(Bone value, uint chain, int chainIndex)
  341. {
  342. if (value != null)
  343. {
  344. if (chain == chainIndex)
  345. {
  346. var chainEnd = _parent;
  347. if (chain > 0 && chainEnd != null)
  348. {
  349. chain = 1;
  350. }
  351. else
  352. {
  353. chain = 0;
  354. chainIndex = 0;
  355. chainEnd = this;
  356. }
  357. if (chainEnd == value || chainEnd.Contains(value))
  358. {
  359. value = null;
  360. chain = 0;
  361. chainIndex = 0;
  362. }
  363. else
  364. {
  365. var ancestor = value;
  366. while (ancestor.ik != null && ancestor.ikChain > 0)
  367. {
  368. if (chainEnd.Contains(ancestor.ik))
  369. {
  370. value = null;
  371. chain = 0;
  372. chainIndex = 0;
  373. break;
  374. }
  375. ancestor = ancestor.parent;
  376. }
  377. }
  378. }
  379. }
  380. else
  381. {
  382. chain = 0;
  383. chainIndex = 0;
  384. }
  385. _ik = value;
  386. _ikChain = chain;
  387. _ikChainIndex = chainIndex;
  388. if (_armature != null)
  389. {
  390. _armature._bonesDirty = true;
  391. }
  392. }
  393. /**
  394. * @private
  395. */
  396. internal void _update(int cacheFrameIndex)
  397. {
  398. _updateState = -1;
  399. if (cacheFrameIndex >= 0 && _cachedFrameIndices != null)
  400. {
  401. var cachedFrameIndex = _cachedFrameIndices[cacheFrameIndex];
  402. if (cachedFrameIndex >= 0 && _cachedFrameIndex == cachedFrameIndex) // Same cache.
  403. {
  404. _transformDirty = BoneTransformDirty.None;
  405. }
  406. else if (cachedFrameIndex >= 0.0f) // Has been Cached.
  407. {
  408. _transformDirty = BoneTransformDirty.All;
  409. _cachedFrameIndex = cachedFrameIndex;
  410. }
  411. else if (
  412. _transformDirty == BoneTransformDirty.All ||
  413. (_parent != null && _parent._transformDirty != BoneTransformDirty.None) ||
  414. (_ik != null && ikWeight > 0.0f && _ik._transformDirty != BoneTransformDirty.None)
  415. ) // Dirty.
  416. {
  417. _transformDirty = BoneTransformDirty.All;
  418. _cachedFrameIndex = -1;
  419. }
  420. else if (_cachedFrameIndex >= 0) // Same cache but not cached yet.
  421. {
  422. _transformDirty = BoneTransformDirty.None;
  423. _cachedFrameIndices[cacheFrameIndex] = _cachedFrameIndex;
  424. }
  425. else // Dirty.
  426. {
  427. _transformDirty = BoneTransformDirty.All;
  428. _cachedFrameIndex = -1;
  429. }
  430. }
  431. else if (
  432. _transformDirty == BoneTransformDirty.All ||
  433. (_parent != null && _parent._transformDirty != BoneTransformDirty.None) ||
  434. (_ik != null && ikWeight > 0.0f && _ik._transformDirty != BoneTransformDirty.None)
  435. )
  436. {
  437. cacheFrameIndex = -1;
  438. _transformDirty = BoneTransformDirty.All; // For update children and ik children.
  439. _cachedFrameIndex = -1;
  440. }
  441. if (_transformDirty != BoneTransformDirty.None)
  442. {
  443. if (_transformDirty == BoneTransformDirty.All)
  444. {
  445. _transformDirty = BoneTransformDirty.Self;
  446. if (_cachedFrameIndex < 0)
  447. {
  448. _updateGlobalTransformMatrix();
  449. if (cacheFrameIndex >= 0)
  450. {
  451. _cachedFrameIndex = _cachedFrameIndices[cacheFrameIndex] = _armature._armatureData.SetCacheFrame(globalTransformMatrix, global);
  452. }
  453. }
  454. else
  455. {
  456. _armature._armatureData.GetCacheFrame(globalTransformMatrix, global, _cachedFrameIndex);
  457. }
  458. _updateState = 0;
  459. }
  460. else
  461. {
  462. _transformDirty = BoneTransformDirty.None;
  463. }
  464. }
  465. }
  466. /**
  467. * @language zh_CN
  468. * 下一帧更新变换。 (当骨骼没有动画状态或动画状态播放完成时,骨骼将不在更新)
  469. * @version DragonBones 3.0
  470. */
  471. public void InvalidUpdate()
  472. {
  473. _transformDirty = BoneTransformDirty.All;
  474. }
  475. /**
  476. * @language zh_CN
  477. * 是否包含骨骼或插槽。
  478. * @returns
  479. * @see DragonBones.TransformObject
  480. * @version DragonBones 3.0
  481. */
  482. public bool Contains(TransformObject child)
  483. {
  484. if (child != null)
  485. {
  486. if (child == this)
  487. {
  488. return false;
  489. }
  490. var ancestor = child;
  491. while (ancestor != this && ancestor != null)
  492. {
  493. ancestor = ancestor.parent;
  494. }
  495. return ancestor == this;
  496. }
  497. return false;
  498. }
  499. /**
  500. * @language zh_CN
  501. * 所有的子骨骼。
  502. * @version DragonBones 3.0
  503. */
  504. public List<Bone> GetBones()
  505. {
  506. _bones.Clear();
  507. var bones = _armature.GetBones();
  508. foreach (var bone in bones)
  509. {
  510. if (bone.parent == this)
  511. {
  512. _bones.Add(bone);
  513. }
  514. }
  515. return _bones;
  516. }
  517. /**
  518. * @language zh_CN
  519. * 所有的插槽。
  520. * @see DragonBones.Slot
  521. * @version DragonBones 3.0
  522. */
  523. public List<Slot> GetSlots()
  524. {
  525. _slots.Clear();
  526. var slots = _armature.GetSlots();
  527. foreach (var slot in slots)
  528. {
  529. if (slot.parent == this)
  530. {
  531. _slots.Add(slot);
  532. }
  533. }
  534. return _slots;
  535. }
  536. /**
  537. * @deprecated
  538. */
  539. public BoneData boneData
  540. {
  541. get { return _boneData; }
  542. }
  543. /**
  544. * @language zh_CN
  545. * 控制此骨骼所有插槽的可见。
  546. * @default true
  547. * @see DragonBones.Slot
  548. * @version DragonBones 3.0
  549. */
  550. public bool visible
  551. {
  552. get { return _visible; }
  553. set
  554. {
  555. if (_visible == value)
  556. {
  557. return;
  558. }
  559. _visible = value;
  560. var slots = _armature.GetSlots();
  561. foreach (var slot in slots)
  562. {
  563. if (slot._parent == this)
  564. {
  565. slot._updateVisible();
  566. }
  567. }
  568. }
  569. }
  570. /**
  571. * @deprecated
  572. */
  573. public uint ikChain
  574. {
  575. get { return _ikChain; }
  576. }
  577. /**
  578. * @deprecated
  579. */
  580. public int ikChainIndex
  581. {
  582. get { return _ikChainIndex; }
  583. }
  584. /**
  585. * @deprecated
  586. */
  587. public Bone ik
  588. {
  589. get { return _ik; }
  590. }
  591. }
  592. }