Skip to content

Commit

Permalink
UsdPreviewSurface shading nodes with opacityThreshold are now VP2 ren…
Browse files Browse the repository at this point in the history
…dered as opaque.

As discussed in PR [#3952](#3952).
  • Loading branch information
jufrantz committed Oct 14, 2024
1 parent 1ca8787 commit 661ea9d
Showing 1 changed file with 36 additions and 22 deletions.
58 changes: 36 additions & 22 deletions lib/usd/pxrUsdPreviewSurface/usdPreviewSurface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,8 @@ MStatus PxrMayaUsdPreviewSurface::initialize()

status = attributeAffects(opacityAttr, outTransparencyOnAttr);
CHECK_MSTATUS_AND_RETURN_IT(status);
status = attributeAffects(opacityThresholdAttr, outTransparencyOnAttr);
CHECK_MSTATUS_AND_RETURN_IT(status);

return status;
}
Expand Down Expand Up @@ -461,32 +463,44 @@ MStatus PxrMayaUsdPreviewSurface::compute(const MPlug& plug, MDataBlock& dataBlo
// details. We don't use the user-visible "outTransparency" attribute for transparency test
// because its value depends on upstream nodes and thus error-prone when the "opacity" plug
// is connected to certain textures. In that case, we should enable transparency.
bool opacityConnected = false;

const MObject opacityAttr
= depNodeFn.attribute(PxrMayaUsdPreviewSurfaceTokens->OpacityAttrName.GetText());
const MPlug opacityPlug(thisMObject(), opacityAttr);
if (opacityPlug.isConnected()) {
const MPlug sourcePlug = opacityPlug.source(&status);
CHECK_MSTATUS(status);
const MObject sourceNode = sourcePlug.node(&status);
CHECK_MSTATUS(status);

// Anim curve output will be evaluated to determine if transparency should be enabled.
if (!sourceNode.hasFn(MFn::kAnimCurve)) {
opacityConnected = true;
}
}
MObject opacityThresholdAttr = depNodeFn.attribute(
PxrMayaUsdPreviewSurfaceTokens->OpacityThresholdAttrName.GetText());
const MDataHandle opacityThresholdData
= dataBlock.inputValue(opacityThresholdAttr, &status);
CHECK_MSTATUS(status);

float transparencyOn = false;
if (opacityConnected) {
transparencyOn = true;
} else {
const MDataHandle opacityData = dataBlock.inputValue(opacityAttr, &status);
CHECK_MSTATUS(status);
const float opacity = opacityData.asFloat();
if (opacity < 1.0f - std::numeric_limits<float>::epsilon()) {

// For masked transparency, we want VP2 to treat the shader as opaque.
if (opacityThresholdData.asFloat() <= 0.0f) {
bool opacityConnected = false;

const MObject opacityAttr
= depNodeFn.attribute(PxrMayaUsdPreviewSurfaceTokens->OpacityAttrName.GetText());
const MPlug opacityPlug(thisMObject(), opacityAttr);
if (opacityPlug.isConnected()) {
const MPlug sourcePlug = opacityPlug.source(&status);
CHECK_MSTATUS(status);
const MObject sourceNode = sourcePlug.node(&status);
CHECK_MSTATUS(status);

// Anim curve output will be evaluated to determine if transparency should be
// enabled.
if (!sourceNode.hasFn(MFn::kAnimCurve)) {
opacityConnected = true;
}
}

if (opacityConnected) {
transparencyOn = true;
} else {
const MDataHandle opacityData = dataBlock.inputValue(opacityAttr, &status);
CHECK_MSTATUS(status);
const float opacity = opacityData.asFloat();
if (opacity < 1.0f - std::numeric_limits<float>::epsilon()) {
transparencyOn = true;
}
}
}

Expand Down

0 comments on commit 661ea9d

Please sign in to comment.