コード例 #1
0
        public static bool Import(string file, List <Geometry> geoList, List <BoundingBox> bboxList,
                                  VertexComponent[] vertexComponents = null, bool combineVB = false, bool combineIB = false)
        {
            Assimp.PostProcessSteps assimpFlags =
                Assimp.PostProcessSteps.FlipWindingOrder |
                Assimp.PostProcessSteps.CalculateTangentSpace |
                Assimp.PostProcessSteps.Triangulate |
                Assimp.PostProcessSteps.SortByPrimitiveType |
                Assimp.PostProcessSteps.PreTransformVertices |
                //Assimp.PostProcessSteps.GenerateNormals |
                Assimp.PostProcessSteps.GenerateSmoothNormals |
                Assimp.PostProcessSteps.GenerateUVCoords |
                Assimp.PostProcessSteps.OptimizeMeshes |
                Assimp.PostProcessSteps.Debone |
                Assimp.PostProcessSteps.ValidateDataStructure;

            var    ctx = new Assimp.AssimpContext();
            string ext = FileUtil.GetExtension(file);

            if (!ctx.IsImportFormatSupported(ext))
            {
                ctx.Dispose();
                return(false);
            }

            File stream = FileSystem.Instance.GetFile(file);

            Assimp.Scene scene = ctx.ImportFileFromStream(stream, assimpFlags);
            float        scale = 1.0f;
            string       path  = FileUtil.GetPath(file);

            vertexBuffer.Clear();
            indexBuffer.Clear();
            vertexOffset = 0;
            indexOffset  = 0;

            if (vertexComponents == null)
            {
                vertexComponents = defaultVertexComponents;
            }

            VertexLayout vertexLayout = new VertexLayout(vertexComponents);

            // Iterate through all meshes in the file and extract the vertex components
            for (int m = 0; m < scene.MeshCount; m++)
            {
                Assimp.Mesh mesh     = scene.Meshes[m];
                var         geometry = ConvertGeometry(mesh, scale, vertexLayout, vertexComponents, combineVB, combineIB, out var meshBoundingBox);
                geoList.Add(geometry);
                bboxList.Add(meshBoundingBox);
            }

            if (combineVB)
            {
                var vb = Buffer.Create(VkBufferUsageFlags.VertexBuffer, false, sizeof(float), vertexBuffer.Count, vertexBuffer.Data);
                foreach (var geo in geoList)
                {
                    geo.VertexBuffer = vb;
                }
            }

            if (combineIB)
            {
                var ib = Buffer.Create(VkBufferUsageFlags.IndexBuffer, false, sizeof(uint), indexBuffer.Count, indexBuffer.Data);
                foreach (var geo in geoList)
                {
                    geo.IndexBuffer = ib;
                }
            }
            ctx.Dispose();
            return(true);
        }
コード例 #2
0
        protected unsafe override bool OnLoad(Model model, File stream)
        {
            var filePath = FileUtil.StandardlizeFile(stream.Name);

            filePath = FileUtil.GetPath(stream.Name);
            FileSystem.AddResourceDir(filePath);
            FileSystem.AddResourceDir(filePath + "textures");

            var    ctx = new Assimp.AssimpContext();
            string ext = FileUtil.GetExtension(loadingFile);

            if (!ctx.IsImportFormatSupported(ext))
            {
                ctx.Dispose();
                return(false);
            }

            //Assimp.Scene scene = ctx.ImportFileFromStream(stream, assimpFlags, ext);
            Assimp.Scene scene = ctx.ImportFile(stream.Name, assimpFlags);

            BoundingBox boundingBox = new BoundingBox();

            string path = FileUtil.GetPath(loadingFile);

            model.VertexBuffers = new List <Buffer>();
            model.IndexBuffers  = new List <Buffer>();

            vertexBuffer.Clear();
            indexBuffer.Clear();
            vertexOffset = 0;
            indexOffset  = 0;
            VertexLayout    vertexLayout = new VertexLayout(vertexComponents);
            List <Geometry> geoList      = new List <Geometry>();

            // Iterate through all meshes in the file and extract the vertex components
            for (int m = 0; m < scene.MeshCount; m++)
            {
                Assimp.Mesh mesh = scene.Meshes[m];
                if (mesh.MaterialIndex >= 0 && mesh.MaterialIndex < scene.MaterialCount)
                {
                    Material mat = ConvertMaterial(path, scene.Materials[mesh.MaterialIndex], mesh.HasTangentBasis);
                    if (mat == null)
                    {
                        continue;
                    }

                    model.Materials.Add(mat);
                }
                else
                {
                    Log.Error("No material : " + mesh.Name);
                }

                var geometry = ConvertGeometry(mesh, scale, vertexLayout, vertexComponents, combineVB, combineIB, out var meshBoundingBox);
                geoList.Add(geometry);

                if (geometry.VertexBuffer != null)
                {
                    model.VertexBuffers.Add(geometry.VertexBuffer);
                }

                if (geometry.IndexBuffer != null)
                {
                    model.IndexBuffers.Add(geometry.IndexBuffer);
                }

                model.Geometries.Add(new [] { geometry });
                model.GeometryCenters.Add(meshBoundingBox.Center);

                boundingBox.Merge(meshBoundingBox);
            }

            if (combineVB)
            {
                var vb = Buffer.Create(VkBufferUsageFlags.VertexBuffer, false, sizeof(float), vertexBuffer.Count, vertexBuffer.Data);
                model.VertexBuffers.Add(vb);

                foreach (var geo in geoList)
                {
                    geo.VertexBuffer = vb;
                }
            }

            if (combineIB)
            {
                var ib = Buffer.Create(VkBufferUsageFlags.IndexBuffer, false, sizeof(uint), indexBuffer.Count, indexBuffer.Data);
                model.IndexBuffers.Add(ib);
                foreach (var geo in geoList)
                {
                    geo.IndexBuffer = ib;
                }
            }

            model.BoundingBox = boundingBox;

            ctx.Dispose();

            FileSystem.RemoveResourceDir(filePath);
            FileSystem.RemoveResourceDir(filePath + "textures");
            return(true);
        }
コード例 #3
0
        static void Main(string[] args)
        {
            if (args.Length == 0)
            {
                Console.WriteLine("No arguments!");
            }

            var objectFile = args[0];

            Console.WriteLine("Loading mesh...");

            Geometry.Geometry geo;
            if (Path.GetExtension(objectFile) == ".hfe")
            {
                // Loading an hfe file
                geo = Geometry.Geometry.LoadFromBinary(objectFile);
            }
            else if (Path.GetExtension(objectFile) == ".json")
            {
                // Loading geometry from json
                geo = Geometry.Geometry.LoadFromJson(objectFile);
            }
            else
            {
                // Loading an external file format
                var importer = new Assimp.AssimpContext();
                importer.SetConfig(new Assimp.Configs.NormalSmoothingAngleConfig(66.0f));
                var scene = importer.ImportFile(objectFile, Assimp.PostProcessPreset.TargetRealTimeMaximumQuality);

                if (!scene.HasMeshes)
                {
                    Console.WriteLine("Scene has no meshes!");
                    return;
                }

                Console.WriteLine("Processing mesh...");
                geo = Geometry.Geometry.FromAssimp(scene.Meshes[0], true, true);
                scene.Clear();
                importer.Dispose();
            }

            bool bUseSymmetricLaplacian = false;
            var  indx = Array.FindIndex(args, t => t == "-sym");

            if (indx != -1)
            {
                Console.WriteLine("Using symmetric laplacian...");
                bUseSymmetricLaplacian = true;
            }

            indx = Array.FindIndex(args, t => t == "-lapout");
            if (indx != -1)
            {
                Console.WriteLine("Creating differential structure...");
                var diff = new DifferentialStructure(geo);

                Console.WriteLine("Writing mesh laplacian to file...");
                var outputFile = args[indx + 1];

                if (bUseSymmetricLaplacian)
                {
                    DifferentialStructure.WriteSparseMatrix(diff.InteriorSymmetrizedLaplacian, outputFile);
                }
                else
                {
                    DifferentialStructure.WriteSparseMatrix(diff.InteriorLaplacian, outputFile);
                }
            }

            indx = Array.FindIndex(args, t => t == "-modesin");
            Mat modes = null;
            Vec spec  = null;
            GeometryVisualMode visMode = GeometryVisualMode.ViewMesh;

            if (indx != -1)
            {
                Console.WriteLine("Reading mode data...");
                var inputFile = args[indx + 1];
                DifferentialStructure.ReadModeData(inputFile, out modes, out spec);

                var diff = new DifferentialStructure(geo);
                if (bUseSymmetricLaplacian)
                {
                    modes = diff.HalfInverseMassMatrix * modes;
                }

                if (geo.HasBoundary)
                {
                    modes = diff.InteriorToClosureMap * modes;
                }

                visMode = GeometryVisualMode.ViewModes;
            }

            indx = Array.FindIndex(args, t => t == "-funcin");
            if (indx != -1)
            {
                Console.WriteLine("Reading function data...");
                var inputFile = args[indx + 1];
                DifferentialStructure.ReadFunctionData(inputFile, out modes);

                var diff = new DifferentialStructure(geo);
                if (geo.HasBoundary)
                {
                    modes = diff.InteriorToClosureMap * modes;
                }

                visMode = GeometryVisualMode.ViewModes;
                spec    = Vec.Build.Dense(modes.RowCount, 0d);
            }

            indx = Array.FindIndex(args, t => t == "-viewmass");
            if (indx != -1)
            {
                visMode = GeometryVisualMode.ViewDegreeVector;
            }

            indx = Array.FindIndex(args, t => t == "-viewlapdiag");
            if (indx != -1)
            {
                visMode = GeometryVisualMode.ViewLapDiagonal;
            }

            indx = Array.FindIndex(args, t => t == "-meshout");
            if (indx != -1)
            {
                Console.WriteLine("Saving processed mesh...");
                var path = args[indx + 1];
                if (Path.GetExtension(path) == ".hfe")
                {
                    geo.SaveToBinary(path);
                }
                else if (Path.GetExtension(path) == ".json")
                {
                    geo.SaveToJson(path);
                }
                else
                {
                    Console.WriteLine("Unrecognized meshout file extension!");
                }
            }

            indx = Array.FindIndex(args, t => t == "-noview");
            if (indx != -1)
            {
                return;
            }

            using (var window = new GeometryDisplayWindow(geo))
            {
                window.VisualMode        = visMode;
                window.ObjectModes       = modes;
                window.ObjectEigenvalues = spec;
                window.Run();
            }
        }