A while ago, I wrote a blog post on energy-conserving wrapped diffuse lighting [1], noting that following standard formula ended up adding a lot of energy:
Instead, we should normalise and use this instead, preventing a sudden surge in energy whenever we decide to wrap:
However, Jorge Jimenez recently pointed out to me that it would be much better if we could generalise the energy-conserving wrapped diffuse model to encompass a slightly more complex model for diffuse lighting, one using a power:
In fact, raising your wrap shading to a power is a pretty common thing. It makes things look a little bit softer and more pleasing on the eye, as Lambertian diffuse tends to have quite a harsh fall-off that you’re trying to get rid of with wrap shading. Valve did it [2], and that was extended by Sloan et. al. [3] who used:
whereas Valve’s original model fixed w as 1.0. We can do better than both of those by making our formula completely generic, allowing us to use any combination of wrap factor and power that we want.
Once Jorge had made the suggestion of extending energy-conservation to this formula, I quickly pulled out my pen and paper and scribbled down some integrals, and to my surprise it all fell out pretty nicely. Naturally, it’s very similar to what’s in my previous post, so forgive me if I skip a few steps…
First, remember that we need to integrate not just over the hemisphere, but further around the sphere until the formula hits zero (at which point in our shader we clamp). So rather than integrating between 0 and ½π, we go from 0 to α, where α is such that:
Now we can begin, and we’ll again normalise by π now so we don’t have to worry about it at the end:
After we’ve got rid of the outer integral, and pulled out what we can of the inner, we’re left with:
We now pull our main trick: let’s substitute , which gives us and therefore get the following equation:
We only have to remember Integration 101 to work that one out, as long as we make a tiny assumption about our value of n (it can’t be -1):
Now, if we put back in our value for x and evaluate using our identity for α, we get the beautifully simple:
Just to check if we’ve got this right, we’ll try n = 1, as in my original post. Using the above formula, we still get , the same as our original answer, so we’ve probably done something right!
Here’s some obligatory shader code for the new model:
// w is between 0 and 1
// n is not -1
float3 wrappedDiffuse = LightColour * pow(saturate((dot(N, L) + w) / (1.0f + w)), n) * (n + 1) / (2 * (1 + w));
Finally, let’s compare the differences between the old models (on the left) and our new energy-conserving models (on the right).
Valve’s model:
Sloan et. al.’s model, with a = 0.5:
Generic model, with w = 0.5 and n = 4.0:
The differences are a lot more subtle this time (especially with the values we’re using) but they’re still there. It’s worth noting that although for the first two models, energy conservation is making the lighting darker, in the last model the lighting becomes brighter. So don’t make any presumptions!
If you want to have a play around yourself, then here’s a RenderMonkey workspace that allows you to tweak the power and wrap factor, and turn on and off energy conservation.
Now surely, there’s no excuse left to use a non-energy conserving wrapped diffuse model?
References
[1] http://blog.stevemcauley.com/2011/12/03/energy-conserv…rapped-diffuse/
[2] Jason Mitchell, Gary McTaggart and Chris Green, Shading in Valve’s Source Engine, Advances in Real-Time Rendering in Graphics and Games, SIGGRAPH 2006, http://www.valvesoftware.com/publications/2006/SIGGRAPH06_Course_ShadingInValvesSourceEngine.pdf
[3] Peter-Pike Sloan, Derek Nowrouzezahrai, Hong Yuan, Wrap Shading, Journal of Graphics Tools, Volume 15, Issue 4, 2011, http://www.iro.umontreal.ca/~derek/files/jgt_wrap.pdf