geomShader.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 GeomNode : public MPxNode
{
public:
GeomNode();
virtual ~GeomNode();
virtual MStatus compute( const MPlug&, MDataBlock& );
virtual void postConstructor();
static void * creator();
static MStatus initialize();
static MTypeId id;
private:
static MObject aPoint;
static MObject aScale;
static MObject aOffset;
static MObject aOutColor;
};
MTypeId GeomNode::id( 0x81004 );
MObject GeomNode::aPoint;
MObject GeomNode::aScale;
MObject GeomNode::aOffset;
MObject GeomNode::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 GeomNode::postConstructor( )
{
setMPSafe(true);
}
GeomNode::GeomNode()
{
}
GeomNode::~GeomNode()
{
}
void* GeomNode::creator()
{
return new GeomNode();
}
MStatus GeomNode::initialize()
{
MFnNumericAttribute nAttr;
aPoint = nAttr.createPoint( "pointObj", "p" );
CHECK_MSTATUS ( nAttr.setStorable(false) );
CHECK_MSTATUS ( nAttr.setHidden(true) );
aScale = nAttr.createPoint( "scale", "s" );
MAKE_INPUT(nAttr);
CHECK_MSTATUS ( nAttr.setDefault(1.0f, 1.0f, 1.0f) );
aOffset = nAttr.createPoint( "offset", "o" );
MAKE_INPUT(nAttr);
aOutColor = nAttr.createColor( "outColor", "oc" );
MAKE_OUTPUT(nAttr);
CHECK_MSTATUS ( addAttribute(aPoint) );
CHECK_MSTATUS ( addAttribute(aScale) );
CHECK_MSTATUS ( addAttribute(aOffset) );
CHECK_MSTATUS ( addAttribute(aOutColor) );
CHECK_MSTATUS ( attributeAffects (aPoint, aOutColor) );
CHECK_MSTATUS ( attributeAffects (aScale, aOutColor) );
CHECK_MSTATUS ( attributeAffects (aOffset,aOutColor) );
return MS::kSuccess;
}
MStatus GeomNode::compute(
const MPlug& plug,
MDataBlock& block )
{
if((plug != aOutColor) && (plug.parent() != aOutColor))
return MS::kUnknownParameter;
MFloatVector resultColor;
MFloatVector & point = block.inputValue( aPoint ).asFloatVector();
MFloatVector & scale = block.inputValue( aScale ).asFloatVector();
MFloatVector & offset = block.inputValue( aOffset ).asFloatVector();
resultColor.x = point.x * scale.x + offset.x;
resultColor.y = point.y * scale.y + offset.y;
resultColor.z = point.z * scale.z + offset.z;
MDataHandle outColorHandle = block.outputValue( aOutColor );
MFloatVector& outColor = outColorHandle.asFloatVector();
outColor = resultColor;
outColorHandle.setClean();
return MS::kSuccess;
}
MStatus initializePlugin( MObject obj )
{
const MString UserClassify( "utility/general" );
MFnPlugin plugin( obj, PLUGIN_COMPANY, "4.5", "Any");
CHECK_MSTATUS ( plugin.registerNode( "geomNode", GeomNode::id,
GeomNode::creator, GeomNode::initialize,
MPxNode::kDependNode, &UserClassify ) );
return MS::kSuccess;
}
MStatus uninitializePlugin( MObject obj )
{
MFnPlugin plugin( obj );
CHECK_MSTATUS ( plugin.deregisterNode( GeomNode::id ) );
return MS::kSuccess;
}