Table of Contents

Interface VertexBufferHelper.IWriter<TDest>

Namespace
Stride.Graphics
Assembly
Stride.Graphics.dll
public interface VertexBufferHelper.IWriter<TDest>

Type Parameters

TDest

Examples

Writing directly to mesh color:

Model.Meshes[0].Draw.VertexBuffers[0].AsReadable(Services, out VertexBufferHelper helper, out int count);
// Write to colors if that semantic already exist in the buffer, otherwise returns false
helper.Write<ColorSemantic, Vector4, MultColor>(new MultColor(){ Color = Color.Gray });
// Upload changes to the GPU
Model.Meshes[0].Draw.VertexBuffers[0].Buffer.Recreate(helper.DataOuter);

private struct MultColor : VertexBufferHelper.IWriter<Vector4>
{
   public Color Color;

   public unsafe void Write<TConverter, TSource>(byte* sourcePointer, int elementCount, int stride)
       where TConverter : IConverter<TSource, Vector4>, IConverter<Vector4, TSource>
       where TSource : unmanaged
   {
       for (byte* end = sourcePointer + elementCount * stride; sourcePointer < end; sourcePointer += stride)
       {
           TConverter.Convert(*(TSource*)sourcePointer, out var val);
           val *= (Vector4)Color;
           TConverter.Convert(val, out *(TSource*)sourcePointer);
       }
   }
}

Methods

Write<TConverter, TSource>(byte*, int, int)

void Write<TConverter, TSource>(byte* sourcePointer, int elementCount, int stride) where TConverter : IConverter<TSource, TDest>, IConverter<TDest, TSource> where TSource : unmanaged

Parameters

sourcePointer byte*

Points to the first element in the vertex buffer, read it as a TSource* to retrieve its value

elementCount int

The amount of vertices. This is not equivalent to the size of the vertex buffer, or the size in bytes taken by individual vertices

stride int

The size in bytes taken by individual vertices, add it to sourcePointer to point to the next element

Type Parameters

TConverter

A helper to convert between TSource and TDest properly

TSource

The source type this vertex buffer was built with, for example Vector2 or Byte4, use TConverter to convert it into a TDest.

Examples

Writing directly to mesh color:

Model.Meshes[0].Draw.VertexBuffers[0].AsReadable(Services, out VertexBufferHelper helper, out int count);
// Write to colors if that semantic already exist in the buffer, otherwise returns false
helper.Write<ColorSemantic, Vector4, MultColor>(new MultColor(){ Color = Color.Gray });
// Upload changes to the GPU
Model.Meshes[0].Draw.VertexBuffers[0].Buffer.Recreate(helper.DataOuter);

private struct MultColor : VertexBufferHelper.IWriter<Vector4>
{
   public Color Color;

   public unsafe void Write<TConverter, TSource>(byte* sourcePointer, int elementCount, int stride)
       where TConverter : IConverter<TSource, Vector4>, IConverter<Vector4, TSource>
       where TSource : unmanaged
   {
       for (byte* end = sourcePointer + elementCount * stride; sourcePointer < end; sourcePointer += stride)
       {
           TConverter.Convert(*(TSource*)sourcePointer, out var val);
           val *= (Vector4)Color;
           TConverter.Convert(val, out *(TSource*)sourcePointer);
       }
   }
}