Optimized Oren-Nayar Approximation Shader Code

Pope Kim Nov 16, 2011

We presented this approximation code as part of our KGC 2011 presentation. Let me just show the code for those people who don't want to read the whole presentation. I've also mentioned this to Wolfgang Engel during KGC 2011 because our previous Oren-Nayar code was from his wiki book.

The best part of this approximation code is that we eliminated the texture look-up, which turned out to be bottleneck for us. It is not mathematically correct, but worked fine for our game, Warhammer 40,000: Space Marine.

```c half ComputeOrenNayarLighting_Fakey( half3 N, half3 L, half3 V, half roughness ) { // Through brute force iteration I found this approximation. Time to test it out. half LdotN = dot( L, N ); half VdotN = dot( V, N ); half result = saturate(LdotN); half soft_rim = saturate(1-VdotN/2); //soft view dependant rim half fakey = pow(1-resultsoft_rim,2);//modulate lambertian by rim lighting half fakey_magic = 0.62; //(1-fakey)fakey_magic to invert and scale down the lighting fakey = fakey_magic - fakey*fakey_magic; return lerp( result, fakey, roughness ); } ``