Implement type conversion for NodeItem #9

Merged
Bogdan Nagirniak merged 12 commits from BogdanNagirniak/blender:matx-nodeitem-type into matx-export-material 2023-09-05 12:03:24 +02:00
5 changed files with 232 additions and 159 deletions
Showing only changes of commit dc5b3c9dba - Show all commits

View File

@ -12,6 +12,49 @@ namespace blender::nodes::materialx {
NodeItem::NodeItem(MaterialX::GraphElement *graph) : graph_(graph) {}
NodeItem::Type NodeItem::type(const std::string &mx_type)
{
if (mx_type == "float") {
return Type::Float;
}
if (mx_type == "vector2") {
return Type::Vector2;
}
if (mx_type == "vector3") {
return Type::Vector3;
}
if (mx_type == "vector4") {
return Type::Vector4;
}
if (mx_type == "color3") {
return Type::Color3;
}
if (mx_type == "color4") {
return Type::Color4;
}
return Type::Other;
}
std::string NodeItem::type(Type mx_type)
{
switch (mx_type) {
case Type::Float:
return "float";
case Type::Vector2:
return "vector2";
case Type::Vector3:
return "vector3";
case Type::Vector4:
return "vector4";
case Type::Color3:
return "color3";
case Type::Color4:
return "color4";
default:
return "";
}
}
NodeItem NodeItem::empty() const
{
return NodeItem(graph_);
@ -135,31 +178,28 @@ bool NodeItem::operator==(const NodeItem &other) const
return false;
}
std::string mx_type;
Type mx_type;
auto val1 = value;
auto val2 = other.value;
if (!adjust_types(val1, val2, mx_type)) {
return false;
}
if (mx_type == "float") {
return val1->asA<float>() == val2->asA<float>();
switch (mx_type) {
case Type::Vector2:
return val1->asA<MaterialX::Vector2>() == val2->asA<MaterialX::Vector2>();
case Type::Vector3:
return val1->asA<MaterialX::Vector3>() == val2->asA<MaterialX::Vector3>();
case Type::Vector4:
return val1->asA<MaterialX::Vector4>() == val2->asA<MaterialX::Vector4>();
case Type::Float:
return val1->asA<float>() == val2->asA<float>();
case Type::Color3:
return val1->asA<MaterialX::Color3>() == val2->asA<MaterialX::Color3>();
case Type::Color4:
return val1->asA<MaterialX::Color4>() == val2->asA<MaterialX::Color4>();
default:
break;
}
if (mx_type == "color3") {
return val1->asA<MaterialX::Color3>() == val2->asA<MaterialX::Color3>();
}
if (mx_type == "color4") {
return val1->asA<MaterialX::Color4>() == val2->asA<MaterialX::Color4>();
}
if (mx_type == "vector2") {
return val1->asA<MaterialX::Vector2>() == val2->asA<MaterialX::Vector2>();
}
if (mx_type == "vector3") {
return val1->asA<MaterialX::Vector3>() == val2->asA<MaterialX::Vector3>();
}
if (mx_type == "vector4") {
return val1->asA<MaterialX::Vector4>() == val2->asA<MaterialX::Vector4>();
}
return false;
}
@ -197,33 +237,40 @@ NodeItem NodeItem::dotproduct(const NodeItem &other) const
{
NodeItem d = arithmetic(other, "dotproduct", [](float a, float b) { return a * b; });
if (d.value) {
std::string mx_type = d.type();
Type mx_type = d.type();
float f = 0.0f;
if (mx_type == "float") {
f = value->asA<float>();
}
else if (mx_type == "color3") {
auto v = value->asA<MaterialX::Color3>();
f = v[0] + v[1] + v[2];
}
else if (mx_type == "color4") {
auto v = value->asA<MaterialX::Color4>();
f = v[0] + v[1] + v[2] + v[3];
}
else if (mx_type == "vector2") {
auto v = value->asA<MaterialX::Vector2>();
f = v[0] + v[1];
}
else if (mx_type == "vector3") {
auto v = value->asA<MaterialX::Vector3>();
f = v[0] + v[1] + v[2];
}
else if (mx_type == "vector4") {
auto v = value->asA<MaterialX::Vector4>();
f = v[0] + v[1] + v[2] + v[3];
}
else {
BLI_assert_unreachable();
switch (mx_type) {
case Type::Float: {
f = value->asA<float>();
break;
}
case Type::Vector2: {
auto v = value->asA<MaterialX::Vector2>();
f = v[0] + v[1];
break;
}
case Type::Vector3: {
auto v = value->asA<MaterialX::Vector3>();
f = v[0] + v[1] + v[2];
break;
}
case Type::Vector4: {
auto v = value->asA<MaterialX::Vector4>();
f = v[0] + v[1] + v[2] + v[3];
break;
}
case Type::Color3: {
auto v = value->asA<MaterialX::Color3>();
f = v[0] + v[1] + v[2];
break;
}
case Type::Color4: {
auto v = value->asA<MaterialX::Color4>();
f = v[0] + v[1] + v[2] + v[3];
break;
}
default:
BLI_assert_unreachable();
}
d.value = MaterialX::Value::createValue(f);
}
@ -246,13 +293,13 @@ NodeItem NodeItem::if_else(const std::string &condition,
}
NodeItem res = empty();
if (type() != "float" || other.type() != "float") {
if (type() != Type::Float || other.type() != Type::Float) {
return res;
}
auto val1 = if_val;
auto val2 = else_val;
std::string mx_type;
Type mx_type;
if (!adjust_types(val1, val2, mx_type)) {
return res;
}
@ -279,7 +326,7 @@ NodeItem NodeItem::if_else(const std::string &condition,
res = func(value->asA<float>(), other.value->asA<float>()) ? val1 : val2;
}
else {
res.node = graph_->addNode(mx_category, MaterialX::EMPTY_STRING, mx_type);
res.node = graph_->addNode(mx_category, MaterialX::EMPTY_STRING, type(mx_type));
res.set_input("value1", *this);
res.set_input("value2", other);
res.set_input("in1", val1);
@ -376,37 +423,43 @@ NodeItem NodeItem::exp() const
NodeItem NodeItem::to_color3() const
{
std::string mx_type = type();
Type mx_type = type();
NodeItem res = empty();
if (value) {
MaterialX::Color3 c;
if (mx_type == "float") {
float v = value->asA<float>();
c = {v, v, v};
}
else if (mx_type == "color3") {
auto v = value->asA<MaterialX::Color3>();
c = {v[0], v[1], v[2]};
}
else if (mx_type == "color4") {
auto v = value->asA<MaterialX::Color4>();
c = {v[0], v[1], v[2]};
}
else if (mx_type == "vector3") {
auto v = value->asA<MaterialX::Vector3>();
c = {v[0], v[1], v[2]};
}
else if (mx_type == "vector4") {
auto v = value->asA<MaterialX::Vector4>();
c = {v[0], v[1], v[2]};
}
else {
return res;
switch (mx_type) {
case Type::Float: {
float v = value->asA<float>();
c = {v, v, v};
break;
}
case Type::Color3: {
BogdanNagirniak marked this conversation as resolved
Review

Should Integer be here?

Should `Integer` be here?
Review

Discussed: let it be here

Discussed: let it be here
auto v = value->asA<MaterialX::Color3>();
c = {v[0], v[1], v[2]};
break;
}
case Type::Color4: {
auto v = value->asA<MaterialX::Color4>();
c = {v[0], v[1], v[2]};
break;
}
case Type::Vector3: {
auto v = value->asA<MaterialX::Vector3>();
c = {v[0], v[1], v[2]};
break;
}
case Type::Vector4: {
auto v = value->asA<MaterialX::Vector4>();
c = {v[0], v[1], v[2]};
break;
}
default:
return res;
}
res.value = MaterialX::Value::createValue<MaterialX::Color3>(c);
}
else if (node) {
if (mx_type != "color3") {
if (mx_type !=Type::Color3) {
return res;
}
res.node = node;
@ -416,13 +469,18 @@ NodeItem NodeItem::to_color3() const
bool NodeItem::is_numeric() const
{
std::string t = type();
return ELEM(t, "float", "color3", "color4", "vector2", "vector3", "vector4");
return type() >= Type::Float;
}
std::string NodeItem::type() const
NodeItem::Type NodeItem::type() const
{
return value ? value->getTypeString() : node->getType();
if (value) {
return type(value->getTypeString());
}
if (node) {
return type(node->getType());
}
return Type::Empty;
}
NodeItem NodeItem::arithmetic(const std::string &mx_category,
@ -483,7 +541,7 @@ NodeItem NodeItem::arithmetic(const NodeItem &other,
return res;
}
std::string mx_type;
Type mx_type;
if (value && other.value) {
auto val1 = value;
auto val2 = other.value;
@ -491,43 +549,50 @@ NodeItem NodeItem::arithmetic(const NodeItem &other,
return res;
}
if (mx_type == "float") {
float v1 = val1->asA<float>();
float v2 = val2->asA<float>();
res.value = MaterialX::Value::createValue<float>(func(v1, v2));
}
else if (mx_type == "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 (mx_type == "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 (mx_type == "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 (mx_type == "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 (mx_type == "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();
switch (mx_type) {
case Type::Float: {
float v1 = val1->asA<float>();
float v2 = val2->asA<float>();
res.value = MaterialX::Value::createValue<float>(func(v1, v2));
break;
}
case Type::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])});
break;
}
case Type::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])});
break;
}
case Type::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])});
break;
}
case Type::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])});
break;
}
case Type::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])});
break;
}
default:
BLI_assert_unreachable();
}
}
else {
@ -537,50 +602,51 @@ NodeItem NodeItem::arithmetic(const NodeItem &other,
return res;
}
res.node = graph_->addNode(mx_category, MaterialX::EMPTY_STRING, mx_type);
res.node = graph_->addNode(mx_category, MaterialX::EMPTY_STRING, type(mx_type));
res.set_input("in1", val1);
res.set_input("in2", val2);
}
return res;
}
MaterialX::ValuePtr NodeItem::float_to_type(float v, std::string mx_type)
MaterialX::ValuePtr NodeItem::float_to_type(float v, Type mx_type)
{
if (mx_type == "float") {
return MaterialX::Value::createValue<float>(v);
MaterialX::ValuePtr res;
switch (mx_type) {
case Type::Float:
res = MaterialX::Value::createValue<float>(v);
break;
case Type::Vector2:
res = MaterialX::Value::createValue<MaterialX::Vector2>({v, v});
break;
case Type::Vector3:
res = MaterialX::Value::createValue<MaterialX::Vector3>({v, v, v});
break;
case Type::Vector4:
res = MaterialX::Value::createValue<MaterialX::Vector4>({v, v, v, v});
break;
case Type::Color3:
res = MaterialX::Value::createValue<MaterialX::Color3>({v, v, v});
break;
case Type::Color4:
res = MaterialX::Value::createValue<MaterialX::Color4>({v, v, v, v});
break;
default:
BLI_assert_unreachable();
}
if (mx_type == "color3") {
return MaterialX::Value::createValue<MaterialX::Color3>({v, v, v});
}
if (mx_type == "color4") {
return MaterialX::Value::createValue<MaterialX::Color4>({v, v, v, 1.0f});
}
if (mx_type == "vector2") {
return MaterialX::Value::createValue<MaterialX::Vector2>({v, v});
}
if (mx_type == "vector3") {
return MaterialX::Value::createValue<MaterialX::Vector3>({v, v, v});
}
if (mx_type == "vector4") {
return MaterialX::Value::createValue<MaterialX::Vector4>({v, v, v, 1.0f});
}
BLI_assert_unreachable();
return nullptr;
return res;
}
bool NodeItem::adjust_types(MaterialX::ValuePtr &val1,
MaterialX::ValuePtr &val2,
std::string &mx_type)
bool NodeItem::adjust_types(MaterialX::ValuePtr &val1, MaterialX::ValuePtr &val2, Type &mx_type)
{
std::string t1 = val1->getTypeString();
std::string t2 = val2->getTypeString();
Type t1 = type(val1->getTypeString());
Type t2 = type(val2->getTypeString());
if (t1 != t2) {
if (t1 == "float") {
if (t1 == Type::Float) {
val1 = float_to_type(val1->asA<float>(), t2);
mx_type = t2;
}
else if (t2 == "float") {
else if (t2 == Type::Float) {
val2 = float_to_type(val2->asA<float>(), t1);
mx_type = t1;
}
@ -594,16 +660,16 @@ bool NodeItem::adjust_types(MaterialX::ValuePtr &val1,
return true;
}
bool NodeItem::adjust_types(NodeItem &val1, NodeItem &val2, std::string &mx_type)
bool NodeItem::adjust_types(NodeItem &val1, NodeItem &val2, Type &mx_type)
{
std::string t1 = val1.type();
std::string t2 = val2.type();
Type t1 = val1.type();
Type t2 = val2.type();
if (t1 != t2) {
if (val1.value && t1 == "float") {
if (val1.value && t1 == Type::Float) {
val1.value = float_to_type(val1.value->asA<float>(), t2);
mx_type = t2;
}
else if (val2.value && t2 == "float") {
else if (val2.value && t2 == Type::Float) {
val2.value = float_to_type(val2.value->asA<float>(), t1);
mx_type = t1;
}

View File

@ -9,6 +9,9 @@
namespace blender::nodes::materialx {
class NodeItem {
public:
enum class Type { Empty = 0, Other, Float, Vector2, Vector3, Vector4, Color3, Color4 };
public:
MaterialX::ValuePtr value;
MaterialX::NodePtr node;
@ -20,6 +23,9 @@ class NodeItem {
NodeItem(MaterialX::GraphElement *graph);
~NodeItem() = default;
static Type type(const std::string &mx_type);
static std::string type(Type mx_type);
NodeItem empty() const;
template<class T> NodeItem val(const T &data) const;
@ -75,19 +81,17 @@ class NodeItem {
NodeItem to_color3() const;
bool is_numeric() const;
std::string type() const;
Type 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;
static MaterialX::ValuePtr float_to_type(float v, std::string mx_type);
static MaterialX::ValuePtr float_to_type(float v, Type mx_type);
/* Functions for adjusting values to make equal types */
static bool adjust_types(MaterialX::ValuePtr &val1,
MaterialX::ValuePtr &val2,
std::string &mx_type);
static bool adjust_types(NodeItem &val1, NodeItem &val2, std::string &mx_type);
static bool adjust_types(MaterialX::ValuePtr &val1, MaterialX::ValuePtr &val2, Type &mx_type);
static bool adjust_types(NodeItem &val1, NodeItem &val2, Type &mx_type);
};
template<class T> NodeItem NodeItem::val(const T &data) const

View File

@ -74,17 +74,20 @@ NodeItem NodeParser::get_input_default(const bNodeSocket &socket)
case SOCK_FLOAT: {
float v = socket.default_value_typed<bNodeSocketValueFloat>()->value;
res.value = MaterialX::Value::createValue<float>(v);
} break;
break;
}
case SOCK_VECTOR: {
const float *v = socket.default_value_typed<bNodeSocketValueVector>()->value;
res.value = MaterialX::Value::createValue<MaterialX::Vector3>(
MaterialX::Vector3(v[0], v[1], v[2]));
} break;
break;
}
case SOCK_RGBA: {
const float *v = socket.default_value_typed<bNodeSocketValueRGBA>()->value;
res.value = MaterialX::Value::createValue<MaterialX::Color4>(
MaterialX::Color4(v[0], v[1], v[2], v[3]));
} break;
break;
}
default: {
CLOG_WARN(LOG_MATERIALX_SHADER, "Unsupported socket type: %d", socket.type);
}

View File

@ -12,7 +12,7 @@ NodeItem TexCheckerNodeParser::compute()
NodeItem color1 = get_input_value("Color1");
NodeItem color2 = get_input_value("Color2");
if (scale.value && scale.type() == "float") {
if (scale.value && scale.type() == NodeItem::Type::Float) {
float v = scale.value->asA<float>();
scale = value(MaterialX::Vector2(v, v));
}

View File

@ -12,7 +12,7 @@ NodeItem TexNoiseNodeParser::compute()
NodeItem detail = get_input_value("Detail");
NodeItem lacunarity = get_input_value("Lacunarity");
if (detail.value && detail.type() == "float") {
if (detail.value && detail.type() == NodeItem::Type::Float) {
detail = value(int(detail.value->asA<float>()));
}