Code improvements + Mix node #30

Merged
Bogdan Nagirniak merged 18 commits from BogdanNagirniak/blender:matx-code-improvements into matx-export-material 2023-09-22 18:23:13 +02:00
4 changed files with 50 additions and 17 deletions
Showing only changes of commit 4739e384ca - Show all commits

View File

@ -251,6 +251,14 @@ NodeItem NodeItem::ceil() const
return arithmetic("ceil", [](float a) { return std::ceilf(a); }); return arithmetic("ceil", [](float a) { return std::ceilf(a); });
} }
NodeItem NodeItem::length() const
{
if (value) {
return dotproduct(*this).sqrt();
}
return create_node("magnitude", Type::Float, {{"in", to_vector()}});
}
NodeItem NodeItem::min(const NodeItem &other) const NodeItem NodeItem::min(const NodeItem &other) const
{ {
return arithmetic(other, "min", [](float a, float b) { return std::min(a, b); }); return arithmetic(other, "min", [](float a, float b) { return std::min(a, b); });
@ -302,7 +310,10 @@ NodeItem NodeItem::dotproduct(const NodeItem &other) const
return val(f); return val(f);
} }
return create_node("dotproduct", Type::Float, {{"in1", *this}, {"in2", other}}); NodeItem item1 = to_vector();
NodeItem item2 = other.to_vector();
cast_types(item1, item2);
return create_node("dotproduct", Type::Float, {{"in1", item1}, {"in2", item2}});
} }
NodeItem NodeItem::mix(const NodeItem &val1, const NodeItem &val2) const NodeItem NodeItem::mix(const NodeItem &val1, const NodeItem &val2) const
@ -318,13 +329,13 @@ NodeItem NodeItem::mix(const NodeItem &val1, const NodeItem &val2) const
BLI_assert(val2.type() == type1); BLI_assert(val2.type() == type1);
/* Special case: mix BSDF/EDF shaders */ /* Special case: mix BSDF/EDF shaders */
return create_node("mix", type1, {{}, {}}); return create_node("mix", type1, {{"bg", val1}, {"fg", val2}, {"mix", *this}});
}; };
NodeItem item1 = val1; NodeItem item1 = val1;
NodeItem item2 = val2; NodeItem item2 = val2;
Type to_type = cast_types(item1, item2); Type to_type = cast_types(item1, item2);
return create_node("mix", to_type, {{"bg", item1}, {"fg", item2}}); return create_node("mix", to_type, {{"bg", item1}, {"fg", item2}, {"mix", *this}});
} }
NodeItem NodeItem::clamp(const NodeItem &min_val, const NodeItem &max_val) const NodeItem NodeItem::clamp(const NodeItem &min_val, const NodeItem &max_val) const
@ -590,6 +601,27 @@ NodeItem NodeItem::convert(Type to_type) const
return res; return res;
} }
NodeItem NodeItem::to_vector() const
{
switch (type()) {
case Type::Float:
case Type::Vector2:
case Type::Vector3:
case Type::Vector4:
return *this;
case Type::Color3:
return convert(Type::Vector3);
case Type::Color4:
return convert(Type::Vector4);
default:
BLI_assert_unreachable();
}
return empty();
}
NodeItem NodeItem::if_else(CompareOp op, NodeItem NodeItem::if_else(CompareOp op,
const NodeItem &other, const NodeItem &other,
const NodeItem &if_val, const NodeItem &if_val,
@ -857,12 +889,10 @@ NodeItem NodeItem::arithmetic(const std::string &category, std::function<float(f
} }
else { else {
NodeItem v = *this; NodeItem v = *this;
if (ELEM(type, Type::Color3, Type::Color4) && if (ELEM(category, "sin", "cos", "tan", "asin", "acos", "atan2", "sqrt", "ln", "exp"))
ELEM(category, "sin", "cos", "tan", "asin", "acos", "atan2", "sqrt", "ln", "exp"))
{ {
/* These functions haven't implementation in MaterialX, converting to Vector types */ /* These functions haven't implementation in MaterialX, converting to Vector types */
type = type == Type::Color3 ? Type::Vector3 : Type::Vector4; v = v.to_vector();
v = v.convert(type);
} }
res = create_node(category, type, {{"in", v}}); res = create_node(category, type, {{"in", v}});
} }

View File

@ -81,6 +81,7 @@ class NodeItem {
NodeItem abs() const; NodeItem abs() const;
NodeItem floor() const; NodeItem floor() const;
NodeItem ceil() const; NodeItem ceil() const;
NodeItem length() const;
NodeItem min(const NodeItem &other) const; NodeItem min(const NodeItem &other) const;
NodeItem max(const NodeItem &other) const; NodeItem max(const NodeItem &other) const;
NodeItem dotproduct(const NodeItem &other) const; NodeItem dotproduct(const NodeItem &other) const;
@ -102,6 +103,7 @@ class NodeItem {
NodeItem sign() const; NodeItem sign() const;
NodeItem exp() const; NodeItem exp() const;
NodeItem convert(Type to_type) const; NodeItem convert(Type to_type) const;
NodeItem to_vector() const;
NodeItem if_else(CompareOp op, NodeItem if_else(CompareOp op,
const NodeItem &other, const NodeItem &other,
const NodeItem &if_val, const NodeItem &if_val,

View File

@ -543,6 +543,15 @@ static void sh_node_mix_build_multi_function(NodeMultiFunctionBuilder &builder)
} }
} }
NODE_SHADER_MATERIALX_BEGIN
#ifdef WITH_MATERIALX
{
/* TODO: Implement */
return empty();
}
#endif
NODE_SHADER_MATERIALX_END
} // namespace blender::nodes::node_sh_mix_cc } // namespace blender::nodes::node_sh_mix_cc
void register_node_type_sh_mix() void register_node_type_sh_mix()
@ -562,5 +571,7 @@ void register_node_type_sh_mix()
ntype.draw_buttons = file_ns::sh_node_mix_layout; ntype.draw_buttons = file_ns::sh_node_mix_layout;
ntype.labelfunc = file_ns::sh_node_mix_label; ntype.labelfunc = file_ns::sh_node_mix_label;
ntype.gather_link_search_ops = file_ns::node_mix_gather_link_searches; ntype.gather_link_search_ops = file_ns::node_mix_gather_link_searches;
ntype.materialx_fn = file_ns::node_shader_materialx;
nodeRegisterType(&ntype); nodeRegisterType(&ntype);
} }

View File

@ -150,15 +150,6 @@ static void sh_node_mix_rgb_build_multi_function(NodeMultiFunctionBuilder &build
builder.construct_and_set_matching_fn<MixRGBFunction>(clamp, mix_type); builder.construct_and_set_matching_fn<MixRGBFunction>(clamp, mix_type);
} }
NODE_SHADER_MATERIALX_BEGIN
#ifdef WITH_MATERIALX
{
/* TODO: Implement */
return empty();
}
#endif
NODE_SHADER_MATERIALX_END
} // namespace blender::nodes::node_shader_mix_rgb_cc } // namespace blender::nodes::node_shader_mix_rgb_cc
void register_node_type_sh_mix_rgb() void register_node_type_sh_mix_rgb()
@ -173,6 +164,5 @@ void register_node_type_sh_mix_rgb()
ntype.gpu_fn = file_ns::gpu_shader_mix_rgb; ntype.gpu_fn = file_ns::gpu_shader_mix_rgb;
ntype.build_multi_function = file_ns::sh_node_mix_rgb_build_multi_function; ntype.build_multi_function = file_ns::sh_node_mix_rgb_build_multi_function;
ntype.gather_link_search_ops = nullptr; ntype.gather_link_search_ops = nullptr;
ntype.materialx_fn = file_ns::node_shader_materialx;
nodeRegisterType(&ntype); nodeRegisterType(&ntype);
} }