Skip to content
Open
Changes from all commits
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
44 changes: 37 additions & 7 deletions source/MaterialXCore/Version.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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<GraphElement>();
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);
Expand Down
Loading