# Speeding up scene.pick() on renderloop

**URL:** <https://forum.babylonjs.com/t/speeding-up-scene-pick-on-renderloop/13148>\
**Category:** Questions\
**Created:** [August 6, 2020, 1:36am UTC](https://forum.babylonjs.com/t/speeding-up-scene-pick-on-renderloop/13148 "2020-08-06T01:36:51Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![morterra](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/morterra/32/25383_2.png) [@morterra](https://forum.babylonjs.com/u/morterra)\
**Post date:** [August 6, 2020, 1:36am UTC](https://forum.babylonjs.com/t/speeding-up-scene-pick-on-renderloop/13148/1 "2020-08-06T01:36:51Z")

</div>

I’m trying to allow the user to navigate through the scene from a FPS camera view (Universal camera), while pointer locked, and any mesh they hover will show a description of the mesh. I have this code all written already, but out of the blue it’s extremely laggy (see: [Javascript behaves differently when Chrome console is open/closed](https://forum.babylonjs.com/t/javascript-behaves-differently-when-chrome-console-is-open-closed/12834) for more info on that). At this point the only way to reduce the lag to a playable state is to remove scene.pick entirely, or just only update the picked mesh every x-number of frames. Removing it is basically out of the question, and only updating every x-number of frames is pretty ugly at runtime.

I have looked into and tried using ActionManagers, but they won’t work in this case because the only way to pick a hovered mesh from the scene is by using the pointer coordinates. When the pointer is locked, the user is expecting the center of the screen to be the focal point, not where the pointer was last when they locked to the screen. There is no way to center the pointer, and no way (that I’m aware of) to change the ActionManager’s coordinates to pick from, so I’m stuck with running scene.pick() every render.

I’ve tried picking with a ray that’s limited to the length of the user’s reach, but this doesn’t really help with the lag issue.

Any ideas on the most efficient way to constantly be picking from the scene?

---

<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:** [August 6, 2020, 2:13am UTC](https://forum.babylonjs.com/t/speeding-up-scene-pick-on-renderloop/13148/2 "2020-08-06T02:13:42Z")

</div>

You could define exactly which meshes to pick every frame:

```auto
meshes.forEach(mesh=> {
  const pick = ray.intersectsMesh(mesh, false);
  // do stuff with pick
});

```

---

<div class="post-metadata">

**Author:** ![morterra](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/morterra/32/25383_2.png) [@morterra](https://forum.babylonjs.com/u/morterra)\
**Post date:** [August 6, 2020, 2:20am UTC](https://forum.babylonjs.com/t/speeding-up-scene-pick-on-renderloop/13148/3 "2020-08-06T02:20:01Z")

</div>

The scene.pick() method returns the closest mesh, which is still the functionality I’m needing. If I displayed a hover for each mesh that the ray intersects it would display the hover text for every mesh that the ray intersects, as opposed to only displaying for the closest mesh.

---

<div class="post-metadata">

**Author:** ![Evgeni\_Popov](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/evgeni_popov/32/4432_2.png) [@Evgeni\_Popov](https://forum.babylonjs.com/u/Evgeni_Popov)\
**Post date:** [August 6, 2020, 10:05am UTC](https://forum.babylonjs.com/t/speeding-up-scene-pick-on-renderloop/13148/4 "2020-08-06T10:05:40Z")

</div>

In your case, a GPU picking could work because the picking occurs always at the same location: the center of the screen.

So, you could render each mesh with a different flat color to a render target texture and setup a camera viewport / scissor test to only render a very small part of the scene, namely the very center of it (maybe 3x3 pixels). Then you can retrieve the 3x3 texture to the CPU and read the color at the center of this texture, that will give the mesh picked. Maybe a 1x1 texture would work too…

It should be fast because given the very small dimension of the render target, most of the meshes will be culled early.

---

<div class="post-metadata">

**Author:** ![morterra](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/morterra/32/25383_2.png) [@morterra](https://forum.babylonjs.com/u/morterra)\
**Post date:** [August 11, 2020, 6:03am UTC](https://forum.babylonjs.com/t/speeding-up-scene-pick-on-renderloop/13148/5 "2020-08-11T06:03:35Z")

</div>

That would be ideal! I’m not seeing any documentation for GPU picking anywhere, is that something I will have to build from scratch or do you know where would be the best place to find more information on this?

---

<div class="post-metadata">

**Author:** ![Evgeni\_Popov](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/evgeni_popov/32/4432_2.png) [@Evgeni\_Popov](https://forum.babylonjs.com/u/Evgeni_Popov)\
**Post date:** [August 11, 2020, 7:09am UTC](https://forum.babylonjs.com/t/speeding-up-scene-pick-on-renderloop/13148/6 "2020-08-11T07:09:45Z")

</div>

You will need to build it from scratch. From this forum:

- [GPU Picking with mouse coordinate](https://forum.babylonjs.com/t/gpu-picking-with-mouse-coordinate/5899)
- [Turn off scene pick onPointerDown and onPointerUp](https://forum.babylonjs.com/t/turn-off-scene-pick-onpointerdown-and-onpointerup/12576)
- [Get accurate FacetData from mesh that is animated by a Skeleton](https://forum.babylonjs.com/t/get-accurate-facetdata-from-mesh-that-is-animated-by-a-skeleton/12563/8)
- [Concave Mesh Collisions](https://forum.babylonjs.com/t/concave-mesh-collisions/12298)

---

<div class="post-metadata">

**Author:** ![morterra](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/morterra/32/25383_2.png) [@morterra](https://forum.babylonjs.com/u/morterra)\
**Post date:** [August 11, 2020, 7:46am UTC](https://forum.babylonjs.com/t/speeding-up-scene-pick-on-renderloop/13148/7 "2020-08-11T07:46:06Z")

</div>

Awesome, thanks! I’m surprised a built in GPU picking doesn’t already exist 🤔

---

<div class="post-metadata">

**Author:** ![Evgeni\_Popov](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/evgeni_popov/32/4432_2.png) [@Evgeni\_Popov](https://forum.babylonjs.com/u/Evgeni_Popov)\
**Post date:** [August 11, 2020, 8:27am UTC](https://forum.babylonjs.com/t/speeding-up-scene-pick-on-renderloop/13148/8 "2020-08-11T08:27:37Z")

</div>

Well, you are free to contribute 😉

---

<div class="post-metadata">

**Author:** ![nasimiasl](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/nasimiasl/32/34_2.png) [@nasimiasl](https://forum.babylonjs.com/u/nasimiasl)\
**Post date:** [August 11, 2020, 12:25pm UTC](https://forum.babylonjs.com/t/speeding-up-scene-pick-on-renderloop/13148/9 "2020-08-11T12:25:44Z")

</div>

hey  
i test it in 2016

> **[GPU picking System](https://www.html5gamedevs.com/topic/22471-gpu-picking-system/)**
>
> hi in this topic we try to solve all problem in GPU picking system i think we have this questions : 1. how can pick changed vertex in gpu side 2. what information we can fetch

[https://www.babylonjs-playground.com/#JAG8B#24](https://www.babylonjs-playground.com/#JAG8B#24)

---

<div class="post-metadata">

**Author:** ![morterra](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/morterra/32/25383_2.png) [@morterra](https://forum.babylonjs.com/u/morterra)\
**Post date:** [August 12, 2020, 2:40am UTC](https://forum.babylonjs.com/t/speeding-up-scene-pick-on-renderloop/13148/10 "2020-08-12T02:40:46Z")

</div>

Wow, great work! It’s way over my head at the moment, but I’ll start working on it. Thank you!

---

<div class="post-metadata">

**Author:** ![Robin\_McLaut](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/robin_mclaut/32/5503_2.png) [@Robin\_McLaut](https://forum.babylonjs.com/u/Robin_McLaut)\
**Post date:** [February 9, 2022, 10:13am UTC](https://forum.babylonjs.com/t/speeding-up-scene-pick-on-renderloop/13148/11 "2022-02-09T10:13:42Z")

</div>

Hey morterra,

any update on this? I’m currently using scene.pick and for 10-15 elements that have to be picked the whole scene freezes for about 300 to 1500 ms when I’m doing this.  
scene.pick (ray.intersectsMesh is also slower than scene.pick) is not ideal at all ☹

---

<div class="post-metadata">

**Author:** ![sebavan](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/sebavan/32/57_2.png) [@sebavan](https://forum.babylonjs.com/u/sebavan)\
**Post date:** [February 21, 2022, 3:25pm UTC](https://forum.babylonjs.com/t/speeding-up-scene-pick-on-renderloop/13148/12 "2022-02-21T15:25:50Z")

</div>

a second for only 15 elements sounds like a lot ☹ could you provide a repro in the playground ?

---

<div class="post-metadata">

**Author:** ![Pryme8](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/pryme8/32/7212_2.png) [@Pryme8](https://forum.babylonjs.com/u/Pryme8)\
**Post date:** [February 21, 2022, 3:48pm UTC](https://forum.babylonjs.com/t/speeding-up-scene-pick-on-renderloop/13148/13 "2022-02-21T15:48:21Z")

</div>

@Robin_McLaut scene.pick should work with little to no problem on pretty advance setups and scenes. Id suspect there is something underlying not being addressed. If you could do like @sebavan requested and post a small PG we could help out more.

---

<div class="post-metadata">

**Author:** ![morterra](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/morterra/32/25383_2.png) [@morterra](https://forum.babylonjs.com/u/morterra)\
**Post date:** [February 28, 2022, 5:33am UTC](https://forum.babylonjs.com/t/speeding-up-scene-pick-on-renderloop/13148/14 "2022-02-28T05:33:12Z")

</div>

It’s been a long time, so I don’t remember what I ended up doing exactly. But I do remember multiple times having weird rotational camera lags which came from Chome. I never found a solution for that problem, they just appeared and fixed by themselves basically. Must’ve been an issue in Chome.

Even though the issue was probably with Chrome, I did spend hundreds of hours analyzing stress points and optimizing as much as possible, incase I could help mitigate the lag. I remember reducing DOM elements and their listeners, I used bounding boxes when possible, and wrote custom picker functions to limits the meshes I’m testing for collisions.

Your situation sounds a bit different, as you shouldn’t have that kind of lag with only a few objects. If you can replicate the issue in a Playground, it shouldn’t be too difficult to pinpoint the cause.
