Blender V5.3
node_geo_set_shade_smooth.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#include "DNA_mesh_types.h"
6
8#include "UI_resources.hh"
9
10#include "NOD_rna_define.hh"
11
12#include "RNA_enum_types.hh"
13
15
16#include "node_geometry_util.hh"
17
19
21{
22 b.use_custom_socket_order();
23 b.allow_any_socket_order();
24 b.add_default_layout();
25 b.add_input<decl::Geometry>("Mesh"_ustr, "Geometry"_ustr)
26 .supported_type(GeometryComponent::Type::Mesh)
27 .description("Geometry to set the smoothness of");
28 b.add_output<decl::Geometry>("Mesh"_ustr, "Geometry"_ustr)
29 .propagate_all_geometry()
30 .align_with_previous();
31 b.add_input<decl::Bool>("Selection"_ustr)
32 .default_value(true)
33 .hide_value()
34 .evaluated_geometry_field();
35 b.add_input<decl::Bool>("Shade Smooth"_ustr).default_value(true).evaluated_geometry_field();
36}
37
38static void node_layout(ui::Layout &layout, bContext * /*C*/, PointerRNA *ptr)
39{
40 layout.prop(ptr, "domain", ui::ITEM_R_EXPAND, std::nullopt, ICON_NONE);
41}
42
43static void node_init(bNodeTree * /*tree*/, bNode *node)
44{
45 node->custom1 = int16_t(AttrDomain::Face);
46}
47
54 const StringRef name,
55 const Field<bool> &selection,
56 const Field<bool> &sharpness)
57{
58 if (selection.depends_on_input() || sharpness.depends_on_input()) {
59 return false;
60 }
61 if (!fn::evaluate_constant_field(selection)) {
62 return true;
63 }
64 if (fn::evaluate_constant_field(sharpness)) {
65 return false;
66 }
67 mesh.attributes_for_write().remove(name);
68 return true;
69}
70
71static void set_sharp(Mesh &mesh,
72 const AttrDomain domain,
73 const StringRef name,
74 const Field<bool> &selection,
75 const Field<bool> &sharpness)
76{
77 const int domain_size = mesh.attributes().domain_size(domain);
78 if (domain_size == 0) {
79 return;
80 }
81 if (try_removing_sharp_attribute(mesh, name, selection, sharpness)) {
82 return;
83 }
84 bke::try_capture_field_on_geometry(mesh.attributes_for_write(),
86 name,
87 domain,
88 selection,
89 sharpness);
90}
91
93{
94 GeometrySet geometry_set = params.extract_input<GeometrySet>("Geometry"_ustr);
95 const AttrDomain domain = AttrDomain(params.node().custom1);
96 const Field<bool> selection = params.extract_input<Field<bool>>("Selection"_ustr);
97 const Field<bool> smooth_field = params.extract_input<Field<bool>>("Shade Smooth"_ustr);
98
99 geometry::foreach_real_geometry(geometry_set, [&](GeometrySet &geometry_set) {
100 if (Mesh *mesh = geometry_set.get_mesh_for_write()) {
102 domain,
103 domain == AttrDomain::Face ? "sharp_face" : "sharp_edge",
104 selection,
105 fn::invert_boolean_field(smooth_field));
106 }
107 });
108 params.set_output("Geometry"_ustr, std::move(geometry_set));
109}
110
111static void node_rna(StructRNA *srna)
112{
114 "domain",
115 "Domain",
116 "",
119 int(AttrDomain::Face));
120}
121
122static void node_register()
123{
124 static bke::bNodeType ntype;
125
126 geo_node_type_base(&ntype, "GeometryNodeSetShadeSmooth"_ustr, GEO_NODE_SET_SHADE_SMOOTH);
127 ntype.ui_name = "Set Shade Smooth";
128 ntype.ui_description =
129 "Control the smoothness of mesh normals around each face by changing the \"shade smooth\" "
130 "attribute";
131 ntype.enum_name_legacy = "SET_SHADE_SMOOTH";
134 ntype.declare = node_declare;
135 ntype.initfunc = node_init;
138
139 node_rna(ntype.rna_ext.srna);
140}
142
143} // namespace blender::nodes::node_geo_set_shade_smooth_cc
#define NODE_CLASS_GEOMETRY
Definition BKE_node.hh:492
#define GEO_NODE_SET_SHADE_SMOOTH
#define NOD_REGISTER_NODE(REGISTER_FUNC)
#define NOD_inline_enum_accessors(member)
bool depends_on_input() const
Definition FN_field.hh:513
T extract_input(const UString identifier)
void set_output(const UString identifier, T &&value)
bool try_capture_field_on_geometry(MutableAttributeAccessor attributes, const fn::FieldContext &field_context, const StringRef name, AttrDomain domain, const fn::Field< bool > &selection, const fn::GField &field)
void node_register_type(bNodeType &ntype)
Definition node.cc:2920
Field< bool > invert_boolean_field(const Field< bool > &field)
Definition field.cc:564
void evaluate_constant_field(const GField &field, void *r_value)
void foreach_real_geometry(bke::GeometrySet &geometry, FunctionRef< void(bke::GeometrySet &geometry_set)> fn)
static void node_declare(NodeDeclarationBuilder &b)
static void set_sharp(Mesh &mesh, const AttrDomain domain, const StringRef name, const Field< bool > &selection, const Field< bool > &sharpness)
static void node_init(bNodeTree *, bNode *node)
static bool try_removing_sharp_attribute(Mesh &mesh, const StringRef name, const Field< bool > &selection, const Field< bool > &sharpness)
static void node_layout(ui::Layout &layout, bContext *, PointerRNA *ptr)
static void node_geo_exec(GeoNodeExecParams params)
PropertyRNA * RNA_def_node_enum(StructRNA *srna, const char *identifier, const char *ui_name, const char *ui_description, const EnumPropertyItem *static_items, const EnumRNAAccessors accessors, std::optional< int > default_value, const EnumPropertyItemFunc item_func, const bool allow_animation)
const EnumPropertyItem rna_enum_attribute_domain_edge_face_items[]
void geo_node_type_base(bke::bNodeType *ntype, UString idname, const std::optional< int16_t > legacy_type)
signed short int16_t
Definition BKE_text.h:77
Defines a node type.
Definition BKE_node.hh:265
void(*) draw_buttons(ui::Layout &, bContext *C, PointerRNA *ptr)
Definition BKE_node.hh:290
std::string ui_description
Definition BKE_node.hh:271
NodeGeometryExecFunction geometry_node_execute
Definition BKE_node.hh:385
void(*) initfunc(bNodeTree *ntree, bNode *node)
Definition BKE_node.hh:320
const char * enum_name_legacy
Definition BKE_node.hh:274
NodeDeclareFunction declare
Definition BKE_node.hh:393
void prop(PointerRNA *ptr, PropertyRNA *prop, int index, int value, eUI_Item_Flag flag, std::optional< StringRef > name_opt, int icon, std::optional< StringRef > placeholder=std::nullopt)