Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

change the ribbon example into a tracer example. #380

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 29 additions & 13 deletions examples/ribbon.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//! Draws a trail and connects the trails using a ribbon.

use bevy::color::palettes::css::YELLOW;
use bevy::math::vec4;
use bevy::prelude::*;
use bevy::{
Expand All @@ -18,7 +19,7 @@ const L: f32 = 0.384;

const TIME_SCALE: f32 = 10.0;
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>> {
Expand Down Expand Up @@ -73,18 +74,12 @@ fn setup(mut commands: Commands, mut effects: ResMut<Assets<EffectAsset>>) {

let clone_modifier = CloneModifier::new(1.0 / TRAIL_SPAWN_RATE, 1);

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 update_lifetime_attr = SetAttributeModifier {
Expand All @@ -111,6 +106,10 @@ fn setup(mut commands: Commands, mut effects: ResMut<Assets<EffectAsset>>) {
.update_groups(move_modifier, ParticleGroupSet::single(0))
.update_groups(clone_modifier, ParticleGroupSet::single(0))
.update_groups(update_lifetime_attr, ParticleGroupSet::single(1))
.render(SizeOverLifetimeModifier {
gradient: Gradient::linear(Vec2::ONE, Vec2::ZERO),
..default()
})
.render(RibbonModifier)
.render_groups(render_color, ParticleGroupSet::single(1));

Expand All @@ -125,9 +124,26 @@ 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_particle_effect(
mut gizmos: Gizmos,
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;

//let pos = vec3(f32::cos(theta), f32::sin(theta), 0.0) * 5.0;
naasblod marked this conversation as resolved.
Show resolved Hide resolved
properties.set("head_pos", (pos).into());
gizmos.sphere(pos, Quat::IDENTITY, 1.0, YELLOW);
transform.translation = pos;
}
}
Loading