Skip to content

Commit 2a2d305

Browse files
author
Benoit Hudson
committed
uni-42394: handle non-identity transforms when transfering animation
1 parent bc6df6e commit 2a2d305

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

Assets/FbxExporters/Editor/FbxExporter.cs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1889,8 +1889,8 @@ protected void ExportAnimationClip (AnimationClip uniAnimClip, GameObject uniRoo
18891889
/// Transfers transform animation from source to dest. Replaces dest's Unity Animation Curves with updated animations.
18901890
/// NOTE: Source must be the parent of dest.
18911891
/// </summary>
1892-
/// <param name="source">Source.</param>
1893-
/// <param name="dest">Destination.</param>
1892+
/// <param name="source">Source animated object.</param>
1893+
/// <param name="dest">Destination, child of the source.</param>
18941894
/// <param name="sampleRate">Sample rate.</param>
18951895
/// <param name="unityCurves">Unity curves.</param>
18961896
private void TransferMotion(Transform source, Transform dest, float sampleRate, ref Dictionary<GameObject, List<UnityCurve>> unityCurves){
@@ -1933,14 +1933,23 @@ private void TransferMotion(Transform source, Transform dest, float sampleRate,
19331933
scaleKeyFrames[k] = new Keyframe[sampleTimes.Count];
19341934
}
19351935

1936+
// If we have a point in local coords represented as a column-vector x, the equation of x in coordinates relative to source's parent is:
1937+
// x_grandparent = source * dest * x
1938+
// Now we're going to change dest to dest' which has the animation from source. And we're going to change
1939+
// source to source' which has no animation. The equation of x will become:
1940+
// x_grandparent = source' * dest' * x
1941+
// We're not changing x_grandparent and x, so we need that:
1942+
// source * dest = source' * dest'
1943+
// We know dest and source (both animated) and source' (static). Solve for dest':
1944+
// dest' = (source')^-1 * source * dest
19361945
int keyIndex = 0;
1946+
var sourceStaticMatrixInverse = Matrix4x4.TRS(source.localPosition, source.localRotation, source.localScale).inverse;
19371947
foreach (var currSampleTime in sampleTimes)
19381948
{
19391949
var sourceLocalMatrix = GetTransformMatrix (currSampleTime, source, sourceUnityCurves);
19401950
var destLocalMatrix = GetTransformMatrix (currSampleTime, dest, destUnityCurves);
19411951

1942-
// child * parent
1943-
var newLocalMatrix = sourceLocalMatrix * destLocalMatrix;
1952+
var newLocalMatrix = sourceStaticMatrixInverse * sourceLocalMatrix * destLocalMatrix;
19441953

19451954
FbxVector4 translation, rotation, scale;
19461955
GetTRSFromMatrix (newLocalMatrix, out translation, out rotation, out scale);

0 commit comments

Comments
 (0)