Commit: 7cd2c1fd2e7a4c82dd5569cf0ee0155c3cca9101
Author: Jacques Lucke
Date: Sat Aug 1 21:56:05 2020 +0200
Branches: master
https://developer.blender.org/rB7cd2c1fd2e7a4c82dd5569cf0ee0155c3cca9101
Particles: support custom particle events in solver
Previously, there were only particle-birth and time-step events.
Now the solver can handle custom events. On the user level
this does not change anything yet. This feature of the solver
will be used by an upcoming Age Reached Event node and
possibly others. When this node exists, I can finally remove
the hardcoded maximum particle age.
===================================================================
M source/blender/simulation/intern/simulation_collect_influences.cc
M source/blender/simulation/intern/simulation_solver.cc
M source/blender/simulation/intern/simulation_solver_influences.cc
M source/blender/simulation/intern/simulation_solver_influences.hh
===================================================================
diff --git a/source/blender/simulation/intern/simulation_collect_influences.cc
b/source/blender/simulation/intern/simulation_collect_influences.cc
index c744defb7d5..c309d18c43a 100644
--- a/source/blender/simulation/intern/simulation_collect_influences.cc
+++ b/source/blender/simulation/intern/simulation_collect_influences.cc
@@ -674,12 +674,18 @@ class ParticleConditionAction : public ParticleAction {
}
if (action_true_ != nullptr) {
- ParticleChunkContext chunk_context{true_indices.as_span(),
context.particles.attributes};
+ ParticleChunkContext chunk_context{context.particles.state,
+ true_indices.as_span(),
+ context.particles.attributes,
+ context.particles.integration};
ParticleActionContext action_context{context.solve_context,
chunk_context};
action_true_->execute(action_context);
}
if (action_false_ != nullptr) {
- ParticleChunkContext chunk_context{false_indices.as_span(),
context.particles.attributes};
+ ParticleChunkContext chunk_context{context.particles.state,
+ false_indices.as_span(),
+ context.particles.attributes,
+ context.particles.integration};
ParticleActionContext action_context{context.solve_context,
chunk_context};
action_false_->execute(action_context);
}
@@ -753,6 +759,63 @@ static void optimize_function_network(CollectContext
&context)
// WM_clipboard_text_set(network.to_dot().c_str(), false);
}
+class AgeReachedEvent : public ParticleEvent {
+ private:
+ std::string attribute_name_;
+
+ public:
+ AgeReachedEvent(std::string attribute_name) :
attribute_name_(std::move(attribute_name))
+ {
+ }
+
+ void filter(ParticleEventFilterContext &context) const override
+ {
+ Span<float> birth_times = context.particles.attributes.get<float>("Birth
Time");
+ Span<int> has_been_triggered =
context.particles.attributes.get<int>(attribute_name_);
+ const float age = 5.0f;
+
+ const float end_time = context.particles.integration->end_time;
+ for (int i : context.particles.index_mask) {
+ if (has_been_triggered[i]) {
+ continue;
+ }
+ const float birth_time = birth_times[i];
+ const float trigger_time = birth_time + age;
+ if (trigger_time > end_time) {
+ continue;
+ }
+
+ const float duration = context.particles.integration->durations[i];
+ TimeInterval interval(end_time - duration, duration);
+ const float time_factor = interval.safe_factor_at_time(trigger_time);
+
+ context.factor_dst[i] = std::max<float>(0.0f, time_factor);
+ }
+ }
+
+ void execute(ParticleActionContext &context) const override
+ {
+ MutableSpan<int> dead_states =
context.particles.attributes.get<int>("Dead");
+ MutableSpan<int> has_been_triggered =
context.particles.attributes.get<int>(attribute_name_);
+ for (int i : context.particles.index_mask) {
+ dead_states[i] = true;
+ has_been_triggered[i] = 1;
+ }
+ }
+};
+
+static void collect_age_reached_events(CollectContext &context)
+{
+ /* TODO: Actually implement an Age Reached Event node. */
+ std::string attribute_name = "Has Been Triggered";
+ const AgeReachedEvent &event =
context.resources.construct<AgeReachedEvent>(AT, attribute_name);
+ for (const DNode *dnode : context.particle_simulation_nodes) {
+ StringRefNull name = get_identifier(context, *dnode);
+ context.influences.particle_events.add_as(name, &event);
+
context.influences.particle_attributes_builder.lookup_as(name)->add<int>(attribute_name,
0);
+ }
+}
+
void collect_simulation_influences(Simulation &simulation,
ResourceCollector &resources,
SimulationInfluences &r_influences,
@@ -774,6 +837,7 @@ void collect_simulation_influences(Simulation &simulation,
collect_emitters(context);
collect_birth_events(context);
collect_time_step_events(context);
+ collect_age_reached_events(context);
optimize_function_network(context);
diff --git a/source/blender/simulation/intern/simulation_solver.cc
b/source/blender/simulation/intern/simulation_solver.cc
index 3c159eb1c58..9f2766aa24f 100644
--- a/source/blender/simulation/intern/simulation_solver.cc
+++ b/source/blender/simulation/intern/simulation_solver.cc
@@ -121,6 +121,170 @@ static void
ensure_attributes_exist(ParticleSimulationState *state, const Attrib
}
}
+BLI_NOINLINE static void apply_remaining_diffs(ParticleChunkContext &context)
+{
+ BLI_assert(context.integration != nullptr);
+ MutableSpan<float3> positions = context.attributes.get<float3>("Position");
+ MutableSpan<float3> velocities = context.attributes.get<float3>("Velocity");
+
+ for (int i : context.index_mask) {
+ positions[i] += context.integration->position_diffs[i];
+ velocities[i] += context.integration->velocity_diffs[i];
+ }
+}
+
+BLI_NOINLINE static void find_next_event_per_particle(
+ SimulationSolveContext &solve_context,
+ ParticleChunkContext &particles,
+ Span<const ParticleEvent *> events,
+ MutableSpan<int> r_next_event_indices,
+ MutableSpan<float> r_time_factors_to_next_event)
+{
+ r_next_event_indices.fill_indices(particles.index_mask, -1);
+ r_time_factors_to_next_event.fill_indices(particles.index_mask, 1.0f);
+
+ Array<float> time_factors(particles.index_mask.min_array_size(), -1.0f);
+ for (int event_index : events.index_range()) {
+ ParticleEventFilterContext event_context{solve_context, particles,
time_factors};
+ const ParticleEvent &event = *events[event_index];
+ event.filter(event_context);
+
+ for (int i : particles.index_mask) {
+ const float time_factor = time_factors[i];
+ const float previously_smallest_time_factor =
r_time_factors_to_next_event[i];
+ if (time_factor >= 0.0f && time_factor <=
previously_smallest_time_factor) {
+ r_time_factors_to_next_event[i] = time_factor;
+ r_next_event_indices[i] = event_index;
+ }
+ }
+ }
+}
+
+BLI_NOINLINE static void forward_particles_to_next_event_or_end(
+ ParticleChunkContext &particles, Span<float> time_factors_to_next_event)
+{
+ MutableSpan<float3> positions = particles.attributes.get<float3>("Position");
+ MutableSpan<float3> velocities =
particles.attributes.get<float3>("Velocity");
+
+ MutableSpan<float3> position_diffs = particles.integration->position_diffs;
+ MutableSpan<float3> velocity_diffs = particles.integration->velocity_diffs;
+ MutableSpan<float> durations = particles.integration->durations;
+
+ for (int i : particles.index_mask) {
+ const float time_factor = time_factors_to_next_event[i];
+ positions[i] += position_diffs[i] * time_factor;
+ velocities[i] += velocity_diffs[i] * time_factor;
+
+ const float remaining_time_factor = 1.0f - time_factor;
+ position_diffs[i] *= remaining_time_factor;
+ velocity_diffs[i] *= remaining_time_factor;
+ durations[i] *= remaining_time_factor;
+ }
+}
+
+BLI_NOINLINE static void group_particles_by_event(
+ IndexMask mask,
+ Span<int> next_event_indices,
+ MutableSpan<Vector<int64_t>> r_particles_per_event)
+{
+ for (int i : mask) {
+ int event_index = next_event_indices[i];
+ if (event_index >= 0) {
+ r_particles_per_event[event_index].append(i);
+ }
+ }
+}
+
+BLI_NOINLINE static void execute_events(SimulationSolveContext &solve_context,
+ ParticleChunkContext &all_particles,
+ Span<const ParticleEvent *> events,
+ Span<Vector<int64_t>>
particles_per_event)
+{
+ for (int event_index : events.index_range()) {
+ Span<int64_t> pindices = particles_per_event[event_index];
+ if (pindices.is_empty()) {
+ continue;
+ }
+
+ const ParticleEvent &event = *events[event_index];
+ ParticleChunkContext particles{
+ all_particles.state, pindices, all_particles.attributes,
all_particles.integration};
+ ParticleActionContext action_context{solve_context, particles};
+ event.execute(action_context);
+ }
+}
+
+BLI_NOINLINE static void find_unfinished_particles(IndexMask index_mask,
+ Span<float>
time_factors_to_next_event,
+ Vector<int64_t>
&r_unfinished_pindices)
+{
+ for (int i : index_mask) {
+ float time_factor = time_factors_to_next_event[i];
+ if (time_factor < 1.0f) {
+ r_unfinished_pindices.append(i);
+ }
+ }
+}
+
+BLI_NOINLINE static void simulate_to_next_event(SimulationSolveContext
&solve_context,
+ ParticleChunkContext
&particles,
+ Span<const ParticleEvent *>
events,
+ Vector<int64_t>
&r_unfinished_pindices)
+{
+ int array_size = particles.index_mask.min_array_size();
+ Array<int> next_event_indices(array_size);
+ Array<float> time_factors_to_next_event(array_size);
+
+ find_next_event_per_particle(
+ solve_context, particles, events, next_event_indices,
time_factors_to_next_event);
+
+ forward_particles_to_next_event_or_end(particles,
time_factors_to_next_event);
+
+ Array<Vector<int64_t>> particles_per_event(events.size());
+ group_particles_by_event(particles.index_mask, next_event_indices,
particles_per_event);
+
+ execute_events(solve_context, particles, events, particles_per_event);
+ find_unfinished_particles(
+ particles.index_mask, time_factors_to_next_event, r_unfinished_pindices);
+}
+
+BLI_NOINLINE static void simulate_with_max_n_events(SimulationSolveContext
&solve_context,
+ ParticleSimulationState
&state,
+ ParticleChunkContext
&particles,
+ int max_events)
+{
+ Span<const ParticleEvent *> events = solve_context.influences.particl
@@ Diff output truncated at 10240 characters. @@
_______________________________________________
Bf-blender-cvs mailing list
[email protected]
https://lists.blender.org/mailman/listinfo/bf-blender-cvs