Blender V5.3
node_geo_volume_to_mesh.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#ifdef WITH_OPENVDB
6# include <openvdb/tools/GridTransformer.h>
7# include <openvdb/tools/VolumeToMesh.h>
8#endif
9
10#include "node_geometry_util.hh"
11
12#include "BKE_material.hh"
13#include "BKE_mesh.hh"
14#include "BKE_volume.hh"
15#include "BKE_volume_grid.hh"
16#include "BKE_volume_to_mesh.hh"
17
19#include "GEO_randomize.hh"
20
22
24
27 "GRID",
28 0,
30 N_("Use resolution of the volume grid")},
32 "VOXEL_AMOUNT",
33 0,
35 N_("Desired number of voxels along one axis")},
37 "VOXEL_SIZE",
38 0,
40 N_("Desired voxel side length")},
41 {0, nullptr, 0, nullptr, nullptr},
42};
43
45{
46 b.add_input<decl::Geometry>("Volume"_ustr)
47 .supported_type(GeometryComponent::Type::Volume)
49 .is_default_link_socket()
50 .description("Volume to convert to a mesh");
51 b.add_input<decl::Menu>("Resolution Mode"_ustr)
52 .static_items(resolution_mode_items)
54 .description("How the voxel size is specified")
55 .translation_context(BLT_I18NCONTEXT_COUNTABLE);
56 b.add_input<decl::Float>("Voxel Size"_ustr)
57 .default_value(0.3f)
58 .min(0.01f)
60 .usage_by_single_menu(VOLUME_TO_MESH_RESOLUTION_MODE_VOXEL_SIZE);
61 b.add_input<decl::Float>("Voxel Amount"_ustr)
62 .default_value(64.0f)
63 .min(0.0f)
65 b.add_input<decl::Float>("Threshold"_ustr)
66 .default_value(0.1f)
67 .description("Values larger than the threshold are inside the generated mesh");
68 b.add_input<decl::Float>("Adaptivity"_ustr).min(0.0f).max(1.0f).subtype(PROP_FACTOR);
69 b.add_output<decl::Geometry>("Mesh"_ustr);
70}
71
72static void node_init(bNodeTree * /*tree*/, bNode *node)
73{
74 /* Still used for forward compatibility. */
75 node->storage = MEM_new<NodeGeometryVolumeToMesh>(__func__);
76}
77
78#ifdef WITH_OPENVDB
79
80static bke::VolumeToMeshResolution get_resolution_param(const GeoNodeExecParams &params)
81{
83 resolution.mode = params.get_input<VolumeToMeshResolutionMode>("Resolution Mode"_ustr);
85 resolution.settings.voxel_amount = std::max(params.get_input<float>("Voxel Amount"_ustr),
86 0.0f);
87 }
88 else if (resolution.mode == VOLUME_TO_MESH_RESOLUTION_MODE_VOXEL_SIZE) {
89 resolution.settings.voxel_size = std::max(params.get_input<float>("Voxel Size"_ustr), 0.0f);
90 }
91
92 return resolution;
93}
94
95static Mesh *create_mesh_from_volume_grids(Span<const openvdb::GridBase *> grids,
96 GeoNodeExecParams &params,
97 const float threshold,
98 const float adaptivity,
99 const bke::VolumeToMeshResolution &resolution)
100{
101 Array<bke::VolumeToMeshDataResult> mesh_data(grids.size());
102 for (const int i : grids.index_range()) {
103 bke::VolumeToMeshDataResult &result = mesh_data[i];
104 result = bke::volume_to_mesh_data(*grids[i], resolution, threshold, adaptivity);
105 if (!result.error.empty()) {
106 params.error_message_add(NodeWarningType::Error, result.error);
107 }
108 }
109
110 int vert_offset = 0;
111 int face_offset = 0;
112 int loop_offset = 0;
113 Array<int> vert_offsets(mesh_data.size());
114 Array<int> face_offsets(mesh_data.size());
115 Array<int> loop_offsets(mesh_data.size());
116 for (const int i : grids.index_range()) {
117 const bke::OpenVDBMeshData &data = mesh_data[i].data;
118 vert_offsets[i] = vert_offset;
119 face_offsets[i] = face_offset;
120 loop_offsets[i] = loop_offset;
121 vert_offset += data.verts.size();
122 face_offset += (data.tris.size() + data.quads.size());
123 loop_offset += (3 * data.tris.size() + 4 * data.quads.size());
124 }
125
126 Mesh *mesh = BKE_mesh_new_nomain(vert_offset, 0, face_offset, loop_offset);
128 MutableSpan<float3> positions = mesh->vert_positions_for_write();
129 MutableSpan<int> dst_face_offsets = mesh->face_offsets_for_write();
130 MutableSpan<int> corner_verts = mesh->corner_verts_for_write();
131
132 for (const int i : grids.index_range()) {
133 const bke::OpenVDBMeshData &data = mesh_data[i].data;
134 bke::fill_mesh_from_openvdb_data(data.verts,
135 data.tris,
136 data.quads,
137 vert_offsets[i],
138 face_offsets[i],
139 loop_offsets[i],
140 positions,
141 dst_face_offsets,
142 corner_verts);
143 }
144
145 bke::mesh_calc_edges(*mesh, false, false);
146 bke::mesh_smooth_set(*mesh, false);
147
148 mesh->tag_overlapping_none();
149
151
152 return mesh;
153}
154
155static Mesh *create_mesh_from_volume(GeometrySet &geometry_set, GeoNodeExecParams &params)
156{
157 const Volume *volume = geometry_set.get_volume();
158 if (volume == nullptr) {
159 return nullptr;
160 }
161
162 const bke::VolumeToMeshResolution resolution = get_resolution_param(params);
163
164 if (resolution.mode == VOLUME_TO_MESH_RESOLUTION_MODE_VOXEL_SIZE &&
165 resolution.settings.voxel_size <= 0.0f)
166 {
167 return nullptr;
168 }
169 if (resolution.mode == VOLUME_TO_MESH_RESOLUTION_MODE_VOXEL_AMOUNT &&
170 resolution.settings.voxel_amount <= 0)
171 {
172 return nullptr;
173 }
174
175 BKE_volume_load(volume, params.bmain());
176
177 Vector<bke::VolumeTreeAccessToken> tree_tokens;
178 Vector<const openvdb::GridBase *> grids;
179 for (const int i : IndexRange(BKE_volume_num_grids(volume))) {
180 const bke::VolumeGridData *volume_grid = BKE_volume_grid_get(volume, i);
181 tree_tokens.append_as();
182 grids.append(&volume_grid->grid(tree_tokens.last()));
183 }
184
185 if (grids.is_empty()) {
186 return nullptr;
187 }
188
189 return create_mesh_from_volume_grids(grids,
190 params,
191 params.get_input<float>("Threshold"_ustr),
192 params.get_input<float>("Adaptivity"_ustr),
193 resolution);
194}
195
196#endif /* WITH_OPENVDB */
197
199{
200#ifdef WITH_OPENVDB
201 GeometrySet geometry_set = params.extract_input<GeometrySet>("Volume"_ustr);
202 geometry::foreach_real_geometry(geometry_set, [&](GeometrySet &geometry_set) {
203 Mesh *mesh = create_mesh_from_volume(geometry_set, params);
204 geometry_set.replace_mesh(mesh);
206 });
207 params.set_output("Mesh"_ustr, std::move(geometry_set));
208#else
210#endif
211}
212
213static void node_register()
214{
215 static bke::bNodeType ntype;
216
217 geo_node_type_base(&ntype, "GeometryNodeVolumeToMesh"_ustr, GEO_NODE_VOLUME_TO_MESH);
218 ntype.ui_name = "Volume to Mesh";
219 ntype.ui_description = "Generate a mesh on the \"surface\" of a volume";
220 ntype.enum_name_legacy = "VOLUME_TO_MESH";
222 ntype.declare = node_declare;
224 ntype, "NodeGeometryVolumeToMesh", node_free_standard_storage, node_copy_standard_storage);
226 ntype.initfunc = node_init;
229}
231
232} // namespace blender::nodes::node_geo_volume_to_mesh_cc
General operations, lookup, etc. for materials.
#define NODE_STORAGE_FUNCS(StorageT)
Definition BKE_node.hh:1353
#define NODE_CLASS_GEOMETRY
Definition BKE_node.hh:492
#define GEO_NODE_VOLUME_TO_MESH
Volume data-block.
#define CTX_N_(context, msgid)
#define BLT_I18NCONTEXT_ID_ID
#define BLT_I18NCONTEXT_COUNTABLE
#define NOD_REGISTER_NODE(REGISTER_FUNC)
constexpr int64_t size() const
Definition BLI_span.hh:256
constexpr IndexRange index_range() const
Definition BLI_span.hh:405
constexpr IndexRange()=default
T extract_input(const UString identifier)
T get_input(const UString identifier) const
void set_output(const UString identifier, T &&value)
std::optional< std::string > translation_context
void node_register_type(bNodeType &ntype)
Definition node.cc:2920
void mesh_smooth_set(Mesh &mesh, bool use_smooth, bool keep_sharp_edges=false)
void mesh_calc_edges(Mesh &mesh, bool keep_existing_edges, bool select_new_edges)
void node_type_storage(bNodeType &ntype, std::optional< StringRefNull > storagename, void(*freefunc)(bNode *node), void(*copyfunc)(bNodeTree *dest_ntree, bNode *dest_node, const bNode *src_node))
Definition node.cc:6166
void foreach_real_geometry(bke::GeometrySet &geometry, FunctionRef< void(bke::GeometrySet &geometry_set)> fn)
void debug_randomize_mesh_order(Mesh *mesh)
Definition randomize.cc:305
static void node_declare(NodeDeclarationBuilder &b)
static void node_geo_exec(GeoNodeExecParams params)
static void node_init(bNodeTree *, bNode *node)
void node_geo_exec_with_missing_openvdb(GeoNodeExecParams &params)
int BKE_volume_num_grids(const Volume *volume)
const bke::VolumeGridData * BKE_volume_grid_get(const Volume *volume, int grid_index)
BMesh const char void * data
Mesh * BKE_mesh_new_nomain(int verts_num, int edges_num, int faces_num, int corners_num)
void BKE_id_material_eval_ensure_default_slot(ID *id)
bool BKE_volume_load(const Volume *volume, const Main *bmain)
@ VOLUME_TO_MESH_RESOLUTION_MODE_VOXEL_AMOUNT
@ VOLUME_TO_MESH_RESOLUTION_MODE_VOXEL_SIZE
@ VOLUME_TO_MESH_RESOLUTION_MODE_GRID
void node_copy_standard_storage(bNodeTree *, bNode *dest_node, const bNode *src_node)
Definition node_util.cc:57
void node_free_standard_storage(bNode *node)
Definition node_util.cc:45
@ PROP_FACTOR
Definition RNA_types.hh:308
@ PROP_DISTANCE
Definition RNA_types.hh:313
void geo_node_type_base(bke::bNodeType *ntype, UString idname, const std::optional< int16_t > legacy_type)
#define min(a, b)
Definition sort.cc:38
void keep_only(Span< GeometryComponent::Type > component_types)
void replace_mesh(Mesh *mesh, GeometryOwnershipType ownership=GeometryOwnershipType::Owned)
static constexpr int _180
Definition BKE_node.hh:247
union blender::bke::VolumeToMeshResolution::@031223152017106026267031022005313173146132011170 settings
Defines a node type.
Definition BKE_node.hh:265
std::string ui_description
Definition BKE_node.hh:271
NodeGeometryExecFunction geometry_node_execute
Definition BKE_node.hh:385
void(*) initfunc(bNodeTree *ntree, bNode *node)
Definition BKE_node.hh:320
const char * enum_name_legacy
Definition BKE_node.hh:274
NodeDeclareFunction declare
Definition BKE_node.hh:393
#define N_(msgid)