我正在使用Assimp .NET库从我的C#应用程序中的Blender导入collada文件(.dae)。问题是多次导入了一些顶点。
以下是使用文件路径到我的collada文件并导入Assimp网格的代码:
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;
}
您可以排除除位置坐标以外的大部分组件。然后我分别使用“PostProcessSteps.RemoveComponent”和“PostProcessSteps.JoinIdenticalVertices”来连接相同的顶点。
ProcessNode() - 递归加载每个网格:
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()只是将所有顶点和索引放在一个单独的列表中:
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;
}
然而,这适用于大多数网格,但并非适用于所有网格。例如,我有以下锥体:
并且所选顶点(即X:0.84,Y:-0.55557,Z:-1.0)在顶点列表中存储三次。我检查了collada文件,这个顶点肯定只存在一次。
答案 0 :(得分:1)
如果你有一些具有不同纹理坐标的顶点(如果你有纹理图册等),那些顶点是重复的。也许你正面临这种特殊情况?
答案 1 :(得分:-1)
我进一步研究了这个问题并找到了以下解决方案:在Visual Studio 2015中,我将“Windows Application”而不是“Class Library”设置为输出类型。突然,重复的顶点消失了。不幸的是,我无法分辨出应用程序类型会影响Assimp的行为的原因。