SkeletalAnimation.h 2.4 KB

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