public void BuildModel() { Mesh[] meshes = new Mesh[Meshes.Count]; for (int i = 0; i < meshes.Length; i++) { RenderableMesh rMesh = Meshes[i]; Mesh mesh = new Mesh(); if (rMesh.Material != null) { Color color = rMesh.Material.Color; Image diffuse = rMesh.Material.DiffuseMap; Image normal = rMesh.Material.NormalMap; Image specular = rMesh.Material.SpecularMap; mesh.Material = new Material(rMesh.MaterialID.ToString("X8"), color, diffuse, normal, specular); } mesh.Indices = rMesh.Indices; for (int j = 0; j < rMesh.Vertices.Count; j++) { VertexData data = rMesh.Vertices[j]; mesh.Vertices.Add(data.Vertex); mesh.Normals.Add(data.Normal1); mesh.UV1.Add(data.UV1); } meshes[i] = mesh; } Model = new Model(meshes); }
public void ReadBody(BinaryReader br) { for (int i = 0; i < NumIndices; i++) { Indices.Add(br.ReadInt16()); } for (int i = 0; i < Meshes.Count; i++) { RenderableMesh mesh = Meshes[i]; br.BaseStream.Position = mesh.IndexOffset; mesh.Indices = new List <uint>(); for (int j = 0; j < mesh.IndexCount; j++) { uint index = br.ReadUInt16() - (uint)mesh.VertexOffsetCount; mesh.Indices.Add(index); } VertexAttribute attributeToUse = new VertexAttribute(); foreach (VertexDesc desc in mesh.VertexDescriptions) { if (desc == null) { continue; } foreach (VertexAttribute attribute in desc.Attributes) { if (attribute.Type != VertexAttributeType.Positions) { continue; // TODO } attributeToUse = attribute; // This is just wrong } } if (attributeToUse.Size == 0) { break; } mesh.Vertices = new List <VertexData>(); br.BaseStream.Position = VertexBlockAddress + mesh.VertexOffsetCount * attributeToUse.Size + attributeToUse.Offset; while (br.BaseStream.Position + attributeToUse.Size < br.BaseStream.Length) // Really, this should not be done like this. { mesh.Vertices.Add(ReadVertex(br, attributeToUse)); } } }
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); }