CCSkeleton3D.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /****************************************************************************
  2. Copyright (c) 2014-2016 Chukong Technologies Inc.
  3. Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
  4. http://www.cocos2d-x.org
  5. Permission is hereby granted, free of charge, to any person obtaining a copy
  6. of this software and associated documentation files (the "Software"), to deal
  7. in the Software without restriction, including without limitation the rights
  8. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the Software is
  10. furnished to do so, subject to the following conditions:
  11. The above copyright notice and this permission notice shall be included in
  12. all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. THE SOFTWARE.
  20. ****************************************************************************/
  21. #ifndef __CCSKELETON3D_H__
  22. #define __CCSKELETON3D_H__
  23. #include "3d/CCBundle3DData.h"
  24. #include "base/CCRef.h"
  25. #include "base/CCVector.h"
  26. NS_CC_BEGIN
  27. /**
  28. * @addtogroup _3d
  29. * @{
  30. */
  31. /**
  32. * @brief Defines a basic hierarchical structure of transformation spaces.
  33. * @lua NA
  34. */
  35. class CC_DLL Bone3D : public Ref
  36. {
  37. friend class Skeleton3D;
  38. friend class MeshSkin;
  39. public:
  40. /**
  41. * Returns the inverse bind pose matrix for this joint.
  42. *
  43. * @return Inverse bind pose matrix.
  44. */
  45. const Mat4& getInverseBindPose();
  46. /**update own world matrix and children's*/
  47. void updateWorldMat();
  48. /**get world matrix*/
  49. const Mat4& getWorldMat();
  50. /**get bone name*/
  51. const std::string& getName() const { return _name; }
  52. /**
  53. * set animation value
  54. * @param trans translate vec3
  55. * @param rot rotation quaternion
  56. * @param scale scale vec3
  57. * @param tag unique tag, only blend animation between different tags
  58. * @param weight blend weight
  59. */
  60. void setAnimationValue(float* trans, float* rot, float* scale, void* tag = nullptr, float weight = 1.0f);
  61. /**clear bone blend states*/
  62. void clearBoneBlendState();
  63. /**
  64. * Creates C3DBone.
  65. */
  66. static Bone3D* create(const std::string& id);
  67. /**
  68. * Sets the inverse bind pose matrix.
  69. *
  70. * @param m Mat4 representing the inverse bind pose for this Bone.
  71. */
  72. void setInverseBindPose(const Mat4& m);
  73. /**
  74. * Sets the bone's original pose.
  75. *
  76. * @param m Mat4 representing the original pose for this Bone.
  77. */
  78. void setOriPose(const Mat4& m);
  79. /**
  80. * reset pose to origin
  81. */
  82. void resetPose();
  83. /**
  84. * Updates the joint matrix.
  85. *
  86. * @param matrixPalette The matrix palette to update.
  87. */
  88. void updateJointMatrix(Vec4* matrixPalette);
  89. /**bone tree, we do not inherit from Node, Node has too many properties that we do not need. A clean Node is needed.*/
  90. Bone3D* getParentBone();
  91. /**get child bone count*/
  92. ssize_t getChildBoneCount() const;
  93. /**get child bone by index*/
  94. Bone3D* getChildBoneByIndex(int index) const;
  95. /**add child bone*/
  96. void addChildBone(Bone3D* bone);
  97. /**remove child bone by index*/
  98. void removeChildBoneByIndex(int index);
  99. /**remove child bone*/
  100. void removeChildBone(Bone3D* bone);
  101. /**remove all child bone*/
  102. void removeAllChildBone();
  103. protected:
  104. /**
  105. * the BoneBlendState struct
  106. * @brief store the transformation and weight for bone blending
  107. */
  108. struct BoneBlendState
  109. {
  110. Vec3 localTranslate;
  111. Quaternion localRot;
  112. Vec3 localScale;
  113. float weight;
  114. void* tag; //
  115. BoneBlendState()
  116. : localRot(Quaternion::identity())
  117. , localScale(Vec3::ONE)
  118. , weight(1.f)
  119. , tag(nullptr)
  120. {
  121. }
  122. };
  123. /**
  124. * Constructor.
  125. */
  126. Bone3D(const std::string& id);
  127. /**
  128. * Destructor.
  129. */
  130. virtual ~Bone3D();
  131. /**
  132. * Update local matrix
  133. */
  134. void updateLocalMat();
  135. /**set world matrix dirty flag*/
  136. void setWorldMatDirty(bool dirty = true);
  137. std::string _name; // bone name
  138. /**
  139. * The Mat4 representation of the Joint's bind pose.
  140. */
  141. Mat4 _invBindPose;
  142. Mat4 _oriPose; //original bone pose
  143. Bone3D* _parent; //parent bone
  144. Vector<Bone3D*> _children;
  145. bool _worldDirty;
  146. Mat4 _world;
  147. Mat4 _local;
  148. std::vector<BoneBlendState> _blendStates;
  149. };
  150. /**
  151. * Skeleton
  152. *
  153. */
  154. class CC_DLL Skeleton3D: public Ref
  155. {
  156. public:
  157. /**
  158. * @lua NA
  159. */
  160. static Skeleton3D* create(const std::vector<NodeData*>& skeletondata);
  161. /**get total bone count*/
  162. ssize_t getBoneCount() const;
  163. /**get bone*/
  164. Bone3D* getBoneByIndex(unsigned int index) const;
  165. Bone3D* getBoneByName(const std::string& id) const;
  166. /**get & set root bone*/
  167. ssize_t getRootCount() const;
  168. Bone3D* getRootBone(int index) const;
  169. /**get bone index*/
  170. int getBoneIndex(Bone3D* bone) const;
  171. /**refresh bone world matrix*/
  172. void updateBoneMatrix();
  173. CC_CONSTRUCTOR_ACCESS:
  174. Skeleton3D();
  175. ~Skeleton3D();
  176. /**remove all bones*/
  177. void removeAllBones();
  178. /**add bone*/
  179. void addBone(Bone3D* bone);
  180. /** create Bone3D from NodeData */
  181. Bone3D* createBone3D(const NodeData& nodedata);
  182. protected:
  183. Vector<Bone3D*> _bones; // bones
  184. Vector<Bone3D*> _rootBones;
  185. };
  186. // end of 3d group
  187. /// @}
  188. NS_CC_END
  189. #endif // __CCSKELETON3D_H__