Question about material defines

I have a lot of opaque geometries. Some of them have UVs but the others have only positions and normals. Let’s say they all use the same PBR materials with almost the same configuration. So basically I will have two define sets, one has uv and Albedo texture as true and the other as false. As a result Babylon will have to generate two shaders.

So how can we guarantee the best rendering performance?

  1. Does Babylon cache the shaders so that we don’t recompile it when we detect an attribute change?
  2. Do we need to sort the geometries so that we can avoid switching between the shaders too frequently ?


  1. Yes totally:)
  2. It is a good idea as the system keeps a cache of the latest used material. But even if you do not sort, we have a lot of cache all over the place and you will see that we only change the barely minimum things
  1. Could you please point me out where I can find the code about shader caching?
  2. After changing WebGL program, how can we update minimum uniforms? Should’t we re-upload all the uniforms? Where can I find relative code in Babylonjs?

Many thanks!

1.Babylon.js/engine.ts at master · BabylonJS/Babylon.js · GitHub
2. It depends if you use ubo or pure uniforms. UBO will be updated once per frame. uniforms are cached and update only if they change:Babylon.js/effect.ts at master · BabylonJS/Babylon.js · GitHub

1 Like