我正在使用Assimp .NET庫在C#應用程序中從Blender導入collada文件(.dae)。問題是幾個頂點被多次導入。Assimp duplicate Vertices
下面是一個使用文件路徑來我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;
}
正如你可以CEE我排除大部分組件,除了位置座標。然後,我分別使用「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文件,這個頂點絕對只存在一次。
這個頂點必須在文件中多次引用,也許我可能值得嘗試查明是否是這種情況,以及哪個屬性是重複的。另外,你有沒有嘗試導出到不同的擴展名,看看它是否總是一樣? (如使用obj和3ds爲例) – Zouch