## Physically based lighting revisited

I’ve been working closely with one of our graphics artists to iron out the faults with the PBR rendering. We also thought it would be a good idea to also include a way to utilize IBL aswell, since it seems to be the way to go when using modern lighting pipelines. Last time I just gathered my code from: http://www.massimpressionsprojects.com/dev/altdevblog/2011/08/23/shader-code-for-physically-based-lighting/ which more or less describes how to implement PBR in realtime, so go there if you wish you to implement PBR in your engine. Instead, I thought I should explain how to combine the PBR with the newly implemented IBL techniques we use!

So to start of, for rendering with IBL you need two textures, or cube maps to be more precise. The first is an ordinary environment map, and is used for reflections. The other is called an irradiance map, and describes the light being radiated from the surrounding environment; an irradiance map can be generated from an environment map using for example ‘cubemapgen’ (https://code.google.com/p/cubemapgen/). The irradiance map is sampled differently from the environment map, whereas the reflections should be just that, a reflection on the surface, the irradiance is more like diffuse light. So to sample reflections and irradiance, we currently use this code (and I doubt it’s gonna be subject to change):

## Geometry

mat2x3 ret; vec4 worldNorm = (InvView * vec4(viewSpaceNormal, 0)); vec3 reflectVec = reflect(worldViewVec, worldNorm.xyz); float x = dot(-viewSpaceNormal, normalize(viewSpacePos.xyz)) * MatFresnelDistance; vec3 rim = FresnelSchlickGloss(specularColor.rgb, x, roughness); ret[1] = textureLod(EnvironmentMap, reflectVec, (1 - roughness) * EnvNumMips).rgb * rim; ret[0] = vec3(0); return ret;

I’m using a matrix here because for some reason subroutines cannot have two ‘out’ arguments and subroutines cannot return structs…

You might also notice how we input a view space normal, so we actually transform it into a world space normal first, so that’s a bit expensive but all of the lighting is done in view space so this is probably going to be cheaper than to perform the lighting into view space for each light. So, without further ado let’s dive into what the code does!

vec3 reflectVec = reflect(worldViewVec, worldNorm.xyz);

Calculate reflection vector by reflecting the view vector with the world normal, this will obviously give us the vector to use when sampling the reflections.

float x = dot(-viewSpaceNormal, normalize(viewSpacePos.xyz)) * MatFresnelDistance;

This is basically the **NV** vector we calculate for use in the Fresnel calculation on the row below.

vec3 rim = FresnelSchlickGloss(specularColor.rgb, x, roughness);

Calculate Fresnel using a modified algorithm which takes into account the roughness of the surface. This function looks like this:

vec3 FresnelSchlickGloss(vec3 spec, float dotprod, float roughness) { float base = 1.0 - saturate(dotprod); float exponent = pow(base, 5); return spec + (max(vec3(roughness), spec) - spec) * exponent; }

By this point we have what we want! So we just sample our textures using the data!

ret[1] = textureLod(EnvironmentMap, reflectVec, (1 - roughness) * EnvNumMips).rgb * rim; ret[0] = textureLod(IrradianceMap, worldNorm.xyz, 0).rgb;

Now we’re done with sampling the reflections. What is left now is to somehow get this into the rendering pipeline for further processing. We use the roughness to select the mipmap, where EnvNumMips denotes the number of mips present in this specific environment map.

This is a typical fragment shader used in Nebula:

shader void psUber(in vec3 ViewSpacePos, in vec3 Tangent, in vec3 Normal, in vec3 Binormal, in vec2 UV, in vec3 WorldViewVec, [color0] out vec4 Albedo, [color1] out vec4 Normals, [color2] out float Depth, [color3] out vec4 Specular, [color4] out vec4 Emissive) { Depth = calcDepth(ViewSpacePos); vec4 diffColor = texture(DiffuseMap, UV) * vec4(MatAlbedoIntensity, MatAlbedoIntensity, MatAlbedoIntensity, 1); float roughness = texture(RoughnessMap, UV).r * MatRoughnessIntensity; vec4 emsvColor = texture(EmissiveMap, UV) * MatEmissiveIntensity; vec4 specColor = texture(SpecularMap, UV) * MatSpecularIntensity; vec4 normals = texture(NormalMap, UV); vec3 bumpNormal = normalize(calcBump(Tangent, Binormal, Normal, normals)); mat2x3 env = calcEnv(specColor, bumpNormal, ViewSpacePos, WorldViewVec, roughness); Specular = calcSpec(specColor.rgb, roughness); Albedo = calcColor(diffColor, vec4(1), AlphaBlendFactor) * (1 - Specular); Emissive = vec4(env[0] * Albedo.rgb + env[1], 1) + emsvColor; Normals = PackViewSpaceNormal(bumpNormal); }

## Inputs and outputs

Here is another interesting detail. What is actually a specular map when using PBR? In our engine, we define it as just that, reflective color in RGB. To simplify authoring for our graphics artists, all textures can be adjusted using simple scalar values. The same actually goes for the Fresnel effect mentioned earlier, albeit it’s not actually physically correct. Anyways, we also use subroutines so all functions called ‘calcXXX’ is calling some subroutine function. What we are interested in here is **env**, and what we feed to **Emissive** and **Albedo**. We can see that we cheat a bit with the albedo color by using **1 – Specular**. This isn’t really energy conserving since it doesn’t take the Fresnel effect into account. To emissive, we simply do this:

Emissive = vec4(env[0] * Albedo.rgb + env[1], 1) + emsvColor;

This calculation comes from the two previously calculated arguments, **env[0]** is the diffuse irradiance, and **env[1]** is the specular reflection. Later in the pipeline, when we have calculated the light we simply add this value to the total color value of said pixel. The next part will cover how lights are calculated using the above data.

## Lights

vec3 viewVec = normalize(ViewSpacePosition); vec3 H = normalize(GlobalLightDir.xyz - viewVec); float NH = saturate(dot(ViewSpaceNormal, H)); float NV = saturate(dot(ViewSpaceNormal, -viewVec)); float HL = saturate(dot(H, GlobalLightDir.xyz)); vec3 spec; BRDFLighting(NH, NL, NV, HL, specPower, specColor.rgb, spec); vec3 final = (albedoColor.rgb + spec) * diff;

So yes, this is the good old calculations normally required to do lighting. We calculate the H vector a bit differently than the usual LightDir + ViewVec, and this is because we actually have the view vector in inverse, since viewVec is the vector FROM the camera to the point, so the formula becomes GlobalLightDir + -viewVec. The same must be applied when calculating the N dot V product, since it’s supposed to represent the angle between the normal and the view vector when looked at from the point on the surface. This is important to consider, since without it the Fresnel attenuation will fail and you will get overly strong specular highlights. I was struggling with getting the specular right and it turned out that the solution was simple, so be sure that these dot products are both saturated and, well, correct! The function then outputs the result to an output parameter, in this case we call it **spec**. **diff** in the final calculation is the diffuse color of the light. Roughness is converted to specular power, but it’s a rather simple process, we simply do:

float specPower = exp(13 * roughness + 1);

To get it into a range of 2-8192 which allows us to get rather strong specular highlights if the roughness is low. Also note that specular power is only relevant to use when we feed it into the BRDF function, and not before. In the previous instances we actually just use the raw roughness value.

## The pros and cons

PBR materials require way more authoring and obviously loads of knowledge from the artists side, albeit the results are loads better since the lighting doesn’t have to be built into the object itself for every scene. Basically, you will need at least 4 texture inputs per object:

**Albedo** – Color of the surface, or in lighting terms it’s the direct reflected color of a surface. Channels = RGBA (A is used for alpha blending/testing)

**Specular/Reflectiveness** – Color of the reflectivity, for most materials this is going to be a whiteish hue, but for some metals, for example gold or copper, the reflectivity is a goldish hue. Channels = RGB, each channel represents each colors reflectivity.

**Roughness/Glossyness** – Value of surface roughness. This is a value going between 0-255 for artists, or in a shader between 0-1 and corresponds to the microsurface density. Depending on how you want it, it can be 1 for glossy, or 1 for rough. Channels = Red only

**Normals** – Self explanatory.

However, in order to get the values just right, we also provide a set of intensity sliders for each texture, which makes it simpler for an artist to get the values just right by scaling the texture values with a simple multiplication. This ensures that roughness and specularity matches the wanted values.