Merge pull request #3531 from JLouis-B/gltf2-exporter-crash

Fixed a crash of the Gltf 2 exporter in the case of an animation without scale animation key.
pull/3550/head^2
Kim Kulling 2020-12-20 21:57:44 +01:00 committed by GitHub
commit 5f5f1cf7a0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 19 additions and 21 deletions

View File

@ -1258,9 +1258,6 @@ inline Ref<Accessor> GetSamplerInputRef(Asset& asset, std::string& animId, Ref<B
inline void ExtractTranslationSampler(Asset& asset, std::string& animId, Ref<Buffer>& buffer, const aiNodeAnim* nodeChannel, float ticksPerSecond, Animation::Sampler& sampler) inline void ExtractTranslationSampler(Asset& asset, std::string& animId, Ref<Buffer>& buffer, const aiNodeAnim* nodeChannel, float ticksPerSecond, Animation::Sampler& sampler)
{ {
const unsigned int numKeyframes = nodeChannel->mNumPositionKeys; const unsigned int numKeyframes = nodeChannel->mNumPositionKeys;
if (numKeyframes == 0) {
return;
}
std::vector<float> times(numKeyframes); std::vector<float> times(numKeyframes);
std::vector<float> values(numKeyframes * 3); std::vector<float> values(numKeyframes * 3);
@ -1281,9 +1278,6 @@ inline void ExtractTranslationSampler(Asset& asset, std::string& animId, Ref<Buf
inline void ExtractScaleSampler(Asset& asset, std::string& animId, Ref<Buffer>& buffer, const aiNodeAnim* nodeChannel, float ticksPerSecond, Animation::Sampler& sampler) inline void ExtractScaleSampler(Asset& asset, std::string& animId, Ref<Buffer>& buffer, const aiNodeAnim* nodeChannel, float ticksPerSecond, Animation::Sampler& sampler)
{ {
const unsigned int numKeyframes = nodeChannel->mNumScalingKeys; const unsigned int numKeyframes = nodeChannel->mNumScalingKeys;
if (numKeyframes == 0) {
return;
}
std::vector<float> times(numKeyframes); std::vector<float> times(numKeyframes);
std::vector<float> values(numKeyframes * 3); std::vector<float> values(numKeyframes * 3);
@ -1304,9 +1298,6 @@ inline void ExtractScaleSampler(Asset& asset, std::string& animId, Ref<Buffer>&
inline void ExtractRotationSampler(Asset& asset, std::string& animId, Ref<Buffer>& buffer, const aiNodeAnim* nodeChannel, float ticksPerSecond, Animation::Sampler& sampler) inline void ExtractRotationSampler(Asset& asset, std::string& animId, Ref<Buffer>& buffer, const aiNodeAnim* nodeChannel, float ticksPerSecond, Animation::Sampler& sampler)
{ {
const unsigned int numKeyframes = nodeChannel->mNumRotationKeys; const unsigned int numKeyframes = nodeChannel->mNumRotationKeys;
if (numKeyframes == 0) {
return;
}
std::vector<float> times(numKeyframes); std::vector<float> times(numKeyframes);
std::vector<float> values(numKeyframes * 4); std::vector<float> values(numKeyframes * 4);
@ -1347,29 +1338,36 @@ void glTF2Exporter::ExportAnimations()
if (anim->mName.length > 0) { if (anim->mName.length > 0) {
nameAnim = anim->mName.C_Str(); nameAnim = anim->mName.C_Str();
} }
Ref<Animation> animRef = mAsset->animations.Create(nameAnim);
for (unsigned int channelIndex = 0; channelIndex < anim->mNumChannels; ++channelIndex) { for (unsigned int channelIndex = 0; channelIndex < anim->mNumChannels; ++channelIndex) {
const aiNodeAnim* nodeChannel = anim->mChannels[channelIndex]; const aiNodeAnim* nodeChannel = anim->mChannels[channelIndex];
// It appears that assimp stores this type of animation as multiple animations.
// where each aiNodeAnim in mChannels animates a specific node.
std::string name = nameAnim + "_" + to_string(channelIndex); std::string name = nameAnim + "_" + to_string(channelIndex);
name = mAsset->FindUniqueID(name, "animation"); name = mAsset->FindUniqueID(name, "animation");
Ref<Animation> animRef = mAsset->animations.Create(name);
Ref<Node> animNode = mAsset->nodes.Get(nodeChannel->mNodeName.C_Str()); Ref<Node> animNode = mAsset->nodes.Get(nodeChannel->mNodeName.C_Str());
Animation::Sampler translationSampler; if (nodeChannel->mNumPositionKeys > 0)
ExtractTranslationSampler(*mAsset, name, bufferRef, nodeChannel, ticksPerSecond, translationSampler); {
AddSampler(animRef, animNode, translationSampler, AnimationPath_TRANSLATION); Animation::Sampler translationSampler;
ExtractTranslationSampler(*mAsset, name, bufferRef, nodeChannel, ticksPerSecond, translationSampler);
AddSampler(animRef, animNode, translationSampler, AnimationPath_TRANSLATION);
}
Animation::Sampler rotationSampler; if (nodeChannel->mNumRotationKeys > 0)
ExtractRotationSampler(*mAsset, name, bufferRef, nodeChannel, ticksPerSecond, rotationSampler); {
AddSampler(animRef, animNode, rotationSampler, AnimationPath_ROTATION); Animation::Sampler rotationSampler;
ExtractRotationSampler(*mAsset, name, bufferRef, nodeChannel, ticksPerSecond, rotationSampler);
AddSampler(animRef, animNode, rotationSampler, AnimationPath_ROTATION);
}
Animation::Sampler scaleSampler; if (nodeChannel->mNumScalingKeys > 0)
ExtractScaleSampler(*mAsset, name, bufferRef, nodeChannel, ticksPerSecond, scaleSampler); {
AddSampler(animRef, animNode, scaleSampler, AnimationPath_SCALE); Animation::Sampler scaleSampler;
ExtractScaleSampler(*mAsset, name, bufferRef, nodeChannel, ticksPerSecond, scaleSampler);
AddSampler(animRef, animNode, scaleSampler, AnimationPath_SCALE);
}
} }
// Assimp documentation staes this is not used (not implemented) // Assimp documentation staes this is not used (not implemented)