# How do I pass an array of Vector3 into glsl through ShaderMaterial?

**URL:** <https://forum.babylonjs.com/t/how-do-i-pass-an-array-of-vector3-into-glsl-through-shadermaterial/37683>\
**Category:** Questions\
**Tags:** material\
**Created:** [January 27, 2023, 5:36pm UTC](https://forum.babylonjs.com/t/how-do-i-pass-an-array-of-vector3-into-glsl-through-shadermaterial/37683 "2023-01-27T17:36:51Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![vtranduc](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/vtranduc/32/27942_2.png) [@vtranduc](https://forum.babylonjs.com/u/vtranduc)\
**Post date:** [January 27, 2023, 5:36pm UTC](https://forum.babylonjs.com/t/how-do-i-pass-an-array-of-vector3-into-glsl-through-shadermaterial/37683/1 "2023-01-27T17:36:51Z")

</div>

Hello,

I am trying to pass a dynamic size array of Vector3 into fragment shader. I get that there are methods like setFloats which I could use to add dynamic size array of floats, but I want the shader to automatically update uniform value whenever values in Vector3 changes. I want something like

```auto
const v1 = new Vector3(1,2,3);
const v2 = new Vector3(4,5,6);

const material = new ShaderMaterial(...my params...);
material.setInt("uSize", 2);
material.setVector3s("uMyVectors", [v1, v2]);

```

Then inside fragment shader

```auto
uniform int uSize;
uniform vec3 uMyVectors[uSize];

```

The size of array, uSize, is flexible, that is why I try to avoid calling setVector3 for each vector. This is possible in OpenGL. I’m curious if Babylon has that support?

Any help would be appreciated!

---

<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:** [January 27, 2023, 9:38pm UTC](https://forum.babylonjs.com/t/how-do-i-pass-an-array-of-vector3-into-glsl-through-shadermaterial/37683/2 "2023-01-27T21:38:04Z")

</div>

material.setArray3 would be the way to go. You would need to expand your vector array in a float array first.

---

<div class="post-metadata">

**Author:** ![vtranduc](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/vtranduc/32/27942_2.png) [@vtranduc](https://forum.babylonjs.com/u/vtranduc)\
**Post date:** [January 30, 2023, 11:01am UTC](https://forum.babylonjs.com/t/how-do-i-pass-an-array-of-vector3-into-glsl-through-shadermaterial/37683/3 "2023-01-30T11:01:37Z")

</div>

Thank you for your response!

The problem with using that is that it does not automatically update the value inside the shader. For example, if I do something like:

```auto
const material = new ShaderMaterial(...my params...);
const v = new Vector3(1,2,3);
material.setArray3("uMyVectors", v.asArray());

```

Then the values of initial vector v will be passed onto shader, but when I update the vector

```auto
v.x = 5;

```

Then, the updated value would not be passed into the shader automatically. I could try to call setArray3 every frame, but I think that would be too inefficient?

---

<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:** [January 30, 2023, 4:30pm UTC](https://forum.babylonjs.com/t/how-do-i-pass-an-array-of-vector3-into-glsl-through-shadermaterial/37683/4 "2023-01-30T16:30:13Z")

</div>

Weird… Could you create a quick repro in the babylon Playground ?

It seems like it could be a bug as it should work like the other ones.

---

<div class="post-metadata">

**Author:** ![vtranduc](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/vtranduc/32/27942_2.png) [@vtranduc](https://forum.babylonjs.com/u/vtranduc)\
**Post date:** [February 6, 2023, 9:31pm UTC](https://forum.babylonjs.com/t/how-do-i-pass-an-array-of-vector3-into-glsl-through-shadermaterial/37683/5 "2023-02-06T21:31:36Z")

</div>

Sure! In here, the color of sphere is supposed to switch between red and purple every 2 seconds. It works with setColor3 but not with setArray3.

> **[Babylon.js Playground](https://playground.babylonjs.com/#BJDST5%232)**
>
> Babylon.js playground is a live editor for Babylon.js WebGL 3D scenes

The reason I don’t want to use setColor3 is because it only accepts 1 vec3 in shader, but I could have any number of colors.

I get that when we create array, the value is simply copied, not referenced. So, in case I want to pass flexible number of values of components of vector3 and color3, like

```auto
material.setFloats([v1.x, v1.y, v2.y, v2.z]);

```

How do I do so so that whenever vectors v1 and v2 update, the shader updates automatically without calling setFloats again? Is this possible?

---

<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:** [February 7, 2023, 5:19pm UTC](https://forum.babylonjs.com/t/how-do-i-pass-an-array-of-vector3-into-glsl-through-shadermaterial/37683/6 "2023-02-07T17:19:55Z")

</div>

> [@vtranduc](#):
>
> `setFloats`

It is because in this case you copy the color in the array. You could work directly with the array instead ? [https://playground.babylonjs.com/#BJDST5#3](https://playground.babylonjs.com/#BJDST5#3)

---

<div class="post-metadata">

**Author:** ![vtranduc](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/vtranduc/32/27942_2.png) [@vtranduc](https://forum.babylonjs.com/u/vtranduc)\
**Post date:** [February 7, 2023, 6:28pm UTC](https://forum.babylonjs.com/t/how-do-i-pass-an-array-of-vector3-into-glsl-through-shadermaterial/37683/7 "2023-02-07T18:28:31Z")

</div>

Thank you for your reply!  
So what if I want to use the values directly from vector/color? For example, I want to pass a position of object A. So when I move A, which changes values in its x y z of position, I want the shader to update automatically just like setVector3 or setColor3, except there may be any number of vectors/colors. Is it possible?  
Currently in my project I’m working on, I create a custom class that extends ShaderMaterial, then dynamically call setVector3 and setColor3 multiple times with dynamic name assigning, then dynamically edit the shader string in the constructor.

---

<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:** [February 8, 2023, 9:41pm UTC](https://forum.babylonjs.com/t/how-do-i-pass-an-array-of-vector3-into-glsl-through-shadermaterial/37683/8 "2023-02-08T21:41:28Z")

</div>

You need to copy the data from the object to the array every frame as the data structure are different. I would advise to do it in the onBindObservable of the material.
