Sign in to follow this  
PlazmaInteractive

OpenGL [Unity and Cg] Am I calculating my PBR shader correctly?

Recommended Posts

Hello everyone. I've started to learn shader programming as a hobby since the start of this year and I'm finally up to the point where I think I can learn how to implement PBR in my shader. So I read and studied a lot of articles that talk about PBR and its calculations. With that, I decided it was time to start coding.

Firstly, I'm going for a metallic/roughness workflow for my PBR shader. I used the Oren-Nayar model for diffuse but I may change it to something more simple like Lambert as I've heard it's computationally more expensive. For the specular microfacet model I used GGX (normal distribution), Smith (geometry) and Schlick's approximation (fresnel). Because I'm still quite new with this, I got the calculations mainly from this site and this person's video.

Now for the code. As I'm using Unity, I'm writing it with the Cg language wrapped in ShaderLab code. After adding in all the calculations in my shader, I see the results and I wasn't sure if I had implemented this properly. I tweaked the roughness and metallic value around and at some given value, it look correct while at the same time, it doesn't look correct on other values. I'm not great at explaining this in words so here are the visual results and my comments regarding them.

The shader code can be found here. Again, as it's for Unity, the ShaderLab codes may be unfamiliar to you so just ignore those and find the Cg codes between the lines CGPROGRAM and ENDCG. I hope some of you understand what is happening here as it's very similar to HLSL and GLSL. If anyone know if I have implemented this correctly or not, please let me know. Thanks!

Share this post


Link to post
Share on other sites
With roughness = 0, it will show perfect reflections. If your light source is a traditional point/spot/directional light, then it will have zero area/volume, so can't possibly be seen directly or in a perfect mirror. They only show up on slightly rough surfaces :lol:
Long term: IBL helps fill in the blackness and area lights have volume, so will show up.
Short term: clamp roughness above some minimum value, such as 0.001, instead of letting it reach zero. You have to do this anyway to avoid numeric instability.

Metals will look crap in general until you add IBL / ambient lighting tricks too.

Share this post


Link to post
Share on other sites

I used the Oren-Nayar model for diffuse but I may change it to something more simple like Lambert as I've heard it's computationally more expensive.

In this paper—in which I am credited as a reviewer—Yoshiharu Gotanda presents equations for Oren-Nayar which are both simpler/more efficient than what you will have found elsewhere and more accurate compared to the full model than other approximations.
http://research.tri-ace.com/Data/s2012_beyond_CourseNotes.pdf


L. Spiro

Share this post


Link to post
Share on other sites

With roughness = 0, it will show perfect reflections. If your light source is a traditional point/spot/directional light, then it will have zero area/volume, so can't possibly be seen directly or in a perfect mirror. They only show up on slightly rough surfaces :lol:
Long term: IBL helps fill in the blackness and area lights have volume, so will show up.
Short term: clamp roughness above some minimum value, such as 0.001, instead of letting it reach zero. You have to do this anyway to avoid numeric instability.

Metals will look crap in general until you add IBL / ambient lighting tricks too.

Yeah, I haven't added any IBL yet because I want to make sure I'm getting all the calculations correctly. About the roughness = 0 thing, in the visual results I linked above, I mentioned that I think the result looks correct at no roughness and 1 metallic. But what I was skeptical about is when it have a roughness of 1 and a metallic of 1 so a rough, metallic material. I thought it's meant to make the whole material look slightly darker like the one that Unity's own standard shader have. In their one, they use a smoothness value so just think of it as being an opposite of mine (0 smoothness means 1 roughness). You can see how theirs look darker but doesn't make it really, really dark like the one I have. Compare theirs to my one.

So other than that, my whole calculations is correct? The one I'm confused about is how to add the diffuse and specular values together. I'm multiplying my diffuse value by 1.0 - metallic value for energy conservation but I'm not sure if that's the correct way to do it.


 

I used the Oren-Nayar model for diffuse but I may change it to something more simple like Lambert as I've heard it's computationally more expensive.

In this paper—in which I am credited as a reviewer—Yoshiharu Gotanda presents equations for Oren-Nayar which are both simpler/more efficient than what you will have found elsewhere and more accurate compared to the full model than other approximations.
http://research.tri-ace.com/Data/s2012_beyond_CourseNotes.pdf


L. Spiro

 

Thanks for providing the paper. I haven't look into it extensively but it does look interesting so I'll study it when I have the time :)

Share this post


Link to post
Share on other sites

Well, in the meantime I decided to add in a simple IBL that someone in another forum have helped and this time, the results looked similar to Unity's own standard shader, but there is a problem regarding having a value of 1 roughness and 1 metallic. You can see all of the results here. My shader is the sphere in the left, standard shader is the one in the right.

Here is the updated code. The modifications can be found starting from line 154.

Share this post


Link to post
Share on other sites

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