Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Custom RayCast function

Discussion in 'Scripting' started by Lizzard4000, Sep 21, 2015.

  1. Lizzard4000

    Lizzard4000

    Joined:
    Mar 3, 2010
    Posts:
    101
    Hello!

    The idea is to use Unitys Physics.Raycast with multiple cpu threads to speed things up.
    But since Physics.Raycast is not thread safe i started to write my own raycast function.

    Currently i cycle through all the triangles of a mesh and check if a ray intersects with the "Möller–Trumbore intersection algorithm".
    But this is like 50 times slower than Unitys raycasting. Is there a better way to do this?
    If my raycast would be 2-3 times slower than Unitys i would still be happy. Since i can still get a huge speed increase if i use 12 threads to process all the raycasts.

    Or is a custom raycast function generally a really bad idea?^^

    Thanks!
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    It would be a bad idea, since you can't count on anyone having 12 CPU cores. You don't get any speed increase by multithreading unless you actually have the hardware to take advantage of it. Most people only have 2 or 4 cores.

    --Eric
     
  3. Minsc

    Minsc

    Joined:
    Dec 30, 2013
    Posts:
    39
    Hi,

    Actually a lot (if not most) of gaming pcs now have intel cpu with 4 cores and 8 threads considering hyper-threading, I also need parallel raycasts, any news on your progress Lizzard?

    Thanks :)
     
  4. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    to be honest it's only going to actually help when the raycasting is actually a bottleneck, given that the default is fairly efficient I'm thinking effort is best spent on other areas picked up by the Profiler and things like that...
     
    Mycroft likes this.
  5. passerbycmc

    passerbycmc

    Joined:
    Feb 12, 2015
    Posts:
    1,739
    if you are raycasting so much, that it is a bottleneck you might want to look into why you are doing so many casts, and what kind of better method you could use.
     
    Mycroft likes this.
  6. hippocoder

    hippocoder

    Digital Ape Moderator

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    Time sliced raycasts are super effective for most of my work. But Unity probably needs to add support for threaded raycasting...

    Depending on what you want to do, it's not total insanity. If it's not many meshes, you may want to build a small 3D table of nearby entry points or hash so you can skip most triangles, should be fast then... ie don't use mesh.