Not sure if this is the best place, but I didn't see any forum better.
Since its been requested, heres a quick tutorial of what does what with maya's CGFX plugin.
First off realize that any cg shader is made up of two parts. The vertex program (or shader), and the pixel (or fragment) shader. The pixel shader does the actual shading of the model, even if it just passes on the info from the vertex shader. Why? because thats how the hardware is set up. The vertex processor processes vertices, the fragment processor handles pixel info.
I'll recommend downloading the CGToolkit from NVIDIA http://developer.nvidia.com/object/cg_toolkit.html and looking in your maya/presets/CGFX/examples/ folder for the included example shaders. Most of them are semi usefull, but none of them make for a perfect starting point. At least they can show you structure and syntax.
The pixel shader and vertex shader components are much like a regular function or method in your programming language of choice.
return_type FunctionName (parameters)
Now a specific difference is that Cg (and hlsl for that matter) have certain keywords that denote preset memory locations/types in the GPU archetecture.
These are
NORMAL
POSITION
TEXCOORD
COLOR
and there may be a few others, but those are the important ones for the input and output functions of the vertex and pixel shaders
Some things to know specific to maya: most cg shaders you'll see out there specify some of their input parameters using the keyword Uniform. For maya this is pretty much unnecessary and actually complicates things more. Normally these parameters are passed from the program to the shader, but since maya can directly access global variables for the shader in its interface the redundancy of the uniform variable input is unneeded.
So about these variables.
Anything you want to be able to tweek in the shader through maya's interface MUST be declared as a variable outside the shaders.
You can use any of the normally supported Cg Variable types (read the users manual in the tool kit, there are too many to go over in any detail here), and they will be represented in maya's menu in a number of ways.
the basic syntax is
variableType VariableName : REGISTERTYPE
<
Parameters
> = defaultValue;
Register type is optional. From the list above is what you can choose. If you declare a variables register type POSITION it will be represented as a right clickable menu in maya that you can put objects into. This is what the lights and camera positions are declared as in my shader.
If you declare its register type as Diffuse it will be a color slider with an alpha slider below it.
if you delcare a float4 (or 3 or 2) type variable without the Diffuse or POSITION types it will simply be a linked set of floating point boxes like is an objects position in the attribute editor.
Parameters are interface parameters available through Cg. Many of them do not do a damn thing in maya. Some that do are
string UIWidget = "Slider";
float UIMin
float UIMax
Using the UIWidget = "Slider" will make a float type variable a slider that you can change values on easily. UIMin and UIMax are what control the slider's range.
Bool type variables are ALWAYS checkboxes. Always.
Back to the shader types.
Vertex shaders feed into pixel shaders. This means they must output data usable by a pixel shader. To this end you must declare structures. This concept is similar to class declarations in C++.
syntax is
struct structName
{
dataType Name : REGISTERTYPE;
//as many as you need here
};
Your vertex program's return type will be of this struct type, ie
structName VertexShader (parameters)
{
new strucName OUT;
//do stuff
OUT.Name = compatableDataType;
return OUT;
}
the pixel shader must be of type float4 so it can return a color, that is an RGBA value.
oh right, important point here, colors are not 0-255 they are 0-1(*or more.. but thats another topic) in floating point data. Ie 0,0,0 is black, 1, 1, 1 is white, 0.5, 0.5, 0.5 is medium gray.
in addition the return value of the pixel shader must be set to a register type of COLOR
this can be either
float4 shaderName (parameters) : COLOR
{
float4 rCol;
//do stuff
return rCol;
}
or
float4 shaderName (parameters)
{
//do stuff
return rCol : COLOR;
}
Either way the return type must be to the COLOR register for output to color.
(yes you can write pixel shaders that alter things other than color, but thats outside the scope of this)
Last but not least the vertex shader needs some info from the application, such as what vertex, where it is, etc. this is the role of the appdata struct
struct appdata
{
float3 Position : POSITION;
float4 Normal : NORMAL;
float2 texcoord0: TEXCOORD0;
float3 Tangent : TEXCOORD1;
float3 Binormal : TEXCOORD2;
};
Just use that one for maya shaders. It works fine, and you can't really do much more (ie second uv channels etc) with Maya's implementation of Cg.
coming next. how to write a simple diffuse and specular blinn shader in vertex and pixel flavors.

GA Online

Reply With Quote





