Welcome to mirror list, hosted at ThFree Co, Russian Federation.

git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJacques Lucke <jacques@blender.org>2020-07-22 15:16:08 +0300
committerJacques Lucke <jacques@blender.org>2020-07-22 15:16:08 +0300
commit980dd43bd4be7d5f2d893148984e5fd96c5747f4 (patch)
treebcfd4a1f02358f97ea1804f81f469689be00f31a /source/blender/simulation/intern/simulation_collect_influences.cc
parent47b6c332587ca45eddc98e75b3f563e2f17a0d47 (diff)
Particles: give emitter its own state
High quality emitters need to maintain state themselves. For example, this it needs to remember when it spawned the last particle. This is especially important when the birth rate is changing over time. Otherwise, there will be very visible artifacts. It is quite likely that other components of the simulation need their own state as well. Therefore, I refactored the `SimulationState` type a bit, to make it more extensible. Instead of using hardcoded type numbers, a string is used to identify the state type. Also, instead of having switch statements in many places, there is a new `SimulationStateType` that encapsulates information about how a specific state is created/freed/copied/... I removed the integration with the point cache for now, because it was not used anyway in it's current state.
Diffstat (limited to 'source/blender/simulation/intern/simulation_collect_influences.cc')
-rw-r--r--source/blender/simulation/intern/simulation_collect_influences.cc51
1 files changed, 36 insertions, 15 deletions
diff --git a/source/blender/simulation/intern/simulation_collect_influences.cc b/source/blender/simulation/intern/simulation_collect_influences.cc
index 4760063daca..764e587d157 100644
--- a/source/blender/simulation/intern/simulation_collect_influences.cc
+++ b/source/blender/simulation/intern/simulation_collect_influences.cc
@@ -280,17 +280,30 @@ static void collect_forces(nodes::MFNetworkTreeMap &network_map,
class MyBasicEmitter : public ParticleEmitter {
private:
Array<std::string> names_;
+ std::string my_state_;
const fn::MultiFunction &inputs_fn_;
uint32_t seed_;
public:
- MyBasicEmitter(Array<std::string> names, const fn::MultiFunction &inputs_fn, uint32_t seed)
- : names_(std::move(names)), inputs_fn_(inputs_fn), seed_(seed)
+ MyBasicEmitter(Array<std::string> names,
+ std::string my_state,
+ const fn::MultiFunction &inputs_fn,
+ uint32_t seed)
+ : names_(std::move(names)),
+ my_state_(std::move(my_state)),
+ inputs_fn_(inputs_fn),
+ seed_(seed)
{
}
void emit(ParticleEmitterContext &context) const override
{
+ auto *state = context.solve_context().state_map().lookup<ParticleMeshEmitterSimulationState>(
+ my_state_);
+ if (state == nullptr) {
+ return;
+ }
+
fn::MFContextBuilder mf_context;
mf_context.add_global_context("PersistentDataHandleMap",
&context.solve_context().handle_map());
@@ -311,15 +324,17 @@ class MyBasicEmitter : public ParticleEmitter {
Vector<float3> new_velocities;
Vector<float> new_birth_times;
- float start_time = context.simulation_time_interval().start();
+ TimeInterval time_interval = context.simulation_time_interval();
+ float start_time = time_interval.start();
RandomNumberGenerator rng{(*(uint32_t *)&start_time) ^ seed_};
- int amount = rate * 10;
- for (int i : IndexRange(amount)) {
- UNUSED_VARS(i);
+ const float time_between_particles = 1.0f / rate;
+ while (state->last_birth_time + time_between_particles < time_interval.end()) {
new_positions.append(rng.get_unit_float3() * 0.3 + float3(object->loc));
new_velocities.append(rng.get_unit_float3());
- new_birth_times.append(context.simulation_time_interval().start());
+ const float birth_time = state->last_birth_time + time_between_particles;
+ new_birth_times.append(birth_time);
+ state->last_birth_time = birth_time;
}
for (StringRef name : names_) {
@@ -328,6 +343,7 @@ class MyBasicEmitter : public ParticleEmitter {
return;
}
+ int amount = new_positions.size();
fn::MutableAttributesRef attributes = allocator->allocate(amount);
initialized_copy_n(new_positions.data(), amount, attributes.get<float3>("Position").data());
@@ -352,7 +368,8 @@ static Vector<const nodes::DNode *> find_linked_particle_simulations(
static ParticleEmitter *create_particle_emitter(const nodes::DNode &dnode,
ResourceCollector &resources,
- nodes::MFNetworkTreeMap &network_map)
+ nodes::MFNetworkTreeMap &network_map,
+ RequiredStates &r_required_states)
{
Vector<const nodes::DNode *> simulation_dnodes = find_linked_particle_simulations(
dnode.output(0));
@@ -377,19 +394,23 @@ static ParticleEmitter *create_particle_emitter(const nodes::DNode &dnode,
fn::MultiFunction &inputs_fn = resources.construct<fn::MFNetworkEvaluator>(
AT, Span<const fn::MFOutputSocket *>(), input_sockets.as_span());
- uint32_t seed = DefaultHash<std::string>{}(dnode_to_path(dnode));
+ std::string my_state_name = dnode_to_path(dnode);
+ r_required_states.add(my_state_name, SIM_TYPE_NAME_PARTICLE_MESH_EMITTER);
+ uint32_t seed = DefaultHash<std::string>{}(my_state_name);
ParticleEmitter &emitter = resources.construct<MyBasicEmitter>(
- AT, std::move(names), inputs_fn, seed);
+ AT, std::move(names), std::move(my_state_name), inputs_fn, seed);
return &emitter;
}
static void collect_emitters(nodes::MFNetworkTreeMap &network_map,
ResourceCollector &resources,
- SimulationInfluences &r_influences)
+ SimulationInfluences &r_influences,
+ RequiredStates &r_required_states)
{
for (const nodes::DNode *dnode :
network_map.tree().nodes_by_type("SimulationNodeParticleMeshEmitter")) {
- ParticleEmitter *emitter = create_particle_emitter(*dnode, resources, network_map);
+ ParticleEmitter *emitter = create_particle_emitter(
+ *dnode, resources, network_map, r_required_states);
if (emitter != nullptr) {
r_influences.particle_emitters.append(emitter);
}
@@ -433,7 +454,7 @@ static void find_used_data_blocks(const nodes::DerivedNodeTree &tree,
void collect_simulation_influences(Simulation &simulation,
ResourceCollector &resources,
SimulationInfluences &r_influences,
- SimulationStatesInfo &r_states_info)
+ RequiredStates &r_required_states)
{
nodes::NodeTreeRefMap tree_refs;
const nodes::DerivedNodeTree tree{simulation.nodetree, tree_refs};
@@ -452,10 +473,10 @@ void collect_simulation_influences(Simulation &simulation,
// WM_clipboard_text_set(network.to_dot().c_str(), false);
collect_forces(network_map, resources, data_sources, r_influences);
- collect_emitters(network_map, resources, r_influences);
+ collect_emitters(network_map, resources, r_influences, r_required_states);
for (const nodes::DNode *dnode : get_particle_simulation_nodes(tree)) {
- r_states_info.particle_simulation_names.add(dnode_to_path(*dnode));
+ r_required_states.add(dnode_to_path(*dnode), SIM_TYPE_NAME_PARTICLE_SIMULATION);
}
find_used_data_blocks(tree, r_influences);