@@ -503,7 +503,7 @@ meshInfo.Vertices [v].z*UnitScaleFactor
503
503
}
504
504
505
505
// get a fbxNode's global default position.
506
- protected void ExportTransform ( UnityEngine . Transform unityTransform , FbxNode fbxNode , TransformExportType exportType )
506
+ protected void ExportTransform ( UnityEngine . Transform unityTransform , FbxNode fbxNode , Vector3 newCenter , TransformExportType exportType )
507
507
{
508
508
// Fbx rotation order is XYZ, but Unity rotation order is ZXY.
509
509
// This causes issues when converting euler to quaternion, causing the final
@@ -522,7 +522,7 @@ protected void ExportTransform (UnityEngine.Transform unityTransform, FbxNode fb
522
522
unityScale = Vector3 . one ;
523
523
break ;
524
524
case TransformExportType . Global :
525
- unityTranslate = unityTransform . position ;
525
+ unityTranslate = GetRecenteredTranslation ( unityTransform , newCenter ) ;
526
526
unityRotate = unityTransform . rotation . eulerAngles ;
527
527
unityScale = unityTransform . lossyScale ;
528
528
break ;
@@ -592,7 +592,8 @@ protected bool ExportInstance (GameObject unityGo, FbxNode fbxNode, FbxScene fbx
592
592
/// </summary>
593
593
protected int ExportComponents (
594
594
GameObject unityGo , FbxScene fbxScene , FbxNode fbxNodeParent ,
595
- int exportProgress , int objectCount , TransformExportType exportType = TransformExportType . Local )
595
+ int exportProgress , int objectCount , Vector3 newCenter ,
596
+ TransformExportType exportType = TransformExportType . Local )
596
597
{
597
598
int numObjectsExported = exportProgress ;
598
599
@@ -613,7 +614,7 @@ protected int ExportComponents (
613
614
return - 1 ;
614
615
}
615
616
616
- ExportTransform ( unityGo . transform , fbxNode , exportType ) ;
617
+ ExportTransform ( unityGo . transform , fbxNode , newCenter , exportType ) ;
617
618
618
619
// try exporting mesh as an instance, export regularly if we cannot
619
620
if ( ! ExportInstance ( unityGo , fbxNode , fbxScene ) ) {
@@ -628,7 +629,7 @@ protected int ExportComponents (
628
629
629
630
// now unityGo through our children and recurse
630
631
foreach ( Transform childT in unityGo . transform ) {
631
- numObjectsExported = ExportComponents ( childT . gameObject , fbxScene , fbxNode , numObjectsExported , objectCount ) ;
632
+ numObjectsExported = ExportComponents ( childT . gameObject , fbxScene , fbxNode , numObjectsExported , objectCount , newCenter ) ;
632
633
}
633
634
return numObjectsExported ;
634
635
}
@@ -691,6 +692,13 @@ public static HashSet<GameObject> RemoveRedundantObjects(IEnumerable<UnityEngine
691
692
return toExport ;
692
693
}
693
694
695
+ /// <summary>
696
+ /// Recursively go through the hierarchy, adding up the bounding box centers
697
+ /// of all the children, to help find the center of all selected objects.
698
+ /// </summary>
699
+ /// <returns>The number of GameObjects in hierarchy starting at t.</returns>
700
+ /// <param name="t">Transform.</param>
701
+ /// <param name="centerAverage">Sum of all the bounding box centers in hierarchy.</param>
694
702
private int FindCenterRecursive ( Transform t , ref Vector3 centerAverage )
695
703
{
696
704
var renderer = t . GetComponent < Renderer > ( ) ;
@@ -717,9 +725,13 @@ private int FindCenterRecursive(Transform t, ref Vector3 centerAverage)
717
725
return count ;
718
726
}
719
727
728
+ /// <summary>
729
+ /// Finds the center of a group of GameObjects.
730
+ /// </summary>
731
+ /// <returns>Center of gameObjects.</returns>
732
+ /// <param name="gameObjects">Game objects.</param>
720
733
private Vector3 FindCenter ( IEnumerable < GameObject > gameObjects )
721
734
{
722
- // renderer, mesh, collider
723
735
Vector3 average = Vector3 . zero ;
724
736
int count = 0 ;
725
737
foreach ( var go in gameObjects ) {
@@ -728,6 +740,17 @@ private Vector3 FindCenter(IEnumerable<GameObject> gameObjects)
728
740
return average / count ;
729
741
}
730
742
743
+ /// <summary>
744
+ /// Gets the recentered translation.
745
+ /// </summary>
746
+ /// <returns>The recentered translation.</returns>
747
+ /// <param name="t">Transform.</param>
748
+ /// <param name="center">Center point.</param>
749
+ private Vector3 GetRecenteredTranslation ( Transform t , Vector3 center )
750
+ {
751
+ return t . position - center ;
752
+ }
753
+
731
754
public enum TransformExportType { Local , Global , Reset } ;
732
755
733
756
/// <summary>
@@ -801,7 +824,9 @@ public int ExportAll (IEnumerable<UnityEngine.Object> unityExportSet)
801
824
802
825
if ( revisedExportSet . Count == 1 ) {
803
826
foreach ( var unityGo in revisedExportSet ) {
804
- exportProgress = this . ExportComponents ( unityGo , fbxScene , fbxRootNode , exportProgress , count , TransformExportType . Reset ) ;
827
+ exportProgress = this . ExportComponents (
828
+ unityGo , fbxScene , fbxRootNode , exportProgress ,
829
+ count , Vector3 . zero , TransformExportType . Reset ) ;
805
830
if ( exportProgress < 0 ) {
806
831
Debug . LogWarning ( "Export Cancelled" ) ;
807
832
return 0 ;
@@ -815,8 +840,8 @@ public int ExportAll (IEnumerable<UnityEngine.Object> unityExportSet)
815
840
test . transform . position = center ;
816
841
817
842
foreach ( var unityGo in revisedExportSet ) {
818
- exportProgress = this . ExportComponents ( unityGo , fbxScene , fbxRootNode , exportProgress , count ,
819
- unityGo . transform . parent == null ? TransformExportType . Local : TransformExportType . Global ) ;
843
+ exportProgress = this . ExportComponents ( unityGo , fbxScene , fbxRootNode ,
844
+ exportProgress , count , center , TransformExportType . Global ) ;
820
845
if ( exportProgress < 0 ) {
821
846
Debug . LogWarning ( "Export Cancelled" ) ;
822
847
return 0 ;
0 commit comments