Skip to content

Commit f9ad815

Browse files
committed
Merge remote-tracking branch 'refs/remotes/origin/master' into UNI-21594-incorrect-rotation-in-maya
# Conflicts: # Assets/FbxExporters/Editor/FbxExporter.cs
2 parents faf2314 + 4d9b4c4 commit f9ad815

File tree

3 files changed

+100
-12
lines changed

3 files changed

+100
-12
lines changed

Assets/FbxExporters/Editor/ConvertToModel.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,12 @@ public static bool OnValidateMenuItem ()
5454
static void OnContextItem (MenuCommand command)
5555
{
5656
if (command == null || command.context == null) {
57-
Debug.LogError ("Error: No GameObject selected");
57+
// We were actually invoked from the top GameObject menu,
58+
// not the context menu, so treat it as such.
59+
OnMenuItem();
5860
return;
5961
}
62+
6063
GameObject selected = command.context as GameObject;
6164
if (selected == null) {
6265
Debug.LogError (string.Format("Error: {0} is not a GameObject and cannot be converted", command.context.name));

Assets/FbxExporters/Editor/FbxExportSettings.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,13 @@ public override void OnInspectorGUI() {
3333
" and unexpected character replacements in Maya.")
3434
),
3535
exportSettings.mayaCompatibleNames);
36-
36+
37+
exportSettings.centerObjects = EditorGUILayout.Toggle (
38+
new GUIContent("Center Objects:",
39+
"Export objects centered around the union of the bounding box of selected objects"),
40+
exportSettings.centerObjects
41+
);
42+
3743
GUILayout.BeginHorizontal ();
3844
GUILayout.Label (new GUIContent (
3945
"Model Prefab Path:",
@@ -92,6 +98,7 @@ public class ExportSettings : FbxExporters.EditorTools.ScriptableSingleton<Expor
9298
public bool weldVertices = true;
9399
public bool embedTextures = false;
94100
public bool mayaCompatibleNames = true;
101+
public bool centerObjects = true;
95102
public string convertToModelSavePath;
96103

97104
void OnEnable()

Assets/FbxExporters/Editor/FbxExporter.cs

Lines changed: 88 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -526,7 +526,7 @@ public static FbxDouble3 QuaternionToXYZEuler(Quaternion q)
526526
}
527527

528528
// 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)
530530
{
531531
// Fbx rotation order is XYZ, but Unity rotation order is ZXY.
532532
// This causes issues when converting euler to quaternion, causing the final
@@ -539,13 +539,13 @@ protected void ExportTransform (UnityEngine.Transform unityTransform, FbxNode fb
539539
UnityEngine.Vector3 unityScale;
540540

541541
switch (exportType) {
542-
case TransformExportType.Zeroed:
542+
case TransformExportType.Reset:
543543
unityTranslate = Vector3.zero;
544544
unityRotate = new FbxDouble3 (0);
545545
unityScale = Vector3.one;
546546
break;
547547
case TransformExportType.Global:
548-
unityTranslate = unityTransform.position;
548+
unityTranslate = GetRecenteredTranslation(unityTransform, newCenter);
549549
unityRotate = QuaternionToXYZEuler(unityTransform.rotation);
550550
unityScale = unityTransform.lossyScale;
551551
break;
@@ -615,7 +615,8 @@ protected bool ExportInstance (GameObject unityGo, FbxNode fbxNode, FbxScene fbx
615615
/// </summary>
616616
protected int ExportComponents (
617617
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)
619620
{
620621
int numObjectsExported = exportProgress;
621622

@@ -636,7 +637,7 @@ protected int ExportComponents (
636637
return -1;
637638
}
638639

639-
ExportTransform ( unityGo.transform, fbxNode, exportType);
640+
ExportTransform ( unityGo.transform, fbxNode, newCenter, exportType);
640641

641642
// try exporting mesh as an instance, export regularly if we cannot
642643
if (!ExportInstance (unityGo, fbxNode, fbxScene)) {
@@ -651,7 +652,7 @@ protected int ExportComponents (
651652

652653
// now unityGo through our children and recurse
653654
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);
655656
}
656657
return numObjectsExported;
657658
}
@@ -714,7 +715,79 @@ public static HashSet<GameObject> RemoveRedundantObjects(IEnumerable<UnityEngine
714715
return toExport;
715716
}
716717

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 };
718791

719792
/// <summary>
720793
/// Export all the objects in the set.
@@ -788,17 +861,22 @@ public int ExportAll (IEnumerable<UnityEngine.Object> unityExportSet)
788861

789862
if(revisedExportSet.Count == 1){
790863
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);
792867
if (exportProgress < 0) {
793868
Debug.LogWarning ("Export Cancelled");
794869
return 0;
795870
}
796871
}
797872
}
798873
else{
874+
// find the center of the export set
875+
Vector3 center = EditorTools.ExportSettings.instance.centerObjects? FindCenter(revisedExportSet) : Vector3.zero;
876+
799877
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);
802880
if (exportProgress < 0) {
803881
Debug.LogWarning ("Export Cancelled");
804882
return 0;

0 commit comments

Comments
 (0)