Digital Lighting: Shader Lab

return to course


One-line Shaders

Tues May 6, 10:30am


Description

In this lab, we'll create and experiment with a series of "one-line" shaders and quickly test them out on a flat grid object.

Setup

  1. Create a new directory in your home area and change directory (cd) to it. Start up your favorite text editor and create a new file called experiment.sl. Put the following text in the file and save it.

    surface experiment()
    {
       Ci = color (1,0,0);
    }
    

  2. The above is a complete RenderMan shader. Compile it by using the shader command with the name of the shader text file experiment.sl.

    > shader experiment.sl
    experiment:
    

    If the shader program prints output other than that above or it prints an error message, double check experiment.sl to make sure it is exactly the same as the text described in step 1.

  3. Place a copy of the rib file patch.rib in the same directory.

  4. Type the command: render patch.rib

    You should see an image of a flat, red square on a black background. The object is a simple grid (actually a bilinear patch) with the shader you just compiled, experiment, applied to it.

Experiment Process

In each of the following experiments, do the following:

  1. Replace the 3rd line in experiment.sl, which looks like:
    Ci = color (1,0,0);
    
    with the new, specified line;
  2. Recompile the experiment shader
  3. Re-render the patch
  4. Observe the results and understand what's going on before you move on to the next experiment. You may want to make a few notes.

Emacs Tips

These tips will save valuable time when writing & testing shaders that are edited in Emacs.

Experiments

Note: You should do all the experiments in order, but if at any time you feel like creating your own experiments, do so! Experiment as much as possible!

  1. Ci = color "hsv" (0.5, 0.8, 0.3);
  2. Ci = s;
  3. Ci = t;
  4. Ci = color (s, 0, 0);
  5. Ci = color (0, t, 0);
  6. Ci = color "hsv" (s, 1, 1);
  7. Ci = color "hsv" (0, t, 1);
  8. Ci = color "hsv" (0, 1, t);
  9. Ci = color (s, t, 0);
  10. Ci = step(0.5, s);
  11. Ci = step(0.2, s);
  12. Ci = step(0.5, t);
  13. Ci = smoothstep(0.4, 0.6, s);
  14. Ci = smoothstep(0.1, 0.9, s);
  15. Ci = mod(s, 1.0);
  16. Ci = mod(s * 5, 1.0);
  17. Ci = step(0.5, mod(s * 5, 1.0));
  18. Ci = smoothstep(0.4, 0.6, mod(s * 5, 1.0));
  19. Ci = mix(color (1,0,0), color (0,0,1), s);
  20. Ci = mix(color (1,0,0), color (0,0,1), smoothstep(0, 1, s));
  21. Ci = spline(t, color (1,1,0), color (1,1,0), color (0,1,0), color (0,0,1), color (0,0,1));

Extras

  1. A tile...
    Ci = step(t, s);
  2. A texture map...
    Ci = texture("pebbles.tex");
  3. Noise at low frequency...
    Ci = noise(s, t);
  4. Noise at medium frequency...
    Ci = noise(s * 10, t * 10);
  5. Noise at high frequency...
    Ci = noise(s * 50, t * 50);
  6. Fancy (a 5-line shader, which could be written in 1 line, but...)
    color red = color(1,0,0);
    color blue = color(0,0,1);
    float sp;
    sp = sin(s * 2 * PI * 4) * 0.5;
    Ci = mix(red, blue, smoothstep(0.3 - sp, 0.7 + sp, t));

Last updated: 5/5/97

Steve May (smay@cgrg.ohio-state.edu)