#include <maya/MIOStream.h>
#include <math.h>
#include <cstdlib>
#include <maya/MString.h>
#include <maya/MPlug.h>
#include <maya/MFnNumericAttribute.h>
#include <maya/MFloatVector.h>
#include <maya/MFnPlugin.h>
#include <maya/MFnDependencyNode.h>
#include <maya/MHWShaderSwatchGenerator.h>
#include <maya/MImage.h>
#include <maya/MRenderUtilities.h>
#include <maya/MMatrix.h>
#include <maya/MDrawRegistry.h>
#include <maya/MPxShaderOverride.h>
#include <maya/MDrawContext.h>
#include <maya/MStateManager.h>
#include <maya/MViewport2Renderer.h>
#include <maya/MShaderManager.h>
#undef ENABLE_TRACE_API_CALLS
#ifdef ENABLE_TRACE_API_CALLS
#define TRACE_API_CALLS(x) cerr <<(x)<<"\n"
#else
#define TRACE_API_CALLS(x)
#endif
#include "vp2BlinnShader.h"
MTypeId vp2BlinnShader::id( 0x00081102 );
MObject vp2BlinnShader::aTransparency;
MObject vp2BlinnShader::aSpecularColor;
MObject vp2BlinnShader::aSpecularRolloff;
MObject vp2BlinnShader::aEccentricity;
MObject vp2BlinnShader::aNonTexturedColor;
MObject vp2BlinnShader::aNonTexturedTransparency;
void * vp2BlinnShader::creator()
{
TRACE_API_CALLS("vp2BlinnShader::creator");
return new vp2BlinnShader();
}
vp2BlinnShader::vp2BlinnShader()
{
TRACE_API_CALLS("vp2BlinnShader::vp2BlinnShader");
}
vp2BlinnShader::~vp2BlinnShader()
{
TRACE_API_CALLS("vp2BlinnShader::~vp2BlinnShader");
}
MStatus vp2BlinnShader::initialize()
{
TRACE_API_CALLS("vp2BlinnShader::initialize");
aSpecularColor = nAttr.
createColor(
"specularColor",
"sc" );
aNonTexturedColor = nAttr.
createColor(
"nonTexturedColor",
"nc");
addAttribute(aColor);
addAttribute(aDiffuse);
addAttribute(aTransparency);
addAttribute(aSpecularColor);
addAttribute(aSpecularRolloff);
addAttribute(aEccentricity);
addAttribute(aNonTexturedColor);
addAttribute(aNonTexturedTransparency);
attributeAffects (aColor, outColor);
attributeAffects (aDiffuse, outColor);
attributeAffects (aTransparency, outColor);
attributeAffects (aSpecularColor, outColor);
attributeAffects (aSpecularRolloff, outColor);
attributeAffects (aEccentricity, outColor);
attributeAffects (aNonTexturedColor,outColor);
attributeAffects (aNonTexturedTransparency,outColor);
return MS::kSuccess;
}
{
TRACE_API_CALLS("vp2BlinnShader::compute");
if ((plug != outColor) && (plug.
parent() != outColor))
return MS::kUnknownParameter;
outColor = color;
return MS::kSuccess;
}
{
{
MString meshShaderball(
"meshShaderball");
unsigned int targetW, targetH;
outImage.
getSize(targetW, targetH);
thisMObject(),
outImage,
}
return MS::kSuccess;
}
{
public:
{
return new vp2BlinnShaderOverride(obj);
}
~vp2BlinnShaderOverride() override
{
if (theRenderer)
{
if (shaderMgr)
{
if (fColorShaderInstance)
{
}
fColorShaderInstance = NULL;
if (fNonTexturedColorShaderInstance)
{
}
fColorShaderInstance = NULL;
}
}
}
{
TRACE_API_CALLS("vp2BlinnShaderOverride::initialize");
if (fColorShaderInstance)
{
}
return MString(
"Autodesk Maya vp2 Blinn Shader Override");
}
{
TRACE_API_CALLS("vp2BlinnShaderOverride::updateDG");
return;
if (!shaderNode)
return;
if (status)
{
node.findPlug("colorR", true).getValue(fDiffuseColor[0]);
node.findPlug("colorG", true).getValue(fDiffuseColor[1]);
node.findPlug("colorB", true).getValue(fDiffuseColor[2]);
node.findPlug("diffuse", true).getValue(fDiffuse);
node.findPlug("transparency", true).getValue(fTransparency);
fDiffuseColor[3] = 1.0f - fTransparency;
node.findPlug("specularColorR", true).getValue(fSpecular[0]);
node.findPlug("specularColorG", true).getValue(fSpecular[1]);
node.findPlug("specularColorB", true).getValue(fSpecular[2]);
node.findPlug("specularRollOff", true).getValue(fSpecularRollOff);
node.findPlug("eccentricity", true).getValue(fEccentricity);
node.findPlug("nonTexturedColorR", true).getValue(fNonTextured[0]);
node.findPlug("nonTexturedColorG", true).getValue(fNonTextured[1]);
node.findPlug("nonTexturedColorB", true).getValue(fNonTextured[2]);
node.findPlug("nonTexturedTransparency", true).getValue(fNonTexturedTransparency);
fNonTextured[3] = 1.0f - fNonTexturedTransparency;
}
}
{
updateShaderInstance();
}
{
TRACE_API_CALLS("vp2BlinnShaderOverride::isTransparent");
return (fTransparency > 0.0f);
}
{
TRACE_API_CALLS("vp2BlinnShaderOverride::endUpdate");
}
{
TRACE_API_CALLS("vp2BlinnShaderOverride::shaderInstance");
return fColorShaderInstance;
}
{
MString out(
"vp2BlinnShaderOverride::activateKey[");
out += key;
out += "]";
TRACE_API_CALLS(out.asChar());
fColorShaderInstance->bind( context );
}
{
MString out(
"vp2BlinnShaderOverride::draw[Count=");
out += renderItemList.
length();
out += "]";
TRACE_API_CALLS(out);
unsigned int passCount = fColorShaderInstance->getPassCount( context );
for (unsigned int i=0; i<passCount; i++)
{
fColorShaderInstance->activatePass( context, i );
}
return true;
}
{
MString out(
"vp2BlinnShaderOverride::terminateKey[");
out += key;
out += "]";
TRACE_API_CALLS(out.asChar());
fColorShaderInstance->unbind( context );
}
{
return (MHWRender::kOpenGL | MHWRender::kDirectX11 | MHWRender::kOpenGLCoreProfile);
}
{
TRACE_API_CALLS("vp2BlinnShaderOverride::nonTexturedShaderInstance");
if (fNonTexturedColorShaderInstance)
{
monitor = true;
fNonTexturedColorShaderInstance->setIsTransparent( fNonTexturedTransparency > 0.0f );
fNonTexturedColorShaderInstance->setParameter("diffuse", 0.8f );
float spec[3] = { 1.0f, 1.0f, 1.0f };
fNonTexturedColorShaderInstance->setParameter("specularColor", &spec[0]);
fNonTexturedColorShaderInstance->setParameter("eccentricity", 0.3f);
fNonTexturedColorShaderInstance->setParameter("specularRollOff", 0.7f);
return fNonTexturedColorShaderInstance;
}
return NULL;
}
protected:
void updateShaderInstance()
{
TRACE_API_CALLS("vp2BlinnShaderOverride::updateShaderInstance");
if (fColorShaderInstance)
{
static bool debugShaderParameters = false;
if (debugShaderParameters)
{
fColorShaderInstance->parameterList(list);
for (
unsigned int i=0; i<list.
length(); i++)
{
fprintf(stderr, "Param[%d] = %s. type=%d\n", i, list[i].asChar(), ptype);
}
}
float trans[3] = { fTransparency, fTransparency, fTransparency };
fColorShaderInstance->setParameter("transparency", &trans[0]);
fColorShaderInstance->setParameter("diffuse", fDiffuse);
fColorShaderInstance->setParameter("color", &fDiffuseColor[0]);
fColorShaderInstance->setParameter("specularColor", &fSpecular[0]);
fColorShaderInstance->setParameter("eccentricity", fEccentricity);
fColorShaderInstance->setParameter("specularRollOff", fSpecularRollOff);
}
if (fNonTexturedColorShaderInstance)
{
fNonTexturedColorShaderInstance->setIsTransparent( fNonTexturedTransparency > 0.0f );
float trans[3] = { fNonTexturedTransparency, fNonTexturedTransparency, fNonTexturedTransparency };
fNonTexturedColorShaderInstance->setParameter("transparency", &trans[0]);
fNonTexturedColorShaderInstance->setParameter("color", &fNonTextured[0]);
}
}
void createShaderInstance()
{
TRACE_API_CALLS("vp2BlinnShaderOverride::createShaderInstance");
if (!shaderMgr)
return;
if (!fColorShaderInstance)
{
fColorShaderInstance = shaderMgr->
getFragmentShader(
"mayaBlinnSurface",
"outSurfaceFinal",
true);
}
if (!fNonTexturedColorShaderInstance)
{
fNonTexturedColorShaderInstance = shaderMgr->
getFragmentShader(
"mayaBlinnSurface",
"outSurfaceFinal",
true);
if (fNonTexturedColorShaderInstance)
{
fNonTexturedColorShaderInstance->
setParameter(
"color", &fNonTextured[0]);
}
}
}
vp2BlinnShaderOverride(
const MObject& obj)
, fColorShaderInstance(NULL)
, fNonTexturedColorShaderInstance(NULL)
, fNonTexturedTransparency(0.0f)
, fTransparency(0.0f)
, fDiffuse(0.8f)
, fEccentricity(0.3f)
, fSpecularRollOff(0.7f)
{
fDiffuseColor[0] = fDiffuseColor[1] = fDiffuseColor[2] = fDiffuseColor[3] = 0.0f;
fSpecular[0] = fSpecular[1] = fSpecular[2] = 0.0f;
fNonTextured[0] = 1.0; fNonTextured[1] = fNonTextured[2] = fNonTextured[3] = 0.0f;
fNonTexturedTransparency = 1.0f;
createShaderInstance();
}
float fTransparency;
float fDiffuse;
float fDiffuseColor[4];
float fSpecular[3];
float fSpecularRollOff;
float fEccentricity;
float fShininess[3];
float fNonTextured[4];
float fNonTexturedTransparency;
};
static const MString svp2BlinnShaderRegistrantId(
"vp2BlinnShaderRegistrantId");
{
TRACE_API_CALLS("initializePlugin");
const MString UserClassify(
"shader/surface/utility/:drawdb/shader/surface/vp2BlinnShader:swatch/"+swatchName);
MFnPlugin plugin( obj, PLUGIN_COMPANY,
"1.0",
"Any");
status = plugin.registerNode( "vp2BlinnShader", vp2BlinnShader::id,
vp2BlinnShader::creator, vp2BlinnShader::initialize,
if (!status) {
status.
perror(
"registerNode");
return status;
}
"drawdb/shader/surface/vp2BlinnShader",
svp2BlinnShaderRegistrantId,
vp2BlinnShaderOverride::Creator);
if (status != MS::kSuccess) return status;
return MS::kSuccess;
}
{
TRACE_API_CALLS("uninitializePlugin");
plugin.deregisterNode( vp2BlinnShader::id );
if (!status) {
status.
perror(
"deregisterNode");
return status;
}
"drawdb/shader/surface/vp2BlinnShader", svp2BlinnShaderRegistrantId);
if (status != MS::kSuccess) return status;
return MS::kSuccess;
}