转RSL Function of the Week
RSL Function of the Week
by Rudy Cortes
The RenderMan Shading Language (RSL) is one of the most appealing and rewarding parts of using a RenderMan renderer. As a programming language that it is, RSL allows you to extend the set of built in functions with some of your own. As explained earlier functions are pieces of code that are packed to be reused (and maintained) with ease. If you are already familiar with writing functions you can just go down to the function of the week index.
Writing Functions
RSL functions are defined as follows:
[class]returnType FunctionName (type Argument; [output Argument]) {
function body;
return value;
}
class can be varying or uniform. You use varying when you expect a value to change through the surface, like when using image maps, while uniform is used when you want to use the same value for the whole surface, like the when using a diffuse coefficient multiplier (usually Kd). The declaration of a class is not necessary, but be aware that most compilers will assign a varying class to functions that are left undeclared. This usually results in higher use of memory.
returnType can be any of the standard RenderMan type float, color, string, point, vector, normal, matrix or void. This represents what type of value your function will return.
Next you have the FunctionName. This is the name of the function you will use for calling it. Names are case sensitive, so myFunction and MyFucntion are considered as 2 different functions. You can name your function anything your want, as long as you don't use any of the names of the built-in functions. Names can contain letters and numbers but must always begin with a letter. Special characters like "+ - * . ^ "cannot be used in function names.
Inside the curved braces are the arguments to your function. The list of arguments are usually separated by a semi-colon ";" unless they are of the same kind. Every variable that will be used by the function must be passed as an argument. If its not passed as an argument it needs to be declared as an external variable with the extern call. You can also declare any amount of output arguments. These are very useful for when you want your function to return more than one value. They are optional when you create a valid function (actually all arguments are optional since you can have functions that take no arguments), but they are required when calling a function that was declared with them.
All functions must return a value, except for the void functions. Functions may only have one return value in then and it must be of the same type as the declared returnType. This is why output arguments are so useful. Here you have a simple example of a function
uniform float add (float a, b; output float c) {
extern float s;
c = a * b * s;
return a + b;
}
This function creates a function named add that returns an uniform float value. It takes two float parameters named a and b, plus an output float named c. Inside the body of the function a float variable s is declared as an external variable. This variable must exist in your shader before the first time add() is called. The output variable receives the value of a * b * s, and the function returns a + b.
Here is a list of useful functions I have created, adapted or found around on the net. I will try to upload a new function every week
FOW Index
colorToFloat() and floatToColor()
remapVal()
gamma()
bias() and gain()
colorAdd(),colorOver() and colorSubstract()
by Rudy Cortes
The RenderMan Shading Language (RSL) is one of the most appealing and rewarding parts of using a RenderMan renderer. As a programming language that it is, RSL allows you to extend the set of built in functions with some of your own. As explained earlier functions are pieces of code that are packed to be reused (and maintained) with ease. If you are already familiar with writing functions you can just go down to the function of the week index.
Writing Functions
RSL functions are defined as follows:
[class]returnType FunctionName (type Argument; [output Argument]) {
function body;
return value;
}
class can be varying or uniform. You use varying when you expect a value to change through the surface, like when using image maps, while uniform is used when you want to use the same value for the whole surface, like the when using a diffuse coefficient multiplier (usually Kd). The declaration of a class is not necessary, but be aware that most compilers will assign a varying class to functions that are left undeclared. This usually results in higher use of memory.
returnType can be any of the standard RenderMan type float, color, string, point, vector, normal, matrix or void. This represents what type of value your function will return.
Next you have the FunctionName. This is the name of the function you will use for calling it. Names are case sensitive, so myFunction and MyFucntion are considered as 2 different functions. You can name your function anything your want, as long as you don't use any of the names of the built-in functions. Names can contain letters and numbers but must always begin with a letter. Special characters like "+ - * . ^ "cannot be used in function names.
Inside the curved braces are the arguments to your function. The list of arguments are usually separated by a semi-colon ";" unless they are of the same kind. Every variable that will be used by the function must be passed as an argument. If its not passed as an argument it needs to be declared as an external variable with the extern call. You can also declare any amount of output arguments. These are very useful for when you want your function to return more than one value. They are optional when you create a valid function (actually all arguments are optional since you can have functions that take no arguments), but they are required when calling a function that was declared with them.
All functions must return a value, except for the void functions. Functions may only have one return value in then and it must be of the same type as the declared returnType. This is why output arguments are so useful. Here you have a simple example of a function
uniform float add (float a, b; output float c) {
extern float s;
c = a * b * s;
return a + b;
}
This function creates a function named add that returns an uniform float value. It takes two float parameters named a and b, plus an output float named c. Inside the body of the function a float variable s is declared as an external variable. This variable must exist in your shader before the first time add() is called. The output variable receives the value of a * b * s, and the function returns a + b.
Here is a list of useful functions I have created, adapted or found around on the net. I will try to upload a new function every week
FOW Index
colorToFloat() and floatToColor()
remapVal()
gamma()
bias() and gain()
colorAdd(),colorOver() and colorSubstract()
还没人赞这篇日记