# Trouble with light position in ShaderMaterial with InstancedMesh

**URL:** <https://discourse.threejs.org/t/trouble-with-light-position-in-shadermaterial-with-instancedmesh/17782>\
**Category:** Questions\
**Tags:** lights, materials, instanced-mesh, shaders\
**Created:** [August 9, 2020, 9:39pm UTC](https://discourse.threejs.org/t/trouble-with-light-position-in-shadermaterial-with-instancedmesh/17782 "2020-08-09T21:39:56Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Natch](https://avatars.discourse-cdn.com/v4/letter/n/858c86/32.png) [@Natch](https://discourse.threejs.org/u/Natch)\
**Post date:** [August 9, 2020, 9:39pm UTC](https://discourse.threejs.org/t/trouble-with-light-position-in-shadermaterial-with-instancedmesh/17782/1 "2020-08-09T21:39:56Z")

</div>

Hi,

I’ve been able to use instancedMesh without too much trouble until I tried to use a ShaderMaterial instead of a built-in material.  
I can’t get the lighting right, my guess is that the light position in the vertex shader needs to be updated to reflect the position of the instances, but I couldn’t find an example showing how to do this.

I recreated the problem by adding a ShaderMaterial to the webgl\_instancing\_dynamic example: [https://jsfiddle.net/Natcher86/b9kpLsjc/50/](https://jsfiddle.net/Natcher86/b9kpLsjc/50/)

You can see that the light position is relative to each instance instead of being global.

Any recommandation or example that explain how to handle lighting in ShaderMaterial ?

Thanks!

---

<div class="post-metadata">

**Author:** ![dubois](https://avatars.discourse-cdn.com/v4/letter/d/e9c0ed/32.png) [@dubois](https://discourse.threejs.org/u/dubois)\
**Post date:** [August 9, 2020, 11:58pm UTC](https://discourse.threejs.org/t/trouble-with-light-position-in-shadermaterial-with-instancedmesh/17782/2 "2020-08-09T23:58:02Z")

</div>

Would something like this suffice?

[https://jsfiddle.net/mqvnjzpc/](https://jsfiddle.net/mqvnjzpc/)

I moved the light to better see it, and i transformed the normals of the objects with the instance matrix. I think the issue with the normal matrix is that it’s not per object, it applies to the entire group of your boxes - which seems wrong, since they all have their own transformations.

---

<div class="post-metadata">

**Author:** ![Natch](https://avatars.discourse-cdn.com/v4/letter/n/858c86/32.png) [@Natch](https://discourse.threejs.org/u/Natch)\
**Post date:** [August 10, 2020, 12:13am UTC](https://discourse.threejs.org/t/trouble-with-light-position-in-shadermaterial-with-instancedmesh/17782/3 "2020-08-10T00:13:42Z")

</div>

Yes, that did the trick!  
Thank you.

---

<div class="post-metadata">

**Author:** ![fossas](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/fossas/32/40154_2.png) [@fossas](https://discourse.threejs.org/u/fossas)\
**Post date:** [May 5, 2021, 2:42pm UTC](https://discourse.threejs.org/t/trouble-with-light-position-in-shadermaterial-with-instancedmesh/17782/4 "2021-05-05T14:42:10Z")

</div>

You are awesome!

---

<div class="post-metadata">

**Author:** ![dubois](https://avatars.discourse-cdn.com/v4/letter/d/e9c0ed/32.png) [@dubois](https://discourse.threejs.org/u/dubois)\
**Post date:** [May 11, 2021, 5:11am UTC](https://discourse.threejs.org/t/trouble-with-light-position-in-shadermaterial-with-instancedmesh/17782/5 "2021-05-11T05:11:37Z")

</div>

Aw ❤ Thank you very much 🙂
