MaterialX: add convert nodes #15

Merged
Bogdan Nagirniak merged 8 commits from matx-add-convert-nodes into matx-export-material 2023-09-08 16:55:00 +02:00
6 changed files with 67 additions and 2 deletions
Showing only changes of commit 7ebca40cfc - Show all commits

View File

@ -153,6 +153,7 @@ if(WITH_MATERIALX)
materialx/nodes/clamp.cc materialx/nodes/clamp.cc
materialx/nodes/huesatval.cc materialx/nodes/huesatval.cc
materialx/nodes/invert.cc materialx/nodes/invert.cc
materialx/nodes/map_range.cc
materialx/nodes/math.cc materialx/nodes/math.cc
materialx/nodes/mix_rgb.cc materialx/nodes/mix_rgb.cc
materialx/nodes/node_item.cc materialx/nodes/node_item.cc

View File

@ -0,0 +1,51 @@
/* SPDX-FileCopyrightText: 2011-2022 Blender Foundation
*
* SPDX-License-Identifier: GPL-2.0-or-later */
#include "node_parser.h"
namespace blender::nodes::materialx {
NodeItem MapRangeNodeParser::compute()
{
/* Interpolation isn't supported by MaterialX. */
const NodeMapRange *map_range = static_cast<NodeMapRange *>(node_->storage);
NodeItem::Type type;
NodeItem val = empty();
NodeItem from_min = empty();
NodeItem from_max = empty();
NodeItem to_min = empty();
NodeItem to_max = empty();
switch (map_range->data_type) {
case CD_PROP_FLOAT:
type = NodeItem::Type::Float;
val = get_input_value("Value", type);
from_min = get_input_value(1, type);
from_max = get_input_value(2, type);
to_min = get_input_value(3, type);
to_max = get_input_value(4, type);
break;
case CD_PROP_FLOAT3:
type = NodeItem::Type::Vector3;
val = get_input_value("Vector", type);
from_min = get_input_value(7, type);
from_max = get_input_value(8, type);
to_min = get_input_value(9, type);
to_max = get_input_value(10, type);
break;
default:
BLI_assert_unreachable();
}
NodeItem res = create_node("range", NodeItem::type(type));
res.set_input("in", val);
res.set_input("inlow", from_min);
res.set_input("inhigh", from_max);
res.set_input("outlow", to_min);
res.set_input("outhigh", to_max);
res.set_input("doclamp", value(bool(map_range->clamp)));
return res;
}
} // namespace blender::nodes::materialx

View File

@ -513,6 +513,9 @@ void NodeItem::set_input(const std::string &name,
case Type::String: case Type::String:
set_input(name, item.value->asA<std::string>(), mx_type); set_input(name, item.value->asA<std::string>(), mx_type);
break; break;
case Type::Bool:
set_input(name, item.value->asA<bool>(), mx_type);
break;
case Type::Integer: case Type::Integer:
set_input(name, item.value->asA<int>(), mx_type); set_input(name, item.value->asA<int>(), mx_type);
break; break;
@ -559,6 +562,9 @@ NodeItem::Type NodeItem::type(const std::string &type_str)
if (type_str == "string") { if (type_str == "string") {
return Type::String; return Type::String;
} }
if (type_str == "boolean") {
return Type::Bool;
}
if (type_str == "integer") { if (type_str == "integer") {
return Type::Integer; return Type::Integer;
} }
@ -588,6 +594,8 @@ std::string NodeItem::type(Type type)
switch (type) { switch (type) {
case Type::String: case Type::String:
return "string"; return "string";
case Type::Bool:
return "boolean";
case Type::Integer: case Type::Integer:
return "integer"; return "integer";
case Type::Float: case Type::Float:
@ -603,6 +611,7 @@ std::string NodeItem::type(Type type)
case Type::Color4: case Type::Color4:
return "color4"; return "color4";
default: default:
BLI_assert_unreachable();
break; break;
} }
return ""; return "";

View File

@ -14,6 +14,7 @@ class NodeItem {
Empty = 0, Empty = 0,
Other, /* For MaterialX types like: surfaceshader, bsdf, edf, ...*/ Other, /* For MaterialX types like: surfaceshader, bsdf, edf, ...*/
String, String,
Bool,
Integer, Integer,
Float, Float,
Vector2, Vector2,
BogdanNagirniak marked this conversation as resolved Outdated

Maybe rename to Boolean, as we use full names for other types?

Maybe rename to Boolean, as we use full names for other types?
@ -47,6 +48,9 @@ class NodeItem {
bool operator==(const NodeItem &other) const; bool operator==(const NodeItem &other) const;
bool operator!=(const NodeItem &other) const; bool operator!=(const NodeItem &other) const;
static Type type(const std::string &type_str);
static std::string type(Type type);
/* Math functions */ /* Math functions */
NodeItem abs() const; NodeItem abs() const;
NodeItem floor() const; NodeItem floor() const;
@ -92,8 +96,6 @@ class NodeItem {
void add_output(const std::string &in_name, Type out_type); void add_output(const std::string &in_name, Type out_type);
private: private:
static Type type(const std::string &type_str);
static std::string type(Type type);
static bool is_arithmetic(Type type); static bool is_arithmetic(Type type);
static Type adjust_types(NodeItem &item1, NodeItem &item2); static Type adjust_types(NodeItem &item1, NodeItem &item2);

View File

@ -140,6 +140,7 @@ NodeItem NodeParser::get_input_link(const bNodeSocket &socket)
CASE_NODE_TYPE(SH_NODE_COMBXYZ, CombineXYZNodeParser) CASE_NODE_TYPE(SH_NODE_COMBXYZ, CombineXYZNodeParser)
CASE_NODE_TYPE(SH_NODE_HUE_SAT, HueSatValNodeParser) CASE_NODE_TYPE(SH_NODE_HUE_SAT, HueSatValNodeParser)
CASE_NODE_TYPE(SH_NODE_INVERT, InvertNodeParser) CASE_NODE_TYPE(SH_NODE_INVERT, InvertNodeParser)
CASE_NODE_TYPE(SH_NODE_MAP_RANGE, MapRangeNodeParser)
CASE_NODE_TYPE(SH_NODE_MATH, MathNodeParser) CASE_NODE_TYPE(SH_NODE_MATH, MathNodeParser)
CASE_NODE_TYPE(SH_NODE_MIX_RGB_LEGACY, MixRGBNodeParser) CASE_NODE_TYPE(SH_NODE_MIX_RGB_LEGACY, MixRGBNodeParser)
CASE_NODE_TYPE(SH_NODE_NORMAL_MAP, NormalMapNodeParser) CASE_NODE_TYPE(SH_NODE_NORMAL_MAP, NormalMapNodeParser)

View File

@ -71,6 +71,7 @@ DECLARE_PARSER(CombineColorNodeParser)
DECLARE_PARSER(CombineXYZNodeParser) DECLARE_PARSER(CombineXYZNodeParser)
DECLARE_PARSER(HueSatValNodeParser) DECLARE_PARSER(HueSatValNodeParser)
DECLARE_PARSER(InvertNodeParser) DECLARE_PARSER(InvertNodeParser)
DECLARE_PARSER(MapRangeNodeParser)
DECLARE_PARSER(MathNodeParser) DECLARE_PARSER(MathNodeParser)
DECLARE_PARSER(MixRGBNodeParser) DECLARE_PARSER(MixRGBNodeParser)
DECLARE_PARSER(NormalMapNodeParser) DECLARE_PARSER(NormalMapNodeParser)