Performance questions #593
Replies: 1 comment 6 replies
-
|
I think this could be potentially related to one or more of your numbered questions, but it seems that you are despawning all tiles when despawning out-of-range chunks, not just those associated with that particular chunk. You can iterate through for maybe_entity in tile_storage.iter_mut() {
if let Some(entity) = maybe_entity.take() {
commands.entity(entity).despawn_recursive();
}
}or make your tiles children of the tilemap so that they are despawned when you use Expand Codediff --git a/src/main.rs b/src/main.rs
index f49eebb..f1e240a 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -45,14 +45,10 @@ fn spawn_chunk(
) {
let tilemap_entity = commands.spawn_empty().id();
let mut tile_storage = TileStorage::empty(CHUNK_SIZE.into());
+ let mut tile_entities = Vec::with_capacity(CHUNK_SIZE.x as usize * CHUNK_SIZE.y as usize);
let mut random = rand::thread_rng();
for x in 0..CHUNK_SIZE.x {
for y in 0..CHUNK_SIZE.y {
- let map_transform = Transform::from_translation(Vec3::new(
- chunk_pos.x as f32 * CHUNK_SIZE.x as f32 * (TILE_SIZE.x),
- chunk_pos.y as f32 * CHUNK_SIZE.y as f32 * (TILE_SIZE.y),
- 0.0,
- ));
total.0 += 1;
if total.0 % 1_000 == 0 {
info!("total: {:?}", total);
@@ -62,7 +58,6 @@ fn spawn_chunk(
let grid_size = TilemapGridSize { x: 32.0, y: 32.0 };
let map_type = TilemapType::Square;
let tile_center = tile_pos.center_in_world(&grid_size, &map_type).extend(1.0);
- let transform = map_transform * Transform::from_translation(tile_center);
let tile_entity = commands
.spawn((
@@ -73,7 +68,7 @@ fn spawn_chunk(
..Default::default()
},
YoTile,
- transform,
+ Transform::from_translation(tile_center),
))
.with_children(|parent| {
let ulam_v = ulam::get_value_from_xy(
@@ -102,6 +97,7 @@ fn spawn_chunk(
})
.id();
tile_storage.set(&tile_pos, tile_entity);
+ tile_entities.push(tile_entity);
}
}
@@ -112,23 +108,26 @@ fn spawn_chunk(
));
let texture_handle: Handle<Image> = asset_server.load("spritesheet/ss-land-v12.png");
- commands.entity(tilemap_entity).insert((
- TilemapBundle {
- grid_size: TilemapGridSize { x: 32.0, y: 32.0 },
- size: CHUNK_SIZE.into(),
- storage: tile_storage,
- texture: TilemapTexture::Single(texture_handle),
- tile_size: TILE_SIZE,
- spacing: TilemapSpacing { x: 2.0, y: 2.0 },
- transform,
- render_settings: TilemapRenderSettings {
- render_chunk_size: RENDER_CHUNK_SIZE,
+ commands
+ .entity(tilemap_entity)
+ .insert((
+ TilemapBundle {
+ grid_size: TilemapGridSize { x: 32.0, y: 32.0 },
+ size: CHUNK_SIZE.into(),
+ storage: tile_storage,
+ texture: TilemapTexture::Single(texture_handle),
+ tile_size: TILE_SIZE,
+ spacing: TilemapSpacing { x: 2.0, y: 2.0 },
+ transform,
+ render_settings: TilemapRenderSettings {
+ render_chunk_size: RENDER_CHUNK_SIZE,
+ ..Default::default()
+ },
..Default::default()
},
- ..Default::default()
- },
- YoMap,
- ));
+ YoMap,
+ ))
+ .add_children(&tile_entities);
}
fn startup(mut commands: Commands) {
@@ -196,27 +195,14 @@ fn despawn_outofrange_chunks(
mut commands: Commands,
camera_query: Query<&Transform, With<Camera>>,
chunks_query_map: Query<(Entity, &Transform), With<YoMap>>,
- chunks_query_tiles: Query<(Entity, &Transform), (Without<YoMap>, With<YoTile>)>,
mut chunk_manager: ResMut<ChunkManager>,
despawn_range: Res<DespawnRange>,
) {
for camera_transform in camera_query.iter() {
- // despawning tiles
- for (entity, chunk_transform) in chunks_query_tiles.iter() {
- let chunk_pos = chunk_transform.translation.xy();
- let distance = camera_transform.translation.xy().distance(chunk_pos);
- if distance > despawn_range.0 {
- let x = (chunk_pos.x / (CHUNK_SIZE.x as f32 * TILE_SIZE.x)).floor() as i32;
- let y = (chunk_pos.y / (CHUNK_SIZE.y as f32 * TILE_SIZE.y)).floor() as i32;
- chunk_manager.spawned_chunks.remove(&IVec2::new(x, y));
- commands.entity(entity).despawn_recursive();
- }
- }
- // despawning map with * 2.0 range to avoid panics
for (entity, chunk_transform) in chunks_query_map.iter() {
let chunk_pos = chunk_transform.translation.xy();
let distance = camera_transform.translation.xy().distance(chunk_pos);
- if distance > despawn_range.0 * 2.0 {
+ if distance > despawn_range.0 {
let x = (chunk_pos.x / (CHUNK_SIZE.x as f32 * TILE_SIZE.x)).floor() as i32;
let y = (chunk_pos.y / (CHUNK_SIZE.y as f32 * TILE_SIZE.y)).floor() as i32;
That seems like a bit of a mystery. I would suggest doing some profiling. The "tracy" profiler makes it pretty easy to log a trace and compare statistics from one portion of a trace to another.
Bevy 0.15 came with a major regression to |
Beta Was this translation helpful? Give feedback.











Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
https://github.com/miketwenty1/wonder15/blob/parentworkingattempt/src/main.rs
A few questions / concerns I have with performance.
I left some questions/comments in code as well.
Note if you run this: Q/E expand or contract the despawn range. WASD is moving, Z/X is zoom. T is toggling text. This game is intended for only web. I've set the project so if you want to run it, you can do a
cargo install wasm-server-runnerand then acargo run --target wasm32-unknown-unknownThe issues are happen much faster and are more easy to identify when pressing and holding Q for a few seconds to increase the despawn range.
Beta Was this translation helpful? Give feedback.
All reactions