From 6f96dfabe5cbf6966282decd2307ade9aacdeaee Mon Sep 17 00:00:00 2001 From: Jacques Lucke Date: Mon, 8 Jun 2020 15:41:41 +0200 Subject: Simulations: initial simulation state and cache The current particle state is stored in a `CustomData` instance and the cache is stored in `PointCache`. The current state exists on the copy-on-write copies of the simulation, while the cache only exists in the original data block. This patch implements a temporary trivial particle simulation that does not use the node system yet. It is used for testing and will be replaced soon. `PointCache` still has some limitations that need to be overcome using separate refactorings. For example, we need to be able to store the number of particles in the point cache. Also we need to change which attributes are stored for a particle system more dynamically than is currently possible afaik. Reviewers: brecht Differential Revision: https://developer.blender.org/D7836 --- source/blender/modifiers/intern/MOD_simulation.cc | 41 +++++++++++++++++++++-- 1 file changed, 39 insertions(+), 2 deletions(-) (limited to 'source/blender/modifiers/intern') diff --git a/source/blender/modifiers/intern/MOD_simulation.cc b/source/blender/modifiers/intern/MOD_simulation.cc index f126d1af959..5ccf945b5a6 100644 --- a/source/blender/modifiers/intern/MOD_simulation.cc +++ b/source/blender/modifiers/intern/MOD_simulation.cc @@ -21,11 +21,14 @@ * \ingroup modifiers */ +#include #include #include #include "MEM_guardedalloc.h" +#include "BLI_float3.hh" +#include "BLI_listbase.h" #include "BLI_utildefines.h" #include "DNA_mesh_types.h" @@ -41,6 +44,8 @@ #include "BKE_lib_query.h" #include "BKE_mesh.h" #include "BKE_modifier.h" +#include "BKE_pointcloud.h" +#include "BKE_simulation.h" /* SpaceType struct has a member called 'new' which obviously conflicts with C++ * so temporarily redefining the new keyword to make it compile. */ @@ -59,6 +64,8 @@ #include "MOD_modifiertypes.h" #include "MOD_ui_common.h" +using BLI::float3; + static void updateDepsgraph(ModifierData *md, const ModifierUpdateDepsgraphContext *ctx) { SimulationModifierData *smd = (SimulationModifierData *)md; @@ -81,12 +88,42 @@ static bool isDisabled(const struct Scene *UNUSED(scene), return smd->simulation == nullptr; } +static const ParticleSimulationState *find_particle_state(SimulationModifierData *smd) +{ + if (smd->simulation == nullptr) { + return nullptr; + } + LISTBASE_FOREACH (const SimulationState *, state, &smd->simulation->states) { + if (state->type == SIM_STATE_TYPE_PARTICLES) { + return (ParticleSimulationState *)state; + } + } + return nullptr; +} + static PointCloud *modifyPointCloud(ModifierData *md, const ModifierEvalContext *UNUSED(ctx), - PointCloud *pointcloud) + PointCloud *input_pointcloud) { SimulationModifierData *smd = (SimulationModifierData *)md; - UNUSED_VARS(smd); + const ParticleSimulationState *state = find_particle_state(smd); + if (state == nullptr) { + return input_pointcloud; + } + + PointCloud *pointcloud = BKE_pointcloud_new_for_eval(input_pointcloud, state->tot_particles); + if (state->tot_particles == 0) { + return pointcloud; + } + + const float3 *positions = (const float3 *)CustomData_get_layer_named( + &state->attributes, CD_LOCATION, "Position"); + memcpy(pointcloud->co, positions, sizeof(float3) * state->tot_particles); + + for (int i = 0; i < state->tot_particles; i++) { + pointcloud->radius[i] = 0.05f; + } + return pointcloud; } -- cgit v1.2.3