Search Unity

Question Slow raycasts

Discussion in 'Physics for ECS' started by jdtec, Jan 25, 2021.

  1. jdtec

    jdtec

    Joined:
    Oct 25, 2017
    Posts:
    302
    Hi,

    A sphere cast is taking 0.012ms to complete. A single raycast is taking 0.14ms in a separate job.

    This is in burst compiled jobs with safety checks off. I'm doing 6 raycasts in a job in total and it's taking around 0.6ms, which seems really slow?

    I have a terrain and some models in the scene but I wouldn't have expected anything like this still.

    1) Is it normal for sphere casts to be faster than a raycast? What could be causing this?
    2) Is this normal performance? What could I be doing wrong to cause the jobs to take so long?

    I'm calling these functions from bursted jobs:
    collisionWorld.CastRay (input, out hit);
    collisionWorld.CastCollider (input, out hit);

    Any help appreciated.
     
  2. rz_0lento

    rz_0lento

    Joined:
    Oct 8, 2013
    Posts:
    2,361
  3. jdtec

    jdtec

    Joined:
    Oct 25, 2017
    Posts:
    302
    Thanks for pointing it out @rz_0lento

    Yea I did actually read through that thread after posting. I see it concludes with inSight01 achieving a 0.0146ms for a single batched raycast for a scene with a single cube in it...

    I'm getting ~10x that cost for a single raycast in a more complex scene that includes a terrain and ~hundred or so other colliders. So maybe this is not unreasonable and I just have unrealistic expectations. I saw your comment on job overhead but I've also tried this using Run() on the main thread and it didn't change much.

    I'm still confused why the sphere cast is faster. I'll try some more tests and report back.
     
  4. jdtec

    jdtec

    Joined:
    Oct 25, 2017
    Posts:
    302
    Since I know more information about what I want from the raycasts in question I ended up replacing them with a hybrid voxel cast with a simple ray to heightmap cell plane intersection when near the heightmap. This gave a ~37.5* speed-up over using a standard raycast on the heightmap.

    Not a critique of Physics as such as I guess it has to solve for the general case, like lots of things in Unity. Maybe Unity physics is simply doing a ray to non-convex mesh intersection test for this situation currently.
     
  5. Sima_Havok

    Sima_Havok

    Joined:
    Dec 16, 2019
    Posts:
    52
    What is the performance of raycasts if you enable RayTraceCamera in any of the physics samples (Physcs Scene Basic Element > Main Camera > RayTraceCamera)

    If raycast performance there is as expected, you can actually copy the necessary scripts and RayTraceUI component from samples into your game and test it there as well.
     
    jdtec likes this.
  6. jasons-novaleaf

    jasons-novaleaf

    Joined:
    Sep 13, 2012
    Posts:
    181
    I haven't used Unity much, but fyi, sphere casts are really cheap. just a distance check. I would not be surprised if it's cheaper than a raycast. Though, how the underlying spatial partition is setup is a huge variable that can impact performance one way or the other.
     
    jdtec likes this.