private Matrix4 CalcInterpolatedRotation(float animationTime, NodeAnimation channel) { Matrix4x4 matrix; if (channel.ScalingKeysCount == 1) { matrix = channel.RotationKeys[0].Value.GetMatrix(); } var rotateIndex = FindRotation(animationTime, channel); var nextRotateIndex = rotateIndex + 1; if (nextRotateIndex < channel.RotationKeysCount) { var rotateKey = channel.RotationKeys[rotateIndex]; var nextRotateKey = channel.RotationKeys[nextRotateIndex]; var deltaTime = nextRotateKey.Time - rotateKey.Time; var factor = (animationTime - rotateKey.Time) / deltaTime; matrix = Quaternion.Slerp(rotateKey.Value, nextRotateKey.Value, factor).GetMatrix(); } else { matrix = channel.RotationKeys[0].Value.GetMatrix(); } return(FromMatrix(matrix)); }
/// <summary> /// Evaluate animation channels at a given time /// </summary> /// <param name="pTime">Time, in seconds. If the time exceeds the animation's /// duration value, it will be looped.</param> /// <param name="isInEndPosition">A problem with automatic looping (and the /// way how assimp handles animation duration) is that evaluating the /// animation at a time that is exactly the animation duration might wrap /// over to the first key frame (i.e. due to numerical inaccuracies). Setting /// this parameter to true causes the pTime value to be ignored and the /// last set of key frames to be taken.</param> public void Evaluate(double pTime, bool isInEndPosition) { // extract ticks per second. Assume default value if not given // every following time calculation happens in ticks pTime *= _ticksPerSecond; // map into anim's duration double time = 0.0f; if (_animation.DurationInTicks > 0.0) { time = pTime % _animation.DurationInTicks; } // calculate the transformations for each animation channel for (int a = 0; a < _animation.NodeAnimationChannelCount; a++) { var channel = _animation.NodeAnimationChannels[a]; var presentPosition = new Vector3D(0, 0, 0); var presentRotation = new Quaternion(1, 0, 0, 0); var presentScaling = new Vector3D(1, 1, 1); if (isInEndPosition) { if (channel.PositionKeyCount > 0) { presentPosition = channel.PositionKeys.Last().Value; _lastPositions[a].Item1 = channel.PositionKeyCount - 1; } if (channel.RotationKeyCount > 0) { presentRotation = channel.RotationKeys.Last().Value; _lastPositions[a].Item2 = channel.RotationKeyCount - 1; } if (channel.ScalingKeyCount > 0) { presentScaling = channel.ScalingKeys.Last().Value; _lastPositions[a].Item3 = channel.ScalingKeyCount - 1; } BuildTransform(ref presentRotation, ref presentScaling, ref presentPosition, out _currentTransforms[a]); continue; } // ******** Position ***** if (channel.PositionKeyCount > 0) { // Look for present frame number. Search from last position if time is after the last time, else from beginning // Should be much quicker than always looking from start for the average use case. var frame = (time >= _lastTime) ? _lastPositions[a].Item1 : 0; while (frame < channel.PositionKeyCount - 1) { if (time < channel.PositionKeys[frame + 1].Time) { break; } frame++; } // interpolate between this frame's value and next frame's value var nextFrame = (frame + 1) % channel.PositionKeyCount; var key = channel.PositionKeys[frame]; var nextKey = channel.PositionKeys[nextFrame]; var diffTime = nextKey.Time - key.Time; if (diffTime < 0.0) { diffTime += _animation.DurationInTicks; } if (diffTime > 0) { var factor = (float)((time - key.Time) / diffTime); presentPosition = key.Value + (nextKey.Value - key.Value) * factor; } else { presentPosition = key.Value; } _lastPositions[a].Item1 = frame; } // ******** Rotation ********* if (channel.RotationKeyCount > 0) { var frame = (time >= _lastTime) ? _lastPositions[a].Item2 : 0; while (frame < channel.RotationKeyCount - 1) { if (time < channel.RotationKeys[frame + 1].Time) { break; } frame++; } // interpolate between this frame's value and next frame's value var nextFrame = (frame + 1) % channel.RotationKeyCount; var key = channel.RotationKeys[frame]; var nextKey = channel.RotationKeys[nextFrame]; double diffTime = nextKey.Time - key.Time; if (diffTime < 0.0) { diffTime += _animation.DurationInTicks; } if (diffTime > 0) { var factor = (float)((time - key.Time) / diffTime); presentRotation = Quaternion.Slerp(key.Value, nextKey.Value, factor); } else { presentRotation = key.Value; } _lastPositions[a].Item2 = frame; } // ******** Scaling ********** if (channel.ScalingKeyCount > 0) { var frame = (time >= _lastTime) ? _lastPositions[a].Item3 : 0; while (frame < channel.ScalingKeyCount - 1) { if (time < channel.ScalingKeys[frame + 1].Time) { break; } frame++; } // TODO: (thom) interpolation maybe? This time maybe even logarithmic, not linear presentScaling = channel.ScalingKeys[frame].Value; _lastPositions[a].Item3 = frame; } BuildTransform(ref presentRotation, ref presentScaling, ref presentPosition, out _currentTransforms[a]); } _lastTime = time; }