示例#1
0
        public BinaryDataReader(Stream stm)
        {
            stream = stm;
            buffer = new byte[sizeof(decimal)];

            ContentBinaryReader br = new ContentBinaryReader(stm, Encoding.Default);

            // 读出所有块
            sectCount = br.ReadInt32();
            positions = new Dictionary <string, Entry>(sectCount);

            for (int i = 0; i < sectCount; i++)
            {
                string name = br.ReadStringUnicode();
                int    size = br.ReadInt32();

                positions.Add(name, new Entry(name, (int)br.BaseStream.Position, size));

                br.BaseStream.Position += size;
            }
            br.Close();
        }
示例#2
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));
        }