@@ -16,6 +16,13 @@ namespace FbxExporters.UnitTests
16
16
public class DefaultSelectionTest : ExporterTestBase
17
17
{
18
18
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
+ }
19
26
20
27
[ TearDown ]
21
28
public override void Term ( )
@@ -24,6 +31,8 @@ public override void Term ()
24
31
if ( m_root ) {
25
32
UnityEngine . Object . DestroyImmediate ( m_root ) ;
26
33
}
34
+ // restore original setting
35
+ FbxExporters . EditorTools . ExportSettings . instance . centerObjects = m_centerObjectsSetting ;
27
36
}
28
37
29
38
[ Test ]
@@ -41,48 +50,68 @@ public void TestDefaultSelection ()
41
50
// - if there is only one root GameObject being exported
42
51
// then zero out the root transform, leave all descendants
43
52
// 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.
47
59
48
60
m_root = CreateHierarchy ( ) ;
49
61
Assert . IsNotNull ( m_root ) ;
50
62
51
63
// test Export Root
52
64
// Expected result: everything gets exported
53
65
// 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 ) ;
56
68
CompareGlobalTransform ( exportedRoot . transform ) ;
57
69
58
70
// test Export Parent1, Child1
59
71
// Expected result: Parent1, Child1, Child2
60
72
// 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" ) ;
62
74
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 ) ;
65
77
CompareGlobalTransform ( exportedRoot . transform ) ;
66
78
67
79
// test Export Child2
68
80
// Expected result: Child2
69
81
// 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 ) ;
73
85
CompareGlobalTransform ( exportedRoot . transform ) ;
74
86
75
87
// test Export Child2, Parent2
76
88
// Expected result: Parent2, Child3, Child2
77
89
// 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 } ;
80
94
95
+ // test without centering objects
96
+ FbxExporters . EditorTools . ExportSettings . instance . centerObjects = false ;
97
+
98
+ exportedRoot = ExportSelection ( exportSet ) ;
81
99
List < GameObject > children = new List < GameObject > ( ) ;
82
100
foreach ( Transform child in exportedRoot . transform ) {
83
101
children . Add ( child . gameObject ) ;
84
102
}
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 ) ;
86
115
}
87
116
88
117
/// <summary>
@@ -92,9 +121,11 @@ public void TestDefaultSelection ()
92
121
/// </summary>
93
122
/// <param name="actual">Actual.</param>
94
123
/// <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
+ {
96
127
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 ) ;
98
129
Assert . AreEqual ( expectedMatrix , actualMatrix ) ;
99
130
}
100
131
@@ -104,18 +135,23 @@ private void CompareGlobalTransform(Transform actual, Transform expected=null){
104
135
/// <returns>The TRS matrix.</returns>
105
136
/// <param name="t">Transform.</param>
106
137
/// <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 ) )
108
140
{
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 ;
112
144
return new FbxAMatrix (
113
145
new FbxVector4 ( translation . x , translation . y , translation . z ) ,
114
146
new FbxVector4 ( rotation . x , rotation . y , rotation . z ) ,
115
147
new FbxVector4 ( scale . x , scale . y , scale . z )
116
148
) ;
117
149
}
118
150
151
+ /// <summary>
152
+ /// Creates test hierarchy.
153
+ /// </summary>
154
+ /// <returns>The hierarchy root.</returns>
119
155
private GameObject CreateHierarchy ( )
120
156
{
121
157
// Create the following hierarchy:
@@ -153,20 +189,41 @@ private GameObject CreateHierarchy ()
153
189
return root ;
154
190
}
155
191
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
+ {
157
201
t . localPosition = pos ;
158
202
t . localEulerAngles = rot ;
159
203
t . localScale = scale ;
160
204
}
161
205
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 )
163
213
{
164
214
var go = new GameObject ( name ) ;
165
215
go . transform . SetParent ( parent ) ;
166
216
return go ;
167
- }
217
+ }
168
218
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 (
170
227
GameObject expectedHierarchy , GameObject actualHierarchy ,
171
228
bool ignoreName = false , bool compareTransform = true )
172
229
{
@@ -190,22 +247,28 @@ private void CompareHierarchies(
190
247
}
191
248
}
192
249
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 ) )
194
257
{
195
258
Assert . AreEqual ( expectedHierarchy . Length , actualHierarchy . Length ) ;
196
259
197
260
System . Array . Sort ( expectedHierarchy , delegate ( GameObject x , GameObject y ) {
198
- return x . name . CompareTo ( y . name ) ;
261
+ return x . name . CompareTo ( y . name ) ;
199
262
} ) ;
200
263
System . Array . Sort ( actualHierarchy , delegate ( GameObject x , GameObject y ) {
201
- return x . name . CompareTo ( y . name ) ;
264
+ return x . name . CompareTo ( y . name ) ;
202
265
} ) ;
203
266
204
267
for ( int i = 0 ; i < expectedHierarchy . Length ; i ++ ) {
205
268
CompareHierarchies ( expectedHierarchy [ i ] , actualHierarchy [ i ] , false , false ) ;
206
269
// if we are Comparing lists of hierarchies, that means that the transforms
207
270
// 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 ) ;
209
272
}
210
273
}
211
274
}
0 commit comments