Fix export of deleted meshes; Add LazyDict::Remove method
When I was merging a node’s multiple meshes into that node’s first mesh’s primitives, I was deleting the merged meshes from the node. However, I wasn’t deleting the merged meshes from the mAsset->meshes Dict, causing the gltf2 export to contain extra unreferenced meshes and duplicate primitives. This new code adds a new method to LazyDict, which removes the object from it, taking care to update indexes of the subsequent objects. This change also requires that `Ref`s of `Mesh`es (stored in node->meshes) have their indexes updated.
This commit is contained in:
@@ -800,9 +800,33 @@ void glTF2Exporter::MergeMeshes()
|
||||
for (unsigned int m = nMeshes - 1; m >= 1; --m) {
|
||||
Ref<Mesh> mesh = node->meshes.at(m);
|
||||
|
||||
firstMesh->primitives.insert(firstMesh->primitives.end(), mesh->primitives.begin(), mesh->primitives.end());
|
||||
//append this mesh's primitives to the first mesh's primitives
|
||||
firstMesh->primitives.insert(
|
||||
firstMesh->primitives.end(),
|
||||
mesh->primitives.begin(),
|
||||
mesh->primitives.end()
|
||||
);
|
||||
|
||||
node->meshes.erase(node->meshes.begin() + m);
|
||||
//remove the mesh from the list of meshes
|
||||
unsigned int removedIndex = mAsset->meshes.Remove(mesh->id.c_str());
|
||||
|
||||
//find the presence of the removed mesh in other nodes
|
||||
for (unsigned int nn = 0; nn < mAsset->nodes.Size(); ++nn) {
|
||||
Ref<Node> node = mAsset->nodes.Get(nn);
|
||||
|
||||
for (unsigned int mm = 0; mm < node->meshes.size(); ++mm) {
|
||||
Ref<Mesh>& meshRef = node->meshes.at(mm);
|
||||
unsigned int meshIndex = meshRef.GetIndex();
|
||||
|
||||
if (meshIndex == removedIndex) {
|
||||
node->meshes.erase(node->meshes.begin() + mm);
|
||||
} else if (meshIndex > removedIndex) {
|
||||
Ref<Mesh> newMeshRef = mAsset->meshes.Get(meshIndex - 1);
|
||||
|
||||
meshRef = newMeshRef;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//since we were looping backwards, reverse the order of merged primitives to their original order
|
||||
|
||||
Reference in New Issue
Block a user