/// <summary> /// Construct an AnimationPlayer to play the clip /// </summary> /// <param name="model"></param> /// <param name="clip"></param> public AnimationPlayer(AnimatedModel model, AnimationClips.Clip clip) { this.model = model; this.clip = clip; Initialize(); }
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); } }
private AnimationClips ProcessAnimations(ModelContent model, NodeContent input, ContentProcessorContext context) { // First build a lookup table so we can determine the // index into the list of bones from a bone name. for (int i = 0; i < model.Bones.Count; i++) { bones[model.Bones[i].Name] = i; } AnimationClips animationClips = new AnimationClips(); ProcessAnimationsRecursive(input, animationClips); // Ensure all animations have a first key frame for every bone foreach (AnimationClips.Clip clip in animationClips.Clips.Values) { for (int b = 0; b < bones.Count; b++) { List<AnimationClips.Keyframe> keyframes = clip.Keyframes[b]; if (keyframes.Count == 0 || keyframes[0].Time > 0) { AnimationClips.Keyframe keyframe = new AnimationClips.Keyframe(); keyframe.Time = 0; Matrix transform = model.Bones[b].Transform; transform.Right = Vector3.Normalize(transform.Right); transform.Up = Vector3.Normalize(transform.Up); transform.Backward = Vector3.Normalize(transform.Backward); keyframe.Rotation = Quaternion.CreateFromRotationMatrix(transform); keyframe.Translation = transform.Translation; keyframes.Insert(0, keyframe); } } } return animationClips; }
public AnimationPlayer(AnimationClips.Clip clip) { this.clip = clip; }
private AnimationClips ProcessAnimations(ModelContent model, NodeContent input, ContentProcessorContext context) { //first build a lookup table so we can determine the index into the list of bones from a bone name for (int i = 0; i < model.Bones.Count; i++) { bones[model.Bones[i].Name] = i; } AnimationClips animationClips = new AnimationClips(); ProcessAnimationRecursive(input, animationClips); return animationClips; }
public AnimationPlayer(AnimationClips.Clip clips) { clip = clips; }
/// <summary> /// Recursive function that processes the entire scene graph, collecting up /// all of the animation data. /// </summary> /// <param name="input">The input scene graph node</param> /// <param name="animationClips">The animation clips object we put animation in</param> private void ProcessAnimationsRecursive(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; } else if (animation.Value.Duration.TotalSeconds > clip.Duration) { clip.Duration = animation.Value.Duration.TotalSeconds; } // // For each channel, 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; // Get rid of unnamed bones if (!bones.TryGetValue(channel.Key, out boneIndex)) { continue; // Ignore if not a named bone } // Get rid of useless animations if (!skinned && IsUselessAnimation(boneIndex)) { // System.Diagnostics.Debug.WriteLine("Removed " + boneIndex.ToString()); continue; // Ignore if useless } 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.Translation = transform.Translation; clip.Keyframes[boneIndex].Add(newKeyframe); } // Linear keyframe reduction (remove from clip.Keyframes) System.Diagnostics.Debug.WriteLine(clip.Keyframes[boneIndex].Count); LinkedList<AnimationClips.Keyframe> keyframes = new LinkedList<AnimationClips.Keyframe>(); foreach (AnimationClips.Keyframe keyframe in clip.Keyframes[boneIndex]) { keyframes.AddLast(keyframe); } LinearKeyframeReduction(keyframes); clip.Keyframes[boneIndex] = keyframes.ToList<AnimationClips.Keyframe>(); System.Diagnostics.Debug.WriteLine(clip.Keyframes[boneIndex].Count); } } // System.Diagnostics.Trace.WriteLine(input.Name); foreach (NodeContent child in input.Children) { ProcessAnimationsRecursive(child, animationClips); } }