Blender V5.3
node_geo_subdivision_surface.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
6
7#include "BKE_attribute.hh"
8#include "BKE_lib_id.hh"
9#include "BKE_mesh.hh"
10#include "BKE_subdiv.hh"
11#include "BKE_subdiv_mesh.hh"
12
13#include "RNA_enum_types.hh"
14
16#include "GEO_randomize.hh"
17
19
20#include "node_geometry_util.hh"
21
23
25
27{
28 b.use_custom_socket_order();
29 b.allow_any_socket_order();
30 b.add_input<decl::Geometry>("Mesh"_ustr)
31 .supported_type(GeometryComponent::Type::Mesh)
32 .description("Mesh to subdivide");
33 b.add_output<decl::Geometry>("Mesh"_ustr).propagate_all_geometry().align_with_previous();
34 b.add_input<decl::Int>("Level"_ustr).default_value(1).min(0).max(6);
35 b.add_input<decl::Float>("Edge Crease"_ustr)
36 .default_value(0.0f)
37 .min(0.0f)
38 .max(1.0f)
39 .subtype(PROP_FACTOR)
40 .evaluated_geometry_field();
41 b.add_input<decl::Float>("Vertex Crease"_ustr)
42 .default_value(0.0f)
43 .min(0.0f)
44 .max(1.0f)
45 .subtype(PROP_FACTOR)
46 .evaluated_geometry_field();
47 b.add_input<decl::Bool>("Limit Surface"_ustr)
48 .default_value(true)
49 .description(
50 "Place vertices at the surface that would be produced with infinite "
51 "levels of subdivision (smoothest possible shape)");
52 b.add_input<decl::Int>("Quality"_ustr)
54 .min(1)
55 .max(10)
56 .description("Accuracy of vertex positions, lower value is faster but less precise.");
57 b.add_input<decl::Menu>("UV Smooth"_ustr)
60 .optional_label()
61 .description("Controls how smoothing is applied to UVs");
62 b.add_input<decl::Menu>("Boundary Smooth"_ustr)
64 .default_value(SUBSURF_BOUNDARY_SMOOTH_ALL)
65 .optional_label()
66 .description("Controls how open boundaries are smoothed");
67}
68
69static void node_init(bNodeTree * /*tree*/, bNode *node)
70{
71 /* Still used for forward compatibility. */
72 node->storage = MEM_new<NodeGeometrySubdivisionSurface>(__func__);
73}
74
75#ifdef WITH_OPENSUBDIV
76
77static void write_vert_creases(Mesh &mesh, const VArray<float> &creases)
78{
79 bke::MutableAttributeAccessor attributes = mesh.attributes_for_write();
80 attributes.remove("crease_vert");
81 attributes.add<float>("crease_vert", AttrDomain::Point, bke::AttributeInitVArray(creases));
82}
83
84static void write_edge_creases(Mesh &mesh, const VArray<float> &creases)
85{
86 bke::MutableAttributeAccessor attributes = mesh.attributes_for_write();
87 attributes.remove("crease_edge");
88 attributes.add<float>("crease_edge", AttrDomain::Edge, bke::AttributeInitVArray(creases));
89}
90
91static bool varray_is_single_zero(const VArray<float> &varray)
92{
93 if (const std::optional<float> value = varray.get_if_single()) {
94 return *value == 0.0f;
95 }
96 return false;
97}
98
99static fn::Field<float> clamp_crease(fn::Field<float> crease_field)
100{
101 static auto clamp_fn = mf::build::SI1_SO<float, float>(
102 "Clamp",
103 [](float value) { return std::clamp(value, 0.0f, 1.0f); },
104 mf::build::exec_presets::AllSpanOrSingle());
105 return fn::Field<float>(fn::FieldOperation::from(clamp_fn, {std::move(crease_field)}));
106}
107
108static Mesh *mesh_subsurf_calc(const Mesh *mesh,
109 const int level,
110 const int quality,
111 const Field<float> &vert_crease_field,
112 const Field<float> &edge_crease_field,
113 const int boundary_smooth,
114 const int uv_smooth,
115 const bool use_limit_surface)
116{
117 const bke::MeshFieldContext point_context{*mesh, AttrDomain::Point};
118 FieldEvaluator point_evaluator(point_context, mesh->verts_num);
119 point_evaluator.add(clamp_crease(vert_crease_field));
120 point_evaluator.evaluate();
121
122 const bke::MeshFieldContext edge_context{*mesh, AttrDomain::Edge};
123 FieldEvaluator edge_evaluator(edge_context, mesh->edges_num);
124 edge_evaluator.add(clamp_crease(edge_crease_field));
125 edge_evaluator.evaluate();
126
127 const VArray<float> vert_creases = point_evaluator.get_evaluated<float>(0);
128 const VArray<float> edge_creases = edge_evaluator.get_evaluated<float>(0);
129 const bool use_creases = !varray_is_single_zero(vert_creases) ||
130 !varray_is_single_zero(edge_creases);
131
132 Mesh *mesh_copy = nullptr;
133 if (use_creases) {
134 /* Due to the "BKE_subdiv" API, the crease layers must be on the input mesh. But in this node
135 * they are provided as separate inputs, not as custom data layers. When needed, retrieve the
136 * mesh with write access and store the new crease values there. */
137 mesh_copy = BKE_mesh_copy_for_eval(*mesh);
138 write_vert_creases(*mesh_copy, vert_creases);
139 write_edge_creases(*mesh_copy, edge_creases);
140 mesh = mesh_copy;
141 }
142
143 bke::subdiv::ToMeshSettings mesh_settings;
144 mesh_settings.resolution = (1 << level) + 1;
145 mesh_settings.use_optimal_display = false;
146
147 bke::subdiv::Settings subdiv_settings;
148 subdiv_settings.is_simple = false;
149 subdiv_settings.is_adaptive = use_limit_surface;
150 subdiv_settings.use_creases = use_creases;
151 subdiv_settings.level = use_limit_surface ? quality : level;
152 subdiv_settings.vtx_boundary_interpolation =
154 subdiv_settings.fvar_linear_interpolation = bke::subdiv::fvar_interpolation_from_uv_smooth(
155 uv_smooth);
156
157 bke::subdiv::Subdiv *subdiv = bke::subdiv::new_from_mesh(&subdiv_settings, mesh);
158 if (!subdiv) {
159 if (mesh_copy) {
160 BKE_id_free(nullptr, mesh_copy);
161 }
162 return nullptr;
163 }
164
165 Mesh *result = bke::subdiv::subdiv_to_mesh(subdiv, &mesh_settings, mesh);
166 bke::subdiv::free(subdiv);
167
168 if (use_creases && result) {
169 /* Remove the layer in case it was created by the node from the field input. The fact
170 * that this node uses attributes to input creases to the subdivision code is meant to be
171 * an implementation detail ideally. */
172 result->attributes_for_write().remove("crease_vert");
173 result->attributes_for_write().remove("crease_edge");
174 }
175
176 if (mesh_copy) {
177 BKE_id_free(nullptr, mesh_copy);
178 }
179
180 if (result) {
182 }
183
184 return result;
185}
186
187#endif
188
190{
191 GeometrySet geometry_set = params.extract_input<GeometrySet>("Mesh"_ustr);
192#ifdef WITH_OPENSUBDIV
193 const Field<float> vert_crease = params.extract_input<Field<float>>("Vertex Crease"_ustr);
194 const Field<float> edge_crease = params.extract_input<Field<float>>("Edge Crease"_ustr);
195
196 const int uv_smooth = params.get_input<eSubsurfUVSmooth>("UV Smooth"_ustr);
197 const int boundary_smooth = params.get_input<eSubsurfBoundarySmooth>("Boundary Smooth"_ustr);
198 const int level = std::max(params.extract_input<int>("Level"_ustr), 0);
199 const int quality = std::clamp(params.extract_input<int>("Quality"_ustr), 1, 10);
200 const bool use_limit_surface = params.extract_input<bool>("Limit Surface"_ustr);
201 if (level == 0) {
202 params.set_output("Mesh"_ustr, std::move(geometry_set));
203 return;
204 }
205 /* At this limit, a subdivided single triangle would be too large to be stored in #Mesh. */
206 if (level >= 16) {
207 params.error_message_add(NodeWarningType::Error, TIP_("The subdivision level is too large"));
209 return;
210 }
211 if (!use_limit_surface && level >= 11) {
213 TIP_("Subdivision result mesh is too large for uniform subdivision"));
215 return;
216 }
217
218 std::atomic<bool> any_subdiv_failed = false;
219
220 geometry::foreach_real_geometry(geometry_set, [&](GeometrySet &geometry_set) {
221 if (const Mesh *mesh = geometry_set.get_mesh()) {
222 Mesh *new_mesh = mesh_subsurf_calc(mesh,
223 level,
224 quality,
225 vert_crease,
226 edge_crease,
227 boundary_smooth,
228 uv_smooth,
229 use_limit_surface);
230 if (new_mesh != nullptr) {
231 geometry_set.replace_mesh(new_mesh);
232 }
233 else {
234 any_subdiv_failed.store(true, std::memory_order_relaxed);
235 }
236 }
237 });
238 if (any_subdiv_failed.load(std::memory_order_relaxed)) {
239 params.error_message_add(
241 TIP_("Subdivision failed for some geometry. Original mesh returned."));
242 }
243#else
245 TIP_("Disabled, Blender was compiled without OpenSubdiv"));
246
247#endif
248 params.set_output("Mesh"_ustr, std::move(geometry_set));
249}
250
251static void node_register()
252{
253 static bke::bNodeType ntype;
254
255 geo_node_type_base(&ntype, "GeometryNodeSubdivisionSurface"_ustr, GEO_NODE_SUBDIVISION_SURFACE);
256 ntype.ui_name = "Subdivision Surface";
257 ntype.ui_description =
258 "Divide mesh faces to form a smooth surface, using the Catmull-Clark subdivision method";
259 ntype.enum_name_legacy = "SUBDIVISION_SURFACE";
261 ntype.declare = node_declare;
263 ntype.initfunc = node_init;
266 "NodeGeometrySubdivisionSurface",
270}
272
273} // namespace blender::nodes::node_geo_subdivision_surface_cc
#define NODE_STORAGE_FUNCS(StorageT)
Definition BKE_node.hh:1353
#define NODE_CLASS_GEOMETRY
Definition BKE_node.hh:492
#define GEO_NODE_SUBDIVISION_SURFACE
#define TIP_(msgid)
#define NOD_REGISTER_NODE(REGISTER_FUNC)
btScalar max
bool add(const StringRef name, const AttrDomain domain, const AttrType data_type, const AttributeInit &initializer)
static FieldOperationPtr from(std::shared_ptr< const mf::MultiFunction > fn, Vector< GField > inputs)
Definition field.cc:252
void error_message_add(const NodeWarningType type, StringRef message) const
T extract_input(const UString identifier)
T get_input(const UString identifier) const
void set_output(const UString identifier, T &&value)
void free(Subdiv *subdiv)
Definition subdiv.cc:192
Subdiv * new_from_mesh(const Settings *settings, const Mesh *mesh)
Definition subdiv.cc:133
FVarLinearInterpolation fvar_interpolation_from_uv_smooth(int uv_smooth)
Definition subdiv.cc:49
VtxBoundaryInterpolation vtx_boundary_interpolation_from_subsurf(int boundary_smooth)
Definition subdiv.cc:69
Mesh * subdiv_to_mesh(Subdiv *subdiv, const ToMeshSettings *settings, const Mesh *coarse_mesh)
void node_register_type(bNodeType &ntype)
Definition node.cc:2920
void node_type_storage(bNodeType &ntype, std::optional< StringRefNull > storagename, void(*freefunc)(bNode *node), void(*copyfunc)(bNodeTree *dest_ntree, bNode *dest_node, const bNode *src_node))
Definition node.cc:6166
void foreach_real_geometry(bke::GeometrySet &geometry, FunctionRef< void(bke::GeometrySet &geometry_set)> fn)
void debug_randomize_mesh_order(Mesh *mesh)
Definition randomize.cc:305
Mesh * BKE_mesh_copy_for_eval(const Mesh &source)
@ SUBSURF_UV_SMOOTH_PRESERVE_BOUNDARIES
const EnumPropertyItem rna_enum_subdivision_uv_smooth_items[]
void BKE_id_free(Main *bmain, void *idv)
const EnumPropertyItem rna_enum_subdivision_boundary_smooth_items[]
@ SUBSURF_BOUNDARY_SMOOTH_ALL
void node_copy_standard_storage(bNodeTree *, bNode *dest_node, const bNode *src_node)
Definition node_util.cc:57
void node_free_standard_storage(bNode *node)
Definition node_util.cc:45
@ PROP_FACTOR
Definition RNA_types.hh:308
void geo_node_type_base(bke::bNodeType *ntype, UString idname, const std::optional< int16_t > legacy_type)
#define min(a, b)
Definition sort.cc:38
const Mesh * get_mesh() const
void replace_mesh(Mesh *mesh, GeometryOwnershipType ownership=GeometryOwnershipType::Owned)
static constexpr int _160
Definition BKE_node.hh:246
Defines a node type.
Definition BKE_node.hh:265
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