Skip to content

Commit b29a32d

Browse files
Handle object-space normalmap in version upgrade (#2251)
This changelist adds handling for object-space `normalmap` nodes in the version upgrade pathway from 1.38 to 1.39.
1 parent 4f7d041 commit b29a32d

2 files changed

Lines changed: 76 additions & 19 deletions

File tree

resources/Materials/TestSuite/stdlib/upgrade/syntax_1_38.mtlx

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,32 @@
3535
<input name="surfaceshader" type="surfaceshader" nodename="N_surface_2" />
3636
</surfacematerial>
3737

38+
<normal name="N_objectnormal" type="vector3">
39+
<input name="space" type="string" value="object" />
40+
</normal>
41+
<multiply name="N_multiply" type="vector3">
42+
<input name="in1" type="vector3" nodename="N_objectnormal" />
43+
<input name="in2" type="float" value="0.5" />
44+
</multiply>
45+
<add name="N_add" type="vector3">
46+
<input name="in1" type="vector3" nodename="N_multiply" />
47+
<input name="in2" type="float" value="0.5" />
48+
</add>
49+
<normalmap name="N_normalmap_3" type="vector3" nodedef="ND_normalmap">
50+
<input name="in" type="vector3" nodename="N_add" />
51+
<input name="space" type="string" value="object" />
52+
</normalmap>
53+
<transformnormal name="N_transformnormal" type="vector3">
54+
<input name="in" type="vector3" nodename="N_normalmap_3" />
55+
<input name="fromspace" type="string" value="object" />
56+
<input name="tospace" type="string" value="world" />
57+
</transformnormal>
58+
<standard_surface name="N_surface_3" type="surfaceshader">
59+
<input name="metalness" type="float" value="1" />
60+
<input name="normal" type="vector3" nodename="N_transformnormal" />
61+
</standard_surface>
62+
<surfacematerial name="N_material_3" type="material">
63+
<input name="surfaceshader" type="surfaceshader" nodename="N_surface_3" />
64+
</surfacematerial>
65+
3866
</materialx>

source/MaterialXCore/Version.cpp

Lines changed: 48 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1316,30 +1316,59 @@ void Document::upgradeVersion()
13161316
}
13171317
else if (nodeCategory == "normalmap")
13181318
{
1319-
// Handle a rename of the float-typed nodedef.
1320-
if (node->getNodeDefString() == "ND_normalmap")
1319+
InputPtr space = node->getInput("space");
1320+
if (space && space->getValueString() == "object")
13211321
{
1322-
node->setNodeDefString("ND_normalmap_float");
1323-
}
1324-
1325-
node->removeInput("space");
1322+
// Replace object-space normalmap with a graph.
1323+
GraphElementPtr graph = node->getAncestorOfType<GraphElement>();
1324+
NodePtr multiply = graph->addNode("multiply", graph->createValidChildName("multiply"), "vector3");
1325+
copyInputWithBindings(node, "in", multiply, "in1");
1326+
multiply->setInputValue("in2", 2.0f);
1327+
NodePtr subtract = graph->addNode("subtract", graph->createValidChildName("subtract"), "vector3");
1328+
subtract->addInput("in1", "vector3")->setConnectedNode(multiply);
1329+
subtract->setInputValue("in2", 1.0f);
1330+
node->setCategory("normalize");
1331+
vector<InputPtr> origInputs = node->getInputs();
1332+
for (InputPtr input : origInputs)
1333+
{
1334+
node->removeChild(input->getName());
1335+
}
1336+
node->addInput("in", "vector3")->setConnectedNode(subtract);
13261337

1327-
// If the normal or tangent inputs are set and the bitangent input is not,
1328-
// the bitangent should be set to normalize(cross(N, T))
1329-
InputPtr normalInput = node->getInput("normal");
1330-
InputPtr tangentInput = node->getInput("tangent");
1331-
InputPtr bitangentInput = node->getInput("bitangent");
1332-
if ((normalInput || tangentInput) && !bitangentInput)
1338+
// Update nodedef name if present.
1339+
if (node->hasNodeDefString())
1340+
{
1341+
node->setNodeDefString("ND_normalize_vector3");
1342+
}
1343+
}
1344+
else
13331345
{
1334-
GraphElementPtr graph = node->getAncestorOfType<GraphElement>();
1335-
NodePtr crossNode = graph->addNode("crossproduct", graph->createValidChildName("normalmap_cross"), "vector3");
1336-
copyInputWithBindings(node, "normal", crossNode, "in1");
1337-
copyInputWithBindings(node, "tangent", crossNode, "in2");
1346+
// Clear tangent-space input.
1347+
node->removeInput("space");
1348+
1349+
// If the normal or tangent inputs are set and the bitangent input is not,
1350+
// the bitangent should be set to normalize(cross(N, T))
1351+
InputPtr normalInput = node->getInput("normal");
1352+
InputPtr tangentInput = node->getInput("tangent");
1353+
InputPtr bitangentInput = node->getInput("bitangent");
1354+
if ((normalInput || tangentInput) && !bitangentInput)
1355+
{
1356+
GraphElementPtr graph = node->getAncestorOfType<GraphElement>();
1357+
NodePtr crossNode = graph->addNode("crossproduct", graph->createValidChildName("normalmap_cross"), "vector3");
1358+
copyInputWithBindings(node, "normal", crossNode, "in1");
1359+
copyInputWithBindings(node, "tangent", crossNode, "in2");
13381360

1339-
NodePtr normalizeNode = graph->addNode("normalize", graph->createValidChildName("normalmap_cross_norm"), "vector3");
1340-
normalizeNode->addInput("in", "vector3")->setConnectedNode(crossNode);
1361+
NodePtr normalizeNode = graph->addNode("normalize", graph->createValidChildName("normalmap_cross_norm"), "vector3");
1362+
normalizeNode->addInput("in", "vector3")->setConnectedNode(crossNode);
13411363

1342-
node->addInput("bitangent", "vector3")->setConnectedNode(normalizeNode);
1364+
node->addInput("bitangent", "vector3")->setConnectedNode(normalizeNode);
1365+
}
1366+
1367+
// Update nodedef name if present.
1368+
if (node->getNodeDefString() == "ND_normalmap")
1369+
{
1370+
node->setNodeDefString("ND_normalmap_float");
1371+
}
13431372
}
13441373
}
13451374
}

0 commit comments

Comments
 (0)