-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathstress.rs
More file actions
146 lines (121 loc) · 4.25 KB
/
stress.rs
File metadata and controls
146 lines (121 loc) · 4.25 KB
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
// A stress test with an unreasonable amount of animated 3D sprites
//
// CLI options:
//
// Pass "2d" for 2D sprites (default)
// Pass "3d" for 3D sprites
//
// Pass --sprites X to render X sprites (default is 100 000)
//
// Best executed in --release mode!
use bevy::{dev_tools::fps_overlay::FpsOverlayPlugin, prelude::*, window::PrimaryWindow};
use bevy_spritesheet_animation::prelude::*;
use clap::{Parser, ValueEnum};
use rand::{Rng, seq::IndexedRandom as _};
#[derive(ValueEnum, Clone)]
enum Mode {
#[clap(name = "2d")]
TwoD,
#[clap(name = "3d")]
ThreeD,
}
#[derive(Parser, Resource)]
struct Cli {
#[arg(value_enum, default_value_t=Mode::TwoD)]
mode: Mode,
#[arg(long, default_value_t = 100_000)]
sprites: usize,
}
fn main() {
let cli = Cli::parse();
App::new()
.add_plugins((
DefaultPlugins.set(ImagePlugin::default_nearest()),
SpritesheetAnimationPlugin,
FpsOverlayPlugin::default(),
))
.insert_resource(cli)
.add_systems(Startup, spawn_sprites)
.run();
}
fn spawn_sprites(
cli: Res<Cli>,
window: Single<&Window, With<PrimaryWindow>>,
mut commands: Commands,
assets: Res<AssetServer>,
mut animations: ResMut<Assets<Animation>>,
mut atlas_layouts: ResMut<Assets<TextureAtlasLayout>>,
) {
// Spawn a camera
match cli.mode {
Mode::TwoD => commands.spawn(Camera2d),
Mode::ThreeD => commands.spawn((
Camera3d::default(),
Transform::from_xyz(0.0, 1000.0, 4000.0).looking_at(Vec3::ZERO, Dir3::Y),
)),
};
// Create base animations from the rows of a spritesheet
let image = assets.load("character.png");
let spritesheet = Spritesheet::new(&image, 8, 8);
let base_animations = [
spritesheet.create_animation().add_partial_row(0, ..5),
spritesheet.create_animation().add_partial_row(1, ..5),
spritesheet.create_animation().add_row(2),
spritesheet.create_animation().add_row(3),
spritesheet.create_animation().add_partial_row(4, ..5),
spritesheet.create_animation().add_partial_row(5, ..5),
spritesheet.create_animation().add_row(6),
spritesheet.create_animation().add_row(7),
];
// Create 100 derived animations, each with random parameters
let mut rng = rand::rng();
let animation_directions = [
AnimationDirection::Forwards,
AnimationDirection::Backwards,
AnimationDirection::PingPong,
];
let animation_handles: Vec<Handle<Animation>> = (0..100)
.map(|_| {
let base_animation = base_animations.choose(&mut rng).unwrap();
let animation = base_animation
.clone()
.set_duration(AnimationDuration::PerFrame(rng.random_range(100..1000)))
.set_direction(*animation_directions.choose(&mut rng).unwrap())
.build();
animations.add(animation)
})
.collect();
// Spawn A LOT of sprites, each with a random animation assigned
let component_generator = spritesheet.with_size_hint(768, 768);
for _ in 0..cli.sprites {
let animation_handle = animation_handles.choose(&mut rng).unwrap();
let transform = Transform::from_translation(random_position(&window));
match cli.mode {
Mode::TwoD => {
commands.spawn((
component_generator.sprite(&mut atlas_layouts),
SpritesheetAnimation::new(animation_handle.clone()),
transform,
));
}
Mode::ThreeD => {
#[cfg(feature = "3d")]
commands.spawn((
component_generator.sprite3d(&mut atlas_layouts),
SpritesheetAnimation::new(animation_handle.clone()),
transform,
));
#[cfg(not(feature = "3d"))]
panic!("3D mode requires the '3d' feature to be enabled");
}
}
}
}
pub fn random_position(window: &Window) -> Vec3 {
let mut rng = rand::rng();
Vec3::new(
rng.random_range(-window.width() / 2.0..window.width() / 2.0),
rng.random_range(-window.height() / 2.0..window.height() / 2.0),
0.0,
)
}