What's the "proper" way to dynamically update defines on a material?

Hello!

A bit of an open question here, but I’m looking for input of what approach you would recommend for updating ‘defines’ on a ShaderMaterial dynamically. I want to make sure we play nicely with the effect/mesh caching systems and rendering steps inside Babylon :slight_smile:

Our setup is that we have custom shader materials that we use like any other in Babylon, and we have some configurations put behind the preprocessor. Some of these options are static for the lifetime of the application, others change a few times, and others will differ for different meshes (ie will require several parallel effects for one material in a frame).

Should be noted here that we do not consider parallel shader compilation at the moment, so it can be assumed to always be disabled :slight_smile:

From experimentation, it seems like updating defines (using Material.setDefine) inside Material.isReady seems to work, but hard to tell if this is the “intended” way.

Thanks in advance :slight_smile:
-Tore

cc @Evgeni_Popov

Yes, you can change the defines of a ShaderMaterial at any time by calling ShaderMaterial.setDefine. You can call it in your own implementation of isReady, but in that case you should do it before calling the isReady function of the base class.

2 Likes

Great! Thanks for the info!

The mesh parameter of the function is optional, but seems like it’s always defined during rendering. Is this a behaviour you rely on internally for your own material implementations? Just want to know if it’s something we should count on or not :slight_smile:

Yes, you can assume mesh is always passed to the isReady function.

1 Like