Example #1
0
        public Mesh(string name, MeshVertex[] vertices, int[] indices, MeshTexture[] textures, MeshColor color)
        {
            Name     = name;
            Vertices = vertices;
            Indices  = indices;
            Textures = textures;
            Color    = color;

            Position = new Vector3
            {
                X = vertices.Sum((vertex) => { return(vertex.Position.X); }),
                Y = vertices.Sum((vertex) => { return(vertex.Position.Y); }),
                Z = vertices.Sum((vertex) => { return(vertex.Position.Z); })
            };

            SetupMesh();
        }
Example #2
0
        private Mesh ProcessMesh(Assimp.Mesh mesh, Scene scene)
        {
            var vertices = new List <MeshVertex>();
            var indices  = new List <int>();
            var textures = new List <MeshTexture>();
            var color    = new MeshColor();

            for (int i = 0; i < mesh.VertexCount; i++)
            {
                var vertex = new MeshVertex();

                // process vertex positions, normals and texture coordinates
                var pos = mesh.Vertices[i];
                vertex.Position = new Vector3(pos.X, pos.Y, pos.Z);

                var norm = mesh.Normals[i];
                vertex.Normal = new Vector3(norm.X, norm.Y, norm.Z);

                if (mesh.HasTextureCoords(0))  // does the mesh contain texture coordinates?
                {
                    var tex = mesh.TextureCoordinateChannels[0][i];
                    vertex.TexCoords = new Vector2(tex.X, tex.Y);
                }
                else
                {
                    vertex.TexCoords = Vector2.Zero;
                }

                vertices.Add(vertex);
            }

            // process indices
            for (int i = 0; i < mesh.FaceCount; i++)
            {
                Face face = mesh.Faces[i];
                for (int j = 0; j < face.IndexCount; j++)
                {
                    indices.Add(face.Indices[j]);
                }
            }

            // process material
            if (mesh.MaterialIndex >= 0)
            {
                Material material = scene.Materials[mesh.MaterialIndex];

                // get all the needed material textures
                if (material.HasTextureDiffuse)
                {
                    List <MeshTexture> diffuseMaps = LoadMaterialTextures(material, TextureType.Diffuse, "texture_diffuse");
                    textures.AddRange(diffuseMaps);
                }
                if (material.HasTextureSpecular)
                {
                    List <MeshTexture> specularMaps = LoadMaterialTextures(material, TextureType.Specular, "texture_specular");
                    textures.AddRange(specularMaps);
                }

                // get all the needed material colors (default values if they're not presented)
                color.Ambient   = material.ColorAmbient;
                color.Diffuse   = material.ColorDiffuse;
                color.Specular  = material.ColorSpecular;
                color.Shininess = material.Shininess;
            }

            return(new Mesh(mesh.Name, vertices.ToArray(), indices.ToArray(), textures.ToArray(), color));
        }