#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/MArrayDataHandle.h>
#include <maya/MFnNumericAttribute.h>
#include <maya/MFnLightDataAttribute.h>
#include <maya/MFloatVector.h>
#include <maya/MFnPlugin.h>
#include <maya/MDrawRegistry.h>
#include <maya/MPxSurfaceShadingNodeOverride.h>
#include <maya/MViewport2Renderer.h>
#include <maya/MFragmentManager.h>
 
 
 
{
    public:
                      depthShader();
    virtual          ~depthShader();
    static  void *    creator();
    
    private:
    
    
};
{
public:
    virtual ~depthShaderOverride();
private:
    depthShaderOverride(
const MObject& obj);
};
MTypeId depthShader::id( 0x81002 );
 
#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 depthShader::postConstructor( )
{
    setMPSafe(true);
}
depthShader::depthShader()
{
}
depthShader::~depthShader()
{
}
void* depthShader::creator()
{
    return new depthShader();
}
{
    
    MAKE_INPUT(nAttr);
    MAKE_INPUT(nAttr);
    MAKE_INPUT(nAttr);
    MAKE_INPUT(nAttr);
    MAKE_INPUT(nAttr);
    
    MAKE_OUTPUT(nAttr);
}
{
    
    if((plug != aOutColor) && (plug.
parent() != aOutColor))
 
    
    
    float ratio = (farClip + pCamera.
z) / ( farClip - nearClip);
 
    resultColor = cNear * ratio + cFar*(1.f - ratio);
    
    outColor = resultColor;
    outColorHandle.setClean();
}
{
    return new depthShaderOverride(obj);
}
depthShaderOverride::depthShaderOverride(
const MObject& obj)
: MPxSurfaceShadingNodeOverride(obj)
, fFragmentName("")
{
    
    
    
    
    
    
    
    
    
    
    static const MString sFragmentName(
"depthShaderPluginFragment");
 
    static const char* sFragmentBody =
        "<fragment uiName=\"depthShaderPluginFragment\" name=\"depthShaderPluginFragment\" type=\"plumbing\" class=\"ShadeFragment\" version=\"1.0\">"
        "   <description><![CDATA[Depth shader fragment]]></description>"
        "   <properties>"
        "       <float name=\"depthValue\" />"
        "       <float3 name=\"color\" />"
        "       <float3 name=\"colorFar\" />"
        "       <float name=\"near\" />"
        "       <float name=\"far\" />"
        "   </properties>"
        "   <values>"
        "       <float name=\"depthValue\" value=\"0.0\" />"
        "       <float3 name=\"color\" value=\"0.0,1.0,0.0\" />"
        "       <float3 name=\"colorFar\" value=\"0.0,0.0,1.0\" />"
        "       <float name=\"near\" value=\"0.0\" />"
        "       <float name=\"far\" value=\"2.0\" />"
        "   </values>"
        "   <outputs>"
        "       <float3 name=\"outColor\" />"
        "   </outputs>"
        "   <implementation>"
        "   <implementation render=\"OGSRenderer\" language=\"Cg\" lang_version=\"2.1\">"
        "       <function_name val=\"depthShaderPluginFragment\" />"
        "       <source><![CDATA["
        "float3 depthShaderPluginFragment(float depthValue, float3 cNear, float3 cFar, float nearClip, float farClip) \n"
        "{ \n"
        "   float ratio = (farClip + depthValue)/(farClip - nearClip); \n"
        "   return cNear*ratio + cFar*(1.0f - ratio); \n"
        "} \n]]>"
        "       </source>"
        "   </implementation>"
        "   <implementation render=\"OGSRenderer\" language=\"HLSL\" lang_version=\"11.0\">"
        "       <function_name val=\"depthShaderPluginFragment\" />"
        "       <source><![CDATA["
        "float3 depthShaderPluginFragment(float depthValue, float3 cNear, float3 cFar, float nearClip, float farClip) \n"
        "{ \n"
        "   float ratio = (farClip + depthValue)/(farClip - nearClip); \n"
        "   return cNear*ratio + cFar*(1.0f - ratio); \n"
        "} \n]]>"
        "       </source>"
        "   </implementation>"
        "   <implementation render=\"OGSRenderer\" language=\"GLSL\" lang_version=\"3.0\">"
        "       <function_name val=\"depthShaderPluginFragment\" />"
        "       <source><![CDATA["
        "vec3 depthShaderPluginFragment(float depthValue, vec3 cNear, vec3 cFar, float nearClip, float farClip) \n"
        "{ \n"
        "   float ratio = (farClip + depthValue)/(farClip - nearClip); \n"
        "   return cNear*ratio + cFar*(1.0f - ratio); \n"
        "} \n]]>"
        "       </source>"
        "   </implementation>"
        "   </implementation>"
        "</fragment>";
    static const MString sVertexFragmentName(
"depthShaderPluginInterpolantFragment");
 
    static const char* sVertexFragmentBody =
        "<fragment uiName=\"depthShaderPluginInterpolantFragment\" name=\"depthShaderPluginInterpolantFragment\" type=\"interpolant\" class=\"ShadeFragment\" version=\"1.0\">"
        "   <description><![CDATA[Depth shader vertex fragment]]></description>"
        "   <properties>"
        "       <float3 name=\"Pm\" semantic=\"Pm\" flags=\"varyingInputParam\" />"
        "       <float4x4 name=\"worldViewProj\" semantic=\"worldviewprojection\" />"
        "   </properties>"
        "   <values>"
        "   </values>"
        "   <outputs>"
        "       <float name=\"outDepthValue\" ^1s/>"
        "   </outputs>"
        "   <implementation>"
        "   <implementation render=\"OGSRenderer\" language=\"Cg\" lang_version=\"2.1\">"
        "       <function_name val=\"depthShaderPluginInterpolantFragment\" />"
        "       <source><![CDATA["
        "float depthShaderPluginInterpolantFragment(float depthValue) \n"
        "{ \n"
        "   return depthValue; \n"
        "} \n]]>"
        "       </source>"
        "       <vertex_source><![CDATA["
        "float idepthShaderPluginInterpolantFragment(float3 Pm, float4x4 worldViewProj) \n"
        "{ \n"
        "   float4 pCamera = mul(worldViewProj, float4(Pm, 1.0f)); \n"
        "   return (pCamera.z - pCamera.w*2.0f); \n"
        "} \n]]>"
        "       </vertex_source>"
        "   </implementation>"
        "   <implementation render=\"OGSRenderer\" language=\"HLSL\" lang_version=\"11.0\">"
        "       <function_name val=\"depthShaderPluginInterpolantFragment\" />"
        "       <source><![CDATA["
        "float depthShaderPluginInterpolantFragment(float depthValue) \n"
        "{ \n"
        "   return depthValue; \n"
        "} \n]]>"
        "       </source>"
        "       <vertex_source><![CDATA["
        "float idepthShaderPluginInterpolantFragment(float3 Pm, float4x4 worldViewProj) \n"
        "{ \n"
        "   float4 pCamera = mul(float4(Pm, 1.0f), worldViewProj); \n"
        "   return (pCamera.z - pCamera.w*2.0f); \n"
        "} \n]]>"
        "       </vertex_source>"
        "   </implementation>"
        "   <implementation render=\"OGSRenderer\" language=\"GLSL\" lang_version=\"3.0\">"
        "       <function_name val=\"depthShaderPluginInterpolantFragment\" />"
        "       <source><![CDATA["
        "float depthShaderPluginInterpolantFragment(float depthValue) \n"
        "{ \n"
        "   return depthValue; \n"
        "} \n]]>"
        "       </source>"
        "       <vertex_source><![CDATA["
        "float idepthShaderPluginInterpolantFragment(vec3 Pm, mat4 worldViewProj) \n"
        "{ \n"
        "   vec4 pCamera = worldViewProj * vec4(Pm, 1.0f); \n"
        "   return (pCamera.z - pCamera.w*2.0f); \n"
        "} \n]]>"
        "       </vertex_source>"
        "   </implementation>"
        "   </implementation>"
        "</fragment>";
    static const MString sFragmentGraphName(
"depthShaderPluginGraph");
 
    static const char* sFragmentGraphBody =
        "<fragment_graph name=\"depthShaderPluginGraph\" ref=\"depthShaderPluginGraph\" class=\"FragmentGraph\" version=\"1.0\">"
        "   <fragments>"
        "           <fragment_ref name=\"depthShaderPluginFragment\" ref=\"depthShaderPluginFragment\" />"
        "           <fragment_ref name=\"depthShaderPluginInterpolantFragment\" ref=\"depthShaderPluginInterpolantFragment\" />"
        "   </fragments>"
        "   <connections>"
        "       <connect from=\"depthShaderPluginInterpolantFragment.outDepthValue\" to=\"depthShaderPluginFragment.depthValue\" />"
        "   </connections>"
        "   <properties>"
        "       <float3 name=\"Pm\" ref=\"depthShaderPluginInterpolantFragment.Pm\" semantic=\"Pm\" flags=\"varyingInputParam\" />"
        "       <float4x4 name=\"worldViewProj\" ref=\"depthShaderPluginInterpolantFragment.worldViewProj\" semantic=\"worldviewprojection\" />"
        "       <float3 name=\"color\" ref=\"depthShaderPluginFragment.color\" />"
        "       <float3 name=\"colorFar\" ref=\"depthShaderPluginFragment.colorFar\" />"
        "       <float name=\"near\" ref=\"depthShaderPluginFragment.near\" />"
        "       <float name=\"far\" ref=\"depthShaderPluginFragment.far\" />"
        "   </properties>"
        "   <values>"
        "       <float3 name=\"color\" value=\"0.0,1.0,0.0\" />"
        "       <float3 name=\"colorFar\" value=\"0.0,0.0,1.0\" />"
        "       <float name=\"near\" value=\"0.0\" />"
        "       <float name=\"far\" value=\"2.0\" />"
        "   </values>"
        "   <outputs>"
        "       <float3 name=\"outColor\" ref=\"depthShaderPluginFragment.outColor\" />"
        "   </outputs>"
        "</fragment_graph>";
    
    if (theRenderer)
    {
        if (fragmentMgr)
        {
            
            bool fragAdded = fragmentMgr->
hasFragment(sFragmentName);
 
            bool vertFragAdded = fragmentMgr->
hasFragment(sVertexFragmentName);
 
            bool graphAdded = fragmentMgr->
hasFragment(sFragmentGraphName);
 
            if (!fragAdded)
            {
            }
            if (!vertFragAdded)
            {
                
                {
                }
                else
                {
                }
            }
            if (!graphAdded)
            {
            }
            
            if (fragAdded && vertFragAdded && graphAdded)
            {
                fFragmentName = sFragmentGraphName;
            }
        }
    }
}
depthShaderOverride::~depthShaderOverride()
{
}
{
}
MString depthShaderOverride::fragmentName()
 const 
{
    return fFragmentName;
}
static const MString sRegistrantId(
"depthShaderPlugin");
 
{
    const MString UserClassify( 
"shader/surface:drawdb/shader/surface/depthShader" );
 
    MFnPlugin plugin(obj, PLUGIN_COMPANY, 
"4.5", 
"Any");
 
    CHECK_MSTATUS( plugin.registerNode(
"depthShader", depthShader::id,
 
                        depthShader::creator, depthShader::initialize,
            "drawdb/shader/surface/depthShader",
            sRegistrantId,
            depthShaderOverride::creator));
}
{
            "drawdb/shader/surface/depthShader",
            sRegistrantId));
}