Esempio n. 1
0
        public static Assimp.Scene GetResourceModel(string resourceName, string format)
        {
            if (AssimpContext == null)
            {
                AssimpContext = new Assimp.AssimpContext();
            }
            var stream = GetResourceStream(resourceName);

            return(AssimpContext.ImportFileFromStream(stream, format));
        }
Esempio n. 2
0
        public static UnityEngine.Mesh ToUnityMesh(File file)
        {
            if (file.FileType != File.Type.Model)
            {
                return(null);
            }

            Assimp.AssimpContext ctx = new Assimp.AssimpContext();
            Assimp.Scene         s   = ctx.ImportFileFromStream(file.StreamData, Assimp.PostProcessSteps.Triangulate);
            if (!s.HasMeshes)
            {
                return(null);
            }
            var mesh = s.Meshes[0];

            var newMesh = new UnityEngine.Mesh();

            newMesh.vertices = mesh.Vertices.ConvertAll(v => v.ToV3()).ToArray();

            //int[] indicies = mesh.GetIndices();

            List <int> indicies = new List <int>();

            foreach (var face in mesh.Faces)
            {
                indicies.AddRange(face.Indices);
            }

            newMesh.triangles = indicies.ToArray();

            for (int i = 0; i < mesh.TextureCoordinateChannelCount; ++i)
            {
                newMesh.SetUVs(i, mesh.TextureCoordinateChannels[i].ConvertAll(v => v.ToV2()));
            }
            return(newMesh);
        }
Esempio n. 3
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);
        }