Search Unity

Reintepreting a native array from <Vector3> as <float3>

Discussion in 'Entity Component System' started by pk1234dva, Oct 11, 2021.

  1. pk1234dva

    pk1234dva

    Joined:
    May 27, 2019
    Posts:
    84
    Could there under any circumstances be an issue with converting
    NativeArray<Vector3> vector3Array
    using
    NativeArray<float3> float3Array = vector3Array.Reinterpret<float3>(UnsafeUtility.SizeOf<Vector3>()); ?

    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.
     
  2. tertle

    tertle

    Joined:
    Jan 25, 2011
    Posts:
    3,761
    You're fine as long as Unity doesn't do something stupid with the layout in the future.
     
  3. Kmsxkuse

    Kmsxkuse

    Joined:
    Feb 15, 2019
    Posts:
    306
    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.
     
  4. apkdev

    apkdev

    Joined:
    Dec 12, 2015
    Posts:
    284
    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.
     
  5. pk1234dva

    pk1234dva

    Joined:
    May 27, 2019
    Posts:
    84
    Thanks guys.
     
  6. Goularou

    Goularou

    Joined:
    Oct 19, 2018
    Posts:
    54
    I find simpler to do
    var vertices = new NativeArray<Vector3>(mesh.vertices, Allocator.TempJob).Reinterpret<float3>();
    where mesh.vertices can by any other vector3[]
     
  7. apkdev

    apkdev

    Joined:
    Dec 12, 2015
    Posts:
    284
    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.
     
    Goularou likes this.
  8. HyperionSniper

    HyperionSniper

    Joined:
    Jun 18, 2017
    Posts:
    30
    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<float3> and NativeArray<Vector3> in Mesh.SetVertices is because they use the underlying unsafe pointer to do pretty much this exact thing in a more direct way.