forked from MonoGame/MonoGame
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ModelProcessorTests.cs
285 lines (249 loc) · 11.6 KB
/
ModelProcessorTests.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
// MonoGame - Copyright (C) The MonoGame Team
// This file is subject to the terms and conditions defined in
// file 'LICENSE.txt', which is part of this source code package.
using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content.Pipeline;
using Microsoft.Xna.Framework.Content.Pipeline.Graphics;
using Microsoft.Xna.Framework.Graphics;
using NUnit.Framework;
using Microsoft.Xna.Framework.Content.Pipeline.Processors;
namespace MonoGame.Tests.ContentPipeline
{
class ModelProcessorTests
{
private NodeContent CreateBasicMesh()
{
var input = new NodeContent
{
Name = "Root",
Identity = new ContentIdentity("dummy", GetType().Name),
Transform = Matrix.CreateRotationZ(MathHelper.ToRadians(60)) *
Matrix.CreateRotationX(MathHelper.ToRadians(40)) *
Matrix.CreateRotationY(MathHelper.ToRadians(50)),
};
{
var mesh = new MeshContent()
{
Name = "Mesh1",
Transform = Matrix.Identity,
};
var geom = new GeometryContent()
{
Name = "Geom1",
};
mesh.Geometry.Add(geom);
input.Children.Add(mesh);
}
{
var mesh2 = new MeshContent()
{
Name = "Mesh2",
Transform = Matrix.Identity,
};
mesh2.Positions.Add(new Vector3(0, 0, 0));
mesh2.Positions.Add(new Vector3(1, 0, 0));
mesh2.Positions.Add(new Vector3(1, 1, 1));
var material = new BasicMaterialContent
{
Name = "Material1",
Alpha = 0.5f,
DiffuseColor = Color.Red.ToVector3(),
VertexColorEnabled = true,
};
var geom2 = new GeometryContent()
{
Name = "Geom2",
Material = material,
};
geom2.Vertices.Add(0);
geom2.Vertices.Add(1);
geom2.Vertices.Add(2);
geom2.Indices.Add(0);
geom2.Indices.Add(1);
geom2.Indices.Add(2);
mesh2.Geometry.Add(geom2);
input.Children.Add(mesh2);
}
return input;
}
[Test]
public void BasicMeshTests()
{
var input = CreateBasicMesh();
var processorContext = new TestProcessorContext(TargetPlatform.Windows, "dummy.xnb");
var processor = new ModelProcessor
{
RotationX = 10,
RotationY = 20,
RotationZ = 30
};
var output = processor.Process(input, processorContext);
// The transform the processor above is applying to the model.
var processorXform = Matrix.CreateRotationZ(MathHelper.ToRadians(30))*
Matrix.CreateRotationX(MathHelper.ToRadians(10))*
Matrix.CreateRotationY(MathHelper.ToRadians(20));
// Test some basics.
Assert.NotNull(output);
Assert.NotNull(output.Meshes);
Assert.AreEqual(2, output.Meshes.Count);
Assert.NotNull(output.Bones);
Assert.AreEqual(3, output.Bones.Count);
Assert.NotNull(output.Root);
Assert.AreEqual(output.Root, output.Bones[0]);
// Stuff to make the tests below cleaner.
var inputMesh1 = input.Children[0] as MeshContent;
Assert.NotNull(inputMesh1);
var inputMesh2 = input.Children[1] as MeshContent;
Assert.NotNull(inputMesh2);
// Test the bones.
Assert.AreEqual("Root", output.Bones[0].Name);
Assert.That(input.Transform, Is.EqualTo(output.Bones[0].Transform).Using(MatrixComparer.Epsilon));
Assert.AreEqual("Mesh1", output.Bones[1].Name);
Assert.That(Matrix.Identity, Is.EqualTo(output.Bones[1].Transform).Using(MatrixComparer.Epsilon));
Assert.AreEqual("Mesh2", output.Bones[2].Name);
Assert.That(Matrix.Identity, Is.EqualTo(output.Bones[2].Transform).Using(MatrixComparer.Epsilon));
// Test the first mesh.
{
var mesh = output.Meshes[0];
Assert.AreEqual("Mesh1", mesh.Name);
Assert.AreEqual(output.Bones[1], mesh.ParentBone);
Assert.AreEqual(inputMesh1, mesh.SourceMesh);
Assert.AreEqual(new BoundingSphere(Vector3.Zero, 0), mesh.BoundingSphere);
Assert.NotNull(mesh.MeshParts);
Assert.AreEqual(1, mesh.MeshParts.Count);
var part = mesh.MeshParts[0];
Assert.NotNull(part);
Assert.IsNull(part.IndexBuffer);
Assert.IsNull(part.VertexBuffer);
Assert.AreEqual(0, part.NumVertices);
Assert.AreEqual(0, part.PrimitiveCount);
Assert.AreEqual(0, part.StartIndex);
Assert.AreEqual(0, part.VertexOffset);
Assert.IsAssignableFrom<BasicMaterialContent>(part.Material);
var material = part.Material as BasicMaterialContent;
Assert.NotNull(material);
Assert.IsNotEmpty(material.OpaqueData);
Assert.IsNull(material.Name);
Assert.IsNull(material.Identity);
Assert.IsNull(material.Alpha);
Assert.IsNull(material.DiffuseColor);
Assert.IsNull(material.EmissiveColor);
Assert.IsNull(material.SpecularColor);
Assert.IsNull(material.SpecularPower);
Assert.IsNull(material.Texture);
Assert.IsEmpty(material.Textures);
Assert.IsTrue(material.OpaqueData.ContainsKey("VertexColorEnabled"));
Assert.IsNotNull(material.VertexColorEnabled);
Assert.IsFalse(material.VertexColorEnabled.Value);
}
// Test the second mesh.
{
var mesh = output.Meshes[1];
Assert.AreEqual("Mesh2", mesh.Name);
Assert.AreEqual(output.Bones[2], mesh.ParentBone);
Assert.AreEqual(inputMesh2, mesh.SourceMesh);
Assert.That(new BoundingSphere(new Vector3(0.3809527f, 0.5858122f, 0.5115654f), 0.8660253f),
Is.EqualTo(mesh.BoundingSphere).Using(BoundingSphereComparer.Epsilon));
Assert.NotNull(mesh.MeshParts);
Assert.AreEqual(1, mesh.MeshParts.Count);
var part = mesh.MeshParts[0];
Assert.NotNull(part);
Assert.AreEqual(1, part.PrimitiveCount);
Assert.AreEqual(0, part.StartIndex);
Assert.AreEqual(0, part.VertexOffset);
Assert.AreEqual(3, part.NumVertices);
Assert.NotNull(part.IndexBuffer);
Assert.AreEqual(3, part.IndexBuffer.Count);
Assert.AreEqual(0, part.IndexBuffer[0]);
Assert.AreEqual(1, part.IndexBuffer[1]);
Assert.AreEqual(2, part.IndexBuffer[2]);
Assert.NotNull(part.VertexBuffer);
Assert.NotNull(part.VertexBuffer.VertexData);
var vertexData = part.VertexBuffer.VertexData;
Assert.AreEqual(36, vertexData.Length);
var positionArray = ArrayUtil.ConvertTo<Vector3>(vertexData);
Assert.AreEqual(3, positionArray.Length);
Assert.AreEqual(Vector3.Transform(new Vector3(0, 0, 0), processorXform), positionArray[0]);
Assert.AreEqual(Vector3.Transform(new Vector3(1, 0, 0), processorXform), positionArray[1]);
Assert.AreEqual(Vector3.Transform(new Vector3(1, 1, 1), processorXform), positionArray[2]);
Assert.IsAssignableFrom<BasicMaterialContent>(part.Material);
var material = part.Material as BasicMaterialContent;
Assert.NotNull(material);
Assert.IsNotEmpty(material.OpaqueData);
Assert.AreEqual("Material1", material.Name);
Assert.IsNull(material.Identity);
Assert.IsTrue(material.OpaqueData.ContainsKey("Alpha"));
Assert.NotNull(material.Alpha);
Assert.AreEqual(0.5f, material.Alpha.Value);
Assert.IsTrue(material.OpaqueData.ContainsKey("DiffuseColor"));
Assert.NotNull(material.DiffuseColor);
Assert.AreEqual(Color.Red.ToVector3(), material.DiffuseColor.Value);
Assert.IsNull(material.EmissiveColor);
Assert.IsNull(material.SpecularColor);
Assert.IsNull(material.SpecularPower);
Assert.IsNull(material.Texture);
Assert.IsEmpty(material.Textures);
Assert.IsTrue(material.OpaqueData.ContainsKey("VertexColorEnabled"));
Assert.IsNotNull(material.VertexColorEnabled);
Assert.IsFalse(material.VertexColorEnabled.Value);
}
}
[Test]
public void DefaultEffectTest()
{
NodeContent input;
{
input = new NodeContent();
var mesh = new MeshContent()
{
Name = "Mesh1"
};
mesh.Positions.Add(new Vector3(0, 0, 0));
mesh.Positions.Add(new Vector3(1, 0, 0));
mesh.Positions.Add(new Vector3(1, 1, 1));
var geom = new GeometryContent();
geom.Vertices.Add(0);
geom.Vertices.Add(1);
geom.Vertices.Add(2);
geom.Indices.Add(0);
geom.Indices.Add(1);
geom.Indices.Add(2);
geom.Vertices.Channels.Add(VertexChannelNames.TextureCoordinate(0), new[]
{
new Vector2(0,0),
new Vector2(1,0),
new Vector2(1,1),
});
var wieghts = new BoneWeightCollection();
wieghts.Add(new BoneWeight("bone1", 0.5f));
geom.Vertices.Channels.Add(VertexChannelNames.Weights(0), new[]
{
wieghts,
wieghts,
wieghts
});
mesh.Geometry.Add(geom);
input.Children.Add(mesh);
var bone1 = new BoneContent { Name = "bone1", Transform = Matrix.CreateTranslation(0,1,0) };
input.Children.Add(bone1);
var anim = new AnimationContent()
{
Name = "anim1",
Duration = TimeSpan.Zero
};
input.Animations.Add(anim.Name, anim);
}
var processorContext = new TestProcessorContext(TargetPlatform.Windows, "dummy.xnb");
var processor = new ModelProcessor
{
DefaultEffect = MaterialProcessorDefaultEffect.SkinnedEffect,
};
var output = processor.Process(input, processorContext);
// TODO: Not sure why, but XNA always returns a BasicMaterialContent
// even when we specify SkinnedEffect as the default. We need to fix
// the test first before we can enable the assert here.
//Assert.IsInstanceOf(typeof(SkinnedMaterialContent), output.Meshes[0].MeshParts[0].Material);
}
}
}