Blender V4.5
node_geo_mesh_subdivide.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
5#include "BKE_subdiv.hh"
6#include "BKE_subdiv_mesh.hh"
7
8#include "GEO_randomize.hh"
9
10#include "node_geometry_util.hh"
11
13
15{
16 b.use_custom_socket_order();
17 b.allow_any_socket_order();
18 b.add_input<decl::Geometry>("Mesh").supported_type(GeometryComponent::Type::Mesh);
19 b.add_output<decl::Geometry>("Mesh").propagate_all().align_with_previous();
20 b.add_input<decl::Int>("Level").default_value(1).min(0).max(6);
21}
22
23#ifdef WITH_OPENSUBDIV
24static Mesh *simple_subdivide_mesh(const Mesh &mesh, const int level)
25{
26 /* Initialize mesh settings. */
27 bke::subdiv::ToMeshSettings mesh_settings;
28 mesh_settings.resolution = (1 << level) + 1;
29 mesh_settings.use_optimal_display = false;
30
31 /* Initialize subdivision settings. */
32 bke::subdiv::Settings subdiv_settings;
33 subdiv_settings.is_simple = true;
34 subdiv_settings.is_adaptive = false;
35 subdiv_settings.use_creases = false;
36 subdiv_settings.level = 1;
37 subdiv_settings.vtx_boundary_interpolation =
40
41 /* Apply subdivision from mesh. */
42 bke::subdiv::Subdiv *subdiv = bke::subdiv::new_from_mesh(&subdiv_settings, &mesh);
43 if (!subdiv) {
44 return nullptr;
45 }
46
47 Mesh *result = bke::subdiv::subdiv_to_mesh(subdiv, &mesh_settings, &mesh);
48
49 bke::subdiv::free(subdiv);
50
52 return result;
53}
54#endif /* WITH_OPENSUBDIV */
55
57{
58 GeometrySet geometry_set = params.extract_input<GeometrySet>("Mesh");
59#ifdef WITH_OPENSUBDIV
60 /* See CCGSUBSURF_LEVEL_MAX for max limit. */
61 const int level = std::max(params.extract_input<int>("Level"), 0);
62 if (level == 0) {
63 params.set_output("Mesh", std::move(geometry_set));
64 return;
65 }
66 /* At this limit, a subdivided single triangle would be too large to be stored in #Mesh. */
67 if (level >= 16) {
68 params.error_message_add(NodeWarningType::Error, TIP_("The subdivision level is too large"));
69 params.set_default_remaining_outputs();
70 return;
71 }
72
73 geometry_set.modify_geometry_sets([&](GeometrySet &geometry_set) {
74 if (const Mesh *mesh = geometry_set.get_mesh()) {
75 geometry_set.replace_mesh(simple_subdivide_mesh(*mesh, level));
76 }
77 });
78#else
79 params.error_message_add(NodeWarningType::Error,
80 TIP_("Disabled, Blender was compiled without OpenSubdiv"));
81#endif
82 params.set_output("Mesh", std::move(geometry_set));
83}
84
85static void node_register()
86{
87 static blender::bke::bNodeType ntype;
88
89 geo_node_type_base(&ntype, "GeometryNodeSubdivideMesh", GEO_NODE_SUBDIVIDE_MESH);
90 ntype.ui_name = "Subdivide Mesh";
91 ntype.ui_description =
92 "Divide mesh faces into smaller ones without changing the shape or volume, using linear "
93 "interpolation to place the new vertices";
94 ntype.enum_name_legacy = "SUBDIVIDE_MESH";
96 ntype.declare = node_declare;
99}
101
102} // namespace blender::nodes::node_geo_mesh_subdivide_cc
#define NODE_CLASS_GEOMETRY
Definition BKE_node.hh:447
#define GEO_NODE_SUBDIVIDE_MESH
#define TIP_(msgid)
#define NOD_REGISTER_NODE(REGISTER_FUNC)
uiWidgetBaseParameters params[MAX_WIDGET_BASE_BATCH]
void free(Subdiv *subdiv)
Definition subdiv.cc:190
Subdiv * new_from_mesh(const Settings *settings, const Mesh *mesh)
Definition subdiv.cc:131
FVarLinearInterpolation fvar_interpolation_from_uv_smooth(int uv_smooth)
Definition subdiv.cc:47
VtxBoundaryInterpolation vtx_boundary_interpolation_from_subsurf(int boundary_smooth)
Definition subdiv.cc:67
Mesh * subdiv_to_mesh(Subdiv *subdiv, const ToMeshSettings *settings, const Mesh *coarse_mesh)
void node_register_type(bNodeType &ntype)
Definition node.cc:2748
void debug_randomize_mesh_order(Mesh *mesh)
Definition randomize.cc:217
static void node_declare(NodeDeclarationBuilder &b)
static void node_geo_exec(GeoNodeExecParams params)
void geo_node_type_base(blender::bke::bNodeType *ntype, std::string idname, const std::optional< int16_t > legacy_type)
const Mesh * get_mesh() const
void modify_geometry_sets(ForeachSubGeometryCallback callback)
void replace_mesh(Mesh *mesh, GeometryOwnershipType ownership=GeometryOwnershipType::Owned)
Defines a node type.
Definition BKE_node.hh:226
std::string ui_description
Definition BKE_node.hh:232
NodeGeometryExecFunction geometry_node_execute
Definition BKE_node.hh:347
const char * enum_name_legacy
Definition BKE_node.hh:235
NodeDeclareFunction declare
Definition BKE_node.hh:355
VtxBoundaryInterpolation vtx_boundary_interpolation
Definition BKE_subdiv.hh:69
FVarLinearInterpolation fvar_linear_interpolation
Definition BKE_subdiv.hh:70