Recreating normals in vertex shader

Hello,

Based on a question I got about an older thread (http://forum.unity3d.com/threads/146254-Computing-normals-in-vertex-shader), I decided to put together a quick and light project and video explaining an approach to recreating normals in a vertex shader in a little more detail. Might be useful for water, who knows?

Video

Project zip:
adrianswall.com/temp/simpleplanewave.zip

The approach shown here is just about finding a way to define “fake neighbors”, or additional points that lie on the unperturbed surface. In the case of the original post, that was a unit sphere. In this case, it’s a plane. Once you have two neighbors, you just take the cross product of the vectors between the sample vertex (the one known to the vertex shader at that time) and each neighbor, and that’s it. In both cases, I used very small displacements between sample and neighbor, which is to help eliminate frequency/accuracy/phasing problems as the perturbation function is basically just sin().

Towards the end of the video, I mention the possibility of storing offsets from sample to neighbor in vertex metadata channels. That would eat quite a lot of metadata, but it might be useful for applying this process to arbitrarily complex meshes. Another way of going about it is to think about what your perturbation is actually doing, and whether there’s a way to just operate on the normal directly, without needing neighbor data.

Cheers,
-Adrian

Awesome tutorial! Loved the chirping birds and the car alarm.

You have given me so many more ideas. I never thought of storing the vertex neighbors in the rgba+uv. Great idea but now I need to think about how to create the maps.

This is good stuff to know so thanks for breaking it all down and sharing.

Andrew