diff --git a/source/MaterialXGenShader/ShaderGraph.cpp b/source/MaterialXGenShader/ShaderGraph.cpp index bcfe1ae441..d5c8bea521 100644 --- a/source/MaterialXGenShader/ShaderGraph.cpp +++ b/source/MaterialXGenShader/ShaderGraph.cpp @@ -569,6 +569,7 @@ ShaderGraphPtr ShaderGraph::create(const ShaderGraph* parent, const string& name } } + input->setBindInput(); const string path = nodeInput->getNamePath(); if (!path.empty()) { @@ -596,6 +597,9 @@ ShaderGraphPtr ShaderGraph::create(const ShaderGraph* parent, const string& name inputSocket->setMetadata(input->getMetadata()); } + // Apply color and unit transforms to each input. + graph->applyInputTransforms(node, newNode, context); + // Set root for upstream dependency traversal root = node; } @@ -616,6 +620,54 @@ ShaderGraphPtr ShaderGraph::create(const ShaderGraph* parent, const string& name return graph; } +void ShaderGraph::applyInputTransforms(ConstNodePtr node, ShaderNodePtr shaderNode, GenContext& context) +{ + ColorManagementSystemPtr colorManagementSystem = context.getShaderGenerator().getColorManagementSystem(); + UnitSystemPtr unitSystem = context.getShaderGenerator().getUnitSystem(); + + const string& targetColorSpace = context.getOptions().targetColorSpaceOverride.empty() ? + _document->getActiveColorSpace() : + context.getOptions().targetColorSpaceOverride; + const string& targetDistanceUnit = context.getOptions().targetDistanceUnit; + + for (InputPtr input : node->getInputs()) + { + if (input->hasValue() || input->hasInterfaceName()) + { + string sourceColorSpace = input->getActiveColorSpace(); + if (input->getType() == FILENAME_TYPE_STRING && node->isColorType()) + { + // Adjust the source color space for filename interface names. + if (input->hasInterfaceName()) + { + for (ConstNodePtr parentNode : context.getParentNodes()) + { + if (!parentNode->isColorType()) + { + InputPtr interfaceInput = parentNode->getInput(input->getInterfaceName()); + string interfaceColorSpace = interfaceInput ? interfaceInput->getActiveColorSpace() : EMPTY_STRING; + if (!interfaceColorSpace.empty()) + { + sourceColorSpace = interfaceColorSpace; + } + } + } + } + + ShaderOutput* shaderOutput = shaderNode->getOutput(); + populateColorTransformMap(colorManagementSystem, shaderOutput, sourceColorSpace, targetColorSpace, false); + populateUnitTransformMap(unitSystem, shaderOutput, input, targetDistanceUnit, false); + } + else + { + ShaderInput* shaderInput = shaderNode->getInput(input->getName()); + populateColorTransformMap(colorManagementSystem, shaderInput, sourceColorSpace, targetColorSpace, true); + populateUnitTransformMap(unitSystem, shaderInput, input, targetDistanceUnit, true); + } + } + } +} + ShaderNode* ShaderGraph::createNode(ConstNodePtr node, GenContext& context) { NodeDefPtr nodeDef = node->getNodeDef(); @@ -669,50 +721,8 @@ ShaderNode* ShaderGraph::createNode(ConstNodePtr node, GenContext& context) } } - // Handle colorspace and unit conversion if needed. - ColorManagementSystemPtr colorManagementSystem = context.getShaderGenerator().getColorManagementSystem(); - UnitSystemPtr unitSystem = context.getShaderGenerator().getUnitSystem(); - const string& targetColorSpace = context.getOptions().targetColorSpaceOverride.empty() ? - _document->getActiveColorSpace() : - context.getOptions().targetColorSpaceOverride; - const string& targetDistanceUnit = context.getOptions().targetDistanceUnit; - - for (InputPtr input : node->getInputs()) - { - if (input->hasValue() || input->hasInterfaceName()) - { - string sourceColorSpace = input->getActiveColorSpace(); - if (input->getType() == FILENAME_TYPE_STRING && node->isColorType()) - { - // Adjust the source color space for filename interface names. - if (input->hasInterfaceName()) - { - for (ConstNodePtr parentNode : context.getParentNodes()) - { - if (!parentNode->isColorType()) - { - InputPtr interfaceInput = parentNode->getInput(input->getInterfaceName()); - string interfaceColorSpace = interfaceInput ? interfaceInput->getActiveColorSpace() : EMPTY_STRING; - if (!interfaceColorSpace.empty()) - { - sourceColorSpace = interfaceColorSpace; - } - } - } - } - - ShaderOutput* shaderOutput = newNode->getOutput(); - populateColorTransformMap(colorManagementSystem, shaderOutput, sourceColorSpace, targetColorSpace, false); - populateUnitTransformMap(unitSystem, shaderOutput, input, targetDistanceUnit, false); - } - else - { - ShaderInput* shaderInput = newNode->getInput(input->getName()); - populateColorTransformMap(colorManagementSystem, shaderInput, sourceColorSpace, targetColorSpace, true); - populateUnitTransformMap(unitSystem, shaderInput, input, targetDistanceUnit, true); - } - } - } + // Apply color and unit transforms to each input. + applyInputTransforms(node, newNode, context); return newNode.get(); } diff --git a/source/MaterialXGenShader/ShaderGraph.h b/source/MaterialXGenShader/ShaderGraph.h index b8ac15fc46..a96f59edce 100644 --- a/source/MaterialXGenShader/ShaderGraph.h +++ b/source/MaterialXGenShader/ShaderGraph.h @@ -92,6 +92,9 @@ class MX_GENSHADER_API ShaderGraph : public ShaderNode const vector& getInputSockets() const { return _outputOrder; } const vector& getOutputSockets() const { return _inputOrder; } + /// Apply color and unit transforms to each input of a node. + void applyInputTransforms(ConstNodePtr node, ShaderNodePtr shaderNode, GenContext& context); + /// Create a new node in the graph ShaderNode* createNode(ConstNodePtr node, GenContext& context);