3ds Max->glTF (exporter v20220401.2) Animated rotation interpolation issue

Hello,
I investigated a bit this issue and found that the inversion seems to come from the “duality” of quaternion, meaning that any given rotation has two possible quaternion representations, and then the sign is ambigous.
If one is known, the other can be found by taking the negative of all four terms. This has the effect of reversing both the rotation angle and the axis of rotation. So for all rotation quaternions, ( q 0, q 1, q 2, q 3) and (− q 0, − q 1, − q 2, − q 3) produce identical rotations.

In the strictly mathematical sense, and under Babylon, the rotation is totally legit, but apparently may conduct to dysfunction with other gltf reader, such unity.

I will investigate into the math we use into matrix decompose to see if my assumption is right and then see if another algorithm may produce “better” result.

regards

EDIT:
I found a simple solution to solve the continuity.


I add this piece of code to test the distance between the two consecutive quaternion

                   ...
                    tm_babylon.decompose(s_babylon, q_babylon, t_babylon);

                    if (q_previous != null)
                    {
                        // avoid jump 
                        // flip the sign of the current quaternion if it makes the distance between the quaternions in the 4D vector space smaller.
                        // any given rotation has two possible quaternion representations, and then the sign is ambigous.
                        // If one is known, the other can be found by taking the negative of all four terms.This has the effect of reversing both the rotation
                        // angle and the axis of rotation.So for all rotation quaternions, (q 0, q 1, q 2, q 3) and(− q 0, − q 1, − q 2, − q 3) produce identical rotations
                        if ((q_babylon - q_previous).SquaredNorm() > (q_babylon + q_previous).SquaredNorm())
                        {
                            q_babylon = -q_babylon;
                        }
                    }
                    q_previous = q_babylon;

Explanation are in the comment into the code. Operator override and some functions are also added to Quaternion class.

@Deltakosh, @stephen_mxr , Guy’s, Let me know what you’re thinking about.

test.zip (19.1 KB)

G.