# Why is "Mesh Selection" time so high and how to reduce it?

**URL:** <https://forum.babylonjs.com/t/why-is-mesh-selection-time-so-high-and-how-to-reduce-it/47069>\
**Category:** Questions\
**Tags:** performance, mesh\
**Created:** [January 9, 2024, 5:29am UTC](https://forum.babylonjs.com/t/why-is-mesh-selection-time-so-high-and-how-to-reduce-it/47069 "2024-01-09T05:29:58Z")\
**Posts on this page:** 19\
**Page:** 1

<div class="post-metadata">

**Author:** ![mikeysee](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/mikeysee/32/27714_2.png) [@mikeysee](https://forum.babylonjs.com/u/mikeysee)\
**Post date:** [January 9, 2024, 5:29am UTC](https://forum.babylonjs.com/t/why-is-mesh-selection-time-so-high-and-how-to-reduce-it/47069/1 "2024-01-09T05:29:58Z")

</div>

Hi Guys,

Apologies for the lack of Playground with this one but you can see the issue in question in action at this page: [Storybook](https://stories.battletabs.com/iframe.html?args=&id=v2-battles-scene-battlescene--battle&viewMode=story)

You can open the inspector by clicking the small FPS counter in the top left.

The issue is we are seeing very high relative “Mesh Selection” times in our total frame time:

 ![chrome_pm8PYTzGhE](https://us1.discourse-cdn.com/flex024/uploads/babylonjs/original/3X/3/f/3ffb28043d695820654061dcffb0dfa74e3efe39.jpeg)

This issue persists even if you set chrome CPU to be 6x slower:

 ![image](https://us1.discourse-cdn.com/flex024/uploads/babylonjs/original/3X/0/3/03164c29102ddb600dc0c08f14a5b9a8836a0cbc.jpeg)

As I understand it “Mesh Selection” time is basically the time it takes to calculate and cull what is in the camera frustrum before being sent to the GPU and according to [these](https://doc.babylonjs.com/features/featuresDeepDive/scene/optimize_your_scene#freezing-the-active-meshes) docs we should be able to skip this culling step by setting the `mesh.alwaysSelectAsActiveMesh` to be true.

So we did this along with setting the scene to intermediate mode as [per this](https://doc.babylonjs.com/features/featuresDeepDive/scene/optimize_your_scene#intermediate-mode) suggestion but still nothing.

I even added this little freeze button:

![image](https://us1.discourse-cdn.com/flex024/uploads/babylonjs/original/3X/0/6/062fca3163b6c2e7900c7c2f8507e2db9e94ceae.png)

Which does the following when clicked:

```auto
 scene.freeActiveMeshes();
 scene.freezeMaterials();
 scene.freeProcessedMaterials();
 scene.freeRenderingGroups();
 console.log("FROZEN!");

```

But it does nothing.

Is there something else going on here? How can we reduce this rather large part of the frame time?

Can someone who knows more look through our scene in the inspector (link provided at the top) and see if there is something in there we arent doing correctly?

Cheers

---

<div class="post-metadata">

**Author:** ![xiehangyun](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/xiehangyun/32/30777_2.png) [@xiehangyun](https://forum.babylonjs.com/u/xiehangyun)\
**Post date:** [January 9, 2024, 7:06am UTC](https://forum.babylonjs.com/t/why-is-mesh-selection-time-so-high-and-how-to-reduce-it/47069/2 "2024-01-09T07:06:00Z")

</div>

[Mesh | Babylon.js Documentation (babylonjs.com)](https://doc.babylonjs.com/typedoc/classes/BABYLON.Mesh#cullingStrategy)  
[1 | Babylon.js Playground (babylonjs.com)](https://playground.babylonjs.com/#HH8T00#223)  
If you can load the model and try to release the comments for each row separately to see the Selection time, the latter two options take almost twice as long.  
However, this may cause inaccurate picking, please see the documentation link.

---

<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:** [January 9, 2024, 10:23am UTC](https://forum.babylonjs.com/t/why-is-mesh-selection-time-so-high-and-how-to-reduce-it/47069/3 "2024-01-09T10:23:11Z")

</div>

> [@mikeysee](#):
>
> `freeActiveMeshes`

I think you meant `freezeActiveMeshes`? You should see a difference if you use `freezeActiveMeshes` (note, however, that the time spent in meshes selection is quite low to start with).

---

<div class="post-metadata">

**Author:** ![mikeysee](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/mikeysee/32/27714_2.png) [@mikeysee](https://forum.babylonjs.com/u/mikeysee)\
**Post date:** [January 9, 2024, 12:15pm UTC](https://forum.babylonjs.com/t/why-is-mesh-selection-time-so-high-and-how-to-reduce-it/47069/4 "2024-01-09T12:15:28Z")

</div>

Oh wow how stupid! You are quite right it should be “freeze” not “free”.

Now an interesting thing happens when I press the “freeze” button on the hud:

See the video above. The Mesh Selection time drops to 0 as expected but the animations stop which I wouldnt have expected. Also the animation time is still really high.

Whats going on?

P.S. You mention:

> (note, however, that the time spent in meshes selection is quite low to start with).

This is true if you are talking about absolute time but I am on a very powerful PC, many of or players are on much worse machines and we are building for mobile too, so I am more interested in the relative percentage of frame time and in particular CPU bound operations.

---

<div class="post-metadata">

**Author:** ![xiehangyun](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/xiehangyun/32/30777_2.png) [@xiehangyun](https://forum.babylonjs.com/u/xiehangyun)\
**Post date:** [January 9, 2024, 1:42pm UTC](https://forum.babylonjs.com/t/why-is-mesh-selection-time-so-high-and-how-to-reduce-it/47069/5 "2024-01-09T13:42:43Z")

</div>

The model animation stopped because you froze the material, which invalidated the shape key change.  
[Baked Texture Animations | Babylon.js Documentation (babylonjs.com)](https://doc.babylonjs.com/features/featuresDeepDive/animation/baked_texture_animations)  
Using GPU animations should optimize animation performance.

---

<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:** [January 9, 2024, 9:07pm UTC](https://forum.babylonjs.com/t/why-is-mesh-selection-time-so-high-and-how-to-reduce-it/47069/6 "2024-01-09T21:07:35Z")

</div>

You can try to call `skeleton.prepare()` yourself (in `scene.onBeforeRenderObservable`, for eg) once you have frozen the active meshes. When active meshes are frozen, this method is not called anymore, and it is what makes the animations run.

---

<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:** [January 9, 2024, 9:44pm UTC](https://forum.babylonjs.com/t/why-is-mesh-selection-time-so-high-and-how-to-reduce-it/47069/7 "2024-01-09T21:44:52Z")

</div>

@mikeysee, let me tell you that your game is amazing and so pretty !!!

---

<div class="post-metadata">

**Author:** ![mikeysee](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/mikeysee/32/27714_2.png) [@mikeysee](https://forum.babylonjs.com/u/mikeysee)\
**Post date:** [January 9, 2024, 11:17pm UTC](https://forum.babylonjs.com/t/why-is-mesh-selection-time-so-high-and-how-to-reduce-it/47069/8 "2024-01-09T23:17:56Z")

</div>

wow! that did the trick! A MASSIVE increase in performance, our players are going to love us.

The only issue now is (as you can see above) everything else is now hidden and only sometimes shows up (which is really strange), for example see the cannonball right at the end only shows up just before it hits the water, then other times it shows up before, its almost as if its only being turned on and off on a 500ms timer or something.

Is there a way to force update just selected mesh’s as “alwaysSelectAsActiveMesh” doesnt seem to do the trick?

EDIT: Manually calling `skeleton.prepare()` does seems to increase the time spent in “Animations” but im hoping that this will go away when we do the below VAT optimization.

---

<div class="post-metadata">

**Author:** ![mikeysee](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/mikeysee/32/27714_2.png) [@mikeysee](https://forum.babylonjs.com/u/mikeysee)\
**Post date:** [January 9, 2024, 11:18pm UTC](https://forum.babylonjs.com/t/why-is-mesh-selection-time-so-high-and-how-to-reduce-it/47069/9 "2024-01-09T23:18:38Z")

</div>

Yes! We have this on our list to look at next and as we arent using blended animations it hopefully will be a good candidate for us.

---

<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:** [January 10, 2024, 11:15am UTC](https://forum.babylonjs.com/t/why-is-mesh-selection-time-so-high-and-how-to-reduce-it/47069/10 "2024-01-10T11:15:31Z")

</div>

> [@mikeysee](#):
>
> Is there a way to force update just selected mesh’s as “alwaysSelectAsActiveMesh” doesnt seem to do the trick?

`alwaysSelectAsActiveMesh` make sure the mesh will always pass the culling stage and will eventually be drawn. However, you should set this property to true for all your meshes before calling `freezeActiveMeshes`, as only the meshes that were active will be drawn afterwards. Also, if you create new meshes after `freezeActiveMeshes` has been called, they won’t be visible! You will have to unfreeze, set `alwaysSelectAsActiveMesh=true` for all new meshes and call `freezeActiveMeshes` again:

The box (created after `freezeActiveMeshes` have been called) won’t appear:

> **[Babylon.js Playground](https://playground.babylonjs.com/#I6SALI%231)**
>
> Babylon.js playground is a live editor for Babylon.js WebGL 3D scenes

The box will appear:

> **[Babylon.js Playground](https://playground.babylonjs.com/#I6SALI)**
>
> Babylon.js playground is a live editor for Babylon.js WebGL 3D scenes

If that still does not work, I think we will need a repro to be able to help more.

---

<div class="post-metadata">

**Author:** ![mikeysee](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/mikeysee/32/27714_2.png) [@mikeysee](https://forum.babylonjs.com/u/mikeysee)\
**Post date:** [January 10, 2024, 11:11pm UTC](https://forum.babylonjs.com/t/why-is-mesh-selection-time-so-high-and-how-to-reduce-it/47069/11 "2024-01-10T23:11:23Z")

</div>

Oh indeed this does work! I have to unfreeze the scene then freeze it again after every mesh creation.

Is there any automated way to do this built into the engine?

---

<div class="post-metadata">

**Author:** ![neu5](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/neu5/32/24720_2.png) [@neu5](https://forum.babylonjs.com/u/neu5)\
**Post date:** [January 11, 2024, 7:47am UTC](https://forum.babylonjs.com/t/why-is-mesh-selection-time-so-high-and-how-to-reduce-it/47069/12 "2024-01-11T07:47:41Z")

</div>

Sorry for the offtopic but what kind of game is that? 😃 Some kind of battleship game?  
Is it online so I can play it? 😉

---

<div class="post-metadata">

**Author:** ![mikeysee](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/mikeysee/32/27714_2.png) [@mikeysee](https://forum.babylonjs.com/u/mikeysee)\
**Post date:** [January 11, 2024, 8:01am UTC](https://forum.babylonjs.com/t/why-is-mesh-selection-time-so-high-and-how-to-reduce-it/47069/13 "2024-01-11T08:01:53Z")

</div>

Yes indeed 🙂 its [https://battletabs.io/](https://battletabs.io/)

We are currently getting close to a mobile release too, using Bablyon of course

---

<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:** [January 11, 2024, 10:18am UTC](https://forum.babylonjs.com/t/why-is-mesh-selection-time-so-high-and-how-to-reduce-it/47069/14 "2024-01-11T10:18:14Z")

</div>

> [@mikeysee](#):
>
> Is there any automated way to do this built into the engine?

No, there’s no automated way, as it could be slow to unfreeze/freeze each time a new mesh is created, in case you create several meshes at once.

---

<div class="post-metadata">

**Author:** ![mikeysee](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/mikeysee/32/27714_2.png) [@mikeysee](https://forum.babylonjs.com/u/mikeysee)\
**Post date:** [January 11, 2024, 11:15pm UTC](https://forum.babylonjs.com/t/why-is-mesh-selection-time-so-high-and-how-to-reduce-it/47069/15 "2024-01-11T23:15:54Z")

</div>

Okay, thats a shame but understood.

---

<div class="post-metadata">

**Author:** ![mikeysee](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/mikeysee/32/27714_2.png) [@mikeysee](https://forum.babylonjs.com/u/mikeysee)\
**Post date:** [January 13, 2024, 1:12am UTC](https://forum.babylonjs.com/t/why-is-mesh-selection-time-so-high-and-how-to-reduce-it/47069/16 "2024-01-13T01:12:38Z")

</div>

Ughh… this really is very messy.

I have to freeze and unfreeze too when a mesh becomes visible or invisible and then there are still lots of issues

There has got to be a better way of getting rid of this mesh selection time surely?

---

<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:** [January 14, 2024, 11:31pm UTC](https://forum.babylonjs.com/t/why-is-mesh-selection-time-so-high-and-how-to-reduce-it/47069/17 "2024-01-14T23:31:15Z")

</div>

If you have a lot of meshes, implementing `scene.getActiveMeshCandidates` could help (see [Curious about this.totalVertices.addCount() in the evaluate active mesh loop - #27 by Evgeni\_Popov](https://forum.babylonjs.com/t/curious-about-this-totalvertices-addcount-in-the-evaluate-active-mesh-loop/43113/27)).

@JCPalmer has also [tried something of his own](https://forum.babylonjs.com/t/make-scene-freezeactivemeshes-much-more-usable/25964/9), perhaps that might be of use to you?

There’s always the possibility of overloading `scene._evaluateActiveMeshes` with your own implementation, removing anything you don’t need.

It would also be useful to take a performance snapshot and see where most of the time is spent in the `_evaluateActiveMeshes` function (this function is doing mesh culling, animates skeletons, dispatch sub-meshes, etc).

---

<div class="post-metadata">

**Author:** ![mikeysee](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/mikeysee/32/27714_2.png) [@mikeysee](https://forum.babylonjs.com/u/mikeysee)\
**Post date:** [January 15, 2024, 12:12am UTC](https://forum.babylonjs.com/t/why-is-mesh-selection-time-so-high-and-how-to-reduce-it/47069/18 "2024-01-15T00:12:38Z")

</div>

Okay thanks, ill look into these suggestions

---

<div class="post-metadata">

**Author:** ![mikeysee](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/mikeysee/32/27714_2.png) [@mikeysee](https://forum.babylonjs.com/u/mikeysee)\
**Post date:** [January 15, 2024, 3:09am UTC](https://forum.babylonjs.com/t/why-is-mesh-selection-time-so-high-and-how-to-reduce-it/47069/19 "2024-01-15T03:09:34Z")

</div>

ye it looks like @JCPalmer was right on the same thinking train as me. I dont know if he is still lurking around on these forums but ill see if he custom Scene implementation works (if I can work out how to slot that into the codebase).

I have also seen the @brunobg name come up in a few places on this forum too when dealing with performance issues around CPU bottlenecks (Mesh Selection and Animations baking etc). I wonder if he has any ideas
