Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
28 changes: 28 additions & 0 deletions resources/Materials/TestSuite/stdlib/upgrade/syntax_1_38.mtlx
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,32 @@
<input name="surfaceshader" type="surfaceshader" nodename="N_surface_2" />
</surfacematerial>

<normal name="N_objectnormal" type="vector3">
<input name="space" type="string" value="object" />
</normal>
<multiply name="N_multiply" type="vector3">
<input name="in1" type="vector3" nodename="N_objectnormal" />
<input name="in2" type="float" value="0.5" />
</multiply>
<add name="N_add" type="vector3">
<input name="in1" type="vector3" nodename="N_multiply" />
<input name="in2" type="float" value="0.5" />
</add>
<normalmap name="N_normalmap_3" type="vector3">
<input name="in" type="vector3" nodename="N_add" />
<input name="space" type="string" value="object" />
</normalmap>
<transformnormal name="N_transformnormal" type="vector3">
<input name="in" type="vector3" nodename="N_normalmap_3" />
<input name="fromspace" type="string" value="object" />
<input name="tospace" type="string" value="world" />
</transformnormal>
<standard_surface name="N_surface_3" type="surfaceshader">
<input name="metalness" type="float" value="1" />
<input name="normal" type="vector3" nodename="N_transformnormal" />
</standard_surface>
<surfacematerial name="N_material_3" type="material">
<input name="surfaceshader" type="surfaceshader" nodename="N_surface_3" />
</surfacematerial>

</materialx>
59 changes: 41 additions & 18 deletions source/MaterialXCore/Version.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1316,30 +1316,53 @@ void Document::upgradeVersion()
}
else if (nodeCategory == "normalmap")
{
// Handle a rename of the float-typed nodedef.
if (node->getNodeDefString() == "ND_normalmap")
InputPtr space = node->getInput("space");
if (space && space->getValueString() == "object")
{
node->setNodeDefString("ND_normalmap_float");
// Replace object-space normalmap with a graph.
GraphElementPtr graph = node->getAncestorOfType<GraphElement>();
NodePtr multiply = graph->addNode("multiply", graph->createValidChildName("multiply"), "vector3");
copyInputWithBindings(node, "in", multiply, "in1");
multiply->setInputValue("in2", 2.0f);
NodePtr subtract = graph->addNode("subtract", graph->createValidChildName("subtract"), "vector3");
subtract->addInput("in1", "vector3")->setConnectedNode(multiply);
subtract->setInputValue("in2", 1.0f);
node->setCategory("normalize");
vector<InputPtr> origInputs = node->getInputs();
for (InputPtr input : origInputs)
{
node->removeChild(input->getName());
}
node->addInput("in", "vector3")->setConnectedNode(subtract);
}
else
{
// Clear tangent-space input.
node->removeInput("space");

node->removeInput("space");
// Handle a rename of the float-typed nodedef.
if (node->getNodeDefString() == "ND_normalmap")
{
node->setNodeDefString("ND_normalmap_float");
}

// 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");
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");
// 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");
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");

NodePtr normalizeNode = graph->addNode("normalize", graph->createValidChildName("normalmap_cross_norm"), "vector3");
normalizeNode->addInput("in", "vector3")->setConnectedNode(crossNode);
NodePtr normalizeNode = graph->addNode("normalize", graph->createValidChildName("normalmap_cross_norm"), "vector3");
normalizeNode->addInput("in", "vector3")->setConnectedNode(crossNode);

node->addInput("bitangent", "vector3")->setConnectedNode(normalizeNode);
node->addInput("bitangent", "vector3")->setConnectedNode(normalizeNode);
}
}
}
}
Expand Down