Stream Builder

Stream Builder are a helper to allow for fast interaction with Streams of known or possibly unknown concrete data types. They allow you to treat a stream much like an array with all the common operations like Add/Remove/Append and indexed retrieval operators. It is possible to also use a streambuilder to work with a subset of a stream. For example if you wanted to work on only 1 channel of tex coords when it contains 4 total


Creating a Stream Accessor
// For example, this will create a stream with the key `Vertex:Position` and the datatype of FVector3f
auto PositionStream = FRealtimeMeshStream::Create<FVector3f>(FRealtimeMeshStreams::Position);

// This will create a simple StreamBuilder where you know the type is FVector3f. This will assert if the types do not match
// This does allow for the fastest interaction with the stream as no internal conversion is performed.
TRealtimeMeshStreamBuilder<FVector3f> PositionStreamBuilder(PositionStream);

// This will create a simple StreamBuilder where you want to work with it as if the data was FVector3d, but the actual streamdata is FVector3f.
// This will incur a slight overhead as it will do the conversion both ways internally, 
// but this is all done through templates, so it's the least overhead of the conversion available
TRealtimeMeshStreamBuilder<FVector3d, FVector3f> PositionStreamBuilder(PositionStream);

// This will create a StreamBuilder where you want to treat the data as a FVector3d, but you don't know the format of the stream. 
// This will perform a dynamic conversion internally, but the formats must be compatible. 
// In this case FVector3d, or FVector3f are safe, as well as custom data types of the same setup
TRealtimeMeshStreamBuilder<FVector3d, void> PositionStreamBuilder(PositionStream);

Creating a Strided Stream Builder
// This creates a texcoords stream with 4 channels using the packed type FVector2DHalf.
auto TexCoordsStream = FRealtimeMeshStream::Create<TRealtimeMeshTexCoords<FVector2DHalf, 4>>(FRealtimeMeshStreams::TexCoords);

// We can do the same stream builder setup as above to work with all 4 channels at the same time
TRealtimeMeshStreamBuilder<TRealtimeMeshTexCoords<FVector2DHalf, 4>> TexCoordStreamBuilder(TexCoordsStream);

// Here we use the strided builder to work with channel 1 as though it was an array of FVector2f and let the builder
// perform the conversion internally.  This can be done with with all the same variations as a normal builder for no-conversion, direct-conversion, or dynamic conversion.
TRealtimeMeshStridedStreamBuilder<FVector2f, FVector2DHalf> TexCoordStreamBuilder(TexCoordsStream, 1);

Working with Stream Builders
// Stream Builders let you treat a stream much like a TArray
// So you have all the normal functions, plus some specialized helpers.
// Some examples are: 

// Add a element
PositionStreamBuilder.Add(FVector3f(0, 0, 0));

// Emplace an element
TexCoordsStreamBuilder.Emplace({ FVector2f(0, 0), FVector2f(0, 0) });

// Reserve the number of elements, preallocating the internal storage
PositionStreamBuilder.Reserve(100);

// Append an initializer list of 3 elements
PositionStreamBuilder.Append({ FVector3f(0, 0, 0), FVector3f(1, 1, 1), FVector3f(2, 2, 2) });

// Remove element 1
PositionStreamBuilder.RemoveAt(1);

// Set the number of elements filling any new elements with the supplied value.
PositionStreamBuilder.SetNumWithFill(128, FVector3f(0, 0, 1));