### RSLWhat is a Surface Shader?

#### Surface Shader Algorithm

Writing a shader is like preparing a meal. While different recipes use different ingredients, all recipes use a general set of rules as well as applying specific rules that make a particular dish unique. An algorithm is a list of rules that must be followed to achieve a certain result. The purpose of a surface shader is to

• calculate the apparent surface opacity,
• calculate the apparent surface color
ie. the color of the light leaving an object,

The renderer uses the opacity information to 'mix' background and foreground surface colors so that background objects in a 3D scene will "show through" any semi-transparent foreground objects. The following algorithm (recipe) lists the four steps that the standard shader, plastic, follows in order to set the appropriate opacity and color of the point on the surface of an object that is being shaded,

 1 Make a copy (n) of the surface normal (N) then, using the viewing vector (I), ensure another copy (nf) faces the camera 2 Set the apparent opacity of the surface (Oi) 3 Find the colors of the light that is coming directly from the light sources and set the 'response' of the surface to those colors. An overall color is found by (generally) making three (ambient, diffuse and specular) lighting calculations. 3.1 add the colors of all the light sources that contribute ambient light, 3.2 add the colors of the light sources that contribute to the diffuse appearance of the surface, 3.3 add the colors of the light sources that contribute to the specular (shiny) highlights of the surface, Before being added, the ambient, diffuse and specular components are scaled by "Ka", "Kd" and "Ks". This enables an artist to control how an object responds to the lights in a scene. 4 Set the apparent color (Ci) of the surface by combining the light color found in step 3 and the opacity found in step 2.

#### Surface Shading & Global Variables

The following table lists the global variables accessible to a surface shader. Those shown in red are "read-only", those in green are the variables to which a surface shader must assign values.

 Global Variable Ci Oi Cs Os N s, t P Ng u,v du, dv dPdu,dPdv L Cl l E Meaning apparent color of the surface (output) apparent opacity the surface (output) true surface color (input) true surface opacity (input) surface shading normal surface texture coordinates surface position surface geometric normal surface parameters change in u, v across the surface change in position with u and v direction from surface to light source light color direction of a ray stricking a surface point position of the camera