Ejemplo n.º 1
0
        public static BoneMap LoadFromStream(Stream stream)
        {
            if (stream == null)
            {
                return(null);
            }

            BinaryReader binaryReader = new BinaryReader(stream);

            BoneMap boneMap = new BoneMap();

            boneMap.Unknown0    = binaryReader.ReadUInt32();
            boneMap.BoneStart   = binaryReader.ReadUInt32();
            boneMap.BoneCount   = binaryReader.ReadUInt32();
            boneMap.Delta       = binaryReader.ReadUInt32();
            boneMap.Unknown1    = binaryReader.ReadUInt32();
            boneMap.BoneEnd     = binaryReader.ReadUInt32();
            boneMap.VertexCount = binaryReader.ReadUInt32();
            boneMap.Unknown2    = binaryReader.ReadUInt32();
            boneMap.IndexCount  = binaryReader.ReadUInt32();

            return(boneMap);
        }
Ejemplo n.º 2
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);
        }