Arithmetic support for NodeItem and node implementation with arithmetic #4

Merged
Bogdan Nagirniak merged 3 commits from BogdanNagirniak/blender:BLEN-512 into matx-export-material 2023-08-29 09:26:20 +02:00
10 changed files with 339 additions and 49 deletions

View File

@ -149,15 +149,15 @@ if(WITH_MATERIALX)
list(APPEND SRC list(APPEND SRC
materialx/material.cc materialx/material.cc
materialx/nodes/bsdf_principled.cc materialx/nodes/bsdf_principled.cc
materialx/nodes/invert.cc
materialx/nodes/math.cc
materialx/nodes/mix_rgb.cc
materialx/nodes/node_parser.cc materialx/nodes/node_parser.cc
materialx/nodes/output_material.cc materialx/nodes/output_material.cc
materialx/nodes/tex_image.cc materialx/nodes/tex_image.cc
materialx/material.h materialx/material.h
materialx/nodes/bsdf_principled.h
materialx/nodes/node_parser.h materialx/nodes/node_parser.h
materialx/nodes/output_material.h
materialx/nodes/tex_image.h
) )
endif() endif()

View File

@ -3,7 +3,7 @@
* SPDX-License-Identifier: GPL-2.0-or-later */ * SPDX-License-Identifier: GPL-2.0-or-later */
#include "material.h" #include "material.h"
#include "nodes/output_material.h" #include "nodes/node_parser.h"
#include <MaterialXCore/Node.h> #include <MaterialXCore/Node.h>
#include <MaterialXFormat/XmlIo.h> #include <MaterialXFormat/XmlIo.h>

View File

@ -2,9 +2,7 @@
* *
* SPDX-License-Identifier: GPL-2.0-or-later */ * SPDX-License-Identifier: GPL-2.0-or-later */
#include "bsdf_principled.h" #include "node_parser.h"
#include <BKE_node_runtime.hh>
namespace blender::nodes::materialx { namespace blender::nodes::materialx {

View File

@ -2,16 +2,15 @@
* *
* SPDX-License-Identifier: GPL-2.0-or-later */ * SPDX-License-Identifier: GPL-2.0-or-later */
#pragma once
#include "node_parser.h" #include "node_parser.h"
namespace blender::nodes::materialx { namespace blender::nodes::materialx {
class OutputMaterialNodeParser : public NodeParser { NodeItem InvertNodeParser::compute()
public: {
using NodeParser::NodeParser; NodeItem fac = get_input_value("Fac");
NodeItem compute() override; NodeItem color = get_input_value("Color");
}; return fac.blend(color, fac.val(1.0f) - color);
}
} // namespace blender::nodes::materialx } // namespace blender::nodes::materialx

View File

@ -2,16 +2,14 @@
* *
* SPDX-License-Identifier: GPL-2.0-or-later */ * SPDX-License-Identifier: GPL-2.0-or-later */
#pragma once
#include "node_parser.h" #include "node_parser.h"
namespace blender::nodes::materialx { namespace blender::nodes::materialx {
class TexImageNodeParser : public NodeParser { NodeItem MathNodeParser::compute()
public: {
using NodeParser::NodeParser; /* TODO: implement */
NodeItem compute() override; return empty_value();
}; }
} // namespace blender::nodes::materialx } // namespace blender::nodes::materialx

View File

@ -2,16 +2,14 @@
* *
* SPDX-License-Identifier: GPL-2.0-or-later */ * SPDX-License-Identifier: GPL-2.0-or-later */
#pragma once
#include "node_parser.h" #include "node_parser.h"
namespace blender::nodes::materialx { namespace blender::nodes::materialx {
class BSDFPrincipledNodeParser : public NodeParser { NodeItem MixRGBNodeParser::compute()
public: {
using NodeParser::NodeParser; /* TODO: implement */
NodeItem compute() override; return empty_value();
}; }
} // namespace blender::nodes::materialx } // namespace blender::nodes::materialx

View File

@ -4,15 +4,17 @@
#include "node_parser.h" #include "node_parser.h"
#include "bsdf_principled.h"
#include "tex_image.h"
#include <BKE_node_runtime.hh> #include <BKE_node_runtime.hh>
namespace blender::nodes::materialx { namespace blender::nodes::materialx {
NodeItem::NodeItem(MaterialX::GraphElement *graph) : graph_(graph) {} NodeItem::NodeItem(MaterialX::GraphElement *graph) : graph_(graph) {}
NodeItem NodeItem::empty() const
{
return NodeItem(graph_);
}
void NodeItem::set_input(const std::string &name, const NodeItem &item) void NodeItem::set_input(const std::string &name, const NodeItem &item)
{ {
if (item.value) { if (item.value) {
@ -25,20 +27,27 @@ void NodeItem::set_input(const std::string &name, const NodeItem &item)
void NodeItem::set_input(const std::string &name, const MaterialX::ValuePtr value) void NodeItem::set_input(const std::string &name, const MaterialX::ValuePtr value)
{ {
std::string mx_type = value->getTypeString();
if (value->isA<float>()) { if (value->isA<float>()) {
set_input(name, value->asA<float>(), "float"); set_input(name, value->asA<float>(), mx_type);
}
else if (value->isA<MaterialX::Vector2>()) {
set_input(name, value->asA<MaterialX::Vector2>(), mx_type);
} }
else if (value->isA<MaterialX::Vector3>()) { else if (value->isA<MaterialX::Vector3>()) {
set_input(name, value->asA<MaterialX::Vector3>(), "vector3"); set_input(name, value->asA<MaterialX::Vector3>(), mx_type);
} }
else if (value->isA<MaterialX::Vector4>()) { else if (value->isA<MaterialX::Vector4>()) {
set_input(name, value->asA<MaterialX::Vector4>(), "vector4"); set_input(name, value->asA<MaterialX::Vector4>(), mx_type);
} }
else if (value->isA<MaterialX::Color3>()) { else if (value->isA<MaterialX::Color3>()) {
set_input(name, value->asA<MaterialX::Color3>(), "color3"); set_input(name, value->asA<MaterialX::Color3>(), mx_type);
} }
else if (value->isA<MaterialX::Color4>()) { else if (value->isA<MaterialX::Color4>()) {
set_input(name, value->asA<MaterialX::Color4>(), "color4"); set_input(name, value->asA<MaterialX::Color4>(), mx_type);
}
else if (value->isA<std::string>()) {
set_input(name, value->asA<std::string>(), mx_type);
} }
else { else {
BLI_assert_unreachable(); BLI_assert_unreachable();
@ -55,29 +64,267 @@ NodeItem::operator bool() const
return value || node; return value || node;
} }
NodeItem NodeItem::to_color3() NodeItem NodeItem::operator+(const NodeItem &other) const
{ {
NodeItem res(graph_); return arithmetic(other, "add", [](float a, float b) { return a + b; });
}
NodeItem NodeItem::operator-(const NodeItem &other) const
{
return arithmetic(other, "subtract", [](float a, float b) { return a - b; });
}
NodeItem NodeItem::operator*(const NodeItem &other) const
{
return arithmetic(other, "multiply", [](float a, float b) { return a * b; });
}
NodeItem NodeItem::operator/(const NodeItem &other) const
{
return arithmetic(other, "divide", [](float a, float b) { return b == 0.0f ? 0.0f : a / b; });
}
bool NodeItem::operator==(const NodeItem &other) const
{
if (node && node == other.node) {
return true;
}
/* TODO: implement */
return false;
}
NodeItem NodeItem::min(const NodeItem &other) const
{
return arithmetic(other, "min", [](float a, float b) { return std::min(a, b); });
}
NodeItem NodeItem::max(const NodeItem &other) const
{
return arithmetic(other, "max", [](float a, float b) { return std::max(a, b); });
}
NodeItem NodeItem::blend(const NodeItem &a, const NodeItem &b) const
{
return (val(1.0f) - *this) * a + *this * b;
}
NodeItem NodeItem::to_color3() const
{
std::string t = type();
NodeItem res = empty();
if (value) { if (value) {
if (value->isA<float>()) { MaterialX::Color3 c;
if (t == "float") {
float v = value->asA<float>(); float v = value->asA<float>();
res.value = MaterialX::Value::createValue<MaterialX::Color3>(MaterialX::Color3(v, v, v)); c = {v, v, v};
} }
else if (value->isA<MaterialX::Color3>()) { else if (t == "color3") {
res.value = value; auto v = value->asA<MaterialX::Color3>();
c = {v[0], v[1], v[2]};
} }
else if (value->isA<MaterialX::Color4>()) { else if (t == "color4") {
auto c = value->asA<MaterialX::Color4>(); auto v = value->asA<MaterialX::Color4>();
res.value = MaterialX::Value::createValue<MaterialX::Color3>( c = {v[0], v[1], v[2]};
MaterialX::Color3(c[0], c[1], c[2]));
} }
else if (t == "vector3") {
auto v = value->asA<MaterialX::Vector3>();
c = {v[0], v[1], v[2]};
}
else if (t == "vector4") {
auto v = value->asA<MaterialX::Vector4>();
c = {v[0], v[1], v[2]};
}
else {
return res;
}
res.value = MaterialX::Value::createValue<MaterialX::Color3>(c);
} }
else if (node) { else if (node) {
if (t != "color3") {
return res;
}
res.node = node; res.node = node;
} }
return res; return res;
} }
bool NodeItem::is_numeric() const
{
std::string t = type();
return ELEM(t, "float", "color3", "color4", "vector2", "vector3", "vector4");
}
std::string NodeItem::type() const
{
return value ? value->getTypeString() : node->getType();
}
NodeItem NodeItem::arithmetic(const std::string &mx_category,
std::function<float(float)> func) const
{
if (!is_numeric()) {
return empty();
}
std::string t = value ? value->getTypeString() : node->getType();
NodeItem res(graph_);
if (value) {
if (t == "float") {
float v = value->asA<float>();
res.value = MaterialX::Value::createValue<float>(func(v));
}
else if (t == "color3") {
auto v = value->asA<MaterialX::Color3>();
res.value = MaterialX::Value::createValue<MaterialX::Color3>(
{func(v[0]), func(v[1]), func(v[2])});
}
else if (t == "color4") {
auto v = value->asA<MaterialX::Color4>();
res.value = MaterialX::Value::createValue<MaterialX::Color4>(
{func(v[0]), func(v[1]), func(v[2]), func(v[3])});
}
else if (t == "vector2") {
auto v = value->asA<MaterialX::Vector2>();
res.value = MaterialX::Value::createValue<MaterialX::Vector2>({func(v[0]), func(v[1])});
}
else if (t == "vector3") {
auto v = value->asA<MaterialX::Vector3>();
res.value = MaterialX::Value::createValue<MaterialX::Vector3>(
{func(v[0]), func(v[1]), func(v[2])});
}
else if (t == "vector4") {
auto v = value->asA<MaterialX::Vector4>();
res.value = MaterialX::Value::createValue<MaterialX::Vector4>(
{func(v[0]), func(v[1]), func(v[2]), func(v[3])});
}
else {
BLI_assert_unreachable();
}
}
else {
res.node = graph_->addNode(mx_category, MaterialX::EMPTY_STRING, t);
res.set_input("in", *this);
}
return res;
}
NodeItem NodeItem::arithmetic(const NodeItem &other,
const std::string &mx_category,
std::function<float(float, float)> func) const
{
NodeItem res = empty();
if (!is_numeric() || !other.is_numeric()) {
return res;
}
std::string t1 = type();
std::string t2 = other.type();
if (value && other.value) {
std::string t = t1;
auto val1 = value;
auto val2 = other.value;
if (t1 != t2) {
if (t1 == "float") {
val1 = float_to_type(val1->asA<float>(), t2);
t = t2;
}
else if (t2 == "float") {
val2 = float_to_type(val2->asA<float>(), t1);
}
else {
return res;
}
}
if (t == "float") {
float v1 = val1->asA<float>();
float v2 = val2->asA<float>();
res.value = MaterialX::Value::createValue<float>(func(v1, v2));
}
else if (t == "color3") {
auto v1 = val1->asA<MaterialX::Color3>();
auto v2 = val2->asA<MaterialX::Color3>();
res.value = MaterialX::Value::createValue<MaterialX::Color3>(
{func(v1[0], v2[0]), func(v1[1], v2[1]), func(v1[2], v2[2])});
}
else if (t == "color4") {
auto v1 = val1->asA<MaterialX::Color4>();
auto v2 = val2->asA<MaterialX::Color4>();
res.value = MaterialX::Value::createValue<MaterialX::Color4>(
{func(v1[0], v2[0]), func(v1[1], v2[1]), func(v1[2], v2[2]), func(v1[3], v2[3])});
}
else if (t == "vector2") {
auto v1 = val1->asA<MaterialX::Vector2>();
auto v2 = val2->asA<MaterialX::Vector2>();
res.value = MaterialX::Value::createValue<MaterialX::Vector2>(
{func(v1[0], v2[0]), func(v1[1], v2[1])});
}
else if (t == "vector3") {
auto v1 = val1->asA<MaterialX::Vector3>();
auto v2 = val2->asA<MaterialX::Vector3>();
res.value = MaterialX::Value::createValue<MaterialX::Vector3>(
{func(v1[0], v2[0]), func(v1[1], v2[1]), func(v1[2], v2[2])});
}
else if (t == "vector4") {
auto v1 = val1->asA<MaterialX::Vector4>();
auto v2 = val2->asA<MaterialX::Vector4>();
res.value = MaterialX::Value::createValue<MaterialX::Vector4>(
{func(v1[0], v2[0]), func(v1[1], v2[1]), func(v1[2], v2[2]), func(v1[3], v2[3])});
}
else {
BLI_assert_unreachable();
}
}
else {
std::string t = t1;
auto val1 = *this;
auto val2 = other;
if (t1 != t2) {
if (val1.value && t1 == "float") {
val1.value = float_to_type(val1.value->asA<float>(), t2);
t = t2;
}
else if (val2.value && t2 == "float") {
val2.value = float_to_type(val2.value->asA<float>(), t1);
}
else {
return res;
}
}
res.node = graph_->addNode(mx_category, MaterialX::EMPTY_STRING, t);
res.set_input("in1", val1);
res.set_input("in2", val2);
}
return res;
}
MaterialX::ValuePtr NodeItem::float_to_type(float v, std::string t) const
{
if (t == "float") {
return MaterialX::Value::createValue<float>(v);
}
if (t == "color3") {
return MaterialX::Value::createValue<MaterialX::Color3>({v, v, v});
}
if (t == "color4") {
return MaterialX::Value::createValue<MaterialX::Color4>({v, v, v, 1.0f});
}
if (t == "vector2") {
return MaterialX::Value::createValue<MaterialX::Vector2>({v, v});
}
if (t == "vector3") {
return MaterialX::Value::createValue<MaterialX::Vector3>({v, v, v});
}
if (t == "vector4") {
return MaterialX::Value::createValue<MaterialX::Vector4>({v, v, v, 1.0f});
}
BLI_assert_unreachable();
return nullptr;
}
NodeParser::NodeParser(MaterialX::GraphElement *graph, NodeParser::NodeParser(MaterialX::GraphElement *graph,
const Depsgraph *depsgraph, const Depsgraph *depsgraph,
const Material *material, const Material *material,
@ -151,6 +398,15 @@ NodeItem NodeParser::get_input_link(const std::string &name)
case SH_NODE_BSDF_PRINCIPLED: case SH_NODE_BSDF_PRINCIPLED:
parser = std::make_unique<BSDFPrincipledNodeParser>(graph, depsgraph, material, in_node); parser = std::make_unique<BSDFPrincipledNodeParser>(graph, depsgraph, material, in_node);
break; break;
case SH_NODE_INVERT:
parser = std::make_unique<InvertNodeParser>(graph, depsgraph, material, in_node);
break;
case SH_NODE_MATH:
parser = std::make_unique<MathNodeParser>(graph, depsgraph, material, in_node);
break;
case SH_NODE_MIX_RGB_LEGACY:
parser = std::make_unique<MixRGBNodeParser>(graph, depsgraph, material, in_node);
break;
case SH_NODE_TEX_IMAGE: case SH_NODE_TEX_IMAGE:
parser = std::make_unique<TexImageNodeParser>(graph, depsgraph, material, in_node); parser = std::make_unique<TexImageNodeParser>(graph, depsgraph, material, in_node);
break; break;

View File

@ -24,6 +24,9 @@ class NodeItem {
NodeItem(MaterialX::GraphElement *graph); NodeItem(MaterialX::GraphElement *graph);
~NodeItem() = default; ~NodeItem() = default;
NodeItem empty() const;
template<class T> NodeItem val(const T &data) const;
template<class T> template<class T>
void set_input(const std::string &name, const T &value, const std::string &mx_type); void set_input(const std::string &name, const T &value, const std::string &mx_type);
void set_input(const std::string &name, const NodeItem &item); void set_input(const std::string &name, const NodeItem &item);
@ -31,10 +34,35 @@ class NodeItem {
void set_input(const std::string &name, const MaterialX::NodePtr node); void set_input(const std::string &name, const MaterialX::NodePtr node);
operator bool() const; operator bool() const;
NodeItem operator+(const NodeItem &other) const;
NodeItem operator-(const NodeItem &other) const;
NodeItem operator*(const NodeItem &other) const;
NodeItem operator/(const NodeItem &other) const;
bool operator==(const NodeItem &other) const;
NodeItem to_color3(); NodeItem min(const NodeItem &other) const;
NodeItem max(const NodeItem &other) const;
NodeItem blend(const NodeItem &a, const NodeItem &b) const;
NodeItem to_color3() const;
bool is_numeric() const;
std::string type() const;
private:
NodeItem arithmetic(const std::string &mx_category, std::function<float(float)> func) const;
NodeItem arithmetic(const NodeItem &other,
const std::string &mx_category,
std::function<float(float, float)> func) const;
MaterialX::ValuePtr float_to_type(float v, std::string t) const;
}; };
template<class T> NodeItem NodeItem::val(const T &data) const
{
NodeItem res(graph_);
res.value = MaterialX::Value::createValue<T>(data);
return res;
}
template<class T> template<class T>
void NodeItem::set_input(const std::string &name, const T &value, const std::string &mx_type) void NodeItem::set_input(const std::string &name, const T &value, const std::string &mx_type)
{ {
@ -67,4 +95,18 @@ class NodeParser {
NodeItem empty_value(); NodeItem empty_value();
}; };
#define DECLARE_PARSER(T) \
class T : public NodeParser { \
public: \
using NodeParser::NodeParser; \
NodeItem compute() override; \
};
DECLARE_PARSER(BSDFPrincipledNodeParser)
DECLARE_PARSER(InvertNodeParser)
DECLARE_PARSER(MathNodeParser)
DECLARE_PARSER(MixRGBNodeParser)
DECLARE_PARSER(OutputMaterialNodeParser)
DECLARE_PARSER(TexImageNodeParser)
} // namespace blender::nodes::materialx } // namespace blender::nodes::materialx

View File

@ -2,7 +2,7 @@
* *
* SPDX-License-Identifier: GPL-2.0-or-later */ * SPDX-License-Identifier: GPL-2.0-or-later */
#include "output_material.h" #include "node_parser.h"
namespace blender::nodes::materialx { namespace blender::nodes::materialx {

View File

@ -2,7 +2,6 @@
* *
* SPDX-License-Identifier: GPL-2.0-or-later */ * SPDX-License-Identifier: GPL-2.0-or-later */
#include "tex_image.h"
#include "node_parser.h" #include "node_parser.h"
#include "hydra/image.h" #include "hydra/image.h"