public static Scene LoadFbx(string fileName)
        {
            //Create a new importer
            using (var 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);

                //This is how we add a logging callback
                LogStream logstream = new LogStream(delegate(String msg, String userData) {
                    Console.WriteLine(msg);
                });
                logstream.Attach();

                //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, PostProcessPreset.TargetRealTimeMaximumQuality);

                return(model);
            }
        }
        /*
         * Interpretes file at 'filepath' using Assimp and converts it to a equivalent Structure class.
         */
        public Structure Interpret(string filepath)
        {
            var   context = new AssimpContext();
            Scene scene;

            try{
                NormalSmoothingAngleConfig smoothingConfig = new NormalSmoothingAngleConfig(0.0f);
                SortByPrimitiveTypeConfig  removeConfig    = new SortByPrimitiveTypeConfig(PrimitiveType.Point | PrimitiveType.Line);
                context.SetConfig(smoothingConfig);
                context.SetConfig(removeConfig);
                scene = context.ImportFile(filepath,
                                           PostProcessSteps.Triangulate |
                                           PostProcessSteps.JoinIdenticalVertices |
                                           PostProcessSteps.CalculateTangentSpace |
                                           PostProcessSteps.GenerateNormals |
                                           PostProcessSteps.FindDegenerates |
                                           PostProcessSteps.SortByPrimitiveType |
                                           PostProcessSteps.FixInFacingNormals);
            }catch (FileNotFoundException e) {
                throw new FileNotFoundException("Unexpected error: Filepath into DataInterpreter not valid. {" + filepath + "}", e);
            }catch {
                throw new IOException("Error when importing file: " + filepath.ToString());
            }

            // For every mesh create a equivalent component.
            foreach (Mesh mesh in scene.Meshes)
            {
                int compIndex = _structure.addComponent(new Component());

                Dictionary <(double, double, double), int> dict = PreprocessVertices(compIndex, mesh.Vertices);

                // For every face, lookup each of the three vertex indices using dict and their coordinates. Then create the face in component with
                // the indices we just got.
                foreach (var face in mesh.Faces)
                {
                    int[] vertices = new int[3];
                    vertices[0] = dict[(mesh.Vertices[face.Indices[0]].X, mesh.Vertices[face.Indices[0]].Y, mesh.Vertices[face.Indices[0]].Z)];
Esempio n. 3
0
        public void TestExportBadFormatId()
        {
            AssimpContext importer            = new AssimpContext();
            NormalSmoothingAngleConfig config = new NormalSmoothingAngleConfig(66.0f);

            importer.SetConfig(config);

            LogStream logStream = new LogStream(delegate(string msg, string userData)
            {
                Console.WriteLine(msg);
            });

            logStream.Attach();

            Scene collada = importer.ImportFile(Path.Combine(TestHelper.RootPath, "TestFiles/duck.dae"));

            bool success = importer.ExportFile(collada, Path.Combine(TestHelper.RootPath, "TestFiles/output/exportedCollada.dae"), "dae");

            Assert.IsFalse(success);

            success = importer.ExportFile(collada, Path.Combine(TestHelper.RootPath, "TestFiles/output/exportedCollada.dae"), "collada");

            Assert.IsTrue(success);
        }
Esempio n. 4
0
        static MeshLoader()
        {
            NormalSmoothingAngleConfig config = new NormalSmoothingAngleConfig(80.0f);

            importer.SetConfig(config);
        }
Esempio n. 5
0
        public static void Init()
        {
            AssimpContext importer            = new AssimpContext();
            NormalSmoothingAngleConfig config = new NormalSmoothingAngleConfig(66.0f);

            importer.SetConfig(config);

            LogStream logStream = new LogStream(delegate(string msg, string userData)
            {
                Console.WriteLine(msg);
            });

            logStream.Attach();

            Scene scene = importer.ImportFile(@"resources/icosphere.obj", PostProcessPreset.TargetRealTimeQuality);

            Mesh.Icosphere = scene.Meshes[0];

            scene = importer.ImportFile(@"resources/icosphereHigh.obj", PostProcessPreset.TargetRealTimeQuality);
            Mesh.IcosphereHigh = scene.Meshes[0];

            scene         = importer.ImportFile(@"resources/asteroid.obj", PostProcessPreset.TargetRealTimeQuality);
            Mesh.Asteroid = scene.Meshes[0];

            scene      = importer.ImportFile(@"resources/point.obj", PostProcessPreset.TargetRealTimeQuality);
            Mesh.Point = scene.Meshes[0];

            scene     = importer.ImportFile(@"resources/cube.obj", PostProcessPreset.TargetRealTimeQuality);
            Mesh.Cube = scene.Meshes[0];

            scene       = importer.ImportFile(@"resources/camera.obj", PostProcessPreset.TargetRealTimeQuality);
            Mesh.Camera = scene.Meshes[0];

            scene             = importer.ImportFile(@"resources/skybox.obj", PostProcessPreset.TargetRealTimeQuality);
            Mesh.SkyboxFront  = scene.Meshes[0];
            Mesh.SkyboxBack   = scene.Meshes[1];
            Mesh.SkyboxRight  = scene.Meshes[2];
            Mesh.SkyboxLeft   = scene.Meshes[3];
            Mesh.SkyboxTop    = scene.Meshes[4];
            Mesh.SkyboxBottom = scene.Meshes[5];

            scene          = importer.ImportFile(@"resources/gizmoPosX.ply", PostProcessPreset.TargetRealTimeQuality);
            Mesh.GizmoXPos = scene.Meshes[0];
            scene          = importer.ImportFile(@"resources/gizmoPosY.ply", PostProcessPreset.TargetRealTimeQuality);
            Mesh.GizmoYPos = scene.Meshes[0];
            scene          = importer.ImportFile(@"resources/gizmoPosZ.ply", PostProcessPreset.TargetRealTimeQuality);
            Mesh.GizmoZPos = scene.Meshes[0];

            scene          = importer.ImportFile(@"resources/gizmoRotX.ply", PostProcessPreset.TargetRealTimeQuality);
            Mesh.GizmoXRot = scene.Meshes[0];
            scene          = importer.ImportFile(@"resources/gizmoRotY.ply", PostProcessPreset.TargetRealTimeQuality);
            Mesh.GizmoYRot = scene.Meshes[0];
            scene          = importer.ImportFile(@"resources/gizmoRotZ.ply", PostProcessPreset.TargetRealTimeQuality);
            Mesh.GizmoZRot = scene.Meshes[0];

            scene        = importer.ImportFile(@"resources/text000.obj", PostProcessPreset.TargetRealTimeQuality);
            Mesh.Text000 = scene.Meshes[0];
            scene        = importer.ImportFile(@"resources/text090.obj", PostProcessPreset.TargetRealTimeQuality);
            Mesh.Text090 = scene.Meshes[0];
            scene        = importer.ImportFile(@"resources/text180.obj", PostProcessPreset.TargetRealTimeQuality);
            Mesh.Text180 = scene.Meshes[0];
            scene        = importer.ImportFile(@"resources/text270.obj", PostProcessPreset.TargetRealTimeQuality);
            Mesh.Text270 = scene.Meshes[0];

            importer.Dispose();
            logStream.Detach();
        }
Esempio n. 6
0
        public ModelSDX(SharpDX.Direct3D11.Device device, string Folder, string File)
        {
            string fileName = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), Folder + File);

            using (AssimpContext importer = new AssimpContext())
            {
                NormalSmoothingAngleConfig config = new NormalSmoothingAngleConfig(66.0f);
                importer.SetConfig(config);
                var Model = importer
                            .ImportFile(fileName,
                                        PostProcessPreset.ConvertToLeftHanded |
                                        PostProcessSteps.CalculateTangentSpace |
                                        PostProcessSteps.FindInvalidData |
                                        PostProcessSteps.GenerateSmoothNormals |
                                        PostProcessSteps.GenerateUVCoords |
                                        PostProcessSteps.JoinIdenticalVertices |
                                        PostProcessSteps.LimitBoneWeights |
                                        PostProcessSteps.SortByPrimitiveType |
                                        PostProcessSteps.TransformUVCoords |
                                        PostProcessSteps.Triangulate);

                if (Model.HasAnimations && Model.Animations.Any(x => x.HasNodeAnimations))
                {
                    HasAnimation = true;

                    _bones = GetBones(Model).ToList();
                    for (int i = 0; i < _bones.Count; i++)
                    {
                        if (!_bones.Any(x => x.Name.Equals(_bones[i].Parent)))
                        {
                            var b = _bones[i];
                            b.Parent  = null;
                            _bones[i] = b;
                        }
                    }
                    BildBone(ref _bones);

                    _baseTransform = GetNodeTransforms().ToArray();
                    foreach (var anim in Model.Animations)
                    {
                        _animations.Add(new AnimationSDX(anim));
                    }
                }
                else
                {
                    Model.Clear();
                    Model = importer
                            .ImportFile(fileName,
                                        PostProcessPreset.ConvertToLeftHanded |
                                        PostProcessSteps.CalculateTangentSpace |
                                        PostProcessSteps.FindInvalidData |
                                        PostProcessSteps.GenerateSmoothNormals |
                                        PostProcessSteps.GenerateUVCoords |
                                        PostProcessSteps.JoinIdenticalVertices |
                                        PostProcessSteps.SortByPrimitiveType |
                                        PostProcessSteps.TransformUVCoords |
                                        PostProcessSteps.Triangulate |
                                        PostProcessSteps.PreTransformVertices
                                        );
                }
                var verts = Model.Meshes.SelectMany(x => x.Vertices).ToArray();
                var X     = verts.Sum(x => x.X) / verts.Length;
                var Y     = verts.Sum(x => x.Y) / verts.Length;
                var Z     = verts.Sum(x => x.Z) / verts.Length;
                Center = new Vector3(X, Y, Z);
                var _meshes = GetMeshes(Model).ToArray();
                _3dMeshes = Create3DMeshes(_meshes, device, Folder).ToArray();
                Model.Clear();
            }
        }
Esempio n. 7
0
        static public AssimpVolume LoadFromFile(string filename)
        {
            string path = Path.Combine("Assets", "Models", filename);

            AssimpContext importer = new AssimpContext();

            NormalSmoothingAngleConfig normalSmoothing = new NormalSmoothingAngleConfig(66.0f);

            importer.SetConfig(normalSmoothing);

            LogStream logStream = new LogStream
                                  (
                delegate(string message, string userData)
            {
                Console.Write(message);
            }
                                  );

            logStream.Attach();

            Scene model = importer.ImportFile(path, PostProcessPreset.TargetRealTimeMaximumQuality);
            Mesh  mesh  = model.Meshes[0];

            AssimpVolume v = new AssimpVolume();

            List <Vector3> newVertices = new List <Vector3>();

            foreach (Assimp.Vector3D vert in mesh.Vertices)
            {
                newVertices.Add(new Vector3(vert.X, vert.Y, vert.Z));
            }
            v.vertices = newVertices.ToArray();

            v.indices = mesh.GetIndices();

            if (mesh.HasNormals)
            {
                v.generateNormals = false;
                List <Vector3> newNormals = new List <Vector3>();
                foreach (Assimp.Vector3D n in mesh.Normals)
                {
                    newNormals.Add(new Vector3(n.X, n.Y, n.Z));
                }
                v.normals = newNormals.ToArray();
            }

            if (mesh.HasTextureCoords(0))
            {
                List <Vector2> newTextureCoords = new List <Vector2>();
                foreach (Assimp.Vector3D tc in mesh.TextureCoordinateChannels[0])
                {
                    newTextureCoords.Add(new Vector2(tc.X, tc.Y));
                }
                v.textureCoords = newTextureCoords.ToArray();
            }

            if (mesh.HasVertexColors(0))
            {
                List <Vector3> newColors = new List <Vector3>();
                foreach (Assimp.Color4D c in mesh.VertexColorChannels[0])
                {
                    newColors.Add(new Vector3(c.R, c.G, c.B));
                }
                v.colors = newColors.ToArray();
            }

            importer.Dispose();
            return(v);
        }
Esempio n. 8
0
        /// <summary>
        /// Load geometry data from a file.
        /// </summary>
        /// <returns>The geometry data.</returns>
        /// <param name="path">Path.</param>
        public static List <Geometry> LoadGeometry(string path)
        {
            // Create assimp context
            var importer = new AssimpContext();

            // Create normal angle configuration
            var config = new NormalSmoothingAngleConfig(66.0f);

            importer.SetConfig(config);

            // Attach log stream
            new LogStream((msg, userData) => {
                LogExtensions.LogStatic("{0}", msg);
                if (!string.IsNullOrEmpty(userData))
                {
                    LogExtensions.LogStatic("\t{0}", userData);
                }
            }).Attach();

            // Set post process flags
            var flags =
                PostProcessSteps.CalculateTangentSpace
                | PostProcessSteps.Triangulate
                | PostProcessSteps.GenerateSmoothNormals
                | PostProcessSteps.TransformUVCoords;

            // Import scene
            var scene = importer.ImportFile(path, flags);

            var geometry = new List <Geometry> ();

            // Iterate over meshes
            foreach (var mesh in scene.Meshes)
            {
                var pos = new List <Vector3> ();
                var tex = new List <Vector2> ();
                var nrm = new List <Vector3> ();

                var hasTexture = mesh.HasTextureCoords(0);

                // Iterate over face groups
                foreach (var group in mesh.Faces)
                {
                    // Iterate over indices
                    foreach (var ind in group.Indices)
                    {
                        pos.Add(mesh.Vertices[ind].ToVector3());
                        if (hasTexture)
                        {
                            tex.Add(mesh.TextureCoordinateChannels [0][ind].ToVector3().ToVector2());
                        }
                        nrm.Add(mesh.Normals[ind].ToVector3());
                    }
                }

                var geom = new Geometry(BeginMode.Triangles)
                           .AddBuffer("v_pos", pos.ToGLBuffer());
                if (hasTexture)
                {
                    geom.AddBuffer("v_tex", tex.ToGLBuffer());
                }
                geom.AddBuffer("v_nrm", nrm.ToGLBuffer());

                geometry.Add(geom);
            }

            return(geometry);
        }