# Adding a predicate to select thin instance in ray picking function

**URL:** <https://forum.babylonjs.com/t/adding-a-predicate-to-select-thin-instance-in-ray-picking-function/52766>\
**Category:** Feature requests\
**Tags:** ray, picking\
**Created:** [August 13, 2024, 6:29am UTC](https://forum.babylonjs.com/t/adding-a-predicate-to-select-thin-instance-in-ray-picking-function/52766 "2024-08-13T06:29:07Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![SSR](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/ssr/32/27196_2.png) [@SSR](https://forum.babylonjs.com/u/SSR)\
**Post date:** [August 13, 2024, 6:29am UTC](https://forum.babylonjs.com/t/adding-a-predicate-to-select-thin-instance-in-ray-picking-function/52766/1 "2024-08-13T06:29:07Z")

</div>

I am using thin instances in my project to improve the performance and it has been very helpful!  
However, there are some instances I wish to exclude from the ray picking. Currently, the pick function only accepts a normal predicate that supports selecting meshes, but there is no way I can select which instances to be pickable.

From the need above, I would like to make a PR to add a `thinInstancePredicate` parameter to the pick function so that we have a way to skip the picking for unwanted thin instances.

The `pick` method in the `Scene` class would look like this after the change

> Scene.prototype.pick = function (  
> x: number,  
> y: number,  
> predicate?: (mesh: AbstractMesh) =\> boolean,  
> fastCheck?: boolean,  
> camera?: Nullable,  
> trianglePredicate?: TrianglePickingPredicate,  
> \_enableDistantPicking = false,  
> thinInstancePredicate?: (mesh: AbstractMesh, thinInstanceIndex: number) =\> boolean  
> ): PickingInfo

Please let me know what you think!

---

<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:** [August 13, 2024, 2:27pm UTC](https://forum.babylonjs.com/t/adding-a-predicate-to-select-thin-instance-in-ray-picking-function/52766/2 "2024-08-13T14:27:49Z")

</div>

I like it, I wonder if it needs to be a separate params or just a new parameter of the predicate, could be -1 in case of none thinInstanced meshes.

Feel free to make a PR for it 🙂

---

<div class="post-metadata">

**Author:** ![roland](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/roland/32/38940_2.png) [@roland](https://forum.babylonjs.com/u/roland)\
**Post date:** [September 14, 2024, 10:35am UTC](https://forum.babylonjs.com/t/adding-a-predicate-to-select-thin-instance-in-ray-picking-function/52766/4 "2024-09-14T10:35:24Z")

</div>

Guys, I’m always very cautios and undecided whether using predicates is a good approach. Sometimes it’s just better/faster to filter the resulting array.

If we allow predicates for thin instances (or anything else) I propose to optimize the code and check for the existence of a predicate only once when entering the function. If it exists, it should run the appropriate code which will call the predicate function for every thin instance and into a second part which will run basically the same code without checking for the predicate in each loop while evaluating the thin instances thus we save some cpu cycles checking for an existing predicate.

Some horrible pseudo code:

```auto
if (thinInstancePredicate) {
 return pickWithPredicate(..., thinInstancePredicate)
}
return pickWithoutPredicate(...)

function pickWithPredicate(..., thinInstancePredicate) {
  thinInstancesMatrices.each(
     if (thinInstancePredicate && thinInstancePredicate(...)) {
       addToPickingResult()
     }
  )
}

function pickWithoutPredicate(...) {
  thinInstancesMatrices.each(
 // if (thinInstancePredicate && thinInstancePredicate(...)) { <-- LEAVE THIS OUT
       addToPickingResult()
 // }
  )
}
```

---

<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:** [September 16, 2024, 4:00pm UTC](https://forum.babylonjs.com/t/adding-a-predicate-to-select-thin-instance-in-ray-picking-function/52766/5 "2024-09-16T16:00:09Z")

</div>

I think the cost of GC will overweight the cost of a single if

---

<div class="post-metadata">

**Author:** ![roland](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/roland/32/38940_2.png) [@roland](https://forum.babylonjs.com/u/roland)\
**Post date:** [September 17, 2024, 5:12am UTC](https://forum.babylonjs.com/t/adding-a-predicate-to-select-thin-instance-in-ray-picking-function/52766/6 "2024-09-17T05:12:53Z")

</div>

> [@Deltakosh](#):
>
> a single if

> [@roland](#):
>
> check for the existence of a predicate only once when entering the function

Yes, that’s the point 😉

---

<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:** [September 17, 2024, 3:46pm UTC](https://forum.babylonjs.com/t/adding-a-predicate-to-select-thin-instance-in-ray-picking-function/52766/7 "2024-09-17T15:46:38Z")

</div>

My point is that I vote for the if 🙂

---

<div class="post-metadata">

**Author:** ![roland](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/roland/32/38940_2.png) [@roland](https://forum.babylonjs.com/u/roland)\
**Post date:** [September 17, 2024, 5:24pm UTC](https://forum.babylonjs.com/t/adding-a-predicate-to-select-thin-instance-in-ray-picking-function/52766/8 "2024-09-17T17:24:44Z")

</div>

Me too. To be precise `if` in the loop or `if` outside the loop?

---

<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:** [September 17, 2024, 5:39pm UTC](https://forum.babylonjs.com/t/adding-a-predicate-to-select-thin-instance-in-ray-picking-function/52766/9 "2024-09-17T17:39:58Z")

</div>

outside probably
