# Re: ShaderMaterial and effects

**URL:** <https://forum.babylonjs.com/t/re-shadermaterial-and-effects/16892>\
**Category:** Bugs\
**Tags:** material\
**Created:** [December 23, 2020, 8:24pm UTC](https://forum.babylonjs.com/t/re-shadermaterial-and-effects/16892 "2020-12-23T20:24:28Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![SarkyKeshy](https://avatars.discourse-cdn.com/v4/letter/s/c37758/32.png) [@SarkyKeshy](https://forum.babylonjs.com/u/SarkyKeshy)\
**Post date:** [December 23, 2020, 8:24pm UTC](https://forum.babylonjs.com/t/re-shadermaterial-and-effects/16892/1 "2020-12-23T20:24:28Z")

</div>

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

[ShaderMaterial | Babylon.js Documentation](https://doc.babylonjs.com/typedoc/classes/babylon.shadermaterial#setarray4)

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](https://doc.babylonjs.com/advanced_topics/introToShaders/shaderCodeInBjs#putting-shader-code-in-babylonjs)

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.

---

<div class="post-metadata">

**Author:** ![sebavan](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/sebavan/32/57_2.png) [@sebavan](https://forum.babylonjs.com/u/sebavan)\
**Post date:** [December 23, 2020, 8:44pm UTC](https://forum.babylonjs.com/t/re-shadermaterial-and-effects/16892/2 "2020-12-23T20:44:56Z")

</div>

Hello and welcome 🙂

Could you share a repro in the playground ? [https://playground.babylonjs.com/](https://playground.babylonjs.com/) So we could all have a look at what is going wrong ?

---

<div class="post-metadata">

**Author:** ![Evgeni\_Popov](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/evgeni_popov/32/4432_2.png) [@Evgeni\_Popov](https://forum.babylonjs.com/u/Evgeni_Popov)\
**Post date:** [December 23, 2020, 9:51pm UTC](https://forum.babylonjs.com/t/re-shadermaterial-and-effects/16892/3 "2020-12-23T21:51:13Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![SarkyKeshy](https://avatars.discourse-cdn.com/v4/letter/s/c37758/32.png) [@SarkyKeshy](https://forum.babylonjs.com/u/SarkyKeshy)\
**Post date:** [December 23, 2020, 10:06pm UTC](https://forum.babylonjs.com/t/re-shadermaterial-and-effects/16892/4 "2020-12-23T22:06:53Z")

</div>

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 😛

---

<div class="post-metadata">

**Author:** ![SarkyKeshy](https://avatars.discourse-cdn.com/v4/letter/s/c37758/32.png) [@SarkyKeshy](https://forum.babylonjs.com/u/SarkyKeshy)\
**Post date:** [December 23, 2020, 11:24pm UTC](https://forum.babylonjs.com/t/re-shadermaterial-and-effects/16892/5 "2020-12-23T23:24:39Z")

</div>

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

> **[Babylon.js Playground](https://playground.babylonjs.com/#YF6L4T%239)**

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.

---

<div class="post-metadata">

**Author:** ![SarkyKeshy](https://avatars.discourse-cdn.com/v4/letter/s/c37758/32.png) [@SarkyKeshy](https://forum.babylonjs.com/u/SarkyKeshy)\
**Post date:** [December 23, 2020, 11:30pm UTC](https://forum.babylonjs.com/t/re-shadermaterial-and-effects/16892/6 "2020-12-23T23:30:11Z")

</div>

> **[Babylon.js Playground](https://playground.babylonjs.com/#YF6L4T%2310)**

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

---

<div class="post-metadata">

**Author:** ![Evgeni\_Popov](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/evgeni_popov/32/4432_2.png) [@Evgeni\_Popov](https://forum.babylonjs.com/u/Evgeni_Popov)\
**Post date:** [December 23, 2020, 11:41pm UTC](https://forum.babylonjs.com/t/re-shadermaterial-and-effects/16892/7 "2020-12-23T23:41:41Z")

</div>

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

> **[Babylon.js Playground](https://playground.babylonjs.com/#YF6L4T%2311)**

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:

> **[Babylon.js Playground](https://playground.babylonjs.com/#YF6L4T%2312)**

---

<div class="post-metadata">

**Author:** ![SarkyKeshy](https://avatars.discourse-cdn.com/v4/letter/s/c37758/32.png) [@SarkyKeshy](https://forum.babylonjs.com/u/SarkyKeshy)\
**Post date:** [December 24, 2020, 4:15am UTC](https://forum.babylonjs.com/t/re-shadermaterial-and-effects/16892/8 "2020-12-24T04:15:41Z")

</div>

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.
