Beispiel #1
0
        public static VertexBufferBinding[] ConvertVertexBufferBinding(SharpGLTF.Schema2.MeshPrimitive mesh, GraphicsDevice device)
        {
            var offset   = 0;
            var vertElem =
                mesh.VertexAccessors
                .Select(
                    x =>
            {
                var y   = ConvertVertexElement(x, offset);
                offset += y.Item2;
                return(y.Item1);
            })
                .ToList();

            var declaration =
                new VertexDeclaration(
                    vertElem.ToArray()
                    );

            var size       = mesh.VertexAccessors.First().Value.Count;
            var byteBuffer = Enumerable.Range(0, size)
                             .Select(
                x =>
                declaration.EnumerateWithOffsets()
                .Select(y => y.VertexElement.SemanticName
                        .Replace("ORD", "ORD_" + y.VertexElement.SemanticIndex)
                        .Replace("BLENDINDICES", "JOINTS_0")
                        .Replace("BLENDWEIGHT", "WEIGHTS_0")
                        )
                .Select(y => mesh.GetVertexAccessor(y).TryGetVertexBytes(x).ToArray())
                )
                             .SelectMany(x => x)
                             .SelectMany(x => x)
                             .ToArray();

            var buffer =
                Stride.Graphics.Buffer.Vertex.New(
                    device,
                    new DataPointer(
                        GCHandle.Alloc(byteBuffer, GCHandleType.Pinned).AddrOfPinnedObject(),
                        byteBuffer.Length
                        )
                    );

            var binding = new VertexBufferBinding(buffer, declaration, size);

            return(new List <VertexBufferBinding>()
            {
                binding
            }.ToArray());
        }
Beispiel #2
0
        public static InputElementDescription[] CreateInputElements(this VertexDeclaration vertexDeclaration)
        {
            var inputElements     = new InputElementDescription[vertexDeclaration.VertexElements.Length];
            var inputElementIndex = 0;

            foreach (var element in vertexDeclaration.EnumerateWithOffsets())
            {
                inputElements[inputElementIndex++] = new InputElementDescription
                {
                    SemanticName      = element.VertexElement.SemanticName,
                    SemanticIndex     = element.VertexElement.SemanticIndex,
                    Format            = element.VertexElement.Format,
                    InputSlot         = 0,
                    AlignedByteOffset = element.Offset,
                };
            }

            return(inputElements);
        }