Esempio n. 1
0
        public static Model LoadFromStream(string name, string displayName, Stream stream)
        {
            BinaryReader binaryReader = new BinaryReader(stream);

            //Header
            byte[] magic = binaryReader.ReadBytes(4);

            if (magic[0] != 'D' ||
                magic[1] != 'M' ||
                magic[2] != 'O' ||
                magic[3] != 'D')
            {
                return(null);
            }
            Model model = new Model();

            model.Version = binaryReader.ReadUInt32();

            if (!Enum.IsDefined(typeof(ModelType), (int)model.Version))
            {
                Debug.LogWarning("Could not decode model " + name + ". Unknown DME version " + model.Version);
                return(null);
            }

            model.ModelType = (ModelType)model.Version;

            model.Name        = name;
            model.DisplayName = displayName;

            model.ModelHeaderOffset = binaryReader.ReadUInt32();

            //DMA
            model.TextureStrings = new List <string>();
            model.Materials      = new List <Material>();
            Dma.LoadFromStream(binaryReader.BaseStream, model.TextureStrings, model.Materials);

            //Bounding Box
            model.Min = new Vector3(binaryReader.ReadSingle(), binaryReader.ReadSingle(), binaryReader.ReadSingle());
            model.Max = new Vector3(binaryReader.ReadSingle(), binaryReader.ReadSingle(), binaryReader.ReadSingle());

            //Meshes
            uint meshCount = binaryReader.ReadUInt32();

            model.Meshes = new List <Mesh>((int)meshCount);

            for (int i = 0; i < meshCount; ++i)
            {
                Mesh mesh = Mesh.LoadFromStream(binaryReader.BaseStream, model.Materials);

                if (mesh == null)
                {
                    continue;
                }

                Material material = model.Materials[(int)mesh.MaterialIndex];
                foreach (Material.Parameter parameter in material.Parameters)
                {
                    LookupTextures(mesh, parameter, model.TextureStrings);

                    if (mesh.BaseDiffuse != null && mesh.BumpMap != null && mesh.SpecMap != null)
                    {
                        break;
                    }
                }

                model.Meshes.Add(mesh);
            }

            //Bone Maps
            uint boneMapCount = binaryReader.ReadUInt32();

            model.BoneMaps = new List <BoneMap>((int)boneMapCount);

            for (int i = 0; i < boneMapCount; ++i)
            {
                BoneMap boneMap = BoneMap.LoadFromStream(binaryReader.BaseStream);

                if (boneMap != null)
                {
                    model.BoneMaps.Add(boneMap);
                }
            }

            //Bone Map Entries
            uint boneMapEntryCount = binaryReader.ReadUInt32();

            model.BoneMapEntries = new List <BoneMapEntry>((int)boneMapEntryCount);

            for (int i = 0; i < boneMapEntryCount; ++i)
            {
                BoneMapEntry boneMapEntry = BoneMapEntry.LoadFromStream(binaryReader.BaseStream);

                if (boneMapEntry != null)
                {
                    model.BoneMapEntries.Add(boneMapEntry);
                }
            }

            return(model);
        }
Esempio n. 2
0
        public bool Deserialize(BinaryStream stream, AssetManager assetManager)
        {
            // Header
            string magic = stream.ReadString(4);

            Assert.AreEqual(MAGIC, magic, "Model File header does not match magic value!");

            Version = stream.ReadUInt32();

            if (!Enum.IsDefined(typeof(ModelType), (int)Version))
            {
                Debug.LogWarning("Could not decode model " + Name + ". Unknown DME version " + Version);
                return(false);
            }

            ModelType = (ModelType)Version;

            ModelHeaderOffset = stream.ReadUInt32();

            // DMA
            Dma.LoadFromStream(stream, TextureStrings, Materials);

            // Bounding Box
            Min = stream.ReadVector3();
            Max = stream.ReadVector3();

            // Meshes
            uint meshCount = stream.ReadUInt32();

            for (int i = 0; i < meshCount; ++i)
            {
                Mesh mesh = Mesh.LoadFromStream(stream, Materials);

                if (mesh == null)
                {
                    continue;
                }

                Material material = Materials[(int)mesh.MaterialIndex];
                foreach (Material.Parameter parameter in material.Parameters)
                {
                    LookupTextures(mesh, parameter, TextureStrings);

                    if (mesh.BaseDiffuse != null && mesh.BumpMap != null && mesh.SpecMap != null)
                    {
                        break;
                    }
                }

                Meshes.Add(mesh);
            }

            // Bone Maps
            uint boneMapCount = stream.ReadUInt32();

            for (int i = 0; i < boneMapCount; ++i)
            {
                BoneMap boneMap = BoneMap.LoadFromStream(stream);

                if (boneMap != null)
                {
                    BoneMaps.Add(boneMap);
                }
            }

            // Bone Map Entries
            uint boneMapEntryCount = stream.ReadUInt32();

            for (int i = 0; i < boneMapEntryCount; ++i)
            {
                BoneMapEntry boneMapEntry = BoneMapEntry.LoadFromStream(stream);

                if (boneMapEntry != null)
                {
                    BoneMapEntries.Add(boneMapEntry);
                }
            }

            return(true);
        }
        public bool InitializeFromStream(string name, string displayName, Stream stream)
        {
            using (BinaryReader binaryReader = new BinaryReader(stream))
            {
                //Header
                byte[] magic = binaryReader.ReadBytes(4);

                if (magic[0] != 'D' ||
                    magic[1] != 'M' ||
                    magic[2] != 'O' ||
                    magic[3] != 'D')
                {
                    return(false);
                }

                Version = binaryReader.ReadUInt32();

                if (!Enum.IsDefined(typeof(ModelType), (int)Version))
                {
                    Debug.LogWarning("Could not decode model " + name + ". Unknown DME version " + Version);
                    return(false);
                }

                ModelType = (ModelType)Version;

                Name        = name;
                DisplayName = displayName;

                ModelHeaderOffset = binaryReader.ReadUInt32();

                //DMA
                Dma.LoadFromStream(binaryReader.BaseStream, TextureStrings, Materials);

                //Bounding Box
                Min = new Vector3(binaryReader.ReadSingle(), binaryReader.ReadSingle(), binaryReader.ReadSingle());
                Max = new Vector3(binaryReader.ReadSingle(), binaryReader.ReadSingle(), binaryReader.ReadSingle());

                //Meshes
                uint meshCount = binaryReader.ReadUInt32();

                for (int i = 0; i < meshCount; ++i)
                {
                    Mesh mesh = Mesh.LoadFromStream(binaryReader.BaseStream, Materials);

                    if (mesh == null)
                    {
                        continue;
                    }

                    Material material = Materials[(int)mesh.MaterialIndex];
                    foreach (Material.Parameter parameter in material.Parameters)
                    {
                        LookupTextures(mesh, parameter, TextureStrings);

                        if (mesh.BaseDiffuse != null && mesh.BumpMap != null && mesh.SpecMap != null)
                        {
                            break;
                        }
                    }

                    Meshes.Add(mesh);
                }

                //Bone Maps
                uint boneMapCount = binaryReader.ReadUInt32();

                for (int i = 0; i < boneMapCount; ++i)
                {
                    BoneMap boneMap = BoneMap.LoadFromStream(binaryReader.BaseStream);

                    if (boneMap != null)
                    {
                        BoneMaps.Add(boneMap);
                    }
                }

                //Bone Map Entries
                uint boneMapEntryCount = binaryReader.ReadUInt32();

                for (int i = 0; i < boneMapEntryCount; ++i)
                {
                    BoneMapEntry boneMapEntry = BoneMapEntry.LoadFromStream(binaryReader.BaseStream);

                    if (boneMapEntry != null)
                    {
                        BoneMapEntries.Add(boneMapEntry);
                    }
                }

                return(true);
            }
        }