forked from MonoGame/MonoGame
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FbxImporter.cs
227 lines (192 loc) · 8.36 KB
/
FbxImporter.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
// 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 System.Collections.Generic;
using System.Linq;
using Assimp;
using Assimp.Configs;
using Microsoft.Xna.Framework.Content.Pipeline.Graphics;
namespace Microsoft.Xna.Framework.Content.Pipeline
{
/// <summary>
/// Provides methods for reading AutoDesk (.fbx) files for use in the Content Pipeline.
/// </summary>
/// <remarks>
/// Since OpenAssetImporter supports lots of formats, there's little that stands in the
/// way of adding more file extensions to the importer attribute and suporting more.
/// </remarks>
[ContentImporter(".fbx", DisplayName = "Fbx Importer - MonoGame", DefaultProcessor = "ModelProcessor")]
public class FbxImporter : ContentImporter<NodeContent>
{
public override NodeContent Import(string filename, ContentImporterContext context)
{
var identity = new ContentIdentity(filename, GetType().Name);
var importer = new AssimpImporter();
importer.AttachLogStream(new LogStream((msg, userData) => context.Logger.LogMessage(msg)));
var scene = importer.ImportFile(filename,
PostProcessSteps.FlipUVs | // So far appears necessary
PostProcessSteps.JoinIdenticalVertices |
PostProcessSteps.Triangulate |
PostProcessSteps.SortByPrimitiveType |
PostProcessSteps.FindInvalidData
);
var rootNode = new NodeContent
{
Name = scene.RootNode.Name,
Identity = identity,
Transform = ToXna(scene.RootNode.Transform)
};
// TODO: Materials
var materials = new List<MaterialContent>();
foreach (var sceneMaterial in scene.Materials)
{
var diffuse = sceneMaterial.GetTexture(TextureType.Diffuse, 0);
materials.Add(new BasicMaterialContent()
{
Name = sceneMaterial.Name,
Identity = identity,
Texture = new ExternalReference<TextureContent>(diffuse.FilePath, identity)
});
}
// Meshes
var meshes = new Dictionary<Mesh, MeshContent>();
foreach (var sceneMesh in scene.Meshes)
{
if (!sceneMesh.HasVertices)
continue;
var mesh = new MeshContent
{
Name = sceneMesh.Name
};
// Position vertices are shared at the mesh level
foreach (var vert in sceneMesh.Vertices)
mesh.Positions.Add(new Vector3(vert.X, vert.Y, vert.Z));
var geom = new GeometryContent
{
Name = string.Empty,
//Material = materials[sceneMesh.MaterialIndex]
};
// Geometry vertices reference 1:1 with the MeshContent parent,
// no indirection is necessary.
geom.Vertices.Positions.AddRange(mesh.Positions);
geom.Vertices.AddRange(Enumerable.Range(0, sceneMesh.VertexCount));
geom.Indices.AddRange(sceneMesh.GetIntIndices());
// Individual channels go here
if (sceneMesh.HasNormals)
geom.Vertices.Channels.Add(VertexChannelNames.Normal(), ToXna(sceneMesh.Normals));
for (var i = 0; i < sceneMesh.TextureCoordsChannelCount; i++)
geom.Vertices.Channels.Add(VertexChannelNames.TextureCoordinate(i),
ToXnaVector2(sceneMesh.GetTextureCoords(i)));
mesh.Geometry.Add(geom);
rootNode.Children.Add(mesh);
meshes.Add(sceneMesh, mesh);
}
// Bones
var bones = new Dictionary<Node, BoneContent>();
var hierarchyNodes = scene.RootNode.Children.SelectDeep(n => n.Children).ToList();
foreach (var node in hierarchyNodes)
{
var bone = new BoneContent
{
Name = node.Name,
Transform = Matrix.Transpose(ToXna(node.Transform))
};
if (node.Parent == scene.RootNode)
rootNode.Children.Add(bone);
else
{
var parent = bones[node.Parent];
parent.Children.Add(bone);
}
// Copy the bone's name to the MeshContent - this appears to be
// the way it comes out of XNA's FBXImporter.
foreach (var meshIndex in node.MeshIndices)
meshes[scene.Meshes[meshIndex]].Name = node.Name;
bones.Add(node, bone);
}
return rootNode;
}
public static Matrix ToXna(Matrix4x4 matrix)
{
var result = Matrix.Identity;
result.M11 = matrix.A1;
result.M12 = matrix.A2;
result.M13 = matrix.A3;
result.M14 = matrix.A4;
result.M21 = matrix.B1;
result.M22 = matrix.B2;
result.M23 = matrix.B3;
result.M24 = matrix.B4;
result.M31 = matrix.C1;
result.M32 = matrix.C2;
result.M33 = matrix.C3;
result.M34 = matrix.C4;
result.M41 = matrix.D1;
result.M42 = matrix.D2;
result.M43 = matrix.D3;
result.M44 = matrix.D4;
return result;
}
public static Vector2[] ToXna(Vector2D[] vectors)
{
var result = new Vector2[vectors.Length];
for (var i = 0; i < vectors.Length; i++)
result[i] = new Vector2(vectors[i].X, vectors[i].Y);
return result;
}
public static Vector2[] ToXnaVector2(Vector3D[] vectors)
{
var result = new Vector2[vectors.Length];
for (var i = 0; i < vectors.Length; i++)
result[i] = new Vector2(vectors[i].X, 1 - vectors[i].Y);
return result;
}
public static Vector3[] ToXna(Vector3D[] vectors)
{
var result = new Vector3[vectors.Length];
for (var i = 0; i < vectors.Length; i++)
result[i] = new Vector3(vectors[i].X, vectors[i].Y, vectors[i].Z);
return result;
}
}
public static class EnumerableExtensions
{
/// <summary>
/// Returns each element of a tree structure in heriarchical order.
/// </summary>
/// <typeparam name="T">The enumerated type.</typeparam>
/// <param name="source">The enumeration to traverse.</param>
/// <param name="selector">A function which returns the children of the element.</param>
/// <returns>An IEnumerable whose elements are in tree structure heriarchical order.</returns>
public static IEnumerable<T> SelectDeep<T>(this IEnumerable<T> source, Func<T, IEnumerable<T>> selector)
{
var stack = new Stack<T>(source.Reverse());
while (stack.Count > 0)
{
// Return the next item on the stack.
var item = stack.Pop();
yield return item;
// Get the children from this item.
var children = selector(item);
// If we have no children then skip it.
if (children == null)
continue;
// We're using a stack, so we need to push the
// children on in reverse to get the correct order.
foreach (var child in children.Reverse())
stack.Push(child);
}
}
/// <summary>
/// Returns an enumerable from a single element.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="item"></param>
/// <returns></returns>
public static IEnumerable<T> AsEnumerable<T>(this T item)
{
yield return item;
}
}
}