Skip to content

Commit

Permalink
Add TypedElement::isColorType method
Browse files Browse the repository at this point in the history
This changelist adds a TypedElement::isColorType helper method, allowing color logic to be written more clearly through the MaterialX API.
  • Loading branch information
jstone-lucasfilm committed Feb 23, 2024
1 parent 1d5ac95 commit 4cd0dcf
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 3 deletions.
6 changes: 6 additions & 0 deletions source/MaterialXCore/Element.h
Original file line number Diff line number Diff line change
Expand Up @@ -869,6 +869,12 @@ class MX_CORE_API TypedElement : public Element
return getAttribute(TYPE_ATTRIBUTE);
}

/// Return true if the element is of color type.
bool isColorType() const
{
return getType() == "color3" || getType() == "color4";
}

/// Return true if the element is of multi-output type.
bool isMultiOutputType() const
{
Expand Down
5 changes: 2 additions & 3 deletions source/MaterialXRender/TextureBaker.inl
Original file line number Diff line number Diff line change
Expand Up @@ -223,8 +223,7 @@ void TextureBaker<Renderer, ShaderGen>::bakeGraphOutput(OutputPtr output, GenCon
return;
}

bool encodeSrgb = _colorSpace == SRGB_TEXTURE &&
(output->getType() == "color3" || output->getType() == "color4");
bool encodeSrgb = _colorSpace == SRGB_TEXTURE && output->isColorType();
Renderer::getFramebuffer()->setEncodeSrgb(encodeSrgb);

ShaderPtr shader = _generator->generate("BakingShader", output, context);
Expand Down Expand Up @@ -412,7 +411,7 @@ DocumentPtr TextureBaker<Renderer, ShaderGen>::generateNewDocumentFromShader(Nod
Color4 uniformColor = _bakedConstantMap[output].color;
string uniformColorString = getValueStringFromColor(uniformColor, bakedInput->getType());
bakedInput->setValueString(uniformColorString);
if (bakedInput->getType() == "color3" || bakedInput->getType() == "color4")
if (bakedInput->isColorType())
{
bakedInput->setColorSpace(_colorSpace);
}
Expand Down
1 change: 1 addition & 0 deletions source/MaterialXTest/MaterialXCore/Document.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ TEST_CASE("Document", "[document]")
constant->setInputValue("value", mx::Color3(0.5f));
mx::OutputPtr output = nodeGraph->addOutput();
output->setConnectedNode(constant);
REQUIRE(output->isColorType());
REQUIRE(doc->validate());

// Create and test a type mismatch in a connection.
Expand Down
1 change: 1 addition & 0 deletions source/PyMaterialX/PyMaterialXCore/PyElement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ void bindPyElement(py::module& mod)
.def("setType", &mx::TypedElement::setType)
.def("hasType", &mx::TypedElement::hasType)
.def("getType", &mx::TypedElement::getType)
.def("isColorType", &mx::TypedElement::isColorType)
.def("isMultiOutputType", &mx::TypedElement::isMultiOutputType)
.def("getTypeDef", &mx::TypedElement::getTypeDef)
.def_readonly_static("TYPE_ATTRIBUTE", &mx::TypedElement::TYPE_ATTRIBUTE);
Expand Down

0 comments on commit 4cd0dcf

Please sign in to comment.