Exemplo n.º 1
0
        public static Mesh CreateFromTXT(string filePath, float scale = 1.0f)
        {
            using(System.IO.StreamReader sr = new System.IO.StreamReader(filePath))
            {
                string line = sr.ReadLine();
                while (!line.Contains("Data:")) line = sr.ReadLine();

                var builder = new MeshBuilder();
                builder.BeginSubmesh();

                uint index = 0;

                while(!sr.EndOfStream)
                {
                    line = sr.ReadLine();
                    if(line.Length > 0)
                    {
                        var v = new MeshVertex();
                        var n = line.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                        v.Position = new Vector3(float.Parse(n[0]), float.Parse(n[1]), float.Parse(n[2])) * scale;
                        v.TexCoord = new Vector2(float.Parse(n[3]), float.Parse(n[4]));
                        v.Normal = new Vector3(float.Parse(n[5]), float.Parse(n[6]), float.Parse(n[7]));
                        builder.Vertex(v);
                        builder.Index(index++);
                    }
                }

                builder.EndSubmesh();
                return builder.Complete(true);
            }
        }
Exemplo n.º 2
0
        public static Mesh CreateFromFile(string filePath, bool rotateYZ = false, bool convertToLeftHanded = false, float scale = 1)
        {
            Func<Assimp.Vector3D, Vector3> _MakeVector3 = v => new Vector3(v.X, v.Y, v.Z);
            Func<Assimp.Vector3D, Vector2> _MakeTexCoord = v =>
                {
                    var x = v.X; while (x > 1) x -= 1; while (x < 0) x += 1;
                    var y = v.Y; while (y > 1) y -= 1; while (y < 0) y += 1;
                    return new Vector2(x, y);
                };

            using (var importer = new AssimpImporter())
            {
                if (Config.LoadMeshPrintLog)
                {
                    LogStream logStream = new LogStream((msg, data) =>
                    {
                        Console.WriteLine(string.Format("Assimp: {0}", msg));
                    });
                    importer.AttachLogStream(logStream);
                }

                PostProcessSteps options = PostProcessSteps.None;
                if(Config.LoadMeshComputeTangent)
                    options |= PostProcessSteps.CalculateTangentSpace;
                if(convertToLeftHanded)
                    options |= PostProcessSteps.MakeLeftHanded;

                var scene = importer.ImportFile(filePath, options);

                if (!scene.HasMeshes) return null;

                var builder = new MeshBuilder();

                Matrix mat = Matrix.Identity;
                if(rotateYZ)
                    mat = Matrix.RotationX(-MathUtil.PiOverTwo);

                foreach (var aiMesh in scene.Meshes)
                {
                    builder.BeginSubmesh();

                    for (int i = 0; i < aiMesh.VertexCount; ++i)
                    {
                        var v = new MeshVertex();
                        v.Position = _MakeVector3(aiMesh.Vertices[i]) * scale;
                        v.Position = Vector3.TransformCoordinate(v.Position, mat);
                        if (aiMesh.HasNormals)
                        {
                            v.Normal = _MakeVector3(aiMesh.Normals[i]);
                            v.Normal = Vector3.TransformNormal(v.Normal, mat);
                        }
                        if (aiMesh.HasTangentBasis)
                        {
                            v.Tangent = _MakeVector3(aiMesh.Tangents[i]);
                            v.Tangent = Vector3.TransformNormal(v.Tangent, mat);
                        }

                        if (aiMesh.HasTextureCoords(0))
                        {
                            var texCoords = aiMesh.GetTextureCoords(0);
                            v.TexCoord = _MakeTexCoord(texCoords[i]);
                        }

                        builder.Vertex(v);
                    }

                    //aiMesh.GetIntIndices().ToList().ForEach(builder.Index);

                    for (int i = 0; i < aiMesh.FaceCount; ++i)
                    {
                        var face = aiMesh.Faces[i];
                        for (int j = 1; j < face.IndexCount - 1;++j )
                        {
                            builder.Index((uint) face.Indices[0]);
                            builder.Index((uint) face.Indices[j]);
                            builder.Index((uint) face.Indices[j+1]);
                        }
                    }

                    //if (scene.HasMaterials)
                    //{
                    //    var folder = System.IO.Path.GetDirectoryName(filePath);
                    //    var materialIndex = aiMesh.MaterialIndex;
                    //    if (materialIndex >= 0 && materialIndex < scene.Materials.Length)
                    //    {
                    //        var material = scene.Materials[materialIndex];
                    //        var textures = material.GetTextures(TextureType.Diffuse);
                    //        if (textures != null)
                    //        {
                    //            builder.Texture(0, System.IO.Path.Combine(folder, textures[0].FilePath));
                    //        }
                    //        textures = material.GetTextures(TextureType.Ambient);
                    //        if (textures != null)
                    //            builder.Texture(1, System.IO.Path.Combine(folder, textures[0].FilePath));
                    //        textures = material.GetTextures(TextureType.Specular);
                    //        if (textures != null)
                    //            builder.Texture(2, System.IO.Path.Combine(folder, textures[0].FilePath));
                    //    }
                    //}

                    builder.EndSubmesh();
                }

                return builder.Complete();
            }
        }