Blender  V2.93
node_geo_subdivision_surface.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 "BKE_mesh.h"
18 #include "BKE_subdiv.h"
19 #include "BKE_subdiv_mesh.h"
20 
21 #include "UI_interface.h"
22 #include "UI_resources.h"
23 
24 #include "node_geometry_util.hh"
25 
27  {SOCK_GEOMETRY, N_("Geometry")},
28  {SOCK_INT, N_("Level"), 1, 0, 0, 0, 0, 6},
29  {SOCK_BOOLEAN, N_("Use Creases")},
30  {SOCK_BOOLEAN, N_("Boundary Smooth"), true},
31  {SOCK_BOOLEAN, N_("Smooth UVs")},
32  {-1, ""},
33 };
34 
36  {SOCK_GEOMETRY, N_("Geometry")},
37  {-1, ""},
38 };
39 
40 namespace blender::nodes {
42 {
43  GeometrySet geometry_set = params.extract_input<GeometrySet>("Geometry");
44 
45  geometry_set = geometry_set_realize_instances(geometry_set);
46 
47  if (!geometry_set.has_mesh()) {
48  params.set_output("Geometry", geometry_set);
49  return;
50  }
51 
52 #ifndef WITH_OPENSUBDIV
53  params.error_message_add(NodeWarningType::Error,
54  TIP_("Disabled, Blender was compiled without OpenSubdiv"));
55 #else
56  const int subdiv_level = clamp_i(params.extract_input<int>("Level"), 0, 30);
57 
58  /* Only process subdivision if level is greater than 0. */
59  if (subdiv_level == 0) {
60  params.set_output("Geometry", std::move(geometry_set));
61  return;
62  }
63 
64  const bool use_crease = params.extract_input<bool>("Use Creases");
65  const bool boundary_smooth = params.extract_input<bool>("Boundary Smooth");
66  const bool smooth_uvs = params.extract_input<bool>("Smooth UVs");
67  const Mesh *mesh_in = geometry_set.get_mesh_for_read();
68 
69  /* Initialize mesh settings. */
70  SubdivToMeshSettings mesh_settings;
71  mesh_settings.resolution = (1 << subdiv_level) + 1;
72  mesh_settings.use_optimal_display = false;
73 
74  /* Initialize subdivision settings. */
75  SubdivSettings subdiv_settings;
76  subdiv_settings.is_simple = false;
77  subdiv_settings.is_adaptive = false;
78  subdiv_settings.use_creases = use_crease;
79  subdiv_settings.level = subdiv_level;
80 
82  !boundary_smooth);
84  smooth_uvs);
85 
86  /* Apply subdivision to mesh. */
87  Subdiv *subdiv = BKE_subdiv_update_from_mesh(nullptr, &subdiv_settings, mesh_in);
88 
89  /* In case of bad topology, skip to input mesh. */
90  if (subdiv == nullptr) {
91  params.set_output("Geometry", std::move(geometry_set));
92  return;
93  }
94 
95  Mesh *mesh_out = BKE_subdiv_to_mesh(subdiv, &mesh_settings, mesh_in);
96  BKE_mesh_calc_normals(mesh_out);
97 
98  MeshComponent &mesh_component = geometry_set.get_component_for_write<MeshComponent>();
99  mesh_component.replace_mesh_but_keep_vertex_group_names(mesh_out);
100 
101  // BKE_subdiv_stats_print(&subdiv->stats);
102  BKE_subdiv_free(subdiv);
103 
104 #endif
105 
106  params.set_output("Geometry", std::move(geometry_set));
107 }
108 
109 } // namespace blender::nodes
110 
112 {
113  static bNodeType ntype;
114 
116  &ntype, GEO_NODE_SUBDIVISION_SURFACE, "Subdivision Surface", NODE_CLASS_GEOMETRY, 0);
120  nodeRegisterType(&ntype);
121 }
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_BOOLEAN
@ 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 GEO_NODE_SUBDIVISION_SURFACE
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_subdivision_surface_exec(GeoNodeExecParams params)
static bNodeSocketTemplate geo_node_subdivision_surface_out[]
static bNodeSocketTemplate geo_node_subdivision_surface_in[]
void register_node_type_geo_subdivision_surface()
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