return to main index



This tutorial presents a C++ pattern plugin called CutrSparky that was derived from an OSL shader named sparky. Both the plugin and the shader when used with a Bxdf such as PxrConstant can control the presence and color of a curve to provide the illusion of a particle or spark - figure 1 rollover.

Figure 1
Curve with and without "sparky" shading.

The OSL code for the original sparky shader is shown below.

Listing 1 (sparky.sl)

    float age = 0 
        string label = "Spark Age",
    float len = 0.01 
        string label = "Spark Length",
    float rearblur = 0.05
        string label = "Spark Rear Blur",
    float frontblur = 0.0 
        string label = "Spark Front Blur",
    color start = color(1,1,1)
        string label = "Spark Head Color",
    color end = color(0.482,0.776,0.996)
        string label = "Spark Tail Color",
    output color resultRGB = 0,
    output float resultMask = 0)
resultMask = smoothstep(age - rearblur, len + age, v) * 
              (1 - smoothstep(age, age + frontblur + len, v));
resultRGB = mix(start, end, resultMask);

Althought the code for CutrSparky is considerably more complicated that the OSL code of sparky most of it is responsible for what might be described as "bookkeeping" - referencing the plugin's inputs and managing memory for it's outputs. The most interesting section of C++ code is shown below and for the purposes of comparison it is accompanied by the equivalent OSL code.


    // Surface parameter
    RtFloat const  *V;
    ctx->GetBuiltinVar(RixShadingContext::k_v, &V);
    // Assign values to each point.
    for(int i = 0; i < ctx->numPts; i++) {
        resultF[i] = RixSmoothStep(age[i] - rearblur[i], width[i] + age[i], V[i]) * 
                     (1 - RixSmoothStep(age[i], width[i] + age[i] + frontblur[i], V[i]));
        resultRGB[i] = RixMix(start[i], end[i], resultF[i]);
    return 0;


    resultMask = smoothstep(age - rearblur, len + age, v) * 
                  (1 - smoothstep(age, age + frontblur + len, v));
    resultRGB = mix(start, end, resultMask);

This video,
shows the result of applying CutrSparky and PxrConstant to 500 static curves.

On frame 1 the "Spark Age" parameter was set to 0.0. On the last frame the value was set to 1.0 - figure 2.

Figure 2

Many OSL functions have Rix... equivalents, such as, smoothstep() and RixSmoothStep(). The Rix "versions" are declared in,


Useful color conversion functions are declared in,


For convenience the commonly used Rix functions are accessible from Cutter's popup menu.

Figure 4

Before using the CutrSparky.args file the value of "rfmdata nodeid" should be edited so that it does not conflict with any other node ids specified in the users RenderMan_for_Maya.ini file. Refer to the tutorial "RixPattern Plugins: Cutter" for information about compiling and using custom RixPattern plugins.

© 2002- Malcolm Kesson. All rights reserved.