예제 #1
0
        private void ReadBones(ContentReader input)
        {
            ModelBone[] bones = new ModelBone[input.ReadInt32()];

            for (int i = 0; i < bones.Length; i++)
            {
                string name = input.ReadObject<string>();
                Matrix transform = input.ReadMatrix();
                bones[i] = new ModelBone(i, name, transform);
            }

            this.bones = new ModelBoneCollection(bones);

            foreach (ModelBone bone in bones)
            {
                ModelBone newParent = this.ReadBoneReference(input);
                int size = input.ReadInt32();
                ModelBone[] newChildren = new ModelBone[size];
                for (int j = 0; j < size; j++)
                {
                    newChildren[j] = this.ReadBoneReference(input);
                }
                bone.SetParentChildren(newParent, newChildren);
            }
        }
예제 #2
0
        protected internal override Model Read(ContentReader reader, Model existingInstance)
        {
            // Read the bone names and transforms.
            uint             boneCount = reader.ReadUInt32();
            List <ModelBone> bones     = new List <ModelBone>((int)boneCount);

            for (uint i = 0; i < boneCount; i += 1)
            {
                string    name   = reader.ReadObject <string>();
                Matrix    matrix = reader.ReadMatrix();
                ModelBone bone   = new ModelBone {
                    Transform = matrix,
                    Index     = (int)i,
                    Name      = name
                };
                bones.Add(bone);
            }
            // Read the bone hierarchy.
            for (int i = 0; i < boneCount; i += 1)
            {
                ModelBone bone = bones[i];
                // Read the parent bone reference.
                int parentIndex = ReadBoneReference(reader, boneCount);
                if (parentIndex != -1)
                {
                    bone.Parent = bones[parentIndex];
                }
                // Read the child bone references.
                uint childCount = reader.ReadUInt32();
                if (childCount != 0)
                {
                    for (uint j = 0; j < childCount; j += 1)
                    {
                        int childIndex = ReadBoneReference(reader, boneCount);
                        if (childIndex != -1)
                        {
                            bone.AddChild(bones[childIndex]);
                        }
                    }
                }
            }

            List <ModelMesh> meshes = new List <ModelMesh>();

            // Read the mesh data.
            int meshCount = reader.ReadInt32();

            for (int i = 0; i < meshCount; i += 1)
            {
                string         name            = reader.ReadObject <string>();
                int            parentBoneIndex = ReadBoneReference(reader, boneCount);
                BoundingSphere boundingSphere  = reader.ReadBoundingSphere();

                // Tag
                object meshTag = reader.ReadObject <object>();

                // Read the mesh part data.
                int partCount = reader.ReadInt32();

                List <ModelMeshPart> parts = new List <ModelMeshPart>(partCount);

                for (uint j = 0; j < partCount; j += 1)
                {
                    ModelMeshPart part;
                    if (existingInstance != null)
                    {
                        part = existingInstance.Meshes[i].MeshParts[(int)j];
                    }
                    else
                    {
                        part = new ModelMeshPart();
                    }

                    part.VertexOffset   = reader.ReadInt32();
                    part.NumVertices    = reader.ReadInt32();
                    part.StartIndex     = reader.ReadInt32();
                    part.PrimitiveCount = reader.ReadInt32();

                    // Tag
                    part.Tag = reader.ReadObject <object>();

                    parts.Add(part);

                    int jj = (int)j;
                    reader.ReadSharedResource <VertexBuffer>(
                        delegate(VertexBuffer v)
                    {
                        parts[jj].VertexBuffer = v;
                    }
                        );
                    reader.ReadSharedResource <IndexBuffer>(
                        delegate(IndexBuffer v)
                    {
                        parts[jj].IndexBuffer = v;
                    }
                        );
                    reader.ReadSharedResource <Effect>(
                        delegate(Effect v)
                    {
                        parts[jj].Effect = v;
                    }
                        );
                }
                if (existingInstance != null)
                {
                    continue;
                }
                ModelMesh mesh = new ModelMesh(reader.GraphicsDevice, parts);
                mesh.Tag        = meshTag;
                mesh.Name       = name;
                mesh.ParentBone = bones[parentBoneIndex];
                mesh.ParentBone.AddMesh(mesh);
                mesh.BoundingSphere = boundingSphere;
                meshes.Add(mesh);
            }
            if (existingInstance != null)
            {
                // Read past remaining data and return existing instance
                ReadBoneReference(reader, boneCount);
                reader.ReadObject <object>();
                return(existingInstance);
            }
            // Read the final pieces of model data.
            int   rootBoneIndex = ReadBoneReference(reader, boneCount);
            Model model         = new Model(reader.GraphicsDevice, bones, meshes);

            model.Root = bones[rootBoneIndex];
            model.BuildHierarchy();
            // Tag?
            model.Tag = reader.ReadObject <object>();
            return(model);
        }
        internal static SkinnedModelBone Read(ContentReader input)
        {
            // Read bone data
            ushort index = input.ReadUInt16();
            string name = input.ReadString();

            // Read bind pose
            Pose bindPose;
            bindPose.Translation = input.ReadVector3();
            bindPose.Orientation = input.ReadQuaternion();
            bindPose.Scale = input.ReadVector3();

            Matrix inverseBindPoseTransform = input.ReadMatrix();
            SkinnedModelBone skinnedBone =
                new SkinnedModelBone(index, name, bindPose, inverseBindPoseTransform);

            // Read bone parent
            input.ReadSharedResource<SkinnedModelBone>(
                delegate(SkinnedModelBone parentBone) { skinnedBone.parent = parentBone; });

            // Read bone children
            int numChildren = input.ReadInt32();
            List<SkinnedModelBone> childrenList = new List<SkinnedModelBone>(numChildren);
            for (int i = 0; i < numChildren; i++)
            {
                input.ReadSharedResource<SkinnedModelBone>(
                    delegate(SkinnedModelBone childBone) { childrenList.Add(childBone); });
            }
            skinnedBone.children = new SkinnedModelBoneCollection(childrenList);

            return skinnedBone;
        }
예제 #4
0
        protected internal override Model Read(ContentReader reader, Model existingInstance)
        {
            List <ModelBone> bones     = new List <ModelBone>();
            uint             boneCount = reader.ReadUInt32();

            for (uint index = 0U; index < boneCount; ++index)
            {
                string    str       = reader.ReadObject <string>();
                Matrix    matrix    = reader.ReadMatrix();
                ModelBone modelBone = new ModelBone()
                {
                    Transform = matrix,
                    Index     = (int)index,
                    Name      = str
                };
                bones.Add(modelBone);
            }
            for (int index1 = 0; (long)index1 < (long)boneCount; ++index1)
            {
                ModelBone modelBone = bones[index1];
                int       index2    = ModelReader.ReadBoneReference(reader, boneCount);
                if (index2 != -1)
                {
                    modelBone.Parent = bones[index2];
                }
                uint num = reader.ReadUInt32();
                if ((int)num != 0)
                {
                    for (uint index3 = 0U; index3 < num; ++index3)
                    {
                        int index4 = ModelReader.ReadBoneReference(reader, boneCount);
                        if (index4 != -1)
                        {
                            modelBone.AddChild(bones[index4]);
                        }
                    }
                }
            }
            List <ModelMesh> meshes = new List <ModelMesh>();
            int num1 = reader.ReadInt32();

            for (int index1 = 0; index1 < num1; ++index1)
            {
                string         str            = reader.ReadObject <string>();
                int            index2         = ModelReader.ReadBoneReference(reader, boneCount);
                BoundingSphere boundingSphere = reader.ReadBoundingSphere();
                reader.ReadObject <object>();
                int num2 = reader.ReadInt32();
                List <ModelMeshPart> parts = new List <ModelMeshPart>();
                for (uint index3 = 0U; (long)index3 < (long)num2; ++index3)
                {
                    ModelMeshPart modelMeshPart = existingInstance == null ? new ModelMeshPart() : ((ReadOnlyCollection <ModelMesh>)existingInstance.Meshes)[index1].MeshParts[(int)index3];
                    modelMeshPart.VertexOffset   = reader.ReadInt32();
                    modelMeshPart.NumVertices    = reader.ReadInt32();
                    modelMeshPart.StartIndex     = reader.ReadInt32();
                    modelMeshPart.PrimitiveCount = reader.ReadInt32();
                    modelMeshPart.Tag            = reader.ReadObject <object>();
                    parts.Add(modelMeshPart);
                    int jj = (int)index3;
                    reader.ReadSharedResource <VertexBuffer>((Action <VertexBuffer>)(v => parts[jj].VertexBuffer = v));
                    reader.ReadSharedResource <IndexBuffer>((Action <IndexBuffer>)(v => parts[jj].IndexBuffer    = v));
                    reader.ReadSharedResource <Effect>((Action <Effect>)(v => parts[jj].Effect = v));
                }
                if (existingInstance == null)
                {
                    ModelMesh mesh = new ModelMesh(reader.GraphicsDevice, parts);
                    mesh.Name       = str;
                    mesh.ParentBone = bones[index2];
                    mesh.ParentBone.AddMesh(mesh);
                    mesh.BoundingSphere = boundingSphere;
                    meshes.Add(mesh);
                }
            }
            if (existingInstance != null)
            {
                ModelReader.ReadBoneReference(reader, boneCount);
                reader.ReadObject <object>();
                return(existingInstance);
            }
            else
            {
                int   index = ModelReader.ReadBoneReference(reader, boneCount);
                Model model = new Model(reader.GraphicsDevice, bones, meshes);
                model.Root = bones[index];
                model.BuildHierarchy();
                model.Tag = reader.ReadObject <object>();
                return(model);
            }
        }
예제 #5
0
 protected internal override BoundingFrustum Read(ContentReader input, BoundingFrustum existingInstance)
 {
     return(new BoundingFrustum(input.ReadMatrix()));
 }
        private void loadMD3Data(ContentReader input, MD3Object m)
        {
            //load meshes
            m.meshes = new List<MD3SubMeshes>();
            int part = input.ReadInt32();
            if (part == 0)
                m.part = TMD3Part.HEAD;
            else if (part == 1)
                m.part = TMD3Part.LOWER;
            else
                m.part = TMD3Part.UPPER;

            m.num_frames = input.ReadInt32();
            int sub_meshes_count = input.ReadInt32();
            for (int i = 0; i < sub_meshes_count; ++i)
            {
                MD3SubMeshes sub_mesh = new MD3SubMeshes();
                sub_mesh.indices = new List<int>();
                sub_mesh.vertices = new List<Vector3>();
                sub_mesh.normals = new List<Vector3>();
                sub_mesh.text_coord = new List<Vector2>();
                sub_mesh.skins = new List<string>();
                sub_mesh.meshinfo.strName = input.ReadString();
                input.ReadObject<List<int>>(sub_mesh.indices);
                input.ReadObject<List<Vector3>>(sub_mesh.vertices);
                input.ReadObject<List<Vector3>>(sub_mesh.normals);
                input.ReadObject<List<Vector2>>(sub_mesh.text_coord);
                m.meshes.Add(sub_mesh);
            }

            //load tags
            m.tags = new List<MD3tag>();
            int tags_count = input.ReadInt32();
            for (int i = 0; i < tags_count; ++i)
            {
                MD3tag tag = new MD3tag();
                tag.strName = input.ReadString();
                tag.vPosition = input.ReadVector3();
                tag.rotation = input.ReadMatrix();
                m.tags.Add(tag);
            }
            //load bounding_boxes
            m.bounding_boxes = new List<BoundingBox>();
            int bb_count = input.ReadInt32();
            for (int i = 0; i < bb_count; ++i)
            {
                BoundingBox bb = new BoundingBox();
                bb.Min = input.ReadVector3();
                bb.Max = input.ReadVector3();
                m.bounding_boxes.Add(bb);
            }
        }