feat: apply_mesh for chunks is smeared over frames

This commit is contained in:
Sara Gerretsen 2026-02-25 22:42:07 +01:00
parent 226c821454
commit 1e44fcd09f
5 changed files with 47 additions and 11 deletions

View file

@ -19,6 +19,23 @@ void Terrain::child_order_changed() {
}
}
void Terrain::update_meshes() {
size_t num{ 1 };
this->dirty_meshes_lock.lock();
num = num > this->dirty_meshes.size() ? this->dirty_meshes.size() : num;
this->dirty_meshes_lock.unlock();
for (size_t i{ 0 }; i < num; i++) {
this->dirty_meshes_lock.lock();
TerrainChunkMesh *mesh{ this->dirty_meshes[0] };
this->dirty_meshes.remove_at(0);
this->dirty_meshes_lock.unlock();
mesh->apply_new_mesh();
}
if (this->dirty_meshes.is_empty()) {
set_process(false);
}
}
void Terrain::_notification(int what) {
switch (what) {
default:
@ -31,6 +48,9 @@ void Terrain::_notification(int what) {
case NOTIFICATION_READY:
construct_chunk_grid();
return;
case NOTIFICATION_PROCESS:
update_meshes();
return;
case NOTIFICATION_EXIT_TREE:
this->workload_lock.lock();
this->threads_stop = true;
@ -116,6 +136,13 @@ void Terrain::push_changed(Rect2 area) {
}
}
void Terrain::mesh_dirty(TerrainChunkMesh *mesh) {
this->dirty_meshes_lock.lock();
this->dirty_meshes.push_back(mesh);
callable_mp(cast_to<Node>(this), &self_type::set_process).call_deferred(true);
this->dirty_meshes_lock.unlock();
}
void Terrain::set_side_length(size_t length) {
this->side_length = length;
if (is_inside_tree()) {