| 44 | namespace TEN::Renderer |
| 45 | { |
| 46 | void Renderer::UpdateAnimation(RendererItem* rItem, RendererObject& rObject, const KeyframeInterpolationData& interpData, int mask, bool useObjectWorldRotation) |
| 47 | { |
| 48 | static auto boneIndices = std::vector<int>{}; |
| 49 | boneIndices.clear(); |
| 50 | |
| 51 | RendererBone* bones[MAX_BONES] = {}; |
| 52 | int nextBone = 0; |
| 53 | |
| 54 | auto* transforms = ((rItem == nullptr) ? rObject.AnimationTransforms.data() : &rItem->AnimTransforms[0]); |
| 55 | |
| 56 | // Push. |
| 57 | bones[nextBone++] = rObject.Skeleton; |
| 58 | |
| 59 | while (nextBone != 0) |
| 60 | { |
| 61 | // Pop last bone in stack. |
| 62 | auto* bonePtr = bones[--nextBone]; |
| 63 | |
| 64 | // Check nullptr, otherwise inventory crashes. |
| 65 | if (bonePtr == nullptr) |
| 66 | return; |
| 67 | |
| 68 | if (interpData.Keyframe0.BoneOrientations.size() <= bonePtr->Index || |
| 69 | (interpData.Alpha != 0.0f && interpData.Keyframe0.BoneOrientations.size() <= bonePtr->Index)) |
| 70 | { |
| 71 | TENLog( |
| 72 | "Attempted to animate object with ID " + GetObjectName((GAME_OBJECT_ID)rItem->ObjectID) + |
| 73 | " using incorrect animation data. Bad animations set for slot?", |
| 74 | LogLevel::Error); |
| 75 | |
| 76 | return; |
| 77 | } |
| 78 | |
| 79 | bool calculateMatrix = (mask >> bonePtr->Index) & 1; |
| 80 | if (calculateMatrix) |
| 81 | { |
| 82 | auto offset0 = interpData.Keyframe0.RootOffset; |
| 83 | auto rotMatrix = Matrix::CreateFromQuaternion(interpData.Keyframe0.BoneOrientations[bonePtr->Index]); |
| 84 | |
| 85 | if (interpData.Alpha != 0.0f) |
| 86 | { |
| 87 | auto offset1 = interpData.Keyframe1.RootOffset; |
| 88 | offset0 = Vector3::Lerp(offset0, offset1, interpData.Alpha); |
| 89 | |
| 90 | auto rotMatrix2 = Matrix::CreateFromQuaternion(interpData.Keyframe1.BoneOrientations[bonePtr->Index]); |
| 91 | |
| 92 | auto quat1 = Quaternion::CreateFromRotationMatrix(rotMatrix); |
| 93 | auto quat2 = Quaternion::CreateFromRotationMatrix(rotMatrix2); |
| 94 | auto quat3 = Quaternion::Slerp(quat1, quat2, interpData.Alpha); |
| 95 | |
| 96 | rotMatrix = Matrix::CreateFromQuaternion(quat3); |
| 97 | } |
| 98 | |
| 99 | // Store bone orientation on current frame. |
| 100 | if (rItem != nullptr) |
| 101 | rItem->BoneOrientations[bonePtr->Index] = Quaternion::CreateFromRotationMatrix(rotMatrix); |
| 102 | |
| 103 | auto tMatrix = (bonePtr == rObject.Skeleton) ? Matrix::CreateTranslation(offset0) : Matrix::Identity; |