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

HYDRA-1267 - fix translation of coat parameters from standardsurface and openpbrsuface #195

Merged
merged 3 commits into from
Nov 4, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
160 changes: 147 additions & 13 deletions lib/mayaHydra/hydraExtensions/adapters/materialNetworkConverter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,74 @@ class MayaHydraUvAttrConverter : public MayaHydraMaterialAttrConverter
const VtValue _value;
}; // namespace

class MayaHydraMixedColorMaterialAttrConverter : public MayaHydraComputedMaterialAttrConverter
{
public:
MayaHydraMixedColorMaterialAttrConverter(
const TfToken& remappedBaseColorName,
const TfToken& remappedBaseWeightName,
const TfToken& remappedCoatColorName,
const TfToken& remappedCoatWeightName)
: _remappedBaseColorName(remappedBaseColorName)
, _remappedBaseWeightName(remappedBaseWeightName)
, _remappedCoatColorName(remappedCoatColorName)
, _remappedCoatWeightName(remappedCoatWeightName)
{
}

SdfValueTypeName GetType() override { return SdfValueTypeNames->Vector3f; }

VtValue GetValue(
MFnDependencyNode& node,
const TfToken& paramName,
const SdfValueTypeName& type,
const VtValue* fallback = nullptr,
MPlugArray* outPlug = nullptr) override
{
VtValue baseColor = MayaHydraMaterialNetworkConverter::ConvertMayaAttrToScaledValue(
node,
_remappedBaseColorName.GetText(),
_remappedBaseWeightName.GetText(),
SdfValueTypeNames->Vector3f,
fallback,
outPlug);

// Mix baseColor with coat coatColor
if (baseColor.IsHolding<GfVec3f>()) {
VtValue coatWeight = MayaHydraMaterialNetworkConverter::ConvertMayaAttrToValue(
node,
_remappedCoatWeightName.GetText(),
SdfValueTypeNames->Float,
fallback,
outPlug);
if (coatWeight.IsHolding<float>()) {
float coatWeightFloat = coatWeight.UncheckedGet<float>();
if (coatWeightFloat != 0.0f) {
VtValue coatColor = MayaHydraMaterialNetworkConverter::ConvertMayaAttrToValue(
node,
_remappedCoatColorName.GetText(),
SdfValueTypeNames->Vector3f,
fallback,
outPlug);
if (coatColor.IsHolding<GfVec3f>()) {
GfVec3f baseColorVec3f = baseColor.UncheckedGet<GfVec3f>();
GfVec3f coatColorVec3f = coatColor.UncheckedGet<GfVec3f>();
GfVec3f coatAttenuationVec3f = GfLerp(coatWeightFloat, GfVec3f(1.0f, 1.0f, 1.0f), coatColorVec3f);
return VtValue(GfCompMult(baseColorVec3f, coatAttenuationVec3f));
}
}
}
}

return baseColor;
}

protected:
const TfToken& _remappedBaseColorName;
const TfToken& _remappedBaseWeightName;
const TfToken& _remappedCoatColorName;
const TfToken& _remappedCoatWeightName;
};

class MayaHydraOpenPBREmissionColorMaterialAttrConverter : public MayaHydraComputedMaterialAttrConverter
Copy link
Collaborator Author

@lilike-adsk lilike-adsk Oct 31, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will be eventually replaced by MayaHydraMixedColorMaterialAttrConverter when Maya has emissionWeight exposed via another PR.

{
Expand Down Expand Up @@ -423,8 +491,71 @@ class MayaHydraOpenPBREmissionColorMaterialAttrConverter : public MayaHydraCompu
emissionWeightFloat = emissionLuminance.UncheckedGet<float>() / 1000.f; // Map Luminance(0.0-1000.0) to Weight(0-1.0)
}

return VtValue(emissionColorVec3f * emissionWeightFloat);
emissionColorVec3f *= emissionWeightFloat;

// Mix emissionColor with coat coatColor
VtValue coatWeight = MayaHydraMaterialNetworkConverter::ConvertMayaAttrToValue(
node, "coatWeight", SdfValueTypeNames->Float, fallback, outPlug);
if (coatWeight.IsHolding<float>()) {
float coatWeightFloat = coatWeight.UncheckedGet<float>();
if (coatWeightFloat != 0.0f) {
VtValue coatColor = MayaHydraMaterialNetworkConverter::ConvertMayaAttrToValue(
node,
"coatColor",
SdfValueTypeNames->Vector3f,
fallback,
outPlug);
if (coatColor.IsHolding<GfVec3f>()) {
GfVec3f coatColorVec3f = coatColor.UncheckedGet<GfVec3f>();
GfVec3f coatAttenuationVec3f = GfLerp(coatWeightFloat, GfVec3f(1.0f, 1.0f, 1.0f), coatColorVec3f);
return VtValue(GfCompMult(emissionColorVec3f, coatAttenuationVec3f));
}
}
}

return VtValue(emissionColorVec3f);
}
};

class MayaHydraClearCoatMaterialAttrConverter
: public MayaHydraComputedMaterialAttrConverter
{
public:
MayaHydraClearCoatMaterialAttrConverter(
const TfToken& remappedCoatColorName,
const TfToken& remappedCoatWeightName)
: _remappedCoatColorName(remappedCoatColorName)
, _remappedCoatWeightName(remappedCoatWeightName)
{
}

SdfValueTypeName GetType() override { return SdfValueTypeNames->Float; }

VtValue GetValue(
MFnDependencyNode& node,
const TfToken& paramName,
const SdfValueTypeName& type,
const VtValue* fallback = nullptr,
MPlugArray* outPlug = nullptr) override
{
VtValue coatColor = MayaHydraMaterialNetworkConverter::ConvertMayaAttrToScaledValue(
node,
_remappedCoatColorName.GetText(),
_remappedCoatWeightName.GetText(),
SdfValueTypeNames->Vector3f,
fallback,
outPlug);
if (coatColor.IsHolding<GfVec3f>()) {
GfVec3f coatColorVec3f = coatColor.UncheckedGet<GfVec3f>();
float clearCoatFolat = (coatColorVec3f[0] + coatColorVec3f[1] + coatColorVec3f[2]) / 3.0f;
return VtValue(clearCoatFolat);
}
return VtValue(0.0f);
}

protected:
const TfToken& _remappedCoatColorName;
const TfToken& _remappedCoatWeightName;
};

class MayaHydraCosinePowerMaterialAttrConverter : public MayaHydraComputedMaterialAttrConverter
Expand Down Expand Up @@ -638,14 +769,16 @@ void MayaHydraMaterialNetworkConverter::initialize()
auto uvConverter = std::make_shared<MayaHydraUvAttrConverter>();

// Standard surface:
auto baseColorConverter = std::make_shared<MayaHydraScaledRemappingMaterialAttrConverter>(
auto baseColorConverter = std::make_shared<MayaHydraMixedColorMaterialAttrConverter>(
MayaHydraAdapterTokens->baseColor,
MayaHydraAdapterTokens->base,
SdfValueTypeNames->Vector3f);
auto emissionColorConverter = std::make_shared<MayaHydraScaledRemappingMaterialAttrConverter>(
MayaHydraAdapterTokens->coatColor,
MayaHydraAdapterTokens->coat);
auto emissionColorConverter = std::make_shared<MayaHydraMixedColorMaterialAttrConverter>(
MayaHydraAdapterTokens->emissionColor,
MayaHydraAdapterTokens->emission,
SdfValueTypeNames->Vector3f);
MayaHydraAdapterTokens->coatColor,
MayaHydraAdapterTokens->coat);
auto specularColorConverter = std::make_shared<MayaHydraScaledRemappingMaterialAttrConverter>(
MayaHydraAdapterTokens->specularColor,
MayaHydraAdapterTokens->specular,
Expand All @@ -656,17 +789,18 @@ void MayaHydraMaterialNetworkConverter::initialize()
MayaHydraAdapterTokens->specularRoughness, SdfValueTypeNames->Float);
auto metallicConverter = std::make_shared<MayaHydraRemappingMaterialAttrConverter>(
MayaHydraAdapterTokens->metalness, SdfValueTypeNames->Float);
auto coatConverter = std::make_shared<MayaHydraRemappingMaterialAttrConverter>(
MayaHydraAdapterTokens->coat, SdfValueTypeNames->Float);
auto clearCoatConverter = std::make_shared<MayaHydraClearCoatMaterialAttrConverter>(
MayaHydraAdapterTokens->coatColor, MayaHydraAdapterTokens->coat);
auto coatRoughnessConverter = std::make_shared<MayaHydraRemappingMaterialAttrConverter>(
MayaHydraAdapterTokens->coatRoughness, SdfValueTypeNames->Float);
auto transmissionToOpacity = std::make_shared<MayaHydraStandardSurfaceTransmissionMaterialAttrConverter>();

// OpenPBR surface:
auto openPBRBaseColorConverter = std::make_shared<MayaHydraScaledRemappingMaterialAttrConverter>(
auto openPBRBaseColorConverter = std::make_shared<MayaHydraMixedColorMaterialAttrConverter>(
MayaHydraAdapterTokens->baseColor,
MayaHydraAdapterTokens->baseWeight,
SdfValueTypeNames->Vector3f);
MayaHydraAdapterTokens->coatColor,
MayaHydraAdapterTokens->coatWeight);
auto openPBREmissionColorConverter = std::make_shared<MayaHydraOpenPBREmissionColorMaterialAttrConverter>();
auto openPBRSpecularColorConverter
= std::make_shared<MayaHydraScaledRemappingMaterialAttrConverter>(
Expand All @@ -680,8 +814,8 @@ void MayaHydraMaterialNetworkConverter::initialize()
MayaHydraAdapterTokens->specularRoughness, SdfValueTypeNames->Float);
auto openPBRMetallicConverter = std::make_shared<MayaHydraRemappingMaterialAttrConverter>(
MayaHydraAdapterTokens->baseMetalness, SdfValueTypeNames->Float);
auto openPBRCoatConverter = std::make_shared<MayaHydraRemappingMaterialAttrConverter>(
MayaHydraAdapterTokens->coatWeight, SdfValueTypeNames->Float);
auto openPBRClearCoatConverter = std::make_shared<MayaHydraClearCoatMaterialAttrConverter>(
MayaHydraAdapterTokens->coatColor, MayaHydraAdapterTokens->coatWeight);
auto openPBRCoatRoughnessConverter = std::make_shared<MayaHydraRemappingMaterialAttrConverter>(
MayaHydraAdapterTokens->coatRoughness, SdfValueTypeNames->Float);
auto openPBRTransmissionToOpacity
Expand Down Expand Up @@ -754,7 +888,7 @@ void MayaHydraMaterialNetworkConverter::initialize()
{ MayaHydraAdapterTokens->specularColor, specularColorConverter },
{ MayaHydraAdapterTokens->ior, specularIORConverter },
{ MayaHydraAdapterTokens->roughness, specularRoughnessConverter },
{ MayaHydraAdapterTokens->clearcoat, coatConverter },
{ MayaHydraAdapterTokens->clearcoat, clearCoatConverter },
{ MayaHydraAdapterTokens->clearcoatRoughness, coatRoughnessConverter },
{ MayaHydraAdapterTokens->opacity, transmissionToOpacity },
{ MayaHydraAdapterTokens->metallic, metallicConverter },
Expand All @@ -769,7 +903,7 @@ void MayaHydraMaterialNetworkConverter::initialize()
{ MayaHydraAdapterTokens->specularColor, openPBRSpecularColorConverter },
{ MayaHydraAdapterTokens->ior, openPBRSpecularIORConverter },
{ MayaHydraAdapterTokens->roughness, openPBRSpecularRoughnessConverter },
{ MayaHydraAdapterTokens->clearcoat, openPBRCoatConverter },
{ MayaHydraAdapterTokens->clearcoat, openPBRClearCoatConverter },
{ MayaHydraAdapterTokens->clearcoatRoughness, openPBRCoatRoughnessConverter },
{ MayaHydraAdapterTokens->opacity, openPBRTransmissionToOpacity },
{ MayaHydraAdapterTokens->metallic, openPBRMetallicConverter },
Expand Down
1 change: 1 addition & 0 deletions lib/mayaHydra/hydraExtensions/adapters/tokens.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ PXR_NAMESPACE_OPEN_SCOPE
(specularIOR) \
(specularRoughness) \
(coat) \
(coatColor) \
(coatWeight) \
(coatRoughness) \
(transmission) \
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 11 additions & 1 deletion test/lib/mayaUsd/render/mayaToHydra/testOpenPBRSurface.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,19 @@ def test_OpenPBRSurface(self):
cmds.setAttr("openPBRSurface1.transmissionWeight", 0.5)
cmds.refresh()
self.assertSnapshotClose("transmissionWeight" + ".png", self.IMAGE_DIFF_FAIL_THRESHOLD, self.IMAGE_DIFF_FAIL_PERCENT)

#Verify Opacity
cmds.setAttr("openPBRSurface1.geometryOpacity", 0.2)
cmds.refresh()
self.assertSnapshotClose("geometryOpacity" + ".png", self.IMAGE_DIFF_FAIL_THRESHOLD, self.IMAGE_DIFF_FAIL_PERCENT)


#Verify Coat
cmds.setAttr("openPBRSurface1.coatWeight", 0.9)
cmds.refresh()
self.assertSnapshotClose("coatWeight" + ".png", self.IMAGE_DIFF_FAIL_THRESHOLD, self.IMAGE_DIFF_FAIL_PERCENT)
cmds.setAttr("openPBRSurface1.coatColor", 0.0,0.0,0.0, type = 'double3')
cmds.refresh()
self.assertSnapshotClose("coatColor" + ".png", self.IMAGE_DIFF_FAIL_THRESHOLD, self.IMAGE_DIFF_FAIL_PERCENT)

if __name__ == '__main__':
fixturesUtils.runTests(globals())
58 changes: 46 additions & 12 deletions test/lib/mayaUsd/render/mayaToHydra/testStandardSurface.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,19 @@
import fixturesUtils
import mtohUtils
import mayaUtils
import platform

class TestStandardSurface(mtohUtils.MayaHydraBaseTestCase): #Subclassing mtohUtils.MayaHydraBaseTestCase to be able to call self.assertSnapshotClose
# MayaHydraBaseTestCase.setUpClass requirement.
_file = __file__

IMAGE_DIFF_FAIL_THRESHOLD = 0.01
IMAGE_DIFF_FAIL_PERCENT = 0.2
@property
def IMAGE_DIFF_FAIL_PERCENT(self):
# Use a larger tolerance for transparency on OSX
if platform.system() == "Darwin":
return 2
return 0.2

#Test the translation from maya standard surface with a maya native plane to usd preview surface.
def test_StandardSurface(self):
Expand All @@ -40,35 +46,63 @@ def test_StandardSurface(self):
cmds.refresh()
self.assertSnapshotClose("default" + ".png", self.IMAGE_DIFF_FAIL_THRESHOLD, self.IMAGE_DIFF_FAIL_PERCENT)

#Verify Diffuse Roughness
cmds.setAttr("standardSurface1.diffuseRoughness", 0.5)
#Disconnect the texture
cmds.disconnectAttr("file1.outColor", "standardSurface1.baseColor")
cmds.refresh()
self.assertSnapshotClose("diffuseRoughness" + ".png", self.IMAGE_DIFF_FAIL_THRESHOLD, self.IMAGE_DIFF_FAIL_PERCENT)
self.assertSnapshotClose("default_noTexture" + ".png", self.IMAGE_DIFF_FAIL_THRESHOLD, self.IMAGE_DIFF_FAIL_PERCENT)

#Verify Base
cmds.setAttr("standardSurface1.baseColor", 0.5,0.0,0.0, type = 'double3')
cmds.refresh()
self.assertSnapshotClose("baseColor" + ".png", self.IMAGE_DIFF_FAIL_THRESHOLD, self.IMAGE_DIFF_FAIL_PERCENT)
cmds.setAttr("standardSurface1.base", 0.5)
cmds.refresh()
self.assertSnapshotClose("baseWeight" + ".png", self.IMAGE_DIFF_FAIL_THRESHOLD, self.IMAGE_DIFF_FAIL_PERCENT)

#Verify Metalness
cmds.setAttr("standardSurface1.metalness", 0.5)
cmds.refresh()
self.assertSnapshotClose("metalness" + ".png", self.IMAGE_DIFF_FAIL_THRESHOLD, self.IMAGE_DIFF_FAIL_PERCENT)

#Verify Specular Roughness
cmds.setAttr("standardSurface1.specularRoughness", 0.5)
#Verify Specular
cmds.setAttr("standardSurface1.specularColor", 0.0,0.0,0.5, type = 'double3')
cmds.refresh()
self.assertSnapshotClose("specularColor" + ".png", self.IMAGE_DIFF_FAIL_THRESHOLD, self.IMAGE_DIFF_FAIL_PERCENT)
cmds.setAttr("standardSurface1.specular", 0.2)
cmds.refresh()
self.assertSnapshotClose("specularWeight" + ".png", self.IMAGE_DIFF_FAIL_THRESHOLD, self.IMAGE_DIFF_FAIL_PERCENT)
cmds.setAttr("standardSurface1.specularRoughness", 0.7)
cmds.refresh()
self.assertSnapshotClose("specularRoughness" + ".png", self.IMAGE_DIFF_FAIL_THRESHOLD, self.IMAGE_DIFF_FAIL_PERCENT)

#Verify Specular IOR
cmds.setAttr("standardSurface1.specularIOR", 0.5)
cmds.refresh()
self.assertSnapshotClose("specularIOR" + ".png", self.IMAGE_DIFF_FAIL_THRESHOLD, self.IMAGE_DIFF_FAIL_PERCENT)

#Verify Emission
cmds.setAttr("standardSurface1.emission", 0.5)
cmds.refresh()
self.assertSnapshotClose("emissionWeight" + ".png", self.IMAGE_DIFF_FAIL_THRESHOLD, self.IMAGE_DIFF_FAIL_PERCENT)
cmds.setAttr("standardSurface1.emissionColor", 0.0,0.5,0.0, type = 'double3')
cmds.refresh()
self.assertSnapshotClose("emissionColor" + ".png", self.IMAGE_DIFF_FAIL_THRESHOLD, self.IMAGE_DIFF_FAIL_PERCENT)

#Verify Transmission
cmds.setAttr("standardSurface1.transmission", 0.5)
cmds.refresh()
self.assertSnapshotClose("transmission" + ".png", self.IMAGE_DIFF_FAIL_THRESHOLD, self.IMAGE_DIFF_FAIL_PERCENT)
self.assertSnapshotClose("transmissionWeight" + ".png", self.IMAGE_DIFF_FAIL_THRESHOLD, self.IMAGE_DIFF_FAIL_PERCENT)

#Verify Opacity
cmds.setAttr("standardSurface1.opacity", 0.2,0.2,0.2, type = 'double3')
cmds.refresh()
self.assertSnapshotClose("geometryOpacity" + ".png", self.IMAGE_DIFF_FAIL_THRESHOLD, self.IMAGE_DIFF_FAIL_PERCENT)

#Verify Emission
cmds.setAttr("standardSurface1.emission", 0.5)
#Verify Coat
cmds.setAttr("standardSurface1.coat", 0.9)
cmds.refresh()
self.assertSnapshotClose("coatWeight" + ".png", self.IMAGE_DIFF_FAIL_THRESHOLD, self.IMAGE_DIFF_FAIL_PERCENT)
cmds.setAttr("standardSurface1.coatColor", 0.0,0.0,0.0, type = 'double3')
cmds.refresh()
self.assertSnapshotClose("emission" + ".png", self.IMAGE_DIFF_FAIL_THRESHOLD, self.IMAGE_DIFF_FAIL_PERCENT)
self.assertSnapshotClose("coatColor" + ".png", self.IMAGE_DIFF_FAIL_THRESHOLD, self.IMAGE_DIFF_FAIL_PERCENT)

if __name__ == '__main__':
fixturesUtils.runTests(globals())
Loading