Physically Based Rendering
by Pierre DragoFire Hay · in Torque 3D Professional · 03/05/2014 (4:39 pm) · 156 replies
I've been slowly reading up on this and wonder if anyone else has looked into this or done any work with it and T3D yet?
For those who don't know what Physically Base Rendering or as some call it Physically Based Shading is here's a topic over at RSI covering what is physically based rendering, this covers most aspects of PBR without getting to technical.
What is PBR or sometimes refered to as PBS or BRDF
The most trivial explanation of a PBR/PBS/BRDF (physical based renderer / physical based shader / bidirectional reflectance distribution function) is that it is the bit of shader code describing how a surface reacts to light. Generally, it is responsible for calculating the specular highlights and diffuse characteristics of the surface material. They are mathematical approximations of how surfaces react to light in the real world. In computer graphics, we try to model the physical world as accurately as possible, but we are constrained by computation. For this reason, the mathematical efficiency of BRDFs is very important. Some of the better known BRDFs - Blinn, Phong and Lambert, for instance - are well known for this reason: they are computationally inexpensive to calculate and intuitive to adjust. However they compromise efficiency for accuracy. If we concern ourselves with more expensive and more accurate models, we uncover a second layer of shading models: Oren-Nayar, Cook-Torrance, Askikhmin-Shirley, etc.
There is no single model that fits every situation, but there are some better than others. The Cook-Torrance model has been shown to be a top performer, when compared against actual acquired BRDF data. Of course, with the good comes the bad and Cook-Torrance is one of the most expensive models to compute. But for overall results, it is hard to beat. So this is our target; a nice implementation of the Cook-Torrance reflectance model. Now this maybe an issue when using Cook-Torrance model on mobiles and consoles due to hardware limitations, so it's a question of which module to use.
What's required?
Base PBR implementation consists of 3 things;
Gamma-correct rendering
[ul]Realistic rendering requires handling values much higher than display white (1.0) Before shading: light intensities, lightmaps, environment maps Shading produces highlights that affect bloom, fog, DoF, motion blur, etc. Cheap solutions exist Good tone mapping (ideally filmic)
Difference between textures and materials.
There are few fundamental distinctions between textures and materials. For example, you cannot apply a texture on a static mesh or BSP geometry. Textures have to be a part of a material. The material is what you would use to texture your environment and apply to Static Meshes.
Here are the differences between a texture and a material:
Textures is a single file, a 2d static image. It is usually a diffuse, specular or a normal map file that you would create in Photoshop or Gimp, as a tga, tiff, bmp, png file. These can be manipulated photographs, hand-painted textures or textures baked in an application such as xNormals.

Materials are made up of various textures combined together inside a Material Editor(in-engine or 3rd party editor). Materials include various textures and material expressions that creates a network of nodes. The final result is a material you can use to apply on your BSP geometry and on Static Meshes. Materials are what you see rendered in-game.
It should be noted that Specular[i] map on a texture, is connected to the [i]refractive index and as such describes a physical property. The shading model then varies this reflectance based on view angle and surface roughness. Thus the surface roughness is adjusted to create variety and specular not be varied for a given material.

A video on PBR for Artist
PBR in Substance by Allegorithmic
Here's a few examples of PBR in action.
tri-Ace Technical Demo Trailer 2011 "Physically-based Rendering"
Star Citizen Avenger PBR
Useful Links:
Pixar writing a Cook-Torrance surface Shader
Pixar writing a BRDF template
Pixar writing a BRDF template part2
Pixar CookTorrance Slim Template
Shading course 2012
Shading course 2013
Physically Based Lighting in Black-Ops
Shader code for physically based lighting
Basic Theory of Physically-Based Rendering
Paprika Render
Mitsuba Render
PBRT Org.
Unreal PBR
Houdini Shaders for physically based rendering (PBR)
D3DBook:(Lighting) Cook-Torrance
Specular BRDF Reference by Brian Karis from Epic game
Books of Interest:
Physically Based Rendering, From Theory to Implementation
ShaderX7
The RenderMan, Shading language Guide
Edit: Updated with new information and links.
For those who don't know what Physically Base Rendering or as some call it Physically Based Shading is here's a topic over at RSI covering what is physically based rendering, this covers most aspects of PBR without getting to technical.
What is PBR or sometimes refered to as PBS or BRDF
The most trivial explanation of a PBR/PBS/BRDF (physical based renderer / physical based shader / bidirectional reflectance distribution function) is that it is the bit of shader code describing how a surface reacts to light. Generally, it is responsible for calculating the specular highlights and diffuse characteristics of the surface material. They are mathematical approximations of how surfaces react to light in the real world. In computer graphics, we try to model the physical world as accurately as possible, but we are constrained by computation. For this reason, the mathematical efficiency of BRDFs is very important. Some of the better known BRDFs - Blinn, Phong and Lambert, for instance - are well known for this reason: they are computationally inexpensive to calculate and intuitive to adjust. However they compromise efficiency for accuracy. If we concern ourselves with more expensive and more accurate models, we uncover a second layer of shading models: Oren-Nayar, Cook-Torrance, Askikhmin-Shirley, etc.
There is no single model that fits every situation, but there are some better than others. The Cook-Torrance model has been shown to be a top performer, when compared against actual acquired BRDF data. Of course, with the good comes the bad and Cook-Torrance is one of the most expensive models to compute. But for overall results, it is hard to beat. So this is our target; a nice implementation of the Cook-Torrance reflectance model. Now this maybe an issue when using Cook-Torrance model on mobiles and consoles due to hardware limitations, so it's a question of which module to use.
What's required?
Base PBR implementation consists of 3 things;
Gamma-correct rendering
- Shading inputs (textures, light colors, vertex colors, etc.) naturally authored, previewed [li]and (often) stored with nonlinear (gamma) encoding
- Final frame buffer also uses nonlinear encoding
- This is done for good reasons
- [li]Perceptually uniform(ish) = efficient use of bits
- Legacy reasons (tools, file formats, hardware)
[ul]
Difference between textures and materials.
There are few fundamental distinctions between textures and materials. For example, you cannot apply a texture on a static mesh or BSP geometry. Textures have to be a part of a material. The material is what you would use to texture your environment and apply to Static Meshes.
Here are the differences between a texture and a material:
Textures is a single file, a 2d static image. It is usually a diffuse, specular or a normal map file that you would create in Photoshop or Gimp, as a tga, tiff, bmp, png file. These can be manipulated photographs, hand-painted textures or textures baked in an application such as xNormals.

Materials are made up of various textures combined together inside a Material Editor(in-engine or 3rd party editor). Materials include various textures and material expressions that creates a network of nodes. The final result is a material you can use to apply on your BSP geometry and on Static Meshes. Materials are what you see rendered in-game.
It should be noted that Specular[i] map on a texture, is connected to the [i]refractive index and as such describes a physical property. The shading model then varies this reflectance based on view angle and surface roughness. Thus the surface roughness is adjusted to create variety and specular not be varied for a given material.

A video on PBR for Artist
PBR in Substance by Allegorithmic
Here's a few examples of PBR in action.
tri-Ace Technical Demo Trailer 2011 "Physically-based Rendering"
Star Citizen Avenger PBR
Useful Links:
Pixar writing a Cook-Torrance surface Shader
Pixar writing a BRDF template
Pixar writing a BRDF template part2
Pixar CookTorrance Slim Template
Shading course 2012
Shading course 2013
Physically Based Lighting in Black-Ops
Shader code for physically based lighting
Basic Theory of Physically-Based Rendering
Paprika Render
Mitsuba Render
PBRT Org.
Unreal PBR
Houdini Shaders for physically based rendering (PBR)
D3DBook:(Lighting) Cook-Torrance
Specular BRDF Reference by Brian Karis from Epic game
Books of Interest:
Physically Based Rendering, From Theory to Implementation
ShaderX7
The RenderMan, Shading language Guide
Edit: Updated with new information and links.
#142
It might be a good idea to follow in Epics steps and research. That way a lot of knowledge can be carried over between T3D and UE4 by artists.
blog.selfshadow.com/publications/s2013-shading-course/
Just my two cents.
07/12/2014 (2:43 am)
UE4 uses Cook-Torrance, obiously. Brian Karis' SIGGRAPH course notes and slides are quite pedagogic about their research and selection of terms (to use GGX/Trowbridge-Reitz over Blinn-Phong for the distribution function, Schlick for the geometric function and fresnel function with a minor modification), it also contain good reasoning about PBR and material models.It might be a good idea to follow in Epics steps and research. That way a lot of knowledge can be carried over between T3D and UE4 by artists.
blog.selfshadow.com/publications/s2013-shading-course/
Just my two cents.
#144
07/12/2014 (6:23 pm)
Pixar RenderMan BRDF template sample code;##
## Copyright (c) 2007 PIXAR. All rights reserved. This program or
## documentation contains proprietary confidential information and trade
## secrets of PIXAR. Reverse engineering of object code is prohibited.
## Use of copyright notice is precautionary and does not imply
## publication.
##
## RESTRICTED RIGHTS NOTICE
##
## Use, duplication, or disclosure by the Government is subject to the
## following restrictions: For civilian agencies, subparagraphs (a) through
## (d) of the Commercial Computer Software--Restricted Rights clause at
## 52.227-19 of the FAR; and, for units of the Department of Defense, DoD
## Supplement to the FAR, clause 52.227-7013 (c)(1)(ii), Rights in
## Technical Data and Computer Software.
## Pixar
## 1200 Park Ave
## Emeryville, CA 94608
----------------------------------------------------------------------------
# template: CookTorranceSC.slim
# author: Scott Eaton
# description: template for a Cook-Torrance shading component.
# Supports tutorial on writing a BRDF template in the RenderMan Courseware.
# $Revision: 1
slim 1 extensions scotte {
extensions courseware pxsl {
template shadingcomponent CookTorranceSC {
label cookTorrance
description {
Returns the result of the specular shading operator. Samples
all incoming lights along the specular direction of the surface.
}
parameter float Intensity {
description "Intensity of specular component"
range {0 1 .001}
default .8
detail varying
}
parameter color Color {
description {
Surface specularColor. Use for metallic surfaces
to obtain a filtered highlight color.
}
detail varying
default {1 1 1}
}
parameter float Roughness {
description {
Size or roughness of highlight
}
range {0 1 .001}
default .4
detail varying
}
parameter float IndexOfRefraction {
description {
index of refraction of material
}
range {0 100 .01}
default 1.3
detail varying
}
parameter float gaussianMultiplier {
description {
multiplier for the gaussian microfacet distribution.
effectively makes the specular highlight brighter.
}
range {1 500 1}
default 100
detail varying
}
parameter float lightType {
label Use
description {
Whether to respond to traditional CG lights,
lighting from environment maps, or both.
}
subtype selector
range {
"Lights" -1
"Environments" 1
"Lights + Environments" 0
}
default 0
}
parameter normal Normal {
detail mustvary "pixar,ShadingNormal"
}
parameter vector ViewDirection {
detail mustvary "pixar,ViewDirection"
display hidden
}
#145
07/12/2014 (6:24 pm)
code continued;collection shadingcomponent result {
access output
display hidden
parameter color col {
access output
}
eval [::aovOutputParameterMacro]
}
RSLInclude "pxslAOV.h"
RSLSource StaticFunction {
void pxslCookTorranceSC(
float Ks;
color specularColor;
float roughness;
float IOR;
float gaussConstant;
uniform float lightType;
normal Ns;
vector V;
output color col;
DECLARE_AOV_OUTPUT_PARAMS
)
{
INIT_AOV_OUTPUT_PARAMS
#if SLIM_SHADERTYPEID != SLIM_TYPEID_light
extern point P;
normal Nn = normalize(Ns);
vector Vn = normalize(V);
float F, Ktransmit;
float m = roughness;
fresnel( -Vn, Nn, 1/IOR, F, Ktransmit);
color cook = 0;
if (lightType <= 0)
{
float NdotV = Nn.Vn;
illuminance("-environment", P, Ns, 1.57,
"lightcache", "reuse")
{
float nonspec = 0;
color inshadow = 0, Cl_noshd = 0, Cl_diff;
lightsource("__nonspecular", nonspec);
if (nonspec < 1)
{
vector Ln = normalize(L);
vector H = normalize(Vn+Ln);
float NdotH = Nn.H;
float NdotL = Nn.Ln;
float VdotH = Vn.H;
float alpha = acos(NdotH);
float D = gaussConstant*exp(-(alpha*alpha)/(m*m));
float G = min(1, min((2*NdotH*NdotV/VdotH), (2*NdotH*NdotL/VdotH)));
color k = (1-nonspec)*(F*D*G)/(PI);
if( 0 != lightsource("_shadow", inshadow) )
o_OcclusionDirect += inshadow;
if( 0 == lightsource("_cl_noshadow", Cl_noshd) )
Cl_noshd = Cl;
Cl_diff = Cl_noshd - Cl;
o_SpecularDirect += k * Cl_noshd;
o_SpecularDirectShadow += k * Cl_diff;
}
}
}
// illumination from environments
// unlike diffuse, this (currently) does not contain any
// object-based calculations
if (lightType >= 0)
{
float nonspec = 0;
color envCol = 0;
float cone = 1.55 * pow(roughness,2);
illuminance ("environment", P, Ns, 1.57,
"lightcache", "refresh",
"send:light:__coneangle", cone,
"send:light:__coneaxis", reflect(-V, Ns),
"light:__nonspecular", nonspec)
{
if (nonspec < 1)
{
o_SpecularEnvironment += (1-nonspec) * Cl;
}
}
}
// scale by Ks
color ks = specularColor * Ks;
o_SpecularDirect *= ks;
o_SpecularDirectShadow *= ks;
o_SpecularEnvironment *= ks;
#endif
col = o_SpecularDirect - o_SpecularDirectShadow +
o_SpecularEnvironment;
o_SpecularColor = specularColor;
}
}
}
}
}
#146
writing a Cook-Torrance surface Shader
writing a BRDF template
writing a BRDF template part2
CookTorrance Slim Template
07/12/2014 (7:38 pm)
links relating to Cook-Torrance:writing a Cook-Torrance surface Shader
writing a BRDF template
writing a BRDF template part2
CookTorrance Slim Template
#147
Edit: One thing I will note is to expect side effects on any models you've already imported as far as the shadowing goes:
www.polycount.com/forum/showpost.php?p=2096054&postcount=124
Left is the equivalent of what we've got at present, right would be the CT
07/12/2014 (8:17 pm)
Pierre: While there's still quite a few concerns about the performance hit, you're more than welcome to go ahead and start porting the current code on over to a Cook-Torrence model yourself while we're working on the rest of this stuff so that we can quickly drop it on in and do up a comparison for evaluation. Sound like a plan?Edit: One thing I will note is to expect side effects on any models you've already imported as far as the shadowing goes:
www.polycount.com/forum/showpost.php?p=2096054&postcount=124
Left is the equivalent of what we've got at present, right would be the CT
#148
07/12/2014 (8:43 pm)
Material Experiments, No fancy Shaders, Just Plain ol T3D, by Ron Kapaun is a good starting point for updating the materials.
#149
I'll try, will depend on my current workload and weather. If this is up into action it'll have to be noted that the specular component is now physical basis, otherwise any artist will get effects they won't want. ;)
07/12/2014 (9:42 pm)
@AzaezelI'll try, will depend on my current workload and weather. If this is up into action it'll have to be noted that the specular component is now physical basis, otherwise any artist will get effects they won't want. ;)
#150
07/12/2014 (11:12 pm)
Quote:www.garagegames.com/community/blogs/view/22764 is a good starting point for updating the materials.That would be why we are expending a significant portion of the time ensuring that we either preserve the workflow, or simplify it with similar results for phase 1. (Barring colored specular colorization as noted in the working thread since there is, simply put, no space in the gbuffer without significant degradation.)
#151
07/15/2014 (1:49 am)
have rewritten topic header
#152

Rounding the bend on www.garagegames.com/community/forums/viewthread/137917, which should get us portability, baseline variable access, and corrected inputs for the upcoming revised math algies. (Not going to make the same mistake as previously, and leave opengl to flounder while we make hefty changes.)
Will note that before he got eaten by the EU4 beast, Mac's https://github.com/andr3wmac/Torque3D/tree/physical_shading fork did include cook-torrence, though only on HDR, so will take a bit of porting work there.
JeffR has also been looking into screen space reflections from the research end to try and cut down on the load there.
10/02/2014 (6:57 pm)
Rounding the bend on www.garagegames.com/community/forums/viewthread/137917, which should get us portability, baseline variable access, and corrected inputs for the upcoming revised math algies. (Not going to make the same mistake as previously, and leave opengl to flounder while we make hefty changes.)
Will note that before he got eaten by the EU4 beast, Mac's https://github.com/andr3wmac/Torque3D/tree/physical_shading fork did include cook-torrence, though only on HDR, so will take a bit of porting work there.
JeffR has also been looking into screen space reflections from the research end to try and cut down on the load there.
#153
10/03/2014 (4:57 am)
@Azaezel Awesome news! I must admit I kinda moved off to UE4 as well myself, but if PBR and SSR shows up in T3D I will definitely be back!
#154
10/03/2014 (4:59 am)
Oops... double posted but it won't let me delete so I just changed the text to this instead :P
#155
That's where I left off before I was "eaten by the UE4 beast" haha. I think I've left it in completely capable hands with Azaezel and JeffR.
10/03/2014 (5:32 am)
That's where I left off before I was "eaten by the UE4 beast" haha. I think I've left it in completely capable hands with Azaezel and JeffR.
#156
I have really get some free time put aside to get back into this, shame about paying work using up all my time ATM.
10/29/2014 (3:03 pm)
@Andrew looks good. Jumped miles ahead from he last time I saw whtat you guys were doing.I have really get some free time put aside to get back into this, shame about paying work using up all my time ATM.
Torque Owner Pierre DragoFire Hay
RedlineIT Ltd
Material appearance is determined by the diffuse reflectance, the specular reflectance for normal incidence (face-on) and a measure of surface roughness. Together with a surface mask (these are four parameters, two colors and two scalars, which fit into two RGBA textures. All parameters of the shading model are spatially varying, which allows for large collection of different appearances on a single texture atlas.
Specular color on a texture, is connected to the refractive index and as such describes a physical property. The shading model then varies this reflectance based on view angle and surface roughness. Thus the surface roughness is adjusted to create variety and specular not be varied for a given material.
e.g.
Varying the specular effects on parts of a concrete floor is due to variance in glossiness; the specular of a rock minerals is the same for the entire surface.
Theoretical Reflectivity and Specular Color by Gamma conversion values.
materials / Reflectance norm / Specular Gray lvl/colour
Water / 0.02 / 44
Glass / 0.03 / 56
Polystyrene / 0.05 / 66
Calcite / 0.06 / 72
Alumina / 0.08 / 80
Diamond / 0.17-0.18 / 114;115;117
Silicon / 0.34-0.49 / 157;163;187
Copper / 0.49-0.92 / 245;214;184
Gold / 0.39-0.96 / 251;233;166
Silver / 0.94-0.99 / 252;250;239
Note: The value for polystyrene can be used as typical for plastic and organic materials. The values for calcite and alumina can be used as typical for rock minerals [i](sapphires and rubies are also made from alumina). The metal examples are for pure and clean metals.