Cg Programming/Unity/Brushed Metal

This tutorial covers anisotropic specular highlights.

It is one of several tutorials about lighting that go beyond the Phong reflection model. However, it is based on lighting with the Phong reflection model as described in (for per-vertex lighting) and  (for per-pixel lighting). If you haven't read those tutorials yet, you should read them first.

While the Phong reflection model is reasonably good for paper, plastics, and some other materials with isotropic reflection (i.e. round highlights), this tutorial looks specifically at materials with anisotropic reflection (i.e. non-round highlights), for example brushed aluminium as in the photo.



Ward's Model of Anisotropic Reflection
Gregory Ward published a suitable model of anisotropic reflection in his work “Measuring and Modeling Anisotropic Reflection”, Computer Graphics (SIGGRAPH ’92 Proceedings), pp. 265–272, July 1992. (A copy of the paper is available online.) This model describes the reflection in terms of a BRDF (bidrectional reflectance distribution function), which is a four-dimensional function that describes how a light ray from any direction is reflected into any other direction. His BRDF model consists of two terms: a diffuse reflectance term, which is $$\rho_d / \pi$$, and a more complicated specular reflectance term.

Let's have a look at the diffuse term $$\rho_d / \pi$$ first: $$\pi$$ is just a constant (about 3.14159) and $$\rho_d$$ specifies the diffuse reflectance. In principle, a reflectance for each wave length is necessary; however, usually one reflectance for each of the three color components (red, green, and blue) is specified. If we include the constant $$\pi$$, $$\rho_d / \pi$$ just represents the diffuse material color $$k_\text{diffuse}$$, which we have first seen in but which also appears in the Phong reflection model (see ). You might wonder why the factor max(0, L·N) doesn't appear in the BRDF. The answer is that the BRDF is defined in such a way that this factor is not included in it (because it isn't really a property of the material) but it should be multiplied with the BRDF when doing any lighting computation.

Thus, in order to implement a given BRDF for opaque materials, we have to multiply all terms of the BRDF with max(0, L·N) and − unless we want to implement physically correct lighting − we can replace any constant factors by user-specified colors, which usually are easier to control than physical quantities.

For the specular term of his BRDF model, Ward presents an approximation in equation 5b of his paper. I adapted it slightly such that it uses the normalized surface normal vector N, the normalized direction to the viewer V, the normalized direction to the light source L, and the normalized halfway vector H which is (V + L) / |V + L|. Using these vectors, Ward's approximation for the specular term becomes:

$$\rho_s \frac{1}{\sqrt{(\mathbf{L}\cdot\mathbf{N}) (\mathbf{V}\cdot\mathbf{N})}} \cdot \frac{1}{4 \pi \alpha_x \alpha_y} \exp\left( -2 \frac{\left((\mathbf{H}\cdot\mathbf{T})/\alpha_x\right)^2 + \left((\mathbf{H}\cdot\mathbf{B})/\alpha_y\right)^2}{1 + \mathbf{H}\cdot\mathbf{N}}\right)$$

Here, $$\rho_s$$ is the specular reflectance, which describes the color and intensity of the specular highlights; $$\alpha_x$$ and $$\alpha_y$$ are material constants that describe the shape and size of the highlights. Since all these variables are material constants, we can combine them in one constant $$k_\text{specular}$$. Thus, we get a slightly shorter version:

$$k_\text{specular} \frac{1}{\sqrt{(\mathbf{L}\cdot\mathbf{N}) (\mathbf{V}\cdot\mathbf{N})}} \exp\left( -2 \frac{\left((\mathbf{H}\cdot\mathbf{T})/\alpha_x\right)^2 + \left((\mathbf{H}\cdot\mathbf{B})/\alpha_y\right)^2}{1 + \mathbf{H}\cdot\mathbf{N}}\right)$$

Remember that we still have to multiply this BRDF term with L·N when implementing it in a shader and set it to 0 if L·N is less than 0. Furthermore, it should also be 0 if V·N is less than 0, i.e., if we are looking at the surface from the “wrong” side.

There are two vectors that haven't been described yet: T and B. T is the brush direction on the surface and B is orthogonal to T but also on the surface. Unity provides us with a tangent vector on the surface as a vertex attribute (see ), which we will use as the vector T. Computing the cross product of N and T generates a vector B, which is orthogonal to N and T, as it should be.

Implementation of Ward's BRDF Model
We base our implementation on the shader for per-pixel lighting in. We need another vertex output parameter  for the tangent vector T (i.e. the brush direction) and we also compute   in the vertex shader to save some instructions in the fragment shader. In the fragment shader, we compute two more directions:  for the halfway vector H and   for the binormal vector B. The properties are  for $$k_\text{diffuse}$$,    for $$k_\text{specular}$$,    for $$\alpha_x$$, and   for $$\alpha_y$$.

The fragment shader is then very similar to the version in except that it computes   and , and implements a different equation for the specular part. Furthermore, this shader computes the dot product L·N only once and stores it in  such that it can be reused without having to recompute it. It looks like this:

Note the term  which resulted from $$\frac{1}{\sqrt{(\mathbf{L}\cdot\mathbf{N}) (\mathbf{V}\cdot\mathbf{N})}} $$ multiplied with $$(\mathbf{L}\cdot\mathbf{N})$$. This makes sure that everything is greater than 0.

Complete Shader Code
The complete shader code just defines the appropriate properties and adds another vertex input parameter for the tangent. Also, it requires a second pass with additive blending but without ambient lighting for additional light sources.

Summary
Congratulations, you finished a rather advanced tutorial! We have seen:
 * What a BRDF (bidirectional reflectance distribution function) is.
 * What Ward's BRDF model for anisotropic reflection is.
 * How to implement Ward's BRDF model.