My problem is rather straight forward: Convert a bytestream byte[] data
containing continuous floats into an array of vectors representing vertices of a mesh Vector3[] vertices
.
Here is my approach so far(only relevant code shown here):
public void SetReconstructedMeshFromDataStream(byte[] data)
{
int vertexBufferSize = BitConverter.ToInt32(data, 0);
Mesh mesh = new Mesh();
// 12 is the size of Vector3:
mesh.vertices = new Vector3[vertexBufferSize / 12];
// srcOffset = 12, because that is the size of the header in the data buffer:
Buffer.BlockCopy(data, 12, mesh.vertices, 0, vertexBufferSize);
}
This fails, since Buffer.BlockCopy()
only works with primitive types. Of course we could just copy to a float[]
, but the how to cast from float[]
to Vector3[]
without a loop?
Basically I want to avoid having to loop over all the vertices of my mesh (and normals, triangles,...), since I already have the correctly ordered data in my bytestream. I simply want to create the objects with the correct size and copy (or reference) the data.
EDIT: Found this answer to a similar question: This is a few years old. Is it still the best answer to my question? Any considerations using unsafe code?