Improvements for Batch Queries

Hey everyone!

We have been working on upgrading batch queries and their usability, and we would like to share an early access to it: https://beta.unity3d.com/download/cc7681295a53/public_download.html

Here are the things that were changed:

  • maxHits property was moved to the ScheduleBatch method. Now you can have a job for setting up commands without touching the main thread.

  • maxHits now properly works with all batch commands. Additionally, it’s guaranteed that you will get the closest hit if you set maxHits to 1.

  • We enabled support of multiple hits per 1 mesh.

  • We have introduced a new struct called QueryParameters for an easier and more configurable way of setting up commands.

  • We also changed signatures for all command structs:

Old signature of RaycastCommand:

RaycastCommand(Vector3 from, Vector3 direction, float distance, int layerMask, int maxHits)

New signature:

RaycastCommand(Vector3 from, Vector3 direction, QueryParameters queryParameters, float distance).

  • QueryParameters struct

  • LayerMask was moved to this structure.

  • Additionally we now support different options for batch queries:

  • Hit triggers - whether triggers return a hit.

  • Hit backfaces - whether backfaces return a hit…

  • Multiple hits per mesh - whether a single mesh can return multiple hits.

  • QueryParameters.Default is available for easier use with default values.

We are attaching a demo project we’ve prepared for you to showcase how new batch queries are functioning.

Also, would like to mention that this feature is going to be a part of the alpha release and will be available in 2022.2.0a6 and up.

Your feedback is greatly appreciated!

7895746–1005544–BatchQueryDemo.zip (40.9 KB)

11 Likes

Are there any plans for adding more commands besides Raycast (like Overlap)?
Or will the only possibility will be to use the ECS physics package for other commands?

1 Like

Hey @Nirlah ,

Thanks for the question! Yes, adding Overlaps to batch queries is in our backlog. But so far, can’t tell what is the ETA for this one.

1 Like

Would like to give an update on overlaps. OverlapBox, OverlapCapsule, and OverlapSphere were added to batch queries! They share the same maxHits logic as RaycastCommand. The Batch version of Overlaps should be available in 2022.2.0a12 and up!

7 Likes

Will this be ported to 2021? Could really use this!

Hey @Cloudwalker ,

Unfortunately, public API changes or any bigger feature works are usually not backported to any previous releases.

Hey everyone,

I was wondering if anyone had a fast way to read the results of an Overlap batch query. Here is my go at it, but it seems quite slow:

for (int i = 0; i < commands.Length; i++) {
    for (int j = 0; j < MAX_HITS; j++) {
        int index = (i * MAX_HITS) + j;
        if (results[index].collider == null) break;
        if (results[index].collider.gameObject != _agents[i].gameObject) {
            Debug.DrawRay(_agents[i].transform.position, results[index].collider.transform.position - _agents[i].transform.position, Color.red);
        }
    }
}

Profiler with Debug.DrawRay:
9293194--1302400--With DrawRay.png

Profiler without Debug.DrawRay:
9293194--1302406--Without DrawRay.png

Profiler without any reading of the array (all lines above commented out):

Thanks for any help!