Tuesday, May 5, 2015

Replacing Blinn-Phong by GGX

When I first started using Unity 5, I had a lot of problems getting specular highlights right with the standard PBR shader. There was a lot of fireflies (very bright pixels that are flashing when moving the camera) and highly visible specular aliasing. This was mostly caused by how Unity compute specular highlights.

The standard Unity 5 shader uses the Blinn-Phong equation to compute the specular part of the lighting while most other modern engines uses GGX. The problem with Blinn-Phong is that it renders a very small and intense specular highlight that accentuate artefacts especially when rendering with HDR. It will cause more visible specular aliasing and it will break intensity based effects like bloom (fireflies).

A more natural way to compute specular highlights is to use a more diffuse equation like GGX. It looks softer and it will attenuate specular aliasing as well as fireflies artefacts. It turns out that you can enable GGX highlights in Unity 5 but it has been disabled by default.



When looking at the deferred PBR shader, I did found that the code to compute GGX is there and that it is possible to modify the source code to get GGX working again.

If you use the deferred renderer, the switch to GGX is painless to make. You only need to replace the deferred shaders in “Edit/Project Settings/Graphics” to the custom GGX deferred shader I uploaded. If you use the forward renderer, you will need to replace all your shaders with the GGX-Standard shader.

You can download the modified shaders here (you need to put all those shaders in the same folder):
https://dl.dropboxusercontent.com/u/24044878/Unity/GGXShaders2.zip