Skip to content

Conversation

CuddlyBunion341
Copy link
Owner

@CuddlyBunion341 CuddlyBunion341 commented Mar 14, 2025

Mesher that does not increase frame times that much

As a follow up on a recent lecture @ ZHAW about concurrency and parallelism, I decided to optimize the mesher systems with an async task pool.

This is an attempt at improving terrain loading performance on the client by using background computations and polling task state.

This opens up the world of possibilities, as the terrain could now be infinite as generating chunk meshes on the fly won't be as blocking as before. Also the load times are significantly reduces as the meshing happens in the background.

Similar to what has been done here:
https://github.com/CuddlyBunion341/beertalk-workers/blob/main/slides.md

Note

This is my first time writing asynchronous Rust code, please be gentle ^^
Everything except for ea89014 is human written.

Before

Untitled.mp4

After

CleanShot.2025-03-16.at.22.36.55.mp4

Todo

  • Define resource for storing pending meshing tasks
  • Have a system to add new tasks to the pool
  • Define system that executes tasks asynchronously
  • Define a system that handles Task results and creates appropriate terrain meshes etc.

Sources

Heavily inspired by: https://bevy-cheatbook.github.io/fundamentals/async-compute.html

@CuddlyBunion341 CuddlyBunion341 self-assigned this Mar 14, 2025
@@ -87,7 +87,7 @@ pub mod client_block {
use client_block::block_properties;
use TextureName::*;

#[derive(Resource)]
#[derive(Resource, Clone)]
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No idea how to circumvent this.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The clone shouldn't be too expensive though, as we are only cloning a hashmap with a few primitives.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The atlas currently consists of 16 slots, so there are not many UVs and enum values that will be cloned.

));
});

let mut index = 0;
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ugly index, can't use enumerate() in this context.

@CuddlyBunion341 CuddlyBunion341 marked this pull request as ready for review March 16, 2025 21:29
@CuddlyBunion341 CuddlyBunion341 changed the title Smooth terrain loading Less blocking terrain mesh generation Mar 16, 2025
@@ -9,6 +11,24 @@ impl SpawnAreaLoaded {
}
}

#[derive(Clone, PartialEq)]
pub enum MeshType {
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reminder:

A logical Chunk of terrain has two geometrical representations:
A solid one, for the cubes like stone and dirt and a transparent one for the grass at the moment, perhaps glass and other stuff in the future.

}
let mesh_option = task_result.unwrap();
if mesh_option.is_none() {
return;
Copy link
Owner Author

@CuddlyBunion341 CuddlyBunion341 Mar 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Info: If chunk is empty, a None Mesh will be created.
In that case the world should not be updated.

@CuddlyBunion341 CuddlyBunion341 enabled auto-merge (squash) March 18, 2025 20:22
@CuddlyBunion341 CuddlyBunion341 enabled auto-merge (squash) March 18, 2025 20:32
@CuddlyBunion341 CuddlyBunion341 merged commit 45d359c into main Mar 18, 2025
3 checks passed
@CuddlyBunion341 CuddlyBunion341 deleted the features/smooth-terrain-loading branch March 18, 2025 20:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant