Blender V4.5
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
7#include "UI_interface.hh"
8#include "UI_resources.hh"
9
10#include "NOD_rna_define.hh"
11
12#include "RNA_enum_types.hh"
13
14#include "node_geometry_util.hh"
15
17
18enum class Mode {
20 Free = 1,
22};
23
25{
26 b.use_custom_socket_order();
27 b.allow_any_socket_order();
28 b.add_default_layout();
29 b.add_input<decl::Geometry>("Mesh").supported_type(GeometryComponent::Type::Mesh);
30 b.add_output<decl::Geometry>("Mesh").propagate_all().align_with_previous();
31 if (const bNode *node = b.node_or_null()) {
32 switch (Mode(node->custom1)) {
33 case Mode::Sharpness:
34 b.add_input<decl::Bool>("Remove Custom").default_value(true);
35 b.add_input<decl::Bool>("Edge Sharpness").field_on_all();
36 b.add_input<decl::Bool>("Face Sharpness").field_on_all();
37 break;
38 case Mode::Free:
40 b.add_input<decl::Vector>("Custom Normal")
41 .subtype(PROP_XYZ)
42 .implicit_field_on_all(NODE_DEFAULT_INPUT_NORMAL_FIELD)
43 .hide_value();
44 break;
45 }
46 }
47}
48
49static void node_layout(uiLayout *layout, bContext * /*C*/, PointerRNA *ptr)
50{
51 const bNode &node = *static_cast<const bNode *>(ptr->data);
52 layout->prop(ptr, "mode", UI_ITEM_NONE, "", ICON_NONE);
53 if (Mode(node.custom1) == Mode::Free) {
54 layout->prop(ptr, "domain", UI_ITEM_NONE, "", ICON_NONE);
55 }
56}
57
58static void node_init(bNodeTree * /*tree*/, bNode *node)
59{
60 node->custom1 = int16_t(Mode::Sharpness);
61 node->custom2 = int16_t(bke::AttrDomain::Point);
62}
63
65{
66 const bNode &node = params.node();
67 const Mode mode = static_cast<Mode>(node.custom1);
68 GeometrySet geometry_set = params.extract_input<GeometrySet>("Mesh");
69
70 bool add_sharpness_and_corner_fan_info = false;
71
72 switch (mode) {
73 case Mode::Sharpness: {
74 const bool remove_custom = params.extract_input<bool>("Remove Custom");
75 const fn::Field sharp_edge = params.extract_input<fn::Field<bool>>("Edge Sharpness");
76 const fn::Field sharp_face = params.extract_input<fn::Field<bool>>("Face Sharpness");
77 geometry_set.modify_geometry_sets([&](GeometrySet &geometry_set) {
78 if (Mesh *mesh = geometry_set.get_mesh_for_write()) {
79 /* Evaluate both fields before storing the result to avoid one attribute change
80 * potentially affecting the other field evaluation. */
81 const bke::MeshFieldContext edge_context(*mesh, bke::AttrDomain::Edge);
82 const bke::MeshFieldContext face_context(*mesh, bke::AttrDomain::Face);
83 fn::FieldEvaluator edge_evaluator(edge_context, mesh->edges_num);
84 fn::FieldEvaluator face_evaluator(face_context, mesh->faces_num);
85 edge_evaluator.add(sharp_edge);
86 face_evaluator.add(sharp_face);
87 edge_evaluator.evaluate();
88 face_evaluator.evaluate();
89 const IndexMask edge_values = edge_evaluator.get_evaluated_as_mask(0);
90 const IndexMask face_values = face_evaluator.get_evaluated_as_mask(0);
91 bke::MutableAttributeAccessor attributes = mesh->attributes_for_write();
92 if (edge_values.is_empty()) {
93 attributes.remove("sharp_edge");
94 }
95 else {
97 "sharp_edge", bke::AttrDomain::Edge);
98 edge_values.to_bools(attr.span);
99 attr.finish();
100 }
101 if (face_values.is_empty()) {
102 attributes.remove("sharp_face");
103 }
104 else {
106 "sharp_face", bke::AttrDomain::Face);
107 face_values.to_bools(attr.span);
108 attr.finish();
109 }
110 if (remove_custom) {
111 attributes.remove("custom_normal");
112 }
113 else {
114 if (const std::optional<bke::AttributeMetaData> meta_data =
115 attributes.lookup_meta_data("custom_normal"))
116 {
117 if (meta_data->domain == bke::AttrDomain::Corner &&
118 meta_data->data_type == CD_PROP_INT16_2D)
119 {
120 add_sharpness_and_corner_fan_info = true;
121 }
122 }
123 }
124 }
125 });
126 break;
127 }
128 case Mode::Free: {
129 const fn::Field custom_normal = params.extract_input<fn::Field<float3>>("Custom Normal");
130 geometry_set.modify_geometry_sets([&](GeometrySet &geometry_set) {
131 if (Mesh *mesh = geometry_set.get_mesh_for_write()) {
132 const bke::AttrDomain domain = bke::AttrDomain(node.custom2);
133 bke::try_capture_field_on_geometry(mesh->attributes_for_write(),
134 bke::MeshFieldContext(*mesh, domain),
135 "custom_normal",
136 domain,
138 custom_normal);
139 }
140 });
141 break;
142 }
144 const fn::Field custom_normal = params.extract_input<fn::Field<float3>>("Custom Normal");
145 geometry_set.modify_geometry_sets([&](GeometrySet &geometry_set) {
146 if (Mesh *mesh = geometry_set.get_mesh_for_write()) {
148 fn::FieldEvaluator evaluator(context, mesh->corners_num);
149 Array<float3> corner_normals(mesh->corners_num);
150 evaluator.add_with_destination<float3>(custom_normal, corner_normals);
151 evaluator.evaluate();
152 bke::mesh_set_custom_normals(*mesh, corner_normals);
153 }
154 });
155 break;
156 }
157 }
158
159 if (add_sharpness_and_corner_fan_info) {
160 params.error_message_add(NodeWarningType::Info,
161 "Adjusting sharpness with \"Tangent Space\" custom normals "
162 "may lead to unexpected results");
163 }
164
165 params.set_output("Mesh", std::move(geometry_set));
166}
167
168static void node_rna(StructRNA *srna)
169{
170 static const EnumPropertyItem mode_items[] = {
171 {int(Mode::Sharpness),
172 "SHARPNESS",
173 0,
174 "Sharpness",
175 "Store the sharpness of each face or edge. Similar to the \"Shade Smooth\" and \"Shade "
176 "Flat\" operators."},
177 {int(Mode::Free),
178 "FREE",
179 0,
180 "Free",
181 "Store custom normals as simple vectors in the local space of the mesh. Values are not "
182 "necessarily updated automatically later on as the mesh is deformed."},
184 "TANGENT_SPACE",
185 0,
186 "Tangent Space",
187 "Store normals in a deformation dependent custom transformation space. This method is "
188 "slower, but can be better when subsequent operations change the mesh without handling "
189 "normals specifically."},
190 {0, nullptr, 0, nullptr, nullptr},
191 };
192
194 "mode",
195 "Mode",
196 "Storage mode for custom normal data",
197 mode_items,
200 "domain",
201 "Domain",
202 "Attribute domain to store free custom normals",
205}
206
207static void node_register()
208{
209 static blender::bke::bNodeType ntype;
210 geo_node_type_base(&ntype, "GeometryNodeSetMeshNormal");
211 ntype.ui_name = "Set Mesh Normal";
212 ntype.ui_description = "Store a normal vector for each mesh element";
214 ntype.declare = node_declare;
216 ntype.initfunc = node_init;
218
220
221 node_rna(ntype.rna_ext.srna);
222}
224
225} // namespace blender::nodes::node_geo_set_mesh_normal_cc
#define NODE_CLASS_GEOMETRY
Definition BKE_node.hh:447
@ CD_PROP_INT16_2D
@ NODE_DEFAULT_INPUT_NORMAL_FIELD
#define NOD_REGISTER_NODE(REGISTER_FUNC)
#define NOD_inline_enum_accessors(member)
@ PROP_XYZ
Definition RNA_types.hh:257
#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, eCustomDataType 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:2748
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
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
Definition RNA_types.hh:909
int16_t custom1
int16_t custom2
void modify_geometry_sets(ForeachSubGeometryCallback callback)
Defines a node type.
Definition BKE_node.hh:226
std::string ui_description
Definition BKE_node.hh:232
void(* initfunc)(bNodeTree *ntree, bNode *node)
Definition BKE_node.hh:277
NodeGeometryExecFunction geometry_node_execute
Definition BKE_node.hh:347
void(* draw_buttons)(uiLayout *, bContext *C, PointerRNA *ptr)
Definition BKE_node.hh:247
NodeDeclareFunction declare
Definition BKE_node.hh:355
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:4227