slopeShaderNode.cpp
#include <math.h>
#include <slopeShaderNode.h>
#include <maya/MDataBlock.h>
#include <maya/MDataHandle.h>
#include <maya/MFloatVector.h>
#include <maya/MFnNumericAttribute.h>
#include <maya/MFnMatrixAttribute.h>
#include <maya/MFnTypedAttribute.h>
#include <maya/MFnGenericAttribute.h>
#define AWdegreesToRadians 0.0174532925199432957692369076848f
MTypeId slopeShaderNode::id( 0x81005 );
MObject slopeShaderNode::aAngle;
MObject slopeShaderNode::aColor1;
MObject slopeShaderNode::aColor2;
MObject slopeShaderNode::aTriangleNormalCamera;
MObject slopeShaderNode::aMatrixEyeToWorld;
MObject slopeShaderNode::aOutColor;
MObject slopeShaderNode::aDirtyShaderAttr;
slopeShaderNode::slopeShaderNode()
{
}
void slopeShaderNode::postConstructor( )
{
setMPSafe(true);
}
slopeShaderNode::~slopeShaderNode()
{
}
void* slopeShaderNode::creator()
{
return new slopeShaderNode();
}
MStatus slopeShaderNode::initialize()
{
MFnNumericAttribute nAttr;
MFnMatrixAttribute nMAttr;
MFnTypedAttribute nTAttr;
MFnGenericAttribute nGAttr;
aAngle = nAttr.create( "angle", "ang", MFnNumericData::kFloat);
nAttr.setDefault(30.0f);
nAttr.setMin(0.0f);
nAttr.setMax(100.0f);
nAttr.setKeyable(true);
nAttr.setStorable(true);
nAttr.setReadable(true);
nAttr.setWritable(true);
aColor1 = nAttr.createColor( "walkableColor", "w" );
nAttr.setDefault(0.0f, 1.0f, 0.0f);
nAttr.setKeyable(true);
nAttr.setStorable(true);
nAttr.setUsedAsColor(true);
nAttr.setReadable(true);
nAttr.setWritable(true);
aColor2 = nAttr.createColor( "nonWalkableColor", "nw" );
nAttr.setDefault(1.0f, 0.0f, 0.0f);
nAttr.setKeyable(true);
nAttr.setStorable(true);
nAttr.setUsedAsColor(true);
nAttr.setReadable(true);
nAttr.setWritable(true);
aTriangleNormalCamera = nAttr.createPoint( "triangleNormalCamera", "n" );
nAttr.setStorable(false);
nAttr.setHidden(true);
nAttr.setReadable(true);
nAttr.setWritable(true);
aMatrixEyeToWorld = nMAttr.create( "matrixEyeToWorld", "mew",
MFnMatrixAttribute::kFloat );
nAttr.setHidden(true);
nMAttr.setWritable(true);
aOutColor = nAttr.createColor( "outColor", "oc" );
nAttr.setStorable(false);
nAttr.setHidden(false);
nAttr.setReadable(true);
nAttr.setWritable(false);
aDirtyShaderAttr = nGAttr.create( "dirtyShaderPlug", "dsp");
nGAttr.setArray(true);
nGAttr.setHidden(false);
nGAttr.setUsesArrayDataBuilder(true);
nGAttr.setReadable(false);
nGAttr.setStorable(true);
nGAttr.setIndexMatters(false);
nGAttr.addAccept(MFnData::kMesh);
addAttribute(aAngle);
addAttribute(aColor1);
addAttribute(aColor2);
addAttribute(aTriangleNormalCamera);
addAttribute(aOutColor);
addAttribute(aMatrixEyeToWorld);
addAttribute(aDirtyShaderAttr);
attributeAffects (aAngle, aOutColor);
attributeAffects (aColor1, aOutColor);
attributeAffects (aColor2, aOutColor);
attributeAffects (aTriangleNormalCamera, aOutColor);
attributeAffects (aDirtyShaderAttr, aOutColor);
return MS::kSuccess;
}
MStatus slopeShaderNode::compute(const MPlug & plug, MDataBlock & block )
{
if ((plug != aOutColor) && (plug.parent() != aOutColor))
return MS::kUnknownParameter;
MFloatVector resultColor;
MFloatVector& walkable = block.inputValue( aColor1 ).asFloatVector();
MFloatVector& nonWalkable = block.inputValue( aColor2 ).asFloatVector();
MFloatVector& surfaceNormal = block.inputValue( aTriangleNormalCamera ).asFloatVector();
MFloatMatrix& viewMatrix = block.inputValue( aMatrixEyeToWorld ).asFloatMatrix();
float angle = block.inputValue( aAngle ).asFloat();
surfaceNormal.normalize();
MFloatVector WSVector = surfaceNormal * viewMatrix;
float scalarNormal = WSVector * MFloatVector(0, 1, 0);
if (scalarNormal < 0.0) scalarNormal *= -1.0;
if(cos(angle*AWdegreesToRadians) < scalarNormal)
resultColor = walkable;
else
resultColor = nonWalkable;
MDataHandle outColorHandle = block.outputValue( aOutColor );
MFloatVector& outColor = outColorHandle.asFloatVector();
outColor = resultColor;
outColorHandle.setClean();
return MS::kSuccess;
}