Viewport 2.0 API
Viewport 2.0 API
0 API Classes
Introduction
Maya 2012 introduces several new API classes for doing custom drawing in the new Viewport 2.0. These
classes give varying levels of control over both shaders and geometry.
Viewport 2.0 uses programmable shaders and retains geometry data on the graphics card wherever
possible. This is a much different model than the old viewport which was simple fixed function OpenGL.
The new classes allow you to access this model so that your plugin code can integrate better with
standard Maya objects and shaders. There is, however, still a way to do fixed function OpenGL through
the API if you require it.
This class provides very low-level access to the draw. You will have to set up all shaders and do correct
state management in order to get correct drawing and to avoid corrupting the draw of other objects. A
new utility class called MDrawContext has been added to assist with state management. An instance of
this object is always passed into the draw call of MPxGeometryDrawOverride.
For a higher level interface to the geometry look at the new MPxGeometryEvaluator class.
MPxGeometryEvaluator
MPxGeometryEvaluator is a high-level interface that allows the user to provide geometry buffers that
will be used by the Viewport 2.0 system to draw a specific DAG object. It must be registered against the
DAG object type just like MPxGeometryDrawOverride.
The functionality of an implementation of MPxGeometryEvaluator is invoked when an associated DAG
object changes. The implementation is expected to provide updated geometry buffers and indexing
information which will be packed by Viewport 2.0 and stored on the graphics card (if possible).
MPxGeometryEvaluator is only invoked when something needs updating and not on every frame.
MPxShadingEffectEvaluator
MPxShadingEffectEvaluator allows the user to create a custom evaluator for associating a "full shading
effect" with a shading node (custom or standard) in Maya. Its primary use is for associating hardware
effects with pre-existing plugin shaders.
A "full shading effect" defines the complete shading and lighting involved to render a given object. Input
resources for shading such as geometry, textures, and lights are defined and bound to the shading effect
via the evaluator as required. The evaluator is fully responsible for these tasks. As an example, for
hardware shading, this can be thought of as implementing a CgFx or HLSL effect file renderer which can
use the resources defined within a Maya scene.
Like MPxGeometryDrawOverride, this is a low-level class. Any object using an instance of the associated
shader to draw will trigger the MPxShadingEffectEvaluator draw callback at draw time. The
implementation is responsible for setting up all shading information. It can then either query the
geometry itself through the DrawContext and then do all the drawing, or it can make a call back into
Maya to allow Viewport 2.0 to handle the draw using the current state.
Like the other two new classes, MPxShadingEffectEvaluator must be registered with MDrawRegistry
using a classification string. This means it can be associated with either a plugin shader type or with an
existing Maya shader type. Viewport 2.0 shader classification strings must begin with “drawdb/shader/”
(for example, the standard lambert is “drawdb/shader/surface/lambert”).
MDrawRegistry
This is where implementations of the new proxy classes must be registered against a specific node type
in Maya. The same proxy may be registered against multiple node types, but each node type may only
have one associated proxy class. Additional registrations override existing registrations.
MDrawContext
All draw context information for use by classes that override the actual draw
(MPxGeometryDrawOverride and MPxShadingEffectEvaluator). This class can be used to get and set
draw state or acquire pointers to the device geometry.
MVertexFieldDescriptor
This class is used by MPxShadingEffectEvaluator to allow the user to describe fields required by custom
shaders. It is also used by MPxGeometryEvaluator to describe the fields that the evaluator must provide
in order to satisfy the shader assigned to the geometry. It can be thought of as an interface between
shaders and geometry.
MVertexIndexingDescriptor
Same as MVertexFieldDescriptor, but used for indexing information.
Devkit Samples
footPrintNode
The foot print node plugin is an example of using MPxLocator. It has been updated to provide a simple
Viewport 2.0 implementation as well using MPxGeometryDrawOverride. Not all draw modes are
supported, but it shows how MPxGeometryDrawOverride is meant to be used.
apiMeshShape
The apiMeshShape plugin is an example of how to implement a custom Maya shape using
MPxSurfaceShape. It has been updated to include how to use MPxGeometryEvaluator in order to
provide vertex data to Viewport 2.0 for drawing a custom Maya shape.
hwPhongShader
hwPhongShader is an example of how to implement a custom hardware shader using
MPxHwShaderNode. It has been updated to include how to use MPxShadingEffectEvaluator to
implement a complete custom shading effect for Viewport 2.0. In its primary mode it uses the simple
drawGeometry interface to allow Viewport 2.0 to handle geometry drawing after it sets up the shader.
However it also has an alternate implementation showing how to query the actual geometry data
through the MDrawContext in order to get more control of the draw.