Geometry Nodes: Only calculate mesh to volume bounds when necessary
In "size" voxel resolution mode, calculating the bounds of the mesh to volume node's input mesh isn't necessary. For high poly this can take a few milliseconds, so this commit skips the calculation unless we need it for the "Amount" mode. Differential Revision: https://developer.blender.org/D15324
This commit is contained in:
parent
4593fb52cf
commit
0ea282f746
|
@ -1,6 +1,7 @@
|
|||
/* SPDX-License-Identifier: GPL-2.0-or-later */
|
||||
|
||||
#include "BLI_float4x4.hh"
|
||||
#include "BLI_function_ref.hh"
|
||||
#include "BLI_string_ref.hh"
|
||||
|
||||
#include "DNA_mesh_types.h"
|
||||
|
@ -28,9 +29,13 @@ struct MeshToVolumeResolution {
|
|||
};
|
||||
|
||||
#ifdef WITH_OPENVDB
|
||||
|
||||
/**
|
||||
* \param bounds_fn: Return the bounds of the mesh positions,
|
||||
* used for deciding the voxel size in "Amount" mode.
|
||||
*/
|
||||
float volume_compute_voxel_size(const Depsgraph *depsgraph,
|
||||
const float3 &bb_min,
|
||||
const float3 &bb_max,
|
||||
FunctionRef<void(float3 &r_min, float3 &r_max)> bounds_fn,
|
||||
const MeshToVolumeResolution resolution,
|
||||
float exterior_band_width,
|
||||
const float4x4 &transform);
|
||||
|
|
|
@ -64,8 +64,7 @@ void OpenVDBMeshAdapter::getIndexSpacePoint(size_t polygon_index,
|
|||
}
|
||||
|
||||
float volume_compute_voxel_size(const Depsgraph *depsgraph,
|
||||
const float3 &bb_min,
|
||||
const float3 &bb_max,
|
||||
FunctionRef<void(float3 &r_min, float3 &r_max)> bounds_fn,
|
||||
const MeshToVolumeResolution res,
|
||||
const float exterior_band_width,
|
||||
const float4x4 &transform)
|
||||
|
@ -81,6 +80,11 @@ float volume_compute_voxel_size(const Depsgraph *depsgraph,
|
|||
if (res.settings.voxel_amount <= 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
float3 bb_min;
|
||||
float3 bb_max;
|
||||
bounds_fn(bb_min, bb_max);
|
||||
|
||||
/* Compute the voxel size based on the desired number of voxels and the approximated bounding
|
||||
* box of the volume. */
|
||||
const float diagonal = math::distance(transform * bb_max, transform * bb_min);
|
||||
|
|
|
@ -133,7 +133,6 @@ static Volume *mesh_to_volume(ModifierData *md,
|
|||
|
||||
const float4x4 mesh_to_own_object_space_transform = float4x4(ctx->object->imat) *
|
||||
float4x4(object_to_convert->obmat);
|
||||
const BoundBox *bb = BKE_object_boundbox_get(mvmd->object);
|
||||
geometry::MeshToVolumeResolution resolution;
|
||||
resolution.mode = (MeshToVolumeModifierResolutionMode)mvmd->resolution_mode;
|
||||
if (resolution.mode == MESH_TO_VOLUME_RESOLUTION_MODE_VOXEL_AMOUNT) {
|
||||
|
@ -149,9 +148,14 @@ static Volume *mesh_to_volume(ModifierData *md,
|
|||
}
|
||||
}
|
||||
|
||||
auto bounds_fn = [&](float3 &r_min, float3 &r_max) {
|
||||
const BoundBox *bb = BKE_object_boundbox_get(mvmd->object);
|
||||
r_min = bb->vec[0];
|
||||
r_max = bb->vec[6];
|
||||
};
|
||||
|
||||
const float voxel_size = geometry::volume_compute_voxel_size(ctx->depsgraph,
|
||||
bb->vec[0],
|
||||
bb->vec[6],
|
||||
bounds_fn,
|
||||
resolution,
|
||||
mvmd->exterior_band_width,
|
||||
mesh_to_own_object_space_transform);
|
||||
|
|
|
@ -106,18 +106,22 @@ static Volume *create_volume_from_mesh(const Mesh &mesh, GeoNodeExecParams ¶
|
|||
}
|
||||
}
|
||||
|
||||
float3 min, max;
|
||||
INIT_MINMAX(min, max);
|
||||
if (!BKE_mesh_wrapper_minmax(&mesh, min, max)) {
|
||||
min = float3(-1.0f);
|
||||
max = float3(1.0f);
|
||||
if (mesh.totvert == 0 || mesh.totpoly == 0) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
const float4x4 mesh_to_volume_space_transform = float4x4::identity();
|
||||
|
||||
auto bounds_fn = [&](float3 &r_min, float3 &r_max) {
|
||||
float3 min{std::numeric_limits<float>::max()};
|
||||
float3 max{-std::numeric_limits<float>::max()};
|
||||
BKE_mesh_wrapper_minmax(&mesh, min, max);
|
||||
r_min = min;
|
||||
r_max = max;
|
||||
};
|
||||
|
||||
const float voxel_size = geometry::volume_compute_voxel_size(params.depsgraph(),
|
||||
min,
|
||||
max,
|
||||
bounds_fn,
|
||||
resolution,
|
||||
exterior_band_width,
|
||||
mesh_to_volume_space_transform);
|
||||
|
|
Loading…
Reference in New Issue