Could there under any circumstances be an issue with converting
NativeArray vector3Array
using
NativeArray float3Array = vector3Array.Reinterpret(UnsafeUtility.SizeOf()); ?
Both Vector3 and float3 are internally stored using three float32, so I can’t see any issue… but I’m not too experienced in low level programming, so perhaps on certain platforms it could cause issues, or something like that.
That should work, in fact you dont need UnsafeUtility.SizeOf<Vector3>() in the reinterpret method. Unity already checks the size of both the original and new types against each other and will throw an exception if they do not line up.
That extension method is used as a “safety override” confirming the reinterpret to resize the alignment of the new NativeArray to the new type size.
As both float3 and Vector3 should be the same size, you dont need that override.
Oh yeah, I do this all the time with no issues. If you reinterpret incompatible types you get garbage data, so it is easy enough to spot bugs. It seems extremely unlikely that Unity would change the float vector layout.
This copies the data twice - first at the mesh.vertices accessor, secondly in the NativeArray constructor. Also, you should probably dispose the NativeArray (not sure if you can do that with the reinterpreted array without storing the original somewhere). But sure, in case of editor code simple solutions are usually better than perfectly optimal code.
People are making it sound like this is tentatively safe, but this is pretty much one of the exact situations Reinterpret was made for, so this should be 100% safe unless Unity introduces some crazy bug that would break everything. I’m sure they had this type of conversion in mind when they created float3, and in fact, the reason you can use NativeArray and NativeArray in Mesh.SetVertices is because they use the underlying unsafe pointer to do pretty much this exact thing in a more direct way.