Would forceCompilation help for NodeMaterials

In CAD system, we used ‘PBRMaterial’ and enabled ‘forceCompilation’ option. That seemed to help while loading multiple meshes as ‘effects’ (shaders) may be reused internally.
Now, we use a custom NodeMaterial created using NME that’s using PBR blocks internally.

We create & assign it for each mesh the same way we did while using Babylon’s PBR material.

By doing so, we noticed that model loading is slower. I suspect that it may be due to multiple shaders (effects) being created.

So,

  1. Would forceCompilation option have any favorable impact on custom ‘NodeMaterial’ similar to PBRMaterial?
  2. Is there any API to track on how many unique shaders (effects) are actually created?

Thanks in advance.

Adding @bghgary and @Evgeni_Popov about the force compilation and NME.

1 Like

I don’t think there’s a forceCompilation option (?) There’s a forceCompilation method that you can call to force compiling a material for a given mesh but it won’t make things faster by itself: the compilation will be done straight when calling the method instead of when the mesh needs to be rendered (if it ever needs to), but in the end it takes the same time to create the effect in both cases.

Maybe it takes a little more time in the NME case because the json files need to be parsed? Note that there is a forceCompilation method for the NodeMaterial class has it is extending Material which has this method.

Regarding 2/, there isn’t a public way to do it (I think), but you can look at engine._compiledEffects

2 Likes

Thanks. I’ll check on them. Though we don’t use json import, we used ‘Generate Code’ option from NME.
Either way, I guess it may take some time there.
Also, engine._compiledEffects should help me compare between my old implementation and my new code that’s based on NodeMaterial to see how many shaders (effects) are actually created.

Would it be helpful to capture a profile with the browser to see where things are slow?

1 Like

In this playground sample, I’ve created 2 NodeMaterial objects using same NME snippet & also two PBRMaterial objects. In _compiledEffects console log, I noticed 3 effects are being created.
Two are for NodeMaterials and only one for PBRMaterial.
Since code of Vertex & Fragment shaders in both NodeMaterial’s effects are identical, I expected only one effect to be created for the two NodeMaterial objects.

I read in Materials help docs that

“a new effect will be compiled unless an equivalent can be found in the cache.”

Hey we recently got some help on this. It sounds like you are asking the same question. Check this response from Deltakosh for how to fix.

4 Likes

Thanks for sharing the info @br-matt . That fix from @Deltakosh helped. Here’s the modified code sample
Now I see just one effect being created. Further, now it’s possible to set different color to the cloned NodeMaterial. That’s what we wanted!!!

3 Likes