Re: ShaderMaterial and effects

According to the documentation the Shader material should have a setArray4()

ShaderMaterial | Babylon.js Documentation

However. This does not seem to be making itself available on a custom shader. There is also mention of it being available via an effect. However after putting the custom vertex and fragment shaders into the effects store and then attaching them to a material as in your documentation.

Putting Shader Code Into Babylon.js | Babylon.js Documentation

If I then try to get the effects attached to the shaderMaterial I just comes back null. So can’t get the effect to attach a Vec4 array uniform to.
Any idea where I am going wrong. If it can’t be answered will try to knock up a mini project to demonstrate.

Hello and welcome :slight_smile:

Could you share a repro in the playground ? So we could all have a look at what is going wrong ?

Note also that setArray4 is really for a uniform which is an array of an array of 4 items([1,2,3,4],[5,6,7,8],[9,10,11,12]), not for an array of 4 items ([1,2,3,4])! Sometimes it is confusing and people use setArray4 to setup an array of 4 items (you should use setFloats or setVector2 / setVector3 / setVector4 for that).

1 Like

Will try sort out a simple example in the morning. Can get around it by running a for loop and setVector4(“data[n]”).Will look into uniformBuffer but not enough examples for me to work out how to attach it to my shader. Especially when i cant get the basics to work :stuck_out_tongue:

Ah thanks for that didn’t know it.
Still couldn’t sleep as so edited an example I did before to demonstrate.

The .setVector4Array or its equivalent command does not exist at this level, only against the effect apparently and just crashes the code.
The .setColor4Array is just too limiting if wanting to send data other than colors.

The .getEffect() just returns null. So can’t access the .setVector4Array through that either.
Actually ideally its the effect.setFloatArray4 I want. But only attached to an effect I can’t find.

This ones a little better shows a few of the things I tried which failed.

setArray4 expects an array of floats, not an array of Vector4:

Also, you can set uniforms that don’t change every frame only a single time, no need to do it in registerBeforeRender (at least when setting the uniforms through the shader material).

If you want to set some uniforms through the effect instead of the shader material, you should use the shader.onBindObservable observable:


Thanks you very much. Just could not see how to access the data. Will try out various combinations and see which works best in my actual situation. The data I am trying to pass will not usually require much changing but small bits of the array will change regularly. Still I can set them individually using setVector4() so will come down to whichever method works the best.

1 Like