Преглед изворни кода

Node interp

Fix node interpolation to remove the jittered (reverted back to previous get node transform)
marauder2k7 пре 1 година
родитељ
комит
58bcea770c
2 измењених фајлова са 82 додато и 96 уклоњено
  1. 67 80
      Engine/source/ts/assimp/assimpAppNode.cpp
  2. 15 16
      Engine/source/ts/assimp/assimpAppSequence.cpp

+ 67 - 80
Engine/source/ts/assimp/assimpAppNode.cpp

@@ -132,105 +132,92 @@ void AssimpAppNode::getAnimatedTransform(MatrixF& mat, F32 t, aiAnimation* animS
          QuatF rot;
          rot.identity();
          // T is in seconds, convert to frames.
-         F32 frame = t * animSeq->mTicksPerSecond;
+         F32 frame = (t * animSeq->mTicksPerSecond + 0.5f) + 1.0f;
 
-         // interpolate scaling.
-         if (nodeAnim->mNumScalingKeys > 1) {
-            U32 scaleIndex = 0;
-
-            for (U32 i = 0; i < nodeAnim->mNumScalingKeys-1; i++)
+         // Transform
+         if (nodeAnim->mNumPositionKeys == 1)
+            trans.set(nodeAnim->mPositionKeys[0].mValue.x, nodeAnim->mPositionKeys[0].mValue.y, nodeAnim->mPositionKeys[0].mValue.z);
+         else
+         {
+            Point3F curPos, lastPos;
+            F32 lastT = 0.0;
+            for (U32 key = 0; key < nodeAnim->mNumPositionKeys; ++key)
             {
-               if (frame < nodeAnim->mScalingKeys[i + 1].mTime) {
-                  scaleIndex = i;
+               F32 curT = sTimeMultiplier * (F32)nodeAnim->mPositionKeys[key].mTime;
+               curPos.set(nodeAnim->mPositionKeys[key].mValue.x, nodeAnim->mPositionKeys[key].mValue.y, nodeAnim->mPositionKeys[key].mValue.z);
+               if ((curT > frame) && (key > 0))
+               {
+                  F32 factor = (frame - lastT) / (curT - lastT);
+                  trans.interpolate(lastPos, curPos, factor);
+                  break;
+               }
+               else if ((curT >= frame) || (key == nodeAnim->mNumPositionKeys - 1))
+               {
+                  trans = curPos;
                   break;
                }
-            }
-
-            const Point3F& scalingStart = Point3F( nodeAnim->mScalingKeys[scaleIndex].mValue.x,
-                                                   nodeAnim->mScalingKeys[scaleIndex].mValue.y,
-                                                   nodeAnim->mScalingKeys[scaleIndex].mValue.z);
-
-            const Point3F& scalingEnd = Point3F(nodeAnim->mScalingKeys[scaleIndex + 1].mValue.x,
-                                                nodeAnim->mScalingKeys[scaleIndex + 1].mValue.y,
-                                                nodeAnim->mScalingKeys[scaleIndex + 1].mValue.z);
-
-            F32 deltaTime = nodeAnim->mScalingKeys[scaleIndex + 1].mTime - nodeAnim->mScalingKeys[scaleIndex].mTime;
-            F32 factor = (frame - nodeAnim->mScalingKeys[scaleIndex].mTime) / deltaTime;
 
-            scale = scalingStart + factor * (scalingEnd - scalingStart);
+               lastT = curT;
+               lastPos = curPos;
+            }
          }
+
+         // Rotation
+         if (nodeAnim->mNumRotationKeys == 1)
+            rot.set(nodeAnim->mRotationKeys[0].mValue.x, nodeAnim->mRotationKeys[0].mValue.y,
+               nodeAnim->mRotationKeys[0].mValue.z, nodeAnim->mRotationKeys[0].mValue.w);
          else
          {
-            scale.set(  nodeAnim->mScalingKeys[0].mValue.x,
-                        nodeAnim->mScalingKeys[0].mValue.y,
-                        nodeAnim->mScalingKeys[0].mValue.z);
-         }
-
-         // interpolate rotation.
-         if (nodeAnim->mNumRotationKeys > 1) {
-            U32 rotationIndex = 0;
-
-            for (U32 i = 0; i < nodeAnim->mNumRotationKeys - 1; i++)
+            QuatF curRot, lastRot;
+            F32 lastT = 0.0;
+            for (U32 key = 0; key < nodeAnim->mNumRotationKeys; ++key)
             {
-               if (frame < nodeAnim->mRotationKeys[i + 1].mTime) {
-                  rotationIndex = i;
+               F32 curT = sTimeMultiplier * (F32)nodeAnim->mRotationKeys[key].mTime;
+               curRot.set(nodeAnim->mRotationKeys[key].mValue.x, nodeAnim->mRotationKeys[key].mValue.y,
+                  nodeAnim->mRotationKeys[key].mValue.z, nodeAnim->mRotationKeys[key].mValue.w);
+               if ((curT > frame) && (key > 0))
+               {
+                  F32 factor = (frame - lastT) / (curT - lastT);
+                  rot.interpolate(lastRot, curRot, factor);
+                  break;
+               }
+               else if ((curT >= frame) || (key == nodeAnim->mNumRotationKeys - 1))
+               {
+                  rot = curRot;
                   break;
                }
-            }
-
-            const QuatF& rotStart = QuatF(nodeAnim->mRotationKeys[rotationIndex].mValue.x,
-                                          nodeAnim->mRotationKeys[rotationIndex].mValue.y,
-                                          nodeAnim->mRotationKeys[rotationIndex].mValue.z,
-                                          nodeAnim->mRotationKeys[rotationIndex].mValue.w);
-
-            const QuatF& rotEnd = QuatF(nodeAnim->mRotationKeys[rotationIndex + 1].mValue.x,
-                                       nodeAnim->mRotationKeys[rotationIndex + 1].mValue.y,
-                                       nodeAnim->mRotationKeys[rotationIndex + 1].mValue.z,
-                                       nodeAnim->mRotationKeys[rotationIndex + 1].mValue.w);
-
-            F32 deltaTime = nodeAnim->mRotationKeys[rotationIndex + 1].mTime - nodeAnim->mRotationKeys[rotationIndex].mTime;
-            F32 factor = (frame - nodeAnim->mRotationKeys[rotationIndex].mTime) / deltaTime;
 
-            rot.interpolate(rotStart, rotEnd, factor);
+               lastT = curT;
+               lastRot = curRot;
+            }
          }
+
+         // Scale
+         if (nodeAnim->mNumScalingKeys == 1)
+            scale.set(nodeAnim->mScalingKeys[0].mValue.x, nodeAnim->mScalingKeys[0].mValue.y, nodeAnim->mScalingKeys[0].mValue.z);
          else
          {
-            rot.set( nodeAnim->mRotationKeys[0].mValue.x,
-                     nodeAnim->mRotationKeys[0].mValue.y,
-                     nodeAnim->mRotationKeys[0].mValue.z,
-                     nodeAnim->mRotationKeys[0].mValue.w);
-         }
-
-         // interpolate position.
-         if (nodeAnim->mNumPositionKeys > 1) {
-            U32 posIndex = 0;
-
-            for (U32 i = 0; i < nodeAnim->mNumPositionKeys - 1; i++)
+            Point3F curScale, lastScale;
+            F32 lastT = 0.0;
+            for (U32 key = 0; key < nodeAnim->mNumScalingKeys; ++key)
             {
-               if (frame < nodeAnim->mPositionKeys[i + 1].mTime) {
-                  posIndex = i;
+               F32 curT = sTimeMultiplier * (F32)nodeAnim->mScalingKeys[key].mTime;
+               curScale.set(nodeAnim->mScalingKeys[key].mValue.x, nodeAnim->mScalingKeys[key].mValue.y, nodeAnim->mScalingKeys[key].mValue.z);
+               if ((curT > frame) && (key > 0))
+               {
+                  F32 factor = (frame - lastT) / (curT - lastT);
+                  scale.interpolate(lastScale, curScale, factor);
+                  break;
+               }
+               else if ((curT >= frame) || (key == nodeAnim->mNumScalingKeys - 1))
+               {
+                  scale = curScale;
                   break;
                }
-            }
-
-            const Point3F& posStart = Point3F( nodeAnim->mPositionKeys[posIndex].mValue.x,
-                                             nodeAnim->mPositionKeys[posIndex].mValue.y,
-                                             nodeAnim->mPositionKeys[posIndex].mValue.z);
-
-            const Point3F& posEnd = Point3F(nodeAnim->mPositionKeys[posIndex + 1].mValue.x,
-                                          nodeAnim->mPositionKeys[posIndex + 1].mValue.y,
-                                          nodeAnim->mPositionKeys[posIndex + 1].mValue.z);
-
-            F32 deltaTime = nodeAnim->mPositionKeys[posIndex + 1].mTime - nodeAnim->mPositionKeys[posIndex].mTime;
-            F32 factor = (frame - nodeAnim->mPositionKeys[posIndex].mTime) / deltaTime;
 
-            trans = posStart + factor * (posEnd - posStart);
-         }
-         else
-         {
-            trans.set(  nodeAnim->mPositionKeys[0].mValue.x,
-                        nodeAnim->mPositionKeys[0].mValue.y,
-                        nodeAnim->mPositionKeys[0].mValue.z);
+               lastT = curT;
+               lastScale = curScale;
+            }
          }
 
          rot.setMatrix(&mat);

+ 15 - 16
Engine/source/ts/assimp/assimpAppSequence.cpp

@@ -17,7 +17,6 @@ AssimpAppSequence::AssimpAppSequence(aiAnimation *a) :
    seqEnd(0.0f)
 {
    mAnim = new aiAnimation(*a);
-
    // Deep copy channels
    mAnim->mChannels = new aiNodeAnim * [a->mNumChannels];
    for (U32 i = 0; i < a->mNumChannels; ++i) {
@@ -68,21 +67,21 @@ AssimpAppSequence::AssimpAppSequence(aiAnimation *a) :
 
    mTimeMultiplier = 1.0f;
 
-   //S32 timeFactor = ColladaUtils::getOptions().animTiming;
-   //S32 fpsRequest = (S32)a->mTicksPerSecond;
-   //if (timeFactor == 0)
-   //{  // Timing specified in frames
-   //   fps = mClamp(fpsRequest, 5 /*TSShapeLoader::MinFrameRate*/, TSShapeLoader::MaxFrameRate);
-   //   mTimeMultiplier = 1.0f / fps;
-   //}
-   //else
-   //{  // Timing specified in seconds or ms depending on format
-   //   if (seqEnd > 1000.0f || a->mDuration > 1000.0f)
-   //      timeFactor = 1000.0f;   // If it's more than 1000 seconds, assume it's ms.
+   S32 timeFactor = ColladaUtils::getOptions().animTiming;
+   S32 fpsRequest = (S32)a->mTicksPerSecond;
+   if (timeFactor == 0)
+   {  // Timing specified in frames
+      fps = mClamp(fpsRequest, 5 /*TSShapeLoader::MinFrameRate*/, TSShapeLoader::MaxFrameRate);
+      mTimeMultiplier = 1.0f / fps;
+   }
+   else
+   {  // Timing specified in seconds or ms depending on format
+      if (seqEnd > 1000.0f || a->mDuration > 1000.0f)
+         timeFactor = 1000.0f;   // If it's more than 1000 seconds, assume it's ms.
 
-   //   timeFactor = mClamp(timeFactor, 1, 1000);
-   //   mTimeMultiplier = 1.0f / timeFactor;
-   //}
+      timeFactor = mClamp(timeFactor, 1, 1000);
+      mTimeMultiplier = 1.0f / timeFactor;
+   }
 
 }
 
@@ -114,5 +113,5 @@ F32 AssimpAppSequence::getPriority() const
 }
 F32 AssimpAppSequence::getBlendRefTime() const 
 { 
-   return -1.0f; 
+   return 0.0f; 
 }