[Bf-blender-cvs] [076c913a3bd] mesh-to-volume-modifier: Volume: initial mesh to volume modifier implementation
Jacques Lucke
noreply at git.blender.org
Wed Sep 23 17:15:49 CEST 2020
Commit: 076c913a3bd2e4f69a9aab2c3d3e167ddf0285a2
Author: Jacques Lucke
Date: Wed Sep 23 17:11:43 2020 +0200
Branches: mesh-to-volume-modifier
https://developer.blender.org/rB076c913a3bd2e4f69a9aab2c3d3e167ddf0285a2
Volume: initial mesh to volume modifier implementation
Using `openvdb::tools::meshToLevelSet` is probably not correct.
I will look into `meshToVolume` next.
===================================================================
M source/blender/makesrna/intern/rna_modifier.c
M source/blender/modifiers/CMakeLists.txt
M source/blender/modifiers/intern/MOD_mesh_to_volume.cc
===================================================================
diff --git a/source/blender/makesrna/intern/rna_modifier.c b/source/blender/makesrna/intern/rna_modifier.c
index 69d9801ea7d..367401addf6 100644
--- a/source/blender/makesrna/intern/rna_modifier.c
+++ b/source/blender/makesrna/intern/rna_modifier.c
@@ -7118,6 +7118,7 @@ static void rna_def_modifier_mesh_to_volume(BlenderRNA *brna)
prop = RNA_def_property(srna, "voxel_size", PROP_FLOAT, PROP_NONE);
RNA_def_property_ui_text(
prop, "Voxel Size", "The smaller this number the higher the resolution of the output");
+ RNA_def_property_range(prop, 0.001, FLT_MAX);
RNA_def_property_update(prop, 0, "rna_Modifier_update");
RNA_define_lib_overridable(false);
diff --git a/source/blender/modifiers/CMakeLists.txt b/source/blender/modifiers/CMakeLists.txt
index fddb26685c7..80951288494 100644
--- a/source/blender/modifiers/CMakeLists.txt
+++ b/source/blender/modifiers/CMakeLists.txt
@@ -178,6 +178,20 @@ if(WITH_GMP)
add_definitions(-DWITH_GMP)
endif()
+if(WITH_OPENVDB)
+ list(APPEND INC
+ ../../../intern/openvdb
+ )
+ list(APPEND INC_SYS
+ ${OPENVDB_INCLUDE_DIRS}
+ )
+ list(APPEND LIB
+ bf_intern_openvdb
+ ${OPENVDB_LIBRARIES}
+ )
+ add_definitions(-DWITH_OPENVDB ${OPENVDB_DEFINITIONS})
+endif()
+
# So we can have special tricks in modifier system.
blender_add_lib(bf_modifiers "${SRC}" "${INC}" "${INC_SYS}" "${LIB}")
diff --git a/source/blender/modifiers/intern/MOD_mesh_to_volume.cc b/source/blender/modifiers/intern/MOD_mesh_to_volume.cc
index d5f2558d942..55fd4d8fbe5 100644
--- a/source/blender/modifiers/intern/MOD_mesh_to_volume.cc
+++ b/source/blender/modifiers/intern/MOD_mesh_to_volume.cc
@@ -18,8 +18,12 @@
* \ingroup modifiers
*/
+#include <vector>
+
#include "BKE_lib_query.h"
+#include "BKE_mesh_runtime.h"
#include "BKE_modifier.h"
+#include "BKE_volume.h"
#include "DNA_mesh_types.h"
#include "DNA_meshdata_types.h"
@@ -39,6 +43,14 @@
#include "MOD_modifiertypes.h"
#include "MOD_ui_common.h"
+#include "BLI_float4x4.hh"
+#include "BLI_index_range.hh"
+
+#ifdef WITH_OPENVDB
+# include <openvdb/openvdb.h>
+# include <openvdb/tools/MeshToVolume.h>
+#endif
+
static void initData(ModifierData *md)
{
MeshToVolumeModifierData *mvmd = reinterpret_cast<MeshToVolumeModifierData *>(md);
@@ -49,6 +61,7 @@ static void initData(ModifierData *md)
static void updateDepsgraph(ModifierData *md, const ModifierUpdateDepsgraphContext *ctx)
{
MeshToVolumeModifierData *mvmd = reinterpret_cast<MeshToVolumeModifierData *>(md);
+ DEG_add_modifier_to_transform_relation(ctx->node, "own transforms");
if (mvmd->object) {
DEG_add_object_relation(
ctx->node, mvmd->object, DEG_OB_COMP_GEOMETRY, "Object that is converted to a volume");
@@ -85,14 +98,62 @@ static void panelRegister(ARegionType *region_type)
modifier_panel_register(region_type, eModifierType_MeshToVolume, panel_draw);
}
-static Volume *modifyVolume(ModifierData *md,
- const ModifierEvalContext *UNUSED(ctx),
- Volume *input_volume)
+static Volume *modifyVolume(ModifierData *md, const ModifierEvalContext *ctx, Volume *input_volume)
{
+#ifdef WITH_OPENVDB
+ using namespace blender;
+
MeshToVolumeModifierData *mvmd = reinterpret_cast<MeshToVolumeModifierData *>(md);
- printf("%f\n", mvmd->voxel_size);
+ if (mvmd->object == NULL) {
+ return input_volume;
+ }
+ if (mvmd->object->type != OB_MESH) {
+ return input_volume;
+ }
+
+ Mesh *mesh = static_cast<Mesh *>(mvmd->object->data);
+ const MLoopTri *looptris = BKE_mesh_runtime_looptri_ensure(mesh);
+ const int looptris_len = BKE_mesh_runtime_looptri_len(mesh);
+
+ float4x4 obmat = mvmd->object->obmat;
+ mul_m4_m4_pre(obmat.values, ctx->object->imat);
+ mul_m4_fl(obmat.values, 1.0f / mvmd->voxel_size);
+ obmat.values[3][3] = 1.0f;
+
+ std::vector<openvdb::Vec3s> vertices(mesh->totvert);
+ for (const int i : IndexRange(mesh->totvert)) {
+ float3 position = obmat * float3(mesh->mvert[i].co);
+ vertices[i] = &position.x;
+ }
+
+ std::vector<openvdb::Vec3I> triangle_indices(looptris_len);
+ for (const int i : IndexRange(looptris_len)) {
+ const MLoopTri &tri = looptris[i];
+ triangle_indices[i] = {
+ mesh->mloop[tri.tri[0]].v,
+ mesh->mloop[tri.tri[1]].v,
+ mesh->mloop[tri.tri[2]].v,
+ };
+ }
+
+ const openvdb::math::Transform xform;
+ openvdb::FloatGrid::Ptr new_grid = openvdb::tools::meshToLevelSet<openvdb::FloatGrid>(
+ xform, vertices, triangle_indices);
+
+ Volume *volume = BKE_volume_new_for_eval(input_volume);
+ VolumeGrid *c_density_grid = BKE_volume_grid_add(volume, "density", VOLUME_GRID_FLOAT);
+ openvdb::FloatGrid::Ptr density_grid = std::static_pointer_cast<openvdb::FloatGrid>(
+ BKE_volume_grid_openvdb_for_write(volume, c_density_grid, false));
+ density_grid->merge(*new_grid);
+ density_grid->transform().postScale(mvmd->voxel_size);
+
+ return volume;
+
+#else
+ UNUSED_VARS(md, ctx);
return input_volume;
+#endif
}
ModifierTypeInfo modifierType_MeshToVolume = {
@@ -112,7 +173,7 @@ ModifierTypeInfo modifierType_MeshToVolume = {
/* modifyPointCloud */ NULL,
/* modifyVolume */ modifyVolume,
- /* initData */ NULL,
+ /* initData */ initData,
/* requiredDataMask */ NULL,
/* freeData */ NULL,
/* isDisabled */ NULL,
More information about the Bf-blender-cvs
mailing list