From 5f35d62d612fbc4af6a4dd48653192b08c0a26ad Mon Sep 17 00:00:00 2001 From: Sebastian Matusik Date: Fri, 28 Feb 2020 18:41:29 -0800 Subject: [PATCH 1/2] Fix for #3037 cause glTF2Importer creating bunch of bones with 0 for vertex with index 0 --- code/glTF2/glTF2Importer.cpp | 77 ++++++++++++++++++++---------------- 1 file changed, 42 insertions(+), 35 deletions(-) diff --git a/code/glTF2/glTF2Importer.cpp b/code/glTF2/glTF2Importer.cpp index 5e137ed79..34591a461 100644 --- a/code/glTF2/glTF2Importer.cpp +++ b/code/glTF2/glTF2Importer.cpp @@ -111,8 +111,9 @@ const aiImporterDesc *glTF2Importer::GetInfo() const { bool glTF2Importer::CanRead(const std::string &pFile, IOSystem *pIOHandler, bool /* checkSig */) const { const std::string &extension = GetExtension(pFile); - if (extension != "gltf" && extension != "glb") + if (extension != "gltf" && extension != "glb") { return false; + } if (pIOHandler) { glTF2::Asset asset(pIOHandler); @@ -323,8 +324,9 @@ static inline bool CheckValidFacesIndices(aiFace *faces, unsigned nFaces, unsign for (unsigned i = 0; i < nFaces; ++i) { for (unsigned j = 0; j < faces[i].mNumIndices; ++j) { unsigned idx = faces[i].mIndices[j]; - if (idx >= nVerts) + if (idx >= nVerts) { return false; + } } } return true; @@ -861,7 +863,19 @@ aiNode *ImportNode(aiScene *pScene, glTF2::Asset &r, std::vector & if (node.skin) { for (int primitiveNo = 0; primitiveNo < count; ++primitiveNo) { aiMesh *mesh = pScene->mMeshes[meshOffsets[mesh_idx] + primitiveNo]; - mesh->mNumBones = static_cast(node.skin->jointNames.size()); + unsigned int numBones =static_cast(node.skin->jointNames.size()); + + std::vector> weighting(numBones); + BuildVertexWeightMapping(node.meshes[0]->primitives[primitiveNo], weighting); + + unsigned int realNumBones = 0; + for (uint32_t i = 0; i < numBones; ++i) { + if (weighting[i].size() > 0) { + realNumBones++; + } + } + + mesh->mNumBones = static_cast(realNumBones); mesh->mBones = new aiBone *[mesh->mNumBones]; // GLTF and Assimp choose to store bone weights differently. @@ -873,43 +887,33 @@ aiNode *ImportNode(aiScene *pScene, glTF2::Asset &r, std::vector & // both because it's somewhat slow and because, for many applications, // we then need to reconvert the data back into the vertex-to-bone // mapping which makes things doubly-slow. - std::vector> weighting(mesh->mNumBones); - BuildVertexWeightMapping(node.meshes[0]->primitives[primitiveNo], weighting); mat4 *pbindMatrices = nullptr; node.skin->inverseBindMatrices->ExtractData(pbindMatrices); + + int cb = 0; + for (uint32_t i = 0; i < numBones; ++i) { + const std::vector &weights = weighting[i]; + if (weights.size() > 0) { + aiBone *bone = new aiBone(); - for (uint32_t i = 0; i < mesh->mNumBones; ++i) { - aiBone *bone = new aiBone(); - - Ref joint = node.skin->jointNames[i]; - if (!joint->name.empty()) { - bone->mName = joint->name; - } else { - // Assimp expects each bone to have a unique name. - static const std::string kDefaultName = "bone_"; - char postfix[10] = { 0 }; - ASSIMP_itoa10(postfix, i); - bone->mName = (kDefaultName + postfix); - } - GetNodeTransform(bone->mOffsetMatrix, *joint); - - CopyValue(pbindMatrices[i], bone->mOffsetMatrix); - - std::vector &weights = weighting[i]; - - bone->mNumWeights = static_cast(weights.size()); - if (bone->mNumWeights > 0) { + Ref joint = node.skin->jointNames[i]; + if (!joint->name.empty()) { + bone->mName = joint->name; + } else { + // Assimp expects each bone to have a unique name. + static const std::string kDefaultName = "bone_"; + char postfix[10] = { 0 }; + ASSIMP_itoa10(postfix, i); + bone->mName = (kDefaultName + postfix); + } + GetNodeTransform(bone->mOffsetMatrix, *joint); + CopyValue(pbindMatrices[i], bone->mOffsetMatrix); + bone->mNumWeights = static_cast(weights.size()); bone->mWeights = new aiVertexWeight[bone->mNumWeights]; memcpy(bone->mWeights, weights.data(), bone->mNumWeights * sizeof(aiVertexWeight)); - } else { - // Assimp expects all bones to have at least 1 weight. - bone->mWeights = new aiVertexWeight[1]; - bone->mNumWeights = 1; - bone->mWeights->mVertexId = 0; - bone->mWeights->mWeight = 0.f; + mesh->mBones[cb++] = bone; } - mesh->mBones[i] = bone; } if (pbindMatrices) { @@ -1232,8 +1236,9 @@ void glTF2Importer::ImportEmbeddedTextures(glTF2::Asset &r) { int numEmbeddedTexs = 0; for (size_t i = 0; i < r.images.Size(); ++i) { - if (r.images[i].HasData()) + if (r.images[i].HasData()) { numEmbeddedTexs += 1; + } } if (numEmbeddedTexs == 0) @@ -1244,7 +1249,9 @@ void glTF2Importer::ImportEmbeddedTextures(glTF2::Asset &r) { // Add the embedded textures for (size_t i = 0; i < r.images.Size(); ++i) { Image &img = r.images[i]; - if (!img.HasData()) continue; + if (!img.HasData()) { + continue; + } int idx = mScene->mNumTextures++; embeddedTexIdxs[i] = idx; From 710dbba52dee0f4ac2f1e59a1f495ed287d88372 Mon Sep 17 00:00:00 2001 From: Sebastian Matusik Date: Fri, 28 Feb 2020 19:19:10 -0800 Subject: [PATCH 2/2] Trimmed trailing whitespaces --- code/glTF2/glTF2Importer.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/code/glTF2/glTF2Importer.cpp b/code/glTF2/glTF2Importer.cpp index 34591a461..8c33674e1 100644 --- a/code/glTF2/glTF2Importer.cpp +++ b/code/glTF2/glTF2Importer.cpp @@ -864,17 +864,17 @@ aiNode *ImportNode(aiScene *pScene, glTF2::Asset &r, std::vector & for (int primitiveNo = 0; primitiveNo < count; ++primitiveNo) { aiMesh *mesh = pScene->mMeshes[meshOffsets[mesh_idx] + primitiveNo]; unsigned int numBones =static_cast(node.skin->jointNames.size()); - + std::vector> weighting(numBones); BuildVertexWeightMapping(node.meshes[0]->primitives[primitiveNo], weighting); - + unsigned int realNumBones = 0; for (uint32_t i = 0; i < numBones; ++i) { if (weighting[i].size() > 0) { realNumBones++; } } - + mesh->mNumBones = static_cast(realNumBones); mesh->mBones = new aiBone *[mesh->mNumBones]; @@ -890,7 +890,7 @@ aiNode *ImportNode(aiScene *pScene, glTF2::Asset &r, std::vector & mat4 *pbindMatrices = nullptr; node.skin->inverseBindMatrices->ExtractData(pbindMatrices); - + int cb = 0; for (uint32_t i = 0; i < numBones; ++i) { const std::vector &weights = weighting[i];