Improve Raycast

Hi, is possible on this sample PG https://playground.babylonjs.com/#DWVUZJ#1 improve the time of raycast (pickWithRay) ? When i pick i have a range between 3 and 7 milliseconds and, i don’t no, if is possible decrease this time.

Thank you!

Hello you can reduce the number of candidates with mesh.isPickabke = false
Other option is to create impostors (simple invisible meshes that will be be picked instead of complex ones)

A other option is to use an octree:
Optimize Your Scene with Octrees - Babylon.js Documentation

3 Likes

@Helder_Oliveira I have been playing around with ‘bounding volume hierarchy’ or bvh, which improves raycasting. here is an example with you teeth example Morton BVH Pick

A warning or two - this is an experimental version, once loaded (please be patient) you need to use the ‘Click to Generate BVH’ button and wait until it disappears before clicking on the jaw. Waiting means that the browser warning about ‘something slowing it down’ might appear two or three times while the bvh is generated (again be patient). Once the button disappears click on the jaw and the time for the pick is given and small red boxes appear wherever the picking ray hit the jaw. I am getting around 1 to 3 ms to pick. I do not know if that is sufficient improvement for you.

In a full version you would be able to generate and save the bvh along with the mesh for later re-loading and use.

The code, should you want to use/develop it, is at GitHub - BabylonJSGuide/bvh: bounding volume hierarchies for ray tracing in Babylon.js.

The good news is that this topic suggests that at some point BVH (using a different method to generate) will become part of Babylon.js and may be generated using the GPU.

More test/info

5 Likes

Wowww, @JohnK, this is amazing :smiley:

Hi @JohnK excellent!!! For me between 1 and 3 milliseconds is very good :wink: I will try and I give my feedback :slight_smile:

Thanks a lot!!

Some good news and not so good news. As I often do I got somethings a bit wrong.

The BVH referred to here is one constructed by a top down method (and hereafter termed tradBVH) and will not be generated using the GPU. When looking at the results further on in this post it is worth noting that this tradBVH was designed for path tracing using the results in a GPU and not directly for ray casting.

This use a Bottom Up method and will be termed MortonBVH.

There is, of course, the standard approach of picking for a mesh already built into Babylon.js, the standard pick method.

Here are three examples of using each of these

Standard Pick - Babylon Basic Pick
tradBVH - Traditional BVH Pick
mortonBVH - Morton BVH Pick

example timings in milliseconds after teeth have loaded.

METHOD               GENERATION                          PICKING
standard pick               0                             avg 30 ish
tradBVH                  6900                             avg 20 ish
mortonBVH               11522                             avg  2 ish

mortonBVH is about twice as slow as the tradBVH in terms of generation and 10 times faster in picking.

Still a work in progress.

3 Likes

@JohnK The results of the mortonBVH demo is quite impressive. Did you continue working on it?

I tried for a while to work on a GPU version but my skills in this area were not good enough and I moved on to other things…

Ok, thanks for the headups!