Skip to content

Commit

Permalink
correct vertex instance (#287)
Browse files Browse the repository at this point in the history
  • Loading branch information
T-rvw authored Feb 7, 2024
1 parent 554888b commit 8eaa0f6
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 45 deletions.
2 changes: 1 addition & 1 deletion private/Consumers/FbxConsumer/FbxConsumerImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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...
Expand Down
1 change: 0 additions & 1 deletion private/Producers/FbxProducer/FbxProducerImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
1 change: 0 additions & 1 deletion private/Scene/Mesh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
3 changes: 0 additions & 3 deletions private/Scene/MeshImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -173,7 +172,6 @@ void MeshImpl::Init(uint32_t vertexCount, uint32_t vertexInstanceCount)
else
{
SetVertexPositionCount(vertexCount);
SetVertexIDToInstanceCount(vertexCount);
SetVertexInstanceToIDCount(vertexInstanceCount);

InitVertexAttributes(vertexInstanceCount);
Expand Down Expand Up @@ -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();
Expand Down
3 changes: 0 additions & 3 deletions private/Scene/MeshImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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());
Expand Down
2 changes: 0 additions & 2 deletions public/Scene/APITypeTraits.inl
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 0 additions & 1 deletion public/Scene/Mesh.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
66 changes: 33 additions & 33 deletions public/Utilities/MeshUtils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ static std::optional<VertexBuffer> 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();
Expand All @@ -30,49 +31,48 @@ static std::optional<VertexBuffer> 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);
}
}

Expand Down Expand Up @@ -120,9 +120,10 @@ static std::optional<VertexBuffer> 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();
Expand All @@ -132,53 +133,52 @@ static std::optional<VertexBuffer> 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())
Expand Down

0 comments on commit 8eaa0f6

Please sign in to comment.