public Task <Mesh> GetMeshAsync(int meshIndex)
        {
            GltfLoader.Schema.Mesh mesh = gltf.Meshes[meshIndex];

            Span <byte>     indexBuffer     = Span <byte> .Empty;
            IndexBufferView?indexBufferView = null;
            bool            is32BitIndex    = false;

            if (mesh.Primitives[0].Indices.HasValue)
            {
                indexBuffer     = GetIndexBuffer(mesh, out int stride);
                is32BitIndex    = stride == sizeof(int);
                indexBufferView = new IndexBufferView(GraphicsResource.CreateBuffer(GraphicsDevice, indexBuffer, ResourceFlags.None), is32BitIndex);
            }

            VertexBufferView[] vertexBufferViews = GetVertexBufferViews(mesh, indexBuffer, is32BitIndex);

            int materialIndex = 0;

            if (mesh.Primitives[0].Material.HasValue)
            {
                materialIndex = mesh.Primitives[0].Material ?? throw new Exception();
            }

            Node node = gltf.Nodes.First(n => n.Mesh == meshIndex);

            float[] matrix = node.Matrix;

            Matrix4x4 worldMatrix = Matrix4x4.Transpose(new Matrix4x4(
                                                            matrix[0], matrix[1], matrix[2], matrix[3],
                                                            matrix[4], matrix[5], matrix[6], matrix[7],
                                                            matrix[8], matrix[9], matrix[10], matrix[11],
                                                            matrix[12], matrix[13], matrix[14], matrix[15]));

            float[] s = node.Scale;
            float[] r = node.Rotation;
            float[] t = node.Translation;

            Vector3    scale       = new Vector3(s[0], s[1], s[2]);
            Quaternion rotation    = new Quaternion(r[0], r[1], r[2], r[3]);
            Vector3    translation = new Vector3(t[0], t[1], t[2]);

            worldMatrix *= Matrix4x4.CreateScale(scale)
                           * Matrix4x4.CreateFromQuaternion(rotation)
                           * Matrix4x4.CreateTranslation(translation);

            MeshDraw meshDraw = new MeshDraw
            {
                IndexBufferView   = indexBufferView,
                VertexBufferViews = vertexBufferViews
            };

            return(Task.FromResult(new Mesh(meshDraw)
            {
                MaterialIndex = materialIndex, WorldMatrix = worldMatrix
            }));
        }
示例#2
0
 public Mesh(MeshDraw meshDraw)
 {
     MeshDraw = meshDraw;
 }