# 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 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 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 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 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.

## Create an account

Register a new account

• ## Partner Spotlight

• ### Forum Statistics

• Total Topics
627662
• Total Posts
2978515
• ### Similar Content

• Both functions are available since 3.0, and I'm currently using glMapBuffer(), which works fine.
But, I was wondering if anyone has experienced advantage in using glMapBufferRange(), which allows to specify the range of the mapped buffer. Could this be only a safety measure or does it improve performance?
Note: I'm not asking about glBufferSubData()/glBufferData. Those two are irrelevant in this case.
• By xhcao
Before using void glBindImageTexture(    GLuint unit, GLuint texture, GLint level, GLboolean layered, GLint layer, GLenum access, GLenum format), does need to make sure that texture is completeness.
• By cebugdev
hi guys,
are there any books, link online or any other resources that discusses on how to build special effects such as magic, lightning, etc. in OpenGL? i mean, yeah most of them are using particles but im looking for resources specifically on how to manipulate the particles to look like an effect that can be use for games,. i did fire particle before, and I want to learn how to do the other 'magic' as well.
Like are there one book or link(cant find in google) that atleast featured how to make different particle effects in OpenGL (or DirectX)? If there is no one stop shop for it, maybe ill just look for some tips on how to make a particle engine that is flexible enough to enable me to design different effects/magic
let me know if you guys have recommendations.
• By dud3
How do we rotate the camera around x axis 360 degrees, without having the strange effect as in my video below?
Mine behaves exactly the same way spherical coordinates would, I'm using euler angles.
Tried googling, but couldn't find a proper answer, guessing I don't know what exactly to google for, googled 'rotate 360 around x axis', got no proper answers.

References:
Code: https://pastebin.com/Hcshj3FQ
The video shows the difference between blender and my rotation:

• By Defend
I've had a Google around for this but haven't yet found some solid advice. There is a lot of "it depends", but I'm not sure on what.
My question is what's a good rule of thumb to follow when it comes to creating/using VBOs & VAOs? As in, when should I use multiple or when should I not? My understanding so far is that if I need a new VBO, then I need a new VAO. So when it comes to rendering multiple objects I can either:
* make lots of VAO/VBO pairs and flip through them to render different objects, or
* make one big VBO and jump around its memory to render different objects.
I also understand that if I need to render objects with different vertex attributes, then a new VAO is necessary in this case.
If that "it depends" really is quite variable, what's best for a beginner with OpenGL, assuming that better approaches can be learnt later with better understanding?

• 10
• 10
• 12
• 22
• 13