From 7c1f6475a3f276aeefe4114539d084b4ad303229 Mon Sep 17 00:00:00 2001 From: T-rvw <429601557@qq.com> Date: Mon, 5 Feb 2024 17:17:19 +0800 Subject: [PATCH] fix fbx mesh export --- .../ProgressiveMesh/GenericToPMToFbx/Main.cpp | 20 +++++++- .../Consumers/FbxConsumer/FbxConsumerImpl.cpp | 47 +++++++++---------- 2 files changed, 40 insertions(+), 27 deletions(-) diff --git a/examples/Prototype/ProgressiveMesh/GenericToPMToFbx/Main.cpp b/examples/Prototype/ProgressiveMesh/GenericToPMToFbx/Main.cpp index a9aefc5..358e651 100644 --- a/examples/Prototype/ProgressiveMesh/GenericToPMToFbx/Main.cpp +++ b/examples/Prototype/ProgressiveMesh/GenericToPMToFbx/Main.cpp @@ -33,6 +33,16 @@ int main(int argc, char** argv) } // Processing + std::map mapMeshToNodeId; + for (uint32_t nodeIndex = 0U; nodeIndex < pSceneDatabase->GetNodeCount(); ++nodeIndex) + { + const auto& node = pSceneDatabase->GetNode(nodeIndex); + for (uint32_t meshIndex = 0U; meshIndex < node.GetMeshIDCount(); ++meshIndex) + { + mapMeshToNodeId[node.GetMeshID(meshIndex)] = nodeIndex; + } + } + uint32_t meshCount = pSceneDatabase->GetMeshCount(); for (uint32_t meshIndex = 0U; meshIndex < meshCount; ++meshIndex) { @@ -71,9 +81,15 @@ int main(int argc, char** argv) pm.InitBoundary(intersection); } - auto lodMesh = pm.GenerateLodMesh(0.1f, 2000, &mesh); + auto lodMesh = pm.GenerateLodMesh(0.5f, 3000, &mesh); lodMesh.SetName(std::format("{}_reduced", mesh.GetName()).c_str()); - lodMesh.SetID(pSceneDatabase->GetMeshCount()); + cd::MeshID lodMeshID(pSceneDatabase->GetMeshCount()); + lodMesh.SetID(lodMeshID); + lodMesh.AddMaterialID(cd::MaterialID::InvalidID); + + cd::NodeID nodeID = mapMeshToNodeId[mesh.GetID()]; + pSceneDatabase->GetNode(nodeID.Data()).AddMeshID(lodMeshID); + pSceneDatabase->AddMesh(cd::MoveTemp(lodMesh)); } diff --git a/private/Consumers/FbxConsumer/FbxConsumerImpl.cpp b/private/Consumers/FbxConsumer/FbxConsumerImpl.cpp index 2fd5fed..9e3be3c 100644 --- a/private/Consumers/FbxConsumer/FbxConsumerImpl.cpp +++ b/private/Consumers/FbxConsumer/FbxConsumerImpl.cpp @@ -290,36 +290,33 @@ void FbxConsumerImpl::ExportMesh(fbxsdk::FbxScene* pScene, fbxsdk::FbxNode* pNod } } - if (mappingSurfaceAttributes) + // If using vertex instance, then init vertex index to attribute index. + for (const auto& polygonGroup : mesh.GetPolygonGroups()) { - // If using vertex instance, then init vertex index to attribute index. - for (const auto& polygonGroup : mesh.GetPolygonGroups()) + for (const auto& polygon : polygonGroup) { - for (const auto& polygon : polygonGroup) + for (cd::VertexID instanceID : polygon) { - for (cd::VertexID instanceID : polygon) - { - cd::VertexID vertexID = mesh.GetVertexInstanceToID(instanceID.Data()); + cd::VertexID vertexID = mappingSurfaceAttributes ? mesh.GetVertexInstanceToID(instanceID.Data()) : instanceID; - if (pNormalElement) - { - pNormalElement->GetIndexArray().SetAt(vertexID.Data(), instanceID.Data()); - } + if (pNormalElement) + { + pNormalElement->GetIndexArray().SetAt(vertexID.Data(), instanceID.Data()); + } - if (pBinormalElement) - { - pBinormalElement->GetIndexArray().SetAt(vertexID.Data(), instanceID.Data()); - } - - if (pTangentElement) - { - pTangentElement->GetIndexArray().SetAt(vertexID.Data(), instanceID.Data()); - } - - if (pAlbedoUVElement) - { - pAlbedoUVElement->GetIndexArray().SetAt(vertexID.Data(), instanceID.Data()); - } + if (pBinormalElement) + { + pBinormalElement->GetIndexArray().SetAt(vertexID.Data(), instanceID.Data()); + } + + if (pTangentElement) + { + pTangentElement->GetIndexArray().SetAt(vertexID.Data(), instanceID.Data()); + } + + if (pAlbedoUVElement) + { + pAlbedoUVElement->GetIndexArray().SetAt(vertexID.Data(), instanceID.Data()); } } }