Skip to content

Commit

Permalink
vp2RenderDelegate: Clarify _IsMaskedTransparency()
Browse files Browse the repository at this point in the history
  • Loading branch information
jufrantz committed Oct 18, 2024
1 parent be90b90 commit d0d2e9c
Showing 1 changed file with 24 additions and 31 deletions.
55 changes: 24 additions & 31 deletions lib/mayaUsd/render/vp2RenderDelegate/material.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1305,13 +1305,24 @@ bool _IsMaskedTransparency(const HdMaterialNetwork& network)
{
const HdMaterialNode& surfaceShader = network.nodes.back();

auto surfaceParamOr = [&surfaceShader](const TfToken& name, auto defVal) {
auto testParamValue = [&](const TfToken& name, auto&& predicate, auto rhsVal) {
using ValueT = std::decay_t<decltype(rhsVal)>;

const auto itr = surfaceShader.parameters.find(name);
return itr == surfaceShader.parameters.end() ? defVal : itr->second.GetWithDefault(defVal);
};
if (itr == surfaceShader.parameters.end() || !itr->second.IsHolding<ValueT>())
return false;

// Names of surfaceShader parameters affecting the mask interpretation.
TfSmallVector<TfToken, 2> maskPrms;
if (!predicate(itr->second.UncheckedGet<ValueT>(), rhsVal))
return false;

// Check if any connection to the param makes its value vary.
return std::none_of(
network.relationships.begin(),
network.relationships.end(),
[&surfaceShader, &name](const HdMaterialRelationship& rel) {
return (rel.outputId == surfaceShader.path) && (rel.outputName == name);
});
};

#ifdef WANT_MATERIALX_BUILD
const auto ndrNode = SdrRegistry::GetInstance().GetNodeByIdentifier(surfaceShader.identifier);
Expand All @@ -1320,37 +1331,19 @@ bool _IsMaskedTransparency(const HdMaterialNetwork& network)
if (ndrNode->GetSourceType() == HdVP2Tokens->mtlx) {
// Check UsdPreviewSurface node based on opacityThreshold.
if (ndrNode->GetFamily() == UsdImagingTokens->UsdPreviewSurface) {
if (surfaceParamOr(_tokens->opacityThreshold, 0.0f) > 0.0f) {
maskPrms.push_back(_tokens->opacityThreshold);
}
return testParamValue(_tokens->opacityThreshold, std::greater<>(), 0.0f);
}
// Check if glTF PBR's alpha_mode is `MASK` and that transmission is disabled.
else if (ndrNode->GetFamily() == _tokens->gltf_pbr) {
if (surfaceParamOr(_tokens->alpha_mode, 0) == 1
&& surfaceParamOr(_tokens->transmission, 0.0f) == 0.0f) {
maskPrms.assign({ _tokens->transmission, _tokens->alpha_mode });
}
}
} else
#endif
// Handle all glslfx surface nodes based on opacityThreshold.
if (surfaceParamOr(_tokens->opacityThreshold, 0.0f) > 0.0f) {
maskPrms.push_back(_tokens->opacityThreshold);
if (ndrNode->GetFamily() == _tokens->gltf_pbr) {
return testParamValue(_tokens->alpha_mode, std::equal_to<>(), 1)
&& testParamValue(_tokens->transmission, std::equal_to<>(), 0.0f);
}

// If no masking parameters were detected, the opacity is not used as a mask.
if (maskPrms.empty()) {
// Unhandled MaterialX terminal.
return false;
}

// Check if any input connection could make the surface non-constantly masked.
return std::none_of(
network.relationships.begin(),
network.relationships.end(),
[&surfaceShader, &maskPrms](const HdMaterialRelationship& rel) {
return (rel.outputId == surfaceShader.path)
&& (std::find(maskPrms.begin(), maskPrms.end(), rel.outputName) != maskPrms.end());
});
#endif
// Handle all glslfx surface nodes based on opacityThreshold.
return testParamValue(_tokens->opacityThreshold, std::greater<>(), 0.0f);
}

//! Return true if the surface shader needs to be rendered in a transparency pass.
Expand Down

0 comments on commit d0d2e9c

Please sign in to comment.