From bb9e3c3593ad58cbe7e333aa2877aab28f36b8a3 Mon Sep 17 00:00:00 2001 From: tirichards Date: Wed, 5 Dec 2018 14:46:56 -0800 Subject: [PATCH] Fix failed assimp validation for glTF2 sample animations --- code/glTF2Importer.cpp | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/code/glTF2Importer.cpp b/code/glTF2Importer.cpp index f6a664e5e..be319d1da 100755 --- a/code/glTF2Importer.cpp +++ b/code/glTF2Importer.cpp @@ -826,15 +826,29 @@ aiNode* ImportNode(aiScene* pScene, glTF2::Asset& r, std::vector& aiBone* bone = new aiBone(); Ref joint = node.skin->jointNames[i]; - bone->mName = joint->name; + 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); std::vector& weights = weighting[i]; bone->mNumWeights = weights.size(); if (bone->mNumWeights > 0) { - bone->mWeights = new aiVertexWeight[bone->mNumWeights]; - memcpy(bone->mWeights, weights.data(), bone->mNumWeights * sizeof(aiVertexWeight)); + 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[i] = bone; } @@ -1022,7 +1036,7 @@ void glTF2Importer::ImportAnimations(glTF2::Asset& r) ++j; } } - + // Use the latest keyframe for the duration of the animation double maxDuration = 0; for (unsigned int j = 0; j < ai_anim->mNumChannels; ++j) { @@ -1047,7 +1061,7 @@ void glTF2Importer::ImportAnimations(glTF2::Asset& r) } } ai_anim->mDuration = maxDuration; - + mScene->mAnimations[i] = ai_anim; } }