diff --git a/code/glTF2Exporter.cpp b/code/glTF2Exporter.cpp index f63c8e89c..888721c41 100644 --- a/code/glTF2Exporter.cpp +++ b/code/glTF2Exporter.cpp @@ -744,6 +744,7 @@ void glTF2Exporter::ExportMeshes() } } +//merges a node's multiple meshes (with one primitive each) into one mesh with multiple primitives void glTF2Exporter::MergeMeshes() { for (unsigned int n = 0; n < mAsset->nodes.Size(); ++n) { @@ -751,16 +752,34 @@ void glTF2Exporter::MergeMeshes() unsigned int nMeshes = node->meshes.size(); - if (nMeshes) { + //skip if it's 1 or less meshes per node + if (nMeshes > 1) { Ref firstMesh = node->meshes.at(0); + Mesh::Primitive firstPrimitive = firstMesh->primitives.at(0); - for (unsigned int m = 1; m < nMeshes; ++m) { + //loop backwards to allow easy removal of a mesh from a node once it's merged + for (unsigned int m = nMeshes - 1; m >= 1; --m) { Ref mesh = node->meshes.at(m); - Mesh::Primitive primitive = mesh->primitives.at(0); - firstMesh->primitives.push_back(primitive); + bool primitivesPushed = false; + + for (unsigned int p = 0; p < mesh->primitives.size(); ++p) { + Mesh::Primitive primitive = mesh->primitives.at(p); + + if (firstPrimitive.mode == primitive.mode) { + firstMesh->primitives.push_back(primitive); + primitivesPushed = true; + } + } + + if (primitivesPushed) { + //remove the merged meshes from the node + node->meshes.erase(node->meshes.begin() + m); + } } - node->meshes.erase(node->meshes.begin() + 1, node->meshes.end()); + //since we were looping backwards, reverse the order of merged primitives to their original order + std::reverse(firstMesh->primitives.begin() + 1, firstMesh->primitives.end()); + } } }