Blender V5.0
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")
32 .supported_type(GeometryComponent::Type::Mesh)
33 .description("Mesh to set the custom normals on");
34 b.add_output<decl::Geometry>("Mesh").propagate_all().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").default_value(true);
39 b.add_input<decl::Bool>("Edge Sharpness").field_on_all();
40 b.add_input<decl::Bool>("Face Sharpness").field_on_all();
41 break;
42 case Mode::Free:
44 b.add_input<decl::Vector>("Custom Normal")
45 .subtype(PROP_XYZ)
46 .implicit_field_on_all(NODE_DEFAULT_INPUT_NORMAL_FIELD)
47 .hide_value();
48 break;
49 }
50 }
51}
52
53static void node_layout(uiLayout *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);
65 node->custom2 = int16_t(bke::AttrDomain::Point);
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");
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");
79 const fn::Field sharp_edge = params.extract_input<fn::Field<bool>>("Edge Sharpness");
80 const fn::Field sharp_face = params.extract_input<fn::Field<bool>>("Face Sharpness");
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. */
85 const bke::MeshFieldContext edge_context(*mesh, bke::AttrDomain::Edge);
86 const bke::MeshFieldContext face_context(*mesh, bke::AttrDomain::Face);
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 {
101 "sharp_edge", bke::AttrDomain::Edge);
102 edge_values.to_bools(attr.span);
103 attr.finish();
104 }
105 if (face_values.is_empty()) {
106 attributes.remove("sharp_face");
107 }
108 else {
110 "sharp_face", bke::AttrDomain::Face);
111 face_values.to_bools(attr.span);
112 attr.finish();
113 }
114 if (remove_custom) {
115 attributes.remove("custom_normal");
116 }
117 else {
118 if (const std::optional<bke::AttributeMetaData> meta_data =
119 attributes.lookup_meta_data("custom_normal"))
120 {
121 if (meta_data->domain == bke::AttrDomain::Corner &&
122 meta_data->data_type == bke::AttrType::Int16_2D)
123 {
124 add_sharpness_and_corner_fan_info = true;
125 }
126 }
127 }
128 }
129 });
130 break;
131 }
132 case Mode::Free: {
133 const fn::Field custom_normal = params.extract_input<fn::Field<float3>>("Custom Normal");
134 geometry::foreach_real_geometry(geometry_set, [&](GeometrySet &geometry_set) {
135 if (Mesh *mesh = geometry_set.get_mesh_for_write()) {
136 const bke::AttrDomain domain = bke::AttrDomain(node.custom2);
137 bke::try_capture_field_on_geometry(mesh->attributes_for_write(),
138 bke::MeshFieldContext(*mesh, domain),
139 "custom_normal",
140 domain,
142 custom_normal);
143 }
144 });
145 break;
146 }
148 const fn::Field custom_normal = params.extract_input<fn::Field<float3>>("Custom Normal");
149 geometry::foreach_real_geometry(geometry_set, [&](GeometrySet &geometry_set) {
150 if (Mesh *mesh = geometry_set.get_mesh_for_write()) {
152 fn::FieldEvaluator evaluator(context, mesh->corners_num);
153 Array<float3> corner_normals(mesh->corners_num);
154 evaluator.add_with_destination<float3>(custom_normal, corner_normals);
155 evaluator.evaluate();
156 mesh->attributes_for_write().remove("custom_normal");
157 bke::mesh_set_custom_normals(*mesh, corner_normals);
158 }
159 });
160 break;
161 }
162 }
163
164 if (add_sharpness_and_corner_fan_info) {
165 params.error_message_add(NodeWarningType::Info,
166 "Adjusting sharpness with \"Tangent Space\" custom normals "
167 "may lead to unexpected results");
168 }
169
170 params.set_output("Mesh", std::move(geometry_set));
171}
172
173static void node_rna(StructRNA *srna)
174{
175 static const EnumPropertyItem mode_items[] = {
176 {int(Mode::Sharpness),
177 "SHARPNESS",
178 0,
179 "Sharpness",
180 "Store the sharpness of each face or edge. Similar to the \"Shade Smooth\" and \"Shade "
181 "Flat\" operators."},
182 {int(Mode::Free),
183 "FREE",
184 0,
185 "Free",
186 "Store custom normals as simple vectors in the local space of the mesh. Values are not "
187 "necessarily updated automatically later on as the mesh is deformed."},
189 "TANGENT_SPACE",
190 0,
191 "Tangent Space",
192 "Store normals in a deformation dependent custom transformation space. This method is "
193 "slower, but can be better when subsequent operations change the mesh without handling "
194 "normals specifically."},
195 {0, nullptr, 0, nullptr, nullptr},
196 };
197
199 "mode",
200 "Mode",
201 "Storage mode for custom normal data",
202 mode_items,
205 "domain",
206 "Domain",
207 "Attribute domain to store free custom normals",
210}
211
212static void node_register()
213{
214 static blender::bke::bNodeType ntype;
215 geo_node_type_base(&ntype, "GeometryNodeSetMeshNormal");
216 ntype.ui_name = "Set Mesh Normal";
217 ntype.ui_description = "Store a normal vector for each mesh element";
219 ntype.declare = node_declare;
221 ntype.initfunc = node_init;
223
225
226 node_rna(ntype.rna_ext.srna);
227}
229
230} // namespace blender::nodes::node_geo_set_mesh_normal_cc
#define NODE_CLASS_GEOMETRY
Definition BKE_node.hh:461
@ NODE_DEFAULT_INPUT_NORMAL_FIELD
#define NOD_REGISTER_NODE(REGISTER_FUNC)
#define NOD_inline_enum_accessors(member)
@ PROP_XYZ
Definition RNA_types.hh:269
#define UI_ITEM_NONE
std::optional< AttributeMetaData > lookup_meta_data(StringRef attribute_id) const
bool remove(const StringRef attribute_id)
GSpanAttributeWriter lookup_or_add_for_write_only_span(StringRef attribute_id, AttrDomain domain, AttrType data_type)
int add(GField field, GVArray *varray_ptr)
Definition field.cc:751
IndexMask get_evaluated_as_mask(int field_index)
Definition field.cc:804
int add_with_destination(GField field, GVMutableArray dst)
Definition field.cc:738
void to_bools(MutableSpan< bool > r_bools) const
uiWidgetBaseParameters params[MAX_WIDGET_BASE_BATCH]
void node_register_type(bNodeType &ntype)
Definition node.cc:2416
bool try_capture_field_on_geometry(MutableAttributeAccessor attributes, const fn::FieldContext &field_context, const StringRef attribute_id, AttrDomain domain, const fn::Field< bool > &selection, const fn::GField &field)
void mesh_set_custom_normals(Mesh &mesh, MutableSpan< float3 > corner_normals)
GField make_constant_field(const CPPType &type, const void *value)
Definition field.cc:528
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(uiLayout *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)
VecBase< float, 3 > float3
void geo_node_type_base(blender::bke::bNodeType *ntype, std::string idname, const std::optional< int16_t > legacy_type)
const EnumPropertyItem rna_enum_attribute_domain_only_mesh_no_edge_items[]
StructRNA * srna
int16_t custom1
int16_t custom2
Defines a node type.
Definition BKE_node.hh:238
std::string ui_description
Definition BKE_node.hh:244
void(* initfunc)(bNodeTree *ntree, bNode *node)
Definition BKE_node.hh:289
NodeGeometryExecFunction geometry_node_execute
Definition BKE_node.hh:354
void(* draw_buttons)(uiLayout *, bContext *C, PointerRNA *ptr)
Definition BKE_node.hh:259
NodeDeclareFunction declare
Definition BKE_node.hh:362
void prop(PointerRNA *ptr, PropertyRNA *prop, int index, int value, eUI_Item_Flag flag, std::optional< blender::StringRef > name_opt, int icon, std::optional< blender::StringRef > placeholder=std::nullopt)
PointerRNA * ptr
Definition wm_files.cc:4238