@@ -526,7 +526,7 @@ public static FbxDouble3 QuaternionToXYZEuler(Quaternion q)
526
526
}
527
527
528
528
// get a fbxNode's global default position.
529
- protected void ExportTransform ( UnityEngine . Transform unityTransform , FbxNode fbxNode , TransformExportType exportType )
529
+ protected void ExportTransform ( UnityEngine . Transform unityTransform , FbxNode fbxNode , Vector3 newCenter , TransformExportType exportType )
530
530
{
531
531
// Fbx rotation order is XYZ, but Unity rotation order is ZXY.
532
532
// This causes issues when converting euler to quaternion, causing the final
@@ -539,13 +539,13 @@ protected void ExportTransform (UnityEngine.Transform unityTransform, FbxNode fb
539
539
UnityEngine . Vector3 unityScale ;
540
540
541
541
switch ( exportType ) {
542
- case TransformExportType . Zeroed :
542
+ case TransformExportType . Reset :
543
543
unityTranslate = Vector3 . zero ;
544
544
unityRotate = new FbxDouble3 ( 0 ) ;
545
545
unityScale = Vector3 . one ;
546
546
break ;
547
547
case TransformExportType . Global :
548
- unityTranslate = unityTransform . position ;
548
+ unityTranslate = GetRecenteredTranslation ( unityTransform , newCenter ) ;
549
549
unityRotate = QuaternionToXYZEuler ( unityTransform . rotation ) ;
550
550
unityScale = unityTransform . lossyScale ;
551
551
break ;
@@ -615,7 +615,8 @@ protected bool ExportInstance (GameObject unityGo, FbxNode fbxNode, FbxScene fbx
615
615
/// </summary>
616
616
protected int ExportComponents (
617
617
GameObject unityGo , FbxScene fbxScene , FbxNode fbxNodeParent ,
618
- int exportProgress , int objectCount , TransformExportType exportType = TransformExportType . Local )
618
+ int exportProgress , int objectCount , Vector3 newCenter ,
619
+ TransformExportType exportType = TransformExportType . Local )
619
620
{
620
621
int numObjectsExported = exportProgress ;
621
622
@@ -636,7 +637,7 @@ protected int ExportComponents (
636
637
return - 1 ;
637
638
}
638
639
639
- ExportTransform ( unityGo . transform , fbxNode , exportType ) ;
640
+ ExportTransform ( unityGo . transform , fbxNode , newCenter , exportType ) ;
640
641
641
642
// try exporting mesh as an instance, export regularly if we cannot
642
643
if ( ! ExportInstance ( unityGo , fbxNode , fbxScene ) ) {
@@ -651,7 +652,7 @@ protected int ExportComponents (
651
652
652
653
// now unityGo through our children and recurse
653
654
foreach ( Transform childT in unityGo . transform ) {
654
- numObjectsExported = ExportComponents ( childT . gameObject , fbxScene , fbxNode , numObjectsExported , objectCount ) ;
655
+ numObjectsExported = ExportComponents ( childT . gameObject , fbxScene , fbxNode , numObjectsExported , objectCount , newCenter ) ;
655
656
}
656
657
return numObjectsExported ;
657
658
}
@@ -714,7 +715,79 @@ public static HashSet<GameObject> RemoveRedundantObjects(IEnumerable<UnityEngine
714
715
return toExport ;
715
716
}
716
717
717
- public enum TransformExportType { Local , Global , Zeroed } ;
718
+ /// <summary>
719
+ /// Recursively go through the hierarchy, unioning the bounding box centers
720
+ /// of all the children, to find the combined bounds.
721
+ /// </summary>
722
+ /// <param name="t">Transform.</param>
723
+ /// <param name="boundsUnion">The Bounds that is the Union of all the bounds on this transform's hierarchy.</param>
724
+ private void EncapsulateBounds ( Transform t , ref Bounds boundsUnion )
725
+ {
726
+ var bounds = GetBounds ( t ) ;
727
+ boundsUnion . Encapsulate ( bounds ) ;
728
+
729
+ foreach ( Transform child in t ) {
730
+ EncapsulateBounds ( child , ref boundsUnion ) ;
731
+ }
732
+ }
733
+
734
+ /// <summary>
735
+ /// Gets the bounds of a transform.
736
+ /// Looks first at the Renderer, then Mesh, then Collider.
737
+ /// Default to a bounds with center transform.position and size zero.
738
+ /// </summary>
739
+ /// <returns>The bounds.</returns>
740
+ /// <param name="t">Transform.</param>
741
+ private Bounds GetBounds ( Transform t )
742
+ {
743
+ var renderer = t . GetComponent < Renderer > ( ) ;
744
+ if ( renderer ) {
745
+ return renderer . bounds ;
746
+ }
747
+ var mesh = t . GetComponent < Mesh > ( ) ;
748
+ if ( mesh ) {
749
+ return mesh . bounds ;
750
+ }
751
+ var collider = t . GetComponent < Collider > ( ) ;
752
+ if ( collider ) {
753
+ return collider . bounds ;
754
+ }
755
+ return new Bounds ( t . position , Vector3 . zero ) ;
756
+ }
757
+
758
+ /// <summary>
759
+ /// Finds the center of a group of GameObjects.
760
+ /// </summary>
761
+ /// <returns>Center of gameObjects.</returns>
762
+ /// <param name="gameObjects">Game objects.</param>
763
+ private Vector3 FindCenter ( IEnumerable < GameObject > gameObjects )
764
+ {
765
+ Bounds bounds = new Bounds ( ) ;
766
+ // Assign the initial bounds to first GameObject's bounds
767
+ // (if we initialize the bounds to 0, then 0 will be part of the bounds)
768
+ foreach ( var go in gameObjects ) {
769
+ var tempBounds = GetBounds ( go . transform ) ;
770
+ bounds = new Bounds ( tempBounds . center , tempBounds . size ) ;
771
+ break ;
772
+ }
773
+ foreach ( var go in gameObjects ) {
774
+ EncapsulateBounds ( go . transform , ref bounds ) ;
775
+ }
776
+ return bounds . center ;
777
+ }
778
+
779
+ /// <summary>
780
+ /// Gets the recentered translation.
781
+ /// </summary>
782
+ /// <returns>The recentered translation.</returns>
783
+ /// <param name="t">Transform.</param>
784
+ /// <param name="center">Center point.</param>
785
+ private Vector3 GetRecenteredTranslation ( Transform t , Vector3 center )
786
+ {
787
+ return t . position - center ;
788
+ }
789
+
790
+ public enum TransformExportType { Local , Global , Reset } ;
718
791
719
792
/// <summary>
720
793
/// Export all the objects in the set.
@@ -788,17 +861,22 @@ public int ExportAll (IEnumerable<UnityEngine.Object> unityExportSet)
788
861
789
862
if ( revisedExportSet . Count == 1 ) {
790
863
foreach ( var unityGo in revisedExportSet ) {
791
- exportProgress = this . ExportComponents ( unityGo , fbxScene , fbxRootNode , exportProgress , count , TransformExportType . Zeroed ) ;
864
+ exportProgress = this . ExportComponents (
865
+ unityGo , fbxScene , fbxRootNode , exportProgress ,
866
+ count , Vector3 . zero , TransformExportType . Reset ) ;
792
867
if ( exportProgress < 0 ) {
793
868
Debug . LogWarning ( "Export Cancelled" ) ;
794
869
return 0 ;
795
870
}
796
871
}
797
872
}
798
873
else {
874
+ // find the center of the export set
875
+ Vector3 center = EditorTools . ExportSettings . instance . centerObjects ? FindCenter ( revisedExportSet ) : Vector3 . zero ;
876
+
799
877
foreach ( var unityGo in revisedExportSet ) {
800
- exportProgress = this . ExportComponents ( unityGo , fbxScene , fbxRootNode , exportProgress , count ,
801
- unityGo . transform . parent == null ? TransformExportType . Local : TransformExportType . Global ) ;
878
+ exportProgress = this . ExportComponents ( unityGo , fbxScene , fbxRootNode ,
879
+ exportProgress , count , center , TransformExportType . Global ) ;
802
880
if ( exportProgress < 0 ) {
803
881
Debug . LogWarning ( "Export Cancelled" ) ;
804
882
return 0 ;
0 commit comments