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 70 additions and 33 deletions
Showing only changes of commit 10569ba111 - Show all commits

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.one() - color);
}
} // namespace blender::nodes::materialx } // namespace blender::nodes::materialx

View File

@ -2,16 +2,13 @@
* *
* 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; return empty_value();
NodeItem compute() override; }
};
} // namespace blender::nodes::materialx } // namespace blender::nodes::materialx

View File

@ -2,16 +2,13 @@
* *
* 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; return empty_value();
NodeItem compute() override; }
};
} // namespace blender::nodes::materialx } // namespace blender::nodes::materialx

View File

@ -4,9 +4,6 @@
#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 {
@ -82,6 +79,21 @@ NodeItem NodeItem::operator/(const NodeItem &other)
return arithmetic(other, "divide", [](float a, float b) { return b == 0.0f ? 0.0f : a / b; }); return arithmetic(other, "divide", [](float a, float b) { return b == 0.0f ? 0.0f : a / b; });
} }
NodeItem NodeItem::min(const NodeItem &other)
{
return arithmetic(other, "min", [](float a, float b) { return std::min(a, b); });
}
NodeItem NodeItem::max(const NodeItem &other)
{
return arithmetic(other, "max", [](float a, float b) { return std::max(a, b); });
}
NodeItem NodeItem::blend(const NodeItem &a, const NodeItem &b)
{
return *this * a + (one() - *this) * b;
}
NodeItem NodeItem::to_color3() const NodeItem NodeItem::to_color3() const
{ {
NodeItem res(graph_); NodeItem res(graph_);
@ -119,6 +131,13 @@ NodeItem NodeItem::to_color3() const
return res; return res;
} }
NodeItem NodeItem::one() const
{
NodeItem res(graph_);
res = 1.0f;
return res;
}
NodeItem NodeItem::arithmetic(const std::string &mx_category, std::function<float(float)> func) NodeItem NodeItem::arithmetic(const std::string &mx_category, std::function<float(float)> func)
{ {
std::string t = value ? value->getTypeString() : node->getType(); std::string t = value ? value->getTypeString() : node->getType();
@ -297,6 +316,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

@ -37,7 +37,12 @@ class NodeItem {
NodeItem operator*(const NodeItem &other); NodeItem operator*(const NodeItem &other);
NodeItem operator/(const NodeItem &other); NodeItem operator/(const NodeItem &other);
NodeItem min(const NodeItem &other);
NodeItem max(const NodeItem &other);
NodeItem blend(const NodeItem &a, const NodeItem &b);
NodeItem to_color3() const; NodeItem to_color3() const;
NodeItem one() const;
private: private:
NodeItem arithmetic(const std::string &mx_category, std::function<float(float)> func); NodeItem arithmetic(const std::string &mx_category, std::function<float(float)> func);
@ -85,4 +90,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"