Пример #1
0
        internal static Mesh FromAssimp(Assimp.Mesh mesh)
        {
            Mesh newMesh = new Mesh();

            newMesh.Indices = mesh.GetUnsignedIndices();


            bool hasTexCoords          = mesh.HasTextureCoords(0);
            List <Assimp.Vector3D> uvs = hasTexCoords ? mesh.TextureCoordinateChannels[0] : null;

            // bounding box values
            float min_x, max_x, min_y, max_y, min_z, max_z;

            min_x = max_x = mesh.Vertices[0].X;
            min_y = max_y = mesh.Vertices[0].Y;
            min_z = max_z = mesh.Vertices[0].Z;

            Vertex[] vertices = new Vertex[mesh.VertexCount];
            for (int i = 0; i < vertices.Length; i++)
            {
                Vector3 vec  = mesh.Vertices[i].ToNumeric();
                Vector3 norm = mesh.Normals[i].ToNumeric();

                if (hasTexCoords)
                {
                    Assimp.Vector3D uv = uvs[i];
                    vertices[i] = new Vertex(vec, new Vector2(uv.X, 1.0f - uv.Y), norm);
                }
                else
                {
                    vertices[i] = new Vertex(vec, Vector2.Zero, norm);
                }

                if (vec.X < min_x)
                {
                    min_x = vec.X;
                }
                if (vec.X > max_x)
                {
                    max_x = vec.X;
                }
                if (vec.Y < min_y)
                {
                    min_y = vec.Y;
                }
                if (vec.Y > max_y)
                {
                    max_y = vec.Y;
                }
                if (vec.Z < min_z)
                {
                    min_z = vec.Z;
                }
                if (vec.Z > max_z)
                {
                    max_z = vec.Z;
                }
            }

            newMesh.BoundingBox = new BoundingBox(new Vector3(min_x, min_y, min_z), new Vector3(max_x, max_y, max_z));
            newMesh.Vertices    = vertices;
            return(newMesh);
        }