public IQueryable <PostDetailModel> GetByTags(string tags, string sessionKey) { var responseMsg = this.PerformOperationAndHandleExceptions( () => { VerifySessionKey(sessionKey); if (tags == null) { throw new InvalidOperationException("Incorrect tags data"); } string[] tagsConstraintSplit = tags.ToLower().Split(new string[] { "," }, StringSplitOptions.None); if (tagsConstraintSplit.Length == 0) { throw new InvalidOperationException("Incorrect tags data"); } HashSet <string> tagsConstraint = new HashSet <string>(); tagsConstraint.UnionWith(tagsConstraintSplit); IRepository <Post> postRepository = this.data.GetPostsRepository(); IQueryable <Post> posts = postRepository.GetConstraint(p => p.Tags.Select(t => t.Name).Intersect(tagsConstraint).Count() == tagsConstraint.Count()); IQueryable <PostDetailModel> postsModels = ModelFunctions.GetPostsDetails(posts); return(postsModels.OrderByDescending(p => p.PostDate)); }); return(responseMsg); }
private bool validateSales() { try { sale = new Sales(); if (selectedCustomer != null) { sale.customerId = selectedCustomer.id; } sale.date = new DateTime(); sale.transactionNumber = ModelFunctions.getNextReferenceNumber("sale", "transaction_number"); sale.officialReceipt = ModelFunctions.getNextReferenceNumber("sale", "official_receipt"); sale.invoiceNumber = ModelFunctions.getNextReferenceNumber("sale", "invoicenumber"); sale.totalGross = 1000; return(true); } catch (Exception err) { MessageBox.Show(err.ToString()); return(false); } }
public IQueryable <PostDetailModel> GetPosts(string sessionKey, int id) { var responseMsg = this.PerformOperationAndHandleExceptions( () => { VerifySessionKey(sessionKey); IRepository <Tag> tagRepository = this.data.GetTagsRepository(); Tag tag = tagRepository.Get(id); if (tag == null) { throw new InvalidOperationException("No such tag"); } IRepository <Post> postRepository = this.data.GetPostsRepository(); IQueryable <Post> posts = postRepository.GetConstraint(p => p.Tags.Any(t => t.TagId == id)); IQueryable <PostDetailModel> postsModels = ModelFunctions.GetPostsDetails(posts); return(postsModels.OrderByDescending(p => p.PostDate)); }); return(responseMsg); }
private void AddS3DMesh(Model3DGroup group, Node obj, Node.Submesh submesh, bool force) { try { var geom = new MeshGeometry3D(); int[] indcs = obj.Indices.Data; Vertex[] vrtcs = obj.Vertices.Data; var iList = ModelFunctions.GetTriangleList(indcs, submesh.FaceStart * 3, submesh.FaceLength * 3, 3); int min = iList.Min(); int max = iList.Max(); for (int i = 0; i < iList.Count; i++) { iList[i] -= min; } var vArray = new Vertex[(max - min) + 1]; Array.Copy(ModelFunctions.DeepClone(vrtcs), min, vArray, 0, (max - min) + 1); foreach (var vertex in vArray) { if (vertex == null) { continue; } VertexValue pos, tex, norm; vertex.TryGetValue("position", 0, out pos); vertex.TryGetValue("texcoords", 0, out tex); var texSize = (obj._2F01 != null) ? obj._2F01.unkC0 : 1; geom.Positions.Add(new Point3D(pos.Data.x, pos.Data.y, pos.Data.z)); geom.TextureCoordinates.Add(new System.Windows.Point(tex.Data.x * texSize, tex.Data.y * texSize)); if (vertex.TryGetValue("normal", 0, out norm)) { geom.Normals.Add(new Vector3D(norm.Data.x, norm.Data.y, norm.Data.z)); } } foreach (var index in iList) { geom.TriangleIndices.Add(index); } GeometryModel3D modeld = new GeometryModel3D(geom, shaders[submesh.MaterialIndex + 1]) { BackMaterial = shaders[submesh.MaterialIndex + 1] }; group.Children.Add(modeld); } catch (Exception ex) { if (!force) { throw ex; } } }
private void LoadS3DMeshes(bool force) { atplDic = new Dictionary <int, Model3DGroup>(); foreach (var obj in atpl.Objects) { if (obj.isInherited || obj._2E01 == null) { continue; } try { var group = new Model3DGroup(); foreach (var submesh in obj.Submeshes) { AddS3DMesh(group, obj, submesh, force); } var mGroup = new Transform3DGroup(); var pObj = (obj._2901 == null) ? obj : atpl.ObjectByID(obj._2901.InheritID); Matrix3D mat0 = ModelFunctions.MatrixFromBounds(obj.BoundingBox.Data); Matrix3D mat1 = (obj.isInheritor) ? ModelFunctions.MatrixFromBounds(pObj.BoundingBox.Data) : mat0; Matrix3D mat2 = ModelFunctions.MatrixFromBounds(atpl.RenderBounds); var mat3 = new Matrix3D(1, 0, 0, 0, 0, 0, 1, 0, 0, -1, 0, 0, 0, 0, 0, 1); var mat4 = new Matrix3D(600, 0, 0, 0, 0, 600, 0, 0, 0, 0, 600, 0, 0, 0, 0, 1); var matX = Matrix3D.Identity; matX.M11 = matX.M22 = matX.M33 = pak.SceneCDT.sets[0].unkf0; //matX.OffsetX = pak.SceneCDT.sets[0].MinBound.x; //matX.OffsetY = pak.SceneCDT.sets[0].MinBound.y; //matX.OffsetZ = pak.SceneCDT.sets[0].MinBound.z; var dmat = new Matrix3D( 1f / 0xFFFF, 0, 0, 0, 0, 1f / 0xFFFF, 0, 0, 0, 0, 1f / 0xFFFF, 0, 0.5, 0.5, 0.5, 1 ); if (pObj.Vertices.Data[0].FormatName == "S3D_World") { dmat = mat0 = mat1 = mat4 = matX = Matrix3D.Identity; } mGroup.Children.Add(new MatrixTransform3D(dmat * mat4 * mat3)); group.Transform = mGroup; atplDic.Add(atpl.Objects.IndexOf(obj), group); } catch (Exception ex) { if (!force) { throw ex; } } } }
private void btnExportModel_Click(object sender, EventArgs e) { List <int> parts = new List <int>(); foreach (TreeNode parent in tvRegions.Nodes) { if (!parent.Checked) { continue; } foreach (TreeNode child in parent.Nodes) { if (!child.Checked) { continue; } if (cache != null) { parts.Add((child.Tag as render_model.Region.Permutation).PieceIndex); } else { parts.Add(atpl.Objects.IndexOf(child.Tag as Node)); } } } var sfd = new SaveFileDialog() { Filter = "EMF Files|*.emf|OBJ Files|*.obj|AMF Files|*.amf|JMS Files|*.jms", FilterIndex = (int)DefaultModeFormat + 1, FileName = (tag != null) ? tag.Filename.Substring(tag.Filename.LastIndexOf("\\") + 1) : atpl.Name }; if (sfd.ShowDialog() != DialogResult.OK) { return; } try { var format = (ModelFormat)(sfd.FilterIndex - 1); if (cache != null) { ModelExtractor.SaveModelParts(sfd.FileName, cache, mode, format, parts, false); TagExtracted(this, tag); } else { ModelFunctions.WriteAMF(sfd.FileName, pak, atpl, parts); TagExtracted(this, item); } } catch (Exception ex) { ErrorExtracting(this, (tag != null) ? (object)tag : (object)item, ex); } }
private void AddMesh(Model3DGroup group, render_model.ModelSection part, render_model.ModelSection.Submesh submesh, bool force) { try { var geom = new MeshGeometry3D(); var iList = ModelFunctions.GetTriangleList(part.Indices, submesh.FaceIndex, submesh.FaceCount, mode.IndexInfoList[part.FacesIndex].FaceFormat); int min = iList.Min(); int max = iList.Max(); for (int i = 0; i < iList.Count; i++) { iList[i] -= min; } var vArray = new Vertex[(max - min) + 1]; Array.Copy(part.Vertices.ToArray(), min, vArray, 0, (max - min) + 1); foreach (var vertex in vArray) { if (vertex == null) { continue; } VertexValue pos, tex, norm; vertex.TryGetValue("position", 0, out pos); vertex.TryGetValue("texcoords", 0, out tex); geom.Positions.Add(new Point3D(pos.Data.x, pos.Data.y, pos.Data.z)); geom.TextureCoordinates.Add(new System.Windows.Point(tex.Data.x, 1f - tex.Data.y)); if (vertex.TryGetValue("normal", 0, out norm)) { geom.Normals.Add(new Vector3D(norm.Data.x, norm.Data.y, norm.Data.z)); } } foreach (var index in iList) { geom.TriangleIndices.Add(index); } GeometryModel3D modeld = new GeometryModel3D(geom, shaders[submesh.ShaderIndex]) { BackMaterial = shaders[submesh.ShaderIndex] }; group.Children.Add(modeld); } catch (Exception ex) { if (!force) { throw ex; } } }
public void CreateMcPrecursorFact() { ContainerStores.GetObjectCache <IAssetFxModel>().PutObject("van", new SessionItem <IAssetFxModel>() { Name = "van", Value = new AssetFxModel(DateTime.Today, null) }); ContainerStores.GetObjectCache <McSettings>().PutObject("set", new SessionItem <McSettings>() { Name = "set", Value = new McSettings() }); Assert.Equal("boom¬0", ModelFunctions.CreateMcModel("boom", "van", "set")); }
private void AddS3DMesh(Model3DGroup group, Node obj, Node.Submesh submesh, bool force) { try { var pObj = (obj._2901 == null) ? obj : atpl.ObjectByID(obj._2901.InheritID); var iOffset = (obj._2901 == null) ? 0 : obj._2901.IndexOffset; var vOffset = (obj._2901 == null) ? 0 : obj._2901.VertexOffset; var geom = new MeshGeometry3D(); var iList = ModelFunctions.GetTriangleList(pObj.Indices.Data, (iOffset + submesh.FaceStart) * 3, submesh.FaceLength * 3, 3); var vArray = new Vertex[submesh.VertLength]; Array.Copy(pObj.Vertices.Data, (vOffset + submesh.VertStart), vArray, 0, submesh.VertLength); foreach (var vertex in vArray) { if (vertex == null) { continue; } VertexValue pos, tex, norm; vertex.TryGetValue("position", 0, out pos); vertex.TryGetValue("texcoords", 0, out tex); var texSize = (obj._2F01 != null) ? obj._2F01.unkC0 : 1; geom.Positions.Add(new Point3D(pos.Data.x, pos.Data.y, pos.Data.z)); geom.TextureCoordinates.Add(new System.Windows.Point(tex.Data.x * texSize, tex.Data.y * texSize)); if (vertex.TryGetValue("normal", 0, out norm)) { geom.Normals.Add(new Vector3D(norm.Data.x, norm.Data.y, norm.Data.z)); } } foreach (var index in iList) { geom.TriangleIndices.Add(index); } GeometryModel3D modeld = new GeometryModel3D(geom, shaders[submesh.MaterialIndex + 1]) { //BackMaterial = shaders[submesh.MaterialIndex + 1] }; group.Children.Add(modeld); } catch (Exception ex) { if (!force) { throw ex; } } }
/// <summary> /// Saves selected pieces of the model from a scenario_structure_bsp tag to disk. /// </summary> /// <param name="Filename">The full path and filename to save to.</param> /// <param name="Cache">The CacheFile containing the scenario_structure_bsp tag.</param> /// <param name="Tag">The scenario_structure_bsp tag.</param> /// <param name="Format">The format to save the model in.</param> /// <param name="ClusterIndices">A List containing the indices of the scenario_structure_bsp.Clusters to save.</param> /// <param name="InstanceIndices">A List containing the indices of the scenario_structure_bsp.GeomInstances to save.</param> public static void SaveBSPParts(string Filename, CacheBase Cache, scenario_structure_bsp BSP, ModelFormat Format, List<int> ClusterIndices, List<int> InstanceIndices) { switch (Format) { case ModelFormat.OBJ: ModelFunctions.WriteOBJ(Filename, Cache, BSP, ClusterIndices, InstanceIndices); break; case ModelFormat.EMF: ModelFunctions.WriteEMF3(Filename, Cache, BSP, ClusterIndices, InstanceIndices); break; default: ModelFunctions.WriteAMF(Filename, Cache, BSP, ClusterIndices, InstanceIndices); break; } }
public IQueryable <PostDetailModel> Get(string keyword, string sessionKey) { var responseMsg = this.PerformOperationAndHandleExceptions( () => { VerifySessionKey(sessionKey); IRepository <Post> postRepository = this.data.GetPostsRepository(); IQueryable <Post> posts = postRepository.GetConstraint(p => p.Title.ToLower().IndexOf(keyword.ToLower()) != -1); IQueryable <PostDetailModel> postsModels = ModelFunctions.GetPostsDetails(posts); return(postsModels.OrderByDescending(p => p.PostDate)); }); return(responseMsg); }
public PostDetailModel Get(string sessionKey, int id) { var responseMsg = this.PerformOperationAndHandleExceptions( () => { VerifySessionKey(sessionKey); IRepository <Post> postRepository = this.data.GetPostsRepository(); Post post = postRepository.Get(id); PostDetailModel postModel = ModelFunctions.GetSinglePostDetails(post); return(postModel); }); return(responseMsg); }
public IQueryable <PostDetailModel> Get(string sessionKey) { var responseMsg = this.PerformOperationAndHandleExceptions( () => { VerifySessionKey(sessionKey); IRepository <Post> postRepository = this.data.GetPostsRepository(); IQueryable <Post> posts = postRepository.All(); IQueryable <PostDetailModel> postsModels = ModelFunctions.GetPostsDetails(posts); return(postsModels.OrderByDescending(p => p.PostDate)); }); return(responseMsg); }
private void LoadS3DMeshes(bool force) { atplDic = new Dictionary <int, Model3DGroup>(); foreach (var obj in atpl.Objects) { if (obj._B903.VertCount == 0 /*&& obj.Submeshes == null*/) { continue; } //if (obj.BoundingBox.Length == 0) continue; var group = new Model3DGroup(); foreach (var submesh in obj.Submeshes) { AddS3DMesh(group, obj, submesh, force); } int xx = atpl.Objects.IndexOf(obj); Matrix3D mat0 = ModelFunctions.MatrixFromBounds(obj.BoundingBox.Data); Matrix3D mat1 = obj.Transform.Data; Matrix3D pMat = atpl.HierarchialTransformUp(obj); var dmat = new Matrix3D( 1f / 0xFFFF, 0, 0, 0, 0, 1f / 0xFFFF, 0, 0, 0, 0, 1f / 0xFFFF, 0, 0.5, 0.5, 0.5, 1 ); var mat2 = new Matrix3D(1, 0, 0, 0, 0, 0, 1, 0, 0, -1, 0, 0, 0, 0, 0, 1); var mat3 = new Matrix3D(100, 0, 0, 0, 0, 100, 0, 0, 0, 0, 0, 100, 0, 0, 0, 1); int id = obj.ParentID; var mGroup = new Transform3DGroup(); mGroup.Children.Add(new MatrixTransform3D(dmat * mat0 * pMat * mat2)); group.Transform = mGroup; atplDic.Add(atpl.Objects.IndexOf(obj), group); } }
/// <summary> /// Saves selected pieces of the model from a render_model tag to disk. /// </summary> /// <param name="Filename">The full path and filename to save to.</param> /// <param name="Cache">The CacheFile containing the render_model tag.</param> /// <param name="Tag">The render_model tag.</param> /// <param name="Format">The format to save the model in.</param> /// <param name="SplitMeshes">Whether to split the pieces into individual submeshes. Only applies when saving in EMF format.</param> /// <param name="PartIndices">A List containing the indices of the render_model.ModelParts to save.</param> public static void SaveModelParts(string Filename, CacheBase Cache, render_model Model, ModelFormat Format, List <int> PartIndices, bool SplitMeshes) { switch (Format) { case ModelFormat.EMF: ModelFunctions.WriteEMF3(Filename, Cache, Model, SplitMeshes, PartIndices); break; case ModelFormat.JMS: ModelFunctions.WriteJMS(Filename, Cache, Model, PartIndices); break; case ModelFormat.OBJ: ModelFunctions.WriteOBJ(Filename, Cache, Model, PartIndices); break; case ModelFormat.AMF: ModelFunctions.WriteAMF(Filename, Cache, Model, PartIndices); break; } }
public override void LoadRaw() { if (RawLoaded) { return; } for (int i = 0; i < ModelSections.Count; i++) { var section = (Halo2Xbox.render_model.ModelSection)ModelSections[i]; var data = cache.GetRawFromID(section.rawOffset, section.rawSize); var ms = new MemoryStream(data); var reader = new EndianReader(ms, IO.EndianFormat.Little); #region Read Submeshes for (int j = 0; j < section.rSize[0] / 72; j++) { var mesh = new 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 reader.SeekTo(40); section.Indices = new int[reader.ReadUInt16()]; section.Vertices = new Vertex[section.vertcount]; var facetype = 5; if (section.facecount * 3 == section.Indices.Length) { facetype = 3; } IndexInfoList.Add(new IndexBufferInfo() { FaceFormat = facetype }); VertInfoList.Add(new VertexBufferInfo() { VertexCount = section.vertcount }); #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 reader.SeekTo(108); int bCount = reader.ReadUInt16(); int[] bArr = new int[bCount]; if (bCount > 0) { reader.SeekTo(section.hSize + section.rOffset[bIndex]); for (int j = 0; j < bCount; j++) { bArr[j] = reader.ReadByte(); } } #region Read Vertices 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() { FormatName = "" }; var p = new Vector( ((float)reader.ReadInt16() + (float)0x7FFF) / (float)0xFFFF, ((float)reader.ReadInt16() + (float)0x7FFF) / (float)0xFFFF, ((float)reader.ReadInt16() + (float)0x7FFF) / (float)0xFFFF, 0); v.Values.Add(new VertexValue(p, 0, "position", 0)); var b = new Vector(); var w = new Vector(); switch (section.type) { case 1: switch (section.bones) { case 0: section.NodeIndex = 0; break; case 1: section.NodeIndex = (bCount > 0) ? bArr[0] : 0; break; } section.Vertices[j] = v; continue; case 2: switch (section.bones) { case 1: b = new Vector(reader.ReadByte(), reader.ReadByte(), 0, 0); w = new Vector(1, 0, 0, 0); break; } break; case 3: switch (section.bones) { case 2: reader.ReadInt16(); b = new Vector(reader.ReadByte(), reader.ReadByte(), 0, 0); w = new Vector((float)reader.ReadByte() / 255f, (float)reader.ReadByte() / 255f, 0, 0); break; case 3: b = new Vector(reader.ReadByte(), reader.ReadByte(), reader.ReadByte(), 0); w = new Vector((float)reader.ReadByte() / 255f, (float)reader.ReadByte() / 255f, (float)reader.ReadByte() / 255f, 0); break; case 4: reader.ReadInt16(); b = new Vector(reader.ReadByte(), reader.ReadByte(), reader.ReadByte(), reader.ReadByte()); w = new Vector((float)reader.ReadByte() / 255f, (float)reader.ReadByte() / 255f, (float)reader.ReadByte() / 255f, (float)reader.ReadByte() / 255f); break; } break; } if (bCount > 0) { b.A = (w.A == 0) ? 0 : bArr[(int)b.A]; b.B = (w.B == 0) ? 0 : bArr[(int)b.B]; b.C = (w.C == 0) ? 0 : bArr[(int)b.C]; b.D = (w.D == 0) ? 0 : bArr[(int)b.D]; } v.Values.Add(new VertexValue(b, 0, "blendindices", 0)); v.Values.Add(new VertexValue(w, 0, "blendweight", 0)); section.Vertices[j] = v; } #endregion #region Read UVs and Normals for (int j = 0; j < section.vertcount; j++) { reader.SeekTo(section.hSize + section.rOffset[uIndex] + (4 * j)); var v = section.Vertices[j]; var uv = new Vector(((float)reader.ReadInt16() + (float)0x7FFF) / (float)0xFFFF, ((float)reader.ReadInt16() + (float)0x7FFF) / (float)0xFFFF); v.Values.Add(new VertexValue(uv, 0, "texcoords", 0)); } 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 ModelFunctions.DecompressVertex(ref section.Vertices, BoundingBoxes[0]); reader.SeekTo(section.hSize + section.rOffset[iIndex]); for (int j = 0; j < section.Indices.Length; j++) { section.Indices[j] = reader.ReadUInt16(); } } RawLoaded = true; }
private void btnExportBSP_Click(object sender, EventArgs e) { var clusts = new List <int>(); var igs = new List <int>(); foreach (TreeNode pnode in tvRegions.Nodes) { if (!pnode.Checked) { continue; } foreach (TreeNode cnode in pnode.Nodes) { if (!cnode.Checked) { continue; } if (cnode.Tag is scenario_structure_bsp.Cluster) { var cluster = cnode.Tag as scenario_structure_bsp.Cluster; clusts.Add(sbsp.Clusters.IndexOf(cluster)); } else if (cnode.Tag is scenario_structure_bsp.InstancedGeometry) { var ig = cnode.Tag as scenario_structure_bsp.InstancedGeometry; igs.Add(sbsp.GeomInstances.IndexOf(ig)); } else if (cnode.Tag is Node) { var obj = cnode.Tag as Node; clusts.Add(atpl.Objects.IndexOf(obj)); } } } var sfd = new SaveFileDialog() { Filter = "EMF Files|*.emf|OBJ Files|*.obj|AMF Files|*.amf", FilterIndex = (int)DefaultModeFormat + 1, FileName = (tag != null) ? tag.Filename.Substring(tag.Filename.LastIndexOf("\\") + 1) : atpl.Name }; if (sfd.ShowDialog() != DialogResult.OK) { return; } try { var format = (ModelFormat)(sfd.FilterIndex - 1); if (cache != null) { BSPExtractor.SaveBSPParts(sfd.FileName, cache, sbsp, format, clusts, igs); TagExtracted(this, tag); } else { ModelFunctions.WriteAMF(sfd.FileName, pak, atpl, clusts); TagExtracted(this, item); } } catch (Exception ex) { ErrorExtracting(this, (tag != null) ? (object)tag : (object)item, ex); } }
public override void LoadRaw() { if (RawLoaded) return; var data = cache.GetRawFromID(geomRawID); var ms = new MemoryStream(data); var reader = new EndianReader(ms, EndianFormat.BigEndian); var validParts = new Dictionary<int, mode.ModelSection>(); LoadFixups(); #region Read Vertices for (int i = 0; i < ModelSections.Count; i++) { var section = ModelSections[i]; if (section.Submeshes.Count == 0) continue; if (section.VertsIndex >= 0 && section.VertsIndex < VertInfoList.Count) reader.SeekTo(VertInfoList[section.VertsIndex].Offset); if (cache.vertexNode == null) throw new NotSupportedException("No vertex definitions found for " + cache.Version.ToString()); #region Get Vertex Definition XmlNode formatNode = null; foreach (XmlNode node in cache.vertexNode.ChildNodes) { if (Convert.ToInt32(node.Attributes["type"].Value, 16) == section.VertexFormat) { formatNode = node; break; } } if (formatNode == null) throw new NotSupportedException("Format " + section.VertexFormat.ToString() + " not found in definition for " + cache.Version.ToString()); #endregion mode.ModelSection validPart; if (validParts.TryGetValue(section.VertsIndex, out validPart)) { section.Vertices = validPart.Vertices; continue; } else validParts.Add(section.VertsIndex, section); section.Vertices = new Vertex[VertInfoList[section.VertsIndex].VertexCount]; #region Get Vertices for (int j = 0; j < VertInfoList[section.VertsIndex].VertexCount; j++) { mode.BoundingBox bb; section.Vertices[j] = new Vertex(reader, formatNode); if (i >= BoundingBoxes.Count) { bb = new mode.BoundingBox(); bb.XBounds = bb.YBounds = bb.ZBounds = bb.UBounds = bb.VBounds = new RealBounds(0, 0); } else bb = BoundingBoxes[i]; ModelFunctions.DecompressVertex(ref section.Vertices[j], bb); } #endregion } #endregion validParts.Clear(); #region Read Indices for (int i = 0; i < ModelSections.Count; i++) { var section = ModelSections[i]; if (section.Submeshes.Count == 0) continue; if (section.FacesIndex >= 0 && section.FacesIndex < IndexInfoList.Count) reader.SeekTo(IndexInfoList[section.FacesIndex].Offset); mode.ModelSection validPart; if (validParts.TryGetValue(section.FacesIndex, out validPart)) { section.Indices = validPart.Indices; continue; } else validParts.Add(section.FacesIndex, section); section.Indices = new int[section.TotalFaceCount]; for (int j = 0; j < section.TotalFaceCount; j++) section.Indices[j] = (VertInfoList[section.VertsIndex].VertexCount > 0xFFFF) ? reader.ReadInt32() : reader.ReadUInt16(); } #endregion RawLoaded = true; }
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 }
private void LoadSections() { for (int x = 0; x < sbsp.ModelSections.Count; x++) { try { var part = sbsp.ModelSections[x]; if (part.Submeshes.Count == 0) { sectList.Add(null); continue; } var group = new Model3DGroup(); var verts = part.Vertices; for (int i = 0; i < part.Submeshes.Count; i++) { var submesh = part.Submeshes[i]; var geom = new MeshGeometry3D(); var iList = ModelFunctions.GetTriangleList(part.Indices, submesh.FaceIndex, submesh.FaceCount, sbsp.IndexInfoList[part.FacesIndex].FaceFormat); int min = iList.Min(); int max = iList.Max(); for (int j = 0; j < iList.Count; j++) { iList[j] -= min; } var vArray = new Vertex[(max - min) + 1]; Array.Copy(verts, min, vArray, 0, (max - min) + 1); foreach (var vertex in vArray) { VertexValue pos, tex, norm; vertex.TryGetValue("position", 0, out pos); vertex.TryGetValue("texcoords", 0, out tex); geom.Positions.Add(new Point3D(pos.Data.x, pos.Data.y, pos.Data.z)); geom.TextureCoordinates.Add(new System.Windows.Point(tex.Data.x, 1f - tex.Data.y)); if (vertex.TryGetValue("normal", 0, out norm)) { geom.Normals.Add(new Vector3D(norm.Data.x, norm.Data.y, norm.Data.z)); } } foreach (var index in iList) { geom.TriangleIndices.Add(index); } GeometryModel3D modeld = new GeometryModel3D(geom, shaders[submesh.ShaderIndex]) { //BackMaterial = shaders[submesh.ShaderIndex] }; group.Children.Add(modeld); } sectList.Add(group); } catch { sectList.Add(null); } } }
public override void LoadRaw() { if (RawLoaded) { return; } var bb = BoundingBoxes[0]; var IH = (Halo1PC.CacheFile.CacheIndexHeader)cache.IndexHeader; var reader = cache.Reader; for (int i = 0; i < ModelSections.Count; i++) { var section = ModelSections[i]; List <int> tIndices = new List <int>(); List <Vertex> tVertices = new List <Vertex>(); for (int j = 0; j < section.Submeshes.Count; j++) { var submesh = (Halo1PC.gbxmodel.ModelSection.Submesh)section.Submeshes[j]; #region Read Indices submesh.FaceIndex = tIndices.Count; var strip = new List <int>(); reader.SeekTo(submesh.FaceOffset + IH.vertDataOffset + IH.indexDataOffset); for (int k = 0; k < submesh.FaceCount; k++) { strip.Add(reader.ReadUInt16() + tVertices.Count); } strip = ModelFunctions.GetTriangleList(strip.ToArray(), 0, strip.Count, 5); strip.Reverse(); submesh.FaceCount = strip.Count; tIndices.AddRange(strip); #endregion #region Read Vertices reader.SeekTo(submesh.VertOffset + IH.vertDataOffset); for (int k = 0; k < submesh.VertexCount; k++) { var v = new Vertex() { FormatName = "Halo1PC_Skinned" }; var position = new RealQuat(reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle()); var normal = new RealQuat(reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle()); var binormal = new RealQuat(reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle()); var tangent = new RealQuat(reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle()); var texcoord = new RealQuat(reader.ReadSingle() * uScale, 1f - reader.ReadSingle() * vScale); var nodes = (Flags.Values[1]) ? new RealQuat(submesh.LocalNodes[reader.ReadInt16()], submesh.LocalNodes[reader.ReadInt16()], 0, 0) : new RealQuat(reader.ReadInt16(), reader.ReadInt16(), 0, 0); var weights = new RealQuat(reader.ReadSingle(), reader.ReadSingle(), 0, 0); v.Values.Add(new VertexValue(position, VertexValue.ValueType.Float32_3, "position", 0)); v.Values.Add(new VertexValue(normal, VertexValue.ValueType.Float32_3, "normal", 0)); v.Values.Add(new VertexValue(binormal, VertexValue.ValueType.Float32_3, "binormal", 0)); v.Values.Add(new VertexValue(tangent, VertexValue.ValueType.Float32_3, "tangent", 0)); v.Values.Add(new VertexValue(texcoord, VertexValue.ValueType.Float32_2, "texcoords", 0)); v.Values.Add(new VertexValue(nodes, VertexValue.ValueType.Int16_N2, "blendindices", 0)); v.Values.Add(new VertexValue(weights, VertexValue.ValueType.Float32_2, "blendweight", 0)); tVertices.Add(v); bb.XBounds.Min = Math.Min(bb.XBounds.Min, position.x); bb.XBounds.Max = Math.Max(bb.XBounds.Max, position.x); bb.YBounds.Min = Math.Min(bb.YBounds.Min, position.y); bb.YBounds.Max = Math.Max(bb.YBounds.Max, position.y); bb.ZBounds.Min = Math.Min(bb.ZBounds.Min, position.z); bb.ZBounds.Max = Math.Max(bb.ZBounds.Max, position.z); } #endregion } section.Indices = tIndices.ToArray(); section.Vertices = tVertices.ToArray(); IndexInfoList.Add(new IndexBufferInfo() { FaceFormat = 3 }); VertInfoList.Add(new VertexBufferInfo() { VertexCount = section.TotalVertexCount }); } RawLoaded = true; }
public override void LoadRaw() { if (RawLoaded) { return; } var mode = this; var data = cache.GetRawFromID(mode.RawID); var ms = new MemoryStream(data); var reader = new EndianReader(ms, IO.EndianFormat.Big); var validParts = new Dictionary <int, mode.ModelSection>(); LoadFixups(); if (mode.IndexInfoList.Count == 0) { throw new Exception("Geometry contains no faces"); } #region Read Vertices for (int i = 0; i < mode.ModelSections.Count; i++) { var section = mode.ModelSections[i]; if (section.Submeshes.Count == 0) { continue; } if (section.VertsIndex >= 0 && section.VertsIndex < mode.VertInfoList.Count) { reader.SeekTo(mode.VertInfoList[section.VertsIndex].Offset); } if (cache.vertexNode == null) { throw new NotSupportedException("No vertex definitions found for " + cache.Version.ToString()); } #region Get Vertex Definition XmlNode formatNode = null; foreach (XmlNode node in cache.vertexNode.ChildNodes) { if (Convert.ToInt32(node.Attributes["type"].Value, 16) == section.VertexFormat) { formatNode = node; break; } } if (formatNode == null) { throw new NotSupportedException("Format " + section.VertexFormat.ToString() + " not found in definition for " + cache.Version.ToString()); } #endregion mode.ModelSection validPart; if (validParts.TryGetValue(section.VertsIndex, out validPart)) { section.Vertices = validPart.Vertices; continue; } else { validParts.Add(section.VertsIndex, section); } section.Vertices = new Vertex[mode.VertInfoList[section.VertsIndex].VertexCount]; #region Get Vertices for (int j = 0; j < mode.VertInfoList[section.VertsIndex].VertexCount; j++) { section.Vertices[j] = new Vertex(reader, formatNode); ModelFunctions.DecompressVertex(ref section.Vertices[j], mode.BoundingBoxes[0]); #region fixups var vert = section.Vertices[j]; VertexValue v; #region rigid fix if (section.NodeIndex != 255 && !mode.Flags.Values[18]) { vert.Values.Add(new VertexValue(new Vector(section.NodeIndex, 0, 0, 0), VertexValue.ValueType.Int8_N4, "blendindices", 0)); vert.Values.Add(new VertexValue(new Vector(1, 0, 0, 0), VertexValue.ValueType.Int8_N4, "blendweight", 0)); } #endregion #region flag 18 fix if (mode.Flags.Values[18]) { VertexValue w; var hasWeights = vert.TryGetValue("blendweight", 0, out w); if (!hasWeights) { w = new VertexValue(new Vector(1, 0, 0, 0), VertexValue.ValueType.Int8_N4, "blendweight", 0); } if (vert.TryGetValue("blendindices", 0, out v)) { v.Data.A = w.Data.A == 0 ? 0 : mode.NodeIndexGroups[i].NodeIndices[(int)v.Data.A].Index; v.Data.B = w.Data.B == 0 ? 0 : mode.NodeIndexGroups[i].NodeIndices[(int)v.Data.B].Index; v.Data.C = w.Data.C == 0 ? 0 : mode.NodeIndexGroups[i].NodeIndices[(int)v.Data.C].Index; v.Data.D = w.Data.D == 0 ? 0 : mode.NodeIndexGroups[i].NodeIndices[(int)v.Data.D].Index; } else { v = new VertexValue(new Vector(0, 0, 0, 0), VertexValue.ValueType.Int8_N4, "blendindices", 0); v.Data.A = mode.NodeIndexGroups[i].NodeIndices[0].Index; vert.Values.Add(v); vert.Values.Add(w); } } #endregion #region rigid_boned fix if (!vert.TryGetValue("blendweight", 0, out v) && vert.TryGetValue("blendindices", 0, out v)) { var q = new Vector( v.Data.A == 0 ? 0 : 1, v.Data.B == 0 ? 0 : 1, v.Data.C == 0 ? 0 : 1, v.Data.D == 0 ? 0 : 1); vert.Values.Add(new VertexValue(q, VertexValue.ValueType.Int8_N4, "blendweight", 0)); } #endregion #endregion } #endregion } #endregion validParts.Clear(); #region Read Indices for (int i = 0; i < mode.ModelSections.Count; i++) { var section = mode.ModelSections[i]; if (section.Submeshes.Count == 0) { continue; } if (section.FacesIndex >= 0 && section.FacesIndex < mode.IndexInfoList.Count) { reader.SeekTo(mode.IndexInfoList[section.FacesIndex].Offset); } mode.ModelSection validPart; if (validParts.TryGetValue(section.FacesIndex, out validPart)) { section.Indices = validPart.Indices; continue; } else { validParts.Add(section.FacesIndex, section); } section.Indices = new int[section.TotalFaceCount]; for (int j = 0; j < section.TotalFaceCount; j++) { section.Indices[j] = (mode.VertInfoList[section.VertsIndex].VertexCount > 0xFFFF) ? reader.ReadInt32() : reader.ReadUInt16(); } } #endregion LoadModelExtras(); mode.RawLoaded = true; }