Implement export of Math node. Continue other arithmetic support for NodeItem #6

Merged
Bogdan Nagirniak merged 9 commits from BogdanNagirniak/blender:matx-arithmetic into matx-export-material 2023-08-31 12:35:55 +02:00
2 changed files with 57 additions and 53 deletions
Showing only changes of commit 620fbc6af8 - Show all commits

View File

@ -29,25 +29,28 @@ void NodeItem::set_input(const std::string &name, const NodeItem &item)
void NodeItem::set_input(const std::string &name, const MaterialX::ValuePtr value)
{
std::string mx_type = value->getTypeString();
if (value->isA<float>()) {
if (mx_type == "float") {
set_input(name, value->asA<float>(), mx_type);
}
else if (value->isA<MaterialX::Vector2>()) {
else if (mx_type == "integer") {
set_input(name, value->asA<int>(), mx_type);
}
else if (mx_type == "vector2") {
set_input(name, value->asA<MaterialX::Vector2>(), mx_type);
}
else if (value->isA<MaterialX::Vector3>()) {
else if (mx_type == "vector3") {
set_input(name, value->asA<MaterialX::Vector3>(), mx_type);
}
else if (value->isA<MaterialX::Vector4>()) {
else if (mx_type == "vector4") {
set_input(name, value->asA<MaterialX::Vector4>(), mx_type);
}
else if (value->isA<MaterialX::Color3>()) {
else if (mx_type == "color3") {
set_input(name, value->asA<MaterialX::Color3>(), mx_type);
}
else if (value->isA<MaterialX::Color4>()) {
else if (mx_type == "color4") {
set_input(name, value->asA<MaterialX::Color4>(), mx_type);
}
else if (value->isA<std::string>()) {
else if (mx_type == "string") {
set_input(name, value->asA<std::string>(), mx_type);
}
else {
@ -116,28 +119,28 @@ bool NodeItem::operator==(const NodeItem &other) const
return false;
}
std::string t;
std::string mx_type;
auto val1 = value;
auto val2 = other.value;
if (!adjust_types(val1, val2, t)) {
if (!adjust_types(val1, val2, mx_type)) {
return false;
}
if (t == "float") {
if (mx_type == "float") {
return val1->asA<float>() == val2->asA<float>();
}
if (t == "color3") {
if (mx_type == "color3") {
return val1->asA<MaterialX::Color3>() == val2->asA<MaterialX::Color3>();
}
if (t == "color4") {
if (mx_type == "color4") {
return val1->asA<MaterialX::Color4>() == val2->asA<MaterialX::Color4>();
}
if (t == "vector2") {
if (mx_type == "vector2") {
return val1->asA<MaterialX::Vector2>() == val2->asA<MaterialX::Vector2>();
}
if (t == "vector3") {
if (mx_type == "vector3") {
return val1->asA<MaterialX::Vector3>() == val2->asA<MaterialX::Vector3>();
}
if (t == "vector4") {
if (mx_type == "vector4") {
return val1->asA<MaterialX::Vector4>() == val2->asA<MaterialX::Vector4>();
}
@ -178,28 +181,28 @@ NodeItem NodeItem::dotproduct(const NodeItem &other) const
{
NodeItem d = arithmetic(other, "dotproduct", [](float a, float b) { return a * b; });
if (d.value) {
std::string t = d.type();
std::string mx_type = d.type();
float f = 0.0f;
if (t == "float") {
if (mx_type == "float") {
f = value->asA<float>();
}
else if (t == "color3") {
else if (mx_type == "color3") {
auto v = value->asA<MaterialX::Color3>();
f = v[0] + v[1] + v[2];
}
else if (t == "color4") {
else if (mx_type == "color4") {
auto v = value->asA<MaterialX::Color4>();
f = v[0] + v[1] + v[2] + v[3];
}
else if (t == "vector2") {
else if (mx_type == "vector2") {
auto v = value->asA<MaterialX::Vector2>();
f = v[0] + v[1];
}
else if (t == "vector3") {
else if (mx_type == "vector3") {
auto v = value->asA<MaterialX::Vector3>();
f = v[0] + v[1] + v[2];
}
else if (t == "vector4") {
else if (mx_type == "vector4") {
auto v = value->asA<MaterialX::Vector4>();
f = v[0] + v[1] + v[2] + v[3];
}
@ -230,8 +233,8 @@ NodeItem NodeItem::if_else(const std::string &condition,
auto val1 = if_val;
auto val2 = else_val;
std::string t;
if (!adjust_types(val1, val2, t)) {
std::string mx_type;
if (!adjust_types(val1, val2, mx_type)) {
return res;
}
@ -257,7 +260,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, t);
res.node = graph_->addNode(mx_category, MaterialX::EMPTY_STRING, mx_type);
res.set_input("value1", *this);
res.set_input("value2", other);
res.set_input("in1", val1);
@ -461,44 +464,44 @@ NodeItem NodeItem::arithmetic(const NodeItem &other,
return res;
}
std::string t;
std::string mx_type;
if (value && other.value) {
auto val1 = value;
auto val2 = other.value;
if (!adjust_types(val1, val2, t)) {
if (!adjust_types(val1, val2, mx_type)) {
return res;
}
if (t == "float") {
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 (t == "color3") {
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 (t == "color4") {
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 (t == "vector2") {
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 (t == "vector3") {
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 (t == "vector4") {
else if (mx_type == "vector4") {
auto v1 = val1->asA<MaterialX::Vector4>();
auto v2 = val2->asA<MaterialX::Vector4>();
res.value = MaterialX::Value::createValue<MaterialX::Vector4>(
@ -511,35 +514,35 @@ NodeItem NodeItem::arithmetic(const NodeItem &other,
else {
auto val1 = *this;
auto val2 = other;
if (!adjust_types(val1, val2, t)) {
if (!adjust_types(val1, val2, mx_type)) {
return res;
}
res.node = graph_->addNode(mx_category, MaterialX::EMPTY_STRING, t);
res.node = graph_->addNode(mx_category, MaterialX::EMPTY_STRING, mx_type);
res.set_input("in1", val1);
res.set_input("in2", val2);
}
return res;
}
MaterialX::ValuePtr NodeItem::float_to_type(float v, std::string t)
MaterialX::ValuePtr NodeItem::float_to_type(float v, std::string mx_type)
{
if (t == "float") {
if (mx_type == "float") {
return MaterialX::Value::createValue<float>(v);
}
if (t == "color3") {
if (mx_type == "color3") {
return MaterialX::Value::createValue<MaterialX::Color3>({v, v, v});
}
if (t == "color4") {
if (mx_type == "color4") {
return MaterialX::Value::createValue<MaterialX::Color4>({v, v, v, 1.0f});
}
if (t == "vector2") {
if (mx_type == "vector2") {
return MaterialX::Value::createValue<MaterialX::Vector2>({v, v});
}
if (t == "vector3") {
if (mx_type == "vector3") {
return MaterialX::Value::createValue<MaterialX::Vector3>({v, v, v});
}
if (t == "vector4") {
if (mx_type == "vector4") {
return MaterialX::Value::createValue<MaterialX::Vector4>({v, v, v, 1.0f});
}
@ -547,48 +550,48 @@ MaterialX::ValuePtr NodeItem::float_to_type(float v, std::string t)
return nullptr;
}
bool NodeItem::adjust_types(MaterialX::ValuePtr &val1, MaterialX::ValuePtr &val2, std::string &t)
bool NodeItem::adjust_types(MaterialX::ValuePtr &val1, MaterialX::ValuePtr &val2, std::string &mx_type)
{
std::string t1 = val1->getTypeString();
std::string t2 = val2->getTypeString();
if (t1 != t2) {
if (t1 == "float") {
val1 = float_to_type(val1->asA<float>(), t2);
t = t2;
mx_type = t2;
}
else if (t2 == "float") {
val2 = float_to_type(val2->asA<float>(), t1);
t = t1;
mx_type = t1;
}
else {
return false;
}
}
else {
t = t1;
mx_type = t1;
}
return true;
}
bool NodeItem::adjust_types(NodeItem &val1, NodeItem &val2, std::string &t)
bool NodeItem::adjust_types(NodeItem &val1, NodeItem &val2, std::string &mx_type)
{
std::string t1 = val1.type();
std::string t2 = val2.type();
if (t1 != t2) {
if (val1.value && t1 == "float") {
val1.value = float_to_type(val1.value->asA<float>(), t2);
t = t2;
mx_type = t2;
}
else if (val2.value && t2 == "float") {
val2.value = float_to_type(val2.value->asA<float>(), t1);
t = t2;
mx_type = t1;
}
else {
return false;
}
}
else {
t = t1;
mx_type = t1;
}
return true;
}

View File

@ -77,9 +77,10 @@ class NodeItem {
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 t);
static bool adjust_types(MaterialX::ValuePtr &val1, MaterialX::ValuePtr &val2, std::string &t);
static bool adjust_types(NodeItem &val1, NodeItem &val2, std::string &t);
static MaterialX::ValuePtr float_to_type(float v, std::string 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);
};
template<class T> NodeItem NodeItem::val(const T &data) const