From b1f97995084253305f708f55dc5bd3b4c3acb301 Mon Sep 17 00:00:00 2001 From: Alexander Gavrilov Date: Wed, 10 Jun 2020 18:10:07 +0300 Subject: Cloth: implement support for a hydrostatic pressure gradient. When a fluid is put under influence of gravity or acceleration, it forms an internal pressure gradient, which causes observable effects like buoyancy. Since now cloth has support for simulating pressure changes caused by fluid compression or expansion, it makes sense to also support the effects of gravity. This is intended for better simulation of objects filled or surrounded by fluids, especially when constrained by collisions or pinned vertices, and should result in more realistic shapes. Obviously, this doesn't actually simulate fluid dynamics; instead it is assumed that the fluid immediately adapts to changes in the shape or acceleration of the object without friction or turbulence, and instantly reaches a new static equilibrium. Differential Revision: https://developer.blender.org/D6442 --- source/blender/blenkernel/BKE_cloth.h | 7 +-- source/blender/blenkernel/intern/cloth.c | 1 + source/blender/blenkernel/intern/pointcache.c | 62 +++++++++++++++++++++------ 3 files changed, 54 insertions(+), 16 deletions(-) (limited to 'source/blender/blenkernel') diff --git a/source/blender/blenkernel/BKE_cloth.h b/source/blender/blenkernel/BKE_cloth.h index f5073ff7aa5..f25482570b1 100644 --- a/source/blender/blenkernel/BKE_cloth.h +++ b/source/blender/blenkernel/BKE_cloth.h @@ -93,9 +93,10 @@ typedef struct Cloth { struct Implicit_Data *implicit; /* our implicit solver connects to this pointer */ struct EdgeSet *edgeset; /* used for selfcollisions */ int last_frame; - float initial_mesh_volume; /* Initial volume of the mesh. Used for pressure */ - struct MEdge *edges; /* Used for hair collisions. */ - struct GHash *sew_edge_graph; /* Sewing edges represented using a GHash */ + float initial_mesh_volume; /* Initial volume of the mesh. Used for pressure */ + float average_acceleration[3]; /* Moving average of overall acceleration. */ + struct MEdge *edges; /* Used for hair collisions. */ + struct GHash *sew_edge_graph; /* Sewing edges represented using a GHash */ } Cloth; /** diff --git a/source/blender/blenkernel/intern/cloth.c b/source/blender/blenkernel/intern/cloth.c index 0b9780ac81c..ce6143116a1 100644 --- a/source/blender/blenkernel/intern/cloth.c +++ b/source/blender/blenkernel/intern/cloth.c @@ -146,6 +146,7 @@ void cloth_init(ClothModifierData *clmd) clmd->sim_parms->uniform_pressure_force = 0.0f; clmd->sim_parms->target_volume = 0.0f; clmd->sim_parms->pressure_factor = 1.0f; + clmd->sim_parms->fluid_density = 0.0f; clmd->sim_parms->vgroup_pressure = 0; // also from softbodies diff --git a/source/blender/blenkernel/intern/pointcache.c b/source/blender/blenkernel/intern/pointcache.c index 9cd17310f07..836fe59d7bd 100644 --- a/source/blender/blenkernel/intern/pointcache.c +++ b/source/blender/blenkernel/intern/pointcache.c @@ -138,6 +138,7 @@ static int ptcache_data_size[] = { static int ptcache_extra_datasize[] = { 0, sizeof(ParticleSpring), + sizeof(float) * 3, }; /* forward declarations */ @@ -176,6 +177,23 @@ static int ptcache_basic_header_write(PTCacheFile *pf) return 1; } +static void ptcache_add_extra_data(PTCacheMem *pm, + unsigned int type, + unsigned int count, + void *data) +{ + PTCacheExtra *extra = MEM_callocN(sizeof(PTCacheExtra), "Point cache: extra data descriptor"); + + extra->type = type; + extra->totdata = count; + + size_t size = extra->totdata * ptcache_extra_datasize[extra->type]; + + extra->data = MEM_mallocN(size, "Point cache: extra data"); + memcpy(extra->data, data, size); + + BLI_addtail(&pm->extradata, extra); +} /* Softbody functions */ static int ptcache_softbody_write(int index, void *soft_v, void **data, int UNUSED(cfra)) { @@ -467,21 +485,12 @@ static int ptcache_particle_totwrite(void *psys_v, int cfra) static void ptcache_particle_extra_write(void *psys_v, PTCacheMem *pm, int UNUSED(cfra)) { ParticleSystem *psys = psys_v; - PTCacheExtra *extra = NULL; if (psys->part->phystype == PART_PHYS_FLUID && psys->part->fluid && psys->part->fluid->flag & SPH_VISCOELASTIC_SPRINGS && psys->tot_fluidsprings && psys->fluid_springs) { - extra = MEM_callocN(sizeof(PTCacheExtra), "Point cache: fluid extra data"); - - extra->type = BPHYS_EXTRA_FLUID_SPRINGS; - extra->totdata = psys->tot_fluidsprings; - - extra->data = MEM_callocN(extra->totdata * ptcache_extra_datasize[extra->type], - "Point cache: extra data"); - memcpy(extra->data, psys->fluid_springs, extra->totdata * ptcache_extra_datasize[extra->type]); - - BLI_addtail(&pm->extradata, extra); + ptcache_add_extra_data( + pm, BPHYS_EXTRA_FLUID_SPRINGS, psys->tot_fluidsprings, psys->fluid_springs); } } @@ -575,6 +584,33 @@ static void ptcache_cloth_interpolate( /* should vert->xconst be interpolated somehow too? - jahka */ } +static void ptcache_cloth_extra_write(void *cloth_v, PTCacheMem *pm, int UNUSED(cfra)) +{ + ClothModifierData *clmd = cloth_v; + Cloth *cloth = clmd->clothObject; + + if (!is_zero_v3(cloth->average_acceleration)) { + ptcache_add_extra_data(pm, BPHYS_EXTRA_CLOTH_ACCELERATION, 1, cloth->average_acceleration); + } +} +static void ptcache_cloth_extra_read(void *cloth_v, PTCacheMem *pm, float UNUSED(cfra)) +{ + ClothModifierData *clmd = cloth_v; + Cloth *cloth = clmd->clothObject; + PTCacheExtra *extra = pm->extradata.first; + + zero_v3(cloth->average_acceleration); + + for (; extra; extra = extra->next) { + switch (extra->type) { + case BPHYS_EXTRA_CLOTH_ACCELERATION: { + copy_v3_v3(cloth->average_acceleration, extra->data); + break; + } + } + } +} + static int ptcache_cloth_totpoint(void *cloth_v, int UNUSED(cfra)) { ClothModifierData *clmd = cloth_v; @@ -1681,8 +1717,8 @@ void BKE_ptcache_id_from_cloth(PTCacheID *pid, Object *ob, ClothModifierData *cl pid->write_stream = NULL; pid->read_stream = NULL; - pid->write_extra_data = NULL; - pid->read_extra_data = NULL; + pid->write_extra_data = ptcache_cloth_extra_write; + pid->read_extra_data = ptcache_cloth_extra_read; pid->interpolate_extra_data = NULL; pid->write_header = ptcache_basic_header_write; -- cgit v1.2.3