Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix VP2 rendering of UsdPreviewSurface with opacityThreshold #3947

Merged
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);
jufrantz marked this conversation as resolved.
Show resolved Hide resolved

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
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
# limitations under the License.
#

import unittest

import fixturesUtils
import imageUtils

Expand Down Expand Up @@ -271,6 +273,7 @@ def testDoubleSided(self):
mayaUtils.setBasicCamera(3)
self.assertSnapshotClose('doubleSided_disabled_back.png')

@unittest.skipUnless(int(os.getenv("MAYA_LIGHTAPI_VERSION")) >= 2, 'UsdPreviewSurface fragment graph for light API v1 does not support opacityThreshold')
def testOpacityThreshold(self):
'''
Test UsdPreviewSurface transparency cut-out. The surface fragments should be
Expand Down
Loading