Skip to content

Commit a44c4c6

Browse files
committed
added test for recentering transforms
1 parent 4a73511 commit a44c4c6

File tree

2 files changed

+76
-15
lines changed

2 files changed

+76
-15
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: 72 additions & 11 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,9 +50,12 @@ 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);
@@ -76,13 +88,30 @@ public void TestDefaultSelection ()
7688
// Expected result: Parent2, Child3, Child2
7789
// Expected transform: Child2 and Parent2 maintain global transform
7890
var parent2 = m_root.transform.Find("Parent2");
79-
exportedRoot = ExportSelection (new Object[]{child2, parent2});
91+
var exportSet = new Object[]{child2, parent2};
92+
// for passing to FindCenter()
93+
var goExportSet = new GameObject[]{ child2.gameObject, parent2.gameObject };
94+
95+
// test without centering objects
96+
FbxExporters.EditorTools.ExportSettings.instance.centerObjects = false;
8097

98+
exportedRoot = ExportSelection (exportSet);
8199
List<GameObject> children = new List<GameObject> ();
82100
foreach (Transform child in exportedRoot.transform) {
83101
children.Add (child.gameObject);
84102
}
85103
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,10 @@ 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)){
96126
var actualMatrix = ConstructTRSMatrix (actual);
97-
var expectedMatrix = expected == null? new FbxAMatrix() : ConstructTRSMatrix (expected, false);
127+
var expectedMatrix = expected == null? new FbxAMatrix() : ConstructTRSMatrix (expected, false, center);
98128
Assert.AreEqual (expectedMatrix, actualMatrix);
99129
}
100130

@@ -104,10 +134,11 @@ private void CompareGlobalTransform(Transform actual, Transform expected=null){
104134
/// <returns>The TRS matrix.</returns>
105135
/// <param name="t">Transform.</param>
106136
/// <param name="local">If set to <c>true</c> use local transform.</param>
107-
private FbxAMatrix ConstructTRSMatrix(Transform t, bool local=true)
137+
/// <param name="center">New center for global transform.</param>
138+
private FbxAMatrix ConstructTRSMatrix(Transform t, bool local=true, Vector3 center=default(Vector3))
108139
{
109-
var translation = local? t.localPosition : t.position;
110-
var rotation = local? t.localEulerAngles : t.eulerAngles;
140+
var translation = local? t.localPosition : FbxExporters.Editor.ModelExporter.GetRecenteredTranslation (t, center);
141+
var rotation = local? t.localEulerAngles : t.eulerAngles;
111142
var scale = local? t.localScale : t.lossyScale;
112143
return new FbxAMatrix (
113144
new FbxVector4 (translation.x, translation.y, translation.z),
@@ -116,6 +147,10 @@ private FbxAMatrix ConstructTRSMatrix(Transform t, bool local=true)
116147
);
117148
}
118149

150+
/// <summary>
151+
/// Creates test hierarchy.
152+
/// </summary>
153+
/// <returns>The hierarchy root.</returns>
119154
private GameObject CreateHierarchy ()
120155
{
121156
// Create the following hierarchy:
@@ -153,19 +188,39 @@ private GameObject CreateHierarchy ()
153188
return root;
154189
}
155190

191+
/// <summary>
192+
/// Sets the transform.
193+
/// </summary>
194+
/// <param name="t">Transform.</param>
195+
/// <param name="pos">Position.</param>
196+
/// <param name="rot">Rotation.</param>
197+
/// <param name="scale">Scale.</param>
156198
private void SetTransform(Transform t, Vector3 pos, Vector3 rot, Vector3 scale){
157199
t.localPosition = pos;
158200
t.localEulerAngles = rot;
159201
t.localScale = scale;
160202
}
161203

204+
/// <summary>
205+
/// Creates a GameObject.
206+
/// </summary>
207+
/// <returns>The created GameObject.</returns>
208+
/// <param name="name">Name.</param>
209+
/// <param name="parent">Parent.</param>
162210
private GameObject CreateGameObject(string name, Transform parent = null)
163211
{
164212
var go = new GameObject (name);
165213
go.transform.SetParent (parent);
166214
return go;
167215
}
168216

217+
/// <summary>
218+
/// Compares the hierarchies.
219+
/// </summary>
220+
/// <param name="expectedHierarchy">Expected hierarchy.</param>
221+
/// <param name="actualHierarchy">Actual hierarchy.</param>
222+
/// <param name="ignoreName">If set to <c>true</c> ignore name.</param>
223+
/// <param name="compareTransform">If set to <c>true</c> compare transform.</param>
169224
private void CompareHierarchies(
170225
GameObject expectedHierarchy, GameObject actualHierarchy,
171226
bool ignoreName = false, bool compareTransform = true)
@@ -190,7 +245,13 @@ private void CompareHierarchies(
190245
}
191246
}
192247

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

@@ -205,7 +266,7 @@ private void CompareHierarchies(GameObject[] expectedHierarchy, GameObject[] act
205266
CompareHierarchies (expectedHierarchy [i], actualHierarchy [i], false, false);
206267
// if we are Comparing lists of hierarchies, that means that the transforms
207268
// should be the global transform of expected, as there is no zeroed out root
208-
CompareGlobalTransform (actualHierarchy [i].transform, expectedHierarchy [i].transform);
269+
CompareGlobalTransform (actualHierarchy [i].transform, expectedHierarchy [i].transform, center);
209270
}
210271
}
211272
}

0 commit comments

Comments
 (0)