15std::array<Vector3,
kJointBones.size()> Angles(
const skills::Key& key) {
17 for (
const skills::Turn& turn : key.turns()) {
18 if (turn.joint() >= 0 && turn.joint() < std::ssize(angles)) {
19 angles[turn.joint()] = {turn.pitch(), turn.yaw(), turn.roll()};
25Matrix ToMatrix(
const Transform& t) {
26 return MatrixMultiply(
27 MatrixMultiply(MatrixScale(t.scale.x, t.scale.y, t.scale.z),
28 QuaternionToMatrix(t.rotation)),
29 MatrixTranslate(t.translation.x, t.translation.y, t.translation.z));
37 for (std::size_t joint = 0; joint <
kJointBones.size(); ++joint) {
38 for (
int bone = 0; bone < static_cast<int>(model.skeleton.boneCount);
40 if (std::strcmp(model.skeleton.bones[bone].name,
kJointBones[joint]) ==
54 for (
const skills::Key& key : move.keys()) {
56 if (ms < key.at_ms()) {
57 const float span =
static_cast<float>(key.at_ms() - from.at_ms());
58 t = span > 0.0f ?
Eased(key.ease(), (ms - from.at_ms()) / span) : 1.0f;
63 const auto before = Angles(from);
64 const auto after = Angles(to);
66 for (std::size_t joint = 0; joint < bend.
turns.size(); ++joint) {
68 Vector3Scale(Vector3Lerp(before[joint], after[joint], t), DEG2RAD);
69 bend.
turns[joint] = QuaternionFromEuler(angle.x, angle.y, angle.z);
71 bend.
shift = {0.0f, Lerp(from.lift(), to.lift(), t),
72 Lerp(from.ahead(), to.ahead(), t)};
77 const ModelSkeleton& skeleton, Transform* pose) {
78 const int count =
static_cast<int>(skeleton.boneCount);
79 std::vector<Quaternion> turn(count, QuaternionIdentity());
80 for (std::size_t joint = 0; joint < joints.size(); ++joint) {
81 if (joints[joint] >= 0) {
82 turn[joints[joint]] = bend.
turns[joint];
85 const std::vector<Transform> clip(pose, pose + count);
88 std::vector<Quaternion> turned(count, QuaternionIdentity());
89 for (
int bone = 0; bone < count; ++bone) {
90 const int parent = skeleton.bones[bone].parent;
92 pose[bone].translation =
93 Vector3Add(clip[bone].translation, Vector3Scale(bend.
shift, metre));
94 turned[bone] = turn[bone];
97 Vector3Subtract(clip[bone].translation, clip[parent].translation);
98 pose[bone].translation =
99 Vector3Add(pose[parent].translation,
100 Vector3RotateByQuaternion(reach, turned[parent]));
101 turned[bone] = QuaternionMultiply(turned[parent], turn[bone]);
103 pose[bone].rotation = QuaternionMultiply(turned[bone], clip[bone].rotation);
108 Turn(bend, joints, metre, model.skeleton, model.currentPose);
109 for (
int bone = 0; bone < static_cast<int>(model.skeleton.boneCount);
112 model.boneMatrices[bone] =
113 MatrixMultiply(MatrixInvert(ToMatrix(model.skeleton.bindPose[bone])),
114 ToMatrix(model.currentPose[bone]));
void Turn(const Bend &bend, const Joints &joints, float metre, const ModelSkeleton &skeleton, Transform *pose)
void Apply(const Bend &bend, const Joints &joints, float metre, Model &model)
float Eased(skills::Ease ease, float t)
constexpr std::array< const char *, 13 > kJointBones
Each joint's bone in the rig, in the order of the joints.
Joints FindJoints(const Model &model)
Bend BendAt(const skills::Move &move, float ms)
The move, ms into it, between the keys either side.
std::array< int, kJointBones.size()> Joints
Where a model's bones are, by joint, or -1 for one it doesn't have.
std::array< Quaternion, kJointBones.size()> turns