Bone.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638
  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.Generic;
  25. namespace DragonBones
  26. {
  27. /// <summary>
  28. /// - Bone is one of the most important logical units in the armature animation system,
  29. /// and is responsible for the realization of translate, rotation, scaling in the animations.
  30. /// A armature can contain multiple bones.
  31. /// </summary>
  32. /// <see cref="DragonBones.BoneData"/>
  33. /// <see cref="DragonBones.Armature"/>
  34. /// <see cref="DragonBones.Slot"/>
  35. /// <version>DragonBones 3.0</version>
  36. /// <language>en_US</language>
  37. /// <summary>
  38. /// - 骨骼在骨骼动画体系中是最重要的逻辑单元之一,负责动画中的平移、旋转、缩放的实现。
  39. /// 一个骨架中可以包含多个骨骼。
  40. /// </summary>
  41. /// <see cref="DragonBones.BoneData"/>
  42. /// <see cref="DragonBones.Armature"/>
  43. /// <see cref="DragonBones.Slot"/>
  44. /// <version>DragonBones 3.0</version>
  45. /// <language>zh_CN</language>
  46. public class Bone : TransformObject
  47. {
  48. /// <summary>
  49. /// - The offset mode.
  50. /// </summary>
  51. /// <see cref="offset"/>
  52. /// <version>DragonBones 5.5</version>
  53. /// <language>en_US</language>
  54. /// <summary>
  55. /// - 偏移模式。
  56. /// </summary>
  57. /// <see cref="offset"/>
  58. /// <version>DragonBones 5.5</version>
  59. /// <language>zh_CN</language>
  60. internal OffsetMode offsetMode;
  61. /// <internal/>
  62. /// <private/>
  63. internal readonly TransformDB animationPose = new TransformDB();
  64. /// <internal/>
  65. /// <private/>
  66. internal bool _transformDirty;
  67. /// <internal/>
  68. /// <private/>
  69. internal bool _childrenTransformDirty;
  70. private bool _localDirty;
  71. /// <internal/>
  72. /// <private/>
  73. internal bool _hasConstraint;
  74. private bool _visible;
  75. private int _cachedFrameIndex;
  76. /// <internal/>
  77. /// <private/>
  78. internal readonly BlendState _blendState = new BlendState();
  79. /// <internal/>
  80. /// <private/>
  81. internal BoneData _boneData;
  82. /// <private/>
  83. protected Bone _parent;
  84. /// <internal/>
  85. /// <private/>
  86. internal List<int> _cachedFrameIndices = new List<int>();
  87. /// <inheritDoc/>
  88. protected override void _OnClear()
  89. {
  90. base._OnClear();
  91. this.offsetMode = OffsetMode.Additive;
  92. this.animationPose.Identity();
  93. this._transformDirty = false;
  94. this._childrenTransformDirty = false;
  95. this._localDirty = true;
  96. this._hasConstraint = false;
  97. this._visible = true;
  98. this._cachedFrameIndex = -1;
  99. this._blendState.Clear();
  100. this._boneData = null; //
  101. this._parent = null;
  102. this._cachedFrameIndices = null;
  103. }
  104. /// <private/>
  105. private void _UpdateGlobalTransformMatrix(bool isCache)
  106. {
  107. var boneData = this._boneData;
  108. var parent = this._parent;
  109. var flipX = this._armature.flipX;
  110. var flipY = this._armature.flipY == DragonBones.yDown;
  111. var rotation = 0.0f;
  112. var global = this.global;
  113. var inherit = parent != null;
  114. var globalTransformMatrix = this.globalTransformMatrix;
  115. if (this.offsetMode == OffsetMode.Additive)
  116. {
  117. if (this.origin != null)
  118. {
  119. //global.CopyFrom(this.origin).Add(this.offset).Add(this.animationPose);
  120. global.x = this.origin.x + this.offset.x + this.animationPose.x;
  121. global.y = this.origin.y + this.offset.y + this.animationPose.y;
  122. global.skew = this.origin.skew + this.offset.skew + this.animationPose.skew;
  123. global.rotation = this.origin.rotation + this.offset.rotation + this.animationPose.rotation;
  124. global.scaleX = this.origin.scaleX * this.offset.scaleX * this.animationPose.scaleX;
  125. global.scaleY = this.origin.scaleY * this.offset.scaleY * this.animationPose.scaleY;
  126. }
  127. else
  128. {
  129. global.CopyFrom(this.offset).Add(this.animationPose);
  130. }
  131. }
  132. else if (this.offsetMode == OffsetMode.None)
  133. {
  134. if (this.origin != null)
  135. {
  136. global.CopyFrom(this.origin).Add(this.animationPose);
  137. }
  138. else
  139. {
  140. global.CopyFrom(this.animationPose);
  141. }
  142. }
  143. else
  144. {
  145. inherit = false;
  146. global.CopyFrom(this.offset);
  147. }
  148. if (inherit)
  149. {
  150. var parentMatrix = parent.globalTransformMatrix;
  151. if (boneData.inheritScale)
  152. {
  153. if (!boneData.inheritRotation)
  154. {
  155. parent.UpdateGlobalTransform();
  156. if (flipX && flipY)
  157. {
  158. rotation = global.rotation - (parent.global.rotation + TransformDB.PI);
  159. }
  160. else if (flipX)
  161. {
  162. rotation = global.rotation + parent.global.rotation + TransformDB.PI;
  163. }
  164. else if (flipY)
  165. {
  166. rotation = global.rotation + parent.global.rotation;
  167. }
  168. else
  169. {
  170. rotation = global.rotation - parent.global.rotation;
  171. }
  172. global.rotation = rotation;
  173. }
  174. global.ToMatrix(globalTransformMatrix);
  175. globalTransformMatrix.Concat(parentMatrix);
  176. if (this._boneData.inheritTranslation)
  177. {
  178. global.x = globalTransformMatrix.tx;
  179. global.y = globalTransformMatrix.ty;
  180. }
  181. else
  182. {
  183. globalTransformMatrix.tx = global.x;
  184. globalTransformMatrix.ty = global.y;
  185. }
  186. if (isCache)
  187. {
  188. global.FromMatrix(globalTransformMatrix);
  189. }
  190. else
  191. {
  192. this._globalDirty = true;
  193. }
  194. }
  195. else
  196. {
  197. if (boneData.inheritTranslation)
  198. {
  199. var x = global.x;
  200. var y = global.y;
  201. global.x = parentMatrix.a * x + parentMatrix.c * y + parentMatrix.tx;
  202. global.y = parentMatrix.b * x + parentMatrix.d * y + parentMatrix.ty;
  203. }
  204. else
  205. {
  206. if (flipX)
  207. {
  208. global.x = -global.x;
  209. }
  210. if (flipY)
  211. {
  212. global.y = -global.y;
  213. }
  214. }
  215. if (boneData.inheritRotation)
  216. {
  217. parent.UpdateGlobalTransform();
  218. if (parent.global.scaleX < 0.0)
  219. {
  220. rotation = global.rotation + parent.global.rotation + TransformDB.PI;
  221. }
  222. else
  223. {
  224. rotation = global.rotation + parent.global.rotation;
  225. }
  226. if (parentMatrix.a * parentMatrix.d - parentMatrix.b * parentMatrix.c < 0.0)
  227. {
  228. rotation -= global.rotation * 2.0f;
  229. if (flipX != flipY || boneData.inheritReflection)
  230. {
  231. global.skew += TransformDB.PI;
  232. }
  233. }
  234. global.rotation = rotation;
  235. }
  236. else if (flipX || flipY)
  237. {
  238. if (flipX && flipY)
  239. {
  240. rotation = global.rotation + TransformDB.PI;
  241. }
  242. else
  243. {
  244. if (flipX)
  245. {
  246. rotation = TransformDB.PI - global.rotation;
  247. }
  248. else
  249. {
  250. rotation = -global.rotation;
  251. }
  252. global.skew += TransformDB.PI;
  253. }
  254. global.rotation = rotation;
  255. }
  256. global.ToMatrix(globalTransformMatrix);
  257. }
  258. }
  259. else
  260. {
  261. if (flipX || flipY)
  262. {
  263. if (flipX)
  264. {
  265. global.x = -global.x;
  266. }
  267. if (flipY)
  268. {
  269. global.y = -global.y;
  270. }
  271. if (flipX && flipY)
  272. {
  273. rotation = global.rotation + TransformDB.PI;
  274. }
  275. else
  276. {
  277. if (flipX)
  278. {
  279. rotation = TransformDB.PI - global.rotation;
  280. }
  281. else
  282. {
  283. rotation = -global.rotation;
  284. }
  285. global.skew += TransformDB.PI;
  286. }
  287. global.rotation = rotation;
  288. }
  289. global.ToMatrix(globalTransformMatrix);
  290. }
  291. }
  292. /// <internal/>
  293. /// <private/>
  294. internal void Init(BoneData boneData, Armature armatureValue)
  295. {
  296. if (this._boneData != null)
  297. {
  298. return;
  299. }
  300. this._boneData = boneData;
  301. this._armature = armatureValue;
  302. if (this._boneData.parent != null)
  303. {
  304. this._parent = this._armature.GetBone(this._boneData.parent.name);
  305. }
  306. this._armature._AddBone(this);
  307. //
  308. this.origin = this._boneData.transform;
  309. }
  310. /// <internal/>
  311. /// <private/>
  312. internal void Update(int cacheFrameIndex)
  313. {
  314. this._blendState.dirty = false;
  315. if (cacheFrameIndex >= 0 && this._cachedFrameIndices != null)
  316. {
  317. var cachedFrameIndex = this._cachedFrameIndices[cacheFrameIndex];
  318. if (cachedFrameIndex >= 0 && this._cachedFrameIndex == cachedFrameIndex)
  319. {
  320. // Same cache.
  321. this._transformDirty = false;
  322. }
  323. else if (cachedFrameIndex >= 0)
  324. {
  325. // Has been Cached.
  326. this._transformDirty = true;
  327. this._cachedFrameIndex = cachedFrameIndex;
  328. }
  329. else
  330. {
  331. if (this._hasConstraint)
  332. {
  333. // Update constraints.
  334. foreach (var constraint in this._armature._constraints)
  335. {
  336. if (constraint._root == this)
  337. {
  338. constraint.Update();
  339. }
  340. }
  341. }
  342. if (this._transformDirty || (this._parent != null && this._parent._childrenTransformDirty))
  343. {
  344. // Dirty.
  345. this._transformDirty = true;
  346. this._cachedFrameIndex = -1;
  347. }
  348. else if (this._cachedFrameIndex >= 0)
  349. {
  350. // Same cache, but not set index yet.
  351. this._transformDirty = false;
  352. this._cachedFrameIndices[cacheFrameIndex] = this._cachedFrameIndex;
  353. }
  354. else
  355. {
  356. // Dirty.
  357. this._transformDirty = true;
  358. this._cachedFrameIndex = -1;
  359. }
  360. }
  361. }
  362. else
  363. {
  364. if (this._hasConstraint)
  365. {
  366. // Update constraints.
  367. foreach (var constraint in this._armature._constraints)
  368. {
  369. if (constraint._root == this)
  370. {
  371. constraint.Update();
  372. }
  373. }
  374. }
  375. if (this._transformDirty || (this._parent != null && this._parent._childrenTransformDirty))
  376. {
  377. // Dirty.
  378. cacheFrameIndex = -1;
  379. this._transformDirty = true;
  380. this._cachedFrameIndex = -1;
  381. }
  382. }
  383. if (this._transformDirty)
  384. {
  385. this._transformDirty = false;
  386. this._childrenTransformDirty = true;
  387. if (this._cachedFrameIndex < 0)
  388. {
  389. var isCache = cacheFrameIndex >= 0;
  390. if (this._localDirty)
  391. {
  392. this._UpdateGlobalTransformMatrix(isCache);
  393. }
  394. if (isCache && this._cachedFrameIndices != null)
  395. {
  396. this._cachedFrameIndex = this._cachedFrameIndices[cacheFrameIndex] = this._armature._armatureData.SetCacheFrame(this.globalTransformMatrix, this.global);
  397. }
  398. }
  399. else
  400. {
  401. this._armature._armatureData.GetCacheFrame(this.globalTransformMatrix, this.global, this._cachedFrameIndex);
  402. }
  403. }
  404. else if (this._childrenTransformDirty)
  405. {
  406. this._childrenTransformDirty = false;
  407. }
  408. this._localDirty = true;
  409. }
  410. /// <internal/>
  411. /// <private/>
  412. internal void UpdateByConstraint()
  413. {
  414. if (this._localDirty)
  415. {
  416. this._localDirty = false;
  417. if (this._transformDirty || (this._parent != null && this._parent._childrenTransformDirty))
  418. {
  419. this._UpdateGlobalTransformMatrix(true);
  420. }
  421. this._transformDirty = true;
  422. }
  423. }
  424. /// <summary>
  425. /// - Forces the bone to update the transform in the next frame.
  426. /// When the bone is not animated or its animation state is finished, the bone will not continue to update,
  427. /// and when the skeleton must be updated for some reason, the method needs to be called explicitly.
  428. /// </summary>
  429. /// <example>
  430. /// TypeScript style, for reference only.
  431. /// <pre>
  432. /// let bone = armature.getBone("arm");
  433. /// bone.offset.scaleX = 2.0;
  434. /// bone.invalidUpdate();
  435. /// </pre>
  436. /// </example>
  437. /// <version>DragonBones 3.0</version>
  438. /// <language>en_US</language>
  439. /// <summary>
  440. /// - 强制骨骼在下一帧更新变换。
  441. /// 当该骨骼没有动画状态或其动画状态播放完成时,骨骼将不在继续更新,而此时由于某些原因必须更新骨骼时,则需要显式调用该方法。
  442. /// </summary>
  443. /// <example>
  444. /// TypeScript 风格,仅供参考。
  445. /// <pre>
  446. /// let bone = armature.getBone("arm");
  447. /// bone.offset.scaleX = 2.0;
  448. /// bone.invalidUpdate();
  449. /// </pre>
  450. /// </example>
  451. /// <version>DragonBones 3.0</version>
  452. /// <language>zh_CN</language>
  453. public void InvalidUpdate()
  454. {
  455. this._transformDirty = true;
  456. }
  457. /// <summary>
  458. /// - Check whether the bone contains a specific bone or slot.
  459. /// </summary>
  460. /// <see cref="DragonBones.Bone"/>
  461. /// <see cref="DragonBones.Slot"/>
  462. /// <version>DragonBones 3.0</version>
  463. /// <language>en_US</language>
  464. /// <summary>
  465. /// - 检查该骨骼是否包含特定的骨骼或插槽。
  466. /// </summary>
  467. /// <see cref="DragonBones.Bone"/>
  468. /// <see cref="DragonBones.Slot"/>
  469. /// <version>DragonBones 3.0</version>
  470. /// <language>zh_CN</language>
  471. public bool Contains(Bone value)
  472. {
  473. if (value == this)
  474. {
  475. return false;
  476. }
  477. Bone ancestor = value;
  478. while (ancestor != this && ancestor != null)
  479. {
  480. ancestor = ancestor.parent;
  481. }
  482. return ancestor == this;
  483. }
  484. /// <summary>
  485. /// - The bone data.
  486. /// </summary>
  487. /// <version>DragonBones 4.5</version>
  488. /// <language>en_US</language>
  489. /// <summary>
  490. /// - 骨骼数据。
  491. /// </summary>
  492. /// <version>DragonBones 4.5</version>
  493. /// <language>zh_CN</language>
  494. public BoneData boneData
  495. {
  496. get { return this._boneData; }
  497. }
  498. /// <summary>
  499. /// - The visible of all slots in the bone.
  500. /// </summary>
  501. /// <default>true</default>
  502. /// <see cref="DragonBones.Slot.visible"/>
  503. /// <version>DragonBones 3.0</version>
  504. /// <language>en_US</language>
  505. /// <summary>
  506. /// - 此骨骼所有插槽的可见。
  507. /// </summary>
  508. /// <default>true</default>
  509. /// <see cref="DragonBones.Slot.visible"/>
  510. /// <version>DragonBones 3.0</version>
  511. /// <language>zh_CN</language>
  512. public bool visible
  513. {
  514. get { return this._visible; }
  515. set
  516. {
  517. if (this._visible == value)
  518. {
  519. return;
  520. }
  521. this._visible = value;
  522. foreach (var slot in this._armature.GetSlots())
  523. {
  524. if (slot.parent == this)
  525. {
  526. slot._UpdateVisible();
  527. }
  528. }
  529. }
  530. }
  531. /// <summary>
  532. /// - The bone name.
  533. /// </summary>
  534. /// <version>DragonBones 3.0</version>
  535. /// <language>en_US</language>
  536. /// <summary>
  537. /// - 骨骼名称。
  538. /// </summary>
  539. /// <version>DragonBones 3.0</version>
  540. /// <language>zh_CN</language>
  541. public string name
  542. {
  543. get { return this._boneData.name; }
  544. }
  545. /// <summary>
  546. /// - The parent bone to which it belongs.
  547. /// </summary>
  548. /// <version>DragonBones 3.0</version>
  549. /// <language>en_US</language>
  550. /// <summary>
  551. /// - 所属的父骨骼。
  552. /// </summary>
  553. /// <version>DragonBones 3.0</version>
  554. /// <language>zh_CN</language>
  555. public Bone parent
  556. {
  557. get { return this._parent; }
  558. }
  559. /// <summary>
  560. /// - Deprecated, please refer to {@link dragonBones.Armature#getSlot()}.
  561. /// </summary>
  562. /// <language>en_US</language>
  563. /// <summary>
  564. /// - 已废弃,请参考 {@link dragonBones.Armature#getSlot()}。
  565. /// </summary>
  566. /// <language>zh_CN</language>
  567. [System.Obsolete("")]
  568. public Slot slot
  569. {
  570. get
  571. {
  572. foreach (var slot in this._armature.GetSlots())
  573. {
  574. if (slot.parent == this)
  575. {
  576. return slot;
  577. }
  578. }
  579. return null;
  580. }
  581. }
  582. }
  583. }