Skip to content

Commit 0e2724c

Browse files
committed
do not try to convert objects to bones that aren't bones in Unity
- only export bones that are specified as such on the skinned mesh as bones - clean up redundant root bone specific code
1 parent 95bc91f commit 0e2724c

File tree

1 file changed

+14
-50
lines changed

1 file changed

+14
-50
lines changed

Packages/com.unity.formats.fbx/Editor/Scripts/FbxExporter.cs

Lines changed: 14 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -984,10 +984,9 @@ private bool ExportSkeleton (SkinnedMeshRenderer skinnedMesh, FbxScene fbxScene,
984984
return false;
985985
}
986986

987-
// Three steps:
987+
// Two steps:
988988
// 0. Set up the map from bone to index.
989-
// 1. Gather complete list of bones
990-
// 2. Set the transforms.
989+
// 1. Set the transforms.
991990

992991
// Step 0: map transform to index so we can look up index by bone.
993992
Dictionary<Transform, int> index = new Dictionary<Transform, int>();
@@ -996,44 +995,11 @@ private bool ExportSkeleton (SkinnedMeshRenderer skinnedMesh, FbxScene fbxScene,
996995
index[unityBoneTransform] = boneIndex;
997996
}
998997

999-
// Step 1: gather all the bones
1000-
HashSet<Transform> boneSet = new HashSet<Transform> ();
1001-
var s = new Stack<Transform> (bones);
1002-
var root = skinnedMesh.rootBone;
1003-
while (s.Count > 0) {
1004-
var t = s.Pop ();
998+
skinnedMeshToBonesMap.Add (skinnedMesh, bones);
1005999

1006-
if (!boneSet.Add (t)) {
1007-
continue;
1008-
}
1009-
1010-
if (t.parent == null) {
1011-
Debug.LogWarningFormat (
1012-
"FbxExporter: {0} is a bone but not a descendant of {1}'s mesh's root bone.",
1013-
t.name, skinnedMesh.name
1014-
);
1015-
continue;
1016-
}
1017-
1018-
// Each skinned mesh in Unity has one root bone, but may have objects
1019-
// between the root bone and leaf bones that are not in the bone list.
1020-
// However all objects between two bones in a hierarchy should be bones
1021-
// as well.
1022-
// e.g. in rootBone -> bone1 -> obj1 -> bone2, obj1 should be a bone
1023-
//
1024-
// Traverse from all leaf bones to the root bone adding everything in between
1025-
// to the boneSet regardless of whether it is in the skinned mesh's bone list.
1026-
if (t != root && !boneSet.Contains(t.parent)) {
1027-
s.Push (t.parent);
1028-
}
1029-
}
1030-
1031-
var boneList = boneSet.ToArray();
1032-
skinnedMeshToBonesMap.Add (skinnedMesh, boneList);
1033-
1034-
// Step 2: Set transforms
1000+
// Step 1: Set transforms
10351001
var boneInfo = new SkinnedMeshBoneInfo (skinnedMesh, index);
1036-
foreach (var bone in boneList) {
1002+
foreach (var bone in bones) {
10371003
var fbxBone = MapUnityObjectToFbxNode [bone.gameObject];
10381004
ExportBoneTransform (fbxBone, fbxScene, bone, boneInfo);
10391005
}
@@ -2826,7 +2792,9 @@ private bool ExportGameObjectAndParents(
28262792
}
28272793

28282794
SkinnedMeshBoneInfo parentBoneInfo = null;
2829-
if (boneInfo != null && boneInfo.skinnedMesh.rootBone != null && unityGo.transform != boneInfo.skinnedMesh.rootBone) {
2795+
if (boneInfo != null && boneInfo.skinnedMesh.rootBone != null &&
2796+
unityGo.transform != boneInfo.skinnedMesh.rootBone && boneInfo.boneDict.ContainsKey(unityGo.transform.parent))
2797+
{
28302798
parentBoneInfo = boneInfo;
28312799
}
28322800

@@ -2891,17 +2859,13 @@ private bool ExportBoneTransform(
28912859
}
28922860

28932861
Matrix4x4 pose;
2894-
if (unityBone == rootBone) {
2895-
pose = (unityBone.parent.worldToLocalMatrix * skinnedMesh.transform.localToWorldMatrix * bindPose.inverse);
2896-
} else {
2897-
// get parent's bind pose
2898-
Matrix4x4 parentBindPose;
2899-
if (!boneInfo.boneToBindPose.TryGetValue (unityBone.parent, out parentBindPose)) {
2900-
parentBindPose = GetBindPose (unityBone.parent, bindPoses, boneDict, skinnedMesh);
2901-
boneInfo.boneToBindPose.Add (unityBone.parent, parentBindPose);
2902-
}
2903-
pose = parentBindPose * bindPose.inverse;
2862+
// get parent's bind pose
2863+
Matrix4x4 parentBindPose;
2864+
if (!boneInfo.boneToBindPose.TryGetValue (unityBone.parent, out parentBindPose)) {
2865+
parentBindPose = GetBindPose (unityBone.parent, bindPoses, boneDict, skinnedMesh);
2866+
boneInfo.boneToBindPose.Add (unityBone.parent, parentBindPose);
29042867
}
2868+
pose = parentBindPose * bindPose.inverse;
29052869

29062870
FbxVector4 translation, rotation, scale;
29072871
GetTRSFromMatrix (pose, out translation, out rotation, out scale);

0 commit comments

Comments
 (0)