Skip to content

Commit 8cd4ccb

Browse files
authored
Merge pull request #371 from Unity-Technologies/uni-42394-scale-transfer-issue
uni-42394: handle non-identity transforms when transfering animation
2 parents 2330396 + 4a87a8d commit 8cd4ccb

File tree

1 file changed

+13
-39
lines changed

1 file changed

+13
-39
lines changed

Assets/FbxExporters/Editor/FbxExporter.cs

Lines changed: 13 additions & 39 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);
@@ -2227,9 +2236,6 @@ protected int ExportTransformHierarchy(
22272236
// Use RSrs as the scaling inheritance instead.
22282237
fbxNode.SetTransformationInheritType (FbxTransform.EInheritType.eInheritRSrs);
22292238

2230-
if (TransformShouldBeReset (unityGo.transform)) {
2231-
exportType = TransformExportType.Reset;
2232-
}
22332239
ExportTransform (unityGo.transform, fbxNode, newCenter, exportType);
22342240

22352241
fbxNodeParent.AddChild (fbxNode);
@@ -2278,35 +2284,6 @@ protected int ExportTransformHierarchy(
22782284
return numObjectsExported;
22792285
}
22802286

2281-
/// <summary>
2282-
/// Checks if the transform should be reset.
2283-
/// Transform should be reset if animation is being transferred, and this transform
2284-
/// is either the animation source, destination, or between these nodes.
2285-
/// </summary>
2286-
/// <returns><c>true</c>, if transform should be reset, <c>false</c> otherwise.</returns>
2287-
/// <param name="t">Transform.</param>
2288-
private bool TransformShouldBeReset(Transform t){
2289-
var source = ExportOptions.AnimationSource;
2290-
var dest = ExportOptions.AnimationDest;
2291-
2292-
if (!source || !dest || source == dest) {
2293-
return false;
2294-
}
2295-
2296-
// don't want to reset destination, if it's a bone this could cause issues with the skinning
2297-
var curr = dest.parent;
2298-
while (curr != source && curr != null) {
2299-
if (t == curr) {
2300-
return true;
2301-
}
2302-
curr = curr.parent;
2303-
}
2304-
if (t == source) {
2305-
return true;
2306-
}
2307-
return false;
2308-
}
2309-
23102287
/// <summary>
23112288
/// Export data containing what to export when
23122289
/// exporting animation only.
@@ -2534,9 +2511,6 @@ private bool ExportGameObjectAndParents(
25342511

25352512
// export regular transform if we are not a bone or failed to export as a bone
25362513
if(!exportedBoneTransform){
2537-
if (TransformShouldBeReset (unityGo.transform)) {
2538-
exportType = TransformExportType.Reset;
2539-
}
25402514
ExportTransform (unityGo.transform, fbxNode, newCenter, exportType);
25412515
}
25422516

0 commit comments

Comments
 (0)