From 24cc552cf48694b5ed07d885c0cc69220cbbe34b Mon Sep 17 00:00:00 2001 From: Johnny Matthews Date: Wed, 13 Oct 2021 08:39:54 -0500 Subject: Geometry Nodes: Material Index, Set Material Add Get/Set Nodes for Material Index Rename Assign Material to Set Material Differential Revision: https://developer.blender.org/D12837 --- release/scripts/startup/nodeitems_builtins.py | 32 +++++-- source/blender/blenkernel/BKE_node.h | 4 +- source/blender/blenkernel/intern/node.cc | 4 +- source/blender/blenloader/intern/versioning_300.c | 15 ++++ source/blender/nodes/CMakeLists.txt | 4 +- source/blender/nodes/NOD_geometry.h | 4 +- source/blender/nodes/NOD_static_types.h | 4 +- .../nodes/node_geo_input_material_index.cc | 42 ++++++++++ .../geometry/nodes/node_geo_material_assign.cc | 97 ---------------------- .../nodes/geometry/nodes/node_geo_set_material.cc | 97 ++++++++++++++++++++++ .../geometry/nodes/node_geo_set_material_index.cc | 78 +++++++++++++++++ 11 files changed, 271 insertions(+), 110 deletions(-) create mode 100644 source/blender/nodes/geometry/nodes/node_geo_input_material_index.cc delete mode 100644 source/blender/nodes/geometry/nodes/node_geo_material_assign.cc create mode 100644 source/blender/nodes/geometry/nodes/node_geo_set_material.cc create mode 100644 source/blender/nodes/geometry/nodes/node_geo_set_material_index.cc diff --git a/release/scripts/startup/nodeitems_builtins.py b/release/scripts/startup/nodeitems_builtins.py index f63e41b0c28..40afcc69bd2 100644 --- a/release/scripts/startup/nodeitems_builtins.py +++ b/release/scripts/startup/nodeitems_builtins.py @@ -180,6 +180,29 @@ def geometry_input_node_items(context): yield NodeItem("GeometryNodeInputPosition") yield NodeItem("GeometryNodeInputRadius") +# Custom Menu for Material Node Input Nodes +def geometry_material_node_items(context): + if context is None: + return + space = context.space_data + if not space: + return + if not space.edit_tree: + return + + if geometry_nodes_legacy_poll(context): + yield NodeItem("GeometryNodeLegacyMaterialAssign") + yield NodeItem("GeometryNodeLegacySelectByMaterial") + yield NodeItemCustom(draw=lambda self, layout, context: layout.separator()) + + yield NodeItem("GeometryNodeMaterialReplace") + yield NodeItemCustom(draw=lambda self, layout, context: layout.separator()) + yield NodeItem("GeometryNodeInputMaterialIndex") + yield NodeItem("GeometryNodeMaterialSelection") + yield NodeItemCustom(draw=lambda self, layout, context: layout.separator()) + yield NodeItem("GeometryNodeSetMaterial") + yield NodeItem("GeometryNodeSetMaterialIndex") + # Custom Menu for Geometry Node Curves def point_node_items(context): if context is None: @@ -662,14 +685,7 @@ geometry_node_categories = [ NodeItem("GeometryNodeRealizeInstances"), ]), GeometryNodeCategory("GEO_INPUT", "Input", items=geometry_input_node_items), - GeometryNodeCategory("GEO_MATERIAL", "Material", items=[ - NodeItem("GeometryNodeLegacyMaterialAssign", poll=geometry_nodes_legacy_poll), - NodeItem("GeometryNodeLegacySelectByMaterial", poll=geometry_nodes_legacy_poll), - - NodeItem("GeometryNodeMaterialAssign"), - NodeItem("GeometryNodeMaterialSelection"), - NodeItem("GeometryNodeMaterialReplace"), - ]), + GeometryNodeCategory("GEO_MATERIAL", "Material", items=geometry_material_node_items), GeometryNodeCategory("GEO_MESH", "Mesh", items=mesh_node_items), GeometryNodeCategory("GEO_PRIMITIVES_MESH", "Mesh Primitives", items=[ NodeItem("GeometryNodeMeshCircle"), diff --git a/source/blender/blenkernel/BKE_node.h b/source/blender/blenkernel/BKE_node.h index 81cf1ed180f..026b6574374 100644 --- a/source/blender/blenkernel/BKE_node.h +++ b/source/blender/blenkernel/BKE_node.h @@ -1494,7 +1494,7 @@ int ntreeTexExecTree(struct bNodeTree *ntree, #define GEO_NODE_INPUT_NORMAL 1079 #define GEO_NODE_ATTRIBUTE_CAPTURE 1080 #define GEO_NODE_MATERIAL_SELECTION 1081 -#define GEO_NODE_MATERIAL_ASSIGN 1082 +#define GEO_NODE_SET_MATERIAL 1082 #define GEO_NODE_REALIZE_INSTANCES 1083 #define GEO_NODE_ATTRIBUTE_STATISTIC 1084 #define GEO_NODE_CURVE_SAMPLE 1085 @@ -1530,6 +1530,8 @@ int ntreeTexExecTree(struct bNodeTree *ntree, #define GEO_NODE_SET_SPLINE_RESOLUTION 1115 #define GEO_NODE_SET_SPLINE_CYCLIC 1116 #define GEO_NODE_SET_POINT_RADIUS 1117 +#define GEO_NODE_INPUT_MATERIAL_INDEX 1118 +#define GEO_NODE_SET_MATERIAL_INDEX 1119 /** \} */ diff --git a/source/blender/blenkernel/intern/node.cc b/source/blender/blenkernel/intern/node.cc index 3e577bc29a3..46022b4f4c1 100644 --- a/source/blender/blenkernel/intern/node.cc +++ b/source/blender/blenkernel/intern/node.cc @@ -5772,6 +5772,7 @@ static void registerGeometryNodes() register_node_type_geo_input_curve_tilt(); register_node_type_geo_input_index(); register_node_type_geo_input_material(); + register_node_type_geo_input_material_index(); register_node_type_geo_input_normal(); register_node_type_geo_input_position(); register_node_type_geo_input_radius(); @@ -5783,7 +5784,6 @@ static void registerGeometryNodes() register_node_type_geo_instance_on_points(); register_node_type_geo_is_viewport(); register_node_type_geo_join_geometry(); - register_node_type_geo_material_assign(); register_node_type_geo_material_replace(); register_node_type_geo_material_selection(); register_node_type_geo_mesh_primitive_circle(); @@ -5815,6 +5815,8 @@ static void registerGeometryNodes() register_node_type_geo_set_curve_handles(); register_node_type_geo_set_curve_radius(); register_node_type_geo_set_curve_tilt(); + register_node_type_geo_set_material(); + register_node_type_geo_set_material_index(); register_node_type_geo_set_point_radius(); register_node_type_geo_set_position(); register_node_type_geo_set_shade_smooth(); diff --git a/source/blender/blenloader/intern/versioning_300.c b/source/blender/blenloader/intern/versioning_300.c index 617cd8b6c58..db2bb73108b 100644 --- a/source/blender/blenloader/intern/versioning_300.c +++ b/source/blender/blenloader/intern/versioning_300.c @@ -1746,5 +1746,20 @@ void blo_do_versions_300(FileData *fd, Library *UNUSED(lib), Main *bmain) */ { /* Keep this block, even when empty. */ + LISTBASE_FOREACH (bNodeTree *, ntree, &bmain->nodetrees) { + if (ntree->type != NTREE_GEOMETRY) { + continue; + } + LISTBASE_FOREACH (bNode *, node, &ntree->nodes) { + if (node->type != GEO_NODE_SET_MATERIAL) { + continue; + } + if (strstr(node->idname, "SetMaterial")) { + /* Make sure we haven't changed this idname already. */ + continue; + } + strcpy(node->idname, "GeometryNodeSetMaterial"); + } + } } } diff --git a/source/blender/nodes/CMakeLists.txt b/source/blender/nodes/CMakeLists.txt index f22b890b243..aad61113e4c 100644 --- a/source/blender/nodes/CMakeLists.txt +++ b/source/blender/nodes/CMakeLists.txt @@ -224,6 +224,7 @@ set(SRC geometry/nodes/node_geo_input_curve_tilt.cc geometry/nodes/node_geo_input_index.cc geometry/nodes/node_geo_input_material.cc + geometry/nodes/node_geo_input_material_index.cc geometry/nodes/node_geo_input_normal.cc geometry/nodes/node_geo_input_position.cc geometry/nodes/node_geo_input_radius.cc @@ -235,7 +236,6 @@ set(SRC geometry/nodes/node_geo_instance_on_points.cc geometry/nodes/node_geo_is_viewport.cc geometry/nodes/node_geo_join_geometry.cc - geometry/nodes/node_geo_material_assign.cc geometry/nodes/node_geo_material_replace.cc geometry/nodes/node_geo_material_selection.cc geometry/nodes/node_geo_mesh_primitive_circle.cc @@ -258,6 +258,8 @@ set(SRC geometry/nodes/node_geo_set_curve_handles.cc geometry/nodes/node_geo_set_curve_radius.cc geometry/nodes/node_geo_set_curve_tilt.cc + geometry/nodes/node_geo_set_material.cc + geometry/nodes/node_geo_set_material_index.cc geometry/nodes/node_geo_set_point_radius.cc geometry/nodes/node_geo_set_position.cc geometry/nodes/node_geo_set_shade_smooth.cc diff --git a/source/blender/nodes/NOD_geometry.h b/source/blender/nodes/NOD_geometry.h index 2d409d6d80b..50142eeb559 100644 --- a/source/blender/nodes/NOD_geometry.h +++ b/source/blender/nodes/NOD_geometry.h @@ -92,6 +92,7 @@ void register_node_type_geo_input_curve_handles(void); void register_node_type_geo_input_curve_tilt(void); void register_node_type_geo_input_index(void); void register_node_type_geo_input_material(void); +void register_node_type_geo_input_material_index(void); void register_node_type_geo_input_normal(void); void register_node_type_geo_input_position(void); void register_node_type_geo_input_radius(void); @@ -103,7 +104,6 @@ void register_node_type_geo_input_tangent(void); void register_node_type_geo_instance_on_points(void); void register_node_type_geo_is_viewport(void); void register_node_type_geo_join_geometry(void); -void register_node_type_geo_material_assign(void); void register_node_type_geo_material_replace(void); void register_node_type_geo_material_selection(void); void register_node_type_geo_mesh_primitive_circle(void); @@ -136,6 +136,8 @@ void register_node_type_geo_separate_geometry(void); void register_node_type_geo_set_curve_handles(void); void register_node_type_geo_set_curve_radius(void); void register_node_type_geo_set_curve_tilt(void); +void register_node_type_geo_set_material(void); +void register_node_type_geo_set_material_index(void); void register_node_type_geo_set_point_radius(void); void register_node_type_geo_set_position(void); void register_node_type_geo_set_shade_smooth(void); diff --git a/source/blender/nodes/NOD_static_types.h b/source/blender/nodes/NOD_static_types.h index d4cc7b42292..30807073e06 100644 --- a/source/blender/nodes/NOD_static_types.h +++ b/source/blender/nodes/NOD_static_types.h @@ -352,6 +352,7 @@ DefNode(GeometryNode, GEO_NODE_INPUT_CURVE_HANDLES, 0, "INPUT_CURVE_HANDLES", In DefNode(GeometryNode, GEO_NODE_INPUT_CURVE_TILT, 0, "INPUT_CURVE_TILT", InputCurveTilt, "Curve Tilt", "") DefNode(GeometryNode, GEO_NODE_INPUT_INDEX, 0, "INDEX", InputIndex, "Index", "") DefNode(GeometryNode, GEO_NODE_INPUT_MATERIAL, def_geo_input_material, "INPUT_MATERIAL", InputMaterial, "Material", "") +DefNode(GeometryNode, GEO_NODE_INPUT_MATERIAL_INDEX, 0, "INPUT_MATERIAL_INDEX", InputMaterialIndex, "Material Index", "") DefNode(GeometryNode, GEO_NODE_INPUT_NORMAL, 0, "INPUT_NORMAL", InputNormal, "Normal", "") DefNode(GeometryNode, GEO_NODE_INPUT_POSITION, 0, "POSITION", InputPosition, "Position", "") DefNode(GeometryNode, GEO_NODE_INPUT_RADIUS, 0, "INPUT_RADIUS", InputRadius, "Radius", "") @@ -363,7 +364,6 @@ DefNode(GeometryNode, GEO_NODE_INPUT_TANGENT, 0, "INPUT_TANGENT", InputTangent, DefNode(GeometryNode, GEO_NODE_INSTANCE_ON_POINTS, 0, "INSTANCE_ON_POINTS", InstanceOnPoints, "Instance on Points", "") DefNode(GeometryNode, GEO_NODE_IS_VIEWPORT, 0, "IS_VIEWPORT", IsViewport, "Is Viewport", "") DefNode(GeometryNode, GEO_NODE_JOIN_GEOMETRY, 0, "JOIN_GEOMETRY", JoinGeometry, "Join Geometry", "") -DefNode(GeometryNode, GEO_NODE_MATERIAL_ASSIGN, 0, "MATERIAL_ASSIGN", MaterialAssign, "Assign Material", "") DefNode(GeometryNode, GEO_NODE_MATERIAL_REPLACE, 0, "MATERIAL_REPLACE", MaterialReplace, "Replace Material", "") DefNode(GeometryNode, GEO_NODE_MATERIAL_SELECTION, 0, "MATERIAL_SELECTION", MaterialSelection, "Material Selection", "") DefNode(GeometryNode, GEO_NODE_MESH_PRIMITIVE_CIRCLE, def_geo_mesh_circle, "MESH_PRIMITIVE_CIRCLE", MeshCircle, "Mesh Circle", "") @@ -386,6 +386,8 @@ DefNode(GeometryNode, GEO_NODE_SEPARATE_GEOMETRY, def_geo_separate_geometry, "SE DefNode(GeometryNode, GEO_NODE_SET_CURVE_HANDLES, def_geo_curve_set_handle_positions, "SET_CURVE_HANDLES", SetCurveHandlePositions, "Set Handle Positions", "") DefNode(GeometryNode, GEO_NODE_SET_CURVE_RADIUS, 0, "SET_CURVE_RADIUS", SetCurveRadius, "Set Curve Radius", "") DefNode(GeometryNode, GEO_NODE_SET_CURVE_TILT, 0, "SET_CURVE_TILT", SetCurveTilt, "Set Curve Tilt", "") +DefNode(GeometryNode, GEO_NODE_SET_MATERIAL, 0, "SET_MATERIAL", SetMaterial, "Set Material", "") +DefNode(GeometryNode, GEO_NODE_SET_MATERIAL_INDEX, 0, "SET_MATERIAL_INDEX", SetMaterialIndex, "Set Material Index", "") DefNode(GeometryNode, GEO_NODE_SET_POINT_RADIUS, 0, "SET_POINT_RADIUS", SetPointRadius, "Set Point Radius", "") DefNode(GeometryNode, GEO_NODE_SET_POSITION, 0, "SET_POSITION", SetPosition, "Set Position", "") DefNode(GeometryNode, GEO_NODE_SET_SHADE_SMOOTH, 0, "SET_SHADE_SMOOTH", SetShadeSmooth, "Set Shade Smooth", "") diff --git a/source/blender/nodes/geometry/nodes/node_geo_input_material_index.cc b/source/blender/nodes/geometry/nodes/node_geo_input_material_index.cc new file mode 100644 index 00000000000..702c83daea0 --- /dev/null +++ b/source/blender/nodes/geometry/nodes/node_geo_input_material_index.cc @@ -0,0 +1,42 @@ +/* + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +#include "node_geometry_util.hh" + +namespace blender::nodes { + +static void geo_node_input_material_index_declare(NodeDeclarationBuilder &b) +{ + b.add_output("Material Index").field_source(); +} + +static void geo_node_input_material_index_exec(GeoNodeExecParams params) +{ + Field material_index_field = AttributeFieldInput::Create("material_index"); + params.set_output("Material Index", std::move(material_index_field)); +} + +} // namespace blender::nodes + +void register_node_type_geo_input_material_index() +{ + static bNodeType ntype; + + geo_node_type_base(&ntype, GEO_NODE_INPUT_MATERIAL_INDEX, "Material Index", NODE_CLASS_INPUT, 0); + ntype.geometry_node_execute = blender::nodes::geo_node_input_material_index_exec; + ntype.declare = blender::nodes::geo_node_input_material_index_declare; + nodeRegisterType(&ntype); +} diff --git a/source/blender/nodes/geometry/nodes/node_geo_material_assign.cc b/source/blender/nodes/geometry/nodes/node_geo_material_assign.cc deleted file mode 100644 index 26c1aabf39f..00000000000 --- a/source/blender/nodes/geometry/nodes/node_geo_material_assign.cc +++ /dev/null @@ -1,97 +0,0 @@ -/* - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License - * as published by the Free Software Foundation; either version 2 - * of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ - -#include "node_geometry_util.hh" - -#include "UI_interface.h" -#include "UI_resources.h" - -#include "DNA_mesh_types.h" -#include "DNA_meshdata_types.h" - -#include "BKE_material.h" - -namespace blender::nodes { - -static void geo_node_material_assign_declare(NodeDeclarationBuilder &b) -{ - b.add_input("Geometry"); - b.add_input("Material").hide_label(); - b.add_input("Selection").default_value(true).hide_value().supports_field(); - b.add_output("Geometry"); -} - -static void assign_material_to_faces(Mesh &mesh, const IndexMask selection, Material *material) -{ - int new_material_index = -1; - for (const int i : IndexRange(mesh.totcol)) { - Material *other_material = mesh.mat[i]; - if (other_material == material) { - new_material_index = i; - break; - } - } - if (new_material_index == -1) { - /* Append a new material index. */ - new_material_index = mesh.totcol; - BKE_id_material_eval_assign(&mesh.id, new_material_index + 1, material); - } - - mesh.mpoly = (MPoly *)CustomData_duplicate_referenced_layer(&mesh.pdata, CD_MPOLY, mesh.totpoly); - for (const int i : selection) { - MPoly &poly = mesh.mpoly[i]; - poly.mat_nr = new_material_index; - } -} - -static void geo_node_material_assign_exec(GeoNodeExecParams params) -{ - Material *material = params.extract_input("Material"); - const Field selection_field = params.extract_input>("Selection"); - - GeometrySet geometry_set = params.extract_input("Geometry"); - - geometry_set.modify_geometry_sets([&](GeometrySet &geometry_set) { - if (geometry_set.has()) { - MeshComponent &mesh_component = geometry_set.get_component_for_write(); - Mesh *mesh = mesh_component.get_for_write(); - if (mesh != nullptr) { - GeometryComponentFieldContext field_context{mesh_component, ATTR_DOMAIN_FACE}; - - fn::FieldEvaluator selection_evaluator{field_context, mesh->totpoly}; - selection_evaluator.add(selection_field); - selection_evaluator.evaluate(); - const IndexMask selection = selection_evaluator.get_evaluated_as_mask(0); - - assign_material_to_faces(*mesh, selection, material); - } - } - }); - - params.set_output("Geometry", std::move(geometry_set)); -} - -} // namespace blender::nodes - -void register_node_type_geo_material_assign() -{ - static bNodeType ntype; - - geo_node_type_base(&ntype, GEO_NODE_MATERIAL_ASSIGN, "Assign Material", NODE_CLASS_GEOMETRY, 0); - ntype.declare = blender::nodes::geo_node_material_assign_declare; - ntype.geometry_node_execute = blender::nodes::geo_node_material_assign_exec; - nodeRegisterType(&ntype); -} diff --git a/source/blender/nodes/geometry/nodes/node_geo_set_material.cc b/source/blender/nodes/geometry/nodes/node_geo_set_material.cc new file mode 100644 index 00000000000..ab7d2ab250d --- /dev/null +++ b/source/blender/nodes/geometry/nodes/node_geo_set_material.cc @@ -0,0 +1,97 @@ +/* + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +#include "node_geometry_util.hh" + +#include "UI_interface.h" +#include "UI_resources.h" + +#include "DNA_mesh_types.h" +#include "DNA_meshdata_types.h" + +#include "BKE_material.h" + +namespace blender::nodes { + +static void geo_node_set_material_declare(NodeDeclarationBuilder &b) +{ + b.add_input("Geometry"); + b.add_input("Material").hide_label(); + b.add_input("Selection").default_value(true).hide_value().supports_field(); + b.add_output("Geometry"); +} + +static void assign_material_to_faces(Mesh &mesh, const IndexMask selection, Material *material) +{ + int new_material_index = -1; + for (const int i : IndexRange(mesh.totcol)) { + Material *other_material = mesh.mat[i]; + if (other_material == material) { + new_material_index = i; + break; + } + } + if (new_material_index == -1) { + /* Append a new material index. */ + new_material_index = mesh.totcol; + BKE_id_material_eval_assign(&mesh.id, new_material_index + 1, material); + } + + mesh.mpoly = (MPoly *)CustomData_duplicate_referenced_layer(&mesh.pdata, CD_MPOLY, mesh.totpoly); + for (const int i : selection) { + MPoly &poly = mesh.mpoly[i]; + poly.mat_nr = new_material_index; + } +} + +static void geo_node_set_material_exec(GeoNodeExecParams params) +{ + Material *material = params.extract_input("Material"); + const Field selection_field = params.extract_input>("Selection"); + + GeometrySet geometry_set = params.extract_input("Geometry"); + + geometry_set.modify_geometry_sets([&](GeometrySet &geometry_set) { + if (geometry_set.has()) { + MeshComponent &mesh_component = geometry_set.get_component_for_write(); + Mesh *mesh = mesh_component.get_for_write(); + if (mesh != nullptr) { + GeometryComponentFieldContext field_context{mesh_component, ATTR_DOMAIN_FACE}; + + fn::FieldEvaluator selection_evaluator{field_context, mesh->totpoly}; + selection_evaluator.add(selection_field); + selection_evaluator.evaluate(); + const IndexMask selection = selection_evaluator.get_evaluated_as_mask(0); + + assign_material_to_faces(*mesh, selection, material); + } + } + }); + + params.set_output("Geometry", std::move(geometry_set)); +} + +} // namespace blender::nodes + +void register_node_type_geo_set_material() +{ + static bNodeType ntype; + + geo_node_type_base(&ntype, GEO_NODE_SET_MATERIAL, "Set Material", NODE_CLASS_GEOMETRY, 0); + ntype.declare = blender::nodes::geo_node_set_material_declare; + ntype.geometry_node_execute = blender::nodes::geo_node_set_material_exec; + nodeRegisterType(&ntype); +} diff --git a/source/blender/nodes/geometry/nodes/node_geo_set_material_index.cc b/source/blender/nodes/geometry/nodes/node_geo_set_material_index.cc new file mode 100644 index 00000000000..ebf6b43ae30 --- /dev/null +++ b/source/blender/nodes/geometry/nodes/node_geo_set_material_index.cc @@ -0,0 +1,78 @@ +/* + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +#include "node_geometry_util.hh" + +namespace blender::nodes { + +static void geo_node_set_material_index_declare(NodeDeclarationBuilder &b) +{ + b.add_input("Geometry"); + b.add_input("Material Index").supports_field().min(0); + b.add_input("Selection").default_value(true).hide_value().supports_field(); + b.add_output("Geometry"); +} + +static void set_material_index_in_component(GeometryComponent &component, + const Field &selection_field, + const Field &index_field) +{ + GeometryComponentFieldContext field_context{component, ATTR_DOMAIN_FACE}; + const int domain_size = component.attribute_domain_size(ATTR_DOMAIN_FACE); + if (domain_size == 0) { + return; + } + + fn::FieldEvaluator selection_evaluator{field_context, domain_size}; + selection_evaluator.add(selection_field); + selection_evaluator.evaluate(); + const IndexMask selection = selection_evaluator.get_evaluated_as_mask(0); + + OutputAttribute_Typed indexes = component.attribute_try_get_for_output_only( + "material_index", ATTR_DOMAIN_FACE); + fn::FieldEvaluator material_evaluator{field_context, &selection}; + material_evaluator.add_with_destination(index_field, indexes.varray()); + material_evaluator.evaluate(); + indexes.save(); +} + +static void geo_node_set_material_index_exec(GeoNodeExecParams params) +{ + GeometrySet geometry_set = params.extract_input("Geometry"); + Field selection_field = params.extract_input>("Selection"); + Field index_field = params.extract_input>("Material Index"); + + geometry_set.modify_geometry_sets([&](GeometrySet &geometry_set) { + if (geometry_set.has_mesh()) { + set_material_index_in_component( + geometry_set.get_component_for_write(), selection_field, index_field); + } + }); + params.set_output("Geometry", std::move(geometry_set)); +} + +} // namespace blender::nodes + +void register_node_type_geo_set_material_index() +{ + static bNodeType ntype; + + geo_node_type_base( + &ntype, GEO_NODE_SET_MATERIAL_INDEX, "Set Material Index", NODE_CLASS_GEOMETRY, 0); + ntype.geometry_node_execute = blender::nodes::geo_node_set_material_index_exec; + ntype.declare = blender::nodes::geo_node_set_material_index_declare; + nodeRegisterType(&ntype); +} -- cgit v1.2.3