Hatch Shading with Light Direction in Unreal Engine



In previous post I describe how to create a view-dependent hatch shader. Today I'll be talking a little bit about how to switch this shader to use different hatch textures based on the direction of the light.

Idea 

With the hatch shader already created, it's pretty simple to change the material so that it reacts to the light direction rather than the viewing angle. Unfortunately Unreal Engine doesn't support easy access to the light source, so we will need to pass the light direction into the material manually.

On the other hand, the changes within the material are minimal.

Setup  

In the view-dependent shading we used Camera Vector node. This node will be substituted with the light direction. If your scene doesn't contain dynamic directional (or main) light, the light direction can be set either in the material, or Material instance as Vector3 parameter.

However, if the direction of your light changes (for example with day/night cycle), you will need to propagate this change to every Material instance.

This is obviously very inefficient and could potentially create a severe drop in performance. Fortunately Unreal Engine offers a way to easily share values between materials via Material Parameter Collections.

We will create one such collection (if you've never done this before, please refer to Material Parameter Collections link). Within it we will define a Vector parameter (in my case named Sun_Direction).


Material

With vector parameter set, we can use the value within our material. Exchange View Vector from previous shader for Collection Parameter node.


And set it to use your parameter.


In my case MPC_Collection is name of Material Parameter Collection asset. The shader now reacts to any value in that asset.

Setting Parameter Value

Last thing remaining is to set the Sun_Direction value. I tested it on a day/night cycle blueprint, that changes light direction on every tick. If you don't have an automatic way to change light direction I suggest this Day/Night Tutorial that can get you up and running quickly.


To get the light direction we use Forward vector of the light. Note that I'm using a directional light that simulates sun, but this can be any light in the scene. The forward vector is then multiplied by -1 due to nature of our algorithm. Final value is then set via Set Vector Parameter Value node where we set appropriate Collection and the name of our parameter.

Results

Here is a comparison of the shader with different light directions.





Extensions

There are many possible extensions to this shader and I encourage you to try them out. Currently the algorithm is limited to a single light. Adding more lights requires changing both shader code and material.

Another possible extension is implementation of point lights. The changes to shader code should involve computing distance to the light and using darkest hatch texture when certain distance threshold is exceeded.

Issues

- In our case, hatching is computed even when the light is below ground (e.g., during night). This can be resolved by clamping values in the shader, or adding visibility checks

References

View-dependent Hatch Shading - shader this tutorial is derived from
Material Parameter Collections - UE asset used for sharing data between materials and material instances
Day/Night Tutorial - day/night tutorial you can use to test the shader

Comments

Popular Posts