# Small FPS dip/stutter when loading many PBRCustomMaterials

**URL:** <https://forum.babylonjs.com/t/small-fps-dip-stutter-when-loading-many-pbrcustommaterials/41205>\
**Category:** Questions\
**Tags:** material, pbr, performance, performance-profiler\
**Created:** [May 28, 2023, 9:45pm UTC](https://forum.babylonjs.com/t/small-fps-dip-stutter-when-loading-many-pbrcustommaterials/41205 "2023-05-28T21:45:52Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![vtange](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/vtange/32/34447_2.png) [@vtange](https://forum.babylonjs.com/u/vtange)\
**Post date:** [May 28, 2023, 9:45pm UTC](https://forum.babylonjs.com/t/small-fps-dip-stutter-when-loading-many-pbrcustommaterials/41205/1 "2023-05-28T21:45:52Z")

</div>

I have a scene that starts out with 143 textures loaded (based on how the camera is facing from the starting player location) initially but when I pan around and see more of the environment more textures are loaded (to 168, as shown in the image below).

When the “total textures” number goes up, there’s a small but noticeable lag/drop in FPS I’m sure it’s due Babylonjs loading these new textures. I think it’s some optimization made to improve load time but is there a setting on the API to just have it load all 168 at the start to avoid the stuttering at playtime? Thanks

![dip](https://us1.discourse-cdn.com/flex024/uploads/babylonjs/original/3X/4/1/41152677534a39369ef9735bafba6b59753760d3.png)

---

<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:** [May 29, 2023, 10:06am UTC](https://forum.babylonjs.com/t/small-fps-dip-stutter-when-loading-many-pbrcustommaterials/41205/2 "2023-05-29T10:06:52Z")

</div>

You can load the textures at start with `new Texture(...)` and wait for them to be loaded before going on?

---

<div class="post-metadata">

**Author:** ![vtange](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/vtange/32/34447_2.png) [@vtange](https://forum.babylonjs.com/u/vtange)\
**Post date:** [May 29, 2023, 5:55pm UTC](https://forum.babylonjs.com/t/small-fps-dip-stutter-when-loading-many-pbrcustommaterials/41205/3 "2023-05-29T17:55:58Z")

</div>

What do you do when the textures are all part of .glb files though? the texture is within the file

---

<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:** [May 29, 2023, 6:20pm UTC](https://forum.babylonjs.com/t/small-fps-dip-stutter-when-loading-many-pbrcustommaterials/41205/4 "2023-05-29T18:20:12Z")

</div>

All the textures are available in `scene.textures`, so you can wait for all of them to be ready after the file has been loaded.

---

<div class="post-metadata">

**Author:** ![vtange](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/vtange/32/34447_2.png) [@vtange](https://forum.babylonjs.com/u/vtange)\
**Post date:** [May 29, 2023, 6:20pm UTC](https://forum.babylonjs.com/t/small-fps-dip-stutter-when-loading-many-pbrcustommaterials/41205/5 "2023-05-29T18:20:17Z")

</div>

on further check it might not be the textures from the glb files…? What are these no\_name textures

 ![screenshot-localhost_3000-2023.05.29-11_18_26](https://us1.discourse-cdn.com/flex024/uploads/babylonjs/original/3X/9/7/9716f25f40c352ce430489376dc2742eaec9f3d6.png)  
 ![screenshot-localhost_3000-2023.05.29-11_18_16](https://us1.discourse-cdn.com/flex024/uploads/babylonjs/original/3X/e/4/e45fdc32f43bb8a4ec9978963520dc5ff8f6051b.png)  
these seem to be the majority of the added textures (going from 143 to ~168) if I check scene.textures

---

<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:** [May 29, 2023, 6:22pm UTC](https://forum.babylonjs.com/t/small-fps-dip-stutter-when-loading-many-pbrcustommaterials/41205/6 "2023-05-29T18:22:28Z")

</div>

It could be the bone textures… However, these textures are generated by code and are very small, so I don’t think they are responsible for the stuttering.

---

<div class="post-metadata">

**Author:** ![vtange](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/vtange/32/34447_2.png) [@vtange](https://forum.babylonjs.com/u/vtange)\
**Post date:** [May 29, 2023, 6:35pm UTC](https://forum.babylonjs.com/t/small-fps-dip-stutter-when-loading-many-pbrcustommaterials/41205/7 "2023-05-29T18:35:35Z")

</div>

yeah those do seem to relate to meshes with skeletons, if I remove all of those the texture counts starts and ends at basically 80. But there’s still a slight stutter when I pan from a spot with not as many meshes/textures to a spot with many meshes/textures for the first time. it’s only on the first time seeing those meshes too - after the initial lag spike it no longer lags when I view them

---

<div class="post-metadata">

**Author:** ![vtange](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/vtange/32/34447_2.png) [@vtange](https://forum.babylonjs.com/u/vtange)\
**Post date:** [May 29, 2023, 6:42pm UTC](https://forum.babylonjs.com/t/small-fps-dip-stutter-when-loading-many-pbrcustommaterials/41205/8 "2023-05-29T18:42:34Z")

</div>

it was the PBRCustomMaterial I used. I use a lot of them for every PBRMaterial I load from .glb so I can tweak the lighting. for each material I did something like:

```auto
            customMaterial.onBindObservable.add(function() {
                if(lumTexture) {
                    customMaterial.getEffect().setTexture('lumTexture', lumTexture);
                }
                customMaterial.getEffect().setColor3("shadeColor", scene.ambientColor);
            });

```

which you can see takes its toll by making a lot of .prepareEffect calls.

 ![aaaaa](https://us1.discourse-cdn.com/flex024/uploads/babylonjs/original/3X/0/4/04d63835d3f353c61679eb0f246928d67d38ce39.png)

---

<div class="post-metadata">

**Author:** ![vtange](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/vtange/32/34447_2.png) [@vtange](https://forum.babylonjs.com/u/vtange)\
**Post date:** [May 29, 2023, 6:45pm UTC](https://forum.babylonjs.com/t/small-fps-dip-stutter-when-loading-many-pbrcustommaterials/41205/9 "2023-05-29T18:45:13Z")

</div>

I think it’s the onBindObservable that causes the initial lag spike when you pan the material(s) into view for the first time. Is there a way to just have the materials calculate the effects when the page loads (so I can put the wait behind a loading screen)?

EDIT: on further check, the lagspike will happen whether I use onBindObservable to set effect params for PBRCustomMaterial’s Fragment\_Before\_FragColor or not. I dont think there’s a way around it then, other than simply not using PBRCustomMaterial

---

<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:** [May 30, 2023, 12:18am UTC](https://forum.babylonjs.com/t/small-fps-dip-stutter-when-loading-many-pbrcustommaterials/41205/10 "2023-05-30T00:18:22Z")

</div>

Yes, the problem with `PBRCustomMaterial` is that we must compile each one (we create a new effect for each) even if they share the same “structure” (meaning, their inputs don’t differ). Try using a regular `PBRMaterial` with a material plugin instead ([Material Plugins | Babylon.js Documentation](https://doc.babylonjs.com/features/featuresDeepDive/materials/using/materialPlugins)).

---

<div class="post-metadata">

**Author:** ![vtange](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/vtange/32/34447_2.png) [@vtange](https://forum.babylonjs.com/u/vtange)\
**Post date:** [May 30, 2023, 4:05am UTC](https://forum.babylonjs.com/t/small-fps-dip-stutter-when-loading-many-pbrcustommaterials/41205/11 "2023-05-30T04:05:35Z")

</div>

cool I’ll try using that then. I only really use the baseColor.rgb and finalColor.rgb in Fragment\_Before\_FragColor anyways.
