BsSkeleton.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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 "BsCurveEvaluator.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. TCurveEvaluatorData<Vector3> positionEval; /**< Time value and cache used for evaluating position curves. */
  38. TCurveEvaluatorData<Quaternion> rotationEval; /**< Time value and cache used for evaluating rotation curves. */
  39. TCurveEvaluatorData<Vector3> scaleEval; /**< Time value and cache used for evaluating scale curves. */
  40. float weight; /**< Determines how much of an influence will this clip have in regard to others in the same layer. */
  41. bool loop; /**< Determines should the animation loop (wrap) once ending or beginning frames are passed. */
  42. };
  43. /** Contains animation states for a single animation layer. */
  44. struct AnimationStateLayer
  45. {
  46. AnimationState* states; /**< Array of animation states in the layer. */
  47. UINT32 numStates; /**< Number of states in @p states. */
  48. UINT8 index; /**< Unique index of the animation layer. */
  49. /**
  50. * Determines should weights of individual states be normalized or kept as is. Non-normalized weights allow the
  51. * total contribution of all states to be less than one.
  52. */
  53. bool normalizeWeights;
  54. };
  55. /**
  56. * Contains information about translation, rotation and scale for every skeleton bone, after being evaluated at a
  57. * a specific time. All values are stored in the same order as the bones in the skeleton they were created by.
  58. */
  59. struct SkeletonPose
  60. {
  61. SkeletonPose();
  62. SkeletonPose(UINT32 numBones);
  63. ~SkeletonPose();
  64. /**< Global matrices transforming vectors from bind pose space to model space at specific animation time. */
  65. Matrix4* bonePoses;
  66. Vector3* positions; /**< Local bone positions at specific animation time. */
  67. Quaternion* rotations; /**< Local bone rotations at specific animation time. */
  68. Vector3* scales; /**< Local bone scales at specific animation time. */
  69. UINT32 numBones; /**< Number of bones in the pose. */
  70. };
  71. /** Contains internal information about a single bone in a Skeleton. */
  72. struct SkeletonBoneInfo
  73. {
  74. String name; /**< Unique name of the bone. */
  75. UINT32 parent; /**< Index of the bone parent, or -1 if root (no parent). */
  76. };
  77. /**
  78. * Contains information about bones required for skeletal animation. Allows caller to evaluate a set of animation
  79. * clips at a specific time and output the relevant skeleton pose.
  80. */
  81. class BS_CORE_EXPORT Skeleton : public IReflectable // Note: Must be immutable in order to be usable on multiple threads
  82. {
  83. public:
  84. ~Skeleton();
  85. /**
  86. * Outputs a skeleton pose containing required transforms for transforming the skeleton to the values specified by
  87. * the provided animation clip evaluated at the specified time.
  88. *
  89. * @param[out] pose Output pose containing the requested transforms. Must be pre-allocated with enough space
  90. * to hold all the bone data of this skeleton.
  91. * @param[in] clip Clip to evaluate.
  92. * @param[in] time Time to evaluate the clip with.
  93. * @param[in] loop Determines should the time be looped (wrapped) if it goes past the clip start/end.
  94. *
  95. * @note It is more efficient to use the other getPose overload as sequential calls can benefit from animation
  96. * evaluator cache.
  97. */
  98. void getPose(SkeletonPose& pose, const AnimationClip& clip, float time, bool loop = true);
  99. /**
  100. * Outputs a skeleton pose containing required transforms for transforming the skeleton to the values specified by
  101. * the provided set of animation curves.
  102. *
  103. * @param[out] pose Output pose containing the requested transforms. Must be pre-allocated with enough space
  104. * to hold all the bone data of this skeleton.
  105. * @param[in] layers One or multiple layers, containing one or multiple animation states to evaluate.
  106. * @param[in] numLayers Number of layers in the @p layers array.
  107. */
  108. void getPose(SkeletonPose& pose, const AnimationStateLayer* layers, UINT32 numLayers);
  109. /** Returns the total number of bones in the skeleton. */
  110. UINT32 getNumBones() const { return mNumBones; }
  111. /** Returns information about a bone at the provided index. */
  112. const SkeletonBoneInfo& getBoneInfo(UINT32 idx) const { return mBoneInfo[idx]; }
  113. /** Returns the inverse bind pose for the bone at the provided index. */
  114. const Matrix4& getInvBindPose(UINT32 idx) const { return mInvBindPoses[idx]; }
  115. /**
  116. * Creates a new Skeleton.
  117. *
  118. * @param[in] bones An array of bones to initialize the skeleton with. Data will be copied.
  119. * @param[in] numBones Number of bones in the @p bones array.
  120. */
  121. static SPtr<Skeleton> create(BONE_DESC* bones, UINT32 numBones);
  122. private:
  123. Skeleton();
  124. Skeleton(BONE_DESC* bones, UINT32 numBones);
  125. UINT32 mNumBones;
  126. Matrix4* mInvBindPoses;
  127. SkeletonBoneInfo* mBoneInfo;
  128. /************************************************************************/
  129. /* SERIALIZATION */
  130. /************************************************************************/
  131. public:
  132. friend class SkeletonRTTI;
  133. static RTTITypeBase* getRTTIStatic();
  134. RTTITypeBase* getRTTI() const override;
  135. /**
  136. * Creates a Skeleton with no data. You must populate its data manually.
  137. *
  138. * @note For serialization use only.
  139. */
  140. static SPtr<Skeleton> createEmpty();
  141. };
  142. /** @} */
  143. }