protected void LoadModelExtras() { var mode = this; #region Mesh Merging foreach (var reg in mode.Regions) { foreach (var perm in reg.Permutations) { if (perm.PieceCount < 2) continue; var firstPart = mode.ModelSections[perm.PieceIndex]; if (firstPart.Submeshes.Count == 0) continue; var verts = firstPart.Vertices.ToList(); var indices = firstPart.Indices.ToList(); for (int i = 1; i < perm.PieceCount; i++) { var nextPart = mode.ModelSections[perm.PieceIndex + i]; foreach (var mesh in nextPart.Submeshes) { firstPart.Submeshes.Add(mesh); mesh.FaceIndex += indices.Count; } for (int j = 0; j < nextPart.Indices.Length; j++) nextPart.Indices[j] += verts.Count; verts.AddRange(nextPart.Vertices); indices.AddRange(nextPart.Indices); nextPart.UnloadRaw(); //save memory seeing as it wont be used now } firstPart.Vertices = verts.ToArray(); firstPart.Indices = indices.ToArray(); } } #endregion #region Mesh Splitting if (mode.InstancedGeometryIndex == -1) return; var part = mode.ModelSections[mode.InstancedGeometryIndex]; var list = new List<mode.ModelSection>(); for (int i = 0; i < part.Submeshes.Count; i++) { var submesh = part.Submeshes[i]; for (int j = submesh.SubsetIndex; j < (submesh.SubsetIndex + submesh.SubsetCount); j++) { var set = part.Subsets[j]; var vList = ModelFunctions.GetTriangleList(part.Indices, set.FaceIndex, set.FaceCount, mode.IndexInfoList[part.FacesIndex].FaceFormat); var newStrip = vList.ToArray(); var min = vList.Min(); var max = vList.Max(); //adjust faces to start at 0, seeing as //we're going to use a new set of vertices for (int k = 0; k < newStrip.Length; k++) newStrip[k] -= min; var verts = new Vertex[(max - min) + 1]; for (int k = 0; k < verts.Length; k++) //need to deep clone in case the vertices need to be verts[k] = (Vertex)ModelFunctions.DeepClone(part.Vertices[k + min]); //transformed, so it doesnt transform all instances #region Make new instances var newPart = new mode.ModelSection() { Vertices = verts, Indices = newStrip, Submeshes = new List<mode.ModelSection.Submesh>(), Subsets = new List<mode.ModelSection.Subset>(), VertexFormat = 1, OpaqueNodesPerVertex = part.OpaqueNodesPerVertex, NodeIndex = mode.GeomInstances[j].NodeIndex, VertsIndex = mode.VertInfoList.Count, FacesIndex = mode.IndexInfoList.Count }; mode.VertInfoList.Add(new mode.VertexBufferInfo() { VertexCount = verts.Length //dont need the rest }); mode.IndexInfoList.Add(new mode.IndexBufferInfo() { FaceFormat = 3 //dont need the rest }); var newMesh = new mode.ModelSection.Submesh() { SubsetCount = 1, SubsetIndex = 0, FaceIndex = 0, FaceCount = newStrip.Length, ShaderIndex = submesh.ShaderIndex, VertexCount = verts.Length }; var newSet = new mode.ModelSection.Subset() { SubmeshIndex = 0, FaceIndex = 0, FaceCount = newStrip.Length, VertexCount = verts.Length }; #endregion newPart.Submeshes.Add(newMesh); newPart.Subsets.Add(newSet); list.Add(newPart); } } //clear raw to save memory seeing as it wont be used anymore mode.ModelSections[mode.InstancedGeometryIndex].UnloadRaw(); mode.ModelSections.AddRange(list.ToArray()); var newRegion = new mode.Region() { Name = "Instances", Permutations = new List<mode.Region.Permutation>() }; for (int i = 0; i < list.Count; i++) { var newPerm = new mode.Region.Permutation() { Name = mode.GeomInstances[i].Name, PieceIndex = mode.InstancedGeometryIndex + i + 1, PieceCount = 1 }; newRegion.Permutations.Add(newPerm); } for (int i = 0; i < newRegion.Permutations.Count; i++) { var modelPart = mode.ModelSections[newRegion.Permutations[i].PieceIndex]; var instance = mode.GeomInstances[i]; //negative scale flips the faces after transform, fix it if (instance.TransformScale < 0) Array.Reverse(modelPart.Indices); for (int j = 0; j < modelPart.Vertices.Length; j++) { var vert = modelPart.Vertices[j]; VertexValue p, n, v, w; vert.TryGetValue("position", 0, out p); vert.TryGetValue("normal", 0, out n); p.Data *= instance.TransformScale; p.Data.Point3DTransform(instance.TransformMatrix); n.Data *= instance.TransformScale; n.Data.Vector3DTransform(instance.TransformMatrix); if (vert.TryGetValue("blendindices", 0, out v)) v.Data = new Vector(instance.NodeIndex, 0, 0, 0); else vert.Values.Add(new VertexValue(new Vector(instance.NodeIndex, 0, 0, 0), VertexValue.ValueType.Int8_N4, "blendindices", 0)); if (vert.TryGetValue("blendweight", 0, out w)) w.Data = new Vector(instance.NodeIndex, 0, 0, 0); else vert.Values.Add(new VertexValue(new Vector(1, 0, 0, 0), VertexValue.ValueType.Int8_N4, "blendweight", 0)); } } mode.Regions.Add(newRegion); #endregion }
public override void LoadRaw() { if (RawLoaded) return; #region Clusters for (int i = 0; i < Clusters.Count; i++) { var section = (Halo2Xbox.scenario_structure_bsp.ModelSection)ModelSections[i]; if (section.rSize.Length == 0 || section.vertcount == 0) { IndexInfoList.Add(new mode.IndexBufferInfo()); continue; } var data = cache.GetRawFromID(section.rawOffset, section.rawSize); var ms = new MemoryStream(data); var reader = new EndianReader(ms, Endian.EndianFormat.LittleEndian); #region Read Submeshes for (int j = 0; j < section.rSize[0] / 72; j++) { var mesh = new mode.ModelSection.Submesh(); reader.SeekTo(section.hSize + section.rOffset[0] + j * 72 + 4); mesh.ShaderIndex = reader.ReadUInt16(); mesh.FaceIndex = reader.ReadUInt16(); mesh.FaceCount = reader.ReadUInt16(); section.Submeshes.Add(mesh); } #endregion #region Get Resource Indices int iIndex = 0, vIndex = 0, uIndex = 0, nIndex = 0, bIndex = 0; for (int j = 0; j < section.rType.Length; j++) { switch (section.rType[j] & 0x0000FFFF) { case 32: iIndex = j; break; case 56: switch ((section.rType[j] & 0xFFFF0000) >> 16) { case 0: vIndex = j; break; case 1: uIndex = j; break; case 2: nIndex = j; break; } break; case 100: bIndex = j; break; } } #endregion section.Vertices = new Vertex[section.vertcount]; for (int j = 0; j < section.vertcount; j++) { reader.SeekTo(section.hSize + section.rOffset[vIndex] + ((section.rSize[vIndex] / section.vertcount) * j)); var v = new Vertex(); var p = new Vector(reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle()); v.Values.Add(new VertexValue(p, 0, "position", 0)); section.Vertices[j] = v; } #region Read UVs and Normals for (int j = 0; j < section.vertcount; j++) { reader.SeekTo(section.hSize + section.rOffset[uIndex] + (8 * j)); var v = section.Vertices[j]; var uv = new Vector(reader.ReadSingle(), 1 - reader.ReadSingle()); v.Values.Add(new VertexValue(uv, 0, "texcoords", 0)); } for (int j = 0; j < section.vertcount; j++) { reader.SeekTo(section.hSize + section.rOffset[uIndex + 1] + (12 * j)); var v = section.Vertices[j]; var n = Vector.FromHenDN3(reader.ReadUInt32()); v.Values.Add(new VertexValue(n, 0, "normal", 0)); } #endregion reader.SeekTo(40); section.Indices = new int[reader.ReadUInt16()]; reader.SeekTo(section.hSize + section.rOffset[iIndex]); for (int j = 0; j < section.Indices.Length; j++) section.Indices[j] = reader.ReadUInt16(); var facetype = 5; if (section.facecount * 3 == section.Indices.Length) facetype = 3; IndexInfoList.Add(new mode.IndexBufferInfo() { FaceFormat = facetype }); } #endregion #region Instances if (GeomInstances.Count == 0) { RawLoaded = true; return; } for (int i = GeomInstances[0].SectionIndex; i < ModelSections.Count; i++) { var section = (Halo2Xbox.scenario_structure_bsp.ModelSection)ModelSections[i]; var geomIndex = i - Clusters.Count; if (section.rSize.Length == 0 || section.vertcount == 0) { IndexInfoList.Add(new mode.IndexBufferInfo()); continue; } var data = cache.GetRawFromID(section.rawOffset, section.rawSize); var ms = new MemoryStream(data); var reader = new EndianReader(ms, Endian.EndianFormat.LittleEndian); #region Read Submeshes for (int j = 0; j < section.rSize[0] / 72; j++) { var mesh = new mode.ModelSection.Submesh(); reader.SeekTo(section.hSize + section.rOffset[0] + j * 72 + 4); mesh.ShaderIndex = reader.ReadUInt16(); mesh.FaceIndex = reader.ReadUInt16(); mesh.FaceCount = reader.ReadUInt16(); section.Submeshes.Add(mesh); } #endregion #region Get Resource Indices int iIndex = 0, vIndex = 0, uIndex = 0, nIndex = 0, bIndex = 0; for (int j = 0; j < section.rType.Length; j++) { switch (section.rType[j] & 0x0000FFFF) { case 32: iIndex = j; break; case 56: switch ((section.rType[j] & 0xFFFF0000) >> 16) { case 0: vIndex = j; break; case 1: uIndex = j; break; case 2: nIndex = j; break; } break; case 100: bIndex = j; break; } } #endregion section.Vertices = new Vertex[section.vertcount]; for (int j = 0; j < section.vertcount; j++) { reader.SeekTo(section.hSize + section.rOffset[vIndex] + ((section.rSize[vIndex] / section.vertcount) * j)); var v = new Vertex(); var p = new Vector(reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle()); v.Values.Add(new VertexValue(p, 0, "position", 0)); section.Vertices[j] = v; } #region Read UVs and Normals for (int j = 0; j < section.vertcount; j++) { reader.SeekTo(section.hSize + section.rOffset[uIndex] + (8 * j)); var v = section.Vertices[j]; var uv = new Vector(reader.ReadSingle(), 1 - reader.ReadSingle()); v.Values.Add(new VertexValue(uv, 0, "texcoords", 0)); //DecompressVertex(ref v, BoundingBoxes[geomIndex]); } for (int j = 0; j < section.vertcount; j++) { reader.SeekTo(section.hSize + section.rOffset[nIndex] + (12 * j)); var v = section.Vertices[j]; var n = Vector.FromHenDN3(reader.ReadUInt32()); v.Values.Add(new VertexValue(n, 0, "normal", 0)); } #endregion reader.SeekTo(40); section.Indices = new int[reader.ReadUInt16()]; reader.SeekTo(section.hSize + section.rOffset[iIndex]); for (int j = 0; j < section.Indices.Length; j++) section.Indices[j] = reader.ReadUInt16(); var facetype = 5; if (section.facecount * 3 == section.Indices.Length) facetype = 3; IndexInfoList.Add(new mode.IndexBufferInfo() { FaceFormat = facetype }); } #endregion RawLoaded = true; }