Blender  V2.93
node_geo_subdivide.cc
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  */
16 
17 // #include "MEM_guardedalloc.h"
18 
19 #include "BKE_mesh.h"
20 #include "BKE_subdiv.h"
21 #include "BKE_subdiv_mesh.h"
22 
23 #include "UI_interface.h"
24 #include "UI_resources.h"
25 
26 #include "node_geometry_util.hh"
27 
29  {SOCK_GEOMETRY, N_("Geometry")},
30  {SOCK_INT, N_("Level"), 1, 0, 0, 0, 0, 6},
31  {-1, ""},
32 };
33 
35  {SOCK_GEOMETRY, N_("Geometry")},
36  {-1, ""},
37 };
38 
39 namespace blender::nodes {
40 
42 {
43  GeometrySet geometry_set = params.extract_input<GeometrySet>("Geometry");
44  geometry_set = geometry_set_realize_instances(geometry_set);
45 
46  if (!geometry_set.has_mesh()) {
47  params.set_output("Geometry", geometry_set);
48  return;
49  }
50 
51 #ifndef WITH_OPENSUBDIV
52  params.error_message_add(NodeWarningType::Error,
53  TIP_("Disabled, Blender was compiled without OpenSubdiv"));
54  params.set_output("Geometry", std::move(geometry_set));
55  return;
56 #endif
57 
58  /* See CCGSUBSURF_LEVEL_MAX for max limit. */
59  const int subdiv_level = clamp_i(params.extract_input<int>("Level"), 0, 11);
60 
61  if (subdiv_level == 0) {
62  params.set_output("Geometry", std::move(geometry_set));
63  return;
64  }
65 
66  const Mesh *mesh_in = geometry_set.get_mesh_for_read();
67 
68  /* Initialize mesh settings. */
69  SubdivToMeshSettings mesh_settings;
70  mesh_settings.resolution = (1 << subdiv_level) + 1;
71  mesh_settings.use_optimal_display = false;
72 
73  /* Initialize subdivision settings. */
74  SubdivSettings subdiv_settings;
75  subdiv_settings.is_simple = true;
76  subdiv_settings.is_adaptive = false;
77  subdiv_settings.use_creases = false;
78  subdiv_settings.level = 1;
80  0);
82 
83  /* Apply subdivision from mesh. */
84  Subdiv *subdiv = BKE_subdiv_update_from_mesh(nullptr, &subdiv_settings, mesh_in);
85 
86  /* In case of bad topology, skip to input mesh. */
87  if (subdiv == nullptr) {
88  params.set_output("Geometry", std::move(geometry_set));
89  return;
90  }
91 
92  Mesh *mesh_out = BKE_subdiv_to_mesh(subdiv, &mesh_settings, mesh_in);
93  BKE_mesh_calc_normals(mesh_out);
94 
95  MeshComponent &mesh_component = geometry_set.get_component_for_write<MeshComponent>();
96  mesh_component.replace_mesh_but_keep_vertex_group_names(mesh_out);
97 
98  BKE_subdiv_free(subdiv);
99 
100  params.set_output("Geometry", std::move(geometry_set));
101 }
102 
103 } // namespace blender::nodes
104 
106 {
107  static bNodeType ntype;
108 
112  nodeRegisterType(&ntype);
113 }
void BKE_mesh_calc_normals(struct Mesh *me)
void node_type_socket_templates(struct bNodeType *ntype, struct bNodeSocketTemplate *inputs, struct bNodeSocketTemplate *outputs)
Definition: node.cc:4527
#define NODE_CLASS_GEOMETRY
Definition: BKE_node.h:359
void nodeRegisterType(struct bNodeType *ntype)
Definition: node.cc:1298
eSubdivVtxBoundaryInterpolation BKE_subdiv_vtx_boundary_interpolation_from_subsurf(int boundary_smooth)
Definition: subdiv.c:75
void BKE_subdiv_free(Subdiv *subdiv)
Definition: subdiv.c:189
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:55
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)
#define N_(msgid)
@ SOCK_INT
@ SOCK_GEOMETRY
Group RGB to Bright Vector Camera Vector Combine Material Light Line Style Layer Add Ambient Diffuse Glossy Refraction Transparent Toon Principled Hair Volume Principled Light Particle Volume Image Sky Noise Wave Voronoi Brick Texture Vector Combine Vertex Separate Vector White RGB Map Separate Set Z Dilate Combine Combine Color Channel Split ID Combine Luminance Directional Alpha Distance Hue Movie Ellipse Bokeh View Corner Anti Mix RGB Hue Separate TEX_NODE_PROC TEX_NODE_PROC TEX_NODE_PROC TEX_NODE_PROC TEX_NODE_PROC Boolean Random Edge Subdivision Point Object Attribute Attribute Attribute Color Attribute Attribute Vector Point Attribute Sample Collection Attribute Attribute Combine GEO_NODE_SUBDIVIDE
void replace_mesh_but_keep_vertex_group_names(Mesh *mesh, GeometryOwnershipType ownership=GeometryOwnershipType::Owned)
uiWidgetBaseParameters params[MAX_WIDGET_BASE_BATCH]
GeometrySet geometry_set_realize_instances(const GeometrySet &geometry_set)
static void geo_node_subdivide_exec(GeoNodeExecParams params)
static bNodeSocketTemplate geo_node_subdivide_out[]
void register_node_type_geo_subdivide()
static bNodeSocketTemplate geo_node_subdivide_in[]
void geo_node_type_base(bNodeType *ntype, int type, const char *name, short nclass, short flag)
GeometryComponent & get_component_for_write(GeometryComponentType component_type)
const Mesh * get_mesh_for_read() const
bool has_mesh() const
bool is_adaptive
Definition: BKE_subdiv.h:76
eSubdivFVarLinearInterpolation fvar_linear_interpolation
Definition: BKE_subdiv.h:87
bool use_creases
Definition: BKE_subdiv.h:84
eSubdivVtxBoundaryInterpolation vtx_boundary_interpolation
Definition: BKE_subdiv.h:86
Compact definition of a node socket.
Definition: BKE_node.h:95
Defines a node type.
Definition: BKE_node.h:221
NodeGeometryExecFunction geometry_node_execute
Definition: BKE_node.h:327