protected void InitSetTime(TimeSpan value)
        {
            TimeSpan time = value;

            currentTimeValue = time;

            // Read keyframe matrices.
            IList <ModelKeyframe> keyframes = currentClipValue.Keyframes;

            while (currentKeyframe < keyframes.Count)
            {
                ModelKeyframe keyframe = keyframes[currentKeyframe];

                // Stop when we've read up to the current time position.
                if (keyframe.Time > currentTimeValue)
                {
                    break;
                }

                // Use this keyframe
                SetKeyframe(keyframe);

                currentKeyframe++;
            }
        }
 /// <summary>
 /// Sets the key frame by storing the current transform
 /// </summary>
 /// <param name="keyframe"></param>
 protected override void SetKeyframe(ModelKeyframe keyframe)
 {
     this.currentTransform = keyframe.Transform;
 }
Exemplo n.º 3
0
        protected override AnimationData Read(ContentReader input, AnimationData existingInstance)
        {
            if (existingInstance != null)
            {
                return(existingInstance);
            }

            List <Matrix> bindPose    = null;
            List <Matrix> invBindPose = null;
            Dictionary <string, ModelAnimationClip> modelAnim = null;
            Dictionary <string, ModelAnimationClip> rootAnim  = null;
            List <int> skeleHierarchy = null;

            BinaryDataReader ad;
            int size = input.ReadInt32();

            VirtualStream vs = new VirtualStream(input.BaseStream, input.BaseStream.Position, size);

            ad = new BinaryDataReader(vs);

            #region BindPoseTag
            if (ad.Contains(BindPoseCountTag))
            {
                int count = ad.GetDataInt32(BindPoseCountTag);
                bindPose = new List <Matrix>(count);

                ContentBinaryReader br2 = ad.GetData(BindPoseTag);
                for (int i = 0; i < count; i++)
                {
                    bindPose.Add(br2.ReadMatrix());
                }
                br2.Close();
            }

            #endregion
            #region InvBindPoseTag


            if (ad.Contains(InvBindPoseCountTag))
            {
                int count = ad.GetDataInt32(InvBindPoseCountTag);
                invBindPose = new List <Matrix>(count);

                ContentBinaryReader br2 = ad.GetData(InvBindPoseTag);
                for (int i = 0; i < count; i++)
                {
                    invBindPose.Add(br2.ReadMatrix());
                }
                br2.Close();
            }

            #endregion

            #region AnimationClipTag

            if (ad.Contains(ModelAnimationClipCountTag))
            {
                int count = ad.GetDataInt32(ModelAnimationClipCountTag);

                modelAnim = new Dictionary <string, ModelAnimationClip>(count);

                ContentBinaryReader br2 = ad.GetData(ModelAnimationClipTag);

                for (int i = 0; i < count; i++)
                {
                    string key = br2.ReadStringUnicode();

                    TimeSpan duration = TimeSpan.FromSeconds(br2.ReadDouble());

                    int frameCount = br2.ReadInt32();
                    List <ModelKeyframe> frames = new List <ModelKeyframe>(frameCount);
                    for (int j = 0; j < frameCount; j++)
                    {
                        int      bone      = br2.ReadInt32();
                        TimeSpan totalSec  = TimeSpan.FromSeconds(br2.ReadDouble());
                        Matrix   transform = br2.ReadMatrix();

                        ModelKeyframe frame = new ModelKeyframe(bone, totalSec, transform);
                        frames.Add(frame);
                    }

                    ModelAnimationClip clip = new ModelAnimationClip(duration, frames);

                    modelAnim.Add(key, clip);
                }
                br2.Close();
            }


            #endregion

            #region RootAnimationClipTag

            if (ad.Contains(RootAnimationClipCountTag))
            {
                int count = ad.GetDataInt32(RootAnimationClipCountTag);

                rootAnim = new Dictionary <string, ModelAnimationClip>(count);

                ContentBinaryReader br2 = ad.GetData(RootAnimationClipTag);

                for (int i = 0; i < count; i++)
                {
                    string key = br2.ReadStringUnicode();

                    TimeSpan duration = TimeSpan.FromSeconds(br2.ReadDouble());

                    int frameCount = br2.ReadInt32();
                    List <ModelKeyframe> frames = new List <ModelKeyframe>(frameCount);
                    for (int j = 0; j < frameCount; j++)
                    {
                        int      bone      = br2.ReadInt32();
                        TimeSpan totalSec  = TimeSpan.FromSeconds(br2.ReadDouble());
                        Matrix   transform = br2.ReadMatrix();

                        ModelKeyframe frame = new ModelKeyframe(bone, totalSec, transform);
                        frames.Add(frame);
                    }

                    ModelAnimationClip clip = new ModelAnimationClip(duration, frames);
                    rootAnim.Add(key, clip);
                }
                br2.Close();
            }

            #endregion

            #region BoneHierarchyTag

            if (ad.Contains(BoneHierarchyCountTag))
            {
                int count = ad.GetDataInt32(BoneHierarchyCountTag);
                skeleHierarchy = new List <int>(count);


                ContentBinaryReader br2 = ad.GetData(BoneHierarchyTag);
                for (int i = 0; i < count; i++)
                {
                    skeleHierarchy.Add(br2.ReadInt32());
                }

                br2.Close();
            }


            #endregion

            return(new AnimationData(modelAnim, rootAnim, bindPose, invBindPose, skeleHierarchy));
        }
 /// <summary>
 /// Sets the key frame for the passed in frame
 /// </summary>
 /// <param name="keyframe"></param>
 protected override void SetKeyframe(ModelKeyframe keyframe)
 {
     boneTransforms[keyframe.Bone] = keyframe.Transform;
 }
 /// <summary>
 /// Virtual method allowing subclasses to set any data associated with a particular keyframe.
 /// </summary>
 /// <param name="keyframe">Keyframe being set</param>
 protected virtual void SetKeyframe(ModelKeyframe keyframe)
 {
 }