Blender  V3.3
node_geo_mesh_subdivide.cc
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 
3 #include "BKE_mesh.h"
4 #include "BKE_subdiv.h"
5 #include "BKE_subdiv_mesh.h"
6 
7 #include "UI_interface.h"
8 #include "UI_resources.h"
9 
10 #include "node_geometry_util.hh"
11 
13 
15 {
16  b.add_input<decl::Geometry>(N_("Mesh")).supported_type(GEO_COMPONENT_TYPE_MESH);
17  b.add_input<decl::Int>(N_("Level")).default_value(1).min(0).max(6);
18  b.add_output<decl::Geometry>(N_("Mesh"));
19 }
20 
21 static void geometry_set_mesh_subdivide(GeometrySet &geometry_set, const int level)
22 {
23  if (!geometry_set.has_mesh()) {
24  return;
25  }
26 
27  const Mesh *mesh_in = geometry_set.get_mesh_for_read();
28 
29  /* Initialize mesh settings. */
30  SubdivToMeshSettings mesh_settings;
31  mesh_settings.resolution = (1 << level) + 1;
32  mesh_settings.use_optimal_display = false;
33 
34  /* Initialize subdivision settings. */
35  SubdivSettings subdiv_settings;
36  subdiv_settings.is_simple = true;
37  subdiv_settings.is_adaptive = false;
38  subdiv_settings.use_creases = false;
39  subdiv_settings.level = 1;
41  0);
43 
44  /* Apply subdivision from mesh. */
45  Subdiv *subdiv = BKE_subdiv_update_from_mesh(nullptr, &subdiv_settings, mesh_in);
46 
47  /* In case of bad topology, skip to input mesh. */
48  if (subdiv == nullptr) {
49  return;
50  }
51 
52  Mesh *mesh_out = BKE_subdiv_to_mesh(subdiv, &mesh_settings, mesh_in);
53 
54  MeshComponent &mesh_component = geometry_set.get_component_for_write<MeshComponent>();
55  mesh_component.replace(mesh_out);
56 
57  BKE_subdiv_free(subdiv);
58 }
59 
61 {
62  GeometrySet geometry_set = params.extract_input<GeometrySet>("Mesh");
63 
64 #ifndef WITH_OPENSUBDIV
65  params.error_message_add(NodeWarningType::Error,
66  TIP_("Disabled, Blender was compiled without OpenSubdiv"));
67  params.set_default_remaining_outputs();
68  return;
69 #endif
70 
71  /* See CCGSUBSURF_LEVEL_MAX for max limit. */
72  const int subdiv_level = clamp_i(params.extract_input<int>("Level"), 0, 11);
73 
74  if (subdiv_level == 0) {
75  params.set_output("Mesh", std::move(geometry_set));
76  return;
77  }
78 
79  geometry_set.modify_geometry_sets(
80  [&](GeometrySet &geometry_set) { geometry_set_mesh_subdivide(geometry_set, subdiv_level); });
81 
82  params.set_output("Mesh", std::move(geometry_set));
83 }
84 
85 } // namespace blender::nodes::node_geo_mesh_subdivide_cc
86 
88 {
90 
91  static bNodeType ntype;
92 
96  nodeRegisterType(&ntype);
97 }
@ GEO_COMPONENT_TYPE_MESH
#define GEO_NODE_SUBDIVIDE_MESH
Definition: BKE_node.h:1390
#define NODE_CLASS_GEOMETRY
Definition: BKE_node.h:359
void nodeRegisterType(struct bNodeType *ntype)
Definition: node.cc:1357
eSubdivVtxBoundaryInterpolation BKE_subdiv_vtx_boundary_interpolation_from_subsurf(int boundary_smooth)
Definition: subdiv.c:66
void BKE_subdiv_free(Subdiv *subdiv)
Definition: subdiv.c:184
Subdiv * BKE_subdiv_update_from_mesh(Subdiv *subdiv, const SubdivSettings *settings, const struct Mesh *mesh)
eSubdivFVarLinearInterpolation BKE_subdiv_fvar_interpolation_from_uv_smooth(int uv_smooth)
Definition: subdiv.c:46
struct Mesh * BKE_subdiv_to_mesh(struct Subdiv *subdiv, const SubdivToMeshSettings *settings, const struct Mesh *coarse_mesh)
MINLINE int clamp_i(int value, int min, int max)
#define TIP_(msgid)
void replace(Mesh *mesh, GeometryOwnershipType ownership=GeometryOwnershipType::Owned)
uiWidgetBaseParameters params[MAX_WIDGET_BASE_BATCH]
static void node_declare(NodeDeclarationBuilder &b)
static void geometry_set_mesh_subdivide(GeometrySet &geometry_set, const int level)
static void node_geo_exec(GeoNodeExecParams params)
static const pxr::TfToken b("b", pxr::TfToken::Immortal)
void register_node_type_geo_mesh_subdivide()
void geo_node_type_base(bNodeType *ntype, int type, const char *name, short nclass)
GeometryComponent & get_component_for_write(GeometryComponentType component_type)
const Mesh * get_mesh_for_read() const
void modify_geometry_sets(ForeachSubGeometryCallback callback)
bool has_mesh() const
bool is_adaptive
Definition: BKE_subdiv.h:60
eSubdivFVarLinearInterpolation fvar_linear_interpolation
Definition: BKE_subdiv.h:71
bool use_creases
Definition: BKE_subdiv.h:68
eSubdivVtxBoundaryInterpolation vtx_boundary_interpolation
Definition: BKE_subdiv.h:70
Defines a node type.
Definition: BKE_node.h:226
NodeGeometryExecFunction geometry_node_execute
Definition: BKE_node.h:316
NodeDeclareFunction declare
Definition: BKE_node.h:324
#define N_(msgid)