# Improve Raycast

**URL:** <https://forum.babylonjs.com/t/improve-raycast/12471>\
**Category:** Questions\
**Created:** [July 14, 2020, 8:54am UTC](https://forum.babylonjs.com/t/improve-raycast/12471 "2020-07-14T08:54:20Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Helder\_Oliveira](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/helder_oliveira/32/8700_2.png) [@Helder\_Oliveira](https://forum.babylonjs.com/u/Helder_Oliveira)\
**Post date:** [July 14, 2020, 8:54am UTC](https://forum.babylonjs.com/t/improve-raycast/12471/1 "2020-07-14T08:54:20Z")

</div>

Hi, is possible on this sample PG [https://playground.babylonjs.com/#DWVUZJ#1](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!

---

<div class="post-metadata">

**Author:** ![Deltakosh](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/deltakosh/32/26635_2.png) [@Deltakosh](https://forum.babylonjs.com/u/Deltakosh)\
**Post date:** [July 14, 2020, 2:34pm UTC](https://forum.babylonjs.com/t/improve-raycast/12471/2 "2020-07-14T14:34:47Z")

</div>

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](https://doc.babylonjs.com/how_to/optimizing_your_scene_with_octrees#optimizing-collisions-and-picking)

---

<div class="post-metadata">

**Author:** ![JohnK](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/johnk/32/26764_2.png) [@JohnK](https://forum.babylonjs.com/u/JohnK)\
**Post date:** [July 14, 2020, 5:04pm UTC](https://forum.babylonjs.com/t/improve-raycast/12471/3 "2020-07-14T17:04:06Z")

</div>

@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](https://agitated-sammet-98ab48.netlify.app/teethmortonbvh)

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](https://github.com/BabylonJSGuide/bvh).

The good news is that this [topic](https://forum.babylonjs.com/t/path-tracing-in-babylonjs/11475) 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](https://forum.babylonjs.com/t/bounding-volume-hierarchies-bvh-for-ray-tracing-with-morton-codes/11423)

---

<div class="post-metadata">

**Author:** ![gbz](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/gbz/32/23780_2.png) [@gbz](https://forum.babylonjs.com/u/gbz)\
**Post date:** [July 14, 2020, 8:29pm UTC](https://forum.babylonjs.com/t/improve-raycast/12471/4 "2020-07-14T20:29:38Z")

</div>

Wowww, @JohnK, this is amazing 😃

---

<div class="post-metadata">

**Author:** ![Helder\_Oliveira](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/helder_oliveira/32/8700_2.png) [@Helder\_Oliveira](https://forum.babylonjs.com/u/Helder_Oliveira)\
**Post date:** [July 15, 2020, 1:14am UTC](https://forum.babylonjs.com/t/improve-raycast/12471/5 "2020-07-15T01:14:27Z")

</div>

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

Thanks a lot!!

---

<div class="post-metadata">

**Author:** ![JohnK](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/johnk/32/26764_2.png) [@JohnK](https://forum.babylonjs.com/u/JohnK)\
**Post date:** [July 16, 2020, 3:43pm UTC](https://forum.babylonjs.com/t/improve-raycast/12471/6 "2020-07-16T15:43:11Z")

</div>

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

> [@JohnK](#):
>
> at some point BVH (using a different method to generate) will become part of Babylon.js and may be generated using the GPU.

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.

> [@JohnK](#):
>
> here is an example with you teeth example [Morton BVH Pick](https://agitated-sammet-98ab48.netlify.app/teethmortonbvh)

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](https://agitated-sammet-98ab48.netlify.app/teethstandard)  
tradBVH - [Traditional BVH Pick](https://agitated-sammet-98ab48.netlify.app/teethtradbvh)  
mortonBVH - [Morton BVH Pick](https://agitated-sammet-98ab48.netlify.app/teethmortonbvh)

example timings in milliseconds after teeth have loaded.

```auto
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.

---

<div class="post-metadata">

**Author:** ![Christian.Herzog](https://avatars.discourse-cdn.com/v4/letter/c/e99b99/32.png) [@Christian.Herzog](https://forum.babylonjs.com/u/Christian.Herzog)\
**Post date:** [September 16, 2021, 2:40pm UTC](https://forum.babylonjs.com/t/improve-raycast/12471/7 "2021-09-16T14:40:06Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![JohnK](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/johnk/32/26764_2.png) [@JohnK](https://forum.babylonjs.com/u/JohnK)\
**Post date:** [September 16, 2021, 4:34pm UTC](https://forum.babylonjs.com/t/improve-raycast/12471/8 "2021-09-16T16:34:20Z")

</div>

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…

---

<div class="post-metadata">

**Author:** ![Christian.Herzog](https://avatars.discourse-cdn.com/v4/letter/c/e99b99/32.png) [@Christian.Herzog](https://forum.babylonjs.com/u/Christian.Herzog)\
**Post date:** [September 17, 2021, 5:39am UTC](https://forum.babylonjs.com/t/improve-raycast/12471/9 "2021-09-17T05:39:15Z")

</div>

Ok, thanks for the headups!
