In my scene, I need to update a specific material, that comes within a GLTF I’ve just imported.
The case is that, if I traverse the different channels, updating each one at a time, a visible artifact (blink-like) happens in the 3d model. Under this strategy, maybe the atomicMaterialsUpdate API can help here, but sadly I can’t find any example of use of PG about.
This said, I thought a complementary (better) approach would be to have a fully-updated backup material, and swap it with the active one by means of a unique assignation, but sadly scene.getMaterialByName("myMateriaNameHerel")=baclupMaterial throw an “invalid assignment left-hand side” exception.
I’ve reviewed de Scene and Material APIs, but none of them seems to fit my needs, so any help on would be really very appreciated.
There should be no blinking if you are using WebGL2, because we are using parallel shader compilation and don’t update the current material (effect) until the new effect is compiled when you change some properties of the material that lead to the creation of a new effect.
Would you be able to setup a repro in the Playground so that we can better help?
Regarding atomicMaterialsUpdate, here’s the doc:
However, I don’t think it will help with your problem at hand.
What you want to do is update the mesh.material property of all the meshes for which you want to change the material.
If you update an existing material, then yes it will be propagated to all meshes that use this material as you don’t change the material property. But if you create a new material, then you will have to loop over all the meshes to change it.
Ok, taking this PG as example, the central point here is that, in the aim to go the more efficient way (I mean with a whole-material-update API callI), I was hoping to be able to use the offendingMaterialUpdate method (lines 49 to 58), but instead it’s necessary the workingMaterialUpdate one.
BTW, as you can see, in order to “dispose” the PBR channels attached to a texture, I’ve assigned them straightforward to “null”, but maybe that is not the more convenient way, isn’t it?
Updating an existing material is probably better as it will save a little bit of memory. But I would not worry about the timings, as I don’t think you will be using those functions so many times in the course of your program?
Bringing the things just a little further, trying to update the PBR channels by means of their attached textures, something is not working here, as I get an exception raised from uniformBuffers.updateColor4ForUniform:
What is happening here?
Again, thank you very much for your time and advices.
P.S: Please note that it fails with both methods: updatetextredMaterialFromAux and updateTexturedMaterial.
You generally want mipmaps as it improves the rendering and performances, so noMipmap=false and samplingMode = BABYLON.Constants.TEXTURE_TRILINEAR_SAMPLINGMODE (those are the default values for these parameters).