From 8828c6f8a77bf026ea40a5950db8ddf83cdf7afa Mon Sep 17 00:00:00 2001 From: Erik Abrahamsson Date: Mon, 24 Apr 2023 03:47:05 +0200 Subject: [PATCH 1/3] Mesh to Volume: Changes in Fog Volume --- source/blender/geometry/GEO_mesh_to_volume.hh | 11 +- .../blender/geometry/intern/mesh_to_volume.cc | 132 +++++++----------- source/blender/makesdna/DNA_modifier_types.h | 7 - source/blender/makesrna/intern/rna_modifier.c | 14 +- .../modifiers/intern/MOD_mesh_to_volume.cc | 24 +--- .../nodes/node_geo_mesh_to_sdf_volume.cc | 10 +- .../geometry/nodes/node_geo_mesh_to_volume.cc | 26 +--- 7 files changed, 79 insertions(+), 145 deletions(-) diff --git a/source/blender/geometry/GEO_mesh_to_volume.hh b/source/blender/geometry/GEO_mesh_to_volume.hh index 393a9cc68fb..7cf2017e2bd 100644 --- a/source/blender/geometry/GEO_mesh_to_volume.hh +++ b/source/blender/geometry/GEO_mesh_to_volume.hh @@ -47,14 +47,17 @@ VolumeGrid *fog_volume_grid_add_from_mesh(Volume *volume, const Mesh *mesh, const float4x4 &mesh_to_volume_space_transform, float voxel_size, - bool fill_volume, - float exterior_band_width, float interior_band_width, float density); /** * Add a new SDF VolumeGrid to the Volume by converting the supplied mesh. */ -VolumeGrid *sdf_volume_grid_add_from_mesh( - Volume *volume, StringRefNull name, const Mesh &mesh, float voxel_size, float half_band_width); +VolumeGrid *sdf_volume_grid_add_from_mesh(Volume *volume, + StringRefNull name, + const Mesh *mesh, + const float4x4 &mesh_to_volume_space_transform, + float voxel_size, + bool fill_interior, + float half_band_width); #endif } // namespace blender::geometry diff --git a/source/blender/geometry/intern/mesh_to_volume.cc b/source/blender/geometry/intern/mesh_to_volume.cc index dd6fd27d6c5..e847d6b2d99 100644 --- a/source/blender/geometry/intern/mesh_to_volume.cc +++ b/source/blender/geometry/intern/mesh_to_volume.cc @@ -12,6 +12,7 @@ #ifdef WITH_OPENVDB # include # include +# include # include namespace blender::geometry { @@ -92,22 +93,19 @@ float volume_compute_voxel_size(const Depsgraph *depsgraph, * box of the volume. */ const float diagonal = math::distance(math::transform_point(transform, bb_max), math::transform_point(transform, bb_min)); - const float approximate_volume_side_length = diagonal + exterior_band_width * 2.0f; - const float voxel_size = approximate_volume_side_length / res.settings.voxel_amount / - volume_simplify; - return voxel_size; + const float voxel_size = + (diagonal / std::max(1.0f, float(res.settings.voxel_amount) - 2.0f * exterior_band_width)); + return voxel_size / volume_simplify; } static openvdb::FloatGrid::Ptr mesh_to_fog_volume_grid( const Mesh *mesh, const float4x4 &mesh_to_volume_space_transform, const float voxel_size, - const bool fill_volume, - const float exterior_band_width, const float interior_band_width, const float density) { - if (voxel_size == 0.0f) { + if (voxel_size < 1e-5f || interior_band_width <= 0.0f) { return nullptr; } @@ -117,61 +115,47 @@ static openvdb::FloatGrid::Ptr mesh_to_fog_volume_grid( mesh_to_index_space_transform.location() -= 0.5f; OpenVDBMeshAdapter mesh_adapter{*mesh, mesh_to_index_space_transform}; - - /* Convert the bandwidths from object in index space. */ - const float exterior = MAX2(0.001f, exterior_band_width / voxel_size); - const float interior = MAX2(0.001f, interior_band_width / voxel_size); - - /* Setting the interior bandwidth to FLT_MAX, will make it fill the entire volume. */ - openvdb::FloatGrid::Ptr new_grid = openvdb::tools::meshToVolume( - mesh_adapter, {}, exterior, fill_volume ? FLT_MAX : interior); - - /* Give each grid cell a fixed density for now. */ - openvdb::tools::foreach ( - new_grid->beginValueOn(), - [density](const openvdb::FloatGrid::ValueOnIter &iter) { iter.setValue(density); }); - - new_grid->setGridClass(openvdb::GRID_FOG_VOLUME); - - return new_grid; -} - -static openvdb::FloatGrid::Ptr mesh_to_sdf_volume_grid(const Mesh &mesh, - const float voxel_size, - const float half_band_width) -{ - if (voxel_size <= 0.0f || half_band_width <= 0.0f) { - return nullptr; - } - - const Span positions = mesh.vert_positions(); - const Span corner_verts = mesh.corner_verts(); - const Span looptris = mesh.looptris(); - - std::vector points(positions.size()); - std::vector triangles(looptris.size()); - - threading::parallel_for(positions.index_range(), 2048, [&](const IndexRange range) { - for (const int i : range) { - const float3 &co = positions[i]; - points[i] = openvdb::Vec3s(co.x, co.y, co.z) - 0.5f * voxel_size; - } - }); - - threading::parallel_for(looptris.index_range(), 2048, [&](const IndexRange range) { - for (const int i : range) { - const MLoopTri &loop_tri = looptris[i]; - triangles[i] = openvdb::Vec3I(corner_verts[loop_tri.tri[0]], - corner_verts[loop_tri.tri[1]], - corner_verts[loop_tri.tri[2]]); - } - }); + const float interior = std::max(1.0f, interior_band_width / voxel_size); openvdb::math::Transform::Ptr transform = openvdb::math::Transform::createLinearTransform( voxel_size); - openvdb::FloatGrid::Ptr new_grid = openvdb::tools::meshToLevelSet( - *transform, points, triangles, half_band_width); + openvdb::FloatGrid::Ptr new_grid = openvdb::tools::meshToVolume( + mesh_adapter, *transform, 1.0f, interior); + openvdb::tools::sdfToFogVolume(*new_grid); + + if (density != 1.0f) { + openvdb::tools::foreach (new_grid->beginValueOn(), + [&](const openvdb::FloatGrid::ValueOnIter &iter) { + iter.modifyValue([&](float &value) { value *= density; }); + }); + } + return new_grid; +} + +static openvdb::FloatGrid::Ptr mesh_to_sdf_volume_grid( + const Mesh *mesh, + const float4x4 &mesh_to_volume_space_transform, + const float voxel_size, + const bool fill_interior, + const float half_band_width) +{ + if (voxel_size < 1e-5f || half_band_width <= 0.0f) { + return nullptr; + } + + float4x4 mesh_to_index_space_transform = math::from_scale(float3(1.0f / voxel_size)); + mesh_to_index_space_transform *= mesh_to_volume_space_transform; + /* Better align generated grid with the source mesh. */ + mesh_to_index_space_transform.location() -= 0.5f; + + OpenVDBMeshAdapter mesh_adapter{*mesh, mesh_to_index_space_transform}; + const float half_band_clamped = std::max(1.0f, half_band_width); + + openvdb::math::Transform::Ptr transform = openvdb::math::Transform::createLinearTransform( + voxel_size); + openvdb::FloatGrid::Ptr new_grid = openvdb::tools::meshToVolume( + mesh_adapter, *transform, half_band_clamped, fill_interior ? FLT_MAX : half_band_clamped); return new_grid; } @@ -180,40 +164,24 @@ VolumeGrid *fog_volume_grid_add_from_mesh(Volume *volume, const Mesh *mesh, const float4x4 &mesh_to_volume_space_transform, const float voxel_size, - const bool fill_volume, - const float exterior_band_width, const float interior_band_width, const float density) { - VolumeGrid *c_grid = BKE_volume_grid_add(volume, name.c_str(), VOLUME_GRID_FLOAT); - openvdb::FloatGrid::Ptr grid = openvdb::gridPtrCast( - BKE_volume_grid_openvdb_for_write(volume, c_grid, false)); - - /* Generate grid from mesh */ - openvdb::FloatGrid::Ptr mesh_grid = mesh_to_fog_volume_grid(mesh, - mesh_to_volume_space_transform, - voxel_size, - fill_volume, - exterior_band_width, - interior_band_width, - density); - - if (mesh_grid != nullptr) { - /* Merge the generated grid. Should be cheap because grid has just been created. */ - grid->merge(*mesh_grid); - /* Change transform so that the index space is correctly transformed to object space. */ - grid->transform().postScale(voxel_size); - } - return c_grid; + openvdb::FloatGrid::Ptr mesh_grid = mesh_to_fog_volume_grid( + mesh, mesh_to_volume_space_transform, voxel_size, interior_band_width, density); + return mesh_grid ? BKE_volume_grid_add_vdb(*volume, name, std::move(mesh_grid)) : nullptr; } VolumeGrid *sdf_volume_grid_add_from_mesh(Volume *volume, const StringRefNull name, - const Mesh &mesh, + const Mesh *mesh, + const float4x4 &mesh_to_volume_space_transform, const float voxel_size, + const bool fill_interior, const float half_band_width) { - openvdb::FloatGrid::Ptr mesh_grid = mesh_to_sdf_volume_grid(mesh, voxel_size, half_band_width); + openvdb::FloatGrid::Ptr mesh_grid = mesh_to_sdf_volume_grid( + mesh, mesh_to_volume_space_transform, voxel_size, fill_interior, half_band_width); return mesh_grid ? BKE_volume_grid_add_vdb(*volume, name, std::move(mesh_grid)) : nullptr; } } // namespace blender::geometry diff --git a/source/blender/makesdna/DNA_modifier_types.h b/source/blender/makesdna/DNA_modifier_types.h index 7af83f8f888..da65898f551 100644 --- a/source/blender/makesdna/DNA_modifier_types.h +++ b/source/blender/makesdna/DNA_modifier_types.h @@ -2333,14 +2333,7 @@ typedef struct MeshToVolumeModifierData { * different. */ int voxel_amount; - /** If true, every cell in the enclosed volume gets a density. Otherwise, the interior_band_width - * is used. */ - char fill_volume; - char _pad1[3]; - - /** Band widths are in object space. */ float interior_band_width; - float exterior_band_width; float density; char _pad2[4]; diff --git a/source/blender/makesrna/intern/rna_modifier.c b/source/blender/makesrna/intern/rna_modifier.c index cb75eb501a8..dae7d8f3b06 100644 --- a/source/blender/makesrna/intern/rna_modifier.c +++ b/source/blender/makesrna/intern/rna_modifier.c @@ -7085,19 +7085,9 @@ static void rna_def_modifier_mesh_to_volume(BlenderRNA *brna) RNA_def_property_range(prop, 0, INT_MAX); RNA_def_property_update(prop, 0, "rna_Modifier_update"); - prop = RNA_def_property(srna, "use_fill_volume", PROP_BOOLEAN, PROP_NONE); - RNA_def_property_boolean_sdna(prop, NULL, "fill_volume", 1); - RNA_def_property_ui_text( - prop, "Fill Volume", "Initialize the density grid in every cell inside the enclosed volume"); - RNA_def_property_update(prop, 0, "rna_Modifier_update"); - prop = RNA_def_property(srna, "interior_band_width", PROP_FLOAT, PROP_NONE); - RNA_def_property_ui_text(prop, "Interior Band Width", "Width of the volume inside of the mesh"); - RNA_def_property_range(prop, 0.0, FLT_MAX); - RNA_def_property_update(prop, 0, "rna_Modifier_update"); - - prop = RNA_def_property(srna, "exterior_band_width", PROP_FLOAT, PROP_NONE); - RNA_def_property_ui_text(prop, "Exterior Band Width", "Width of the volume outside of the mesh"); + RNA_def_property_ui_text( + prop, "Interior Band Width", "Width of the gradient inside of the mesh"); RNA_def_property_range(prop, 0.0, FLT_MAX); RNA_def_property_update(prop, 0, "rna_Modifier_update"); diff --git a/source/blender/modifiers/intern/MOD_mesh_to_volume.cc b/source/blender/modifiers/intern/MOD_mesh_to_volume.cc index cb13f032969..ca4c9b84858 100644 --- a/source/blender/modifiers/intern/MOD_mesh_to_volume.cc +++ b/source/blender/modifiers/intern/MOD_mesh_to_volume.cc @@ -51,9 +51,7 @@ static void initData(ModifierData *md) mvmd->resolution_mode = MESH_TO_VOLUME_RESOLUTION_MODE_VOXEL_AMOUNT; mvmd->voxel_size = 0.1f; mvmd->voxel_amount = 32; - mvmd->fill_volume = true; - mvmd->interior_band_width = 0.1f; - mvmd->exterior_band_width = 0.1f; + mvmd->interior_band_width = 0.2f; mvmd->density = 1.0f; } @@ -89,12 +87,7 @@ static void panel_draw(const bContext * /*C*/, Panel *panel) { uiLayout *col = uiLayoutColumn(layout, false); - uiItemR(col, ptr, "use_fill_volume", 0, nullptr, ICON_NONE); - uiItemR(col, ptr, "exterior_band_width", 0, nullptr, ICON_NONE); - - uiLayout *subcol = uiLayoutColumn(col, false); - uiLayoutSetActive(subcol, !mvmd->fill_volume); - uiItemR(subcol, ptr, "interior_band_width", 0, nullptr, ICON_NONE); + uiItemR(col, ptr, "interior_band_width", 0, nullptr, ICON_NONE); } { uiLayout *col = uiLayoutColumn(layout, false); @@ -140,13 +133,13 @@ static Volume *mesh_to_volume(ModifierData *md, resolution.mode = (MeshToVolumeModifierResolutionMode)mvmd->resolution_mode; if (resolution.mode == MESH_TO_VOLUME_RESOLUTION_MODE_VOXEL_AMOUNT) { resolution.settings.voxel_amount = mvmd->voxel_amount; - if (resolution.settings.voxel_amount <= 0.0f) { + if (resolution.settings.voxel_amount < 1.0f) { return input_volume; } } else if (resolution.mode == MESH_TO_VOLUME_RESOLUTION_MODE_VOXEL_SIZE) { resolution.settings.voxel_size = mvmd->voxel_size; - if (resolution.settings.voxel_size <= 0.0f) { + if (resolution.settings.voxel_size < 1e-5f) { return input_volume; } } @@ -157,11 +150,8 @@ static Volume *mesh_to_volume(ModifierData *md, r_max = bb->vec[6]; }; - const float voxel_size = geometry::volume_compute_voxel_size(ctx->depsgraph, - bounds_fn, - resolution, - mvmd->exterior_band_width, - mesh_to_own_object_space_transform); + const float voxel_size = geometry::volume_compute_voxel_size( + ctx->depsgraph, bounds_fn, resolution, 0.0f, mesh_to_own_object_space_transform); /* Create a new volume. */ Volume *volume; @@ -178,8 +168,6 @@ static Volume *mesh_to_volume(ModifierData *md, mesh, mesh_to_own_object_space_transform, voxel_size, - mvmd->fill_volume, - mvmd->exterior_band_width, mvmd->interior_band_width, mvmd->density); diff --git a/source/blender/nodes/geometry/nodes/node_geo_mesh_to_sdf_volume.cc b/source/blender/nodes/geometry/nodes/node_geo_mesh_to_sdf_volume.cc index 05f096c1119..24cda23cf16 100644 --- a/source/blender/nodes/geometry/nodes/node_geo_mesh_to_sdf_volume.cc +++ b/source/blender/nodes/geometry/nodes/node_geo_mesh_to_sdf_volume.cc @@ -39,6 +39,7 @@ static void node_declare(NodeDeclarationBuilder &b) .default_value(3.0f) .min(1.01f) .max(10.0f); + b.add_input(N_("Fill Interior")); b.add_output(CTX_N_(BLT_I18NCONTEXT_ID_ID, "Volume")) .translation_context(BLT_I18NCONTEXT_ID_ID); } @@ -91,6 +92,7 @@ static Volume *create_volume_from_mesh(const Mesh &mesh, GeoNodeExecParams ¶ const NodeGeometryMeshToVolume &storage = node_storage(params.node()); const float half_band_width = params.get_input("Half-Band Width"); + const bool fill_interior = params.get_input("Fill Interior"); geometry::MeshToVolumeResolution resolution; resolution.mode = (MeshToVolumeModifierResolutionMode)storage.resolution_mode; @@ -132,7 +134,13 @@ static Volume *create_volume_from_mesh(const Mesh &mesh, GeoNodeExecParams ¶ Volume *volume = reinterpret_cast(BKE_id_new_nomain(ID_VO, nullptr)); /* Convert mesh to grid and add to volume. */ - geometry::sdf_volume_grid_add_from_mesh(volume, "distance", mesh, voxel_size, half_band_width); + geometry::sdf_volume_grid_add_from_mesh(volume, + "distance", + &mesh, + mesh_to_volume_space_transform, + voxel_size, + fill_interior, + half_band_width); return volume; } diff --git a/source/blender/nodes/geometry/nodes/node_geo_mesh_to_volume.cc b/source/blender/nodes/geometry/nodes/node_geo_mesh_to_volume.cc index e3f391563f9..5c30986fca2 100644 --- a/source/blender/nodes/geometry/nodes/node_geo_mesh_to_volume.cc +++ b/source/blender/nodes/geometry/nodes/node_geo_mesh_to_volume.cc @@ -32,21 +32,12 @@ static void node_declare(NodeDeclarationBuilder &b) .max(FLT_MAX) .subtype(PROP_DISTANCE); b.add_input(N_("Voxel Amount")).default_value(64.0f).min(0.0f).max(FLT_MAX); - b.add_input(N_("Exterior Band Width")) - .default_value(0.1f) - .min(0.0f) - .max(FLT_MAX) - .subtype(PROP_DISTANCE) - .description(N_("Width of the volume outside of the mesh")); b.add_input(N_("Interior Band Width")) - .default_value(0.0f) - .min(0.0f) + .default_value(0.2f) + .min(0.0001f) .max(FLT_MAX) .subtype(PROP_DISTANCE) - .description(N_("Width of the volume inside of the mesh")); - b.add_input(N_("Fill Volume")) - .default_value(true) - .description(N_("Initialize the density grid in every cell inside the enclosed volume")); + .description(N_("Width of the gradient inside of the mesh")); b.add_output(CTX_N_(BLT_I18NCONTEXT_ID_ID, "Volume")) .translation_context(BLT_I18NCONTEXT_ID_ID); } @@ -86,9 +77,7 @@ static Volume *create_volume_from_mesh(const Mesh &mesh, GeoNodeExecParams ¶ *(const NodeGeometryMeshToVolume *)params.node().storage; const float density = params.get_input("Density"); - const float exterior_band_width = params.get_input("Exterior Band Width"); const float interior_band_width = params.get_input("Interior Band Width"); - const bool fill_volume = params.get_input("Fill Volume"); geometry::MeshToVolumeResolution resolution; resolution.mode = (MeshToVolumeModifierResolutionMode)storage.resolution_mode; @@ -119,11 +108,8 @@ static Volume *create_volume_from_mesh(const Mesh &mesh, GeoNodeExecParams ¶ r_max = max; }; - const float voxel_size = geometry::volume_compute_voxel_size(params.depsgraph(), - bounds_fn, - resolution, - exterior_band_width, - mesh_to_volume_space_transform); + const float voxel_size = geometry::volume_compute_voxel_size( + params.depsgraph(), bounds_fn, resolution, 0.0f, mesh_to_volume_space_transform); Volume *volume = reinterpret_cast(BKE_id_new_nomain(ID_VO, nullptr)); @@ -133,8 +119,6 @@ static Volume *create_volume_from_mesh(const Mesh &mesh, GeoNodeExecParams ¶ &mesh, mesh_to_volume_space_transform, voxel_size, - fill_volume, - exterior_band_width, interior_band_width, density); -- 2.30.2 From 590415278fa287092da0df3795131f05739de8f3 Mon Sep 17 00:00:00 2001 From: Erik Abrahamsson Date: Tue, 25 Apr 2023 19:21:21 +0200 Subject: [PATCH 2/3] Revert some of the changes and add comments --- source/blender/geometry/GEO_mesh_to_volume.hh | 9 +-- .../blender/geometry/intern/mesh_to_volume.cc | 70 ++++++++++++------- .../nodes/node_geo_mesh_to_sdf_volume.cc | 10 +-- 3 files changed, 46 insertions(+), 43 deletions(-) diff --git a/source/blender/geometry/GEO_mesh_to_volume.hh b/source/blender/geometry/GEO_mesh_to_volume.hh index 7cf2017e2bd..98eff7a8999 100644 --- a/source/blender/geometry/GEO_mesh_to_volume.hh +++ b/source/blender/geometry/GEO_mesh_to_volume.hh @@ -52,12 +52,7 @@ VolumeGrid *fog_volume_grid_add_from_mesh(Volume *volume, /** * Add a new SDF VolumeGrid to the Volume by converting the supplied mesh. */ -VolumeGrid *sdf_volume_grid_add_from_mesh(Volume *volume, - StringRefNull name, - const Mesh *mesh, - const float4x4 &mesh_to_volume_space_transform, - float voxel_size, - bool fill_interior, - float half_band_width); +VolumeGrid *sdf_volume_grid_add_from_mesh( + Volume *volume, StringRefNull name, const Mesh &mesh, float voxel_size, float half_band_width); #endif } // namespace blender::geometry diff --git a/source/blender/geometry/intern/mesh_to_volume.cc b/source/blender/geometry/intern/mesh_to_volume.cc index e847d6b2d99..e2308b47fe5 100644 --- a/source/blender/geometry/intern/mesh_to_volume.cc +++ b/source/blender/geometry/intern/mesh_to_volume.cc @@ -10,6 +10,7 @@ #include "GEO_mesh_to_volume.hh" #ifdef WITH_OPENVDB +# include # include # include # include @@ -73,29 +74,34 @@ float volume_compute_voxel_size(const Depsgraph *depsgraph, const float exterior_band_width, const float4x4 &transform) { - const float volume_simplify = BKE_volume_simplify_factor(depsgraph); - if (volume_simplify == 0.0f) { + const float simplify_factor = BKE_volume_simplify_factor(depsgraph); + if (simplify_factor <= 0.0f) { return 0.0f; } if (res.mode == MESH_TO_VOLUME_RESOLUTION_MODE_VOXEL_SIZE) { - return res.settings.voxel_size / volume_simplify; + return res.settings.voxel_size / simplify_factor; } if (res.settings.voxel_amount <= 0) { - return 0; + return 0.0f; } 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. */ + /* Compute the diagonal of the bounding box. This is used because + * it will always be bigger than the widest side of the mesh. */ const float diagonal = math::distance(math::transform_point(transform, bb_max), math::transform_point(transform, bb_min)); + + /* To get the approximate size per voxel, first subtract the exterior band from the requested + * voxel amount, then divide the diagonal with this value if it's bigger than 1. */ const float voxel_size = (diagonal / std::max(1.0f, float(res.settings.voxel_amount) - 2.0f * exterior_band_width)); - return voxel_size / volume_simplify; + + /* Return the simplified voxel size. */ + return voxel_size / simplify_factor; } static openvdb::FloatGrid::Ptr mesh_to_fog_volume_grid( @@ -133,29 +139,42 @@ static openvdb::FloatGrid::Ptr mesh_to_fog_volume_grid( return new_grid; } -static openvdb::FloatGrid::Ptr mesh_to_sdf_volume_grid( - const Mesh *mesh, - const float4x4 &mesh_to_volume_space_transform, - const float voxel_size, - const bool fill_interior, - const float half_band_width) +static openvdb::FloatGrid::Ptr mesh_to_sdf_volume_grid(const Mesh &mesh, + const float voxel_size, + const float half_band_width) { - if (voxel_size < 1e-5f || half_band_width <= 0.0f) { + if (voxel_size <= 0.0f || half_band_width <= 0.0f) { return nullptr; } - float4x4 mesh_to_index_space_transform = math::from_scale(float3(1.0f / voxel_size)); - mesh_to_index_space_transform *= mesh_to_volume_space_transform; - /* Better align generated grid with the source mesh. */ - mesh_to_index_space_transform.location() -= 0.5f; + const Span positions = mesh.vert_positions(); + const Span corner_verts = mesh.corner_verts(); + const Span looptris = mesh.looptris(); - OpenVDBMeshAdapter mesh_adapter{*mesh, mesh_to_index_space_transform}; - const float half_band_clamped = std::max(1.0f, half_band_width); + std::vector points(positions.size()); + std::vector triangles(looptris.size()); + + threading::parallel_for(positions.index_range(), 2048, [&](const IndexRange range) { + for (const int i : range) { + const float3 &co = positions[i]; + points[i] = openvdb::Vec3s(co.x, co.y, co.z) - 0.5f * voxel_size; + } + }); + + threading::parallel_for(looptris.index_range(), 2048, [&](const IndexRange range) { + for (const int i : range) { + const MLoopTri &loop_tri = looptris[i]; + triangles[i] = openvdb::Vec3I(corner_verts[loop_tri.tri[0]], + corner_verts[loop_tri.tri[1]], + corner_verts[loop_tri.tri[2]]); + } + }); openvdb::math::Transform::Ptr transform = openvdb::math::Transform::createLinearTransform( voxel_size); - openvdb::FloatGrid::Ptr new_grid = openvdb::tools::meshToVolume( - mesh_adapter, *transform, half_band_clamped, fill_interior ? FLT_MAX : half_band_clamped); + openvdb::FloatGrid::Ptr new_grid = openvdb::tools::meshToLevelSet( + *transform, points, triangles, half_band_width); + return new_grid; } @@ -174,14 +193,11 @@ VolumeGrid *fog_volume_grid_add_from_mesh(Volume *volume, VolumeGrid *sdf_volume_grid_add_from_mesh(Volume *volume, const StringRefNull name, - const Mesh *mesh, - const float4x4 &mesh_to_volume_space_transform, + const Mesh &mesh, const float voxel_size, - const bool fill_interior, const float half_band_width) { - openvdb::FloatGrid::Ptr mesh_grid = mesh_to_sdf_volume_grid( - mesh, mesh_to_volume_space_transform, voxel_size, fill_interior, half_band_width); + openvdb::FloatGrid::Ptr mesh_grid = mesh_to_sdf_volume_grid(mesh, voxel_size, half_band_width); return mesh_grid ? BKE_volume_grid_add_vdb(*volume, name, std::move(mesh_grid)) : nullptr; } } // namespace blender::geometry diff --git a/source/blender/nodes/geometry/nodes/node_geo_mesh_to_sdf_volume.cc b/source/blender/nodes/geometry/nodes/node_geo_mesh_to_sdf_volume.cc index 24cda23cf16..05f096c1119 100644 --- a/source/blender/nodes/geometry/nodes/node_geo_mesh_to_sdf_volume.cc +++ b/source/blender/nodes/geometry/nodes/node_geo_mesh_to_sdf_volume.cc @@ -39,7 +39,6 @@ static void node_declare(NodeDeclarationBuilder &b) .default_value(3.0f) .min(1.01f) .max(10.0f); - b.add_input(N_("Fill Interior")); b.add_output(CTX_N_(BLT_I18NCONTEXT_ID_ID, "Volume")) .translation_context(BLT_I18NCONTEXT_ID_ID); } @@ -92,7 +91,6 @@ static Volume *create_volume_from_mesh(const Mesh &mesh, GeoNodeExecParams ¶ const NodeGeometryMeshToVolume &storage = node_storage(params.node()); const float half_band_width = params.get_input("Half-Band Width"); - const bool fill_interior = params.get_input("Fill Interior"); geometry::MeshToVolumeResolution resolution; resolution.mode = (MeshToVolumeModifierResolutionMode)storage.resolution_mode; @@ -134,13 +132,7 @@ static Volume *create_volume_from_mesh(const Mesh &mesh, GeoNodeExecParams ¶ Volume *volume = reinterpret_cast(BKE_id_new_nomain(ID_VO, nullptr)); /* Convert mesh to grid and add to volume. */ - geometry::sdf_volume_grid_add_from_mesh(volume, - "distance", - &mesh, - mesh_to_volume_space_transform, - voxel_size, - fill_interior, - half_band_width); + geometry::sdf_volume_grid_add_from_mesh(volume, "distance", mesh, voxel_size, half_band_width); return volume; } -- 2.30.2 From b2496e1f634b26a4852f606871aa236c5c52724e Mon Sep 17 00:00:00 2001 From: Erik Abrahamsson Date: Tue, 25 Apr 2023 21:39:53 +0200 Subject: [PATCH 3/3] Remove even more unnecessary improvements --- source/blender/geometry/intern/mesh_to_volume.cc | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/source/blender/geometry/intern/mesh_to_volume.cc b/source/blender/geometry/intern/mesh_to_volume.cc index e2308b47fe5..2f41c9ba29c 100644 --- a/source/blender/geometry/intern/mesh_to_volume.cc +++ b/source/blender/geometry/intern/mesh_to_volume.cc @@ -74,16 +74,16 @@ float volume_compute_voxel_size(const Depsgraph *depsgraph, const float exterior_band_width, const float4x4 &transform) { - const float simplify_factor = BKE_volume_simplify_factor(depsgraph); - if (simplify_factor <= 0.0f) { + const float volume_simplify = BKE_volume_simplify_factor(depsgraph); + if (volume_simplify == 0.0f) { return 0.0f; } if (res.mode == MESH_TO_VOLUME_RESOLUTION_MODE_VOXEL_SIZE) { - return res.settings.voxel_size / simplify_factor; + return res.settings.voxel_size / volume_simplify; } if (res.settings.voxel_amount <= 0) { - return 0.0f; + return 0; } float3 bb_min; @@ -101,7 +101,7 @@ float volume_compute_voxel_size(const Depsgraph *depsgraph, (diagonal / std::max(1.0f, float(res.settings.voxel_amount) - 2.0f * exterior_band_width)); /* Return the simplified voxel size. */ - return voxel_size / simplify_factor; + return voxel_size / volume_simplify; } static openvdb::FloatGrid::Ptr mesh_to_fog_volume_grid( -- 2.30.2