I managed to finally recreate a problem in the playground regarding shaders. For some reason the NodeMaterial I have attached to meshes in my scene is using a textureSampler from another ShaderMaterial. You can see in the playground below that occasionally the texture of the shields will switch. I’m assuming it has something to do with the depth of the objects in the scene and the order which they are drawn.
If I turn off needAlphaBlending in the ShaderMaterial the texture problem is fixed but then I don’t get proper alpha blending on the laser effect and it just fills in white for the transparent parts. Is there something I’m doing wrong here?
I have exactly the same issue with the PostProcess class using a custom shader. The PostProcess will use a texture from a separated ShaderMaterial. Yet I made sure to never use “textureSampler” name in all the other Shader code.
I am currently on babylonJS version 5.0.0-alpha.31
Heya, I noticed that the documentation says they “need not appear in the uniforms array” but doesn’t mention the need for them to be in the samplers array…
Edit: here’s a little PR to update the doc, for example, that adds the samplers array to the code snippet and rewords that line to explain that the textures need to be in the sampers array.
I re-open this topic as I just encounter something similar but between ShaderMaterial and PostProcess this time.
As you can see in this playground, I have a ShaderMaterial and a PostProcess that use the same Shader code. If I set the sampler textureMask on the PostProcess only, it will automatically be used by the ShaderMaterial also.
In my specific use case, this is problematic as I sometimes change the sampler on a ShaderMaterial on purpose and it will also change it in my PostProcess where I don’t want it.
Thanks a lot for your help and have a great weekend!
It’s a bug not setting the texture(s) declared in a samplers list of ShaderMaterial, either by using ShaderMaterial.setTexture() or through the effect (ShaderMaterial.getEffect().setTexture()) in the ShaderMaterial.onBindObservable observable.
If you set a texture using ShaderMaterial.setTexture(), this texture will be used only by the shader, not by the post process. For the post process, you must assign the right texture in the onApply observable.
So:
should not happen. Are you able to provide a repro for this one?
Also note that in your PG, you must also provide a texture for textureSampler when using the shader material (for the post process it is automatically bound). As explained above, it’s a bug (or at least an undefined behaviour) not to provide a value for all textures used by the shader (in WebGPU, it’s likely to crash if you don’t bind a texture that is used by the shader code).
Unfortunately I can’t manage to reproduce the bug in the playground.
One thing I have notice is that if the Mesh with the ShaderMaterial leaves the viewport, the Postprocess will use the right sampler again but if the Mesh is back in the viewport, the bug also is.
Without a repro it’s going to be difficult because I don’t see how it’s possible given the existing code (setting the texture for the post process and for the shader material are two different code paths).