Example #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);
            }
        }
Example #2
0
        public static Mesh CreateQuad(float width = 20, float height = 0)
        {
            var hw = width / 2;
            var hh = height > 0 ? height / 2 : hw;
            var meshBuilder = new MeshBuilder();

            meshBuilder.BeginSubmesh();

            var vertices = new MeshVertex[4];
            vertices[0] = new MeshVertex();
            vertices[0].Position = new Vector3(-hw, -hh, 0);
            vertices[0].Normal = new Vector3(0, 0, 1);
            //vertices[0].Tangent = new Vector3(0, 1, 0);
            vertices[0].TexCoord = new Vector2(1, 1);

            vertices[1] = new MeshVertex();
            vertices[1].Position = new Vector3(hw, -hh, 0);
            vertices[1].Normal = new Vector3(0, 0, 1);
            //vertices[1].Tangent = new Vector3(1, 0, 0);
            vertices[1].TexCoord = new Vector2(0, 1);

            vertices[2] = new MeshVertex();
            vertices[2].Position = new Vector3(-hw, hh, 0);
            vertices[2].Normal = new Vector3(0, 0, 1);
            //vertices[2].Tangent = new Vector3(-1, 0, 0);
            vertices[2].TexCoord = new Vector2(1, 0);

            vertices[3] = new MeshVertex();
            vertices[3].Position = new Vector3(hw, hh, 0);
            vertices[3].Normal = new Vector3(0, 0, 1);
            //vertices[3].Tangent = new Vector3(0, -1, 0);
            vertices[3].TexCoord = new Vector2(0, 0);

            vertices.ToList().ForEach(meshBuilder.Vertex);

            meshBuilder.Index(0);
            meshBuilder.Index(1);
            meshBuilder.Index(2);
            meshBuilder.Index(2);
            meshBuilder.Index(1);
            meshBuilder.Index(3);

            meshBuilder.EndSubmesh();

            return meshBuilder.Complete();
        }
Example #3
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();
            }
        }