Ejemplo n.º 1
0
        private void MenuImportMesh_Click(object sender, RoutedEventArgs e)
        {
            // Configure open file dialog box
            Microsoft.Win32.OpenFileDialog openDlg = new Microsoft.Win32.OpenFileDialog();
            openDlg.FileName = ""; // Default file name
            openDlg.DefaultExt = ".*"; // Default file extension
            openDlg.Filter = "All files |*.*"; // Filter files by extension

            // Show open file dialog box
            Nullable<bool> result = openDlg.ShowDialog();

            // Process open file dialog box results
            if (result == true)
            {
                // Open document
                string filename = openDlg.FileName;

                //Create a new importer
                AssimpContext importer = new AssimpContext();

                //This is how we add a configuration (each config is its own class)
                //NormalSmoothingAngleConfig config = new NormalSmoothingAngleConfig(66.0f);
                //importer.SetConfig(config);

                var flags = PostProcessPreset.TargetRealTimeMaximumQuality | PostProcessSteps.Triangulate | PostProcessSteps.SortByPrimitiveType | PostProcessSteps.FlipUVs;

                //Import the model. All configs are set. The model
                //is imported, loaded into managed memory. Then the unmanaged memory is released, and everything is reset.
                Scene model = importer.ImportFile(filename, flags);

                var mesh = new Hatzap.Models.Mesh();

                Assimp.Mesh aMesh = null;

                foreach (var item in model.Meshes)
                {
                    if (item.PrimitiveType != Assimp.PrimitiveType.Triangle)
                        continue;

                    aMesh = item;
                    break;
                }

                if (aMesh != null)
                {
                    AssimpConvertor ac = new AssimpConvertor();

                    mesh = ac.FromAssimp(aMesh);
                }
                else
                {
                    Debug.WriteLine("ERROR: No triangle meshes found in imported model.");
                }

                importer.Dispose();

                if (mesh == null)
                    return;

                filename = System.IO.Path.GetFileNameWithoutExtension(filename);

                filename += ".mesh";

                // Configure save file dialog box
                Microsoft.Win32.SaveFileDialog saveDlg = new Microsoft.Win32.SaveFileDialog();
                saveDlg.FileName = filename; // Default file name
                saveDlg.DefaultExt = ".mesh"; // Default file extension
                saveDlg.Filter = "Mesh files (.mesh)|*.mesh"; // Filter files by extension

                // Show save file dialog box
                result = saveDlg.ShowDialog();

                // Process save file dialog box results
                if (result == true)
                {
                    // Save document
                    filename = saveDlg.FileName;

                    MeshManager meshManager = new MeshManager();

                    using (var fs = File.OpenWrite(filename))
                    {
                        meshManager.TemporarySaveAsset(mesh, fs);
                    }
                }
            }
        }