SkeletalAnimation.h 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  2. // SPDX-License-Identifier: MIT
  3. #pragma once
  4. #include <Core/Reference.h>
  5. #include <ObjectStream/SerializableObject.h>
  6. namespace JPH {
  7. class SkeletonPose;
  8. /// Resource for a skinned animation
  9. class SkeletalAnimation : public RefTarget<SkeletalAnimation>
  10. {
  11. public:
  12. JPH_DECLARE_SERIALIZABLE_NON_VIRTUAL(SkeletalAnimation)
  13. /// Constains the current state of a joint, a local space transformation relative to its parent joint
  14. class JointState
  15. {
  16. public:
  17. JPH_DECLARE_SERIALIZABLE_NON_VIRTUAL(JointState)
  18. /// Convert from a local space matrix
  19. void FromMatrix(Mat44Arg inMatrix);
  20. /// Convert to matrix representation
  21. void ToMatrix(Mat44 &outMatrix);
  22. Quat mRotation = Quat::sIdentity(); ///< Local space rotation of the joint
  23. Vec3 mTranslation = Vec3::sZero(); ///< Local space translation of the joint
  24. };
  25. /// Contains the state of a single joint at a particular time
  26. class Keyframe : public JointState
  27. {
  28. public:
  29. JPH_DECLARE_SERIALIZABLE_NON_VIRTUAL(Keyframe)
  30. float mTime = 0.0f; ///< Time of keyframe in seconds
  31. };
  32. using KeyframeVector = vector<Keyframe>;
  33. /// Contains the animation for a single joint
  34. class AnimatedJoint
  35. {
  36. public:
  37. JPH_DECLARE_SERIALIZABLE_NON_VIRTUAL(AnimatedJoint)
  38. string mJointName; ///< Name of the joint
  39. KeyframeVector mKeyframes; ///< List of keyframes over time
  40. };
  41. using AnimatedJointVector = vector<AnimatedJoint>;
  42. /// Get the length (in seconds) of this animation
  43. float GetDuration() const;
  44. /// Scale the size of all joints by inScale
  45. void ScaleJoints(float inScale);
  46. /// Get the (interpolated) joint transforms at time inTime
  47. void Sample(float inTime, SkeletonPose &ioPose) const;
  48. /// Get joint samples
  49. const AnimatedJointVector & GetAnimatedJoints() const { return mAnimatedJoints; }
  50. AnimatedJointVector & GetAnimatedJoints() { return mAnimatedJoints; }
  51. private:
  52. AnimatedJointVector mAnimatedJoints; ///< List of joints and keyframes
  53. bool mIsLooping = true; ///< If this animation loops back to start
  54. };
  55. } // JPH