Stylized nodeMaterial renders quantized lighting in wrong direction on mesh?

I am trying to create a two tone shader following the Create Your Own Toon Shader youtube video from Nick from BabylonJS team.

To test shaders and debug I created this simple playground with a spotlight rotating around the sphere.
It uses the ParseFromSnippetAsync so we can easily load some NME shaders to illustrate what I am not understanding or getting right here.

Before loading a nodeMaterial the lighting appears normal or lets say “default” if not realistic. But when I am building the shader it quickly flips for lack of a better description. One would assume that the ambient color not be colored first and then specular highlights on the back of the sphere, away from the light? SI am new to this so its probably somthing obvious I dont understand.

// Final version of BJS shader from guide

The completed shader #81NNDY#2 can be used to illustrate the issue, but the problem appears for me as soon as I have gotten the N Dot L and feed it into the fragment shader the issue is there.

// Start of shader

Can someone smarter than me point out what I am missing?

Hi @MacGuffin and welcome to the forum!

N Dot L return a value with a range of [-1 … 1]
If you want to convert it to a texture coordinate or a light value, you’ll need to chane its range like *2 + 0.5

Thanks for the reply!

I dont know if that is relevant for my problem? I am not trying to add light intensity or map the value from a texture ramp.

I am trying to figure out why the logic of what parts get colored seems backwards after applying the N Dot L.

If you use the simple shader example slug #J5ZSAN in my playground example Babylon.js Playground you will see what I mean hopefully. When we replace the default light with this very simple shader the light source will light the back of the sphere and the front facing normal towards the light will be dark.


I tried changing the order of inputs of (N) and (L) to input left and right on the Dot product node but it changes nothing.

I think I get it.
NME shader is not correct.
N.L is 1 when mesh normal and light direction have the same direction.
But for lighting, avalue of 1 is expected when light direction and mesh normal are in the opposite direction.
Short answer : negate N.L value or Mesh Normal.

N.L and L.N will return the same value.