Hello Community,
am having a system where I’m manipulating a mesh through a DLL on C++ side. The mesh vertices are sent via a pinned GCHandle gained like:
GCHandle.Alloc(mesh.vertices, GCHandleType.Pinned);
This works are expected.
However now I would also like to read those same vertices array with burst, thus needing a native array - WITHOUT COPYING (since this is for procedural terrain generation in realtime hence why doing the heavy lifting in C++).
The method NativeArrayUnsafeUtility.ConvertExistingDataToNativeArray seemed perfect for that and an example like this (unrelated to meshes now) compiles, given unsafe context:
Vector3[] my_verts = new Vector3[100];
my_verts[0] = new Vector3(17, 3, 9);
// Convert the managed array to a NativeArray (without copying the data)
GCHandle mesh_vertices_gc = GCHandle.Alloc(my_verts, GCHandleType.Pinned);
IntPtr ptr = mesh_vertices_gc.AddrOfPinnedObject();
NativeArray<Vector3> nativeArray = NativeArrayUnsafeUtility.ConvertExistingDataToNativeArray<Vector3>((void*)ptr, 100, Allocator.None);
Debug.Log("PTR : " + ptr); // A valid looking adress
Debug.Log("Length: " + nativeArray.Length); // 100 -> correct
However trying to access nativeArray[0] results in:
Somebody know what I am missing here?
Huge thanks in advance!