public float GetKeyFramesAtTime( float time, out KeyFrame keyFrame1, out KeyFrame keyFrame2, out short firstKeyIndex )
{
short firstIndex = -1;
float totalLength = parent.Length;
// wrap time
while ( time > totalLength )
time -= totalLength;
int i = 0;
// makes compiler happy so it wont complain about this var being unassigned
keyFrame1 = null;
// find the last keyframe before or on current time
for ( i = 0; i < keyFrameList.Count; i++ )
{
KeyFrame keyFrame = keyFrameList[ i ];
// kick out now if the current frames time is greater than the current time
if ( keyFrame.Time > time )
break;
keyFrame1 = keyFrame;
++firstIndex;
}
// trap case where there is no key before this time
// use the first key anyway and pretend it's time index 0
if ( firstIndex == -1 )
{
keyFrame1 = keyFrameList[ 0 ];
++firstIndex;
}
// fill index of the first key
firstKeyIndex = firstIndex;
// parametric time
// t1 = time of previous keyframe
// t2 = time of next keyframe
float t1, t2;
// find first keyframe after the time
// if no next keyframe, wrap back to first
// TODO: Verify logic
if ( firstIndex == ( keyFrameList.Count - 1 ) )
{
keyFrame2 = keyFrameList[ 0 ];
t2 = totalLength;
}
else
{
keyFrame2 = keyFrameList[ firstIndex + 1 ];
t2 = keyFrame2.Time;
}
t1 = keyFrame1.Time;
if ( t1 == t2 )
{
// same keyframe
return 0.0f;
}
else
{
return ( time - t1 ) / ( t2 - t1 );
}
}