Export to MatX various Texture nodes #5

Merged
Bogdan Nagirniak merged 17 commits from matx-add-tex-nodes into matx-export-material 2023-08-31 19:49:26 +02:00
4 changed files with 98 additions and 0 deletions
Showing only changes of commit 5c3d7a17b6 - Show all commits

View File

@ -157,6 +157,7 @@ if(WITH_MATERIALX)
materialx/nodes/tex_image.cc materialx/nodes/tex_image.cc
materialx/nodes/tex_environment.cc materialx/nodes/tex_environment.cc
materialx/nodes/tex_noise.cc materialx/nodes/tex_noise.cc
materialx/nodes/tex_checker.cc
materialx/material.h materialx/material.h
materialx/nodes/node_parser.h materialx/nodes/node_parser.h

View File

@ -359,6 +359,13 @@ NodeItem NodeParser::get_input_default(const std::string &name)
res.value = MaterialX::Value::createValue<int>(v); res.value = MaterialX::Value::createValue<int>(v);
return res; return res;
} }
/* Explicit check for Scale socket of Checker Texture node since MaterialX has scales for X and Y axes
* and Blender only single. */
if (node->typeinfo->type == SH_NODE_TEX_CHECKER && STREQ(socket.name, "Scale")) {
float v = socket.default_value_typed<bNodeSocketValueFloat>()->value;
res.value = MaterialX::Value::createValue<MaterialX::Vector2>({v, v});
return res;
}
switch (socket.type) { switch (socket.type) {
case SOCK_FLOAT: { case SOCK_FLOAT: {
float v = socket.default_value_typed<bNodeSocketValueFloat>()->value; float v = socket.default_value_typed<bNodeSocketValueFloat>()->value;
@ -425,6 +432,9 @@ NodeItem NodeParser::get_input_link(const std::string &name)
case SH_NODE_TEX_NOISE: case SH_NODE_TEX_NOISE:
parser = std::make_unique<TexNoiseNodeParser>(graph, depsgraph, material, in_node); parser = std::make_unique<TexNoiseNodeParser>(graph, depsgraph, material, in_node);
break; break;
case SH_NODE_TEX_CHECKER:
parser = std::make_unique<TexCheckerNodeParser>(graph, depsgraph, material, in_node);
break;
default: default:
// TODO: warning log // TODO: warning log
return res; return res;

View File

@ -110,5 +110,6 @@ DECLARE_PARSER(OutputMaterialNodeParser)
DECLARE_PARSER(TexImageNodeParser) DECLARE_PARSER(TexImageNodeParser)
DECLARE_PARSER(TexEnvironmentNodeParser) DECLARE_PARSER(TexEnvironmentNodeParser)
DECLARE_PARSER(TexNoiseNodeParser) DECLARE_PARSER(TexNoiseNodeParser)
DECLARE_PARSER(TexCheckerNodeParser)
} // namespace blender::nodes::materialx } // namespace blender::nodes::materialx

View File

@ -0,0 +1,86 @@
/* SPDX-FileCopyrightText: 2011-2022 Blender Foundation
*
* SPDX-License-Identifier: GPL-2.0-or-later */
#include "node_parser.h"
#include "hydra/image.h"
#include "DEG_depsgraph_query.h"
#include <MaterialXFormat/XmlIo.h>
namespace blender::nodes::materialx {
/* For some reason validator complains about 'octaves' input of 'fractal3d' node (which doesn't even exist here)
* and 'in1' input of 'modulo' node, perhaps implementation is fully correct and similar to standart MaterialX
* Checkerboard node (added in 1.38.8). Also seems like USD due to lack of support doesn't render this material,
* but MaterialX Viewer and GraphEditor do. */
NodeItem TexCheckerNodeParser::compute()
{
Image *image = (Image *)node->id;
NodeTexChecker *tex = static_cast<NodeTexChecker *>(node->storage);
Scene *scene = DEG_get_input_scene(depsgraph);
Main *bmain = DEG_get_bmain(depsgraph);
NodeItem texcoord = create_node("texcoord", "vector2", true);
NodeItem frequency = create_node("constant", "vector2", true);
NodeItem scale = get_input_value("Scale");
/* Modifier to follow Cycles result. */
NodeItem scale_fitter(graph);
scale_fitter.value = MaterialX::Value::createValue<float>(4.0f);
frequency.set_input("value", scale * scale_fitter);
NodeItem mult = create_node("multiply", "vector2", true);
mult.set_input("in1", texcoord);
mult.set_input("in2", frequency);
NodeItem offset = create_node("constant", "vector2", true);
offset.set_input("value", MaterialX::Value::createValue<MaterialX::Vector2>({0.0f, 0.0f}));
NodeItem place2d = create_node("place2d", "vector2", true);
place2d.set_input("texcoord", mult);
place2d.set_input("offset", offset);
NodeItem separate = create_node("separate2", "multioutput", true);
separate.set_input("in", place2d);
separate.node->addOutput("outx", "float");
separate.node->addOutput("outy", "float");
NodeItem modulo_x = create_node("modulo", "float", true);
modulo_x.set_input("in1", separate);
modulo_x.set_input("in2", MaterialX::Value::createValue<float>(2.0f));
modulo_x.node->setConnectedOutput("in1", separate.node->getOutput("outx"));
NodeItem floor_x = create_node("floor", "float", true);
floor_x.set_input("in", modulo_x);
NodeItem modulo_y = create_node("modulo", "float", true);
modulo_y.set_input("in1", separate);
modulo_y.set_input("in2", MaterialX::Value::createValue<float>(2.0f));
modulo_y.node->setConnectedOutput("in1", separate.node->getOutput("outy"));
NodeItem floor_y = create_node("floor", "float", true);
floor_y.set_input("in", modulo_y);
NodeItem add = create_node("add", "float", true);
add.set_input("in1", floor_x);
add.set_input("in2", floor_y);
NodeItem ifequal = create_node("ifequal", "float", true);
ifequal.set_input("value1", add);
ifequal.set_input("value2", MaterialX::Value::createValue<float>(1.0f));
ifequal.set_input("in1", MaterialX::Value::createValue<float>(1.0f));
ifequal.set_input("in2", MaterialX::Value::createValue<float>(0.0f));
NodeItem mix = create_node("mix", "color3", true);
NodeItem color1 = get_input_value("Color1");
NodeItem color2 = get_input_value("Color2");
mix.set_input("fg", color1.to_color3());
mix.set_input("bg", color2.to_color3());
mix.set_input("mix", ifequal);
return mix;
}
} // namespace blender::nodes::materialx