1 from __future__
import division
14 import maya.api.OpenMaya
as om
15 import maya.api.OpenMayaRender
as omr
27 The presence of this function tells Maya that the plugin produces, and
28 expects to be passed, objects created using the Maya Python API 2.0.
35 class depthShader(om.MPxNode):
37 id = om.MTypeId( 0x81164 )
55 nAttr = om.MFnNumericAttribute()
59 depthShader.aColorNear = nAttr.createColor(
"color",
"c")
64 nAttr.default = (0.0, 1.0, 0.0)
67 depthShader.aColorFar = nAttr.createColor(
"colorFar",
"cf")
72 nAttr.default = (0.0, 0.0, 1.0)
74 depthShader.aNear = nAttr.create(
"near",
"n", om.MFnNumericData.kFloat)
80 nAttr.setSoftMax(1000.0)
82 depthShader.aFar = nAttr.create(
"far",
"f", om.MFnNumericData.kFloat)
88 nAttr.setSoftMax(1000.0)
91 depthShader.aPointCamera = nAttr.createPoint(
"pointCamera",
"p")
99 depthShader.aOutColor = nAttr.createColor(
"outColor",
"oc")
100 nAttr.keyable =
False
101 nAttr.storable =
False
102 nAttr.readable =
True
103 nAttr.writable =
False
105 om.MPxNode.addAttribute(depthShader.aColorNear)
106 om.MPxNode.addAttribute(depthShader.aColorFar)
107 om.MPxNode.addAttribute(depthShader.aNear)
108 om.MPxNode.addAttribute(depthShader.aFar)
109 om.MPxNode.addAttribute(depthShader.aPointCamera)
110 om.MPxNode.addAttribute(depthShader.aOutColor)
112 om.MPxNode.attributeAffects(depthShader.aColorNear, depthShader.aOutColor)
113 om.MPxNode.attributeAffects(depthShader.aColorFar, depthShader.aOutColor)
114 om.MPxNode.attributeAffects(depthShader.aNear, depthShader.aOutColor)
115 om.MPxNode.attributeAffects(depthShader.aFar, depthShader.aOutColor)
116 om.MPxNode.attributeAffects(depthShader.aPointCamera, depthShader.aOutColor)
119 om.MPxNode.__init__(self)
121 def compute(self, plug, block):
123 if (plug != depthShader.aOutColor)
and (plug.parent() != depthShader.aOutColor):
127 pCamera = block.inputValue(depthShader.aPointCamera).asFloatVector()
128 cNear = block.inputValue(depthShader.aColorNear).asFloatVector()
129 cFar = block.inputValue(depthShader.aColorFar).asFloatVector()
130 nearClip = block.inputValue(depthShader.aNear).asFloat()
131 farClip = block.inputValue(depthShader.aFar).asFloat()
135 dist = farClip - nearClip
137 ratio = (farClip + pCamera.z) / dist
138 resultColor = cNear * ratio + cFar*(1.0 - ratio)
141 outColorHandle = block.outputValue( depthShader.aOutColor )
142 outColorHandle.setMFloatVector( resultColor )
143 outColorHandle.setClean()
148 def postConstructor(self):
154 class depthShaderOverride(omr.MPxSurfaceShadingNodeOverride):
157 return depthShaderOverride(obj)
159 def __init__(self, obj):
160 omr.MPxSurfaceShadingNodeOverride.__init__(self, obj)
163 fragmentMgr = omr.MRenderer.getFragmentManager()
164 if fragmentMgr !=
None:
165 if not fragmentMgr.hasFragment(
"depthShaderPluginFragment"):
166 fragmentBody = textwrap.dedent(
"""
167 <fragment uiName=\"depthShaderPluginFragment\" name=\"depthShaderPluginFragment\" type=\"plumbing\" class=\"ShadeFragment\" version=\"1.0\">
168 <description><![CDATA[Depth shader fragment]]></description>
170 <float name=\"depthValue\" />
171 <float3 name=\"color\" />
172 <float3 name=\"colorFar\" />
173 <float name=\"near\" />
174 <float name=\"far\" />
177 <float name=\"depthValue\" value=\"0.0\" />
178 <float3 name=\"color\" value=\"0.0,1.0,0.0\" />
179 <float3 name=\"colorFar\" value=\"0.0,0.0,1.0\" />
180 <float name=\"near\" value=\"0.0\" />
181 <float name=\"far\" value=\"2.0\" />
184 <float3 name=\"outColor\" />
187 <implementation render=\"OGSRenderer\" language=\"Cg\" lang_version=\"2.1\">
188 <function_name val=\"depthShaderPluginFragment\" />
190 float3 depthShaderPluginFragment(float depthValue, float3 cNear, float3 cFar, float nearClip, float farClip) \n
192 float ratio = (farClip + depthValue)/(farClip - nearClip); \n
193 return cNear*ratio + cFar*(1.0f - ratio); \n
197 <implementation render=\"OGSRenderer\" language=\"HLSL\" lang_version=\"11.0\">
198 <function_name val=\"depthShaderPluginFragment\" />
200 float3 depthShaderPluginFragment(float depthValue, float3 cNear, float3 cFar, float nearClip, float farClip) \n
202 float ratio = (farClip + depthValue)/(farClip - nearClip); \n
203 return cNear*ratio + cFar*(1.0f - ratio); \n
207 <implementation render=\"OGSRenderer\" language=\"GLSL\" lang_version=\"3.0\">
208 <function_name val=\"depthShaderPluginFragment\" />
210 vec3 depthShaderPluginFragment(float depthValue, vec3 cNear, vec3 cFar, float nearClip, float farClip) \n
212 float ratio = (farClip + depthValue)/(farClip - nearClip); \n
213 return cNear*ratio + cFar*(1.0f - ratio); \n
220 fragmentMgr.addShadeFragmentFromBuffer(fragmentBody.encode(
'utf-8'),
False)
222 if not fragmentMgr.hasFragment(
"depthShaderPluginInterpolantFragment"):
223 vertexFragmentBody = textwrap.dedent(
"""
224 <fragment uiName=\"depthShaderPluginInterpolantFragment\" name=\"depthShaderPluginInterpolantFragment\" type=\"interpolant\" class=\"ShadeFragment\" version=\"1.0\">
225 <description><![CDATA[Depth shader vertex fragment]]></description>
227 <float3 name=\"Pm\" semantic=\"Pm\" flags=\"varyingInputParam\" />
228 <float4x4 name=\"worldViewProj\" semantic=\"worldviewprojection\" />
233 <float name=\"outDepthValue\" ^1s/>
236 <implementation render=\"OGSRenderer\" language=\"Cg\" lang_version=\"2.1\">
237 <function_name val=\"depthShaderPluginInterpolantFragment\" />
239 float depthShaderPluginInterpolantFragment(float depthValue) \n
241 return depthValue; \n
244 <vertex_source><![CDATA[
245 float idepthShaderPluginInterpolantFragment(float3 Pm, float4x4 worldViewProj) \n
247 float4 pCamera = mul(worldViewProj, float4(Pm, 1.0f)); \n
248 return (pCamera.z - pCamera.w*2.0f); \n
252 <implementation render=\"OGSRenderer\" language=\"HLSL\" lang_version=\"11.0\">
253 <function_name val=\"depthShaderPluginInterpolantFragment\" />
255 float depthShaderPluginInterpolantFragment(float depthValue) \n
257 return depthValue; \n
260 <vertex_source><![CDATA[
261 float idepthShaderPluginInterpolantFragment(float3 Pm, float4x4 worldViewProj) \n
263 float4 pCamera = mul(float4(Pm, 1.0f), worldViewProj); \n
264 return (pCamera.z - pCamera.w*2.0f); \n
268 <implementation render=\"OGSRenderer\" language=\"GLSL\" lang_version=\"3.0\">
269 <function_name val=\"depthShaderPluginInterpolantFragment\" />
271 float depthShaderPluginInterpolantFragment(float depthValue) \n
273 return depthValue; \n
276 <vertex_source><![CDATA[
277 float idepthShaderPluginInterpolantFragment(vec3 Pm, mat4 worldViewProj) \n
279 vec4 pCamera = worldViewProj * vec4(Pm, 1.0f); \n
280 return (pCamera.z - pCamera.w*2.0f); \n
288 if omr.MRenderer.drawAPI() == omr.MRenderer.kDirectX11:
289 vertexFragmentBody = vertexFragmentBody.replace(
"^1s",
"semantic=\"extraDepth\" ")
291 vertexFragmentBody = vertexFragmentBody.replace(
"^1s",
" ")
293 fragmentMgr.addShadeFragmentFromBuffer(vertexFragmentBody.encode(
'utf-8'),
False)
295 if not fragmentMgr.hasFragment(
"depthShaderPluginGraph"):
296 fragmentGraphBody = textwrap.dedent(
"""
297 <fragment_graph name=\"depthShaderPluginGraph\" ref=\"depthShaderPluginGraph\" class=\"FragmentGraph\" version=\"1.0\">
299 <fragment_ref name=\"depthShaderPluginFragment\" ref=\"depthShaderPluginFragment\" />
300 <fragment_ref name=\"depthShaderPluginInterpolantFragment\" ref=\"depthShaderPluginInterpolantFragment\" />
303 <connect from=\"depthShaderPluginInterpolantFragment.outDepthValue\" to=\"depthShaderPluginFragment.depthValue\" />
306 <float3 name=\"Pm\" ref=\"depthShaderPluginInterpolantFragment.Pm\" semantic=\"Pm\" flags=\"varyingInputParam\" />
307 <float4x4 name=\"worldViewProj\" ref=\"depthShaderPluginInterpolantFragment.worldViewProj\" semantic=\"worldviewprojection\" />
308 <float3 name=\"color\" ref=\"depthShaderPluginFragment.color\" />
309 <float3 name=\"colorFar\" ref=\"depthShaderPluginFragment.colorFar\" />
310 <float name=\"near\" ref=\"depthShaderPluginFragment.near\" />
311 <float name=\"far\" ref=\"depthShaderPluginFragment.far\" />
314 <float3 name=\"color\" value=\"0.0,1.0,0.0\" />
315 <float3 name=\"colorFar\" value=\"0.0,0.0,1.0\" />
316 <float name=\"near\" value=\"0.0\" />
317 <float name=\"far\" value=\"2.0\" />
320 <float3 name=\"outColor\" ref=\"depthShaderPluginFragment.outColor\" />
322 </fragment_graph>""")
324 fragmentMgr.addFragmentGraphFromBuffer(fragmentGraphBody.encode(
'utf-8'))
326 def supportedDrawAPIs(self):
327 return omr.MRenderer.kOpenGL | omr.MRenderer.kOpenGLCoreProfile | omr.MRenderer.kDirectX11
329 def fragmentName(self):
330 return "depthShaderPluginGraph"
335 sRegistrantId =
"depthShaderPlugin_py"
337 def initializePlugin(obj):
338 plugin = om.MFnPlugin(obj,
"Autodesk",
"4.5",
"Any")
340 userClassify =
"shader/surface:drawdb/shader/surface/depthShader_py"
341 plugin.registerNode(
"depthShader_py", depthShader.id, depthShader.creator, depthShader.initialize, om.MPxNode.kDependNode, userClassify)
343 sys.stderr.write(
"Failed to register node\n")
348 omr.MDrawRegistry.registerSurfaceShadingNodeOverrideCreator(
"drawdb/shader/surface/depthShader_py", sRegistrantId, depthShaderOverride.creator)
350 sys.stderr.write(
"Failed to register override\n")
353 def uninitializePlugin(obj):
354 plugin = om.MFnPlugin(obj)
356 plugin.deregisterNode(depthShader.id)
358 sys.stderr.write(
"Failed to deregister node\n")
363 omr.MDrawRegistry.deregisterSurfaceShadingNodeOverrideCreator(
"drawdb/shader/surface/depthShader_py", sRegistrantId)
365 sys.stderr.write(
"Failed to deregister override\n")