private void ProcessAnimationRecursive(NodeContent input, AnimationClips animationClips)
        {
            foreach (KeyValuePair <string, AnimationContent> animation in input.Animations)
            {
                // do we have this animation before?
                AnimationClips.Clip clip;
                if (!animationClips.Clips.TryGetValue(animation.Key, out clip))
                {
                    //never before seen clip
                    System.Diagnostics.Trace.WriteLine("New clip: " + animation.Key);
                    clip           = new AnimationClips.Clip();
                    clip.Name      = animation.Key;
                    clip.Duration  = animation.Value.Duration.TotalSeconds;
                    clip.Keyframes = new List <AnimationClips.Keyframe> [bones.Count];
                    for (int b = 0; b < bones.Count; b++)
                    {
                        clip.Keyframes[b] = new List <AnimationClips.Keyframe>();
                    }

                    animationClips.Clips[animation.Key] = clip;
                }

                //for each canell, determine the bone and then process all of the keyframes for that bone
                foreach (KeyValuePair <string, AnimationChannel> channel in animation.Value.Channels)
                {
                    // what is the bone index?
                    int boneIndex;
                    if (!bones.TryGetValue(channel.Key, out boneIndex))
                    {
                        continue; //ignore if not a named bone
                    }
                    foreach (AnimationKeyframe keyframe in channel.Value)
                    {
                        Matrix transform = keyframe.Transform; //keyframe transformation
                        AnimationClips.Keyframe newKeyFrame = new AnimationClips.Keyframe();
                        newKeyFrame.Time = keyframe.Time.TotalSeconds;

                        transform.Right          = Vector3.Normalize(transform.Right);
                        transform.Up             = Vector3.Normalize(transform.Up);
                        transform.Backward       = Vector3.Normalize(transform.Backward);
                        newKeyFrame.Rotation     = Quaternion.CreateFromRotationMatrix(transform);
                        newKeyFrame.Translastion = transform.Translation;

                        clip.Keyframes[boneIndex].Add(newKeyFrame);
                    }
                }
            }
            //System.Diagnostics.Trace.WriteLine(input.Name);

            foreach (NodeContent child in input.Children)
            {
                ProcessAnimationRecursive(child, animationClips);
            }
        }
Ejemplo n.º 2
0
 protected override void Write(ContentWriter output, AnimationClips clips)
 {
     output.Write(clips.Clips.Count);
     foreach (KeyValuePair <string, AnimationClips.Clip> clipItem in clips.Clips)
     {
         AnimationClips.Clip clip = clipItem.Value;
         output.Write(clip.Name);
         output.Write(clip.Duration);
         output.Write(clip.Keyframes.Length);
         foreach (List <AnimationClips.Keyframe> keyframes in clip.Keyframes)
         {
             output.Write(keyframes.Count);
             foreach (AnimationClips.Keyframe keyframe in keyframes)
             {
                 output.Write(keyframe.Time);
                 output.Write(keyframe.Rotation);
                 output.Write(keyframe.Translastion);
             }
         }
     }
 }