Normal Reconstruction

From Shader Forge Wiki
Jump to: navigation, search
Normal reconstruction.png

The problem

When reshaping your mesh using Vertex Offset, the normal direction of the surface will not follow along. This is simply because normals are stored in the mesh, and not computed in real-time.

The solution

You can reconstruct the normal direction, with a method called partial derivation. Partial derivatives can be hard to implement, and have a tendency to be very context dependent.

If you have a mesh that reshapes according to a sine wave over time, the math for reconstructing the normal would look very different compared to how you would do it when using a heightmap.

In this example, the normals of the surface are being reconstructed in order to respond properly to the vertex offsetting, which in this case is done using partial derivatives, read from a heightmap

Simple partial derivative.png

Alternative Solution

This is a simple to implement method to make all of the edges display hard shading without having them split, so they can be reworked as needed. This node tree can be plugged into custom lighting calculations in place of the Normal Dir node, or directly into the normal channel of the shader.