Exemple #1
0
        }                    // Disable parameterless constructor

        public static Model LoadModel(string path)
        {
            if (path == null)
            {
                throw new ArgumentNullException("path", "Path must not be null");
            }

            var mdl = new Model();

            Scene scene;
            var   lmp = LumpManager.GetLumpFullPath(path);

            using (Stream stream = lmp.AsStream) {
                using (AssimpContext context = new AssimpContext()) {
                    LumpIOSystem ioSys = new LumpIOSystem();
                    context.SetIOSystem(ioSys);
                    scene = context.ImportFileFromStream(stream, PostProcessSteps.Triangulate | PostProcessSteps.FlipUVs);

                    if (scene == null || scene.SceneFlags.HasFlag(SceneFlags.Incomplete) || scene.RootNode == null)
                    {
                        throw new Core.FatalError(String.Format("Error loading model \"{0}\"", path));
                    }
                }
            }
            mdl.directory = Path.GetDirectoryName(path);

            mdl.meshes = new List <MeshData> (scene.MeshCount);
            mdl.ProcessNode(scene.RootNode, scene);

            return(mdl);
        }
        public void TestMultiSearchDirectoryLoad()
        {
            String fileName = "fenris.lws";
            String[] searchPaths = { Path.Combine(TestHelper.RootPath, "TestFiles\\fenris\\scenes"), Path.Combine(TestHelper.RootPath, "TestFiles\\fenris\\objects") };
            FileIOSystem ioSystem = new FileIOSystem(searchPaths);

            AssimpContext importer = new AssimpContext();
            importer.SetIOSystem(ioSystem);

            //None, using the "target high quality flags caused a crash with this model.
            Scene scene = importer.ImportFile(fileName, PostProcessSteps.None);
            Assert.IsNotNull(scene);
        }
        public void TestMultiSearchDirectoryConvert()
        {
            String fileName = Path.Combine(TestHelper.RootPath, "TestFiles\\fenris\\scenes\\fenris.lws");
            String[] searchPaths = { Path.Combine(TestHelper.RootPath, "TestFiles\\fenris\\objects") };
            FileIOSystem ioSystem = new FileIOSystem(searchPaths);

            AssimpContext importer = new AssimpContext();
            importer.SetIOSystem(ioSystem);

            //Output path has to be specified fully, since we may be creating the file
            String outputPath = Path.Combine(TestHelper.RootPath, "TestFiles\\fenris\\fenris2.obj");
            importer.ConvertFromFileToFile(fileName, PostProcessSteps.None, outputPath, "obj", PostProcessSteps.None);
        }
        public void TestIOSystemError()
        {
            String fileName = "duckduck.dae"; //GOOSE!
            String[] searchPaths = { Path.Combine(TestHelper.RootPath, "TestFiles") };
            FileIOSystem ioSystem = new FileIOSystem(searchPaths);

            AssimpContext importer = new AssimpContext();
            importer.SetIOSystem(ioSystem);
            Assert.Throws<AssimpException>(delegate()
            {
                importer.ImportFile(fileName, PostProcessSteps.None);
            });
        }
        public void TestIOSystemError()
        {
            String fileName = "duckduck.dae"; //GOOSE!

            String[]     searchPaths = { Path.Combine(TestHelper.RootPath, "TestFiles") };
            FileIOSystem ioSystem    = new FileIOSystem(searchPaths);

            AssimpContext importer = new AssimpContext();

            importer.SetIOSystem(ioSystem);
            Assert.Throws <AssimpException>(delegate()
            {
                importer.ImportFile(fileName, PostProcessSteps.None);
            });
        }
        public void TestMultiSearchDirectoryConvert()
        {
            String fileName = Path.Combine(TestHelper.RootPath, "TestFiles/fenris/scenes/fenris.lws");

            String[]     searchPaths = { Path.Combine(TestHelper.RootPath, "TestFiles/fenris/objects") };
            FileIOSystem ioSystem    = new FileIOSystem(searchPaths);

            AssimpContext importer = new AssimpContext();

            importer.SetIOSystem(ioSystem);

            //Output path has to be specified fully, since we may be creating the file
            String outputPath = Path.Combine(TestHelper.RootPath, "TestFiles/fenris/fenris2.obj");

            importer.ConvertFromFileToFile(fileName, PostProcessSteps.None, outputPath, "obj", PostProcessSteps.None);
        }
        public void TestMultiSearchDirectoryLoad()
        {
            String fileName = "fenris.lws";

            String[]     searchPaths = { Path.Combine(TestHelper.RootPath, "TestFiles/fenris/scenes"), Path.Combine(TestHelper.RootPath, "TestFiles/fenris/objects") };
            FileIOSystem ioSystem    = new FileIOSystem(searchPaths);

            AssimpContext importer = new AssimpContext();

            importer.SetIOSystem(ioSystem);

            //None, using the "target high quality flags caused a crash with this model.
            Scene scene = importer.ImportFile(fileName, PostProcessSteps.None);

            Assert.IsNotNull(scene);
        }
        public void TestIOSystem_ImportObj()
        {
            String dir = Path.Combine(TestHelper.RootPath, "TestFiles");

            LogStream.IsVerboseLoggingEnabled = true;
            ConsoleLogStream log = new ConsoleLogStream();

            log.Attach();

            using (AssimpContext importer = new AssimpContext())
            {
                FileIOSystem iOSystem = new FileIOSystem(dir);
                importer.SetIOSystem(iOSystem);

                //Using stream does not use the IO system...
                using (Stream fs = File.OpenRead(Path.Combine(dir, "sphere.obj")))
                {
                    Scene scene = importer.ImportFileFromStream(fs, "obj");
                    Assert.IsTrue(scene != null);
                    Assert.IsTrue(scene.HasMeshes);
                    Assert.IsTrue(scene.HasMaterials);

                    //No material file, so the mesh will always use the default material
                    Assert.IsTrue(scene.Materials[scene.Meshes[0].MaterialIndex].Name == "DefaultMaterial");
                }

                //Using custom IO system requires us to pass in the file name, assimp will ask the io system to get a stream
                Scene scene2 = importer.ImportFile("sphere.obj");
                Assert.IsTrue(scene2 != null);
                Assert.IsTrue(scene2.HasMeshes);
                Assert.IsTrue(scene2.HasMaterials);

                //Should have found a material with the name "SphereMaterial" in the mtl file
                Assert.IsTrue(scene2.Materials[scene2.Meshes[0].MaterialIndex].Name == "SphereMaterial");
            }
        }
        public ImportedModelContainer ImportModel(ResourceLink sourceFile, ImportOptions importOptions)
        {
            var modelContainer = new ImportedModelContainer(sourceFile, importOptions);

            // Load Assimp scene
            using var assimpContext = new AssimpContext();
            assimpContext.SetIOSystem(new AssimpIOSystem(sourceFile));
            var scene = assimpContext.ImportFile(
                sourceFile.FileNameWithExtension,
                PostProcessPreset.TargetRealTimeFast | PostProcessSteps.SplitLargeMeshes);

            // Load all materials
            ProcessMaterials(modelContainer, scene);

            // Load all scene objects
            var boundBoxCalculator = new ObjectTreeBoundingBoxCalculator();

            ProcessNode(modelContainer, scene, scene.RootNode, null, boundBoxCalculator);

            // Finish loading
            modelContainer.FinishLoading(boundBoxCalculator.CreateBoundingBox());

            return(modelContainer);
        }