Skip to content

Commit 803603b

Browse files
committed
make sure SetVertexWeights accounts for merged vertices
* TODO: find a better way than recalculating the indices of the vertices * Also don't add every FbxNode exported as part of the bind pose, only the bones * convert axis of exported translation and rotation
1 parent c1545e4 commit 803603b

File tree

1 file changed

+24
-12
lines changed

1 file changed

+24
-12
lines changed

Assets/FbxExporters/Editor/FbxExporter.cs

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -728,7 +728,7 @@ SkinnedMeshRenderer unitySkin
728728
ExportSkin (unitySkin, meshInfo, fbxScene, fbxMesh, fbxNode);
729729

730730
// add bind pose
731-
ExportBindPose (fbxNode, fbxScene);
731+
ExportBindPose (unitySkin, fbxNode, fbxScene);
732732

733733
if (Verbose)
734734
Debug.Log (string.Format ("exporting {0} {1}", "Skin", fbxNode.GetName ()));
@@ -795,8 +795,8 @@ private bool ExportSkeleton (SkinnedMeshRenderer skinnedMesh, FbxScene fbxScene)
795795

796796
// Step 2: connect up the hierarchy.
797797
foreach (var unityBone in bones) {
798-
var fbxBone = MapUnityObjectToFbxNode [unityBone.gameObject];
799-
if (unityBone.parent != null) {
798+
if (unityBone.parent != null && MapUnityObjectToFbxNode.ContainsKey(unityBone.parent.gameObject)) {
799+
var fbxBone = MapUnityObjectToFbxNode [unityBone.gameObject];
800800
var fbxParent = MapUnityObjectToFbxNode [unityBone.parent.gameObject];
801801
fbxParent.AddChild (fbxBone);
802802
}
@@ -833,13 +833,13 @@ private bool ExportSkeleton (SkinnedMeshRenderer skinnedMesh, FbxScene fbxScene)
833833
matrix.GetElements (out translation, out rotation, out shear, out scale, out sign);
834834

835835
// Bones should have zero rotation, and use a pivot instead.
836-
fbxBone.LclTranslation.Set (new FbxDouble3(translation.X*UnitScaleFactor, translation.Y*UnitScaleFactor, translation.Z*UnitScaleFactor));
836+
fbxBone.LclTranslation.Set (new FbxDouble3(-translation.X*UnitScaleFactor, translation.Y*UnitScaleFactor, translation.Z*UnitScaleFactor));
837837
fbxBone.LclRotation.Set (new FbxDouble3(0,0,0));
838838
fbxBone.LclScaling.Set (new FbxDouble3 (scale.X, scale.Y, scale.Z));
839839

840840
fbxBone.SetRotationActive (true);
841841
fbxBone.SetPivotState (FbxNode.EPivotSet.eSourcePivot, FbxNode.EPivotState.ePivotReference);
842-
fbxBone.SetPreRotation (FbxNode.EPivotSet.eSourcePivot, new FbxVector4 (rotation.X, rotation.Y, rotation.Z));
842+
fbxBone.SetPreRotation (FbxNode.EPivotSet.eSourcePivot, new FbxVector4 (rotation.X, -rotation.Y, -rotation.Z));
843843
}
844844

845845
return true;
@@ -866,7 +866,7 @@ private bool ExportSkin (SkinnedMeshRenderer skinnedMesh,
866866
FbxCluster fbxCluster = FbxCluster.Create (fbxScene, "BoneWeightCluster");
867867

868868
fbxCluster.SetLink (fbxBoneNode);
869-
fbxCluster.SetLinkMode (FbxCluster.ELinkMode.eTotalOne);
869+
fbxCluster.SetLinkMode (FbxCluster.ELinkMode.eNormalize);
870870

871871
boneCluster.Add (i, fbxCluster);
872872

@@ -894,6 +894,16 @@ private bool ExportSkin (SkinnedMeshRenderer skinnedMesh,
894894
/// </summary>
895895
private void SetVertexWeights (MeshInfo meshInfo, Dictionary<int, FbxCluster> boneCluster)
896896
{
897+
// Create control points.
898+
Dictionary<Vector3, int> ControlPointToIndex = new Dictionary<Vector3, int> ();
899+
var vertices = meshInfo.Vertices;
900+
for (int v = 0, n = meshInfo.VertexCount; v < n; v++) {
901+
if (ControlPointToIndex.ContainsKey (vertices [v])) {
902+
continue;
903+
}
904+
ControlPointToIndex [vertices [v]] = ControlPointToIndex.Count();
905+
}
906+
897907
// set the vertex weights for each bone
898908
for (int i = 0; i < meshInfo.BoneWeights.Length; i++) {
899909
var boneWeights = meshInfo.BoneWeights;
@@ -917,24 +927,26 @@ boneWeights [i].weight3
917927
if (!boneCluster.ContainsKey (indices [j])) {
918928
continue;
919929
}
920-
boneCluster [indices [j]].AddControlPointIndex (i, weights [j]);
930+
//boneCluster [indices [j]].AddControlPointIndex (i, weights [j]);
931+
boneCluster [indices [j]].AddControlPointIndex (ControlPointToIndex[meshInfo.Vertices[i]], weights [j]);
921932
}
922933
}
923934
}
924935

925936
/// <summary>
926937
/// Export bind pose of mesh to skeleton
927938
/// </summary>
928-
protected bool ExportBindPose (FbxNode fbxMeshNode, FbxScene fbxScene)
939+
protected bool ExportBindPose (SkinnedMeshRenderer skinnedMesh, FbxNode fbxMeshNode, FbxScene fbxScene)
929940
{
930941
FbxPose fbxPose = FbxPose.Create (fbxScene, fbxMeshNode.GetName());
931942

932943
// set as bind pose
933944
fbxPose.SetIsBindPose (true);
934945

935946
// assume each bone node has one weighted vertex cluster
936-
foreach (FbxNode fbxNode in MapUnityObjectToFbxNode.Values)
937-
{
947+
for (int i = 0; i < skinnedMesh.bones.Length; i++) {
948+
FbxNode fbxBoneNode = MapUnityObjectToFbxNode [skinnedMesh.bones[i].gameObject];
949+
938950
// EvaluateGlobalTransform returns an FbxAMatrix (affine matrix)
939951
// which has to be converted to an FbxMatrix so that it can be passed to fbxPose.Add().
940952
// The hierarchy for FbxMatrix and FbxAMatrix is as follows:
@@ -945,9 +957,9 @@ protected bool ExportBindPose (FbxNode fbxMeshNode, FbxScene fbxScene)
945957
//
946958
// Therefore we can't convert directly from FbxAMatrix to FbxMatrix,
947959
// however FbxMatrix has a constructor that takes an FbxAMatrix.
948-
FbxMatrix fbxBindMatrix = new FbxMatrix(fbxNode.EvaluateGlobalTransform ());
960+
FbxMatrix fbxBindMatrix = new FbxMatrix(fbxBoneNode.EvaluateGlobalTransform ());
949961

950-
fbxPose.Add (fbxNode, fbxBindMatrix);
962+
fbxPose.Add (fbxBoneNode, fbxBindMatrix);
951963
}
952964

953965
fbxPose.Add (fbxMeshNode, new FbxMatrix (fbxMeshNode.EvaluateGlobalTransform ()));

0 commit comments

Comments
 (0)