28 b.use_custom_socket_order();
29 b.allow_any_socket_order();
32 .description(
"Mesh to subdivide");
33 b.add_output<
decl::Geometry>(
"Mesh"_ustr).propagate_all_geometry().align_with_previous();
40 .evaluated_geometry_field();
46 .evaluated_geometry_field();
50 "Place vertices at the surface that would be produced with infinite "
51 "levels of subdivision (smoothest possible shape)");
56 .description(
"Accuracy of vertex positions, lower value is faster but less precise.");
61 .description(
"Controls how smoothing is applied to UVs");
66 .description(
"Controls how open boundaries are smoothed");
72 node->storage = MEM_new<NodeGeometrySubdivisionSurface>(__func__);
80 attributes.
remove(
"crease_vert");
87 attributes.
remove(
"crease_edge");
91static bool varray_is_single_zero(
const VArray<float> &varray)
93 if (
const std::optional<float> value = varray.get_if_single()) {
94 return *value == 0.0f;
99static fn::Field<float> clamp_crease(fn::Field<float> crease_field)
101 static auto clamp_fn = mf::build::SI1_SO<float, float>(
103 [](
float value) {
return std::clamp(value, 0.0f, 1.0f); },
104 mf::build::exec_presets::AllSpanOrSingle());
108static Mesh *mesh_subsurf_calc(
const Mesh *mesh,
111 const Field<float> &vert_crease_field,
112 const Field<float> &edge_crease_field,
113 const int boundary_smooth,
115 const bool use_limit_surface)
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();
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();
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);
132 Mesh *mesh_copy =
nullptr;
138 write_vert_creases(*mesh_copy, vert_creases);
139 write_edge_creases(*mesh_copy, edge_creases);
143 bke::subdiv::ToMeshSettings mesh_settings;
144 mesh_settings.resolution = (1 << level) + 1;
145 mesh_settings.use_optimal_display =
false;
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 =
168 if (use_creases &&
result) {
172 result->attributes_for_write().remove(
"crease_vert");
173 result->attributes_for_write().remove(
"crease_edge");
192#ifdef WITH_OPENSUBDIV
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);
202 params.
set_output(
"Mesh"_ustr, std::move(geometry_set));
211 if (!use_limit_surface && level >= 11) {
213 TIP_(
"Subdivision result mesh is too large for uniform subdivision"));
218 std::atomic<bool> any_subdiv_failed =
false;
222 Mesh *new_mesh = mesh_subsurf_calc(
mesh,
230 if (new_mesh !=
nullptr) {
234 any_subdiv_failed.store(
true, std::memory_order_relaxed);
238 if (any_subdiv_failed.load(std::memory_order_relaxed)) {
241 TIP_(
"Subdivision failed for some geometry. Original mesh returned."));
245 TIP_(
"Disabled, Blender was compiled without OpenSubdiv"));
248 params.
set_output(
"Mesh"_ustr, std::move(geometry_set));
256 ntype.
ui_name =
"Subdivision Surface";
258 "Divide mesh faces to form a smooth surface, using the Catmull-Clark subdivision method";
266 "NodeGeometrySubdivisionSurface",
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))