Setting up blending for transparency drawing in Viewport 2.0
 
 
 

In Viewport 2.0, blending is specified separately for RGB channels and alpha channels. This behavior is different from that of the default and high quality renderer, where the RGB and alpha share a single set of blending parameters.

When writing your plug-in, use the MHWRender::MBlendStateDesc blend state class to specify your blending.

Set your source and destination blend and alpha blend, and set the blend operation to add. The blend factor should be set to 1, 1, 1, 1.

The following example is taken from the hwPhongShader node plug-in that is provided in the Maya API Devkit.

MHWRender::MBlendStateDesc blendStateDesc;
for(int i = 0; i < (blendStateDesc.independentBlendEnable ? MHWRender::MBlendState::kMaxTargets : 1); ++i)
{
   blendStateDesc.targetBlends[i].blendEnable = true;
   blendStateDesc.targetBlends[i].sourceBlend = MHWRender::MBlendState::kSourceAlpha;
   blendStateDesc.targetBlends[i].destinationBlend = MHWRender::MBlendState::kInvSourceAlpha; 
   blendStateDesc.targetBlends[i].blendOperation = MHWRender::MBlendState::kAdd;
   blendStateDesc.targetBlends[i].alphaSourceBlend = MHWRender::MBlendState::kOne;
   blendStateDesc.targetBlends[i].alphaDestinationBlend = MHWRender::MBlendState::kInvSourceAlpha;
   blendStateDesc.targetBlends[i].alphaBlendOperation = MHWRender::MBlendState::kAdd;
}

blendStateDesc.blendFactor[0] = 1.0f;
blendStateDesc.blendFactor[1] = 1.0f;
blendStateDesc.blendFactor[2] = 1.0f;
blendStateDesc.blendFactor[3] = 1.0f; 

To set up the same state via the built-in cgfx plug-in example, specify the following pass state:

BlendFunc = int2( SrcAlpha, OneMinusSrcAlpha);
BlendFuncSeparate = int4( SrcAlpha, OneMinusSrcAlpha, One, OneMinusSrcAlpha);
BlendEquationSeparate = int2( Add, Add );
BlendColor = float4(1.0f,1.0f,1.0f,1.0f);

where:

in the MBlendStateDesc structure.

The default blend used for transparency drawing is { source alpha, 1-source alpha }, as shown in the examples.