-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAssimpImporter.cs
More file actions
287 lines (211 loc) · 8.86 KB
/
AssimpImporter.cs
File metadata and controls
287 lines (211 loc) · 8.86 KB
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
286
287
using Assimp;
using StbiSharp;
namespace tmdl_utility;
public class AssimpImporter
{
public static ModelUtility.Scene LoadAssimp(UtilityInitInfo info)
{
var importer = new AssimpContext();
//importer.SetConfig();
var maxBonesPerVertex = 4;
var scene = importer.ImportFile(info.Source,
PostProcessSteps.GenerateUVCoords |
PostProcessSteps.FlipUVs |
PostProcessSteps.Triangulate);
Directory.CreateDirectory(info.Dest);
//importer.ExportFile(scene, info.Dest + Path.GetFileNameWithoutExtension(info.Source) + ".fbx", "fbx");
var mscene = new ModelUtility.Scene();
mscene.name = scene.Name;
mscene.rootNode = new ModelUtility.Node();
mscene.rootNode.name = Path.GetFileNameWithoutExtension(info.Source);
mscene.rootNode.AddChild(ProcessAiNode(scene.RootNode));
#region Assimp Models
var boneDict = new Dictionary<string, BoneInfo>();
var model = new ModelUtility.Model();
model.Meshes = new ModelUtility.Mesh[scene.MeshCount];
model.Name = "TMDL_" + scene.Name;
var modelNode = new ModelUtility.Node(model.Name);
modelNode.SetParent(mscene.rootNode);
foreach (var sceneMesh in scene.Meshes)
{
var mesh = new ModelUtility.Mesh();
mesh.Vertices = new ModelUtility.Vec3[sceneMesh.VertexCount];
mesh.Normals = new ModelUtility.Vec3[sceneMesh.VertexCount];
mesh.UV0 = new ModelUtility.Vec2[sceneMesh.VertexCount];
mesh.VertexWeights = new float[sceneMesh.VertexCount][];
mesh.BoneIDs = new int[sceneMesh.VertexCount][];
mesh.Name = sceneMesh.Name;
mesh.Indices = new ushort[sceneMesh.FaceCount * 3];
var boneIndices = new List<int>[sceneMesh.VertexCount];
var boneWeights = new List<float>[sceneMesh.VertexCount];
for (var i = 0; i < sceneMesh.VertexCount; i++)
{
boneIndices[i] = new List<int> { -1, -1, -1, -1 };
boneWeights[i] = new List<float> { 0, 0, 0, 0 };
}
foreach (var bone in sceneMesh.Bones)
{
var boneName = bone.Name;
var boneId = -1;
if (!boneDict.ContainsKey(boneName))
{
var boneInfo = new BoneInfo();
boneInfo.id = boneDict.Count;
boneDict.Add(boneName, boneInfo);
boneId = boneInfo.id;
}
else
{
boneId = boneDict[boneName].id;
}
foreach (var (vertexId, weight) in bone.VertexWeights)
{
if (boneIndices[vertexId].Count > 4)
continue;
if (vertexId == 40)
Console.Write("");
if (weight <= 0.01)
continue;
for (var i = 0; i < 4; ++i)
if (boneIndices[vertexId][i] < 0)
{
boneIndices[vertexId][i] = boneId;
boneWeights[vertexId][i] = weight;
break;
}
}
}
/*
for (var i = 0; i < boneWeights.Length; i++)
{
var weight = boneWeights[i];
float totalWeight = weight.Sum();
for (int j = 0; j < weight.Count; j++)
{
boneWeights[i][j] /= totalWeight;
}
}
*/
for (var i = 0; i < sceneMesh.Vertices.Count; i++)
{
var vtx = sceneMesh.Vertices[i];
var nrm = sceneMesh.Normals[i];
var uv0 = sceneMesh.TextureCoordinateChannels[0][i];
mesh.Vertices[i] = new ModelUtility.Vec3(vtx.X, vtx.Y, vtx.Z);
mesh.Normals[i] = new ModelUtility.Vec3(nrm.X, nrm.Y, nrm.Z);
mesh.UV0[i] = new ModelUtility.Vec2(uv0.X, uv0.Y);
mesh.BoneIDs[i] = boneIndices[i].ToArray();
mesh.VertexWeights[i] = boneWeights[i].ToArray();
}
var iidx = 0;
foreach (var sceneMeshFace in sceneMesh.Faces)
foreach (var index in sceneMeshFace.Indices)
{
mesh.Indices[iidx] = (ushort)index;
iidx++;
}
mesh.MaterialIndex = sceneMesh.MaterialIndex;
model.Meshes[scene.Meshes.IndexOf(sceneMesh)] = mesh;
var meshNode = new ModelUtility.Node(mesh.Name);
meshNode.Meshes.Add(scene.Meshes.IndexOf(sceneMesh));
meshNode.SetParent(modelNode);
}
model.Skeleton = new ModelUtility.Skeleton(mscene.GetNode(boneDict.Keys.ToList()[0]));
foreach (var sceneMesh in scene.Meshes)
foreach (var bone in sceneMesh.Bones)
{
var b = model.Skeleton.GetBone(bone.Name);
if (b != null) b.offsetMatrix = bone.OffsetMatrix;
}
foreach (var skeletonBone in model.Skeleton.bones) mscene.RemoveNode(skeletonBone.name);
var armatureNode = mscene.GetNode("Armature");
armatureNode.IsBone = true;
var snode = model.Skeleton.ToNode();
armatureNode.AddChild(snode);
armatureNode.SetParent(modelNode);
model.Textures = new ModelUtility.Texture[scene.TextureCount];
for (var i = 0; i < scene.Textures.Count; i++)
{
var tex = scene.Textures[i];
var texture = new ModelUtility.Texture();
texture.width = tex.Width;
texture.height = tex.Height;
//texture.name = tex.Filename;
if (tex.IsCompressed)
{
using (var memoryStream = new MemoryStream())
{
memoryStream.Write(tex.CompressedData);
var image = Stbi.LoadFromMemory(memoryStream, 4);
texture.width = image.Width;
texture.height = image.Height;
texture.data = image.Data.ToArray();
texture.channelCount = image.NumChannels;
// Use image.Width, image.Height,
// image.NumChannels, and image.Data.
}
}
else
{
texture.channelCount = 4;
var dat = new List<byte>();
for (var x = 0; x < texture.width; x++)
for (var y = 0; y < texture.height; y++)
{
var texel = tex.NonCompressedData[x * texture.width + y];
dat.AddRange(new List<byte> { texel.R, texel.G, texel.B, texel.A }.AsReadOnly());
}
texture.data = dat.ToArray();
}
model.Textures[i] = texture;
}
model.Materials = new ModelUtility.Material[scene.MaterialCount];
foreach (var sceneMaterial in scene.Materials)
{
var material = new ModelUtility.Material();
if (model.Textures.Length > 0)
{
var texs = sceneMaterial.GetAllMaterialTextures();
foreach (var allMaterialTexture in texs)
material.Textures.Add(allMaterialTexture.TextureType.ToString(),
model.Textures[allMaterialTexture.TextureIndex].name);
}
material.name = sceneMaterial.Name;
model.Materials[scene.Materials.IndexOf(sceneMaterial)] = material;
}
model.Animations = new ModelUtility.Animation[scene.AnimationCount];
foreach (var anim in scene.Animations)
{
var animation = new ModelUtility.Animation(anim);
animation.ApplySkeleton(model.Skeleton);
model.Animations[scene.Animations.IndexOf(anim)] = animation;
}
mscene.models.Add(model);
#endregion
return mscene;
}
public static ModelUtility.Node ProcessAiNode(Node ai, int count = -1, bool isBone = false)
{
var node = new ModelUtility.Node();
node.name = ai.Name;
node.id = count++;
node.Meshes = ai.MeshIndices;
ai.Transform.AI_DecomposeMatrix(out var translation, out var rotation, out var scale);
node.Position = new ModelUtility.Vec3(translation);
node.Rotation = new ModelUtility.Vec4(rotation);
node.Scale = new ModelUtility.Vec3(scale);
foreach (var child in ai.Children)
{
var ib = isBone;
if (ai.Name == "Armature")
ib = true;
var n = ProcessAiNode(child, count, ib);
n.SetParent(node);
}
return node;
}
public class BoneInfo
{
public int id;
}
}