I'm using the Assimp .NET library to import collada files (.dae) from Blender in my C# application. The problem is that a few vertices are imported multiple times.
Here is the code that uses a filepath to my collada-file and imports the Assimp mesh:
public List<Mesh> GenerateMeshes(String path)
{
AssimpImporter importer = new AssimpImporter();
importer.SetConfig(new RemoveComponentConfig(ExcludeComponent.Animations | ExcludeComponent.Boneweights | ExcludeComponent.Cameras | ExcludeComponent.Colors
| ExcludeComponent.Lights | ExcludeComponent.Materials | ExcludeComponent.Normals |
ExcludeComponent.TangentBasis | ExcludeComponent.TexCoords | ExcludeComponent.Textures));
var scene = importer.ImportFile(path, PostProcessSteps.RemoveComponent | PostProcessSteps.JoinIdenticalVertices);
ProcessNode(scene.RootNode, scene);
return meshes;
}
As you can cee I exclude most of the components except the position coordinates. Then I use "PostProcessSteps.RemoveComponent" and "PostProcessSteps.JoinIdenticalVertices", respectively to join identical vertices.
The ProcessNode() - loads each mesh recursively:
private void ProcessNode(Node node, Assimp.Scene scene)
{
for (int i = 0; i < node.MeshCount; i++)
{
// The node object only contains indices to index the actual objects in the scene.
// The scene contains all the data, node is just to keep stuff organized (like relations between nodes).
Assimp.Mesh m = scene.Meshes[node.MeshIndices[i]];
meshes.Add(ProcessMesh(m, node));
}
// After we've processed all of the meshes (if any) we then recursively process each of the children nodes
if (node.HasChildren)
{
for (int i = 0; i < node.Children.Length; i++)
{
ProcessNode(node.Children[i], scene);
}
}
}
ProcessMesh() does nothing more than put all vertices and indices in a separate list:
private Mesh ProcessMesh(Assimp.Mesh mesh, Node node)
{
// Data to fill
List<Vector3d> vertices = new List<Vector3d>();
List<int> indices = new List<int>();
for (var i = 0; i < mesh.VertexCount; i++)
{
Vector3d vertex = new Vector3d(mesh.Vertices[i].X, mesh.Vertices[i].Y, mesh.Vertices[i].Z); // Positions
vertices.Add(vertex);
}
// Now walk through each of the mesh's faces and retrieve the corresponding vertex indices.
for (int i = 0; i < mesh.FaceCount; i++)
{
Face face = mesh.Faces[i];
// Retrieve all indices of the face and store them in the indices vector
for (int j = 0; j < face.IndexCount; j++)
indices.Add((int)face.Indices[j]);
}
//node.Transform
Mesh geoObject = new Mesh(vertices.ToArray(), indices.ToArray(), null, null);
geoObject.ModelMatrix = Convert(node.Transform);
return geoObject;
}
Nevertheless, this works for most meshes, but not for all. For example, I have the following cone:
and the selected vertex (i.e. X: 0.84, Y:-0.55557, Z: -1.0) is stored three times in the vertex list. I checked the collada file and this vertex definitely exists only once.
I further investigated this issue and found the following solution: In Visual Studio 2015 I set "Windows Application" instead of "Class Library" as Output Type. Suddenly the duplicated vertices vanished. Unfortunately, I cannot tell the reason why the application type influences Assimp's behaviour.