diff options
Diffstat (limited to 'source/blender/simulation/intern/simulation_collect_influences.cc')
-rw-r--r-- | source/blender/simulation/intern/simulation_collect_influences.cc | 51 |
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); |