0

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?

Roland Deschain
  • 2,211
  • 19
  • 50
  • `float[] to Vector3[] without a loop` I don't think you can as we don't really know how exactly Unity stores these values in memory – derHugo Sep 27 '21 at 13:30
  • 1
    You might want to look into the [new Mesh API using Unity's JobSystem](https://github.com/Unity-Technologies/MeshApiExamples) though .. the JobSystem uses [`NativeArray`](https://docs.unity3d.com/ScriptReference/Unity.Collections.NativeArray_1.html) and youc an use [`Reinterpret`](https://docs.unity3d.com/ScriptReference/Unity.Collections.NativeArray_1.Reinterpret.html) on them to convert between collection types – derHugo Sep 27 '21 at 13:31
  • @derHugo Thanks for the suggestions, the answer below brought me to NativeArrays as well! :) – Roland Deschain Sep 27 '21 at 14:44

1 Answers1

1

Have done something quite similar once to transfer vectors from a C++ dll to an array of vectors. I do not recall the exact code, but it involved the Unity Collections package. That one provides a series of native datatypes including NativeArray that has a Reinterpret method which directly allows to reinterpret the type into another one. In my case, I already had a series of floats, but it might work directly from bytes as well.

That said, do you have control over the code that generates the byte stream? Because if you are writing your own native plugin, it is possible to get a handle to the mesh vertices and directly manipulate those on the DLL side which is likely another bit faster. The official native rendering plugin example in Unity's github demonstrates this.

AlexGeorg
  • 967
  • 1
  • 7
  • 16
  • Hi, thank you. For your first part, did you mean this function:https://docs.unity3d.com/ScriptReference/Unity.Collections.NativeArray_1.Reinterpret.html? Concerning 2: Yeah I have control over that code, however it is a different C++ project, running on a separate machine (The Unity/C# code runs on a mobile device - this is for an AR App) – Roland Deschain Sep 27 '21 at 13:33
  • Yes, exactly that one. This one seems to have been built into Unity by now. So you might not even need the extra collections package I was referring to: https://docs.unity3d.com/Packages/com.unity.collections@0.0/manual/index.html – AlexGeorg Sep 27 '21 at 13:35
  • 1
    OK, I'll test out if it works with my use case and report back. Thanks! – Roland Deschain Sep 27 '21 at 13:37