Monday, 1 May 2017

Leadwerks PBR #2

0DA7FDF32E4C3DBC9982F6F89ADD4995AA34B00F (2048×1152)
Crytek's Sponza atrium rendered in Leadwerks using PBR


Physically Based Rendering (PBR) is a workflow and rendering method. It couples a set of shaders that approximate physically accurate optical effects with input that is based on the properties of real surfaces.


PBR has become the standard method for rendering surfaces over the last few years. It is the default rendering method for game engines such as UE4, Unity 5, FrostBite, Cryengine and more. It is also being used in film studios such as Disney and Pixar. 

This project aimed to implement this rendering method into the Leadwerks game engine to bring the benefits of PBR to it's users. The flexibility of the engine, coupled with recent updates to it's graphics engine such as environment probes and HDRi have allowed this to happen.


Features:


PBR is supported in both the C++ and LUA versions of the engine.

Physically based shading model

The system uses a physically based shading model based on EA's Frostbite engine and Unreal engine 4's implementation. The shading model uses Disney's principled BRDF for diffuse lighting, with the frostbite normalisation factors. 

Specular highlights are generated using a BRDF consisting of; GGX for the NDF, Shlick's approximation for Fresnel effects and Smiths shadowing for the visibility function.
For more information on the details on physically based shading models I recommend the work done by S├ębastian Lagarde for remember me as well as the SIGGRAPH physically based rendering courses. 

Metalness / roughness workflow

To create content for the PBR system a roughness/metalness workflow is used. The system has been deigned to match the output from Substance painter/designer as closely as possible.

  • Roughness: determines how rough a surface is, black = shiny, white  = rough. 
  • Metalness: determines whether a surface is metal or not, white = metal, black = dielectric(non-metal). this should really be fully white or fully black.
  • Specular: Specularity can also be modified, specularity affects the intensity of specular reflections, this value relies on the Index of Refraction of a material. 
    • For shaders that don't specify specular, the value defaults to 0.04. This value represents an ior of 1.5, most materials have ior's near this value. If a texture is used then the value of the texture is mapped to a 0.001 - 0.08 range. 99% of surfaces have specular values in this range. This precedent is taken from the UE4 system.

Decal support:

Decals allow for areas of extra detail to be added to areas.

Decals modify the underlying surface properties, this includes roughness and metalness.  

Terrain / Vegetation support:

Vegetation and terrain are supported.
Image provided by Jen from the Leadwerks forums - showcasing the vegetation and terrain shaders

The vegetation shaders all work the same as the built in defaults, with distinctions for static, leaves and ground foliage. For best results I advise using shadows on vegetation objects, if performance allows.

Terrain also works in the same way as default leadwerks. Because of this metalness, roughness and specular cannot be controlled by textures. Therefore terrain is assumed to be dielectric, Roughness uses a combination of the Albedo's value and a set value of  0.05 and specular is set to 0.04 (this is the same value used when there is no specular map).

ToneMapping

Also included in the PBR system is a Filmic tonemapping post process shader. This allows artists to take full advantage of the HDR system available inside Leadwerks. 



Download


The project is available from GitHub




Limitations / improvements

  • Requires a gamma-correction post process shader. Not a huge issue, adding a pp is pretty easy but still something to remember.
  • Currently the built in environment probes are stored in a low dynamic range. This leads to clamping and precision errors as HDR values move towards extremes. this limits the usefulness of HDR. This is an engine issue I can't fix.
  • Probes also use simple mipmapping for different roughness values, PBR often performs a convolution on these values to better match reflection blurring due to roughness. A fix may be possible for this, but would require C++.





References:


Alamia, M. (-). Article - Physically Based Rendering - Cook–Torrance. http://www.codinglabs.net/article_physically_based_rendering_cook_torrance.aspx 
Alamia, M. (-). article_gamma_vs_linear.  http://www.codinglabs.net/article_gamma_vs_linear.aspx
Alamia, M. (-). Physically Based Rendering. http://www.codinglabs.net/article_physically_based_rendering.aspx
Brent, B. (2012). Physically-Based Shading at Disney.
Carmack, J. (2013). The Physics of Light and Rendering. QuakeCon 2013.  https://www.youtube.com/watch?v=P6UKhR0T6cs 
Chan, D. (2015). Real-World Measurements for Call of Duty: Advanced Warfare. Siggraph 2015. Sledgehammer Games/ Activision Blizzard.
Christian. (2011). The Blinn-Phong Normalization Zoo. Retrieved from The Tenth Planet: http://www.thetenthplanet.de/archives/255
Driscoll, R. (2009). ENERGY CONSERVATION IN GAMES.  http://www.rorydriscoll.com/2009/01/25/energy-conservation-in-games/
Gotanda, Y. (2012). Practical Physically Based Rendering in Real-time. Game Developers Conference. tri-Ace, Inc.
Gotanda, Y., Hoffman, N., Martinez, A., & Snow, B. (2010). Physically Based Shading Models for Film and Game Production. Siggraph 2010.  http://renderwonk.com/publications/s2010-shading-course/hoffman/s2010_physically_based_shading_hoffman_a.pdf
Hable, J. (2010, December 5). Everything has Fresnel. http://filmicgames.com/archives/557
Hable, J. (2010, November 7). Everything is shiny. http://filmicgames.com/archives/547
Hoffman, N. (2010). Crafting Physically Motivated Shading Models for Game Development. Siggraph 2010. Activision. http://renderwonk.com/publications/s2010-shading-course/hoffman/s2010_physically_based_shading_hoffman_b.pdf
Hoffman, N. (2013). Background: Physics and Math of Shading. Siggraph 2013. http://blog.selfshadow.com/publications/s2013-shading-course/hoffman/s2013_pbs_physics_math_slides.pdf
Iwanicki, M., & Pesce, A. (2015). Approximate models for physically based rendering. Siggraph 2015. Activision.
Judge, K. (2011, August 23). Shader Code for Physically Based Lighting. http://altdevblog.com/2011/08/23/shader-code-for-physically-based-lighting/
Karis, B. (2013). Real Shading in Unreal Engine. Siggraph 2013. Retrieved from http://blog.selfshadow.com/publications/s2013-shading-course/karis/s2013_pbs_epic_slides.pdf
Klint, J. (2006-2016). Leadwerks Engine. http://www.leadwerks.com
Lafortune, E. (n.d.). Mathematical Models and Monte Carlo Algorithms for Physically Based Rendering. Leuven: Department of Computer Science, Faculty of Engineering, Leuven University.
Lafortune, E. P., & Willems, Y. D. (1994). Using the Modified Phong Reflectance Model for Physically Based Rendering. Department of Computing Science, K.U. Leuven.
Lagarde, S. (2011). Adopting a physically based shading model. https://seblagarde.wordpress.com/2011/08/17/hello-world/
Lagarde, S. (2011). Feeding a physically based shading model. Retrieved from https://seblagarde.wordpress.com/2011/08/17/feeding-a-physical-based-lighting-mode/
Lagarde, S., & Hardui, L. (2013). The Art and Rendering of Remember Me. seblagarde.wordpress.com/2013/08/22/gdceurope-2013-talk-the-art-and-rendering-of-remember-me
Lagarde, S., & rousiers, C. d. (2014). Moving FROSTBITE to PBR. Siggraph 2014. Electronic Arts(EA), EA Dice.
Lazarov, D. (2011). Physically based Lighting in Call of Duty: Black-Ops. http://advances.realtimerendering.com/s2011/index.html
Lazarov, D. (2013). Getting More Physical in Call of Duty Black Ops 2. Siggraph 2013. Activision. http://blog.selfshadow.com/publications/s2013-shading-course/lazarov/s2013_pbs_black_ops_2_slides_v2.pdf
Meinl, F., & Dabrovic, M. The Atrium Sponza Palace. Crytek. Retrieved from http://www.crytek.com/cryengine/cryengine3/downloads
Neubelt, D., & Pettineo, M. (2013). Crafting a Next-Gen Material Pipeline for The Order:1886. Siggraph 2013. ReadyAtDawn Studios.  http://blog.selfshadow.com/publications/s2013-shading-course/rad/s2013_pbs_rad_slides.pdf
Pharr, M., & Humphreys, G. (2012). Physically based rendering: From Theory to Implementation. Morgan Kaufmann.
Seymour, M. (2013). Game environments – Part A: rendering Remember Me. https://www.fxguide.com/featured/game-environments-parta-remember-me-rendering/
Wilson, J. (2013). PBR Practice. http://www.marmoset.co/toolbag/learn/pbr-practice