I have about 100 meshes and each have unique diffureTexture. Performance is good.
Then, the next version is this:
Load a mesh
Create new CustomMaterial and apply it to mesh
This time the performance gets very bad… it seems that CustomMaterial gets recompiled all the time or something.
I find many threads about cloning CustomMaterial already etc. but still could not find this exact issue. My initial idea was that plain CustomMaterial without any customization would be just like StandardMaterial…
Could I somehow clone the CustomMaterial and change the diffuseTexture only without recompiling the whole shader?
It should definitely be possible to cache the shader and reuse the effect. Could you provide a quick repro in the playground I am worried we might have a cache issue ?
Hmm… I am wondering how StandardMaterial handles this “out-of-the-box” without any performance hit?
For CustomMaterial to be as effective should it somehow cache the internal shaders globally instead of always recompiling them? I feel uncomfortable that just changing the material color would lead into implementing a pool etc.
I am also thinking this workaround: Should I just inherit my own material from StandardMaterial and change the shader code there?
Or 2nd idea: Could I create uniforms for material color and texturemap and use only single instance of CustomMaterial? For color I think I know how to do this but not for diffuseTexture… Edit: This seems to be difficult as CustomMaterial returns undefined for getEffect()… At least it seems that I totally misunderstand StandardMaterial and CustomMaterial relationship by just trying to use intuition
You will still have some perf hit because there’s a creation of a material each frame, so some GC will kick in at some times. It’s not as bad as with a CustomMaterial because the StandardMaterial is able to reuse an existing effect and not create a new one each time, something the CustomMaterial can’t do currently.
That’s basically what CustomMaterial is doing.
You can do something like that, but it is not really supported as the _uniformBuffer variable is not exposed:
Thank you! This is just what I am looking for! I would be very happy to see this part of exposed API as well. I find it very usefull.
And about this:
const mat = new BABYLON.CustomMaterial(); // can also use StandardMaterial for that matter, no need for a CustomMaterial as no shader code is modified
… I do need to modify the shader in the real use case. That’s the exact reason to do this with CustomMaterial.