forked from djeedai/bevy_hanabi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
init.rs
213 lines (192 loc) Β· 6.33 KB
/
init.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
//! This example demonstrates the various position initializing modifiers.
//!
//! The example spawns a single burst of particles according to several position
//! 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;
use bevy::{
core_pipeline::tonemapping::Tonemapping,
log::LogPlugin,
prelude::*,
render::{render_resource::WgpuFeatures, settings::WgpuSettings, RenderPlugin},
};
use bevy_hanabi::prelude::*;
#[cfg(feature = "examples_world_inspector")]
use bevy_inspector_egui::quick::WorldInspectorPlugin;
#[derive(Component)]
struct RotateSpeed(pub f32);
fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut wgpu_settings = WgpuSettings::default();
wgpu_settings
.features
.set(WgpuFeatures::VERTEX_WRITABLE_STORAGE, true);
let mut app = App::default();
app.insert_resource(ClearColor(Color::DARK_GRAY))
.add_plugins(
DefaultPlugins
.set(LogPlugin {
level: bevy::log::Level::WARN,
filter: "bevy_hanabi=warn,init=trace".to_string(),
update_subscriber: None,
})
.set(RenderPlugin {
render_creation: wgpu_settings.into(),
synchronous_pipeline_compilation: false,
})
.set(WindowPlugin {
primary_window: Some(Window {
title: "π Hanabi β init".to_string(),
..default()
}),
..default()
}),
)
.add_plugins(HanabiPlugin);
#[cfg(feature = "examples_world_inspector")]
app.add_plugins(WorldInspectorPlugin::default());
app.add_systems(Startup, setup)
.add_systems(Update, (bevy::window::close_on_esc, rotate_effect))
.run();
Ok(())
}
const COLOR: Vec4 = Vec4::new(0.7, 0.7, 1.0, 1.0);
const SIZE: Vec2 = Vec2::splat(0.1);
fn base_effect<M, F>(name: impl Into<String>, mut make_modifier: F) -> EffectAsset
where
M: Modifier + Send + Sync + 'static,
F: FnMut(&ExprWriter) -> M,
{
let writer = ExprWriter::new();
let init = make_modifier(&writer);
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(
commands: &mut Commands,
name: String,
speed: f32,
transform: Transform,
effect: Handle<EffectAsset>,
mesh: Handle<Mesh>,
material: Handle<StandardMaterial>,
) {
commands
.spawn((
Name::new(format!("{}_parent", name)),
SpatialBundle {
transform,
..Default::default()
},
))
.with_children(|p| {
p.spawn((
Name::new(name),
ParticleEffectBundle {
effect: ParticleEffect::new(effect),
..Default::default()
},
RotateSpeed(speed),
))
.with_children(|p| {
// Reference cube to visualize the emit origin
p.spawn(PbrBundle {
mesh,
material,
..Default::default()
});
});
});
}
const COUNT: f32 = 500_f32;
fn setup(
mut commands: Commands,
mut effects: ResMut<Assets<EffectAsset>>,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>,
) {
let mut camera = Camera3dBundle {
tonemapping: Tonemapping::None,
..default()
};
camera.transform.translation = Vec3::new(0.0, 0.0, 50.0);
commands.spawn(camera);
commands.spawn(DirectionalLightBundle {
directional_light: DirectionalLight {
color: Color::WHITE,
// Crank the illuminance way (too) high to make the reference cube clearly visible
illuminance: 1000.,
shadows_enabled: false,
..Default::default()
},
..Default::default()
});
let cube = meshes.add(Cuboid {
half_size: Vec3::splat(0.5),
});
let mat = materials.add(Color::PURPLE);
spawn_effect(
&mut commands,
"SetPositionCircleModifier".to_string(),
3.,
Transform::from_translation(Vec3::new(-20., 0., 0.)),
effects.add(base_effect("SetPositionCircleModifier", |writer| {
SetPositionCircleModifier {
center: writer.lit(Vec3::ZERO).expr(),
axis: writer.lit(Vec3::Z).expr(),
radius: writer.lit(5.).expr(),
dimension: ShapeDimension::Volume,
}
})),
cube.clone(),
mat.clone(),
);
spawn_effect(
&mut commands,
"SetPositionSphereModifier".to_string(),
3.,
Transform::from_translation(Vec3::new(0., 0., 0.)),
effects.add(base_effect("SetPositionSphereModifier", |writer| {
SetPositionSphereModifier {
center: writer.lit(Vec3::ZERO).expr(),
radius: writer.lit(5.).expr(),
dimension: ShapeDimension::Volume,
}
})),
cube.clone(),
mat.clone(),
);
spawn_effect(
&mut commands,
"SetPositionCone3dModifier".to_string(),
3.,
Transform::from_translation(Vec3::new(20., 0., 0.)),
effects.add(base_effect("SetPositionCone3dModifier", |writer| {
SetPositionCone3dModifier {
height: writer.lit(10.).expr(),
base_radius: writer.lit(1.).expr(),
top_radius: writer.lit(4.).expr(),
dimension: ShapeDimension::Volume,
}
})),
cube.clone(),
mat.clone(),
);
}
fn rotate_effect(time: Res<Time>, mut query: Query<(&RotateSpeed, &mut Transform)>) {
for (speed, mut transform) in &mut query {
let a = (time.elapsed_seconds() * 0.1 * speed.0) * PI;
*transform = Transform::from_rotation(Quat::from_rotation_y(a));
}
}