Skip to content

Commit

Permalink
HYDRA-1226 - Support translation of openPBR (#193)
Browse files Browse the repository at this point in the history
* support translation of openPBR

* use emissionLuminance/1000.0f as emissionWeight

* remove unused token

* add a unit test

* update threshold for osx and test scene for 2025.3

* add missing platform lib

* set same specularDiffuse for consistent results on 2025 and 2026

* update based on review

* update based on review
  • Loading branch information
lilike-adsk authored Oct 30, 2024
1 parent d8c2707 commit 9e1ec41
Show file tree
Hide file tree
Showing 19 changed files with 483 additions and 6 deletions.
129 changes: 123 additions & 6 deletions lib/mayaHydra/hydraExtensions/adapters/materialNetworkConverter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,38 @@ class MayaHydraUvAttrConverter : public MayaHydraMaterialAttrConverter
const VtValue _value;
}; // namespace


class MayaHydraOpenPBREmissionColorMaterialAttrConverter : public MayaHydraComputedMaterialAttrConverter
{
public:
SdfValueTypeName GetType() override { return SdfValueTypeNames->Vector3f; }

VtValue GetValue(
MFnDependencyNode& node,
const TfToken& paramName,
const SdfValueTypeName& type,
const VtValue* fallback = nullptr,
MPlugArray* outPlug = nullptr) override
{
GfVec3f emissionColorVec3f(1.0f, 1.0f, 1.0f);
VtValue emissionColor = MayaHydraMaterialNetworkConverter::ConvertMayaAttrToValue(
node, "emissionColor", SdfValueTypeNames->Vector3f, fallback, outPlug);
if (emissionColor.IsHolding<GfVec3f>()) {
emissionColorVec3f = emissionColor.UncheckedGet<GfVec3f>();
}

// TODO: Use emissionWeight directly when it's available in Maya
float emissionWeightFloat = 0.0f;
VtValue emissionLuminance = MayaHydraMaterialNetworkConverter::ConvertMayaAttrToValue(
node, "emissionLuminance", SdfValueTypeNames->Float, fallback, outPlug);
if (emissionLuminance.IsHolding<float>()) {
emissionWeightFloat = emissionLuminance.UncheckedGet<float>() / 1000.f; // Map Luminance(0.0-1000.0) to Weight(0-1.0)
}

return VtValue(emissionColorVec3f * emissionWeightFloat);
}
};

class MayaHydraCosinePowerMaterialAttrConverter : public MayaHydraComputedMaterialAttrConverter
{
public:
Expand Down Expand Up @@ -428,7 +460,7 @@ class MayaHydraCosinePowerMaterialAttrConverter : public MayaHydraComputedMateri
}
};

class MayaHydraTransmissionMaterialAttrConverter : public MayaHydraComputedMaterialAttrConverter
class MayaHydraStandardSurfaceTransmissionMaterialAttrConverter : public MayaHydraComputedMaterialAttrConverter
{
public:
SdfValueTypeName GetType() override { return SdfValueTypeNames->Float; }
Expand All @@ -455,7 +487,7 @@ class MayaHydraTransmissionMaterialAttrConverter : public MayaHydraComputedMater
return *fallback;
}
TF_DEBUG(MAYAHYDRALIB_ADAPTER_GET)
.Msg("MayaHydraTransmissionMaterialAttrConverter::GetValue(): "
.Msg("MayaHydraStandardSurfaceTransmissionMaterialAttrConverter::GetValue(): "
"No float plug found with name: transmission and no "
"fallback given");
return VtValue();
Expand All @@ -469,16 +501,61 @@ class MayaHydraTransmissionMaterialAttrConverter : public MayaHydraComputedMater
val = 1.0e-4f;
}

float fGeometryOpacity = 1.0f;
if (geometryOpacityR.IsHolding<float>() && geometryOpacityG.IsHolding<float>()
&& geometryOpacityB.IsHolding<float>()) {
// Take the average as there is only 1 parameter in hydra
fGeometryOpacity = (1.0f / 3.0f)
float fGeometryOpacity = (1.0f / 3.0f)
* (geometryOpacityR.UncheckedGet<float>() + geometryOpacityG.UncheckedGet<float>()
+ geometryOpacityB.UncheckedGet<float>());

val *= fGeometryOpacity;
}

val *= fGeometryOpacity;
return VtValue(val);
}
};

class MayaHydraOpenPBRTransmissionMaterialAttrConverter : public MayaHydraComputedMaterialAttrConverter
{
public:
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 transmission = MayaHydraMaterialNetworkConverter::ConvertMayaAttrToValue(
node, "transmissionWeight", type, nullptr, outPlug);
// Combine transmission and Geometry Opacity
VtValue geometryOpacity = MayaHydraMaterialNetworkConverter::ConvertMayaAttrToValue(
node, "geometryOpacity", type, nullptr, outPlug);

if (!transmission.IsHolding<float>()) {
if (fallback) {
return *fallback;
}
TF_DEBUG(MAYAHYDRALIB_ADAPTER_GET)
.Msg("MayaHydraOpenPBRTransmissionMaterialAttrConverter::GetValue(): "
"No float plug found with name: transmission and no "
"fallback given");
return VtValue();
}

float val = 1.0f - transmission.UncheckedGet<float>();
if (val < 1.0e-4f) {
// Clamp lower value as an opacity of 0.0 in hydra makes the object fully transparent,
// but in VP2 we still see the specular highlight if any, avoiding 0.0 leads to the same
// effect in hydra.
val = 1.0e-4f;
}

if (geometryOpacity.IsHolding<float>()) {
float fGeometryOpacity = geometryOpacity.UncheckedGet<float>();
val *= fGeometryOpacity;
}

return VtValue(val);
}
Expand Down Expand Up @@ -583,7 +660,32 @@ void MayaHydraMaterialNetworkConverter::initialize()
MayaHydraAdapterTokens->coat, SdfValueTypeNames->Float);
auto coatRoughnessConverter = std::make_shared<MayaHydraRemappingMaterialAttrConverter>(
MayaHydraAdapterTokens->coatRoughness, SdfValueTypeNames->Float);
auto transmissionToOpacity = std::make_shared<MayaHydraTransmissionMaterialAttrConverter>();
auto transmissionToOpacity = std::make_shared<MayaHydraStandardSurfaceTransmissionMaterialAttrConverter>();

// OpenPBR surface:
auto openPBRBaseColorConverter = std::make_shared<MayaHydraScaledRemappingMaterialAttrConverter>(
MayaHydraAdapterTokens->baseColor,
MayaHydraAdapterTokens->baseWeight,
SdfValueTypeNames->Vector3f);
auto openPBREmissionColorConverter = std::make_shared<MayaHydraOpenPBREmissionColorMaterialAttrConverter>();
auto openPBRSpecularColorConverter
= std::make_shared<MayaHydraScaledRemappingMaterialAttrConverter>(
MayaHydraAdapterTokens->specularColor,
MayaHydraAdapterTokens->specularWeight,
SdfValueTypeNames->Vector3f);
auto openPBRSpecularIORConverter = std::make_shared<MayaHydraRemappingMaterialAttrConverter>(
MayaHydraAdapterTokens->specularIOR, SdfValueTypeNames->Float);
auto openPBRSpecularRoughnessConverter
= std::make_shared<MayaHydraRemappingMaterialAttrConverter>(
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 openPBRCoatRoughnessConverter = std::make_shared<MayaHydraRemappingMaterialAttrConverter>(
MayaHydraAdapterTokens->coatRoughness, SdfValueTypeNames->Float);
auto openPBRTransmissionToOpacity
= std::make_shared<MayaHydraOpenPBRTransmissionMaterialAttrConverter>();

auto fixedZeroFloat = std::make_shared<MayaHydraFixedMaterialAttrConverter>(0.0f);
auto fixedOneFloat = std::make_shared<MayaHydraFixedMaterialAttrConverter>(1.0f);
Expand Down Expand Up @@ -657,6 +759,21 @@ void MayaHydraMaterialNetworkConverter::initialize()
{ MayaHydraAdapterTokens->opacity, transmissionToOpacity },
{ MayaHydraAdapterTokens->metallic, metallicConverter },
} } },
{ MayaHydraAdapterTokens->openPBRSurface,
{ UsdImagingTokens->UsdPreviewSurface, // Maya OpenPBR surface shader translated to a
// UsdPreviewSurface with the following
// UsdPreviewSurface parameters mapped
{
{ MayaHydraAdapterTokens->diffuseColor, openPBRBaseColorConverter },
{ MayaHydraAdapterTokens->emissiveColor, openPBREmissionColorConverter },
{ MayaHydraAdapterTokens->specularColor, openPBRSpecularColorConverter },
{ MayaHydraAdapterTokens->ior, openPBRSpecularIORConverter },
{ MayaHydraAdapterTokens->roughness, openPBRSpecularRoughnessConverter },
{ MayaHydraAdapterTokens->clearcoat, openPBRCoatConverter },
{ MayaHydraAdapterTokens->clearcoatRoughness, openPBRCoatRoughnessConverter },
{ MayaHydraAdapterTokens->opacity, openPBRTransmissionToOpacity },
{ MayaHydraAdapterTokens->metallic, openPBRMetallicConverter },
} } },
{ MayaHydraAdapterTokens->file,
{ UsdImagingTokens->UsdUVTexture, // Maya file translated to a UsdUVTexture with the
// following UsdUVTexture parameters mapped
Expand Down
6 changes: 6 additions & 0 deletions lib/mayaHydra/hydraExtensions/adapters/tokens.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ PXR_NAMESPACE_OPEN_SCOPE
(clearcoatRoughness) \
(emissiveColor) \
(specular) \
(specularWeight) \
(specularColor) \
(metallic) \
(useSpecularWorkflow) \
Expand All @@ -45,19 +46,24 @@ PXR_NAMESPACE_OPEN_SCOPE
(diffuseColor) \
(displacement) \
(base) \
(baseWeight) \
(baseColor) \
(emission) \
(emissionWeight) \
(emissionColor) \
(metalness) \
(baseMetalness) \
(specularIOR) \
(specularRoughness) \
(coat) \
(coatWeight) \
(coatRoughness) \
(transmission) \
(lambert) \
(blinn) \
(phong) \
(standardSurface) \
(openPBRSurface) \
(file) \
(place2dTexture) \
(fileTextureName) \
Expand Down
1 change: 1 addition & 0 deletions test/lib/mayaUsd/render/mayaToHydra/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ set(INTERACTIVE_TEST_SCRIPT_FILES
testLookThrough.py
testObjectTemplate.py
testStandardSurface.py
testOpenPBRSurface.py
testFlowViewportAPI.py
testStageVariants.py|skipOnPlatform:osx # HYDRA-1127 : refinedWire not working on OSX
testStagePayloadsReferences.py
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.
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.
98 changes: 98 additions & 0 deletions test/lib/mayaUsd/render/mayaToHydra/testOpenPBRSurface.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
#
# Copyright 2024 Autodesk, Inc. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

import maya.cmds as cmds
import fixturesUtils
import mtohUtils
import mayaUtils
import platform

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

IMAGE_DIFF_FAIL_THRESHOLD = 0.01
@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 OpenPBR surface with a maya native plane to usd preview surface.
def test_OpenPBRSurface(self):
# Load a maya scene with a maya native plane, which has autodesk OpenPBR surface as material
testFile = mayaUtils.openTestScene(
"testOpenPBRSurface",
"testOpenPBRSurface.ma")
cmds.refresh()

#Verify Default display
panel = mayaUtils.activeModelPanel()
cmds.modelEditor(panel, edit=True, displayTextures=True)
cmds.refresh()
self.assertSnapshotClose("default" + ".png", self.IMAGE_DIFF_FAIL_THRESHOLD, self.IMAGE_DIFF_FAIL_PERCENT)

#Disconnect the texture
cmds.disconnectAttr("file1.outColor", "openPBRSurface1.baseColor")
cmds.refresh()
self.assertSnapshotClose("default_noTexture" + ".png", self.IMAGE_DIFF_FAIL_THRESHOLD, self.IMAGE_DIFF_FAIL_PERCENT)

#Verify Base
cmds.setAttr("openPBRSurface1.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("openPBRSurface1.baseWeight", 0.5)
cmds.refresh()
self.assertSnapshotClose("baseWeight" + ".png", self.IMAGE_DIFF_FAIL_THRESHOLD, self.IMAGE_DIFF_FAIL_PERCENT)

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

#Verify Specular
cmds.setAttr("openPBRSurface1.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("openPBRSurface1.specularWeight", 0.2)
cmds.refresh()
self.assertSnapshotClose("specularWeight" + ".png", self.IMAGE_DIFF_FAIL_THRESHOLD, self.IMAGE_DIFF_FAIL_PERCENT)
cmds.setAttr("openPBRSurface1.specularRoughness", 0.7)
cmds.refresh()
self.assertSnapshotClose("specularRoughness" + ".png", self.IMAGE_DIFF_FAIL_THRESHOLD, self.IMAGE_DIFF_FAIL_PERCENT)
cmds.setAttr("openPBRSurface1.specularIOR", 0.5)
cmds.refresh()
self.assertSnapshotClose("specularIOR" + ".png", self.IMAGE_DIFF_FAIL_THRESHOLD, self.IMAGE_DIFF_FAIL_PERCENT)

#Verify Emission
cmds.setAttr("openPBRSurface1.emissionLuminance", 500.0)
cmds.refresh()
self.assertSnapshotClose("emissionLuminance" + ".png", self.IMAGE_DIFF_FAIL_THRESHOLD, self.IMAGE_DIFF_FAIL_PERCENT)
cmds.setAttr("openPBRSurface1.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("openPBRSurface1.transmissionWeight", 0.5)
cmds.refresh()
self.assertSnapshotClose("transmissionWeight" + ".png", self.IMAGE_DIFF_FAIL_THRESHOLD, self.IMAGE_DIFF_FAIL_PERCENT)
cmds.setAttr("openPBRSurface1.geometryOpacity", 0.2)
cmds.refresh()
self.assertSnapshotClose("geometryOpacity" + ".png", self.IMAGE_DIFF_FAIL_THRESHOLD, self.IMAGE_DIFF_FAIL_PERCENT)

if __name__ == '__main__':
fixturesUtils.runTests(globals())
Binary file added test/testSamples/testOpenPBRSurface/diffuse.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 9e1ec41

Please sign in to comment.