Sign in to follow this  

Examples of PBR shaders

This topic is 390 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I seem to have far too many days reading about PBR, and tweaking my own GLSL shaders, but what would be really helpful instead of slides / equations would be some examples of functioning shaders, and I haven't seemed to find any examples, just theory but not much practice. :wacko:

 

I already have a shader taking in albedo, tangent space normal map, roughness map and metallic map, and while it is easy to produce something that looks 'nice', I could really do with some reference implementations, as there seem to be umpteen million ways of combining diffuse / specular / fresnel effects etc, and my physics / maths is obviously not going to compare with experts in the field. :P

 

Ultimately I am looking to reproduce / mimic much of the 'universal shaders' used by unreal and in unity, for my 3d painting app, so that users can have some kind of WYSIWYG experience. I have no experience with either of these engines, but it strikes me I should in theory just be able to 'copy and paste' the shaders, or use minimal changes?

 

So can anyone recommend any good links?

Share this post


Link to post
Share on other sites

Hey!
TheCherno is working on a PBR engine (both DX and GL) here is the link to his git: https://github.com/TheCherno/Sparky. His pbr implementation is the one used in Frostbite.
See you.

Thanks! I'm just having a look through that now. I was gradually going through Naty Hoffman's siggraph talk trying to implement some of it, but it will be nice to have a working implementation to work from.

 

https://www.youtube.com/watch?v=j-A0mwsJRmk

Share this post


Link to post
Share on other sites

 

Hey!
TheCherno is working on a PBR engine (both DX and GL) here is the link to his git: https://github.com/TheCherno/Sparky. His pbr implementation is the one used in Frostbite.
See you.

Thanks! I'm just having a look through that now. I was gradually going through Naty Hoffman's siggraph talk trying to implement some of it, but it will be nice to have a working implementation to work from.

 

https://www.youtube.com/watch?v=j-A0mwsJRmk

 

Those talks are really useful but they usually lack of code, also you could check: http://blog.selfshadow.com/publications/s2013-shading-course/karis/s2013_pbs_epic_notes_v2.pdf . SIGRAPH have some great articles about pbr :P
Edited by piluve

Share this post


Link to post
Share on other sites

Grab a copy of Unreal Engine, there's a bunch of PBR shader code in there too. Be aware that also the core concepts and approaches are pretty similar across engines, there are subtle differences in representations, texture formats, scales, etc.

Edited by Promit

Share this post


Link to post
Share on other sites

After a bit of effort to install the latest unreal engine (and then uninstall it, as it was far too big and bloated and slow, must try harder epic..) I found the shaders were actually surprisingly well written and understandable. Although I got a little lost at the LobeRoughness and LobeEnergy, trying to figure out where that was coming in from.

 

While doing this I made sure I was using linear colour in the shaders:

http://http.developer.nvidia.com/GPUGems3/gpugems3_ch24.html

 

While I had been careful to do everything inside 3D Paint in 16 bit linear colour, then downconvert to 8 bit sRGB for the final composite, I had been doing the usual 'wrong' thing in the shaders (sRGB treated as linear). So I made sure to specify the input texture as sRGB, then applied gamma correction from linear -> sRGB at the end of the shader (perhaps I should be requesting an sRGB frame buffer, is this better?). I could incidentally, upload the texture as 16 bit linear, but then I'd be using twice the upload bandwidth. Will investigate.

 

While making sure everything is linear helped stop the highlights blowing out, it also had the undesired effect of making the light look more like a point light, with sharper shadows:

 

24fig03.jpg

With (a) being linear, and (b) being the naughty 'incorrect' version. Trouble is to my eye, the incorrect version looks better lol. So I ended up putting a power function on the dot(normal, lightvector) to get it back looking softer shadows. Which may defeat the object of going linear somewhat lol. :D

 

Another thing I have noticed about this PBR stuff:

 

Following from the 'everything is shiny' approach, with everything reflecting white specular (except metals), the logical result of this, is that everything looks desaturated! So it seems to avoid your objects looking like washed out messes, you either have to decrease the specular power on rough surfaces, or you have to bump up the saturation in the albedo to compensate. <_<

 

I suspect that maybe in real life colours are much more saturated than we think, we are just used to seeing them combined with white specular.

 

It also helped explain to me why a polarizer works when taking photos on a sunny day, to make the colours more saturated. It must filter out more of the specular highlights than diffuse, giving the saturated look.

 

All this has certainly changed the way I think about lighting and colours. I find myself looking at spoons etc thinking 'that's interesting' lol. I guess CG professionals spend much of their life getting distracted by spoons, curtains, glass etc... :lol:

 

Share this post


Link to post
Share on other sites

24fig03.jpg

With (a) being linear, and (b) being the naughty 'incorrect' version. Trouble is to my eye, the incorrect version looks better lol.

Except (a) really is correct:
planet-earth-from-space-nasa-i10.jpg

 

The problem with "what looks nice" is that PBR requires a realistic lighting environment. Directional, Spot and Point lights are not physically based, they lack surface area. Beyond that, ambient light sources are also incredibly important to make the surfaces feel right.

Share this post


Link to post
Share on other sites

Well saying (a) is more correct is only true for that particular light source size and distance I think. :ph34r: If you were to move the light source right up close, if the area was large relative to the sphere, afaik you'd get real lighting closer to (b). Even with the sun's size, far away it might act like a point light, but close up it would be more a soft light (albeit really bright!!).

 

In studio photography there is much emphasis on the use of softboxes, this is important because they spread out the light and give soft shadows more like (b), which are much more pleasing to the eye for a lot of subjects.

 

softbox1-670x376.jpg

 

So something more akin to (b) in terms of real life shadow is very possible, and happens all the time in real life. And while properly calculating an area light is prohibitively expensive in realtime, something like changing the dot product is a pretty good approximation imo. Of course you are right in that doing this may affect other aspects of the lighting simulation.

 

Personally I'd prefer to get something that looked really good, I think it is always going to be a bit of a compromise. :D I guess you can fiddle with the ambient term, but I'm not sure that will make up for lack of soft lighting.

Share this post


Link to post
Share on other sites

Well saying (a) is more correct is only true for that particular light source size and distance I think. :ph34r: If you were to move the light source right up close, if the area was large relative to the sphere, afaik you'd get real lighting closer to (b). Even with the sun's size, far away it might act like a point light, but close up it would be more a soft light (albeit really bright!!).

I'm starting to think that you're mixing directional and point lights. The example you first showed is the n.l falloff specifically for a directional light, which is why I used the photo of earth - the sun being far enough away that it can be approximated as a directional light. A point light on the otherhand, the closer to the surface it is, the direction of the light incoming to the surface will start varying, which will change the falloff pattern across a sphere.

Regarding light size though, the standard PBR model also works well with simulating area lights  - check out the presentation for the most recent Killzone game https://www.guerrilla-games.com/read/killzone-shadow-fall-creating-art-tools-for-a-new-generation.
At a basic level for diffuse lighting it involves clamping the light direction to point to the closest visible part of the surface, rather than editing the n.l part of the equation. This produces a far more plausible result as its scales better with any light shape - where as scaling the n.l factor causes light leaking artifacts.

Edit: FWIW, the N dot L part of lighting is actually physically correct (assuming perfect reflectors) https://en.wikipedia.org/wiki/Lambert's_cosine_law

Edited by Digitalfragment

Share this post


Link to post
Share on other sites

At a basic level for diffuse lighting it involves clamping the light direction to point to the closest visible part of the surface, rather than editing the n.l part of the equation. This produces a far more plausible result as its scales better with any light shape - where as scaling the n.l factor causes light leaking artifacts.

This sounds interesting, I'm sure there are much better ways of doing it! Remember I am a total newbie to this, changing the dot product was just a quick bodge test, that took me about 2 seconds. My observation was simply that soft lighting looked nicer than hard lighting, to this layperson, I wouldn't pretend to have a clue the best way to achieve them lol!  :D

 

And I have only so far implemented point lights, this will probably be fine for me for the time being, in my particular situation I only need to show some indication of the roughness / metallic etc channels to the user, as I'm only making an editing tool rather than an in-game renderer.

Edited by lawnjelly

Share this post


Link to post
Share on other sites

This sounds interesting, I'm sure there are much better ways of doing it! Remember I am a total newbie to this, changing the dot product was just a quick bodge test, that took me about 2 seconds. My observation was simply that soft lighting looked nicer than hard lighting, to this layperson, I wouldn't pretend to have a clue the best way to achieve them lol!

 

Hah, its all good. I've fallen back to modifying n dot l countless times, its just good to understand the why / why not for it. IIRC Valve modified the dot product results for its skin shader in HL2 to fake subsurface scattering and give that soft feel. Good luck with your shaders :)

Share this post


Link to post
Share on other sites

This topic is 390 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this