From 8eaa0f6747053c8c040249a1dc6f40d7fcca098e Mon Sep 17 00:00:00 2001 From: Jiajun <75730859+T-rvw@users.noreply.github.com> Date: Wed, 7 Feb 2024 18:50:34 +0800 Subject: [PATCH] correct vertex instance (#287) --- .../Consumers/FbxConsumer/FbxConsumerImpl.cpp | 2 +- .../Producers/FbxProducer/FbxProducerImpl.cpp | 1 - private/Scene/Mesh.cpp | 1 - private/Scene/MeshImpl.cpp | 3 - private/Scene/MeshImpl.h | 3 - public/Scene/APITypeTraits.inl | 2 - public/Scene/Mesh.h | 1 - public/Utilities/MeshUtils.hpp | 66 +++++++++---------- 8 files changed, 34 insertions(+), 45 deletions(-) diff --git a/private/Consumers/FbxConsumer/FbxConsumerImpl.cpp b/private/Consumers/FbxConsumer/FbxConsumerImpl.cpp index 9e3be3c..12b7528 100644 --- a/private/Consumers/FbxConsumer/FbxConsumerImpl.cpp +++ b/private/Consumers/FbxConsumer/FbxConsumerImpl.cpp @@ -215,7 +215,7 @@ void FbxConsumerImpl::ExportMesh(fbxsdk::FbxScene* pScene, fbxsdk::FbxNode* pNod } // Check if mesh surface attributes are using instance mapping. - uint32_t vertexInstanceIDCount = mesh.GetVertexIDToInstanceCount(); + uint32_t vertexInstanceIDCount = mesh.GetVertexInstanceToIDCount(); bool mappingSurfaceAttributes = vertexInstanceIDCount > 0U; auto mappingMode = mappingSurfaceAttributes ? fbxsdk::FbxGeometryElement::eByPolygonVertex : fbxsdk::FbxGeometryElement::eByControlPoint; // Actually, we should support normal/binormal/tangent mapping directly... diff --git a/private/Producers/FbxProducer/FbxProducerImpl.cpp b/private/Producers/FbxProducer/FbxProducerImpl.cpp index 42e1a63..3d389e8 100644 --- a/private/Producers/FbxProducer/FbxProducerImpl.cpp +++ b/private/Producers/FbxProducer/FbxProducerImpl.cpp @@ -1084,7 +1084,6 @@ cd::MeshID FbxProducerImpl::ImportMesh(const fbxsdk::FbxMesh* pFbxMesh, cd::Scen { uint32_t controlPointIndex = pFbxMesh->GetPolygonVertex(polygonIndex, polygonVertexIndex); uint32_t vertexInstanceID = polygonVertexBeginIndex + polygonVertexIndex; - mesh.SetVertexIDToInstance(controlPointIndex, vertexInstanceID); mesh.SetVertexInstanceToID(vertexInstanceID, controlPointIndex); polygon.push_back(vertexInstanceID); diff --git a/private/Scene/Mesh.cpp b/private/Scene/Mesh.cpp index 951e6ec..b0b1155 100644 --- a/private/Scene/Mesh.cpp +++ b/private/Scene/Mesh.cpp @@ -14,7 +14,6 @@ PIMPL_COMPLEX_TYPE_APIS(Mesh, AABB); PIMPL_COMPLEX_TYPE_APIS(Mesh, VertexFormat); PIMPL_VECTOR_TYPE_APIS(Mesh, VertexPosition); PIMPL_VECTOR_TYPE_APIS(Mesh, VertexInstanceToID); -PIMPL_VECTOR_TYPE_APIS(Mesh, VertexIDToInstance); PIMPL_VECTOR_TYPE_APIS(Mesh, VertexNormal); PIMPL_VECTOR_TYPE_APIS(Mesh, VertexTangent); PIMPL_VECTOR_TYPE_APIS(Mesh, VertexBiTangent); diff --git a/private/Scene/MeshImpl.cpp b/private/Scene/MeshImpl.cpp index afe72c1..d9f3220 100644 --- a/private/Scene/MeshImpl.cpp +++ b/private/Scene/MeshImpl.cpp @@ -158,7 +158,6 @@ void MeshImpl::FromHalfEdgeMesh(const HalfEdgeMesh& halfEdgeMesh, ConvertStrateg void MeshImpl::Init(uint32_t vertexCount) { SetVertexPositionCount(vertexCount); - SetVertexIDToInstanceCount(0U); SetVertexInstanceToIDCount(0U); InitVertexAttributes(vertexCount); @@ -173,7 +172,6 @@ void MeshImpl::Init(uint32_t vertexCount, uint32_t vertexInstanceCount) else { SetVertexPositionCount(vertexCount); - SetVertexIDToInstanceCount(vertexCount); SetVertexInstanceToIDCount(vertexInstanceCount); InitVertexAttributes(vertexInstanceCount); @@ -201,7 +199,6 @@ void MeshImpl::ShrinkToFit() { GetVertexPositions().shrink_to_fit(); GetVertexInstanceToIDs().shrink_to_fit(); - GetVertexIDToInstances().shrink_to_fit(); GetVertexNormals().shrink_to_fit(); GetVertexTangents().shrink_to_fit(); diff --git a/private/Scene/MeshImpl.h b/private/Scene/MeshImpl.h index 71b9c80..19e1c93 100644 --- a/private/Scene/MeshImpl.h +++ b/private/Scene/MeshImpl.h @@ -32,7 +32,6 @@ class MeshImpl final IMPLEMENT_COMPLEX_TYPE_APIS(Mesh, VertexFormat); IMPLEMENT_VECTOR_TYPE_APIS(Mesh, VertexPosition); IMPLEMENT_VECTOR_TYPE_APIS(Mesh, VertexInstanceToID); - IMPLEMENT_VECTOR_TYPE_APIS(Mesh, VertexIDToInstance); IMPLEMENT_VECTOR_TYPE_APIS(Mesh, VertexNormal); IMPLEMENT_VECTOR_TYPE_APIS(Mesh, VertexTangent); IMPLEMENT_VECTOR_TYPE_APIS(Mesh, VertexBiTangent); @@ -100,7 +99,6 @@ class MeshImpl final Init(vertexCount, vertexInstanceCount); inputArchive.ImportBuffer(GetVertexInstanceToIDs().data()); - inputArchive.ImportBuffer(GetVertexIDToInstances().data()); inputArchive.ImportBuffer(GetVertexPositions().data()); inputArchive.ImportBuffer(GetVertexNormals().data()); inputArchive.ImportBuffer(GetVertexTangents().data()); @@ -151,7 +149,6 @@ class MeshImpl final outputArchive.ExportBuffer(GetBlendShapeIDs().data(), GetBlendShapeIDs().size()); outputArchive.ExportBuffer(GetSkinIDs().data(), GetSkinIDs().size()); outputArchive.ExportBuffer(GetVertexInstanceToIDs().data(), GetVertexInstanceToIDs().size()); - outputArchive.ExportBuffer(GetVertexIDToInstances().data(), GetVertexIDToInstances().size()); outputArchive.ExportBuffer(GetVertexPositions().data(), GetVertexPositions().size()); outputArchive.ExportBuffer(GetVertexNormals().data(), GetVertexNormals().size()); outputArchive.ExportBuffer(GetVertexTangents().data(), GetVertexTangents().size()); diff --git a/public/Scene/APITypeTraits.inl b/public/Scene/APITypeTraits.inl index b6d746a..d96188c 100644 --- a/public/Scene/APITypeTraits.inl +++ b/public/Scene/APITypeTraits.inl @@ -160,9 +160,7 @@ struct MeshTypeTraits // Vector using VertexPosition = cd::Point; - using VertexInstanceToID = cd::VertexID; - using VertexIDToInstance = cd::VertexInstanceID; using VertexNormal = cd::Direction; using VertexTangent = cd::Direction; using VertexBiTangent = cd::Direction; diff --git a/public/Scene/Mesh.h b/public/Scene/Mesh.h index 52689a0..d974923 100644 --- a/public/Scene/Mesh.h +++ b/public/Scene/Mesh.h @@ -30,7 +30,6 @@ class CORE_API Mesh final EXPORT_COMPLEX_TYPE_APIS(Mesh, VertexFormat); EXPORT_VECTOR_TYPE_APIS(Mesh, VertexPosition); EXPORT_VECTOR_TYPE_APIS(Mesh, VertexInstanceToID); - EXPORT_VECTOR_TYPE_APIS(Mesh, VertexIDToInstance); EXPORT_VECTOR_TYPE_APIS(Mesh, VertexNormal); EXPORT_VECTOR_TYPE_APIS(Mesh, VertexTangent); EXPORT_VECTOR_TYPE_APIS(Mesh, VertexBiTangent); diff --git a/public/Utilities/MeshUtils.hpp b/public/Utilities/MeshUtils.hpp index afeca1a..b969422 100644 --- a/public/Utilities/MeshUtils.hpp +++ b/public/Utilities/MeshUtils.hpp @@ -18,9 +18,10 @@ static std::optional BuildVertexBufferForStaticMesh(const cd::Mesh const bool containsColor = requiredVertexFormat.Contains(cd::VertexAttributeType::Color); VertexBuffer vertexBuffer; - const uint32_t vertexCount = mesh.GetVertexCount(); + bool mappingSurfaceAttributes = mesh.GetVertexInstanceToIDCount() > 0U; + const uint32_t vertexInstanceCount = mappingSurfaceAttributes ? mesh.GetVertexInstanceToIDCount() : mesh.GetVertexCount(); const uint32_t vertexFormatStride = requiredVertexFormat.GetStride(); - vertexBuffer.resize(vertexCount * vertexFormatStride); + vertexBuffer.resize(vertexInstanceCount * vertexFormatStride); uint32_t vbDataSize = 0U; auto vbDataPtr = vertexBuffer.data(); @@ -30,49 +31,48 @@ static std::optional BuildVertexBufferForStaticMesh(const cd::Mesh vbDataSize += dataSize; }; - bool mappingSurfaceAttributes = mesh.GetVertexIDToInstanceCount() > 0U; - for (uint32_t vertexIndex = 0; vertexIndex < vertexCount; ++vertexIndex) + for (uint32_t vertexInstance = 0; vertexInstance < vertexInstanceCount; ++vertexInstance) { if (containsPosition) { - constexpr uint32_t dataSize = cd::Point::Size * sizeof(cd::Point::ValueType); - FillVertexBuffer(mesh.GetVertexPosition(vertexIndex).begin(), dataSize); - } + uint32_t vertexID = vertexInstance; + if (mappingSurfaceAttributes) + { + vertexID = mesh.GetVertexInstanceToID(vertexInstance).Data(); + } - cd::VertexInstanceID vertexInstanceID = vertexIndex; - if (mappingSurfaceAttributes) - { - vertexInstanceID = mesh.GetVertexIDToInstance(vertexIndex); + constexpr uint32_t dataSize = cd::Point::Size * sizeof(cd::Point::ValueType); + FillVertexBuffer(mesh.GetVertexPosition(vertexID).begin(), dataSize); } if (containsNormal) { constexpr uint32_t dataSize = cd::Direction::Size * sizeof(cd::Direction::ValueType); - FillVertexBuffer(mesh.GetVertexNormal(vertexInstanceID.Data()).begin(), dataSize); + FillVertexBuffer(mesh.GetVertexNormal(vertexInstance).begin(), dataSize); } if (containsTangent) { constexpr uint32_t dataSize = cd::Direction::Size * sizeof(cd::Direction::ValueType); - FillVertexBuffer(mesh.GetVertexTangent(vertexInstanceID.Data()).begin(), dataSize); + FillVertexBuffer(mesh.GetVertexTangent(vertexInstance).begin(), dataSize); } if (containsBiTangent) { constexpr uint32_t dataSize = cd::Direction::Size * sizeof(cd::Direction::ValueType); - FillVertexBuffer(mesh.GetVertexBiTangent(vertexInstanceID.Data()).begin(), dataSize); + FillVertexBuffer(mesh.GetVertexBiTangent(vertexInstance).begin(), dataSize); } if (containsUV) { constexpr uint32_t dataSize = cd::UV::Size * sizeof(cd::UV::ValueType); - FillVertexBuffer(mesh.GetVertexUV(0)[vertexInstanceID.Data()].begin(), dataSize); + FillVertexBuffer(mesh.GetVertexUV(0)[vertexInstance].begin(), dataSize); } if (containsColor) { constexpr uint32_t dataSize = cd::Color::Size * sizeof(cd::Color::ValueType); - FillVertexBuffer(mesh.GetVertexColor(0)[vertexInstanceID.Data()].begin(), dataSize); + FillVertexBuffer(mesh.GetVertexColor(0)[vertexInstance].begin(), dataSize); } } @@ -120,9 +120,10 @@ static std::optional BuildVertexBufferForSkeletalMesh(const cd::Me const bool containsColor = requiredVertexFormat.Contains(cd::VertexAttributeType::Color); VertexBuffer vertexBuffer; - const uint32_t vertexCount = mesh.GetVertexCount(); + bool mappingSurfaceAttributes = mesh.GetVertexInstanceToIDCount() > 0U; + const uint32_t vertexInstanceCount = mappingSurfaceAttributes ? mesh.GetVertexInstanceToIDCount() : mesh.GetVertexCount(); const uint32_t vertexFormatStride = requiredVertexFormat.GetStride(); - vertexBuffer.resize(vertexCount * vertexFormatStride); + vertexBuffer.resize(vertexInstanceCount * vertexFormatStride); uint32_t vbDataSize = 0U; auto vbDataPtr = vertexBuffer.data(); @@ -132,53 +133,52 @@ static std::optional BuildVertexBufferForSkeletalMesh(const cd::Me vbDataSize += dataSize; }; - bool mappingSurfaceAttributes = mesh.GetVertexIDToInstanceCount() > 0U; - for (uint32_t vertexIndex = 0; vertexIndex < vertexCount; ++vertexIndex) + for (uint32_t vertexInstance = 0; vertexInstance < vertexInstanceCount; ++vertexInstance) { - if (containsPosition) + uint32_t vertexID = vertexInstance; + if (mappingSurfaceAttributes) { - constexpr uint32_t dataSize = cd::Point::Size * sizeof(cd::Point::ValueType); - FillVertexBuffer(mesh.GetVertexPosition(vertexIndex).begin(), dataSize); + vertexID = mesh.GetVertexInstanceToID(vertexInstance).Data(); } - cd::VertexInstanceID vertexInstanceID = vertexIndex; - if (mappingSurfaceAttributes) + if (containsPosition) { - vertexInstanceID = mesh.GetVertexIDToInstance(vertexIndex); + constexpr uint32_t dataSize = cd::Point::Size * sizeof(cd::Point::ValueType); + FillVertexBuffer(mesh.GetVertexPosition(vertexID).begin(), dataSize); } if (containsNormal) { constexpr uint32_t dataSize = cd::Direction::Size * sizeof(cd::Direction::ValueType); - FillVertexBuffer(mesh.GetVertexNormal(vertexInstanceID.Data()).begin(), dataSize); + FillVertexBuffer(mesh.GetVertexNormal(vertexInstance).begin(), dataSize); } if (containsTangent) { constexpr uint32_t dataSize = cd::Direction::Size * sizeof(cd::Direction::ValueType); - FillVertexBuffer(mesh.GetVertexTangent(vertexInstanceID.Data()).begin(), dataSize); + FillVertexBuffer(mesh.GetVertexTangent(vertexInstance).begin(), dataSize); } if (containsBiTangent) { constexpr uint32_t dataSize = cd::Direction::Size * sizeof(cd::Direction::ValueType); - FillVertexBuffer(mesh.GetVertexBiTangent(vertexInstanceID.Data()).begin(), dataSize); + FillVertexBuffer(mesh.GetVertexBiTangent(vertexInstance).begin(), dataSize); } if (containsUV) { constexpr uint32_t dataSize = cd::UV::Size * sizeof(cd::UV::ValueType); - FillVertexBuffer(mesh.GetVertexUV(0)[vertexInstanceID.Data()].begin(), dataSize); + FillVertexBuffer(mesh.GetVertexUV(0)[vertexInstance].begin(), dataSize); } if (containsColor) { constexpr uint32_t dataSize = cd::Color::Size * sizeof(cd::Color::ValueType); - FillVertexBuffer(mesh.GetVertexColor(0)[vertexInstanceID.Data()].begin(), dataSize); + FillVertexBuffer(mesh.GetVertexColor(0)[vertexInstance].begin(), dataSize); } - auto& vertexBoneNameArray = skin.GetVertexBoneNameArray(vertexIndex); - auto& vertexBoneWeightArray = skin.GetVertexBoneWeightArray(vertexIndex); + auto& vertexBoneNameArray = skin.GetVertexBoneNameArray(vertexID); + auto& vertexBoneWeightArray = skin.GetVertexBoneWeightArray(vertexID); for (uint32_t vertexInfluenceIndex = 0U; vertexInfluenceIndex < vertexMaxInfluenceCount; ++vertexInfluenceIndex) { if (vertexInfluenceIndex < vertexBoneNameArray.size())