Blender V5.3
node_geo_set_mesh_normal.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2025 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#include "BKE_mesh.hh"
6
8#include "UI_resources.hh"
9
10#include "NOD_rna_define.hh"
11
13
14#include "RNA_enum_types.hh"
15
16#include "node_geometry_util.hh"
17
19
20enum class Mode {
22 Free = 1,
24};
25
27{
28 b.use_custom_socket_order();
29 b.allow_any_socket_order();
30 b.add_default_layout();
31 b.add_input<decl::Geometry>("Mesh"_ustr)
32 .supported_type(GeometryComponent::Type::Mesh)
33 .description("Mesh to set the custom normals on");
34 b.add_output<decl::Geometry>("Mesh"_ustr).propagate_all_geometry().align_with_previous();
35 if (const bNode *node = b.node_or_null()) {
36 switch (Mode(node->custom1)) {
37 case Mode::Sharpness:
38 b.add_input<decl::Bool>("Remove Custom"_ustr).default_value(true);
39 b.add_input<decl::Bool>("Edge Sharpness"_ustr).evaluated_geometry_field();
40 b.add_input<decl::Bool>("Face Sharpness"_ustr).evaluated_geometry_field();
41 break;
42 case Mode::Free:
44 b.add_input<decl::Vector>("Custom Normal"_ustr)
45 .subtype(PROP_XYZ)
46 .evaluated_geometry_field()
48 break;
49 }
50 }
51}
52
53static void node_layout(ui::Layout &layout, bContext * /*C*/, PointerRNA *ptr)
54{
55 const bNode &node = *static_cast<const bNode *>(ptr->data);
56 layout.prop(ptr, "mode", UI_ITEM_NONE, "", ICON_NONE);
57 if (Mode(node.custom1) == Mode::Free) {
58 layout.prop(ptr, "domain", UI_ITEM_NONE, "", ICON_NONE);
59 }
60}
61
62static void node_init(bNodeTree * /*tree*/, bNode *node)
63{
64 node->custom1 = int16_t(Mode::Sharpness);
66}
67
69{
70 const bNode &node = params.node();
71 const Mode mode = static_cast<Mode>(node.custom1);
72 GeometrySet geometry_set = params.extract_input<GeometrySet>("Mesh"_ustr);
73
74 bool add_sharpness_and_corner_fan_info = false;
75
76 switch (mode) {
77 case Mode::Sharpness: {
78 const bool remove_custom = params.extract_input<bool>("Remove Custom"_ustr);
79 const fn::Field sharp_edge = params.extract_input<fn::Field<bool>>("Edge Sharpness"_ustr);
80 const fn::Field sharp_face = params.extract_input<fn::Field<bool>>("Face Sharpness"_ustr);
81 geometry::foreach_real_geometry(geometry_set, [&](GeometrySet &geometry_set) {
82 if (Mesh *mesh = geometry_set.get_mesh_for_write()) {
83 /* Evaluate both fields before storing the result to avoid one attribute change
84 * potentially affecting the other field evaluation. */
87 fn::FieldEvaluator edge_evaluator(edge_context, mesh->edges_num);
88 fn::FieldEvaluator face_evaluator(face_context, mesh->faces_num);
89 edge_evaluator.add(sharp_edge);
90 face_evaluator.add(sharp_face);
91 edge_evaluator.evaluate();
92 face_evaluator.evaluate();
93 const IndexMask edge_values = edge_evaluator.get_evaluated_as_mask(0);
94 const IndexMask face_values = face_evaluator.get_evaluated_as_mask(0);
95 bke::MutableAttributeAccessor attributes = mesh->attributes_for_write();
96 if (edge_values.is_empty()) {
97 attributes.remove("sharp_edge");
98 }
99 else if (edge_values.size() == mesh->edges_num) {
100 attributes.remove("sharp_edge");
101 attributes.add<bool>(
103 }
104 else {
106 "sharp_edge", bke::AttrDomain::Edge);
107 edge_values.to_bools(attr.span);
108 attr.finish();
109 }
110 if (face_values.is_empty()) {
111 attributes.remove("sharp_face");
112 }
113 else if (face_values.size() == mesh->faces_num) {
114 attributes.remove("sharp_face");
115 attributes.add<bool>(
117 }
118 else {
120 "sharp_face", bke::AttrDomain::Face);
121 face_values.to_bools(attr.span);
122 attr.finish();
123 }
124 if (remove_custom) {
125 attributes.remove("custom_normal");
126 }
127 else {
128 if (const std::optional<bke::AttributeMetaData> meta_data =
129 attributes.lookup_meta_data("custom_normal"))
130 {
131 if (meta_data->domain == bke::AttrDomain::Corner &&
132 meta_data->data_type == bke::AttrType::Int16_2D)
133 {
134 add_sharpness_and_corner_fan_info = true;
135 }
136 }
137 }
138 }
139 });
140 break;
141 }
142 case Mode::Free: {
143 const fn::Field custom_normal = params.extract_input<fn::Field<float3>>(
144 "Custom Normal"_ustr);
145 geometry::foreach_real_geometry(geometry_set, [&](GeometrySet &geometry_set) {
146 if (Mesh *mesh = geometry_set.get_mesh_for_write()) {
147 const bke::AttrDomain domain = bke::AttrDomain(node.custom2);
148 bke::try_capture_field_on_geometry(mesh->attributes_for_write(),
149 bke::MeshFieldContext(*mesh, domain),
150 "custom_normal",
151 domain,
152 fn::Field<bool>(true),
153 custom_normal);
154 }
155 });
156 break;
157 }
159 const fn::Field custom_normal = params.extract_input<fn::Field<float3>>(
160 "Custom Normal"_ustr);
161 geometry::foreach_real_geometry(geometry_set, [&](GeometrySet &geometry_set) {
162 if (Mesh *mesh = geometry_set.get_mesh_for_write()) {
164 fn::FieldEvaluator evaluator(context, mesh->corners_num);
165 Array<float3> corner_normals(mesh->corners_num);
166 evaluator.add_with_destination<float3>(custom_normal, corner_normals);
167 evaluator.evaluate();
168 mesh->attributes_for_write().remove("custom_normal");
169 bke::mesh_set_custom_normals(*mesh, corner_normals);
170 }
171 });
172 break;
173 }
174 }
175
176 if (add_sharpness_and_corner_fan_info) {
178 "Adjusting sharpness with \"Tangent Space\" custom normals "
179 "may lead to unexpected results");
180 }
181
182 params.set_output("Mesh"_ustr, std::move(geometry_set));
183}
184
185static void node_rna(StructRNA *srna)
186{
187 static const EnumPropertyItem mode_items[] = {
188 {int(Mode::Sharpness),
189 "SHARPNESS",
190 0,
191 "Sharpness",
192 "Store the sharpness of each face or edge. Similar to the \"Shade Smooth\" and \"Shade "
193 "Flat\" operators."},
194 {int(Mode::Free),
195 "FREE",
196 0,
197 "Free",
198 "Store custom normals as simple vectors in the local space of the mesh. Values are not "
199 "necessarily updated automatically later on as the mesh is deformed."},
201 "TANGENT_SPACE",
202 0,
203 "Tangent Space",
204 "Store normals in a deformation dependent custom transformation space. This method is "
205 "slower, but can be better when subsequent operations change the mesh without handling "
206 "normals specifically."},
207 {0, nullptr, 0, nullptr, nullptr},
208 };
209
211 "mode",
212 "Mode",
213 "Storage mode for custom normal data",
214 mode_items,
217 "domain",
218 "Domain",
219 "Attribute domain to store free custom normals",
222}
223
224static void node_register()
225{
226 static bke::bNodeType ntype;
227 geo_node_type_base(&ntype, "GeometryNodeSetMeshNormal"_ustr);
228 ntype.ui_name = "Set Mesh Normal";
229 ntype.ui_description = "Store a normal vector for each mesh element";
231 ntype.declare = node_declare;
233 ntype.initfunc = node_init;
235
237
238 node_rna(ntype.rna_ext.srna);
239}
241
242} // namespace blender::nodes::node_geo_set_mesh_normal_cc
#define NODE_CLASS_GEOMETRY
Definition BKE_node.hh:492
#define NOD_REGISTER_NODE(REGISTER_FUNC)
#define NOD_inline_enum_accessors(member)
#define UI_ITEM_NONE
std::optional< AttributeMetaData > lookup_meta_data(StringRef name) const
bool add(const StringRef name, const AttrDomain domain, const AttrType data_type, const AttributeInit &initializer)
GSpanAttributeWriter lookup_or_add_for_write_only_span(StringRef name, AttrDomain domain, AttrType data_type)
int add(GField field, GVArray *varray_ptr)
IndexMask get_evaluated_as_mask(int field_index)
int add_with_destination(GField field, GVMutableArray dst)
void to_bools(MutableSpan< bool > r_bools) const
void error_message_add(const NodeWarningType type, StringRef message) const
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
void mesh_set_custom_normals(Mesh &mesh, MutableSpan< float3 > corner_normals)
void foreach_real_geometry(bke::GeometrySet &geometry, FunctionRef< void(bke::GeometrySet &geometry_set)> fn)
static void node_init(bNodeTree *, bNode *node)
static void node_declare(NodeDeclarationBuilder &b)
static void node_geo_exec(GeoNodeExecParams params)
static void node_layout(ui::Layout &layout, bContext *, PointerRNA *ptr)
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_only_mesh_no_edge_items[]
VecBase< float, 3 > float3
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
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)