#include <math.h>
#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 CheckerNode : public MPxNode
{
public:
CheckerNode();
virtual ~CheckerNode();
virtual MStatus compute( const MPlug&, MDataBlock& );
virtual void postConstructor();
static void * creator();
static MStatus initialize();
static const MTypeId id;
private:
static MObject aColor1;
static MObject aColor2;
static MObject aBias;
static MObject aUVCoord;
static MObject aOutColor;
static MObject aOutAlpha;
};
const MTypeId CheckerNode::id( 0x81006 );
MObject CheckerNode::aColor1;
MObject CheckerNode::aColor2;
MObject CheckerNode::aBias;
MObject CheckerNode::aUVCoord;
MObject CheckerNode::aOutColor;
MObject CheckerNode::aOutAlpha;
#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 CheckerNode::postConstructor( )
{
setMPSafe(true);
}
CheckerNode::CheckerNode()
{
}
CheckerNode::~CheckerNode()
{
}
void * CheckerNode::creator()
{
return new CheckerNode();
}
MStatus CheckerNode::initialize()
{
MFnNumericAttribute nAttr;
aColor1 = nAttr.createColor("color1", "c1");
MAKE_INPUT(nAttr);
CHECK_MSTATUS(nAttr.setDefault(0., .58824, .644) );
aColor2 = nAttr.createColor("color2", "c2");
MAKE_INPUT(nAttr);
CHECK_MSTATUS(nAttr.setDefault(1., 1., 1.) );
aBias = nAttr.create( "bias", "b", MFnNumericData::k2Float);
MAKE_INPUT(nAttr);
CHECK_MSTATUS(nAttr.setMin(0.0f, 0.0f) );
CHECK_MSTATUS(nAttr.setMax(1.0f, 1.0f) );
CHECK_MSTATUS(nAttr.setDefault(0.5f, 0.5f) );
MObject child1 = nAttr.create( "uCoord", "u", MFnNumericData::kFloat);
MObject child2 = nAttr.create( "vCoord", "v", MFnNumericData::kFloat);
aUVCoord = nAttr.create( "uvCoord","uv", child1, child2);
MAKE_INPUT(nAttr);
CHECK_MSTATUS(nAttr.setHidden(true) );
aOutColor = nAttr.createColor("outColor", "oc");
MAKE_OUTPUT(nAttr);
aOutAlpha = nAttr.create( "outAlpha", "oa", MFnNumericData::kFloat);
MAKE_OUTPUT(nAttr);
CHECK_MSTATUS(addAttribute(aColor1));
CHECK_MSTATUS(addAttribute(aColor2));
CHECK_MSTATUS(addAttribute(aBias));
CHECK_MSTATUS(addAttribute(aUVCoord));
CHECK_MSTATUS(addAttribute(aOutColor));
CHECK_MSTATUS(addAttribute(aOutAlpha));
CHECK_MSTATUS(attributeAffects (aColor1, aOutColor));
CHECK_MSTATUS(attributeAffects(aColor1, aOutAlpha));
CHECK_MSTATUS(attributeAffects (aColor2, aOutColor));
CHECK_MSTATUS(attributeAffects(aColor2, aOutAlpha));
CHECK_MSTATUS(attributeAffects(aBias, aOutColor));
CHECK_MSTATUS(attributeAffects(aBias, aOutAlpha));
CHECK_MSTATUS(attributeAffects (aUVCoord, aOutColor));
CHECK_MSTATUS(attributeAffects(aUVCoord, aOutAlpha));
return MS::kSuccess;
}
MStatus CheckerNode::compute(
const MPlug& plug,
MDataBlock& block )
{
if((plug != aOutColor) && (plug.parent() != aOutColor) &&
(plug != aOutAlpha))
return MS::kUnknownParameter;
MFloatVector resultColor;
float2 & uv = block.inputValue( aUVCoord ).asFloat2();
float2 & bias = block.inputValue( aBias ).asFloat2();
int count = 0;
if (uv[0] - floorf(uv[0]) < bias[0]) count++;
if (uv[1] - floorf(uv[1]) < bias[1]) count++;
if (count & 1)
resultColor = block.inputValue( aColor2 ).asFloatVector();
else
resultColor = block.inputValue( aColor1 ).asFloatVector();
MDataHandle outColorHandle = block.outputValue( aOutColor );
MFloatVector& outColor = outColorHandle.asFloatVector();
outColor = resultColor;
outColorHandle.setClean();
MDataHandle outAlphaHandle = block.outputValue( aOutAlpha );
float& outAlpha = outAlphaHandle.asFloat();
outAlpha = (count & 1) ? 1.f : 0.f;
outAlphaHandle.setClean();
return MS::kSuccess;
}
MStatus initializePlugin( MObject obj )
{
const MString UserClassify( "texture/2d" );
MFnPlugin plugin(obj, PLUGIN_COMPANY, "4.5", "Any");
CHECK_MSTATUS( plugin.registerNode("checkerTexture", CheckerNode::id,
CheckerNode::creator, CheckerNode::initialize,
MPxNode::kDependNode, &UserClassify ) );
return MS::kSuccess;
}
MStatus uninitializePlugin( MObject obj )
{
MFnPlugin plugin( obj );
CHECK_MSTATUS( plugin.deregisterNode( CheckerNode::id ) );
return MS::kSuccess;
}