diff options
author | ishbosamiya <ishbosamiya@gmail.com> | 2021-08-19 13:54:56 +0300 |
---|---|---|
committer | ishbosamiya <ishbosamiya@gmail.com> | 2021-08-19 13:54:56 +0300 |
commit | 5afe71daf54212c3488306c986fb73504183bfb6 (patch) | |
tree | f2feacaf113ac46bee1d02f0ee55308fffc76cfc | |
parent | 111458f6c7a8df52d073577e653935dc2902e8f0 (diff) |
adaptive_mesh: serialize and dump the cloth mesh every frame
-rw-r--r-- | source/blender/blenkernel/BKE_cloth_remesh.hh | 5 | ||||
-rw-r--r-- | source/blender/blenkernel/intern/cloth.c | 7 | ||||
-rw-r--r-- | source/blender/blenkernel/intern/cloth_remesh.cc | 60 |
3 files changed, 72 insertions, 0 deletions
diff --git a/source/blender/blenkernel/BKE_cloth_remesh.hh b/source/blender/blenkernel/BKE_cloth_remesh.hh index cba6bd108a4..f9d977660db 100644 --- a/source/blender/blenkernel/BKE_cloth_remesh.hh +++ b/source/blender/blenkernel/BKE_cloth_remesh.hh @@ -63,6 +63,11 @@ extern "C" { Mesh *BKE_cloth_remesh(struct Object *ob, struct ClothModifierData *clmd, struct Mesh *mesh); +void BKE_cloth_serialize_adaptive_mesh(struct Object *ob, + struct ClothModifierData *clmd, + struct Mesh *mesh, + const char *location); + #ifdef __cplusplus } } /* namespace blender::bke */ diff --git a/source/blender/blenkernel/intern/cloth.c b/source/blender/blenkernel/intern/cloth.c index d8dfc88aece..1e99921b911 100644 --- a/source/blender/blenkernel/intern/cloth.c +++ b/source/blender/blenkernel/intern/cloth.c @@ -33,6 +33,7 @@ #include "BLI_linklist.h" #include "BLI_math.h" #include "BLI_rand.h" +#include "BLI_string.h" #include "BLI_utildefines.h" #include "DEG_depsgraph.h" @@ -328,6 +329,12 @@ static Mesh *do_step_cloth( // printf ( "%f\n", ( float ) tval() ); + { + char location[48]; + BLI_snprintf(location, 48, "/tmp/cloth_sim/framenr_%05d.mesh", framenr); + BKE_cloth_serialize_adaptive_mesh(ob, clmd, mesh, location); + } + if (remesh) { /* In case remeshing is enabled, the remeshing function will * return the final mesh needed */ diff --git a/source/blender/blenkernel/intern/cloth_remesh.cc b/source/blender/blenkernel/intern/cloth_remesh.cc index 4b64ad4fc0e..e76b51629e3 100644 --- a/source/blender/blenkernel/intern/cloth_remesh.cc +++ b/source/blender/blenkernel/intern/cloth_remesh.cc @@ -1374,6 +1374,66 @@ static void set_cloth_information_when_new_mesh(Object *ob, ClothModifierData *c clmd->clothObject->bvhselftree = bvhtree_build_from_cloth(clmd, clmd->coll_parms->selfepsilon); } +void BKE_cloth_serialize_adaptive_mesh(Object *ob, + ClothModifierData *clmd, + Mesh *mesh, + const char *location) +{ + AdaptiveRemeshParams<internal::ClothNodeData, Cloth> params; + params.size_min = clmd->sim_parms->remeshing_size_min; + params.extra_data_to_end = [](const Cloth &cloth, size_t index) { + BLI_assert(index < cloth.mvert_num); + BLI_assert(cloth.verts); + return internal::ClothNodeData(cloth.verts[index]); + }; + params.post_extra_data_to_end = [](Cloth & /*unused*/) { + /* Do nothing */ + }; + + params.end_to_extra_data = + [](Cloth & /*unused*/, internal::ClothNodeData /*unused*/, size_t /*unused*/) { + /* Do nothing */ + }; + params.pre_end_to_extra_data = [](Cloth &cloth, size_t num_nodes) { + /* Do not allocate cloth.verts, it shouldn't have been modified */ + BLI_assert(cloth.verts != nullptr); + BLI_assert(cloth.mvert_num == num_nodes); + }; + + const auto remeshing = clmd->sim_parms->flags & CLOTH_SIMSETTINGS_FLAG_REMESH; + Mesh *cloth_to_object_res = nullptr; + if (remeshing && clmd->prev_frame_mesh) { + cloth_to_object_res = cloth_to_object(ob, clmd, clmd->prev_frame_mesh, true); + } + else { + cloth_to_object_res = cloth_to_object(ob, clmd, mesh, true); + } + + internal::MeshIO meshio_input; + meshio_input.read(cloth_to_object_res); + + internal::AdaptiveMesh<internal::ClothNodeData> adaptive_mesh; + adaptive_mesh.read(meshio_input); + + Cloth &extra_data = *clmd->clothObject; + + /* Load up the `NodeData`'s extra_data */ + { + auto i = 0; + for (auto &node : adaptive_mesh.get_nodes_mut()) { + node.set_extra_data(internal::NodeData(params.extra_data_to_end(extra_data, i))); + i++; + } + + params.post_extra_data_to_end(extra_data); + } + + const auto serialized = adaptive_mesh.serialize(); + internal::dump_file(location, serialized); + + BKE_mesh_eval_delete(cloth_to_object_res); +} + Mesh *BKE_cloth_remesh(Object *ob, ClothModifierData *clmd, Mesh *mesh) { if (clmd->prev_frame_mesh) { |