Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Please make Unity.Physics.MeshCollider.Create take NativeSlice

Discussion in 'Physics for ECS' started by pbhogan, Apr 25, 2022.

  1. pbhogan

    pbhogan

    Joined:
    Aug 17, 2012
    Posts:
    377
    Please make Unity.Physics.MeshCollider.Create() alternatives that take NativeSlices or allow passing in custom vertices and triangles counts.

    It is currently impossible to pass in an existing pre-built array of triangles of which you only want to use the first n triangles (or vertices, for that matter).

    This is useful with procedural terrains or anything where you can pre-calculate the triangle index order. As it is now we have to allocate temporary arrays and copy out the section needed from the pre-built array.
     
  2. tertle

    tertle

    Joined:
    Jan 25, 2011
    Posts:
    3,738
    Can't you just use NativeArray.GetSubArray(startIndex, length) for this specific cases.

    That said, I don't disagree with your sentiment. There are other cases i can think where native slice actually makes sense when you actually need strides to generate a mesh, for example if you built your mesh with float4 verts.
     
  3. pbhogan

    pbhogan

    Joined:
    Aug 17, 2012
    Posts:
    377
    Yes. Yes I can.

    I think I need you on speed dial to stop me posting stupid things.

    I was looking at NativeArray yesterday going, "surely there's a way to grab non-allocating subset of an array" and I blanked right over GetSubArray() and fixated on slices.

    But yeah, on the vertex side slices would be nice. I'm having to extract from an array of vertex data containing position, normals, etc.

    And furthermore, looking into MeshCollider.Create(), it looks like it's taking the NativeArray<int3> and immediately turning it into a NativeArray<int>, which is what you're more likely to have when making a mesh in the first place. And then it's making sure it has welded vertices, etc. I could see it being useful to tell it you know your vertices are already unique.

    It's a pity the MeshBuilder and MeshConnectivityBuilder APIs are internal. There's some useful stuff in there. If those were exposed we could make our own version without copying all the code out. I may do that anyway if it warrants optimization down the road.