diff --git a/source/MaterialXCore/Version.cpp b/source/MaterialXCore/Version.cpp index 689b9cf720..880393071c 100644 --- a/source/MaterialXCore/Version.cpp +++ b/source/MaterialXCore/Version.cpp @@ -58,6 +58,33 @@ void copyInputWithBindings(NodePtr sourceNode, const string& sourceInputName, } } +InputPtr removeUnboundInput(NodePtr node, const string& inputName) +{ + InputPtr input = node->getInput(inputName); + if (input && !input->hasNodeName() && !input->hasNodeGraphString() && + !input->hasOutputString() && !input->hasInterfaceName()) + { + node->removeInput(inputName); + input = nullptr; + } + return input; +} + +void copyInputOrConnectWorldGeomProp(NodePtr sourceNode, const string& sourceInputName, + NodePtr destNode, const string& destInputName, + GraphElementPtr graph, const string& geomCategory) +{ + if (sourceNode->getInput(sourceInputName)) + { + copyInputWithBindings(sourceNode, sourceInputName, destNode, destInputName); + return; + } + + NodePtr geomNode = graph->addNode(geomCategory, graph->createValidChildName("normalmap_" + geomCategory), "vector3"); + geomNode->setInputValue("space", string("world")); + destNode->addInput(destInputName, "vector3")->setConnectedNode(geomNode); +} + } // anonymous namespace void Document::upgradeVersion() @@ -1405,17 +1432,20 @@ void Document::upgradeVersion() // Clear tangent-space input. node->removeInput("space"); - // If the normal or tangent inputs are set and the bitangent input is not, - // the bitangent should be set to normalize(cross(N, T)) - InputPtr normalInput = node->getInput("normal"); - InputPtr tangentInput = node->getInput("tangent"); - InputPtr bitangentInput = node->getInput("bitangent"); + // Normalmap frame inputs have 1.39 default geometric properties, so any + // unbound legacy literal should be ignored in favor of those geomprops. + InputPtr normalInput = removeUnboundInput(node, "normal"); + InputPtr tangentInput = removeUnboundInput(node, "tangent"); + InputPtr bitangentInput = removeUnboundInput(node, "bitangent"); + + // If the normal or tangent inputs are set and the bitangent input is not, + // the bitangent should be set to normalize(cross(N, T)). if ((normalInput || tangentInput) && !bitangentInput) { GraphElementPtr graph = node->getAncestorOfType(); NodePtr crossNode = graph->addNode("crossproduct", graph->createValidChildName("normalmap_cross"), "vector3"); - copyInputWithBindings(node, "normal", crossNode, "in1"); - copyInputWithBindings(node, "tangent", crossNode, "in2"); + copyInputOrConnectWorldGeomProp(node, "normal", crossNode, "in1", graph, "normal"); + copyInputOrConnectWorldGeomProp(node, "tangent", crossNode, "in2", graph, "tangent"); NodePtr normalizeNode = graph->addNode("normalize", graph->createValidChildName("normalmap_cross_norm"), "vector3"); normalizeNode->addInput("in", "vector3")->setConnectedNode(crossNode);