Shader texture changing problem

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?

https://www.babylonjs-playground.com/#798ASY#3

Definitely a bug! I’m on it

Ok found and will be fixed in next nightly

1 Like

Thanks! I was going crazy trying to figure out what I did wrong with the texture setting!

fixed was pushed

2 Likes

Has this fixed been pushed to the latest preview build for @babylonjs/core on npm?

not yet. Next npm update will probably be this friday

Actually I might need to do one today :slight_smile: I ll keep you posted

4.2.0-alpha.10 is out

2 Likes

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

Can it be related to @Tyzor issue?

Thanks :wink:

1 Like

Can you repro in the PG?

1 Like

Hello @Deltakosh,

I manage to reproduce the issue. It seems that if you set the shader texture later after buiding it, then it won’t work.

Working example:
https://playground.babylonjs.com/#6NBTZ0#3

Exact same thing with a simple timeout:
https://playground.babylonjs.com/#6NBTZ0#2

And there is absolutely no error in the console.

Thanks for your help, Pichou

1 Like

Hey @PichouPichou,

The textures must be listed in the samplers array, not the uniforms when creating the shader material:
https://playground.babylonjs.com/#6NBTZ0#4

3 Likes

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… :face_with_monocle:

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. :slightly_smiling_face:

2 Likes

Okay nice!

Thanks a lot for the clarification @Evgeni_Popov

See you :wink:

1 Like

Hello everybody. Hope you are all doing great!

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).

1 Like

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.

I also use an included shader that manages the textureMask sampler. I reproduced that in the playground but still no bug:

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).

2 Likes