public void ConvertMTKToRenderModel(M2TStructure structure) { List <Vertex[]> vertices = new List <Vertex[]>(); LODs = new LOD[structure.Lods.Length]; for (int i = 0; i != structure.Lods.Length; i++) { M2TStructure.Lod lod = structure.Lods[i]; vertices.Add(lod.Vertices); LOD lod2 = new LOD(); lod2.Indices = lod.Indices; lod2.ModelParts = new ModelPart[lod.Parts.Length]; for (int y = 0; y != lod.Parts.Length; y++) { ModelPart part = new ModelPart(); part.NumFaces = lod.Parts[y].NumFaces; part.StartIndex = lod.Parts[y].StartIndex; part.MaterialHash = lod.Parts[y].Hash; switch (part.MaterialHash) { case 1337: part.Material = RenderStorageSingleton.Instance.Prefabs.GizmoRed; break; case 1338: part.Material = RenderStorageSingleton.Instance.Prefabs.GizmoBlue; break; case 1339: part.Material = RenderStorageSingleton.Instance.Prefabs.GizmoGreen; break; default: part.Material = MaterialsManager.LookupMaterialByHash(part.MaterialHash); break; } lod2.ModelParts[y] = part; } lod2.Vertices = new VertexLayouts.NormalLayout.Vertex[lod.Vertices.Length]; for (int y = 0; y != lod.Vertices.Length; y++) { var vertice = new VertexLayouts.NormalLayout.Vertex(); vertice.Position = lod.Vertices[y].Position; vertice.Normal = lod.Vertices[y].Normal; vertice.TexCoord0 = lod.Vertices[y].UVs[0]; vertice.TexCoord7 = lod.Vertices[y].UVs[3]; lod2.Vertices[y] = vertice; } LODs[i] = lod2; } BoundingBox = new RenderBoundingBox(); BoundingBox.Init(BoundingBoxExtenders.CalculateBounds(vertices)); BoundingBox.SetTransform(Transform); BoundingBox.DoRender = false; SetupShaders(); }
/// <summary> /// Insert new mesh into the collision data. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void AddCollisionModel(object sender, System.EventArgs e) { //make sure an actual model has been selected. if (openM2T.ShowDialog() != DialogResult.OK) { MessageBox.Show("Failed to select an M2T model.", "Toolkit", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } M2TStructure colModel = new M2TStructure(); if (openM2T.FileName.ToLower().EndsWith(".m2t")) { colModel.ReadFromM2T(new BinaryReader(File.Open(openM2T.FileName, FileMode.Open))); } else if (openM2T.FileName.ToLower().EndsWith(".fbx")) { colModel.ReadFromFbx(openM2T.FileName); } //crash happened/ if (colModel.Lods[0] == null) { return; } Collision.NXSStruct nxsData = new Collision.NXSStruct(); nxsData.Hash = FNV64.Hash(colModel.Name); nxsData.Data.BuildBasicCollision(colModel.Lods[0]); nxsData.Sections = new Collision.Section[colModel.Lods[0].Parts.Length]; int curEdges = 0; for (int i = 0; i != nxsData.Sections.Length; i++) { nxsData.Sections[i] = new Collision.Section(); nxsData.Sections[i].Unk1 = (int)Enum.Parse(typeof(CollisionMaterials), colModel.Lods[0].Parts[i].Material) - 2; nxsData.Sections[i].Start = curEdges; nxsData.Sections[i].NumEdges = colModel.Lods[0].Parts[i].Indices.Length * 3; curEdges += colModel.Lods[0].Parts[i].Indices.Length * 3; } Collision.Placement placement = new Collision.Placement(); placement.Hash = nxsData.Hash; placement.Unk5 = 128; placement.Unk4 = -1; placement.Position = new Vector3(0, 0, 0); placement.Rotation = new Vector3(0); SceneData.Collisions.NXSData.Add(nxsData.Hash, nxsData); SceneData.Collisions.Placements.Add(placement); treeView1.Nodes.Clear(); LoadInCollision(); }
public bool PreInit(IntPtr WindowHandle) { D3D = new DirectX11Class(); if (!D3D.Init(WindowHandle)) { MessageBox.Show("Failed to initialize DirectX11!"); return(false); } Timer = new TimerClass(); FPS = new FPSClass(); Timer.Init(); FPS.Init(); RenderStorageSingleton.Instance.Prefabs = new RenderPrefabs(); if (!RenderStorageSingleton.Instance.ShaderManager.Init(D3D.Device)) { MessageBox.Show("Failed to initialize Shader Manager!"); return(false); } //this is backup! RenderStorageSingleton.Instance.TextureCache.Add(0, TextureLoader.LoadTexture(D3D.Device, D3D.DeviceContext, "texture.dds")); var structure = new M2TStructure(); //import gizmo RenderModel model = new RenderModel(); structure.ReadFromM2T("Resources/GizmoModel.m2t"); model.ConvertMTKToRenderModel(structure); model.InitBuffers(D3D.Device, D3D.DeviceContext); model.DoRender = false; RenderModel sky = new RenderModel(); structure = new M2TStructure(); structure.ReadFromM2T("Resources/sky_backdrop.m2t"); sky.ConvertMTKToRenderModel(structure); sky.InitBuffers(D3D.Device, D3D.DeviceContext); sky.DoRender = false; Assets.Add(1, sky); RenderModel clouds = new RenderModel(); structure = new M2TStructure(); structure.ReadFromM2T("Resources/weather_clouds.m2t"); clouds.ConvertMTKToRenderModel(structure); clouds.InitBuffers(D3D.Device, D3D.DeviceContext); clouds.DoRender = false; Assets.Add(2, clouds); return(true); }
public Collision.CollisionModel BuildFromM2TStructure(M2TStructure sourceModel) { Collision.CollisionModel collisionModel = new Collision.CollisionModel(); collisionModel.Hash = FNV64.Hash(sourceModel.Name); Lod modelLod = sourceModel.Lods[0]; IList <Vector3> vertexList = modelLod.Vertices.Select(v => v.Position).ToList(); // Material sections should be unique and sorted by material var sortedParts = new SortedDictionary <ushort, List <ModelPart> >( modelLod.Parts .GroupBy(p => MaterialToIndex(p.Material)) .ToDictionary(p => p.Key, p => p.ToList()) ); collisionModel.Sections = new List <Collision.Section>(sortedParts.Count); IList <TriangleMesh.Triangle> orderedTriangles = new List <TriangleMesh.Triangle>(modelLod.Indices.Length / 3); IList <ushort> materials = new List <ushort>(); foreach (var part in sortedParts) { var sameMaterialParts = part.Value; collisionModel.Sections.Add(new Collision.Section { Material = part.Key - 2, Start = orderedTriangles.Count * 3, NumEdges = (int)sameMaterialParts.Sum(p => p.NumFaces) * 3 }); foreach (var sameMaterialPart in sameMaterialParts) { var start = (int)sameMaterialPart.StartIndex; var end = start + sameMaterialPart.NumFaces * 3; for (int ci = start; ci < end; ci += 3) { orderedTriangles.Add(new TriangleMesh.Triangle( modelLod.Indices[ci], modelLod.Indices[ci + 1], modelLod.Indices[ci + 2] )); materials.Add(part.Key); } } } collisionModel.Mesh = new TriangleMeshBuilder().Build(vertexList, orderedTriangles, materials); return(collisionModel); }
public bool PreInit(IntPtr WindowHandle) { D3D = new DirectX11Class(); if (!D3D.Init(WindowHandle)) { MessageBox.Show("Failed to initialize DirectX11!"); } Profile.Init(); if (!RenderStorageSingleton.Instance.IsInitialised()) { bool result = RenderStorageSingleton.Instance.Initialise(D3D); var structure = new M2TStructure(); //import gizmo RenderModel gizmo = new RenderModel(); structure.ReadFromM2T("Resources/GizmoModel.m2t"); gizmo.ConvertMTKToRenderModel(structure); gizmo.InitBuffers(D3D.Device, D3D.DeviceContext); gizmo.DoRender = true; TranslationGizmo = new GizmoTool(gizmo); sky = new RenderModel(); structure = new M2TStructure(); structure.ReadFromM2T("Resources/sky_backdrop.m2t"); sky.ConvertMTKToRenderModel(structure); sky.InitBuffers(D3D.Device, D3D.DeviceContext); clouds = new RenderModel(); structure = new M2TStructure(); structure.ReadFromM2T("Resources/weather_clouds.m2t"); clouds.ConvertMTKToRenderModel(structure); clouds.InitBuffers(D3D.Device, D3D.DeviceContext); clouds.DoRender = false; } selectionBox.SetColour(System.Drawing.Color.Red); selectionBox.Init(new BoundingBox(new Vector3(0.5f), new Vector3(-0.5f))); selectionBox.DoRender = false; return(true); }
/// <summary> /// Insert all collisions into the editor. /// </summary> public void LoadInCollision() { treeView1.Nodes.Clear(); for (int i = 0; i != SceneData.Collisions.NXSData.Count; i++) { Collision.NXSStruct nxsData = SceneData.Collisions.NXSData.ElementAt(i).Value; TreeNode node = new TreeNode(nxsData.Hash.ToString()); node.Tag = nxsData; node.Name = nxsData.Hash.ToString(); M2TStructure model = new M2TStructure(); model.Lods = new Lod[1]; model.Lods[0] = new Lod(); ; model.Lods[0].Vertices = new Vertex[nxsData.Data.Vertices.Length]; for (int x = 0; x != model.Lods[0].Vertices.Length; x++) { model.Lods[0].Vertices[x] = new Vertex(); model.Lods[0].Vertices[x].Position = nxsData.Data.Vertices[x]; } List <CollisionMaterials> typeList = new List <CollisionMaterials>(); List <List <Short3> > values = new List <List <Short3> >(); for (int x = 0; x != nxsData.Data.Materials.Length; x++) { CollisionMaterials[] mats = nxsData.Data.Materials; Int3[] triangles = nxsData.Data.Triangles; if (!typeList.Contains(mats[x])) { typeList.Add(mats[x]); values.Add(new List <Short3>()); values[typeList.Count - 1].Add(new Short3(triangles[x])); } else { for (int y = 0; y != typeList.Count; y++) { if (typeList[y] == mats[x]) { values[y].Add(new Short3(triangles[x])); } } } } model.Lods[0].Parts = new ModelPart[typeList.Count]; for (int x = 0; x != typeList.Count; x++) { model.Lods[0].Parts[x] = new ModelPart(); model.Lods[0].Parts[x].Indices = values[x].ToArray(); model.Lods[0].Parts[x].Material = typeList[x].ToString(); } model.ExportCollisionToM2T(node.Name); treeView1.Nodes.Add(node); } CustomEDD frame = new CustomEDD(); for (int i = 0; i != SceneData.Collisions.Placements.Count; i++) { Collision.Placement data = SceneData.Collisions.Placements[i]; CustomEDD.Entry entry = new CustomEDD.Entry(); entry.LodCount = 1; entry.LODNames = new string[1]; entry.LODNames[0] = data.Hash.ToString(); entry.Position = data.Position; entry.Rotation = new Matrix33(); for (int x = 0; x != treeView1.Nodes.Count; x++) { if (data.Hash.ToString() == treeView1.Nodes[x].Name) { TreeNode node = new TreeNode(treeView1.Nodes[x].Nodes.Count + 1.ToString()); node.Tag = data; node.Name = treeView1.Nodes[x].Nodes.Count + 1.ToString(); treeView1.Nodes[x].Nodes.Add(node); } } frame.Entries.Add(entry); } frame.EntryCount = frame.Entries.Count; using (BinaryWriter writer = new BinaryWriter(File.Create("collisions/frame.edd"))) { frame.WriteToFile(writer); } }