Implement export of Shader BSDF nodes #13

Merged
Bogdan Nagirniak merged 13 commits from BogdanNagirniak/blender:matx-shader-bsdf-nodes into matx-export-material 2023-09-07 11:22:44 +02:00
3 changed files with 23 additions and 18 deletions
Showing only changes of commit fa280cbbfa - Show all commits

View File

@ -101,6 +101,7 @@ NodeItem NodeItem::operator+(const NodeItem &other) const
{ {
Type type = this->type(); Type type = this->type();
if (ELEM(type, Type::BSDF, Type::EDF)) { if (ELEM(type, Type::BSDF, Type::EDF)) {
/* Special case: add BSDF/EDF shaders */
NodeItem res = empty(); NodeItem res = empty();
if (other.type() == type) { if (other.type() == type) {
res.node = graph_->addNode("add", MaterialX::EMPTY_STRING, this->type(type)); res.node = graph_->addNode("add", MaterialX::EMPTY_STRING, this->type(type));
@ -130,8 +131,10 @@ NodeItem NodeItem::operator*(const NodeItem &other) const
{ {
Type type = this->type(); Type type = this->type();
if (ELEM(type, Type::BSDF, Type::EDF)) { if (ELEM(type, Type::BSDF, Type::EDF)) {
/* Special case: multiple BSDF/EDF shader by Float or Color3 */
NodeItem res = empty(); NodeItem res = empty();
if (ELEM(other.type(), Type::Float, Type::Color3)) { Type other_type = other.type();
if (ELEM(other_type, Type::Float, Type::Color3)) {
res.node = graph_->addNode("multiply", MaterialX::EMPTY_STRING, this->type(type)); res.node = graph_->addNode("multiply", MaterialX::EMPTY_STRING, this->type(type));
res.set_input("in1", *this); res.set_input("in1", *this);
res.set_input("in2", other); res.set_input("in2", other);
@ -178,7 +181,7 @@ bool NodeItem::operator==(const NodeItem &other) const
NodeItem item1 = *this; NodeItem item1 = *this;
NodeItem item2 = other; NodeItem item2 = other;
Type to_type = adjust_types(item1, item2); Type to_type = cast_types(item1, item2);
if (to_type == Type::Empty) { if (to_type == Type::Empty) {
return false; return false;
} }
@ -560,7 +563,7 @@ NodeItem NodeItem::if_else(CompareOp op,
auto item1 = if_val; auto item1 = if_val;
auto item2 = else_val; auto item2 = else_val;
Type to_type = adjust_types(item1, item2); Type to_type = cast_types(item1, item2);
if (to_type == Type::Empty) { if (to_type == Type::Empty) {
return res; return res;
} }
@ -671,7 +674,7 @@ bool NodeItem::is_arithmetic(Type type)
return type >= Type::Float && type <= Type::Color4; return type >= Type::Float && type <= Type::Color4;
} }
NodeItem::Type NodeItem::adjust_types(NodeItem &item1, NodeItem &item2) NodeItem::Type NodeItem::cast_types(NodeItem &item1, NodeItem &item2)
{ {
Type t1 = item1.type(); Type t1 = item1.type();
Type t2 = item2.type(); Type t2 = item2.type();
@ -761,7 +764,7 @@ NodeItem NodeItem::arithmetic(const NodeItem &other,
NodeItem res = empty(); NodeItem res = empty();
NodeItem item1 = *this; NodeItem item1 = *this;
NodeItem item2 = other; NodeItem item2 = other;
Type to_type = adjust_types(item1, item2); Type to_type = cast_types(item1, item2);
if (to_type == Type::Empty) { if (to_type == Type::Empty) {
return res; return res;
} }

View File

@ -11,22 +11,24 @@ namespace blender::nodes::materialx {
class NodeItem { class NodeItem {
public: public:
enum class Type { enum class Type {
Empty = 0, Any = 0,
Any, Empty,
/* Value types */ /* Value types */
String, String,
Filename, Filename,
Integer, Integer,
/* Block of arithmetic types. Ordered by type cast */
Float, Float,
Vector2, Vector2,
Vector3, Vector3,
Vector4,
Color3, Color3,
Vector4,
Color4, Color4,
/* End of arithmetic types */
/* Shader types /* Shader types
* Note: There are only supported types */ * NOTE: There are only supported types */
BSDF, BSDF,
EDF, EDF,
SurfaceShader, SurfaceShader,
@ -106,7 +108,7 @@ class NodeItem {
private: private:
static bool is_arithmetic(Type type); static bool is_arithmetic(Type type);
static Type adjust_types(NodeItem &item1, NodeItem &item2); static Type cast_types(NodeItem &item1, NodeItem &item2);
bool is_arithmetic() const; bool is_arithmetic() const;
NodeItem arithmetic(const std::string &category, std::function<float(float)> func) const; NodeItem arithmetic(const std::string &category, std::function<float(float)> func) const;