Physically based rendering (PBR) is a general term for techniques in 3D graphics that approximate physical laws. The term PBR has come to mean techniques which aim for realistic rendering of real world materials. The PBR approach developed by Disney in 2012 ( Citation: Burley, 2012 Burley, B. (2012). Physically-based shading at disney. Retrieved from https://api.semanticscholar.org/CorpusID:7260137 ) still forms the basis of real-time rendering of realistic materials. This post explains the physical models and mathematics behind PBR. My renderer uses a similar PBR system which can be found in the PBR shader.
Principles
To realistically render materials in a consistent manner some important laws should be obeyed.
- Conservation of energy, the energy of light hitting a surface is less than or equal to the energy reflected.
- Law of reflection, the incident angle equals the reflected angle for light reflecting off a mirror.
- Fresnel Equations, the proportion of light reflected or transmitted when hitting a surface follows the Fresnel equations.
Accurately simulating all the behaviors of light is infeasible for real-time graphics, simplifying assumptions about light are made in PBR including:
- Light is always unpolarized.
- Light’s wave properties can be ignored (eg. diffraction, interference) and treated as rays, this is called geometry optics.
- Light would act the same going from a light source to viewer as vice versa, called Helmholtz reciprocity.
These assumptions rarely cause problems which are visible but certain situations or materials can exaggerate errors. For example, when multiple reflections occur between surfaces, accounting for polarization can visibly change the result ( Citation: Wilkie & Weidlich, 2012 Wilkie, A. & Weidlich, A. (2012). Polarised light in computer graphics. Association for Computing Machinery. https://doi.org/10.1145/2407783.2407791 ) .
The Model
For each pixel on the screen imagine a ray which travels from a virtual eye and through the pixel. The ray will eventually hit an object. If the object is opaque (ignoring atmospheric effects) the colour of that pixel is determined by the light the object scatters from that point towards the pixel. PBR is about calculating the “intensity” of this light.
The model.
Radiance and Irradiance
Intensity is an ambiguous term, radiance and irradiance are two physical units of light which can be mapped to a pixel’s brightness. Radiance and irradiance technically have the same physical units of watts per square metre () but understanding their differences is key to ensuring PBR conserves energy.
Irradiance
Irradiance is the relative flux through or onto an area.
Imagine a point emitting light in every direction with a total radiant flux of watts (joules per second). If we stand farther away from the light source, the flux is spread over a larger area. Imagine an area , in this case it happens to lie on the sphere’s surface but it could be anywhere, some amount of flux from the light source passes through this area. If we moved the area farther from the light source, the area appears “dim”. If we increased the area and captured more flux, the area would still be “dim”. This means the intensity we care about is an amount of flux relative to the area it passes through. The irradiance is then
The notation indicates that the area is very small, this is useful because it means the area is flat and has a normal.
Radiance
Radiance is the flux from a certain direction onto or reflected from an area.
Irradiance considers flux coming from every direction, but in rendering we want to know the light from a certain direction. Radiance is the directional form of irradiance. The problem is, if we looked at the flux from precisely one direction it would always be zero (similar to how the probability of a dart hitting a point is zero at every point). To resolve this, imagine a unit sphere surrounding our area with flux coming from the direction of a small “patch” on the surface. Now that we have an area the flux can be non-zero. The patch is analogous to the arc of a circle. The length of an arc is measured in radians, the patch’s area is measured in steradians instead.
Projecting the area so it is perpendicular to the flux.
If the area is not perpendicular to the direction the flux is coming from, the flux through the area is not the same as the flux if was oriented towards . So that the orientation of the surface doesn’t affect the radiance, is projected towards the light which is given by where is the angle between the area’s normal and direction of the radiance.
Putting everything together, radiance from a direction is
Just like an arc length is , the area of our patch is the projected area multiplied by which we divide the incoming flux over. The notation again indicates a small quantity, in practice the area becomes a point (eg. ) and the solid angle becomes a ray.
The reason why radiance is defined for a patch on a sphere is that it makes the radiance of a light source independent of the distance from the light source. This can be illustrating by considering the following. As the distance from a light source increases, the flux decreases following the inverse square law , but the solid angle occupied by the light source also decreases , cancelling out.
We can find irradiance onto an area by adding radiances from all incident directions, defined to be 1,
where projects the flux back onto the area .
Radiance is the radiant flux () per unit solid angle () in the direction of a ray per unit projected area () perpendicular to the ray. ( Citation: Torrance & Sparrow, 1967 Torrance, K. & Sparrow, E. (1967). Theory for off-specular reflection from roughened surfaces\ast. J. Opt. Soc. Am., 57(9). 1105–1114. https://doi.org/10.1364/JOSA.57.001105 )
In this post we are going to assume that the radiance of each light in a scene is known. Graphics engines have different methods for calculating radiance for light sources. The simplest is a directional light such as the sun, which is a constant value.
Colour
Spectral power distribution of sunlight.
The radiances we deal with will vary with the wavelength (i.e. colour) of the light. We could calculate the radiance for every visible wavelength, called a spectral power distribution (SPD), but this would be very computationally expensive. Luckily, we can approximate the perceived SPD by combining red, green and blue (RGB) radiances. For this reason, treat radiance and any spectral quantities as RGB vectors where relevant2.
Rendering Equation
Returning to our model, We only care about flux directed towards the pixel (light from other directions is not focused by a camera’s lens or the cornea) from the object. The pixel’s colour is determined by the radiance towards the pixel from the point on the object. The light the object scatters towards our eye is either reflected from the surroundings or emitted by the object.
This is called the rendering equation. can be a constant or sampled from a texture. depends on how all the lights of the scene interact with the material of the object at .
As mentioned earlier, the point is analogous to a small planar area with normal . Light which reflects off the surface can not come from behind the plane, so we consider only incident radiances directed from a hemisphere lying on the surface. This assumption preclude large subsurface scattering (light hitting another point on the surface and scattering through the surface and emerging outside ) which are significant in materials such as skin and water.
Hemisphere considers light from all directions incident on point .
We are trying to find the reflected radiance , this depends on the irradiance onto the surface. In the same manner we converted radiance to irradiance, we can convert incident radiances into an irradiance onto the surface. A function called the bidirectional reflectance distribution function (BRDF, units of ) then tells us how much of this irradiance becomes radiance in the reflected or viewing direction.
In this article will mean a solid angle which is directed towards .
You may be wondering why the BRDF is defined in terms of incident irradiance and not radiance, it is so that a BRDF does not vary systematically with light direction. This does not mean the BRDF does not depend on light direction, only that it’s directionality is explained by the reflection of the material itself and not by the projection of flux. Another way of thinking about this is that the term projects the incident radiance onto the area . This theoretical understanding can be checked by substituting the definitions of irradiance, radiance and the BRDF noting that
BRDF
Slices from the BRDFs of different materials showing how reflection varies according to incident direction, from the MERL BRDF dataset.
BRDF is the proportion of radiance reflected towards for a certain irradiance from ,
Since the BRDF is defined by input irradiance energy conservation can be guaranteed if the reflected radiances are together less than the incident irradiance. Using the same conversion from radiance to irradiance, this time for the reflected radiances, we can ensure energy conservation if
To satisfy Helmholtz reciprocity we should be able to swap incident and reflected directions,
Different materials have different BRDFs which can be measured empirically or calculated from theoretical models. PBR consists of a group of energy conserving BRDFs which are usually derived from the microfacet model. Before deriving and explaining the microfacet BRDF we will derive some simpler BRDFs which will be important later.
Lambertian
Unpacked snow is close to a lambertian material. “Snowy Hillside, Creag a’ Bhealaich, Sutherland” by Andrew Tryon, Creative Commons.
The simplest possible BRDF would be some constant, energy conserving value. This corresponds to a material which reflects light in every direction with equal radiance. A material may only be slightly lambertian, the part of the reflection which is lambertian is called diffuse reflection.
When light hits a surface we usually think of it bouncing off like a mirror, how does a lambertian material fit into this model? While it’s true that some light reflects off a surface like a mirror, this is the specular part of the reflection, some light is also transmitted into the material’s surface. For most materials, transmitted light continues to be scattered in the subsurface of the material which until the light is directed back out of the material in a random direction, close to where it entered. This light has a random direction and is the diffuse reflection.
Yellow light hits a surface, the green wavelengths are absorbed in the subsurface and the red wavelengths scatter diffusely making the material appear red.
In coloured materials, some wavelengths of light are absorbed by the material in the process of refracting. These wavelengths are removed from the diffuse reflection, colouring the material. The colour of the diffuse reflection from white light is called the albedo of a material.
To calculate the Lambertian BRDF, set where is the albedo. Assuming a constant BRDF and ensuring energy conservation (my derivation) we find
This is the Lambertian BRDF.
Mirror
Mirror ball ornament demonstrating specular reflection, CC0.
We now consider the specular reflection off the surface. Metals, especially highly polished metals, reflect light to produce a mirrored appearance. How much a material is specular or diffuse will be accounted for eventually, for now we will focus on a perfect mirror. Unlike diffuse reflection, specular reflection is usually considered to not have a colour because the light is not absorbed by the material.
BRDF of a perfect mirror for some specific incident direction.
The law of reflection tells us the reflected light is the incident light flipped about the normal. This means the mirror BRDF should only be positive when the reflection direction equals the incident direction flipped about the normal. The BRDF of a perfectly planar mirror can be expressed in two equivalent ways, in terms of the halfway vector , or polar and azimuthal angles.
where . is the Dirac delta function which is positive only when , for example means the delta is positive when the halfway vector equals the normal vector . The mirror BRDF is energy conserving (my derivation).
Specular Microfacet
If you scratch a metal it becomes less mirror-like, polishing makes it more mirror-like. In scratching the metal we are changing the microscopic surface of the metal. A material’s microsurface can be very complicated so we make an assumption that materials microsurfaces only vary in how “rough” they are and that there are no overhangs. Rough materials are less flat and more “bumpy.”
Microsurface of gypsum, coloured SEM by Steve Gschmeissner.
The microfacet model accounts for this bumpiness by treating our area as consisting of many smaller planes called microfacet which have random orientations. The normal of a microfacet is called a micronormal , as opposed to the macronormal . Materials which are smoother have more micronormals which align with the macronormal, and rougher materials have micronormals in more “random” orientations.
Microsurface of a mirror, the roughness is so all micronormals align with the macronormal.
Microsurface of a rough material, reflections occur towards random orientations causing some reflections to be blocked.
If we consider microfacets of every possible orientation and add their contribution we can find the overall BRDF of the macrosurface.
where is still a hemisphere. and are how visible the microfacet is along the incident and refracted directions respectively. This accounts for the difference in projected areas for microfacets of different orientations. This is separate to the geometry term which accounts for one facet blocking another facet.
As stated, each microfacet will have it’s own BRDF , in this section we are going to assume mirror-like microfacets that only reflect specularly.
Roughness
We defined roughness as a measure of the microscopic “bumpiness,” a more rigorous definition is the variance in the orientations of the microfacets. Assuming microfacets on average point towards the macronormal, then rougher materials have more micronormals pointing at random. The roughness is usually represented by which is between and . Disney suggested a more visually “linear” roughness parameter used when authoring.
Microfacet BRDF
It is common to treat each microfacet as a mirror, we will also assume perfect reflectance for now (). From the mirror BRDF derived earlier,
where is some function which ensures energy conservation. We need to solve for , our energy conservation integral requires
The delta function means that rather than having to sum over all incoming light, we only care about light coming from a direction our micronormal can reflect, when . For this reason, if the integral can be made in terms of , not we can solve for . So we write
We now need to find , how changes relative to . This can be solved geometrically by projecting the translated solid angle, if you are interested read my derivation. We find
Substituting back into our energy conservation integral,
With the delta function enforcing ,
So our microfacet BRDF is
Substituting the microfacet BRDF, the overall specular BRDF is
Again, the delta function eliminates the integral and enforces . This make sense intuitively, since we only care about microfacets when they can actually reflect light, meaning their normals align with the halfway vector.
Density Term
If is the probability density for a micronormal directed into a solid angle . If you think of each microfacet as occupying tiny areas , then if we project each microfacet they should cover our macro area because there are no overhangs. This can be expressed as the following
A valid density term should fit this condition and be physically plausible. Since there can’t be overhangs, assume if not stated that the distribution is when .
There are different models of rough surfaces, one idea is to consider the height at each point on the surface as following a gaussian distribution with variance set to the roughness parameter . This is called the Beckmann distribution, the height distribution is converted to a probability distribution over solid angles,
Another model is GGX or Trowbridge-Reitz distribution proposed by Walter et al. ( Citation: Walter, Marschner & al., 2007 Walter, B., Marschner, S., Li, H. & Torrance, K. (2007). Microfacet models for refraction through rough surfaces. Eurographics Association. ) . The GGX distribution better matches empirical measurements of rough material which show longer tails of steeply angled microfacets than predicted by the Beckmann distribution. The probability distribution is
The GGX distribution is also simpler to calculate. The GGX distribution fits our area rule:
The distributions presented here assume a constant roughness across our macrofacet, this is not true for some materials. Materials such as brushed metal can have a different “roughness” for different viewing direction, meaning the microfacets are aligned differently along different directions in the material. This is called anisotropic reflection, as opposed to the isotropic reflection presented here.
Geometry Term
The geometry term is the proportion of a microfacet that is visible from both the incident and reflected directions. Light which would hit a microfacet may be blocked by another part of the surface, this is called shadowing. Alternatively, reflected light may be blocked from the viewer by the surface, called occlusion.
A rough surface which shows both shadowing and occlusion effects.
Since we don’t know the actual microsurface, the geometry term tells us expected amount of occlusion and shadowing for the microfacet distribution.
Smith ( Citation: Smith, 1967 Smith, B. (1967). Geometrical shadowing of a random rough surface. IEEE Transactions on Antennas and Propagation, 15(5). 668–671. https://doi.org/10.1109/TAP.1967.1138991 ) suggested an approximation for the geometry term . Helmoholtz reciprocity requires that the amount a microfacet is expected to be visible is the same from both the incident and reflected directions. Let the expected amount that the microfacet is visible from be . Assuming that the visibility from and are independent then our geometry term is
This is an approximation because the visibility is not actually independent, for example a microfacet in a deep V is less visible from both directions. This is called a uncorrelated geometry function.
Smith derived a method to calculate for any distribution of microfacets (from the density term ). This technique can be used to derive the Smith-GGX shadowing function, which was approximated by Schlick ( Citation: Schlick, 1994 Schlick, C. (1994). An inexpensive BRDF model for physically-based rendering. Computer Graphics Forum, 13(3). 233–246. https://doi.org/https://doi.org/10.1111/1467-8659.1330233 ) .
If the surfaces heights are assumed to be uncorrelated then is . Alternatively Earl Hammon Jr also suggested a cheap approximation to a correlated geometry term based on ray traced results,
PBR
Fresnel Equation
In solving our specular microfacet BRDF we assumed that each microfacet was a perfect mirror, but as discussed when deriving the Lambertian BRDF some of the light is not reflected, but rather scattered inside the material.
Some proportion of the incident energy is transimitted, and some is reflected.
The fresnel equations describe the proportion of incident radiance which is transmitted through a boundary, such as from air to our material. is a simplied form of the Fresnel equation which is the proportion of unpolarized light which is specularly reflected from an air-dielectric boundary. depends on normal at the boundary, light direction, and the index of refraction (IOR) of the dielectric. The full Fresnel equation can be computationally expensive, Schlick’s approximation is commonly used instead,
where is the colour of the specularly reflected light coming from the normal. The Schlick approximation is a Padé approximation (similar to a taylor series) using the conditions that the reflection should be when incident light is perpendicular and when incident light is perpendicular. The Schlick approximation has the advantage that the mediums’ IOR don’t have to be known, only the perpendicular specular colour . The derivation of the Schlick approximation is beyond this post but is well explained by the original paper ( Citation: Schlick, 1994 Schlick, C. (1994). An inexpensive BRDF model for physically-based rendering. Computer Graphics Forum, 13(3). 233–246. https://doi.org/https://doi.org/10.1111/1467-8659.1330233 ) .
A dialetric’s is closer to white, meaning specular reflection is not tinted. A conductor’s specular colour is closer to it’s “albedo” or base colour, since the diffuse reflections disappears and the specular must account for the material’s colour.
Specular and Diffuse
Our final PBR BRDF must combine the specular microfacet with a diffuse reflection. The specular reflection only occurs when light is not transmitted, following the Fresnel equation where the boundary is oriented towards the microfacets ,
Light which is not specularly reflected is diffusely reflected, which means we have to solve our microfacet BRDF again for the case of a Lambertian microfacet BRDF (). Unfortunately, our microfacet integral
has no closed form solution since there is no longer a delta function to eliminate the integral. It is simpler if we ignore the microfacets for the Lambertian and use the macrofacet normal instead.
Our final PBR microfacet BRDF is then
The radiance directed towards the viewer is
Which can be solved analytically using IBL or with an assumption of analytically light sources such as directional lights being the only incident radiances, for a set of analytical light sources
Code
The code for direct lighting of a dielectric PBR is found below for reference, there are some approximation used which can be found or derived from Edwards et al. ( Citation: Edwards, Boulos & al., 2006 Edwards, D., Boulos, S., Johnson, J., Shirley, P., Ashikhmin, M., Stark, M. & Wyman, C. (2006). The halfway vector disk for BRDF modeling. ACM Trans. Graph., 25. 1–18. https://doi.org/10.1145/1122501.1122502 ) . This code is for illustration purposes and is not optimised.
#define M_PI 3.14159265358979323846264338327950288
#define EPSILON 1.19209e-07
uniform float time;
uniform int G2_mode;
uniform int D_mode;
uniform float alpha;
uniform float dir_radiance;
uniform vec3 albedo;
uniform vec3 specular;
uniform vec3 emissive;
varying vec4 p;
varying vec3 n;
vec3 F_Schlick(float cLdotH, vec3 F0) {
return F0 + (1.0-F0)*pow(1.0 - cLdotH, 5.0);
}
float G2_GGX_corr(float cLdotN, float cVdotN) {
// Correlated Approximation
float nom = cLdotN * cVdotN;
float denom = mix(2.0*nom, cLdotN + cVdotN, alpha) + EPSILON;
return nom / denom;
}
float G2_GGX_uncorr(float cLdotN, float cVdotN) {
// Uncorrelated Approximation
float nom = 4.0 * cLdotN * cVdotN;
float denom = ((2.0 - alpha)*cLdotN + alpha) * ((2.0 - alpha)*cVdotN + alpha);
return nom / denom;
}
float G1_Beckmann(float cXdotN) {
float a = 1.0 / (alpha*sqrt(1.0 / (cXdotN*cXdotN + EPSILON) - 1.0) + EPSILON);
float a2 = a*a;
return (a < 1.6) ? ((3.535*a + 2.181*a2) / (1.0 + 2.276*a + 2.577*a2)) : 1.0;
}
float G2_Beckmann_uncorr(float cLdotN, float cVdotN) {
return G1_Beckmann(cLdotN) * G1_Beckmann(cVdotN);
}
// Derived from Λ = (1 - G1) / G1
float A_Beckmann(float cXdotN) {
float a = 1.0 / (alpha*sqrt(1.0 / (cXdotN*cXdotN + EPSILON) - 1.0) + EPSILON);
float a2 = a*a;
return (a < 1.6) ? ((1.0 - 1.259*a + 0.396*a2) / (3.535*a + 2.181*a2)) : 0.0;
}
// G2 = 1 / (1 + Λ(i) + Λ(r))
float G2_Beckmann_corr(float cLdotN, float cVdotN) {
return 1.0 / (1.0 + A_Beckmann(cLdotN) + A_Beckmann(cVdotN));
}
float D_Beckmann(float HdotN) {
float a2 = alpha*alpha;
float c2 = HdotN*HdotN;
float t2 = 1.0/(c2+EPSILON) - 1.0;
float nom = exp(-t2/a2);
float denom = M_PI*a2*c2*c2 + EPSILON;
return nom / denom;
}
float D_GGX(float HdotN) {
float a2 = alpha*alpha;
float c2 = HdotN*HdotN;
float nom = a2;
float denom = M_PI*(c2 * (a2 - 1.0) + 1.0)*(c2 * (a2 - 1.0) + 1.0);
return nom / denom;
}
vec3 L_r(vec3 N, vec3 V, float cVdotN, vec3 L, float radiance) {
vec3 H = normalize(V + L);
float cLdotN = max(dot(L,N),0.0);
float cLdotH = max(dot(L,H),0.0);
float HdotN = dot(H,N);
vec3 F = F_Schlick(cLdotH, specular);
float D = D_GGX(HdotN);
float G2 = G2_GGX_corr(cLdotN, cVdotN);
vec3 brdf_s = F*D*G2 / (4.0 * cLdotN * cVdotN + EPSILON);
vec3 brdf_d = (1.0 - F) * albedo / M_PI;
return (brdf_s + brdf_d)*radiance*cLdotN;
}
Further Reading
- Nayer et al. developed a microfacet model for diffuse reflection which is more accurate than the Lambertian used here ( Citation: Oren & Nayar, 1994 Oren, M. & Nayar, S. (1994). Generalization of lambert’s reflectance model. Association for Computing Machinery. https://doi.org/10.1145/192161.192213 ) .
- Walter et al. applied the microfacet model to refraction in transparent materials ( Citation: Walter, Marschner & al., 2007 Walter, B., Marschner, S., Li, H. & Torrance, K. (2007). Microfacet models for refraction through rough surfaces. Eurographics Association. ) .
- Sébastien Lagarde’s memo has a Mathematica notebook which is helpful for understanding the Fresnel equation and it’s approximations (link).
- Suggest relevant or interesting articles and I will add them.
References
- Burley (2012)
- Burley, B. (2012). Physically-based shading at disney. Retrieved from https://api.semanticscholar.org/CorpusID:7260137
- Stair, Johnston & Bagg (1954)
- Stair, R., Johnston, R. & Bagg, T. (1954). Spectral distribution of energy from the sun. Journal of Research of the National Bureau of Standards, 53(2). Retrieved from https://nvlpubs.nist.gov/nistpubs/jres/53/jresv53n2p113_A1b.pdf
- Mobley (2021)
- Mobley, C. (2021). Ocean optics web book. Retrieved from https://www.oceanopticsbook.info/
- Rense (1950)
- Rense, W. (1950). Polarization studies of light diffusely reflected from ground and etched glass surfaces. J. Opt. Soc. Am., 40(1). 55–59. https://doi.org/10.1364/JOSA.40.000055
- Torrance & Sparrow (1967)
- Torrance, K. & Sparrow, E. (1967). Theory for off-specular reflection from roughened surfaces\ast. J. Opt. Soc. Am., 57(9). 1105–1114. https://doi.org/10.1364/JOSA.57.001105
- Edwards, Boulos, Johnson, Shirley, Ashikhmin, Stark & Wyman (2006)
- Edwards, D., Boulos, S., Johnson, J., Shirley, P., Ashikhmin, M., Stark, M. & Wyman, C. (2006). The halfway vector disk for BRDF modeling. ACM Trans. Graph., 25. 1–18. https://doi.org/10.1145/1122501.1122502
- Smith (1967)
- Smith, B. (1967). Geometrical shadowing of a random rough surface. IEEE Transactions on Antennas and Propagation, 15(5). 668–671. https://doi.org/10.1109/TAP.1967.1138991
- Earl Hammon (2017)
- Earl Hammon, J. (2017). PBR diffuse lighting for GGX+smith microsurfaces. Retrieved from https://www.gdcvault.com/play/1024478/PBR-Diffuse-Lighting-for-GGX
- Walter, Marschner, Li & Torrance (2007)
- Walter, B., Marschner, S., Li, H. & Torrance, K. (2007). Microfacet models for refraction through rough surfaces. Eurographics Association.
- Heitz (2014)
- Heitz, E. (2014). Understanding the masking-shadowing function in microfacet-based BRDFs. Retrieved from https://jcgt.org/published/0003/02/03/paper.pdf
- Wilkie & Weidlich (2012)
- Wilkie, A. & Weidlich, A. (2012). Polarised light in computer graphics. Association for Computing Machinery. https://doi.org/10.1145/2407783.2407791
- Oren & Nayar (1994)
- Oren, M. & Nayar, S. (1994). Generalization of lambert’s reflectance model. Association for Computing Machinery. https://doi.org/10.1145/192161.192213
- Schlick (1994)
- Schlick, C. (1994). An inexpensive BRDF model for physically-based rendering. Computer Graphics Forum, 13(3). 233–246. https://doi.org/https://doi.org/10.1111/1467-8659.1330233
Let me know if there are any mistakes or problems with this post and I will fix them. Problems found so far
- None