Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/thirdparty/jolt_physics/Jolt/Skeleton/SkeletalAnimation.h
9912 views
1
// Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
2
// SPDX-FileCopyrightText: 2021 Jorrit Rouwe
3
// SPDX-License-Identifier: MIT
4
5
#pragma once
6
7
#include <Jolt/Core/Reference.h>
8
#include <Jolt/Core/Result.h>
9
#include <Jolt/Core/StreamUtils.h>
10
#include <Jolt/ObjectStream/SerializableObject.h>
11
12
JPH_NAMESPACE_BEGIN
13
14
class SkeletonPose;
15
16
/// Resource for a skinned animation
17
class JPH_EXPORT SkeletalAnimation : public RefTarget<SkeletalAnimation>
18
{
19
JPH_DECLARE_SERIALIZABLE_NON_VIRTUAL(JPH_EXPORT, SkeletalAnimation)
20
21
public:
22
/// Contains the current state of a joint, a local space transformation relative to its parent joint
23
class JointState
24
{
25
JPH_DECLARE_SERIALIZABLE_NON_VIRTUAL(JPH_EXPORT, JointState)
26
27
public:
28
/// Convert from a local space matrix
29
void FromMatrix(Mat44Arg inMatrix);
30
31
/// Convert to matrix representation
32
inline Mat44 ToMatrix() const { return Mat44::sRotationTranslation(mRotation, mTranslation); }
33
34
Quat mRotation = Quat::sIdentity(); ///< Local space rotation of the joint
35
Vec3 mTranslation = Vec3::sZero(); ///< Local space translation of the joint
36
};
37
38
/// Contains the state of a single joint at a particular time
39
class Keyframe : public JointState
40
{
41
JPH_DECLARE_SERIALIZABLE_NON_VIRTUAL(JPH_EXPORT, Keyframe)
42
43
public:
44
float mTime = 0.0f; ///< Time of keyframe in seconds
45
};
46
47
using KeyframeVector = Array<Keyframe>;
48
49
/// Contains the animation for a single joint
50
class AnimatedJoint
51
{
52
JPH_DECLARE_SERIALIZABLE_NON_VIRTUAL(JPH_EXPORT, AnimatedJoint)
53
54
public:
55
String mJointName; ///< Name of the joint
56
KeyframeVector mKeyframes; ///< List of keyframes over time
57
};
58
59
using AnimatedJointVector = Array<AnimatedJoint>;
60
61
/// Get the length (in seconds) of this animation
62
float GetDuration() const;
63
64
/// Scale the size of all joints by inScale
65
void ScaleJoints(float inScale);
66
67
/// If the animation is looping or not. If an animation is looping, the animation will continue playing after completion
68
void SetIsLooping(bool inIsLooping) { mIsLooping = inIsLooping; }
69
bool IsLooping() const { return mIsLooping; }
70
71
/// Get the (interpolated) joint transforms at time inTime
72
void Sample(float inTime, SkeletonPose &ioPose) const;
73
74
/// Get joint samples
75
const AnimatedJointVector & GetAnimatedJoints() const { return mAnimatedJoints; }
76
AnimatedJointVector & GetAnimatedJoints() { return mAnimatedJoints; }
77
78
/// Saves the state of this animation in binary form to inStream.
79
void SaveBinaryState(StreamOut &inStream) const;
80
81
using AnimationResult = Result<Ref<SkeletalAnimation>>;
82
83
/// Restore a saved ragdoll from inStream
84
static AnimationResult sRestoreFromBinaryState(StreamIn &inStream);
85
86
private:
87
AnimatedJointVector mAnimatedJoints; ///< List of joints and keyframes
88
bool mIsLooping = true; ///< If this animation loops back to start
89
};
90
91
JPH_NAMESPACE_END
92
93