Skip to content

Commit

Permalink
A whole lot of work trying to add a second fn
Browse files Browse the repository at this point in the history
  • Loading branch information
Masterchef365 committed Mar 5, 2024
1 parent 52bfcbd commit f02374a
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 58 deletions.
62 changes: 40 additions & 22 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ pub struct TemplateApp {
show_settings_only: bool,

advanced: bool,

node_graph_fn_viewed: NodeGraphFns,
}

impl TemplateApp {
Expand Down Expand Up @@ -105,6 +107,8 @@ impl TemplateApp {
}
}

#[derive(Clone, Copy, PartialEq, Eq)]
#[derive(serde::Serialize, serde::Deserialize)]
enum NodeGraphFns {
PerNeighbor,
PerParticle,
Expand Down Expand Up @@ -144,7 +148,10 @@ impl eframe::App for TemplateApp {

if self.tweak.particle_mode == ParticleBehaviourMode::NodeGraph {
SidePanel::right("NodeGraph").show(ctx, |ui| {
self.per_neighbor_fn.show(ui);
match self.node_graph_fn_viewed {
NodeGraphFns::PerNeighbor => self.per_neighbor_fn.show(ui),
NodeGraphFns::PerParticle => self.per_particle_fn.show(ui),
}
});
}

Expand Down Expand Up @@ -179,12 +186,16 @@ impl TemplateApp {
}
}

let node = vorpal_widgets::vorpal_core::highlevel::convert_node(
let per_neighbor_node = vorpal_widgets::vorpal_core::highlevel::convert_node(
self.per_neighbor_fn.extract_output_node(),
);

let per_particle_node = vorpal_widgets::vorpal_core::highlevel::convert_node(
self.per_particle_fn.extract_output_node(),
);

self.sim
.step(&self.tweak, &self.life, &self.node_cfg, &node);
.step(&self.tweak, &self.life, &self.node_cfg, &per_neighbor_node, &per_particle_node);

self.single_step = false;
}
Expand Down Expand Up @@ -244,6 +255,26 @@ impl TemplateApp {
}

fn settings_gui(&mut self, ui: &mut Ui) {
ui.separator();
ui.label("Particle behaviour");
ui.horizontal(|ui| {
ui.selectable_value(
&mut self.tweak.particle_mode,
ParticleBehaviourMode::Off,
"Off",
);
ui.selectable_value(
&mut self.tweak.particle_mode,
ParticleBehaviourMode::NodeGraph,
"Node graph",
);
ui.selectable_value(
&mut self.tweak.particle_mode,
ParticleBehaviourMode::ParticleLife,
"Particle life",
);
});

let mut reset = false;
ui.separator();
ui.strong("Simulation state");
Expand Down Expand Up @@ -440,26 +471,12 @@ impl TemplateApp {
});
ui.checkbox(&mut self.well, "Particle well");

ui.separator();
ui.horizontal(|ui| {
ui.selectable_value(
&mut self.tweak.particle_mode,
ParticleBehaviourMode::Off,
"Off",
);
ui.selectable_value(
&mut self.tweak.particle_mode,
ParticleBehaviourMode::NodeGraph,
"Node graph",
);
ui.selectable_value(
&mut self.tweak.particle_mode,
ParticleBehaviourMode::ParticleLife,
"Particle life",
);
});

if self.tweak.particle_mode == ParticleBehaviourMode::NodeGraph {
ui.strong("Node graph configuration");
ui.horizontal(|ui| {
ui.selectable_value(&mut self.node_graph_fn_viewed, NodeGraphFns::PerNeighbor, "Per-neighbor accel");
ui.selectable_value(&mut self.node_graph_fn_viewed, NodeGraphFns::PerParticle, "Per-particle accel");
});
ui.add(
DragValue::new(&mut self.node_cfg.neighbor_radius)
.clamp_range(1e-2..=20.0)
Expand All @@ -469,6 +486,7 @@ impl TemplateApp {
}

if self.tweak.particle_mode == ParticleBehaviourMode::ParticleLife {
ui.strong("Particle life configuration");
let mut behav_cfg = self.life.behaviours[(0, 0)];
if self.advanced {
ui.add(
Expand Down
113 changes: 77 additions & 36 deletions src/sim.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ impl Sim {
}
}

pub fn step(&mut self, tweak: &SimTweak, life: &LifeConfig, node_cfg: &NodeInteractionCfg, nodes: &Rc<Node>) {
pub fn step(&mut self, tweak: &SimTweak, life: &LifeConfig, node_cfg: &NodeInteractionCfg, per_neighbor_nodes: &Rc<Node>, per_particle_nodes: &Rc<Node>) {
//puffin::profile_scope!("Complete Step");
// Step particles
apply_global_force(&mut self.particles, Vec2::new(0., -tweak.gravity), tweak.dt);
Expand All @@ -181,7 +181,8 @@ impl Sim {
particle_life_interactions(&mut self.particles, life, tweak.dt)
}
ParticleBehaviourMode::NodeGraph => {
node_interactions(&mut self.particles, nodes, node_cfg, tweak.dt)
per_neighbor_node_interactions(&mut self.particles, per_neighbor_nodes, node_cfg, tweak.dt);
per_particle_node_interactions(&mut self.particles, per_particle_nodes, node_cfg, tweak.dt);
}
ParticleBehaviourMode::Off => (),
}
Expand Down Expand Up @@ -689,14 +690,86 @@ impl Default for NodeInteractionCfg {
}
}

fn node_interactions(
pub fn per_particle_fn_inputs() -> ParameterList {
let params = [
(
ExternInputId::new("neigh-radius".to_string()),
DataType::Scalar,
),
(ExternInputId::new("our-type".into()), DataType::Scalar),
(ExternInputId::new("position".into()), DataType::Vec2),
(ExternInputId::new("velocity".into()), DataType::Vec2),
]
.into_iter()
.collect();

ParameterList(params)
}

fn per_particle_node_interactions(
particles: &mut [Particle],
node: &Rc<Node>,
cfg: &NodeInteractionCfg,
dt: f32,
) {
//evaluate_node(node, ctx)
puffin::profile_scope!("Particle interactions");
puffin::profile_scope!("Per-Particle node interactions");
for i in 0..particles.len() {
// The vector pointing from a to b
//let diff = points[neighbor] - points[i];

let inputs = [
(
ExternInputId::new("our-type".into()),
Value::Scalar(particles[i].color as f32),
),
(
ExternInputId::new("position".into()),
Value::Vec2(particles[i].pos.to_array()),
),
(
ExternInputId::new("velocity".into()),
Value::Vec2(particles[i].vel.to_array()),
),
];
let params = ExternParameters {
inputs: inputs.into_iter().collect(),
};

let ret = evaluate_node(&node, &params);
let Value::Vec2([fx, fy]) = ret.as_ref().unwrap() else {
panic!("{:?}", &ret);
};
particles[i].vel += dt * Vec2::new(*fx, *fy);
}
}

pub fn per_neighbor_fn_inputs() -> ParameterList {
let params = [
(
ExternInputId::new("neigh-radius".to_string()),
DataType::Scalar,
),
(ExternInputId::new("pos-diff".to_string()), DataType::Vec2),
(ExternInputId::new("neigh-type".into()), DataType::Scalar),
(ExternInputId::new("our-type".into()), DataType::Scalar),
(ExternInputId::new("position".into()), DataType::Vec2),
(ExternInputId::new("velocity".into()), DataType::Vec2),
]
.into_iter()
.collect();

ParameterList(params)
}

fn per_neighbor_node_interactions(
particles: &mut [Particle],
node: &Rc<Node>,
cfg: &NodeInteractionCfg,
dt: f32,
) {
//evaluate_node(node, ctx)
puffin::profile_scope!("Per-Neighbor node interactions");
let points: Vec<Vec2> = particles.iter().map(|p| p.pos).collect();
let accel = QueryAccelerator::new(&points, cfg.neighbor_radius);
//accel.stats("Life");
Expand Down Expand Up @@ -745,39 +818,7 @@ fn node_interactions(
}
}

pub fn per_particle_fn_inputs() -> ParameterList {
let params = [
(
ExternInputId::new("neigh-radius".to_string()),
DataType::Scalar,
),
(ExternInputId::new("our-type".into()), DataType::Scalar),
(ExternInputId::new("position".into()), DataType::Vec2),
(ExternInputId::new("velocity".into()), DataType::Vec2),
]
.into_iter()
.collect();

ParameterList(params)
}

pub fn per_neighbor_fn_inputs() -> ParameterList {
let params = [
(
ExternInputId::new("neigh-radius".to_string()),
DataType::Scalar,
),
(ExternInputId::new("pos-diff".to_string()), DataType::Vec2),
(ExternInputId::new("neigh-type".into()), DataType::Scalar),
(ExternInputId::new("our-type".into()), DataType::Scalar),
(ExternInputId::new("position".into()), DataType::Vec2),
(ExternInputId::new("velocity".into()), DataType::Vec2),
]
.into_iter()
.collect();

ParameterList(params)
}

impl Default for SimTweak {
fn default() -> Self {
Expand Down

0 comments on commit f02374a

Please sign in to comment.