From bc14a1fd221c42cacfa7971e73bf997f7ed13131 Mon Sep 17 00:00:00 2001 From: dexy Date: Fri, 28 Jan 2022 06:25:41 +1100 Subject: [PATCH] Improved animations game replication and performance --- CodeWalker.Core/GameFiles/Resources/Clip.cs | 2 +- .../GameFiles/Resources/Drawable.cs | 30 ++++++++++++------- CodeWalker.Core/Utils/Quaternions.cs | 11 +++++++ 3 files changed, 31 insertions(+), 12 deletions(-) diff --git a/CodeWalker.Core/GameFiles/Resources/Clip.cs b/CodeWalker.Core/GameFiles/Resources/Clip.cs index e34a2d3..0d47701 100644 --- a/CodeWalker.Core/GameFiles/Resources/Clip.cs +++ b/CodeWalker.Core/GameFiles/Resources/Clip.cs @@ -922,7 +922,7 @@ namespace CodeWalker.GameFiles var aseq = seq.Sequences[boneIndex]; var q0 = aseq.EvaluateQuaternion(f0); var q1 = aseq.EvaluateQuaternion(f1); - var q = interpolate ? Quaternion.Slerp(q0, q1, frame.Alpha1) : q0; + var q = interpolate ? QuaternionExtension.FastLerp(q0, q1, frame.Alpha1) : q0; return q; } diff --git a/CodeWalker.Core/GameFiles/Resources/Drawable.cs b/CodeWalker.Core/GameFiles/Resources/Drawable.cs index a7c8d5f..4a4377c 100644 --- a/CodeWalker.Core/GameFiles/Resources/Drawable.cs +++ b/CodeWalker.Core/GameFiles/Resources/Drawable.cs @@ -2233,19 +2233,27 @@ namespace CodeWalker.GameFiles public void UpdateAnimTransform() { - //AnimTransform = Matrix.AffineTransformation(1.0f, AnimRotation, AnimTranslation);//(local transform) - var pos = AnimTranslation; - var ori = AnimRotation; - var sca = AnimScale; - var pbone = Parent; - while (pbone != null) + AnimTransform = Matrix.AffineTransformation(1.0f, AnimRotation, AnimTranslation); + AnimTransform.ScaleVector *= AnimScale; + if (Parent != null) { - pos = pbone.AnimRotation.Multiply(pos /** pbone.AnimScale*/) + pbone.AnimTranslation; - ori = pbone.AnimRotation * ori; - pbone = pbone.Parent; + AnimTransform = AnimTransform * Parent.AnimTransform; } - AnimTransform = Matrix.AffineTransformation(1.0f, ori, pos);//(global transform) - AnimTransform.ScaleVector *= sca; + + ////AnimTransform = Matrix.AffineTransformation(1.0f, AnimRotation, AnimTranslation);//(local transform) + + //var pos = AnimTranslation; + //var ori = AnimRotation; + //var sca = AnimScale; + //var pbone = Parent; + //while (pbone != null) + //{ + // pos = pbone.AnimRotation.Multiply(pos /** pbone.AnimScale*/) + pbone.AnimTranslation; + // ori = pbone.AnimRotation * ori; + // pbone = pbone.Parent; + //} + //AnimTransform = Matrix.AffineTransformation(1.0f, ori, pos);//(global transform) + //AnimTransform.ScaleVector *= sca; } public void UpdateSkinTransform() { diff --git a/CodeWalker.Core/Utils/Quaternions.cs b/CodeWalker.Core/Utils/Quaternions.cs index 2fa924d..3d99dc7 100644 --- a/CodeWalker.Core/Utils/Quaternions.cs +++ b/CodeWalker.Core/Utils/Quaternions.cs @@ -67,6 +67,17 @@ namespace CodeWalker return new Vector4(q.X, q.Y, q.Z, q.W); } + public static Quaternion FastLerp(Quaternion a, Quaternion b, float v) + { + var r = new Quaternion(); + var vi = 1.0f - v; + r.X = vi * a.X + v * b.X; + r.Y = vi * a.Y + v * b.Y; + r.Z = vi * a.Z + v * b.Z; + r.W = vi * a.W + v * b.W; + r.Normalize(); + return r; + } }