@@ -728,7 +728,7 @@ SkinnedMeshRenderer unitySkin
728
728
ExportSkin ( unitySkin , meshInfo , fbxScene , fbxMesh , fbxNode ) ;
729
729
730
730
// add bind pose
731
- ExportBindPose ( fbxNode , fbxScene ) ;
731
+ ExportBindPose ( unitySkin , fbxNode , fbxScene ) ;
732
732
733
733
if ( Verbose )
734
734
Debug . Log ( string . Format ( "exporting {0} {1}" , "Skin" , fbxNode . GetName ( ) ) ) ;
@@ -795,8 +795,8 @@ private bool ExportSkeleton (SkinnedMeshRenderer skinnedMesh, FbxScene fbxScene)
795
795
796
796
// Step 2: connect up the hierarchy.
797
797
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 ] ;
800
800
var fbxParent = MapUnityObjectToFbxNode [ unityBone . parent . gameObject ] ;
801
801
fbxParent . AddChild ( fbxBone ) ;
802
802
}
@@ -833,13 +833,13 @@ private bool ExportSkeleton (SkinnedMeshRenderer skinnedMesh, FbxScene fbxScene)
833
833
matrix . GetElements ( out translation , out rotation , out shear , out scale , out sign ) ;
834
834
835
835
// 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 ) ) ;
837
837
fbxBone . LclRotation . Set ( new FbxDouble3 ( 0 , 0 , 0 ) ) ;
838
838
fbxBone . LclScaling . Set ( new FbxDouble3 ( scale . X , scale . Y , scale . Z ) ) ;
839
839
840
840
fbxBone . SetRotationActive ( true ) ;
841
841
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 ) ) ;
843
843
}
844
844
845
845
return true ;
@@ -866,7 +866,7 @@ private bool ExportSkin (SkinnedMeshRenderer skinnedMesh,
866
866
FbxCluster fbxCluster = FbxCluster . Create ( fbxScene , "BoneWeightCluster" ) ;
867
867
868
868
fbxCluster . SetLink ( fbxBoneNode ) ;
869
- fbxCluster . SetLinkMode ( FbxCluster . ELinkMode . eTotalOne ) ;
869
+ fbxCluster . SetLinkMode ( FbxCluster . ELinkMode . eNormalize ) ;
870
870
871
871
boneCluster . Add ( i , fbxCluster ) ;
872
872
@@ -894,6 +894,16 @@ private bool ExportSkin (SkinnedMeshRenderer skinnedMesh,
894
894
/// </summary>
895
895
private void SetVertexWeights ( MeshInfo meshInfo , Dictionary < int , FbxCluster > boneCluster )
896
896
{
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
+
897
907
// set the vertex weights for each bone
898
908
for ( int i = 0 ; i < meshInfo . BoneWeights . Length ; i ++ ) {
899
909
var boneWeights = meshInfo . BoneWeights ;
@@ -917,24 +927,26 @@ boneWeights [i].weight3
917
927
if ( ! boneCluster . ContainsKey ( indices [ j ] ) ) {
918
928
continue ;
919
929
}
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 ] ) ;
921
932
}
922
933
}
923
934
}
924
935
925
936
/// <summary>
926
937
/// Export bind pose of mesh to skeleton
927
938
/// </summary>
928
- protected bool ExportBindPose ( FbxNode fbxMeshNode , FbxScene fbxScene )
939
+ protected bool ExportBindPose ( SkinnedMeshRenderer skinnedMesh , FbxNode fbxMeshNode , FbxScene fbxScene )
929
940
{
930
941
FbxPose fbxPose = FbxPose . Create ( fbxScene , fbxMeshNode . GetName ( ) ) ;
931
942
932
943
// set as bind pose
933
944
fbxPose . SetIsBindPose ( true ) ;
934
945
935
946
// 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
+
938
950
// EvaluateGlobalTransform returns an FbxAMatrix (affine matrix)
939
951
// which has to be converted to an FbxMatrix so that it can be passed to fbxPose.Add().
940
952
// The hierarchy for FbxMatrix and FbxAMatrix is as follows:
@@ -945,9 +957,9 @@ protected bool ExportBindPose (FbxNode fbxMeshNode, FbxScene fbxScene)
945
957
//
946
958
// Therefore we can't convert directly from FbxAMatrix to FbxMatrix,
947
959
// however FbxMatrix has a constructor that takes an FbxAMatrix.
948
- FbxMatrix fbxBindMatrix = new FbxMatrix ( fbxNode . EvaluateGlobalTransform ( ) ) ;
960
+ FbxMatrix fbxBindMatrix = new FbxMatrix ( fbxBoneNode . EvaluateGlobalTransform ( ) ) ;
949
961
950
- fbxPose . Add ( fbxNode , fbxBindMatrix ) ;
962
+ fbxPose . Add ( fbxBoneNode , fbxBindMatrix ) ;
951
963
}
952
964
953
965
fbxPose . Add ( fbxMeshNode , new FbxMatrix ( fbxMeshNode . EvaluateGlobalTransform ( ) ) ) ;
0 commit comments