# Is there any way to bind ShaderMaterial uniforms per mesh?

**URL:** <https://forum.babylonjs.com/t/is-there-any-way-to-bind-shadermaterial-uniforms-per-mesh/41626>\
**Category:** Questions\
**Created:** [June 15, 2023, 10:59am UTC](https://forum.babylonjs.com/t/is-there-any-way-to-bind-shadermaterial-uniforms-per-mesh/41626 "2023-06-15T10:59:50Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![MiikaH](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/miikah/32/23054_2.png) [@MiikaH](https://forum.babylonjs.com/u/MiikaH)\
**Post date:** [June 15, 2023, 10:59am UTC](https://forum.babylonjs.com/t/is-there-any-way-to-bind-shadermaterial-uniforms-per-mesh/41626/1 "2023-06-15T10:59:50Z")

</div>

I’m trying to bind ShaderMaterial uniforms to specific values per mesh.

Initially it seemed like calling set uniform method at `onBindObservable` would be the way, but because `ShaderMaterial` calls `onBindObservable` _after_ it has applied the specified uniform values it only makes the next rendered meshes use the desired value.

Then I was thinking that maybe I could set the values on the “effect” directly, but that wouldn’t work when the material can have multiple effects for different attribute combinations.

Here is a Playground test case. It generates random colors for each mesh and tries to apply them on material bind:

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

Any suggestions how to make this work without having to inject custom code to Babylon.js function prototypes?

---

<div class="post-metadata">

**Author:** ![RaananW](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/raananw/32/28955_2.png) [@RaananW](https://forum.babylonjs.com/u/RaananW)\
**Post date:** [June 15, 2023, 12:30pm UTC](https://forum.babylonjs.com/t/is-there-any-way-to-bind-shadermaterial-uniforms-per-mesh/41626/2 "2023-06-15T12:30:04Z")

</div>

Would that be the expected behavior? [ShaderMaterial bind per mesh values | Babylon.js Playground (babylonjs-playground.com)](https://www.babylonjs-playground.com/#HP8QPZ#2)

---

<div class="post-metadata">

**Author:** ![MiikaH](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/miikah/32/23054_2.png) [@MiikaH](https://forum.babylonjs.com/u/MiikaH)\
**Post date:** [June 15, 2023, 1:06pm UTC](https://forum.babylonjs.com/t/is-there-any-way-to-bind-shadermaterial-uniforms-per-mesh/41626/3 "2023-06-15T13:06:45Z")

</div>

Yes that is correct indeed. 👍

I suppose that means that `getEffect()` when called in `onBindObservable` always returns the active effect for rendering, even if shaderMaterial internally has multiple effects or even material override active?

---

<div class="post-metadata">

**Author:** ![MiikaH](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/miikah/32/23054_2.png) [@MiikaH](https://forum.babylonjs.com/u/MiikaH)\
**Post date:** [June 15, 2023, 1:15pm UTC](https://forum.babylonjs.com/t/is-there-any-way-to-bind-shadermaterial-uniforms-per-mesh/41626/4 "2023-06-15T13:15:09Z")

</div>

Looks good though. I created a test that better matches my use case of render target + material override and it still seems to be working: [https://www.babylonjs-playground.com/#HP8QPZ#3](https://www.babylonjs-playground.com/#HP8QPZ#3)

I’m still not sure if there is any documentation that `getEffect()` is guaranteed to return the active effect in `onBindObservable`, but at least it “seems” to work so I’ll take it. Hopefully it doesn’t break in the future. 🙂

---

<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:** [June 15, 2023, 1:48pm UTC](https://forum.babylonjs.com/t/is-there-any-way-to-bind-shadermaterial-uniforms-per-mesh/41626/5 "2023-06-15T13:48:48Z")

</div>

yup getEffect in onBind will be all good even in the future.
