mixtureShader.cpp
#include <maya/MPxNode.h>
#include <maya/MIOStream.h>
#include <maya/MString.h>
#include <maya/MTypeId.h>
#include <maya/MPlug.h>
#include <maya/MDataBlock.h>
#include <maya/MDataHandle.h>
#include <maya/MFnNumericAttribute.h>
#include <maya/MFloatVector.h>
#include <maya/MFnPlugin.h>
class MixtureNode : public MPxNode
{
public:
MixtureNode();
virtual ~MixtureNode();
virtual MStatus compute( const MPlug&, MDataBlock& );
virtual void postConstructor();
static void * creator();
static MStatus initialize();
static MTypeId id;
private:
static MObject aColor1;
static MObject aColor2;
static MObject aAlphaInput1;
static MObject aAlphaInput2;
static MObject aOutColor;
};
MTypeId MixtureNode::id( 0x81007 );
MObject MixtureNode::aColor1;
MObject MixtureNode::aColor2;
MObject MixtureNode::aAlphaInput1;
MObject MixtureNode::aAlphaInput2;
MObject MixtureNode::aOutColor;
#define MAKE_INPUT(attr) \
CHECK_MSTATUS ( attr.setKeyable(true) ); \
CHECK_MSTATUS ( attr.setStorable(true) ); \
CHECK_MSTATUS ( attr.setReadable(true) ) ; \
CHECK_MSTATUS ( attr.setWritable(true) );
#define MAKE_OUTPUT(attr) \
CHECK_MSTATUS ( attr.setKeyable(false) ) ; \
CHECK_MSTATUS ( attr.setStorable(false) ); \
CHECK_MSTATUS ( attr.setReadable(true) ) ; \
CHECK_MSTATUS ( attr.setWritable(false) );
void MixtureNode::postConstructor( )
{
setMPSafe(true);
}
MixtureNode::MixtureNode()
{
}
MixtureNode::~MixtureNode()
{
}
void* MixtureNode::creator()
{
return new MixtureNode();
}
MStatus MixtureNode::initialize()
{
MFnNumericAttribute nAttr;
aColor1 = nAttr.createColor( "color1", "c1" );
MAKE_INPUT(nAttr);
CHECK_MSTATUS ( nAttr.setDefault(0.0f, 1.0f, 0.0f) );
aColor2 = nAttr.createColor( "color2", "c2" );
MAKE_INPUT(nAttr);
CHECK_MSTATUS ( nAttr.setDefault(0.0f, 0.0f, 1.0f) );
aAlphaInput1= nAttr.createColor( "mask1", "m1" );
MAKE_INPUT(nAttr);
CHECK_MSTATUS ( nAttr.setDefault(.5f, .5f, .5f) );
aAlphaInput2= nAttr.createColor( "mask2", "m2" );
MAKE_INPUT(nAttr);
CHECK_MSTATUS ( nAttr.setDefault(.5f, .5f, .5f) );
aOutColor = nAttr.createColor( "outColor", "oc" );
MAKE_OUTPUT(nAttr);
CHECK_MSTATUS ( addAttribute(aColor1) );
CHECK_MSTATUS ( addAttribute(aColor2) );
CHECK_MSTATUS ( addAttribute(aAlphaInput1) );
CHECK_MSTATUS ( addAttribute(aAlphaInput2) );
CHECK_MSTATUS ( addAttribute(aOutColor) );
CHECK_MSTATUS ( attributeAffects(aColor1, aOutColor) );
CHECK_MSTATUS ( attributeAffects(aColor2, aOutColor) );
CHECK_MSTATUS ( attributeAffects(aAlphaInput1, aOutColor) );
CHECK_MSTATUS ( attributeAffects(aAlphaInput2, aOutColor) );
return MS::kSuccess;
}
MStatus MixtureNode::compute(const MPlug& plug, MDataBlock& block )
{
if ((plug != aOutColor) && (plug.parent() != aOutColor))
return MS::kUnknownParameter;
MFloatVector color1 = block.inputValue( aColor1 ).asFloatVector();
MFloatVector color2 = block.inputValue( aColor2 ).asFloatVector();
MFloatVector mask1 = block.inputValue( aAlphaInput1 ).asFloatVector();
MFloatVector mask2 = block.inputValue( aAlphaInput2 ).asFloatVector();
color1[0] *= mask1[0]; color1[1] *= mask1[1]; color1[2] *= mask1[2];
color2[0] *= mask2[0]; color2[1] *= mask2[1]; color2[2] *= mask2[2];
MDataHandle outColorHandle = block.outputValue( aOutColor );
MFloatVector& outColor = outColorHandle.asFloatVector();
outColor = color1 + color2;
outColorHandle.setClean();
return MS::kSuccess;
}
MStatus initializePlugin( MObject obj )
{
const MString UserClassify( "utility/color" );
MFnPlugin plugin( obj, PLUGIN_COMPANY, "4.5", "Any");
CHECK_MSTATUS ( plugin.registerNode( "mixture", MixtureNode::id,
MixtureNode::creator, MixtureNode::initialize,
MPxNode::kDependNode, &UserClassify ) );
return MS::kSuccess;
}
MStatus uninitializePlugin( MObject obj )
{
MFnPlugin plugin( obj );
CHECK_MSTATUS ( plugin.deregisterNode( MixtureNode::id ) );
return MS::kSuccess;
}