Changeset View
Changeset View
Standalone View
Standalone View
source/blender/nodes/geometry/nodes/node_geo_subdivision_surface.cc
| /* SPDX-License-Identifier: GPL-2.0-or-later */ | /* SPDX-License-Identifier: GPL-2.0-or-later */ | ||||
| #include "BLI_task.hh" | |||||
| #include "DNA_mesh_types.h" | #include "DNA_mesh_types.h" | ||||
| #include "DNA_meshdata_types.h" | #include "DNA_meshdata_types.h" | ||||
| #include "DNA_modifier_types.h" | #include "DNA_modifier_types.h" | ||||
| #include "BKE_mesh.h" | #include "BKE_mesh.h" | ||||
| #include "BKE_subdiv.h" | #include "BKE_subdiv.h" | ||||
| #include "BKE_subdiv_mesh.h" | #include "BKE_subdiv_mesh.h" | ||||
| #include "UI_interface.h" | #include "UI_interface.h" | ||||
| #include "UI_resources.h" | #include "UI_resources.h" | ||||
| #include "node_geometry_util.hh" | #include "node_geometry_util.hh" | ||||
| namespace blender::nodes::node_geo_subdivision_surface_cc { | namespace blender::nodes::node_geo_subdivision_surface_cc { | ||||
| NODE_STORAGE_FUNCS(NodeGeometrySubdivisionSurface) | NODE_STORAGE_FUNCS(NodeGeometrySubdivisionSurface) | ||||
| static void node_declare(NodeDeclarationBuilder &b) | static void node_declare(NodeDeclarationBuilder &b) | ||||
| { | { | ||||
| b.add_input<decl::Geometry>(N_("Mesh")).supported_type(GEO_COMPONENT_TYPE_MESH); | b.add_input<decl::Geometry>(N_("Mesh")).supported_type(GEO_COMPONENT_TYPE_MESH); | ||||
| b.add_input<decl::Int>(N_("Level")).default_value(1).min(0).max(6); | b.add_input<decl::Int>(N_("Level")).default_value(1).min(0).max(6); | ||||
| b.add_input<decl::Float>(N_("Crease")) | b.add_input<decl::Float>(N_("Edge Crease")) | ||||
| .default_value(0.0f) | |||||
| .min(0.0f) | |||||
| .max(1.0f) | |||||
| .supports_field() | |||||
| .subtype(PROP_FACTOR); | |||||
| b.add_input<decl::Float>(N_("Vertex Crease")) | |||||
Blendify: This change will require versioning to maintain compatibility with older files. See usage of… | |||||
| .default_value(0.0f) | .default_value(0.0f) | ||||
| .min(0.0f) | .min(0.0f) | ||||
| .max(1.0f) | .max(1.0f) | ||||
| .supports_field() | .supports_field() | ||||
| .subtype(PROP_FACTOR); | .subtype(PROP_FACTOR); | ||||
| b.add_output<decl::Geometry>(N_("Mesh")); | b.add_output<decl::Geometry>(N_("Mesh")); | ||||
| } | } | ||||
| static void node_layout(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr) | static void node_layout(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr) | ||||
| { | { | ||||
| uiItemR(layout, ptr, "uv_smooth", 0, "", ICON_NONE); | uiItemR(layout, ptr, "uv_smooth", 0, "", ICON_NONE); | ||||
| uiItemR(layout, ptr, "boundary_smooth", 0, "", ICON_NONE); | uiItemR(layout, ptr, "boundary_smooth", 0, "", ICON_NONE); | ||||
| } | } | ||||
| static void node_init(bNodeTree *UNUSED(ntree), bNode *node) | static void node_init(bNodeTree *UNUSED(ntree), bNode *node) | ||||
| { | { | ||||
| NodeGeometrySubdivisionSurface *data = MEM_cnew<NodeGeometrySubdivisionSurface>(__func__); | NodeGeometrySubdivisionSurface *data = MEM_cnew<NodeGeometrySubdivisionSurface>(__func__); | ||||
| data->uv_smooth = SUBSURF_UV_SMOOTH_PRESERVE_BOUNDARIES; | data->uv_smooth = SUBSURF_UV_SMOOTH_PRESERVE_BOUNDARIES; | ||||
| data->boundary_smooth = SUBSURF_BOUNDARY_SMOOTH_ALL; | data->boundary_smooth = SUBSURF_BOUNDARY_SMOOTH_ALL; | ||||
| node->storage = data; | node->storage = data; | ||||
| } | } | ||||
| #ifdef WITH_OPENSUBDIV | |||||
| static void materialize_and_clamp_creases(const VArray<float> &crease_varray, | |||||
| MutableSpan<float> creases) | |||||
| { | |||||
| threading::parallel_for(creases.index_range(), 1024, [&](IndexRange range) { | |||||
| crease_varray.materialize(range, creases); | |||||
| for (const int i : range) { | |||||
| creases[i] = std::clamp(creases[i], 0.0f, 1.0f); | |||||
| } | |||||
| }); | |||||
| } | |||||
| static void write_vertex_creases(Mesh &mesh, const VArray<float> &crease_varray) | |||||
| { | |||||
| float *crease; | |||||
Not Done Inline ActionsNodes should generally only do a single field evaluation if possible, in case any of the field network is shared between the inputs. HooglyBoogly: Nodes should generally only do a single field evaluation if possible, in case any of the field… | |||||
Done Inline ActionsIs this an information for other future patches, or a requested change? For the latter, how would it work if the number of vertices and do not match (which is most likely)? kevindietrich: Is this an information for other future patches, or a requested change? For the latter, how… | |||||
Not Done Inline ActionsOh right, sorry, I missed that this had to evaluate on two separate domains. HooglyBoogly: Oh right, sorry, I missed that this had to evaluate on two separate domains. | |||||
| if (CustomData_has_layer(&mesh.vdata, CD_CREASE)) { | |||||
| crease = static_cast<float *>(CustomData_get_layer(&mesh.vdata, CD_CREASE)); | |||||
| } | |||||
| else { | |||||
| crease = static_cast<float *>( | |||||
| CustomData_add_layer(&mesh.vdata, CD_CREASE, CD_DEFAULT, nullptr, mesh.totvert)); | |||||
| } | |||||
| materialize_and_clamp_creases(crease_varray, {crease, mesh.totvert}); | |||||
| } | |||||
| static void write_edge_creases(MeshComponent &mesh, const VArray<float> &crease_varray) | |||||
| { | |||||
| OutputAttribute_Typed<float> attribute = mesh.attribute_try_get_for_output_only<float>( | |||||
| "crease", ATTR_DOMAIN_EDGE); | |||||
| materialize_and_clamp_creases(crease_varray, attribute.as_span()); | |||||
| attribute.save(); | |||||
| } | |||||
| static bool varray_is_nonzero(const VArray<float> &varray) | |||||
| { | |||||
| return !(varray.is_single() && varray.get_internal_single() == 0.0f); | |||||
| } | |||||
| #endif | |||||
| static void node_geo_exec(GeoNodeExecParams params) | static void node_geo_exec(GeoNodeExecParams params) | ||||
| { | { | ||||
| GeometrySet geometry_set = params.extract_input<GeometrySet>("Mesh"); | GeometrySet geometry_set = params.extract_input<GeometrySet>("Mesh"); | ||||
| #ifndef WITH_OPENSUBDIV | #ifndef WITH_OPENSUBDIV | ||||
| params.error_message_add(NodeWarningType::Error, | params.error_message_add(NodeWarningType::Error, | ||||
| TIP_("Disabled, Blender was compiled without OpenSubdiv")); | TIP_("Disabled, Blender was compiled without OpenSubdiv")); | ||||
| #else | #else | ||||
| Field<float> crease_field = params.extract_input<Field<float>>("Crease"); | Field<float> edge_crease_field = params.extract_input<Field<float>>("Edge Crease"); | ||||
| Field<float> vertex_crease_field = params.extract_input<Field<float>>("Vertex Crease"); | |||||
| const NodeGeometrySubdivisionSurface &storage = node_storage(params.node()); | const NodeGeometrySubdivisionSurface &storage = node_storage(params.node()); | ||||
| const int uv_smooth = storage.uv_smooth; | const int uv_smooth = storage.uv_smooth; | ||||
| const int boundary_smooth = storage.boundary_smooth; | const int boundary_smooth = storage.boundary_smooth; | ||||
| const int subdiv_level = clamp_i(params.extract_input<int>("Level"), 0, 30); | const int subdiv_level = clamp_i(params.extract_input<int>("Level"), 0, 30); | ||||
| /* Only process subdivision if level is greater than 0. */ | /* Only process subdivision if level is greater than 0. */ | ||||
| if (subdiv_level == 0) { | if (subdiv_level == 0) { | ||||
| params.set_output("Mesh", std::move(geometry_set)); | params.set_output("Mesh", std::move(geometry_set)); | ||||
| return; | return; | ||||
| } | } | ||||
| geometry_set.modify_geometry_sets([&](GeometrySet &geometry_set) { | geometry_set.modify_geometry_sets([&](GeometrySet &geometry_set) { | ||||
| if (!geometry_set.has_mesh()) { | if (!geometry_set.has_mesh()) { | ||||
| return; | return; | ||||
| } | } | ||||
| MeshComponent &mesh_component = geometry_set.get_component_for_write<MeshComponent>(); | const MeshComponent &mesh_component = *geometry_set.get_component_for_read<MeshComponent>(); | ||||
| AttributeDomain domain = ATTR_DOMAIN_EDGE; | const int verts_num = mesh_component.attribute_domain_size(ATTR_DOMAIN_POINT); | ||||
| GeometryComponentFieldContext field_context{mesh_component, domain}; | const int edges_num = mesh_component.attribute_domain_size(ATTR_DOMAIN_EDGE); | ||||
| const int domain_size = mesh_component.attribute_domain_size(domain); | if (verts_num == 0 || edges_num == 0) { | ||||
| if (domain_size == 0) { | |||||
| return; | return; | ||||
| } | } | ||||
| FieldEvaluator evaluator(field_context, domain_size); | GeometryComponentFieldContext point_context{mesh_component, ATTR_DOMAIN_POINT}; | ||||
| evaluator.add(crease_field); | FieldEvaluator point_evaluator(point_context, verts_num); | ||||
| evaluator.evaluate(); | point_evaluator.add(vertex_crease_field); | ||||
| const VArray<float> &creases = evaluator.get_evaluated<float>(0); | point_evaluator.evaluate(); | ||||
| const VArray<float> vertex_creases = point_evaluator.get_evaluated<float>(0); | |||||
| OutputAttribute_Typed<float> crease = mesh_component.attribute_try_get_for_output_only<float>( | |||||
| "crease", domain); | GeometryComponentFieldContext edge_context{mesh_component, ATTR_DOMAIN_EDGE}; | ||||
| MutableSpan<float> crease_span = crease.as_span(); | FieldEvaluator edge_evaluator(edge_context, edges_num); | ||||
| for (auto i : creases.index_range()) { | edge_evaluator.add(edge_crease_field); | ||||
| crease_span[i] = std::clamp(creases[i], 0.0f, 1.0f); | edge_evaluator.evaluate(); | ||||
| const VArray<float> edge_creases = edge_evaluator.get_evaluated<float>(0); | |||||
| const bool use_creases = varray_is_nonzero(vertex_creases) || varray_is_nonzero(edge_creases); | |||||
| if (use_creases) { | |||||
| write_vertex_creases(*geometry_set.get_mesh_for_write(), vertex_creases); | |||||
| write_edge_creases(geometry_set.get_component_for_write<MeshComponent>(), edge_creases); | |||||
| } | } | ||||
| crease.save(); | |||||
| /* Initialize mesh settings. */ | /* Initialize mesh settings. */ | ||||
| SubdivToMeshSettings mesh_settings; | SubdivToMeshSettings mesh_settings; | ||||
| mesh_settings.resolution = (1 << subdiv_level) + 1; | mesh_settings.resolution = (1 << subdiv_level) + 1; | ||||
| mesh_settings.use_optimal_display = false; | mesh_settings.use_optimal_display = false; | ||||
| /* Initialize subdivision settings. */ | /* Initialize subdivision settings. */ | ||||
| SubdivSettings subdiv_settings; | SubdivSettings subdiv_settings; | ||||
| subdiv_settings.is_simple = false; | subdiv_settings.is_simple = false; | ||||
| subdiv_settings.is_adaptive = false; | subdiv_settings.is_adaptive = false; | ||||
| subdiv_settings.use_creases = !(creases.is_single() && creases.get_internal_single() == 0.0f); | subdiv_settings.use_creases = use_creases; | ||||
| subdiv_settings.level = subdiv_level; | subdiv_settings.level = subdiv_level; | ||||
| subdiv_settings.vtx_boundary_interpolation = | subdiv_settings.vtx_boundary_interpolation = | ||||
| BKE_subdiv_vtx_boundary_interpolation_from_subsurf(boundary_smooth); | BKE_subdiv_vtx_boundary_interpolation_from_subsurf(boundary_smooth); | ||||
| subdiv_settings.fvar_linear_interpolation = BKE_subdiv_fvar_interpolation_from_uv_smooth( | subdiv_settings.fvar_linear_interpolation = BKE_subdiv_fvar_interpolation_from_uv_smooth( | ||||
| uv_smooth); | uv_smooth); | ||||
| Mesh *mesh_in = mesh_component.get_for_write(); | const Mesh &mesh_in = *geometry_set.get_mesh_for_read(); | ||||
| /* Apply subdivision to mesh. */ | /* Apply subdivision to mesh. */ | ||||
| Subdiv *subdiv = BKE_subdiv_update_from_mesh(nullptr, &subdiv_settings, mesh_in); | Subdiv *subdiv = BKE_subdiv_update_from_mesh(nullptr, &subdiv_settings, &mesh_in); | ||||
| /* In case of bad topology, skip to input mesh. */ | /* In case of bad topology, skip to input mesh. */ | ||||
| if (subdiv == nullptr) { | if (subdiv == nullptr) { | ||||
| return; | return; | ||||
| } | } | ||||
| Mesh *mesh_out = BKE_subdiv_to_mesh(subdiv, &mesh_settings, mesh_in); | Mesh *mesh_out = BKE_subdiv_to_mesh(subdiv, &mesh_settings, &mesh_in); | ||||
| mesh_component.replace(mesh_out); | geometry_set.replace_mesh(mesh_out); | ||||
| BKE_subdiv_free(subdiv); | BKE_subdiv_free(subdiv); | ||||
| }); | }); | ||||
| #endif | #endif | ||||
| params.set_output("Mesh", std::move(geometry_set)); | params.set_output("Mesh", std::move(geometry_set)); | ||||
| } | } | ||||
| } // namespace blender::nodes::node_geo_subdivision_surface_cc | } // namespace blender::nodes::node_geo_subdivision_surface_cc | ||||
| Show All 20 Lines | |||||
This change will require versioning to maintain compatibility with older files. See usage of version_node_input_socket_name()