January 10th,2008
What do we want to achieve in this tutorial?

"MathColor" shader simply applies basic math functions to your shader. In this tutorial we are going to have two colors and set of operands that will affect on final color outcomes. You are also going to learn how to write a "Attribute Editor Template" in mel to control the GUI of your shader  in maya.
Setup Our Shader Code in VC++

Please follow my first tutorial to see how you can setup VC++ for your shader. This shader is called HS_MathColor. Of course, you can name your shader whatever you wish. We will start with the same structure we had for our first shader and then add our codes to it. I also changed the color of new codes so you can see better what is added in this tutorial.
Using "fabsf" is necessary because it returns absolute value of floating point value." 
 
#include <math.h>             // Is needed for Fabs
#include "shader.h"

extern "C" {

DLLEXPORT int HS_MathColor_version() {return 1;}
DLLEXPORT miBoolean
HS_MathColor(miColor *, miState *, struct HS_MathColor *);

}
struct HS_
MathColor {

    miInteger Operation;   // Operands
    miColor   Color1;        // Color1
    miColor   Color2;        // Color2

              
};

DLLEXPORT miBoolean HS_MathColor( miColor *result, miState *state, struct HS_MathColor *param)

{

 
miInteger Operation = *mi_eval_integer(&param->Operation);
  miColor   Color1      = *mi_eval_color(&param->Color1);
  miColor   Color2      = *mi_eval_color(&param->Color2);

  switch(Operation) {

        case 0: // No Operation

              break;

        case 1: // Add

              result->r  = Color1.r + Color2.r;
              result->g = Color1.g + Color2.g;
              result->b = Color1.b + Color2.b;

              break
;

        case 2: // Difference

             result->r  = fabsf(Color1.r - Color2.r);
             result->g = fabsf(Color1.g - Color2.g);
             result->b = fabsf(Color1.b - Color2.b);

             break
;

         case 3: // Multiply

             result->r  = Color1.r * Color2.r;
             result->g = Color1.g * Color2.g;
             result->b = Color1.b * Color2.b;

             break
;

         case 4: // Divide

           result->r  = fabs(Color1.r / Color2.r);
           result->g = fabs(Color1.g / Color2.g);
           result->b = fabs(Color1.b / Color2.b);

           break
;

          default: result->r = result->g = result->b = 1;  // default color white
          break
;

}

return miTRUE;

}

Shader Scene Construction

Now it is time to setup our mi scene. Again, here in our mi file we setup all the variables and their default values. That's all.
 
# *******************Maya User Interface*********************

declare shader color "HS_MathColor" (

      integer "Operation",          #: default 0 min 0 max 4
      color    "Color1",              #: default 1.0 1.0 1.0
      color    "Color2"               #: default 1.0 1.0 1.0

)

version 1
apply material

# *******************Max User Interface*********************

gui "gui_HS_MathColor" {

    control "Global" "Global" ("uiName" "HS_MathColor",  "category" "Illumination")

    control "Operation" "integer" ("value" 0,"range" 0 4,"uiName" "Mode Type(0=no opr,1=add,2=dif,3=mult,4=div)")

    control "Color1" "color" ("value" 1.0 1.0 1.0, "range" 0.0 1.0)

    control "Color2" "color" ("value" 1.0 1.0 1.0, "range" 0.0 1.0)

}

end declare

 
MAYA GUI Setup Using Attribute Editor Template

At this stage our shader has no proper GUI. For making a proper GUI for your shaders in general you need to make a Attribute Editor Template using Mel scripting. AET basically defines the look of the user interface and the attribute names that Maya uses to display it's shaders. If you go to your maya folder where it is installed ( e.g C:\Program Files\Autodesk\Maya2008\scripts\AETemplates), You can find these templates.
Following right image shows our shader GUI without using AET. Of course the functionality works perfectly fine but what if you want to give this shader to someone to use. The user simply has no idea what 0 to 4 in front of  Operations means and also everything is setup in a way that makes the shader variables hard to read.
The first step is to make a mel file and save it in your maya script folder ( e.g C:\Program Files\Autodesk\Maya2008\scripts) and make sure you change your script name to something like this  (
AEHS_MathColorTemplate.mel). This is important because if your script name doesn't have "AE" at the beginning and "Template" at the end ,Maya can't recognize it as a template script.
Now open up AEHS_MathColorTemplate.mel in any texteditor and add the following code to it.

global proc AEHS_MathColorTemplate( string $nodeName )

{
     editorTemplate -beginScrollLayout;  // begin scroll

          editorTemplate -beginLayout "MATH COLOR" -collapse 0; // begin layout
        
              editorTemplate -addSeparator; 
             
                   editorTemplate -l "
Operation:" -addControl "Operation:";   // Operation:
            
              editorTemplate -addSeparator;
             
                   editorTemplate -l "
Color1:"  -addControl "Color1";   // Color1:
             
              editorTemplate -addSeparator;
             
                   editorTemplate -l "
Color2:"  -addControl "Color2";  // Color2:

              editorTemplate -addSeparator;

      editorTemplate -endLayout;  // end layout

      editorTemplate -addExtraControls; 

   editorTemplate -endScrollLayout; // end scroll
}

What we did so far was adding a scroll layout, labels, and separators to clean up our GUI. 
 

We are almost done , the next step is to make a "option menu" which shows our operands.

global proc AEHS_MathColor_Operation( string $attrName )
{

      setUITemplate -pst attributeEditorTemplate;

      rowColumnLayout -nc 1 -columnWidth 1 600;

     attrEnumOptionMenuGrp -l "Operation:" -at $attrName
                                         -ei 0 "No operation"
                                         -ei 1 "Add"
                                         -ei 2 "Difference"
                                         -ei 3 "Multiply"
                                         -ei 4 "Divide"
                                         OperandsGrp;
     setUITemplate -ppt;

}


global proc AEHS_MathColor_replace_mode( string $attrName )
{

    attrEnumOptionMenuGrp -e -at $attrName OperandsGrp;
}


Next step is to call our Attribute procedure we just made using "
editorTemplate -callCustom" in our Node procedure.

At this point in the template when building the dialog, the procedure specified by the first argument is to be called to create some UI objects when a new node type is edited. The procedure specified by the second argument is to be called if an attribute editor already exists and another node of the same type is now to be edited. The replacing procedure should connect any controls created by the creating procedure to the equivalent attributes in the new node. A list of zero or more attributes specifies the attributes which the two procedures will involve. (Explanation From Mel Command Reference).

editorTemplate -callCustom "AEHS_MathColor_Operation" "AEHS_MathColor_replace_mode" "Operation";
 
3DSMAX GUI

Hereit looks our GUI for our shader in 3dsmax.

AEHS_MathColorTemplate.mel Final Code

global proc AEHS_MathColor_Operation( string $attrName )
{

      setUITemplate -pst attributeEditorTemplate;

      rowColumnLayout -nc 1 -columnWidth 1 600;

      attrEnumOptionMenuGrp -l "Operation:" -at $attrName
                        
                                           -ei 0 "No operation"
                                           -ei 1 "Add"
                                           -ei 2 "Difference"
                                           -ei 3 "Multiply"
                                           -ei 4 "Divide"
      OperandsGrp;

      setUITemplate -ppt;

}

global proc AEHS_MathColor_replace_mode( string $attrName )
{

               attrEnumOptionMenuGrp -e -at $attrName OperandsGrp;
}


global proc AEHS_MathColorTemplate( string $nodeName )
{
      
      editorTemplate -beginScrollLayout;

          editorTemplate -beginLayout "MATH COLOR" -collapse 0;

              editorTemplate -addSeparator;
         
           
  editorTemplate -callCustom "AEHS_MathColor_Operation" "AEHS_MathColor_replace_mode" "Operation";

              editorTemplate -addSeparator;
        
              editorTemplate -l "Color 1:" -addControl "Color1";

              editorTemplate -addSeparator;

              editorTemplate -l "Color 2:" -addControl "Color2";

              editorTemplate -addSeparator;

        editorTemplate -endLayout;

        editorTemplate -addExtraControls;

editorTemplate -endScrollLayout;
}
 
DOWNLOAD
Here you can download all the codes we did in this tutorial. Download



 

 

 

All contents copyright © 2008-2012 Hamed Sabri ALL RIGHTS RESERVED