以下程序是大量 SpriteComponents 的一个非常简约的示例。在此示例中,使用 FPS 计数器,您可以看到 SpriteComponents 的成本似乎是多么高。
我确实注意到,如果我重用材质句柄,它确实会加快速度,但即便如此,速度还是非常慢。另外,我不确定重复使用材料手柄有多安全。我注意到在突破示例中,它们没有重用句柄。
尽管如此,在没有其他系统的情况下,我每秒可以得到大约 10 帧。
use bevy:: {
prelude::*,
};
struct Box;
struct Name(String);
#[derive(Default)]
struct FPSCounter(Timer,u32);
const WIDTH: f32 = 640.0;
const HEIGHT: f32 = 480.0;
pub struct drawPlugin;
impl Plugin for drawPlugin {
fn build(&self, app: &mut AppBuilder) {
app
.add_resource(FPSCounter(Timer::from_seconds(1.0,true),0))
.add_startup_system(setup.system())
.add_system(fps_counter.system());
}
}
fn fps_counter(time: Res<Time>, mut timer: ResMut<FPSCounter>) {
timer.0.tick(time.delta_seconds);
timer.1 += 1;
if timer.0.finished {
println!("One-{}",timer.1);
timer.1 = 0;
}
}
fn main() {
App::build()
.add_resource(WindowDescriptor {
title: "Test Prog!".to_string(),
width: WIDTH as u32,
height: HEIGHT as u32,
vsync: true,
resizable: false,
..Default::default()
})
.add_default_plugins()
.add_plugin(drawPlugin)
.run();
}
fn setup(mut cmds: Commands, mut mats: ResMut<Assets<ColorMaterial>>,asset_server: Res<AssetServer>) {
const sq_width: f32 = 10.0;
const sq_height: f32 = 10.0;
let left = -(WIDTH /2.0) + sq_width / 2.0;
let right = (WIDTH / 2.0) - sq_width / 2.0;
let top = (HEIGHT / 2.0) - sq_height / 2.0;
let bot = -(HEIGHT / 2.0) + sq_height / 2.0;
let mymat = mats.add(Color::rgb(1.0,0.0,0.0).into());
cmds
.spawn(Camera2dComponents::default())
.spawn(UiCameraComponents::default());
for box_num2 in 1..=30 {
for box_num in 1..=30 {
cmds.spawn(SpriteComponents {
material: mymat,
translation: Translation(Vec3::new(left + ((sq_width + 1.0) * box_num as f32),top - ((sq_height + 1.0) * box_num2 as f32),0.0)),
sprite: Sprite {
size: Vec2::new(sq_width,sq_height),
},
..Default::default()
}).with(Box {}).with(Name("Box1".to_string()));
}
}
}