Exemplo n.º 1
0
        public Hatzap.Models.Mesh FromAssimp(Assimp.Mesh amesh)
        {
            var mesh = new Hatzap.Models.Mesh();

            var v = amesh.Vertices;
            var n = amesh.Normals;
            var t = amesh.Tangents;
            var b = amesh.BiTangents;
            var tc = amesh.TextureCoordinateChannels;
            var c = amesh.VertexColorChannels;

            var verts = new Vector3[v.Count];
            Vector3[] norms;
            Vector3[] tangents;
            Vector3[] binormals;
            Vector3[][] uv;
            Vector4[][] colors;

            if (amesh.HasNormals) norms = new Vector3[v.Count]; else norms = null;
            if (amesh.HasTangentBasis) tangents = new Vector3[v.Count]; else tangents = null;
            if (amesh.HasTangentBasis) binormals = new Vector3[v.Count]; else binormals = null;
            if (amesh.HasTextureCoords(0))
            {
                uv = new Vector3[amesh.TextureCoordinateChannelCount][];

                for(int i = 0; i < amesh.TextureCoordinateChannelCount; i++)
                {
                    uv[i] = new Vector3[v.Count];
                }

            }
            else
            {
                uv = null;
            }
            if (amesh.HasVertexColors(0))
            {
                colors = new Vector4[amesh.VertexColorChannelCount][];

                for (int i = 0; i < amesh.VertexColorChannelCount; i++)
                {
                    colors[i] = new Vector4[v.Count];
                }
            }
            else
            {
                colors = new Vector4[1][];
                colors[0] = new Vector4[v.Count];
            }

            for (int i = 0; i < v.Count; i++)
            {
                verts[i] = new Vector3(v[i].X, v[i].Y, v[i].Z);
                if (norms != null) norms[i] = new Vector3(n[i].X, n[i].Y, n[i].Z);
                if (tangents != null) tangents[i] = new Vector3(t[i].X, t[i].Y, t[i].Z);
                if (binormals != null) binormals[i] = new Vector3(b[i].X, b[i].Y, b[i].Z);
                if (uv != null)
                {
                    for(int j = 0; j < amesh.TextureCoordinateChannelCount; j++)
                    {
                        uv[j][i] = new Vector3(tc[j][i].X, tc[j][i].Y, tc[j][i].Z);
                    }
                }
                if (colors != null)
                {
                    if (amesh.HasVertexColors(0))
                    {
                        for (int j = 0; j < amesh.VertexColorChannelCount; j++)
                        {
                            colors[j][i] = new Vector4(c[j][i].R, c[j][i].G, c[j][i].B, c[j][i].A);
                        }
                    }
                    else
                    {
                        colors[0][i] = new Vector4(1, 1, 1, 1);
                    }

                }
            }

            mesh.Vertices = verts;
            mesh.Normals = norms;
            mesh.Tangents = tangents;
            mesh.Binormals = binormals;
            mesh.UV = uv;
            mesh.Colors = colors;

            var aindices = amesh.GetIndices();

            var indices = new uint[aindices.Length];

            for (int i = 0; i < aindices.Length; i++)
            {
                indices[i] = (uint)aindices[i];
            }

            mesh.Indices = indices;

            return mesh;
        }