@@ -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
@@ -516,13 +516,13 @@ protected void ExportTransform (UnityEngine.Transform unityTransform, FbxNode fb
516
516
UnityEngine . Vector3 unityScale ;
517
517
518
518
switch ( exportType ) {
519
- case TransformExportType . Zeroed :
519
+ case TransformExportType . Reset :
520
520
unityTranslate = Vector3 . zero ;
521
521
unityRotate = Vector3 . zero ;
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,7 +692,79 @@ public static HashSet<GameObject> RemoveRedundantObjects(IEnumerable<UnityEngine
691
692
return toExport ;
692
693
}
693
694
694
- public enum TransformExportType { Local , Global , Zeroed } ;
695
+ /// <summary>
696
+ /// Recursively go through the hierarchy, unioning the bounding box centers
697
+ /// of all the children, to find the combined bounds.
698
+ /// </summary>
699
+ /// <param name="t">Transform.</param>
700
+ /// <param name="boundsUnion">The Bounds that is the Union of all the bounds on this transform's hierarchy.</param>
701
+ private void EncapsulateBounds ( Transform t , ref Bounds boundsUnion )
702
+ {
703
+ var bounds = GetBounds ( t ) ;
704
+ boundsUnion . Encapsulate ( bounds ) ;
705
+
706
+ foreach ( Transform child in t ) {
707
+ EncapsulateBounds ( child , ref boundsUnion ) ;
708
+ }
709
+ }
710
+
711
+ /// <summary>
712
+ /// Gets the bounds of a transform.
713
+ /// Looks first at the Renderer, then Mesh, then Collider.
714
+ /// Default to a bounds with center transform.position and size zero.
715
+ /// </summary>
716
+ /// <returns>The bounds.</returns>
717
+ /// <param name="t">Transform.</param>
718
+ private Bounds GetBounds ( Transform t )
719
+ {
720
+ var renderer = t . GetComponent < Renderer > ( ) ;
721
+ if ( renderer ) {
722
+ return renderer . bounds ;
723
+ }
724
+ var mesh = t . GetComponent < Mesh > ( ) ;
725
+ if ( mesh ) {
726
+ return mesh . bounds ;
727
+ }
728
+ var collider = t . GetComponent < Collider > ( ) ;
729
+ if ( collider ) {
730
+ return collider . bounds ;
731
+ }
732
+ return new Bounds ( t . position , Vector3 . zero ) ;
733
+ }
734
+
735
+ /// <summary>
736
+ /// Finds the center of a group of GameObjects.
737
+ /// </summary>
738
+ /// <returns>Center of gameObjects.</returns>
739
+ /// <param name="gameObjects">Game objects.</param>
740
+ private Vector3 FindCenter ( IEnumerable < GameObject > gameObjects )
741
+ {
742
+ Bounds bounds = new Bounds ( ) ;
743
+ // Assign the initial bounds to first GameObject's bounds
744
+ // (if we initialize the bounds to 0, then 0 will be part of the bounds)
745
+ foreach ( var go in gameObjects ) {
746
+ var tempBounds = GetBounds ( go . transform ) ;
747
+ bounds = new Bounds ( tempBounds . center , tempBounds . size ) ;
748
+ break ;
749
+ }
750
+ foreach ( var go in gameObjects ) {
751
+ EncapsulateBounds ( go . transform , ref bounds ) ;
752
+ }
753
+ return bounds . center ;
754
+ }
755
+
756
+ /// <summary>
757
+ /// Gets the recentered translation.
758
+ /// </summary>
759
+ /// <returns>The recentered translation.</returns>
760
+ /// <param name="t">Transform.</param>
761
+ /// <param name="center">Center point.</param>
762
+ private Vector3 GetRecenteredTranslation ( Transform t , Vector3 center )
763
+ {
764
+ return t . position - center ;
765
+ }
766
+
767
+ public enum TransformExportType { Local , Global , Reset } ;
695
768
696
769
/// <summary>
697
770
/// Export all the objects in the set.
@@ -764,17 +837,22 @@ public int ExportAll (IEnumerable<UnityEngine.Object> unityExportSet)
764
837
765
838
if ( revisedExportSet . Count == 1 ) {
766
839
foreach ( var unityGo in revisedExportSet ) {
767
- exportProgress = this . ExportComponents ( unityGo , fbxScene , fbxRootNode , exportProgress , count , TransformExportType . Zeroed ) ;
840
+ exportProgress = this . ExportComponents (
841
+ unityGo , fbxScene , fbxRootNode , exportProgress ,
842
+ count , Vector3 . zero , TransformExportType . Reset ) ;
768
843
if ( exportProgress < 0 ) {
769
844
Debug . LogWarning ( "Export Cancelled" ) ;
770
845
return 0 ;
771
846
}
772
847
}
773
848
}
774
849
else {
850
+ // find the center of the export set
851
+ Vector3 center = EditorTools . ExportSettings . instance . centerObjects ? FindCenter ( revisedExportSet ) : Vector3 . zero ;
852
+
775
853
foreach ( var unityGo in revisedExportSet ) {
776
- exportProgress = this . ExportComponents ( unityGo , fbxScene , fbxRootNode , exportProgress , count ,
777
- unityGo . transform . parent == null ? TransformExportType . Local : TransformExportType . Global ) ;
854
+ exportProgress = this . ExportComponents ( unityGo , fbxScene , fbxRootNode ,
855
+ exportProgress , count , center , TransformExportType . Global ) ;
778
856
if ( exportProgress < 0 ) {
779
857
Debug . LogWarning ( "Export Cancelled" ) ;
780
858
return 0 ;
0 commit comments