static void ExportModel(MDL model) { IFileFormat fileFormat = (IFileFormat)model; string daePath = fileFormat.FileInfo.FileName.Replace(".mdl", ".dae"); string folder = Path.GetFileNameWithoutExtension(fileFormat.FileInfo.FileName); string folderDir = Path.Combine(Path.GetDirectoryName(fileFormat.FileInfo.FilePath), folder); if (!Directory.Exists($"{folder}")) { Directory.CreateDirectory($"{folder}"); } var materials = model.Header.Materials; var drawElements = model.Header.DrawElements; //Export by draw element to export shape flags for (int i = 0; i < drawElements.Length; i++) { File.WriteAllText($"{folder}/Mesh{i}.json", model.ExportMaterial(drawElements[i])); } File.WriteAllText($"{folder}/SamplerList.json", model.ExportSamplers()); var settings = new DAE.ExportSettings(); settings.ImageFolder = folder; DAE.Export(Path.Combine(folderDir, daePath), settings, model); Console.WriteLine($"Exported model {model.FileInfo.FileName}!"); }
public static Octree CreateFromModel(MDL model) { Vector3 epsilon = new Vector3(0.001f, 0.001f, 0.001f); Octree octree = new Octree { numModels = 1, bounds = new BoundingBox(model.Extents.Min - epsilon, model.Extents.Max + epsilon) }; List <OctreeFace> faces = new List <OctreeFace>(); for (ushort i = 0; i < model.Faces.Count; i++) { faces.Add(new OctreeFace { FaceNum = i, HitNumber = -1, Vertices = new List <Vector3> { model.Vertices[model.Faces[i].Verts[0]].Position, model.Vertices[model.Faces[i].Verts[1]].Position, model.Vertices[model.Faces[i].Verts[2]].Position } }); } octree.root = (OctreeNode)OctreeNode.Create(octree.bounds, faces); return(octree); }
static void DestroyEngine(Engine engine) { engine.Release(); engine = null; MDL.CheckStateAndClear(); HDL.CheckStateAndClear(); Assert(Engine._engines.Count, 0); }
public void Init(BSPEntity ent) { this.Ent = ent; transform.position = ent.GetVector3("origin"); transform.eulerAngles = new Vector3(transform.eulerAngles.x, ent.GetInt("angle"), transform.eulerAngles.z); mdl = new MDL(mdlFiles[ent.KeyValues["classname"]]); mdl.go.transform.SetParent(transform); mdl.go.transform.localPosition = Vector3.zero; }
public AuroraModel(Stream mdlStream, Stream mdxStream, Game importFrom) { this.importFrom = importFrom; mdlObject = new MDL(); Dictionary <string, Stream> other = new Dictionary <string, Stream> { { "mdl", mdlStream }, { "mdx", mdxStream } }; mdlObject.Load(mdlStream, other); }
/// <summary> /// Sets the data for the mesh combo box /// </summary> private void PartComboBoxChanged() { is3DLoaded = false; if (CompositeVM != null && !disposing) { disposing = true; CompositeVM.Dispose(); } try { List <ComboBoxInfo> cbi = new List <ComboBoxInfo>(); MDL mdl = new MDL(selectedItem, selectedCategory, Info.raceID[SelectedRace.Name], SelectedBody.ID, SelectedPart.ID); meshList = mdl.GetMeshList(); modelName = mdl.GetModelName(); materialStrings = mdl.GetMaterialStrings(); cbi.Add(new ComboBoxInfo() { Name = Strings.All, ID = Strings.All, IsNum = false }); if (meshList.Count > 1) { for (int i = 0; i < meshList.Count; i++) { cbi.Add(new ComboBoxInfo() { Name = i.ToString(), ID = i.ToString(), IsNum = true }); } } MeshComboBox = new ObservableCollection <ComboBoxInfo>(cbi); MeshIndex = 0; if (cbi.Count > 1) { MeshEnabled = true; } else { MeshEnabled = false; } } catch (Exception ex) { MessageBox.Show("[Main] part 3D Error \n" + ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error); } }
public void ConfigLoad() { System.Version assemblyVersion = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version; DateTime bulidDate = new DateTime(2000, 1, 1).AddDays(assemblyVersion.Build).AddSeconds(assemblyVersion.Revision * 2); Version = "Ver. " + bulidDate.ToString("yyyy-MM-dd"); _sys = _Data.Inst.sys; _mdl = _Data.Inst.mdl; _sysStatus = _Data.Inst.sys.status; viewIOList.ioSRC = _sys.io.lst; Logger.Inst.MakeSrcHdl($"MFC"); _log.OnWriteLog += _Log_OnMsg; }
void BenchmarkLoad() { loadTimer = new Stopwatch(); loadTimer.Start(); using (Stream mdlStream = File.OpenRead(mdlLocation)) using (Stream mdxStream = File.OpenRead(mdxLocation)) { AuroraModel auroraModel = new AuroraModel(mdlStream, mdxStream, Game.KotOR); mdlObj = auroraModel.mdlObject; } loadTimer.Stop(); }
// Use this for initialization void Start() { DirectoryInfo levelDirectoryPath = new DirectoryInfo("Assets/Resources/Models/"); FileInfo[] fileInfo = levelDirectoryPath.GetFiles("*.mdl", SearchOption.AllDirectories); float x = 0f; foreach (FileInfo fi in fileInfo) { MDL mdl = new MDL(fi.Name); mdl.go.transform.position = Vector3.zero + new Vector3(x, 0f, 0f); mdl.go.transform.eulerAngles = new Vector3(0f, -90f, 0f); x -= 60f; } }
public override void Export(Asset asset, string path) { Model model = (asset as Model); int materialIndex = 1; ModelManipulator.PreProcess(model, PreProcessOptions.SplitMeshPart); //PreProcessOptions.Dedupe | | PreProcessOptions.ResolveNonManifold foreach (ModelMesh mesh in model.Meshes) { MDL mdl = new MDL(); //mdl.ModelFlags = MDL.Flags.USERData; materialIndex = 0; foreach (ModelMeshPart meshpart in mesh.MeshParts.OrderByDescending(m => m.VertexCount).ToList()) { MDLMaterialGroup mdlmesh = new MDLMaterialGroup(materialIndex, (meshpart.Material != null ? meshpart.Material.Name : "DEFAULT")); int masterVertOffset = mdl.Vertices.Count; foreach (Vertex v in meshpart.VertexBuffer.Data) { mdl.Vertices.Add( new MDLVertex( v.Position.X, v.Position.Y, v.Position.Z, v.Normal.X, v.Normal.Y, v.Normal.Z, v.UV.X, v.UV.Y, v.UV.Z, v.UV.W, (byte)(v.Colour.R * 255), (byte)(v.Colour.G * 255), (byte)(v.Colour.B * 255), (byte)(v.Colour.A * 255) ) ); } for (int i = 0; i < meshpart.IndexBuffer.Data.Count; i += 3) { mdl.Faces.Add(new MDLFace(materialIndex, 0, masterVertOffset + meshpart.IndexBuffer.Data[i + 0], masterVertOffset + meshpart.IndexBuffer.Data[i + 1], masterVertOffset + meshpart.IndexBuffer.Data[i + 2])); } Stripper stripper = new Stripper(meshpart.IndexBuffer.Data.Count / 3, meshpart.IndexBuffer.Data.ToArray()) { OneSided = true, ConnectAllStrips = true }; stripper.ShakeItBaby(); if (stripper.Strips[0].Count > 3) { List <int> strip = stripper.Strips[0]; if ((strip.Count & 1) == 1) { strip.Reverse(); } else { strip.Insert(0, strip[0]); } HashSet <int> uniqueVerts = new HashSet <int> { strip[0], strip[1] }; mdlmesh.StripOffset = masterVertOffset; mdlmesh.StripList.Add(new MDLPoint(strip[0], false)); mdlmesh.StripList.Add(new MDLPoint(strip[1], false)); for (int i = 2; i < strip.Count; i++) { uniqueVerts.Add(strip[i]); MDLPoint point = new MDLPoint(strip[i], (strip.GetRange(i - 2, 3).Distinct().Count() != 3)); mdlmesh.StripList.Add(point); } mdlmesh.StripVertCount = uniqueVerts.Count; } int patchOffset = int.MaxValue; for (int i = 1; i < stripper.Strips.Count; i++) { patchOffset = Math.Min(patchOffset, stripper.Strips[i].Min()); } for (int i = 1; i < stripper.Strips.Count; i++) { List <int> patch = stripper.Strips[i]; mdlmesh.TriListOffset = masterVertOffset + patchOffset; for (int j = 2; j >= 0; j--) { int index = patch[j] - patchOffset; mdlmesh.TriList.Add(new MDLPoint(index)); mdlmesh.TriListVertCount = Math.Max(mdlmesh.TriListVertCount, index + 1); } } mdlmesh.CalculateExtents(mdl.Vertices); mdl.Meshes.Add(mdlmesh); materialIndex++; Console.WriteLine("Saved mesh"); } mdl.Save(path + mesh.Name + ".mdl"); Console.WriteLine("Saved MDL"); } }
//public override string GetHints(string currentPath) //{ // string hints = string.Empty; // while (Directory.Exists(Directory.GetParent(currentPath).FullName)) // { // if (Directory.Exists(Path.Combine(Directory.GetParent(currentPath).FullName, "Models"))) // { // } // } // if (currentPath != null && Directory.Exists(Directory.GetParent(currentPath).FullName)) // { // hints = $"{currentPath};"; // if (Directory.Exists(Path.Combine(Directory.GetParent(currentPath).FullName, "PIXELMAP"))) // { // hints += $"{Path.Combine(Directory.GetParent(currentPath).FullName, "PIXELMAP")};"; // } // } // return hints; //} public override Asset Import(string path) { MDL mdl = MDL.Load(path); Model model = new Model(); ModelMesh mesh = new ModelMesh(); // 2015-07-12 : Commenting out SupportingDocuments["Source"] to see if anything breaks // model.SupportingDocuments["Source"] = mdl; bool bUsePrepData = true; for (int i = 0; i < mdl.Meshes.Count; i++) { Dictionary <int, int> newIndex = new Dictionary <int, int>(); ModelMeshPart meshpart = new ModelMeshPart(); MDLMaterialGroup mdlmesh = mdl.GetMesh(i); meshpart.Material = SceneManager.Current.Content.Load <Material, MaterialImporter>(mdlmesh.Name, Path.GetDirectoryName(path), true); if (bUsePrepData) { foreach (MDLFace f in mdl.Faces.Where(f => f.MaterialID == i)) { for (int j = 0; j < 3; j++) { if (!newIndex.ContainsKey(f.Verts[j])) { MDLVertex v = mdl.Vertices[f.Verts[j]]; int index = meshpart.AddVertex(new Vector3(v.Position.X, v.Position.Y, v.Position.Z), new Vector3(v.Normal.X, v.Normal.Y, v.Normal.Z), new Vector2(v.UV.X, v.UV.Y), new Vector2(v.UV2.X, v.UV2.Y), v.Colour, false); newIndex.Add(f.Verts[j], index); } } meshpart.AddFace( newIndex[f.Verts[0]], newIndex[f.Verts[1]], newIndex[f.Verts[2]] ); } } else { int[] verts = new int[3]; for (int j = 0; j < mdlmesh.StripList.Count - 2; j++) { if (mdlmesh.StripList[j + 2].Degenerate) { continue; } verts[0] = mdlmesh.StripList[j + 0].Index; if (j % 2 == 0) { verts[1] = mdlmesh.StripList[j + 1].Index; verts[2] = mdlmesh.StripList[j + 2].Index; } else { verts[1] = mdlmesh.StripList[j + 2].Index; verts[2] = mdlmesh.StripList[j + 1].Index; } for (int k = 0; k < 3; k++) { if (!newIndex.ContainsKey(verts[k])) { MDLVertex v = mdl.Vertices[verts[k]]; int index = meshpart.AddVertex(new Vector3(v.Position.X, v.Position.Y, v.Position.Z), new Vector3(v.Normal.X, v.Normal.Y, v.Normal.Z), new Vector2(v.UV.X, v.UV.Y), new Vector2(v.UV2.X, v.UV2.Y), v.Colour, false); newIndex.Add(verts[k], index); } } meshpart.AddFace( newIndex[verts[0]], newIndex[verts[1]], newIndex[verts[2]] ); } // Process patch list for (int j = 0; j < mdlmesh.TriList.Count; j += 3) { verts[0] = mdlmesh.TriList[j + 0].Index; verts[1] = mdlmesh.TriList[j + 1].Index; verts[2] = mdlmesh.TriList[j + 2].Index; for (int k = 0; k < 3; k++) { if (!newIndex.ContainsKey(verts[k])) { MDLVertex v = mdl.Vertices[verts[k]]; int index = meshpart.AddVertex(new Vector3(v.Position.X, v.Position.Y, v.Position.Z), new Vector3(v.Normal.X, v.Normal.Y, v.Normal.Z), new Vector2(v.UV.X, v.UV.Y), new Vector2(v.UV2.X, v.UV2.Y), v.Colour, false); newIndex.Add(verts[k], index); } } meshpart.AddFace( newIndex[verts[0]], newIndex[verts[1]], newIndex[verts[2]] ); } } mesh.AddModelMeshPart(meshpart); Console.WriteLine(meshpart.VertexCount / 3); } mesh.Name = mdl.Name; model.SetName(mdl.Name, model.AddMesh(mesh)); return(model); }
static void Main(string[] args) { if (args.Length == 0) { Console.WriteLine(""); Console.WriteLine("Tool made by KillzXGaming."); Console.WriteLine("Mdl research thanks to opeyx and SpaceCats."); Console.WriteLine(""); Console.WriteLine("Arguments:"); Console.WriteLine(""); Console.WriteLine("Convert .mdl to .dae/.png/.json formats:"); Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine(" ModelConverter.exe (target .mdl file)"); Console.ForegroundColor = ConsoleColor.White; Console.WriteLine(""); Console.WriteLine("Create a new .mdl file:"); Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine(" ModelConverter.exe (extracted .mdl folder or .dae) (originalFile.mdl)"); Console.ForegroundColor = ConsoleColor.White; Console.WriteLine("Optional Arguments:"); Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine("-skel (Uses custom skeleton when creating new mdl)"); Console.ForegroundColor = ConsoleColor.White; return; } string folder = ""; string target = ""; string originalMdlTarget = ""; string appFolder = AppDomain.CurrentDomain.BaseDirectory; //Input is a folder if (Directory.Exists(args[0])) { folder = args[0]; var daeFiles = Directory .EnumerateFiles(folder, "*.*", SearchOption.AllDirectories) .Where(s => Path.GetExtension(s).ToLowerInvariant() == ".dae").ToList(); if (daeFiles.Count == 0) { throw new Exception("No .dae files found in folder!"); } target = daeFiles[0]; } else { folder = Path.GetDirectoryName(args[0]); target = args[0]; } if (Utils.GetExtension(target) == ".dae") { if (args.Length > 1) { originalMdlTarget = args[1]; } else if (File.Exists($"{folder}.mdl")) { originalMdlTarget = $"{folder}.mdl"; } else if (File.Exists(target.Replace(".dae", ".mdl"))) { originalMdlTarget = target.Replace(".dae", ".mdl"); } else { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("Error! Make sure you input the original .mdl file."); Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine(""); Console.WriteLine(" ModelConverter.exe (extracted .mdl folder or .dae) (originalFile.mdl)"); Console.ForegroundColor = ConsoleColor.White; return; } } var importedModel = (IModelFormat)STFileLoader.OpenFileFormat(target); if (importedModel is MDL) { ExportModel((MDL)importedModel); } else { var importModel = importedModel.ToGeneric(); importModel.OrderBones(importModel.Skeleton.Bones.OrderBy(x => GetBoneIndex(x.Name)).ToList()); //Load any extra texture maps not referenced by the model itself for texture animations foreach (var file in Directory.GetFiles(Path.GetDirectoryName(target))) { if (Utils.GetExtension(file) == ".png") { string texname = Path.GetFileNameWithoutExtension(file); if (!importModel.Textures.Any(x => x.Name == texname)) { importModel.Textures.Add(new GenericBitmapTexture(file)); } } } if (originalMdlTarget != string.Empty && !args.Contains("-skel")) { var mdl = (IModelFormat)STFileLoader.OpenFileFormat(originalMdlTarget); importModel.Skeleton = mdl.ToGeneric().Skeleton; //Recalculate the bone indices on the original skeleton foreach (var mesh in importModel.Meshes) { for (int v = 0; v < mesh.Vertices.Count; v++) { for (int j = 0; j < mesh.Vertices[v].BoneNames.Count; j++) { var boneName = mesh.Vertices[v].BoneNames[j]; var boneIndex = importModel.Skeleton.Bones.FindIndex(x => x.Name == boneName); mesh.Vertices[v].BoneIndices[j] = boneIndex; } } } } var model = new MDL(); model.FileInfo = new File_Info(); model.Header = new MDL_Parser(); if (File.Exists($"{folder}\\SamplerList.json")) { string json = File.ReadAllText($"{folder}\\SamplerList.json"); model.ReplaceSamplers(json); } STGenericScene scene = new STGenericScene(); scene.Models.Add(importModel); model.FromGeneric(scene); //Reset the indices and assign by json file foreach (var node in model.Header.Nodes) { node.ShapeCount = 0; node.ShapeIndex = 0; } Node[] nodeList = new Node[model.Header.Nodes.Length]; for (int i = 0; i < nodeList.Length; i++) { nodeList[i] = new Node(); } var drawElements = model.Header.DrawElements; for (int i = 0; i < importModel.Meshes.Count; i++) { ushort nodeIndex = 0; //Check both the default and the imported mesh names. and inject data to these slots if (File.Exists($"{folder}\\{importModel.Meshes[i].Name}.json")) { string json = File.ReadAllText($"{folder}\\{importModel.Meshes[i].Name}.json"); nodeIndex = model.ReplaceMaterial(json, drawElements[i]); } else if (File.Exists($"{appFolder}\\Defaults.json")) { string json = File.ReadAllText($"{appFolder}\\Defaults.json"); nodeIndex = model.ReplaceMaterial(json, drawElements[i]); } //Here we add our draw elements to the assigned node they use from the json files if (nodeIndex < model.Header.Nodes.Length) { nodeList[nodeIndex].DrawElements.Add(drawElements[i]); } else { nodeList[0].DrawElements.Add(drawElements[i]); } } //Create a new draw list in proper order List <DrawElement> sortedElements = new List <DrawElement>(); for (int i = 0; i < nodeList.Length; i++) { sortedElements.AddRange(nodeList[i].DrawElements); } model.Header.DrawElements = sortedElements.ToArray(); //Set the referenced draw elements/shapes used for the node lists ushort index = 0; for (int i = 0; i < nodeList.Length; i++) { model.Header.Nodes[i].ShapeIndex = index; model.Header.Nodes[i].ShapeCount = (ushort)nodeList[i].DrawElements.Count; index += (ushort)nodeList[i].DrawElements.Count; } Console.WriteLine("Saving file.."); string name = Path.GetFileName(folder); STFileSaver.SaveFileFormat(model, $"{name}.new.mdl"); Console.WriteLine($"Saved model " + $"{name}.new.mdl!"); } }
public void OnGUI() { using (new EditorGUILayout.VerticalScope()) { mdlLocation = EditorGUILayout.TextField("MDL Location", mdlLocation); mdxLocation = EditorGUILayout.TextField("MDX Location", mdxLocation); if (GUILayout.Button("Load")) { using (Stream mdlStream = File.OpenRead(mdlLocation)) using (Stream mdxStream = File.OpenRead(mdxLocation)) { AuroraModel auroraModel = new AuroraModel(mdlStream, mdxStream, Game.KotOR); mdlObj = auroraModel.mdlObject; } } if (GUILayout.Button("Instantiate")) { using (Stream mdlStream = File.OpenRead(mdlLocation)) using (Stream mdxStream = File.OpenRead(mdxLocation)) { // Create an instance GameObject model = AuroraEngine.Resources.LoadModel(mdlStream, mdxStream, new Dictionary <string, GameObject>(), Vector3.zero, null, null, null); } } if (GUILayout.Button("Benchmark Load")) { BenchmarkLoad(); } if (GUILayout.Button("Benchmark Instantiate")) { BenchmarkInstantiate(); } if (loadTimer != null) { GUILayout.Label("Loading: " + loadTimer.Elapsed.ToString()); } if (instantiateTimer != null) { GUILayout.Label("Instantiation: " + instantiateTimer.Elapsed.ToString()); } if (loadTimer != null && instantiateTimer != null) { GUILayout.Label("Loading takes " + loadTimer.Elapsed.TotalSeconds / instantiateTimer.Elapsed.TotalSeconds); } //using (var scope = new EditorGUILayout.ScrollViewScope(scroll, "box")) //{ // scroll = scope.scrollPosition; // if (mdlObj != null) // { // // Show the MDL object's information // ShowMDL(); // } //} } }