PBR Lighting in Leadwerks
Physically-based Rendering (PBR), often also called Physically-based Shading (PBS), has taken the world of game engines and content creation tools by storm over the last couple of years. And for good reason: Art assets can be produced in a much more predictable and consistent way, since its properties directly relate to (measurable) real-world data, like the diffuse color of a material or the luminous flux (in lumen) of a light source. Even better, those assets also behave predictably and cosistently under varying lighting conditions. And since I really like the results of PBR pipelines (and love fooling around with shaders) I had a go (actually a second one) at implementing PBR in Leadwerks.
So what is (different in) PBR? There are many good resources on this around (e.g. the tutorials by Marmoset http://www.marmoset.co/toolbag/learn), but in a nutshell:
- Special care is taken to make the reflection by a material energy-conserving. Meaning: There can't be more light coming out of a material than went in (unless it is an emitter, of course).
- The properties of a material are modeled after its real-world cousin. This is especially obvious for metals: Metals have no diffuse reflection (which is actually the main contributor to any material's reflection in a classical pipeline). Physically, the diffuse reflection of a material consists of light that is absorbed and then re-emitted (with the diffuse color of the respective material). Metals don't do that (much) - any light hitting a material is being reflected right away, never entering its surface. As a consequence, the diffuse color (usuall called albedo) of a metal is pitch black.
- Everything is shiny: Even non-glossy non-metals do have a (low) amount of reflectivity, being especially apparent at glazing angles (Fresnel effect). Most materials don't have a colored reflection, athough some metals do (like gold or copper).
You will find two main workflows for PBR pipelines: specular-gloss and metalness-roughness. These are basically just two different ways of specifying a material's properties, one giving more artistic freedom and less artifact (specular-gloss), with the other being slightly more intuitive and memory/bandwidth friendly (metalness-roughness). Since I don't have access to the setup of the G buffers in Leadwerks' deferred renderer, I went with the metalness-roughness variant since I could squeeze that into the current setup.
Apart from modifying the lighting shaders to use the different rendering algorithm (I used the GGX specular lighting), it was important to include indirect specular lighting because otherwise metals would be mostly black. The standard way to do this is to use special cubemaps (I created mine using https://www.knaldtech.com/lys/). I also added a simple form of diffuse IBL (image-based lighting) using spherical harmonics.
Some other things are important when adopting a PBR lighting algorithm:
- Use linear space. PC monitors actually use gamma space, which is why most texture files are also encoded in gamma space. The problem here is that adding several colors in gamma space gives an incorrect result (for a more detailed description see http://http.developer.nvidia.com/GPUGems3/gpugems3_ch24.html). Therefore, textures have to be converted to linear space, and the rendering result must be converted back to gamma space for displaying on a monitor.
- The accuracy of an 8bit per channel frame buffer (which is what Leadwerks currently uses) does not yield best results, 16bits per channel would be preferable. And some textures (cubemaps, e.g.) should actually be in 16bit formats as well.
But enough chit-chat, what does all this actually look like?
First of all, we have to "usual" diffuse lighting, here by a directional light
and its corresponding specular companion
Together with the diffuse ambient term
and the indirect specular
this adds up to the total lighting seen at the beginning of the page
So, what's next? Well, actually I'm not convinced I have done all this right - there are some artifacts I have to look at, and I'm sure if the calibration is right. Also I need to write a cubemap generator to create those textures in-engine.
Stay tuned!
- 12
17 Comments
Recommended Comments