ShaderLab Tutorial 1, Page 2

Picture

”OUTPUT” DATA - THINGS YOU CAN VARY

These are some of the attributes of a surface that you can set from within your shader code.

Note that these "output" variables are special cases. For instance, once you've worked out what colour you want the point to be, you MUST put that result into Co, as this is the only place that tS will take the colour value from.


Info : Colour
Variables : Co
Description : Set the colour of the current shading point.

Colour shaders are the best ones to start coding with - they will let you get used to SL and its interface; they will let you practice coding and learn how to vary values based on other attributes; finally, they provide perfect, no-nonsense feedback on the results of your code (it's far easier to see the way colour changes than it is to spot how shininess or reflections are changing). One idea is to develop your code based on colour first, and change it over to alter the more complex features later.

Co is another vector, which means it is made up of three parts. In this case, they are called r, g and b, for red, green and blue. The values for each of these parts should be between 0 and 1 (although you  can go beyond these values, which can be useful in certain situations).

Only colour and reflectance shaders can set Co.



Info : Transparency
Variables : Alpha
Description : Set the opacity of the object.

This varies from 0 to 1, with 1 being opaque and 0 being completely transparent. It can only be set by the transparency shader.

 

Info : How The Point Responds To Light
Variables : User defined names, but the naming conventions that I've adopted are given below. You'll find similar conventions used in RenderMan shader code, which is part of the reason I've chosen to use these particular variable names.

These variables are used in a formula to work out the point's final colour. Some standard formulae are included here to get you started.
Ambient - Ka
As you'd guess, the ambient light setting for an object. This is equivalent to the ambient slider in the native tS shaders.

Diffuse - Kd
This is how much effect diffuse lighting has on the point. This is equivalent to the diffuse slider in the native tS shaders.

Shininess - Ks
This sets the strength of specular highlights. This is equivalent to the shininess slider in the native tS shaders.

Roughness - roughness
This defines how sharp a specular highlight is. This is equivalent to the roughness slider in the native tS shaders.

Specular Colour - specular_color
This defines the colour of the specular highlights.

Putting All These Light Settings Together
Here's some simple code for a reflectance shader, which acts much like the Matte shader in tS, and an example image using the code :

Basic Ambient and Diffuse lighting.

Ka = 0;
Kd = 0.7;
color = Co;
Co = color * ( Ka * ambient() + Kd * diffuse());

Right, let's go through what this does. It starts by taking the point's base colour (as set by the colour shader and stored in Co), and storing the value in "color", which is a user variable (that is, it isn't something that SL or tS recognise or will look after for you).

The next command works out the effect of the lighting on the current point. This works by taking the underlying base colour, as set by the colour shader, and modifying that colour by the effects of the lights.

You’ll be relieved to know that all the complex calculations to work out the effect of the lights are done for you using special functions. All you need to do is multiply the results passed back from these functions by the variable which stores the strength of that particular effect.

So our statement starts by taking the variable “color” and multiplying it by the effect of the lighting. The first lighting effect accounted for is the ambient lighting. The user variable Ka contains the equivalent of the value set by the ambient slider in the native tS shaders, amd this is multiplied by the function ambient().

The next effect accounted for is diffuse lighting. Our variable Kd is the equivalent of the diffuse slider in the native tS shaders. The built-in function diffuse() does all the hard work of adding up the effect of all the lights in the scene, including each light's colour and it's strength at that particular point, and the code simply multiplies this result by the strength set for diffuse.

The ambient and diffuse lighting effects are added together (everything in brackets gets calculated first), and then the base colour is multiplied by the lighting effects to give the final colour for that point on the object’s surface. Remember, the results of the calculation must be placed into Co, as this is the only variable that tS will look in to get the colour for the current pixel.

(You might want to note that you could create a reflectance shader which was one stage simpler than this - if you were to simply have
"Co = color * Ka * ambient()", you would be left with a shader similar to the tS Constant shader, except you could alter the brightness using the Ka variable, whereas the native constant shader has no controls at all.)

Let's add in some specular highlights then :

Basic lighting with Specular highlights.

Ka = 0;
Kd = 0.7;
Ks = 0.6;
roughness = 0.1;
color = Co;
Co = color * (Ka * ambient () + Kd * diffuse) + Ks * specular(1/roughness);

Our new bit of code works in much the same way as the code we wrote earlier, and all we’ve had to do is add on the effect of the specular highlight at the end.

Ks stores the equivalent of the shininess slider in the native tS shaders, and the specular function works out the effects of the lights for us. One difference in using specular() function as opposed to the other lighting functions is that it needs to be told how rough the object is, since this will affect the results of tS's calculation of the specular highlights.

To tell tS how rough our object is, we put the value in the brackets for the specular function. This value has to be 1 divided by the roughness setting.


Now let's add in the specular colour :

Adding colour to the Specular highlight.

Ka = 0;
Kd = 0.7;
Ks = 0.6;
roughness = 0.1;
specular_color = (0.6, 0.6, 1);
color = Co;
Co = color * (Ka * ambient () + Kd * diffuse) + specular_color * Ks * specular(1/roughness);

To add the effect of specular colour, we multiply the previous specular calculation by the desired colour, which was stored in a new variable called specular_color.

This gives a plastic type shader, as the specular color has a constant value. In this example, the specular colour is blue (remember that colours are set using the red, green and blue values, so our specular colour is set to 0.6 red, 0.6 green and 1 blue).

To make a metallic shader, where the specular color takes on the color of the underlying material, just change the line "specular_color = (1,1,1);" into "specular_color = Co;"

That's enough about adding in the effects of lighting for the moment!



Info : Displacement, Mirroring
I'll leave these until a later date as they are a little more complicated.

Picture

SHADERLAB INTRO PAGE

PREV PAGE

NEXT PAGE