Mesh "flickering" when setting material initially

Hi folks,

I faced an issue when applying materials, as shown in this PG example.
In the top corner of the viewer there is a “Set material” button, which sets a randomly generated PBR material on one of the 2 spheres.

When clicking the button the first time it takes some split seconds to apply the material.
In this time the sphere gets invisible and you have some kind of “flickering” effect.
However when clicking the button again, the material change is instant, no matter on which mesh the material is applied.

Is this a known/desired behaviour and is there some way to get around it?

That’s because a new shader has to be compiled by the system and it takes some time. So, until the shader is compiled, the sphere can’t be rendered correctly, hence the flickering.

You can force the compilation of the new material for your mesh and update the mesh material only when the compilation is finished:

@Evgeni_Popov Thx for the very fast response, seems to work fine!
Is it ok to do this everytime (waiting for the forceCompilationAsync callback) when applying a material or can there be drawbacks?
For example does it take significantly longer when applying materials when the shader is already compiled?

No, if the shader (effect) is already existing and does not need to be compiled, you will only incur a penalty of one frame at most, as the forceCompilationAsync is using a promise to deliver the result. If you want to avoid this delay, you can use forceCompilation instead.

1 Like