SkeletalAnimation.h 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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/Core/Result.h>
  7. #include <Jolt/Core/StreamUtils.h>
  8. #include <Jolt/ObjectStream/SerializableObject.h>
  9. JPH_NAMESPACE_BEGIN
  10. class SkeletonPose;
  11. /// Resource for a skinned animation
  12. class JPH_EXPORT SkeletalAnimation : public RefTarget<SkeletalAnimation>
  13. {
  14. JPH_DECLARE_SERIALIZABLE_NON_VIRTUAL(JPH_EXPORT, SkeletalAnimation)
  15. public:
  16. /// Contains the current state of a joint, a local space transformation relative to its parent joint
  17. class JointState
  18. {
  19. JPH_DECLARE_SERIALIZABLE_NON_VIRTUAL(JPH_EXPORT, JointState)
  20. public:
  21. /// Convert from a local space matrix
  22. void FromMatrix(Mat44Arg inMatrix);
  23. /// Convert to matrix representation
  24. inline Mat44 ToMatrix() const { return Mat44::sRotationTranslation(mRotation, mTranslation); }
  25. Quat mRotation = Quat::sIdentity(); ///< Local space rotation of the joint
  26. Vec3 mTranslation = Vec3::sZero(); ///< Local space translation of the joint
  27. };
  28. /// Contains the state of a single joint at a particular time
  29. class Keyframe : public JointState
  30. {
  31. JPH_DECLARE_SERIALIZABLE_NON_VIRTUAL(JPH_EXPORT, Keyframe)
  32. public:
  33. float mTime = 0.0f; ///< Time of keyframe in seconds
  34. };
  35. using KeyframeVector = Array<Keyframe>;
  36. /// Contains the animation for a single joint
  37. class AnimatedJoint
  38. {
  39. JPH_DECLARE_SERIALIZABLE_NON_VIRTUAL(JPH_EXPORT, AnimatedJoint)
  40. public:
  41. String mJointName; ///< Name of the joint
  42. KeyframeVector mKeyframes; ///< List of keyframes over time
  43. };
  44. using AnimatedJointVector = Array<AnimatedJoint>;
  45. /// Get the length (in seconds) of this animation
  46. float GetDuration() const;
  47. /// Scale the size of all joints by inScale
  48. void ScaleJoints(float inScale);
  49. /// If the animation is looping or not. If an animation is looping, the animation will continue playing after completion
  50. void SetIsLooping(bool inIsLooping) { mIsLooping = inIsLooping; }
  51. bool IsLooping() const { return mIsLooping; }
  52. /// Get the (interpolated) joint transforms at time inTime
  53. void Sample(float inTime, SkeletonPose &ioPose) const;
  54. /// Get joint samples
  55. const AnimatedJointVector & GetAnimatedJoints() const { return mAnimatedJoints; }
  56. AnimatedJointVector & GetAnimatedJoints() { return mAnimatedJoints; }
  57. /// Saves the state of this animation in binary form to inStream.
  58. void SaveBinaryState(StreamOut &inStream) const;
  59. using AnimationResult = Result<Ref<SkeletalAnimation>>;
  60. /// Restore a saved ragdoll from inStream
  61. static AnimationResult sRestoreFromBinaryState(StreamIn &inStream);
  62. private:
  63. AnimatedJointVector mAnimatedJoints; ///< List of joints and keyframes
  64. bool mIsLooping = true; ///< If this animation loops back to start
  65. };
  66. JPH_NAMESPACE_END