Skip to content

Commit

Permalink
Implement particle groups, enabling simple particle trails.
Browse files Browse the repository at this point in the history
This commit essentially allows multiple effects to share the same
underlying buffer. Such sub-effects are known as *particle groups*. An
effect may have up to 32 particle groups, each with its own capacity.
Every modifier specifies which particle groups it affects. Spawners
always spawn into particle group 0; this restriction was implemented for
simplicity and can be lifted later.

A new modifier has been added, `CloneModifier`. This allows particles to
be duplicated into a group on a fixed rate. By cloning particles into a
group, particle trails can be created.

It's intended that a node graph can be layered on top of particle groups
in the future. Particle groups are the most efficient way I can think of
to implement node graphs in any case, so this seems to me to be a step
toward a node-based editor.

Currently, init modifiers don't run on cloned particles. Their age is,
however, reset to 0 if present, as this is typically the most useful
behavior.

Internally, each particle group receives its own update and render
invocation. This reduces branching in both shaders, simplifies much of
the bookkeeping, and enables ribbon rendering for trails in the future
(needed for weapon effects, etc.)

The new `worms` example illustrates how trails can be produced with this
groups feature.
  • Loading branch information
pcwalton committed Mar 6, 2024
1 parent 4933a35 commit 0c49f95
Show file tree
Hide file tree
Showing 36 changed files with 2,314 additions and 1,717 deletions.
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,10 @@ required-features = [ "bevy/bevy_winit", "bevy/bevy_pbr", "bevy/png", "3d" ]
name = "2d"
required-features = [ "bevy/bevy_winit", "bevy/bevy_sprite", "2d" ]

[[example]]
name = "worms"
required-features = [ "bevy/bevy_winit", "bevy/bevy_pbr", "3d" ]

[workspace]
resolver = "2"
members = ["."]
Binary file added assets/circle.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion examples/2d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ fn setup(
// By default the asset spawns the particles at Z=0.
let spawner = Spawner::rate(30.0.into());
let effect = effects.add(
EffectAsset::new(4096, spawner, writer.finish())
EffectAsset::new(vec![4096], spawner, writer.finish())
.with_name("2d")
.init(init_pos)
.init(init_vel)
Expand Down
7 changes: 2 additions & 5 deletions examples/activate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,7 @@ use bevy::{
log::LogPlugin,
prelude::*,
render::{
camera::{Projection, ScalingMode},
render_resource::WgpuFeatures,
settings::WgpuSettings,
RenderPlugin,
camera::ScalingMode, render_resource::WgpuFeatures, settings::WgpuSettings, RenderPlugin,
},
};
#[cfg(feature = "examples_world_inspector")]
Expand Down Expand Up @@ -136,7 +133,7 @@ fn setup(
};

let effect = effects.add(
EffectAsset::new(32768, spawner, writer.finish())
EffectAsset::new(vec![32768], spawner, writer.finish())
.with_name("activate")
.init(init_pos)
.init(init_vel)
Expand Down
2 changes: 1 addition & 1 deletion examples/billboard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ fn setup(
let rotation_attr = writer.attr(Attribute::F32_0).expr();

let effect = effects.add(
EffectAsset::new(32768, Spawner::rate(64.0.into()), writer.finish())
EffectAsset::new(vec![32768], Spawner::rate(64.0.into()), writer.finish())
.with_name("billboard")
.with_alpha_mode(bevy_hanabi::AlphaMode::Mask(alpha_cutoff))
.init(init_pos)
Expand Down
2 changes: 1 addition & 1 deletion examples/circle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ fn setup(

let effect = effects.add(
EffectAsset::new(
32768,
vec![32768],
Spawner::burst(32.0.into(), 8.0.into()),
writer.finish(),
)
Expand Down
2 changes: 1 addition & 1 deletion examples/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ fn setup(mut commands: Commands, mut effects: ResMut<Assets<EffectAsset>>) {
};

let effect = effects.add(
EffectAsset::new(32768, Spawner::rate(500.0.into()), writer.finish())
EffectAsset::new(vec![32768], Spawner::rate(500.0.into()), writer.finish())
.with_name("whirlwind")
.init(init_pos)
.init(init_age)
Expand Down
2 changes: 1 addition & 1 deletion examples/firework.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ fn setup(mut commands: Commands, mut effects: ResMut<Assets<EffectAsset>>) {
};

let effect = EffectAsset::new(
32768,
vec![32768],
Spawner::burst(2500.0.into(), 2.0.into()),
writer.finish(),
)
Expand Down
13 changes: 3 additions & 10 deletions examples/force_field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@ use bevy::{
core_pipeline::tonemapping::Tonemapping,
log::LogPlugin,
prelude::*,
render::{
camera::Projection, render_resource::WgpuFeatures, settings::WgpuSettings, RenderPlugin,
},
render::{render_resource::WgpuFeatures, settings::WgpuSettings, RenderPlugin},
};
#[cfg(feature = "examples_world_inspector")]
use bevy_inspector_egui::quick::WorldInspectorPlugin;
Expand Down Expand Up @@ -80,12 +78,7 @@ struct RepulsorMarker(pub bool);

#[cfg(feature = "examples_world_inspector")]
mod inspector {
use bevy::{
ecs::system::{Local, Resource},
prelude::*,
reflect::Reflect,
window::PrimaryWindow,
};
use bevy::{ecs::system::Resource, prelude::*, reflect::Reflect, window::PrimaryWindow};
use bevy_egui::EguiContext;
use bevy_hanabi::EffectProperties;
use bevy_inspector_egui::{inspector_options::std_options::NumberDisplay, prelude::*};
Expand Down Expand Up @@ -338,7 +331,7 @@ fn setup(

// Force field effects
let effect = effects.add(
EffectAsset::new(32768, spawner, writer.finish())
EffectAsset::new(vec![32768], spawner, writer.finish())
.with_name("force_field")
.with_property("repulsor_position", Value::Vector(REPULSOR_POS.into()))
.with_property("attraction_accel", Value::Scalar(20.0.into()))
Expand Down
2 changes: 1 addition & 1 deletion examples/gradient.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ fn setup(
};

let effect = effects.add(
EffectAsset::new(32768, Spawner::rate(1000.0.into()), writer.finish())
EffectAsset::new(vec![32768], Spawner::rate(1000.0.into()), writer.finish())
.with_name("gradient")
.init(init_pos)
.init(init_vel)
Expand Down
22 changes: 13 additions & 9 deletions examples/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,15 +72,19 @@ where

let init = make_modifier(&writer);

EffectAsset::new(32768, Spawner::once(COUNT.into(), true), writer.finish())
.with_name(name)
.with_simulation_space(SimulationSpace::Local)
.init(init)
.render(OrientModifier::new(OrientMode::FaceCameraPosition))
.render(SetColorModifier {
color: COLOR.into(),
})
.render(SetSizeModifier { size: SIZE.into() })
EffectAsset::new(
vec![32768],
Spawner::once(COUNT.into(), true),
writer.finish(),
)
.with_name(name)
.with_simulation_space(SimulationSpace::Local)
.init(init)
.render(OrientModifier::new(OrientMode::FaceCameraPosition))
.render(SetColorModifier {
color: COLOR.into(),
})
.render(SetSizeModifier { size: SIZE.into() })
}

fn spawn_effect(
Expand Down
2 changes: 1 addition & 1 deletion examples/instancing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ fn setup(
};

let effect = effects.add(
EffectAsset::new(512, Spawner::rate(50.0.into()), writer.finish())
EffectAsset::new(vec![512], Spawner::rate(50.0.into()), writer.finish())
.with_name("instancing")
.init(init_pos)
.init(init_vel)
Expand Down
6 changes: 3 additions & 3 deletions examples/lifetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ fn setup(
};
let effect1 = effects.add(
EffectAsset::new(
512,
vec![512],
Spawner::burst(50.0.into(), period.into()),
writer1.finish(),
)
Expand Down Expand Up @@ -172,7 +172,7 @@ fn setup(
};
let effect2 = effects.add(
EffectAsset::new(
512,
vec![512],
Spawner::burst(50.0.into(), period.into()),
writer2.finish(),
)
Expand Down Expand Up @@ -227,7 +227,7 @@ fn setup(
};
let effect3 = effects.add(
EffectAsset::new(
512,
vec![512],
Spawner::burst(50.0.into(), period.into()),
writer3.finish(),
)
Expand Down
2 changes: 1 addition & 1 deletion examples/multicam.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ fn make_effect(color: Color) -> EffectAsset {
speed: writer.lit(6.).expr(),
};

EffectAsset::new(32768, Spawner::rate(5.0.into()), writer.finish())
EffectAsset::new(vec![32768], Spawner::rate(5.0.into()), writer.finish())
.with_name("effect")
.init(init_pos)
.init(init_vel)
Expand Down
2 changes: 1 addition & 1 deletion examples/portal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ fn setup(mut commands: Commands, mut effects: ResMut<Assets<EffectAsset>>) {
let tangent_accel = TangentAccelModifier::constant(&mut module, Vec3::ZERO, Vec3::Z, 30.);

let effect1 = effects.add(
EffectAsset::new(32768, Spawner::rate(5000.0.into()), module)
EffectAsset::new(vec![16384, 16384], Spawner::rate(5000.0.into()), module)
.with_name("portal")
.init(init_pos)
.init(init_age)
Expand Down
2 changes: 1 addition & 1 deletion examples/random.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ fn setup(

let effect = effects.add(
EffectAsset::new(
32768,
vec![32768],
Spawner::burst(CpuValue::Uniform((1., 100.)), CpuValue::Uniform((1., 4.))),
writer.finish(),
)
Expand Down
26 changes: 15 additions & 11 deletions examples/spawn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ fn setup(
};

let effect1 = effects.add(
EffectAsset::new(32768, Spawner::rate(500.0.into()), writer1.finish())
EffectAsset::new(vec![32768], Spawner::rate(500.0.into()), writer1.finish())
.with_name("emit:rate")
.with_property("my_accel", Vec3::new(0., -3., 0.).into())
.init(init_pos1)
Expand Down Expand Up @@ -198,15 +198,19 @@ fn setup(
speed: writer2.lit(2.).expr(),
};
let effect2 = effects.add(
EffectAsset::new(32768, Spawner::once(1000.0.into(), true), writer2.finish())
.with_name("emit:once")
.init(init_pos2)
.init(init_vel2)
.init(init_age2)
.init(init_lifetime2)
.render(ColorOverLifetimeModifier {
gradient: gradient2,
}),
EffectAsset::new(
vec![32768],
Spawner::once(1000.0.into(), true),
writer2.finish(),
)
.with_name("emit:once")
.init(init_pos2)
.init(init_vel2)
.init(init_age2)
.init(init_lifetime2)
.render(ColorOverLifetimeModifier {
gradient: gradient2,
}),
);

commands
Expand Down Expand Up @@ -266,7 +270,7 @@ fn setup(

let effect3 = effects.add(
EffectAsset::new(
32768,
vec![32768],
Spawner::burst(400.0.into(), 3.0.into()),
writer3.finish(),
)
Expand Down
2 changes: 1 addition & 1 deletion examples/spawn_on_command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ fn setup(
let init_vel = SetAttributeModifier::new(Attribute::VELOCITY, velocity.expr());

let effect = effects.add(
EffectAsset::new(32768, spawner, writer.finish())
EffectAsset::new(vec![32768], spawner, writer.finish())
.with_name("spawn_on_command")
.with_property("spawn_color", 0xFFFFFFFFu32.into())
.with_property("normal", Vec3::ZERO.into())
Expand Down
2 changes: 1 addition & 1 deletion examples/visibility.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ fn setup(
};

let mut asset = EffectAsset::new(
4096,
vec![4096],
Spawner::burst(50.0.into(), 15.0.into()),
writer.finish(),
)
Expand Down
Loading

0 comments on commit 0c49f95

Please sign in to comment.