public static MaterialEntry Read(BundleEntry entry)
        {
            MaterialEntry result = new MaterialEntry();

            List <BundleDependency> dependencies = entry.GetDependencies();

            foreach (BundleDependency dependency in dependencies)
            {
                ulong id = dependency.EntryID;

                //DebugTimer t = DebugTimer.Start("LoadDep");
                BundleEntry descEntry1 = entry.Archive.GetEntryByID(id);
                if (descEntry1 == null)
                {
                    string file = BundleCache.GetFileByEntryID(id);
                    if (!string.IsNullOrEmpty(file))
                    {
                        BundleArchive archive = BundleArchive.Read(file);
                        descEntry1 = archive.GetEntryByID(id);
                    }
                }
                //t.StopLog();

                //DebugTimer t2 = DebugTimer.Start("LoadTextureState");
                if (descEntry1 != null && descEntry1.Type == EntryType.TextureState)
                {
                    TextureState state = TextureState.Read(descEntry1);

                    result.TextureStates.Add(state);
                }
                //t2.StopLog();
            }

            MemoryStream  ms = entry.MakeStream();
            BinaryReader2 br = new BinaryReader2(ms);

            br.BigEndian = entry.Console;

            // TODO: Read Material

            br.Close();
            ms.Close();

            result.Color = Color.White;

            if (result.TextureStates.Count > 0)
            {
                result.DiffuseMap = result.TextureStates[0].Texture;
            }
            if (result.TextureStates.Count > 1)
            {
                result.NormalMap = result.TextureStates[1].Texture;
            }
            if (result.TextureStates.Count > 2)
            {
                result.SpecularMap = result.TextureStates[2].Texture;
            }

            return(result);
        }
Beispiel #2
0
        public bool Read(BundleEntry entry, ILoader loader)
        {
            Clear();

            List <BundleDependency> dependencies = entry.GetDependencies();

            ID = entry.ID;

            MemoryStream  ms = entry.MakeStream();
            BinaryReader2 br = new BinaryReader2(ms);

            br.BigEndian = entry.Console;

            Unknown1      = br.ReadSingle();
            Unknown2      = br.ReadSingle();
            Unknown3      = br.ReadSingle();
            Unknown4      = br.ReadSingle();
            Unknown5      = br.ReadInt16();
            MeshCount     = br.ReadInt16();
            StartOffset   = br.ReadInt32();
            Unknown8      = br.ReadInt32();
            Unknown9      = br.ReadInt32();
            Unknown10     = br.ReadInt16();
            Unknown10_1   = br.ReadInt16();
            UnknownOffset = br.ReadInt32();
            Unknown12     = br.ReadInt32();
            Unknown13     = br.ReadInt32();

            br.BaseStream.Position = StartOffset;

            for (int i = 0; i < MeshCount; i++)
            {
                int offset = br.ReadInt32();
                MeshVertexOffsets.Add(offset);
            }

            /*if (entry.Platform == BundlePlatform.PS3)
             * {
             *  br.BaseStream.Position += 16 - (br.BaseStream.Position % 16);
             *  result.NumIndices = br.ReadInt16();
             *  result.Unknown = br.ReadInt16();
             * }
             * else
             * {*/
            NumIndices = br.ReadInt32();
            //}

            Unknown15 = br.ReadInt32();
            Unknown16 = br.ReadInt32();
            Unknown17 = br.ReadInt32();
            // some extra data here in BPR

            br.BaseStream.Position = UnknownOffset;
            if (NumIndices == 0) // BPR
            {
                // ???
                br.BaseStream.Position += 12;
            }
            VertexBlockAddress = br.ReadInt32();
            Unknown18          = (NumIndices == 0) ? 0 : br.ReadInt32();
            VertexBlockSize    = br.ReadInt32();


            for (int i = 0; i < MeshCount; i++)
            {
                br.BaseStream.Position = MeshVertexOffsets[i];

                RenderableMesh mesh = new RenderableMesh();

                mesh.RotationMatrix   = br.ReadMatrix4();
                mesh.Unknown19        = br.ReadInt32();
                mesh.Unknown20        = br.ReadInt32();
                mesh.IndexOffsetCount = br.ReadInt32();
                if (NumIndices == 0) // BPR
                {
                    mesh.NumFaces = br.ReadInt32() / 3;
                }
                else
                {
                    mesh.NumVertices       = br.ReadInt32();
                    mesh.VertexOffsetCount = br.ReadInt32();
                    mesh.VertexOffsetCount = 0;
                    mesh.NumFaces          = br.ReadInt32();
                }
                int cPos = (int)br.BaseStream.Position;
                mesh.MaterialIDInternal = br.ReadInt32();
                foreach (BundleDependency dependency in dependencies)
                {
                    if (dependency.EntryPointerOffset == cPos)
                    {
                        mesh.MaterialID = dependency.EntryID;
                        break;
                    }
                }
                mesh.Unknown21         = br.ReadInt16();
                mesh.Unknown22         = br.ReadInt16();
                mesh.NumIndicesOffset  = br.ReadInt32();
                mesh.VerticesOffsetPtr = br.ReadInt32();

                mesh.VertexDescriptionsInternal = new int[6];
                mesh.VertexDescriptionIDs       = new ulong[6];
                for (int j = 0; j < mesh.VertexDescriptionsInternal.Length; j++)
                {
                    int pos = (int)br.BaseStream.Position;
                    foreach (BundleDependency dependency in dependencies)
                    {
                        if (dependency.EntryPointerOffset == pos)
                        {
                            mesh.VertexDescriptionIDs[j] = dependency.EntryID;
                            break;
                        }
                    }

                    mesh.VertexDescriptionsInternal[j] = br.ReadInt32();
                }

                Meshes.Add(mesh);
            }

            br.Close();
            ms.Close();

            loader?.SetStatus("Loading Meshes");
            for (int i = 0; i < Meshes.Count; i++)
            {
                RenderableMesh mesh = Meshes[i];

                int progress = (i + 1) * 100 / Meshes.Count;

                loader?.SetStatus("Loading Meshes: " + (i + 1) + "/" + Meshes.Count);
                loader?.SetProgress(progress);

                if (LoadMaterials)
                {
                    BundleEntry descEntry1 = entry.Archive.GetEntryByID(mesh.MaterialID);
                    if (descEntry1 == null)
                    {
                        string file = BundleCache.GetFileByEntryID(mesh.MaterialID);
                        if (!string.IsNullOrEmpty(file))
                        {
                            BundleArchive archive = BundleArchive.Read(file);
                            descEntry1 = archive.GetEntryByID(mesh.MaterialID);
                        }
                    }

                    if (descEntry1 != null)
                    {
                        mesh.Material = MaterialEntry.Read(descEntry1);
                    }
                }

                mesh.VertexDescriptions = new VertexDesc[6];
                for (int j = 0; j < mesh.VertexDescriptions.Length; j++)
                {
                    ulong       vertexDescID = mesh.VertexDescriptionIDs[j];
                    BundleEntry descEntry    = entry.Archive.GetEntryByID(vertexDescID);
                    if (descEntry == null)
                    {
                        string file = BundleCache.GetFileByEntryID(vertexDescID);
                        if (!string.IsNullOrEmpty(file))
                        {
                            BundleArchive archive = BundleArchive.Read(file);
                            descEntry = archive.GetEntryByID(vertexDescID);
                        }
                    }

                    if (descEntry != null)
                    {
                        mesh.VertexDescriptions[j] = VertexDesc.Read(descEntry);
                    }
                }
            }

            ms           = entry.MakeStream(true);
            br           = new BinaryReader2(ms);
            br.BigEndian = entry.Console;
            ReadBody(br);
            br.Close();
            ms.Close();

            BuildModel();

            _scene = MakeScene(loader);

            return(true);
        }