BsSkeleton.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #pragma once
  4. #include "BsCorePrerequisites.h"
  5. #include "Reflection/BsIReflectable.h"
  6. #include "Math/BsMatrix4.h"
  7. #include "Math/BsVector3.h"
  8. #include "Math/BsQuaternion.h"
  9. #include "Animation/BsCurveCache.h"
  10. #include "Scene/BsTransform.h"
  11. namespace bs
  12. {
  13. class SkeletonMask;
  14. /** @addtogroup Animation-Internal
  15. * @{
  16. */
  17. /**
  18. * Contains indices for position/rotation/scale animation curves. Used for quick mapping of bones in a skeleton to
  19. * relevant animation curves.
  20. */
  21. struct AnimationCurveMapping
  22. {
  23. UINT32 position;
  24. UINT32 rotation;
  25. UINT32 scale;
  26. };
  27. /** Information about a single bone used for constructing a skeleton. */
  28. struct BONE_DESC
  29. {
  30. String name; /**< Unique name of the bone. */
  31. UINT32 parent; /**< Index of the parent bone, if any. -1 if root bone. */
  32. Transform localTfrm; /**< Local transform of the bone, relative to other bones in the hierarchy. */
  33. Matrix4 invBindPose; /**< Inverse bind pose which transforms vertices from their bind pose into local space. */
  34. };
  35. /** Contains information about a single playing animation clip. */
  36. struct AnimationState
  37. {
  38. SPtr<AnimationCurves> curves; /**< All curves in the animation clip. */
  39. AnimationCurveMapping* boneToCurveMapping; /**< Mapping of bone indices to curve indices for quick lookup .*/
  40. AnimationCurveMapping* soToCurveMapping; /**< Mapping of scene object indices to curve indices for quick lookup. */
  41. TCurveCache<Vector3>* positionCaches; /**< Cache used for evaluating position curves. */
  42. TCurveCache<Quaternion>* rotationCaches; /**< Cache used for evaluating rotation curves. */
  43. TCurveCache<Vector3>* scaleCaches; /**< Cache used for evaluating scale curves. */
  44. TCurveCache<float>* genericCaches; /**< Cache used for evaluating generic curves. */
  45. float time; /**< Time to evaluate the curve at. */
  46. float weight; /**< Determines how much of an influence will this clip have in regard to others in the same layer. */
  47. bool loop; /**< Determines should the animation loop (wrap) once ending or beginning frames are passed. */
  48. bool disabled; /**< If true the clip state will not be evaluated. */
  49. };
  50. /** Contains animation states for a single animation layer. */
  51. struct AnimationStateLayer
  52. {
  53. AnimationState* states; /**< Array of animation states in the layer. */
  54. UINT32 numStates; /**< Number of states in @p states. */
  55. UINT8 index; /**< Unique index of the animation layer. */
  56. /**
  57. * If true animations from this layer will be added on top of other layers using the per-state weights. If false
  58. * the weights will be normalized, animations will be blended with each other according to the normalized weights
  59. * and then added on top of other layers.
  60. */
  61. bool additive;
  62. };
  63. /**
  64. * Contains local translation, rotation and scale values for each bone in a skeleton, after being evaluated at a
  65. * specific time of an animation. All values are stored in the same order as the bones in the skeleton they were
  66. * created by.
  67. */
  68. struct LocalSkeletonPose
  69. {
  70. LocalSkeletonPose();
  71. LocalSkeletonPose(UINT32 numBones);
  72. LocalSkeletonPose(UINT32 numPos, UINT32 numRot, UINT32 numScale);
  73. LocalSkeletonPose(const LocalSkeletonPose& other) = delete;
  74. LocalSkeletonPose(LocalSkeletonPose&& other);
  75. ~LocalSkeletonPose();
  76. LocalSkeletonPose& operator=(const LocalSkeletonPose& other) = delete;
  77. LocalSkeletonPose& operator=(LocalSkeletonPose&& other);
  78. Vector3* positions; /**< Local bone positions at specific animation time. */
  79. Quaternion* rotations; /**< Local bone rotations at specific animation time. */
  80. Vector3* scales; /**< Local bone scales at specific animation time. */
  81. bool* hasOverride; /**< True if the bone transform was overriden externally (local pose was ignored). */
  82. UINT32 numBones; /**< Number of bones in the pose. */
  83. };
  84. /** Contains internal information about a single bone in a Skeleton. */
  85. struct SkeletonBoneInfo
  86. {
  87. String name; /**< Unique name of the bone. */
  88. UINT32 parent; /**< Index of the bone parent, or -1 if root (no parent). */
  89. };
  90. /**
  91. * @native
  92. * Contains information about bones required for skeletal animation. Allows caller to evaluate a set of animation
  93. * clips at a specific time and output the relevant skeleton pose.
  94. * @endnative
  95. * @script
  96. * Contains information about bones required for skeletal animation.
  97. * @endscript
  98. */
  99. class BS_CORE_EXPORT BS_SCRIPT_EXPORT(m:Animation) Skeleton : public IReflectable // Note: Must be immutable in order to be usable on multiple threads
  100. {
  101. public:
  102. ~Skeleton();
  103. /**
  104. * Outputs a skeleton pose containing required transforms for transforming the skeleton to the values specified by
  105. * the provided animation clip evaluated at the specified time.
  106. *
  107. * @param[out] pose Output pose containing the requested transforms. Must be pre-allocated with enough space
  108. * to hold all the bone matrices of this skeleton.
  109. * @param[in] mask Mask that filters which skeleton bones are enabled or disabled.
  110. * @param[out] localPose Output pose containing the local transforms. Must be pre-allocated with enough space
  111. * to hold all the bone data of this skeleton.
  112. * @param[in] clip Clip to evaluate.
  113. * @param[in] time Time to evaluate the clip with.
  114. * @param[in] loop Determines should the time be looped (wrapped) if it goes past the clip start/end.
  115. *
  116. * @note It is more efficient to use the other getPose overload as sequential calls can benefit from animation
  117. * evaluator cache.
  118. */
  119. void getPose(Matrix4* pose, LocalSkeletonPose& localPose, const SkeletonMask& mask, const AnimationClip& clip,
  120. float time, bool loop = true);
  121. /**
  122. * Outputs a skeleton pose containing required transforms for transforming the skeleton to the values specified by
  123. * the provided set of animation curves.
  124. *
  125. * @param[out] pose Output pose containing the requested transforms. Must be pre-allocated with enough space
  126. * to hold all the bone matrices of this skeleton.
  127. * @param[in] mask Mask that filters which skeleton bones are enabled or disabled.
  128. * @param[out] localPose Output pose containing the local transforms. Must be pre-allocated with enough space
  129. * to hold all the bone data of this skeleton.
  130. * @param[in] layers One or multiple layers, containing one or multiple animation states to evaluate.
  131. * @param[in] numLayers Number of layers in the @p layers array.
  132. */
  133. void getPose(Matrix4* pose, LocalSkeletonPose& localPose, const SkeletonMask& mask,
  134. const AnimationStateLayer* layers, UINT32 numLayers);
  135. /** Returns the total number of bones in the skeleton. */
  136. BS_SCRIPT_EXPORT(pr:getter,n:NumBones)
  137. UINT32 getNumBones() const { return mNumBones; }
  138. /** Returns information about a bone at the provided index. */
  139. const SkeletonBoneInfo& getBoneInfo(UINT32 idx) const { return mBoneInfo[idx]; }
  140. /** Searches all bones to find a root bone. Returns -1 if no root can be found. */
  141. UINT32 getRootBoneIndex() const;
  142. /** Returns the inverse bind pose for the bone at the provided index. */
  143. const Matrix4& getInvBindPose(UINT32 idx) const { return mInvBindPoses[idx]; }
  144. /**
  145. * Creates a new Skeleton.
  146. *
  147. * @param[in] bones An array of bones to initialize the skeleton with. Data will be copied.
  148. * @param[in] numBones Number of bones in the @p bones array.
  149. */
  150. static SPtr<Skeleton> create(BONE_DESC* bones, UINT32 numBones);
  151. private:
  152. Skeleton();
  153. Skeleton(BONE_DESC* bones, UINT32 numBones);
  154. UINT32 mNumBones = 0;
  155. Transform* mBoneTransforms = nullptr;
  156. Matrix4* mInvBindPoses = nullptr;
  157. SkeletonBoneInfo* mBoneInfo = nullptr;
  158. /************************************************************************/
  159. /* SERIALIZATION */
  160. /************************************************************************/
  161. public:
  162. friend class SkeletonRTTI;
  163. static RTTITypeBase* getRTTIStatic();
  164. RTTITypeBase* getRTTI() const override;
  165. /**
  166. * Creates a Skeleton with no data. You must populate its data manually.
  167. *
  168. * @note For serialization use only.
  169. */
  170. static SPtr<Skeleton> createEmpty();
  171. };
  172. /** @} */
  173. }