@@ -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,9 +50,12 @@ 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 ) ;
@@ -76,13 +88,30 @@ public void TestDefaultSelection ()
76
88
// Expected result: Parent2, Child3, Child2
77
89
// Expected transform: Child2 and Parent2 maintain global transform
78
90
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 ;
80
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
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,10 @@ 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 ) ) {
96
126
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 ) ;
98
128
Assert . AreEqual ( expectedMatrix , actualMatrix ) ;
99
129
}
100
130
@@ -104,10 +134,11 @@ private void CompareGlobalTransform(Transform actual, Transform expected=null){
104
134
/// <returns>The TRS matrix.</returns>
105
135
/// <param name="t">Transform.</param>
106
136
/// <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 ) )
108
139
{
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 ;
111
142
var scale = local ? t . localScale : t . lossyScale ;
112
143
return new FbxAMatrix (
113
144
new FbxVector4 ( translation . x , translation . y , translation . z ) ,
@@ -116,6 +147,10 @@ private FbxAMatrix ConstructTRSMatrix(Transform t, bool local=true)
116
147
) ;
117
148
}
118
149
150
+ /// <summary>
151
+ /// Creates test hierarchy.
152
+ /// </summary>
153
+ /// <returns>The hierarchy root.</returns>
119
154
private GameObject CreateHierarchy ( )
120
155
{
121
156
// Create the following hierarchy:
@@ -153,19 +188,39 @@ private GameObject CreateHierarchy ()
153
188
return root ;
154
189
}
155
190
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>
156
198
private void SetTransform ( Transform t , Vector3 pos , Vector3 rot , Vector3 scale ) {
157
199
t . localPosition = pos ;
158
200
t . localEulerAngles = rot ;
159
201
t . localScale = scale ;
160
202
}
161
203
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>
162
210
private GameObject CreateGameObject ( string name , Transform parent = null )
163
211
{
164
212
var go = new GameObject ( name ) ;
165
213
go . transform . SetParent ( parent ) ;
166
214
return go ;
167
215
}
168
216
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>
169
224
private void CompareHierarchies (
170
225
GameObject expectedHierarchy , GameObject actualHierarchy ,
171
226
bool ignoreName = false , bool compareTransform = true )
@@ -190,7 +245,13 @@ private void CompareHierarchies(
190
245
}
191
246
}
192
247
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 ) )
194
255
{
195
256
Assert . AreEqual ( expectedHierarchy . Length , actualHierarchy . Length ) ;
196
257
@@ -205,7 +266,7 @@ private void CompareHierarchies(GameObject[] expectedHierarchy, GameObject[] act
205
266
CompareHierarchies ( expectedHierarchy [ i ] , actualHierarchy [ i ] , false , false ) ;
206
267
// if we are Comparing lists of hierarchies, that means that the transforms
207
268
// 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 ) ;
209
270
}
210
271
}
211
272
}
0 commit comments