Search Unity

Feature Request NativeArray overloads of Physics.RaycastNonAlloc etc for usability from Burst code

Discussion in 'Burst' started by SamOld, May 24, 2021.

  1. SamOld

    SamOld

    Joined:
    Aug 17, 2018
    Posts:
    333
    I recently discovered that the
    Physics.Raycast
    etc functions are directly callable from Burst code, which was a wonderful revelation as it would allow me to significantly tidy up and speed up my character controller, which is currently half Bursted and half not. These hopes were immediately dashed when I realised that the
    Physics.RaycastAll
    and
    Physics.RaycastNonAlloc
    versions are not Burstable, as all of their overloads use managed arrays!

    Could we have
    NativeArray
    overloads to enable this please?


    I'm talking about all of the cast functions, so ray, box, sphere, etc.
    Physics.RaycastAll(Ray ray, Allocator allocator): NativeArray<RaycastHit>
    and
    Physics.RaycastNonAlloc(Ray ray, NativeArray<RaycastHit> results)
    . Same too for
    OverlapSphereNonAlloc
    etc.

    While we're at it, perhaps
    NativeList
    versions of
    RaycastNonAlloc
    would also make sense to generally avoid allocations, but guarantee all results.

    Perhaps this thread would be better in the Physics forum, but my motivation comes from the Burst experience, and this forum allows feature request tags and that one doesn't.
     
    Last edited: May 26, 2021
    chriscode and apkdev like this.
  2. sheredom

    sheredom

    Unity Technologies

    Joined:
    Jul 15, 2019
    Posts:
    300
    This isn't exactly a Burst issue, so I've pinged some of the physics people internally about this.
     
    apkdev and SamOld like this.
  3. SamOld

    SamOld

    Joined:
    Aug 17, 2018
    Posts:
    333
    Yeah in hindsight this was the wrong forum, but thank you very much!
     
    sheredom likes this.
  4. SamOld

    SamOld

    Joined:
    Aug 17, 2018
    Posts:
    333
    I just realised that I was a bit silly in requesting
    OverlapSphereNonAlloc
    as that returns managed colliders, so that's a bit harder than the casts. An unmanaged collider type that would allow more Burstable physics would be a lovely addition though! That would unlock things like
    ComputePenetration
    too.

    I've got quite a few character controllers that are very procedural. The basic flow of each is something like
    1. A load of matrix maths
    2. A few physics queries
    3. A load more matrix maths
    4. A Movement integration loop that does about 10 iterations of queries and matrix maths
    5. A final batch of matrix maths
    Each of the matrix maths heavy stages benefits significantly from being Bursted, but the need to repeatedly drop out of Burst to do the queries is frustrating the whole process and negating the benefits. I have to shuttle a lot of data back and forth across the Burst boundary each time. As I'm iterating on the design I sometimes want to add one extra physics query in one place, but that particular query isn't Burstable, so I have to restructure.

    In my opinion, the usefulness of Burst in non ECS projects would be significantly improved by tighter integration with the physics APIs. I'm finding that this frustration comes up a lot. In most places it's ok to do a big batch of data processing in Burst then pass the results to the engine at the end, but physics is one of the few APIs that performance critical code often wants to access rapidly.

    Perhaps it would be useful for the Burst and physics teams to work together on this workflow at some point.
     
    chriscode likes this.