Skip to content

Commit

Permalink
Initialize the age of particles in examples (djeedai#203)
Browse files Browse the repository at this point in the history
Fix all examples to initialize the age of particles. Otherwise they will
never die and won't despawn.
  • Loading branch information
djeedai authored Jul 10, 2023
1 parent 8e99e3a commit 0b724c8
Show file tree
Hide file tree
Showing 16 changed files with 70 additions and 11 deletions.
4 changes: 4 additions & 0 deletions examples/2d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ fn setup(

let writer = ExprWriter::new();

let age = writer.lit(0.).expr();
let init_age = InitAttributeModifier::new(Attribute::AGE, age);

let lifetime = writer.lit(5.).expr();
let init_lifetime = InitAttributeModifier::new(Attribute::LIFETIME, lifetime);

Expand All @@ -99,6 +102,7 @@ fn setup(
axis: Vec3::Z,
speed: 0.1.into(),
})
.init(init_age)
.init(init_lifetime)
.render(SizeOverLifetimeModifier {
gradient: Gradient::constant(Vec2::splat(0.02)),
Expand Down
4 changes: 4 additions & 0 deletions examples/activate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,9 @@ fn setup(

let writer = ExprWriter::new();

let age = writer.lit(0.).expr();
let init_age = InitAttributeModifier::new(Attribute::AGE, age);

let lifetime = writer.lit(5.).expr();
let init_lifetime = InitAttributeModifier::new(Attribute::LIFETIME, lifetime);

Expand All @@ -118,6 +121,7 @@ fn setup(
center: Vec3::ZERO,
speed: 0.1.into(),
})
.init(init_age)
.init(init_lifetime)
.render(SizeOverLifetimeModifier {
gradient: Gradient::constant(Vec2::splat(0.02)),
Expand Down
4 changes: 4 additions & 0 deletions examples/billboard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ fn setup(

let writer = ExprWriter::new();

let age = writer.lit(0.).expr();
let init_age = InitAttributeModifier::new(Attribute::AGE, age);

let lifetime = writer.lit(5.).expr();
let init_lifetime = InitAttributeModifier::new(Attribute::LIFETIME, lifetime);

Expand All @@ -84,6 +87,7 @@ fn setup(
axis: Vec3::Y,
speed: CpuValue::Uniform((0.7, 0.5)),
})
.init(init_age)
.init(init_lifetime)
.render(ParticleTextureModifier {
texture: texture_handle,
Expand Down
4 changes: 4 additions & 0 deletions examples/circle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ fn setup(

let writer = ExprWriter::new();

let age = writer.lit(0.).expr();
let init_age = InitAttributeModifier::new(Attribute::AGE, age);

let lifetime = writer.lit(5.).expr();
let init_lifetime = InitAttributeModifier::new(Attribute::LIFETIME, lifetime);

Expand All @@ -79,6 +82,7 @@ fn setup(
axis: Vec3::Y,
speed: CpuValue::Uniform((1.0, 1.5)),
})
.init(init_age)
.init(init_lifetime)
.render(ParticleTextureModifier {
texture: texture_handle.clone(),
Expand Down
4 changes: 4 additions & 0 deletions examples/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ fn setup(mut commands: Commands, mut effects: ResMut<Assets<EffectAsset>>) {

let writer = ExprWriter::new();

let age = writer.lit(0.).expr();
let init_age = InitAttributeModifier::new(Attribute::AGE, age);

// Give a bit of variation by randomizing the lifetime per particle
let lifetime = writer.lit(2.5).uniform(writer.lit(3.5)).expr();
let init_lifetime = InitAttributeModifier::new(Attribute::LIFETIME, lifetime);
Expand All @@ -87,6 +90,7 @@ fn setup(mut commands: Commands, mut effects: ResMut<Assets<EffectAsset>>) {
radius: 4.,
dimension: ShapeDimension::Surface,
})
.init(init_age)
.init(init_lifetime)
.init(InitVelocityTangentModifier {
origin: Vec3::ZERO,
Expand Down
4 changes: 4 additions & 0 deletions examples/force_field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,9 @@ fn setup(

let writer = ExprWriter::new();

let age = writer.lit(0.).expr();
let init_age = InitAttributeModifier::new(Attribute::AGE, age);

let lifetime = writer.lit(5.).expr();
let init_lifetime = InitAttributeModifier::new(Attribute::LIFETIME, lifetime);

Expand All @@ -176,6 +179,7 @@ fn setup(
center: Vec3::ZERO,
speed: CpuValue::Uniform((0.1, 0.3)),
})
.init(init_age)
.init(init_lifetime)
.update(ForceFieldModifier::new(vec![
ForceFieldSource {
Expand Down
4 changes: 4 additions & 0 deletions examples/gradient.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ fn setup(

let writer = ExprWriter::new();

let age = writer.lit(0.).expr();
let init_age = InitAttributeModifier::new(Attribute::AGE, age);

let lifetime = writer.lit(5.).expr();
let init_lifetime = InitAttributeModifier::new(Attribute::LIFETIME, lifetime);

Expand All @@ -86,6 +89,7 @@ fn setup(
center: Vec3::ZERO,
speed: 2.0.into(),
})
.init(init_age)
.init(init_lifetime)
.render(ParticleTextureModifier {
texture: texture_handle.clone(),
Expand Down
8 changes: 2 additions & 6 deletions examples/init.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
//! This example demonstrates the various position initializing modifiers.
//!
//! The example spawns a single burst of particles according to several position
//! modifiers, with a near-infinite lifetime (1 hour) and without any velocity
//! nor acceleration. This allows visualizing the distribution of particles on
//! modifiers, with an infinite lifetime, and without any velocity nor
//! acceleration. This allows visualizing the distribution of particles on
//! spawn.
use std::f32::consts::PI;
Expand Down Expand Up @@ -51,12 +51,8 @@ const SIZE: Vec2 = Vec2::splat(0.1);
fn base_effect(name: impl Into<String>) -> EffectAsset {
let writer = ExprWriter::new();

let lifetime = writer.lit(3600.).expr();
let init_lifetime = InitAttributeModifier::new(Attribute::LIFETIME, lifetime);

EffectAsset::new(32768, Spawner::once(COUNT.into(), true), writer.finish())
.with_name(name)
.init(init_lifetime)
.render(BillboardModifier)
.render(SetColorModifier {
color: COLOR.into(),
Expand Down
4 changes: 4 additions & 0 deletions examples/instancing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,9 @@ fn setup(

let writer = ExprWriter::new();

let age = writer.lit(0.).expr();
let init_age = InitAttributeModifier::new(Attribute::AGE, age);

let lifetime = writer.lit(12.).expr();
let init_lifetime = InitAttributeModifier::new(Attribute::LIFETIME, lifetime);

Expand All @@ -236,6 +239,7 @@ fn setup(
center: Vec3::ZERO,
speed: 2.0.into(),
})
.init(init_age)
.init(init_lifetime)
.render(ColorOverLifetimeModifier { gradient }),
);
Expand Down
9 changes: 9 additions & 0 deletions examples/lifetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ fn setup(
gradient1.add_key(1.0, Vec4::ONE);

let writer1 = ExprWriter::new();
let age1 = writer1.lit(0.).expr();
let init_age1 = InitAttributeModifier::new(Attribute::AGE, age1);
let lifetime1 = writer1.lit(lifetime1).expr();
let init_lifetime1 = InitAttributeModifier::new(Attribute::LIFETIME, lifetime1);
let effect1 = effects.add(
Expand All @@ -107,6 +109,7 @@ fn setup(
center: Vec3::ZERO,
speed: 2.0.into(),
})
.init(init_age1)
.init(init_lifetime1)
.render(ColorOverLifetimeModifier {
gradient: gradient1,
Expand Down Expand Up @@ -139,6 +142,8 @@ fn setup(
gradient2.add_key(1.0, Vec4::new(1.0, 1.0, 0.0, 1.0));

let writer2 = ExprWriter::new();
let age2 = writer2.lit(0.).expr();
let init_age2 = InitAttributeModifier::new(Attribute::AGE, age2);
let lifetime2 = writer2.lit(lifetime2).expr();
let init_lifetime2 = InitAttributeModifier::new(Attribute::LIFETIME, lifetime2);
let effect2 = effects.add(
Expand All @@ -157,6 +162,7 @@ fn setup(
center: Vec3::ZERO,
speed: 2.0.into(),
})
.init(init_age2)
.init(init_lifetime2)
.render(ColorOverLifetimeModifier {
gradient: gradient2,
Expand Down Expand Up @@ -189,6 +195,8 @@ fn setup(
gradient3.add_key(1.0, Vec4::new(0.75, 0.25, 0.0, 1.0));

let writer3 = ExprWriter::new();
let age3 = writer3.lit(0.).expr();
let init_age3 = InitAttributeModifier::new(Attribute::AGE, age3);
let lifetime3 = writer3.lit(lifetime3).expr();
let init_lifetime3 = InitAttributeModifier::new(Attribute::LIFETIME, lifetime3);
let effect3 = effects.add(
Expand All @@ -207,6 +215,7 @@ fn setup(
center: Vec3::ZERO,
speed: 2.0.into(),
})
.init(init_age3)
.init(init_lifetime3)
.render(ColorOverLifetimeModifier {
gradient: gradient3,
Expand Down
4 changes: 4 additions & 0 deletions examples/multicam.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ fn make_effect(color: Color) -> EffectAsset {

let writer = ExprWriter::new();

let age = writer.lit(0.).expr();
let init_age = InitAttributeModifier::new(Attribute::AGE, age);

let lifetime = writer.lit(5.).expr();
let init_lifetime = InitAttributeModifier::new(Attribute::LIFETIME, lifetime);

Expand All @@ -68,6 +71,7 @@ fn make_effect(color: Color) -> EffectAsset {
center: Vec3::ZERO,
speed: 6.0.into(),
})
.init(init_age)
.init(init_lifetime)
.update(update_accel)
.render(ColorOverLifetimeModifier {
Expand Down
4 changes: 4 additions & 0 deletions examples/portal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ fn setup(mut commands: Commands, mut effects: ResMut<Assets<EffectAsset>>) {

let writer = ExprWriter::new();

let age = writer.lit(0.).expr();
let init_age = InitAttributeModifier::new(Attribute::AGE, age);

// Give a bit of variation by randomizing the lifetime per particle
let lifetime = writer.lit(0.6).uniform(writer.lit(1.3)).expr();
let init_lifetime = InitAttributeModifier::new(Attribute::LIFETIME, lifetime);
Expand All @@ -83,6 +86,7 @@ fn setup(mut commands: Commands, mut effects: ResMut<Assets<EffectAsset>>) {
radius: 4.,
dimension: ShapeDimension::Surface,
})
.init(init_age)
.init(init_lifetime)
.update(update_drag)
.update(RadialAccelModifier::constant(Vec3::ZERO, -6.0))
Expand Down
4 changes: 4 additions & 0 deletions examples/random.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ fn setup(

let writer = ExprWriter::new();

let age = writer.lit(0.).expr();
let init_age = InitAttributeModifier::new(Attribute::AGE, age);

let lifetime = writer.lit(5.).expr();
let init_lifetime = InitAttributeModifier::new(Attribute::LIFETIME, lifetime);

Expand All @@ -94,6 +97,7 @@ fn setup(
center: Vec3::ZERO,
speed: 2.0.into(),
})
.init(init_age)
.init(init_lifetime)
.update(update_accel)
.render(ColorOverLifetimeModifier { gradient }),
Expand Down
11 changes: 11 additions & 0 deletions examples/spawn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,9 @@ fn setup(

let writer1 = ExprWriter::new();

let age1 = writer1.lit(0.).expr();
let init_age1 = InitAttributeModifier::new(Attribute::AGE, age1);

let lifetime1 = writer1.lit(5.).expr();
let init_lifetime1 = InitAttributeModifier::new(Attribute::LIFETIME, lifetime1);

Expand All @@ -119,6 +122,7 @@ fn setup(
center: Vec3::ZERO,
speed: 10.0.into(),
})
.init(init_age1)
.init(init_lifetime1)
.update(update_accel1)
.render(ColorOverLifetimeModifier {
Expand Down Expand Up @@ -157,6 +161,8 @@ fn setup(
gradient2.add_key(1.0, Vec4::splat(0.0));

let writer2 = ExprWriter::new();
let age2 = writer2.lit(0.).expr();
let init_age2 = InitAttributeModifier::new(Attribute::AGE, age2);
let lifetime2 = writer2.lit(5.).expr();
let init_lifetime2 = InitAttributeModifier::new(Attribute::LIFETIME, lifetime2);
let effect2 = effects.add(
Expand All @@ -171,6 +177,7 @@ fn setup(
center: Vec3::ZERO,
speed: 2.0.into(),
})
.init(init_age2)
.init(init_lifetime2)
.render(ColorOverLifetimeModifier {
gradient: gradient2,
Expand Down Expand Up @@ -206,6 +213,9 @@ fn setup(

let writer3 = ExprWriter::new();

let age3 = writer3.lit(0.).expr();
let init_age3 = InitAttributeModifier::new(Attribute::AGE, age3);

let lifetime3 = writer3.lit(5.).expr();
let init_lifetime3 = InitAttributeModifier::new(Attribute::LIFETIME, lifetime3);

Expand Down Expand Up @@ -235,6 +245,7 @@ fn setup(
center: Vec3::ZERO,
speed: 2.0.into(),
})
.init(init_age3)
.init(init_lifetime3)
.init(init_size3)
.update(update_accel3)
Expand Down
4 changes: 4 additions & 0 deletions examples/visibility.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,9 @@ fn setup(
let velocity = writer.lit(Vec3::X * 3.).expr();
let init_velocity = InitAttributeModifier::new(Attribute::VELOCITY, velocity);

let age = writer.lit(0.).expr();
let init_age = InitAttributeModifier::new(Attribute::AGE, age);

let lifetime = writer.lit(15.).expr();
let init_lifetime = InitAttributeModifier::new(Attribute::LIFETIME, lifetime);

Expand All @@ -111,6 +114,7 @@ fn setup(
dimension: ShapeDimension::Volume,
})
.init(init_velocity)
.init(init_age)
.init(init_lifetime)
//.update(AccelModifier::constant(Vec3::new(0., 2., 0.)))
.render(ColorOverLifetimeModifier { gradient });
Expand Down
5 changes: 0 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -781,11 +781,6 @@ impl CompiledParticleEffect {
}
(init_context.init_code, init_context.init_extra)
};
// Warn in debug if the shader doesn't initialize the particle lifetime
#[cfg(debug_assertions)]
if !init_code.contains(&format!("particle.{}", Attribute::LIFETIME.name())) {
warn!("Effect '{}' does not initialize the particle lifetime; particles will have a default lifetime of zero, and will immediately die after spawning.", asset.name);
}

// Generate the shader code for the update shader
let (mut update_code, update_extra, force_field) = {
Expand Down

0 comments on commit 0b724c8

Please sign in to comment.