BsSkeleton.h 7.8 KB

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