浏览代码

Merge pull request #2244 from bubba/gltf2-anim-duration

Calculate the mMaxDuration property based off of keyframes for GLTF2
Kim Kulling 6 年之前
父节点
当前提交
da0d49f751
共有 1 个文件被更改,包括 20 次插入0 次删除
  1. 20 0
      code/glTF2Importer.cpp

+ 20 - 0
code/glTF2Importer.cpp

@@ -1022,6 +1022,26 @@ void glTF2Importer::ImportAnimations(glTF2::Asset& r)
                 ++j;
             }
         }
+        
+        // Use the latest keyframe for the duration of the animation
+        unsigned int maxDuration = 0;
+        for (unsigned int j = 0; j < ai_anim->mNumChannels; ++j) {
+            auto chan = ai_anim->mChannels[j];
+            if (chan->mNumPositionKeys) {
+                auto lastPosKey = chan->mPositionKeys[chan->mNumPositionKeys - 1];
+                if (lastPosKey.mTime > maxDuration) maxDuration = lastPosKey.mTime;
+            }
+            if (chan->mNumRotationKeys) {
+                auto lastRotKey = chan->mRotationKeys[chan->mNumRotationKeys - 1];
+                if (lastRotKey.mTime > maxDuration) maxDuration = lastRotKey.mTime;
+            }
+            if (chan->mNumScalingKeys) {
+                auto lastScaleKey = chan->mScalingKeys[chan->mNumScalingKeys - 1];
+                if (lastScaleKey.mTime > maxDuration) maxDuration = lastScaleKey.mTime;
+            }
+        }
+        ai_anim->mDuration = maxDuration;
+        
         mScene->mAnimations[i] = ai_anim;
     }
 }