#include <maya/MDrawContext.h>
#include <maya/MDrawRegistry.h>
#include <maya/MFnPlugin.h>
#include <maya/MFragmentManager.h>
#include <maya/MGlobal.h>
#include <maya/MPxHardwareShader.h>
#include <maya/MPxShaderOverride.h>
#include <maya/MRenderUtilities.h>
#include <maya/MShaderManager.h>
#include <maya/MStateManager.h>
#include <maya/MString.h>
{
public:
~customSpriteShader() override
{
}
static void* creator()
{
return new customSpriteShader();
}
{
return MS::kSuccess;
}
private:
customSpriteShader()
{
}
};
MTypeId customSpriteShader::id(0x00081033);
{
public:
~customSpriteShaderOverride() override;
{
return new customSpriteShaderOverride(obj);
}
{
}
{
return fShaderInstance;
}
{
monitorNode = false;
return fNonTexturedShaderInstance;
}
{
return true;
}
{
return true;
}
MString initialize(
const MInitContext& initContext, MInitFeedback& initFeedback)
override;
static MStatus registerShadeFragments();
static MStatus deregisterShadeFragments();
private:
customSpriteShaderOverride(
const MObject& obj);
void createShaderInstances();
private:
static bool sDebugFragment;
};
bool customSpriteShaderOverride::sDebugFragment = (getenv("MAYA_CUSTOMSPRITESHADER_DEBUG_FRAGMENTS") != NULL);
customSpriteShaderOverride::customSpriteShaderOverride(
const MObject& obj)
, fShaderInstance(NULL)
, fNonTexturedShaderInstance(NULL)
{
createShaderInstances();
}
customSpriteShaderOverride::~customSpriteShaderOverride()
{
if (renderer)
{
if (shaderMgr)
{
if (fShaderInstance)
{
fShaderInstance = NULL;
}
if (fNonTexturedShaderInstance)
{
fNonTexturedShaderInstance = NULL;
}
}
}
}
void customSpriteShaderOverride::createShaderInstances()
{
if (renderer)
{
if (shaderMgr)
{
"customFileTextureBlinnShader", "outSurfaceFinal", true);
"customPoint2ViewAlignedTexturedQuad", "GPUStage", "GPUStage");
{
}
path +=
MString(
"/plug-ins/customSpriteShader/");
if (texture)
{
texResource.
texture = texture;
fShaderInstance->setParameter("map", texResource);
}
else
{
MString errorMsg =
MString(
"customSpriteShader failed to acquire texture from ") + path +
MString(
"snow.png");
}
if (sampler)
{
fShaderInstance->setParameter("textureSampler", *sampler);
}
"customSolidColorBlinnShader", "outSurfaceFinal", true);
"customPoint2ViewAlignedSolidQuad", "GPUStage", "GPUStage");
float customColor[3] = {1.0, 0.0, 0.0};
fNonTexturedShaderInstance->setParameter("customColor", customColor);
float customTransparency[3] = { 0.0, 0.0, 0.0 };
fNonTexturedShaderInstance->setParameter("customTransparency", customTransparency);
fNonTexturedShaderInstance->setIsTransparent(false);
if (sDebugFragment)
{
fShaderInstance->writeEffectSourceToFile(filePath);
if (dc && shaderInstance)
{
if (!shaderInstance->
bind(*dc))
{
}
}
}
}
}
}
MString customSpriteShaderOverride::initialize(
const MInitContext&, MInitFeedback&)
{
empty,
3);
empty,
3);
spritePP,
spritePP,
4);
empty,
2);
addGeometryRequirement(positionDesc);
addGeometryRequirement(normalDesc);
addGeometryRequirement(spriteDesc);
addGeometryRequirement(uvDesc);
if (fShaderInstance)
{
addShaderSignature(*fShaderInstance);
}
return MString(
"customSpriteShaderOverride");
}
{
bool handlePass = false;
for (
unsigned int i=0; i<passSem.
length(); i++)
{
{
{
handlePass = true;
}
}
{
handlePass = true;
}
{
handlePass = false;
}
}
return handlePass;
}
{
if (fShaderInstance)
{
fShaderInstance->bind(context);
}
}
{
if (fShaderInstance)
{
unsigned int passCount = fShaderInstance->getPassCount(context);
if (passCount)
{
for (unsigned int i = 0; i < passCount; i++)
{
fShaderInstance->activatePass(context, i);
}
}
}
return true;
}
{
if (fShaderInstance)
{
fShaderInstance->unbind(context);
}
}
bool asGraph)
{
{
MString errorMsg =
MString(
"customSpriteShader failed to add fragment from ") + fileName;
return false;
}
sFragmentArray.append(fragName);
return true;
}
MStatus customSpriteShaderOverride::registerShadeFragments()
{
if (renderer)
{
if (fragMgr)
{
{
}
path += "/plug-ins/customSpriteShader";
MString info =
MString(
"customSpriteShader added a fragment search path: ") + path;
if (addFragmentXML(*fragMgr, "customFileTextureOutputColor.xml", false) &&
addFragmentXML(*fragMgr, "customFileTextureOutputTransparency.xml", false) &&
addFragmentXML(*fragMgr, "customPoint2ViewAlignedSolidQuad.xml", false) &&
addFragmentXML(*fragMgr, "customPoint2ViewAlignedTexturedQuad.xml", false) &&
addFragmentXML(*fragMgr, "customFileTextureBlinnShader.xml", true) &&
addFragmentXML(*fragMgr, "customSolidColorBlinnShader.xml", true))
{
return MS::kSuccess;
}
}
}
return MS::kFailure;
}
MStatus customSpriteShaderOverride::deregisterShadeFragments()
{
bool success = false;
if (renderer)
{
if (fragMgr)
{
success = true;
for (unsigned int i = 0; i < sFragmentArray.length(); i++)
{
MString fragName = sFragmentArray[i];
{
success = false;
MString errorMsg =
MString(
"customSpriteShader failed to remove fragment ") + sFragmentArray[i];
}
}
}
}
sFragmentArray.clear();
return success ? MS::kSuccess : MS::kFailure;
}
static MString sCustomSpriteShaderRegistrantId(
"customSpriteShaderRegistrantId");
static MString sCustomSpriteShaderDrawdbClassification(
"drawdb/shader/surface/customSpriteShader");
{
MStatus status = customSpriteShaderOverride::registerShadeFragments();
if (status != MS::kSuccess)
{
status.
perror(
"registerShadeFragments");
return status;
}
MFnPlugin plugin(obj,
"Autodesk",
"1.0",
"Any");
MString UserClassification(
"shader/surface/:");
UserClassification += sCustomSpriteShaderDrawdbClassification;
status = plugin.registerNode(
"customSpriteShader",
customSpriteShader::id,
customSpriteShader::creator,
customSpriteShader::initialize,
&UserClassification);
if (status != MS::kSuccess)
{
status.
perror(
"registerNode");
return status;
}
sCustomSpriteShaderDrawdbClassification,
sCustomSpriteShaderRegistrantId,
customSpriteShaderOverride::creator);
if (status != MS::kSuccess)
{
status.
perror(
"registerShaderOverrideCreator");
return status;
}
return status;
}
{
MStatus status = plugin.deregisterNode(customSpriteShader::id);
if (status != MS::kSuccess)
{
status.
perror(
"deregisterNode");
return status;
}
sCustomSpriteShaderDrawdbClassification,
sCustomSpriteShaderRegistrantId);
if (status != MS::kSuccess)
{
status.
perror(
"deregisterShaderOverrideCreator");
return status;
}
status = customSpriteShaderOverride::deregisterShadeFragments();
if (status != MS::kSuccess)
{
status.
perror(
"deregisterShadeFragments");
return status;
}
return status;
}