Topics

Wednesday, August 15, 2012

Making of a Planet - Part 3 - Shader Basics

In this post I'll be describing what I learnt about shaders when building the CG Earth, with specific examples of Houdini VEX shader networks. First off, a quick introduction to how shaders work.

What are Shaders?

Shaders can be thought of a set of program instructions that tells a renderer how to determine the color of a surface (or volume) at any given location. In 3D programs, they are also referred to as materials.

Shaders are actually program code written in a shading language. However, Houdini allows the code to be build visually as a node network (called a SHOP network, for SHader OPerators). The most famous industry shader language for rendering CGI is the RenderMan shading language. For real-time graphics (e.g. games and 3D applications) you will hear names like GLSL and HLSL. Houdini's shading language is called VEX (Vector EXpressions).

Regardless of shading language used, the simplest shader returns a constant color at every point, making the object appear uniformly flat. In the following example, a constant color blue is being fed into the surface output of a Material Shader Builder node in Houdini's SHOP network:

The constant shader instructs the renderer...
 ...to use the same color for every point on the surface

Let's consider how light may affect how the surface looks.

The following example is the basic Lambert shader (which mathematically is the dot product of the normalized surface normal vector, N, and the normalized light vector, L). It's amazingly elegant that this single dot product formula is enough to describe how the light falls off a surface depending on its angle to the light source:

A classic Lambertian shader...
...makes the teapot surface react correctly to light

Lighting Inside a Loop

In Houdini, this lighting calculation must take place inside what is called an illuminance loop. A loop in computer programming is a sequence of instructions that repeats itself until particular conditions are met. Think of it as repeatedly calculating the same lighting formula (dot product) for all the points on the surface that appears in the final image, using the corresponding N and L vectors for each point location.

So we now have a properly lit teapot, but what if we want it in blue color? All we need to do is to multiply the result of the illuminance loop with a constant (blue color) before passing it to the output:

The Lambert shading network from before is actually hidden inside the illuminance loop node above
Only one dot product and one multiplication is needed to get this blue teapot

Houdini already includes a selection of basic CG shaders (such as Lambert) as a network node so we don't really need to create our own illuminance loops. However, writing your own illuminance loop function allows specific low-level control over the look of your material. For the Earth shader, I adapted a light wrap shader inside the illuminance loop to simulate atmospheric scattering.

Building Visual Complexity

When shaders take into account various factors such as lighting and texturing, more complex looks can be achieved. Notice also how we can take the first two separately rendered images (the constant blue and the grayscale lit teapots) and combine them using a multiply blending in a 2D program to get the same results (blue lit teapot). Therein lies the flexibility and power of render passes and compositing, because we can tweak the amount of blending without having to re-render in 3D.

The example below is a slightly more complex shader including specular highlights and texture mapping, but the basic concept is the same as the previous examples. Notice that the texture map (diffuse color) is multiplied with the Lambert shading, and the specular is added to the results.

No illumination loop used here
More complex shaders will result in more realistic surface appearance

Shader writing may not be everyone's cup of tea, but with some effort anyone can write their own shader. There's a definite amount of mathematics and logical thinking involved, but with it comes the power to go beyond what is available off-the-shelf.

If you're keen to find out more, I recommend the following books:

1) Texturing and Modeling: A Procedural Approach
2) The RenderMan Companion: A Programmer's Guide to Realistic Computer Graphics
3) Advanced RenderMan: Creating CGI for Motion Pictures

That's it for today. Next post I'll elaborate more about my Earth shaders.

No comments:

Post a Comment