BsSkeleton.h 7.3 KB

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