Trouble with light position in ShaderMaterial with InstancedMesh

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/

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!

1 Like

Would something like this suffice?

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.

2 Likes

Yes, that did the trick!
Thank you.

1 Like