Skip to content

Commit e211433

Browse files
authored
Merge pull request #67 from Unity-Technologies/fix-default-selection-test-failing
fix default selection test failing
2 parents 4a73511 + d3f350b commit e211433

File tree

2 files changed

+95
-32
lines changed

2 files changed

+95
-32
lines changed

Assets/FbxExporters/Editor/FbxExporter.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -823,7 +823,7 @@ public static HashSet<GameObject> RemoveRedundantObjects(IEnumerable<UnityEngine
823823
/// </summary>
824824
/// <param name="t">Transform.</param>
825825
/// <param name="boundsUnion">The Bounds that is the Union of all the bounds on this transform's hierarchy.</param>
826-
private void EncapsulateBounds(Transform t, ref Bounds boundsUnion)
826+
private static void EncapsulateBounds(Transform t, ref Bounds boundsUnion)
827827
{
828828
var bounds = GetBounds (t);
829829
boundsUnion.Encapsulate (bounds);
@@ -840,7 +840,7 @@ private void EncapsulateBounds(Transform t, ref Bounds boundsUnion)
840840
/// </summary>
841841
/// <returns>The bounds.</returns>
842842
/// <param name="t">Transform.</param>
843-
private Bounds GetBounds(Transform t)
843+
private static Bounds GetBounds(Transform t)
844844
{
845845
var renderer = t.GetComponent<Renderer> ();
846846
if (renderer) {
@@ -862,7 +862,7 @@ private Bounds GetBounds(Transform t)
862862
/// </summary>
863863
/// <returns>Center of gameObjects.</returns>
864864
/// <param name="gameObjects">Game objects.</param>
865-
private Vector3 FindCenter(IEnumerable<GameObject> gameObjects)
865+
public static Vector3 FindCenter(IEnumerable<GameObject> gameObjects)
866866
{
867867
Bounds bounds = new Bounds();
868868
// Assign the initial bounds to first GameObject's bounds
@@ -884,7 +884,7 @@ private Vector3 FindCenter(IEnumerable<GameObject> gameObjects)
884884
/// <returns>The recentered translation.</returns>
885885
/// <param name="t">Transform.</param>
886886
/// <param name="center">Center point.</param>
887-
private Vector3 GetRecenteredTranslation(Transform t, Vector3 center)
887+
public static Vector3 GetRecenteredTranslation(Transform t, Vector3 center)
888888
{
889889
return t.position - center;
890890
}

Assets/FbxExporters/Editor/UnitTests/DefaultSelectionTest.cs

Lines changed: 91 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,13 @@ namespace FbxExporters.UnitTests
1616
public class DefaultSelectionTest : ExporterTestBase
1717
{
1818
protected GameObject m_root;
19+
protected bool m_centerObjectsSetting;
20+
21+
[SetUp]
22+
public void Init ()
23+
{
24+
m_centerObjectsSetting = FbxExporters.EditorTools.ExportSettings.instance.centerObjects;
25+
}
1926

2027
[TearDown]
2128
public override void Term ()
@@ -24,6 +31,8 @@ public override void Term ()
2431
if (m_root) {
2532
UnityEngine.Object.DestroyImmediate (m_root);
2633
}
34+
// restore original setting
35+
FbxExporters.EditorTools.ExportSettings.instance.centerObjects = m_centerObjectsSetting;
2736
}
2837

2938
[Test]
@@ -41,48 +50,68 @@ public void TestDefaultSelection ()
4150
// - if there is only one root GameObject being exported
4251
// then zero out the root transform, leave all descendants
4352
// with local transform
44-
// - if there are multiple root GameObjects, then export
45-
// the global transform of each, and local transform
46-
// of descendants
53+
// - if there are multiple root GameObjects, export
54+
// the global transform of root GameObjects, and local transform
55+
// of descendants.
56+
// if Center Objects is checked in the preferences,
57+
// then export the translations so they are centered
58+
// around the center of the union of the bounding boxes.
4759

4860
m_root = CreateHierarchy ();
4961
Assert.IsNotNull (m_root);
5062

5163
// test Export Root
5264
// Expected result: everything gets exported
5365
// Expected transform: root is zeroed out, all other transforms unchanged
54-
var exportedRoot = ExportSelection (new Object[]{m_root});
55-
CompareHierarchies(m_root, exportedRoot, true, false);
66+
var exportedRoot = ExportSelection (new Object[]{ m_root });
67+
CompareHierarchies (m_root, exportedRoot, true, false);
5668
CompareGlobalTransform (exportedRoot.transform);
5769

5870
// test Export Parent1, Child1
5971
// Expected result: Parent1, Child1, Child2
6072
// Expected transform: Parent1 zeroed out, all other transforms unchanged
61-
var parent1 = m_root.transform.Find("Parent1");
73+
var parent1 = m_root.transform.Find ("Parent1");
6274
var child1 = parent1.Find ("Child1");
63-
exportedRoot = ExportSelection (new Object[]{parent1.gameObject, child1.gameObject});
64-
CompareHierarchies(parent1.gameObject, exportedRoot, true, false);
75+
exportedRoot = ExportSelection (new Object[]{ parent1.gameObject, child1.gameObject });
76+
CompareHierarchies (parent1.gameObject, exportedRoot, true, false);
6577
CompareGlobalTransform (exportedRoot.transform);
6678

6779
// test Export Child2
6880
// Expected result: Child2
6981
// Expected transform: Child2 zeroed out
70-
var child2 = parent1.Find("Child2").gameObject;
71-
exportedRoot = ExportSelection (new Object[]{child2});
72-
CompareHierarchies(child2, exportedRoot, true, false);
82+
var child2 = parent1.Find ("Child2").gameObject;
83+
exportedRoot = ExportSelection (new Object[]{ child2 });
84+
CompareHierarchies (child2, exportedRoot, true, false);
7385
CompareGlobalTransform (exportedRoot.transform);
7486

7587
// test Export Child2, Parent2
7688
// Expected result: Parent2, Child3, Child2
7789
// Expected transform: Child2 and Parent2 maintain global transform
78-
var parent2 = m_root.transform.Find("Parent2");
79-
exportedRoot = ExportSelection (new Object[]{child2, parent2});
90+
var parent2 = m_root.transform.Find ("Parent2");
91+
var exportSet = new Object[]{ child2, parent2 };
92+
// for passing to FindCenter()
93+
var goExportSet = new GameObject[]{ child2.gameObject, parent2.gameObject };
8094

95+
// test without centering objects
96+
FbxExporters.EditorTools.ExportSettings.instance.centerObjects = false;
97+
98+
exportedRoot = ExportSelection (exportSet);
8199
List<GameObject> children = new List<GameObject> ();
82100
foreach (Transform child in exportedRoot.transform) {
83101
children.Add (child.gameObject);
84102
}
85-
CompareHierarchies(new GameObject[]{child2, parent2.gameObject}, children.ToArray());
103+
CompareHierarchies (new GameObject[]{ child2, parent2.gameObject }, children.ToArray ());
104+
105+
// test with centered objects
106+
FbxExporters.EditorTools.ExportSettings.instance.centerObjects = true;
107+
var newCenter = FbxExporters.Editor.ModelExporter.FindCenter (goExportSet);
108+
109+
exportedRoot = ExportSelection (exportSet);
110+
children = new List<GameObject> ();
111+
foreach (Transform child in exportedRoot.transform) {
112+
children.Add (child.gameObject);
113+
}
114+
CompareHierarchies (new GameObject[]{ child2, parent2.gameObject }, children.ToArray (), newCenter);
86115
}
87116

88117
/// <summary>
@@ -92,9 +121,11 @@ public void TestDefaultSelection ()
92121
/// </summary>
93122
/// <param name="actual">Actual.</param>
94123
/// <param name="expected">Expected.</param>
95-
private void CompareGlobalTransform(Transform actual, Transform expected=null){
124+
/// <param name="center">New center for expected transform, if present.</param>
125+
private void CompareGlobalTransform (Transform actual, Transform expected = null, Vector3 center = default(Vector3))
126+
{
96127
var actualMatrix = ConstructTRSMatrix (actual);
97-
var expectedMatrix = expected == null? new FbxAMatrix() : ConstructTRSMatrix (expected, false);
128+
var expectedMatrix = expected == null ? new FbxAMatrix () : ConstructTRSMatrix (expected, false, center);
98129
Assert.AreEqual (expectedMatrix, actualMatrix);
99130
}
100131

@@ -104,18 +135,23 @@ private void CompareGlobalTransform(Transform actual, Transform expected=null){
104135
/// <returns>The TRS matrix.</returns>
105136
/// <param name="t">Transform.</param>
106137
/// <param name="local">If set to <c>true</c> use local transform.</param>
107-
private FbxAMatrix ConstructTRSMatrix(Transform t, bool local=true)
138+
/// <param name="center">New center for global transform.</param>
139+
private FbxAMatrix ConstructTRSMatrix (Transform t, bool local = true, Vector3 center = default(Vector3))
108140
{
109-
var translation = local? t.localPosition : t.position;
110-
var rotation = local? t.localEulerAngles : t.eulerAngles;
111-
var scale = local? t.localScale : t.lossyScale;
141+
var translation = local ? t.localPosition : FbxExporters.Editor.ModelExporter.GetRecenteredTranslation (t, center);
142+
var rotation = local ? t.localEulerAngles : t.eulerAngles;
143+
var scale = local ? t.localScale : t.lossyScale;
112144
return new FbxAMatrix (
113145
new FbxVector4 (translation.x, translation.y, translation.z),
114146
new FbxVector4 (rotation.x, rotation.y, rotation.z),
115147
new FbxVector4 (scale.x, scale.y, scale.z)
116148
);
117149
}
118150

151+
/// <summary>
152+
/// Creates test hierarchy.
153+
/// </summary>
154+
/// <returns>The hierarchy root.</returns>
119155
private GameObject CreateHierarchy ()
120156
{
121157
// Create the following hierarchy:
@@ -153,20 +189,41 @@ private GameObject CreateHierarchy ()
153189
return root;
154190
}
155191

156-
private void SetTransform(Transform t, Vector3 pos, Vector3 rot, Vector3 scale){
192+
/// <summary>
193+
/// Sets the transform.
194+
/// </summary>
195+
/// <param name="t">Transform.</param>
196+
/// <param name="pos">Position.</param>
197+
/// <param name="rot">Rotation.</param>
198+
/// <param name="scale">Scale.</param>
199+
private void SetTransform (Transform t, Vector3 pos, Vector3 rot, Vector3 scale)
200+
{
157201
t.localPosition = pos;
158202
t.localEulerAngles = rot;
159203
t.localScale = scale;
160204
}
161205

162-
private GameObject CreateGameObject(string name, Transform parent = null)
206+
/// <summary>
207+
/// Creates a GameObject.
208+
/// </summary>
209+
/// <returns>The created GameObject.</returns>
210+
/// <param name="name">Name.</param>
211+
/// <param name="parent">Parent.</param>
212+
private GameObject CreateGameObject (string name, Transform parent = null)
163213
{
164214
var go = new GameObject (name);
165215
go.transform.SetParent (parent);
166216
return go;
167-
}
217+
}
168218

169-
private void CompareHierarchies(
219+
/// <summary>
220+
/// Compares the hierarchies.
221+
/// </summary>
222+
/// <param name="expectedHierarchy">Expected hierarchy.</param>
223+
/// <param name="actualHierarchy">Actual hierarchy.</param>
224+
/// <param name="ignoreName">If set to <c>true</c> ignore name.</param>
225+
/// <param name="compareTransform">If set to <c>true</c> compare transform.</param>
226+
private void CompareHierarchies (
170227
GameObject expectedHierarchy, GameObject actualHierarchy,
171228
bool ignoreName = false, bool compareTransform = true)
172229
{
@@ -190,22 +247,28 @@ private void CompareHierarchies(
190247
}
191248
}
192249

193-
private void CompareHierarchies(GameObject[] expectedHierarchy, GameObject[] actualHierarchy)
250+
/// <summary>
251+
/// Compares the hierarchies.
252+
/// </summary>
253+
/// <param name="expectedHierarchy">Expected hierarchy.</param>
254+
/// <param name="actualHierarchy">Actual hierarchy.</param>
255+
/// <param name="center">New center for global transforms.</param>
256+
private void CompareHierarchies (GameObject[] expectedHierarchy, GameObject[] actualHierarchy, Vector3 center = default(Vector3))
194257
{
195258
Assert.AreEqual (expectedHierarchy.Length, actualHierarchy.Length);
196259

197260
System.Array.Sort (expectedHierarchy, delegate (GameObject x, GameObject y) {
198-
return x.name.CompareTo(y.name);
261+
return x.name.CompareTo (y.name);
199262
});
200263
System.Array.Sort (actualHierarchy, delegate (GameObject x, GameObject y) {
201-
return x.name.CompareTo(y.name);
264+
return x.name.CompareTo (y.name);
202265
});
203266

204267
for (int i = 0; i < expectedHierarchy.Length; i++) {
205268
CompareHierarchies (expectedHierarchy [i], actualHierarchy [i], false, false);
206269
// if we are Comparing lists of hierarchies, that means that the transforms
207270
// should be the global transform of expected, as there is no zeroed out root
208-
CompareGlobalTransform (actualHierarchy [i].transform, expectedHierarchy [i].transform);
271+
CompareGlobalTransform (actualHierarchy [i].transform, expectedHierarchy [i].transform, center);
209272
}
210273
}
211274
}

0 commit comments

Comments
 (0)