Beispiel #1
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);
        }
Beispiel #2
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);
        }