ShaderGraph Diffuse Shading

13 Mar 2019

So you might want to make your own shader in Shadergraph using the unlit shader, but are having trouble making diffuse lighting. I will take you through the steps for how to do this.

Light Direction

The first step is to create a node for the light direction. ShaderGraph doesn’t currently have a node for this so you will need to create a custom node. I have previously made a tutorial that goes over how to create a custom node for the light direction.

Dot Product

Once you have the light node, you can use it for the diffuse shading. To do this, all you need to do is create a normal vector node. Then select “world” from the space dropdown menu. This gets the normal vector from the surface of the object.

To achieve the shading effect, we do the dot product of the normal vector and the light direction vector. The dot product returns a single value which is why it produces a greyscale diffuse colour.

In short, when the light is directly facing the normal, it produces a value of 1. When the light direction is perpendicular to the normal, it produces a value of 0. When the normal is facing away from the light it produces -1.

If you want to read more about the maths of diffuse lighting, I recommend reading this.

The negative values aren’t desirable in our case so we are going to use a maximum node to keep all the values above 0. You can achieve some interesting artistic results if you skip this node though, so try experimenting.

Light Colour

To set the colour of the lighting, we can simply multiply the diffuse shading with the light colour. I have edited the light node so the colour shows in the editor. The light node colour is set to white by default when previewed in ShaderGraph, so don’t worry if it doesn’t look like the picture.

We can also add ambient light to complete the shader. For the ambient light we can use the ambient node that is built into ShaderGraph. Then all we have to do is add the results from the diffuse lighting with the ambient colour. Then you can connect it to the master shader.

I’ve noticed that the ambient light doesn’t updated inside Unity. You have to go into ShaderGraph and save it every time you alter the ambient light colour. This is probably something that will be ironed out since ShaderGraph is still in development.