Skip to content

Commit

Permalink
Change the ribbon example into a tracer example. (#380)
Browse files Browse the repository at this point in the history
  • Loading branch information
naasblod authored Oct 29, 2024
1 parent 6335a65 commit 003fff3
Showing 1 changed file with 29 additions and 16 deletions.
45 changes: 29 additions & 16 deletions examples/ribbon.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
//! Draws a trail and connects the trails using a ribbon.
//! Uses with_ribbons to draw a "tracer" or a "trail" following an Entity.
//! The movement of the head particle is achieved by linking the particle position to a CPU position using a [Property] in [move_head].
use bevy::math::vec4;
use bevy::prelude::*;
Expand All @@ -16,15 +17,15 @@ use utils::*;
const K: f32 = 0.64;
const L: f32 = 0.384;

const TIME_SCALE: f32 = 10.0;
const TIME_SCALE: f32 = 6.5;
const SHAPE_SCALE: f32 = 25.0;
const LIFETIME: f32 = 2.5;
const LIFETIME: f32 = 1.5;
const TRAIL_SPAWN_RATE: f32 = 256.0;

fn main() -> Result<(), Box<dyn std::error::Error>> {
let app_exit = utils::make_test_app("ribbon")
.add_systems(Startup, setup)
.add_systems(Update, move_particle_effect)
.add_systems(Update, move_head)
.run();
app_exit.into_result()
}
Expand Down Expand Up @@ -71,18 +72,12 @@ fn setup(mut commands: Commands, mut effects: ResMut<Assets<EffectAsset>>) {
value: writer.lit(0.5).expr(),
};

let time = writer.time().mul(writer.lit(TIME_SCALE));
let pos = writer.add_property("head_pos", Vec3::ZERO.into());
let pos = writer.prop(pos);

let move_modifier = SetAttributeModifier {
attribute: Attribute::POSITION,
value: (WriterExpr::vec3(
writer.lit(1.0 - K).mul(time.clone().cos())
+ writer.lit(L * K) * (writer.lit((1.0 - K) / K) * time.clone()).cos(),
writer.lit(1.0 - K).mul(time.clone().sin())
- writer.lit(L * K) * (writer.lit((1.0 - K) / K) * time.clone()).sin(),
writer.lit(0.0),
) * writer.lit(SHAPE_SCALE))
.expr(),
value: pos.expr(),
};

let render_color = ColorOverLifetimeModifier {
Expand All @@ -98,6 +93,10 @@ fn setup(mut commands: Commands, mut effects: ResMut<Assets<EffectAsset>>) {
.init_groups(init_lifetime_attr, ParticleGroupSet::single(0))
.init_groups(init_size_attr, ParticleGroupSet::single(0))
.update_groups(move_modifier, ParticleGroupSet::single(0))
.render(SizeOverLifetimeModifier {
gradient: Gradient::linear(Vec2::ONE, Vec2::ZERO),
..default()
})
.render_groups(render_color, ParticleGroupSet::single(1));

let effect = effects.add(effect);
Expand All @@ -111,9 +110,23 @@ fn setup(mut commands: Commands, mut effects: ResMut<Assets<EffectAsset>>) {
.insert(Name::new("ribbon"));
}

fn move_particle_effect(mut query: Query<&mut Transform, With<ParticleEffect>>, timer: Res<Time>) {
let theta = timer.elapsed_seconds() * 1.0;
fn move_head(
mut query: Query<&mut Transform, With<ParticleEffect>>,
mut effect: Query<&mut EffectProperties>,
timer: Res<Time>,
) {
let Ok(mut properties) = effect.get_single_mut() else {
return;
};
for mut transform in query.iter_mut() {
transform.translation = vec3(f32::cos(theta), f32::sin(theta), 0.0) * 5.0;
let time = timer.elapsed_seconds() * TIME_SCALE;
let pos = vec3(
(1.0 - K) * (time.clone().cos()) + (L * K) * (((1.0 - K) / K) * time.clone()).cos(),
(1.0 - K) * (time.clone().sin()) - (L * K) * (((1.0 - K) / K) * time.clone()).sin(),
0.0,
) * SHAPE_SCALE;

properties.set("head_pos", (pos).into());
transform.translation = pos;
}
}

0 comments on commit 003fff3

Please sign in to comment.