chore: massively sped up mesh gen performance

This commit is contained in:
Sara Gerretsen 2026-02-25 18:57:06 +01:00
parent 8ff1b1404d
commit 811970a306
7 changed files with 201 additions and 79 deletions

View file

@ -5,6 +5,11 @@
void TerrainChunkMesh::_bind_methods() {}
void TerrainChunkMesh::apply_new_mesh() {
set_mesh(this->new_mesh);
this->terrain->mesh_task_complete();
}
void TerrainChunkMesh::generate_vertices() {
ERR_FAIL_COND_EDMSG(this->terrain == nullptr, "TerrainChunkMesh::generate_vertices: no terrain assigned");
ERR_FAIL_COND_EDMSG(this->size <= 0.f, "TerrainChunkMesh::generate_vertices: size <= 0");
@ -56,9 +61,7 @@ void TerrainChunkMesh::_notification(int what) {
default:
return;
case NOTIFICATION_READY:
if (!get_mesh().is_valid()) {
set_mesh(memnew(ArrayMesh));
}
set_process_thread_group(ProcessThreadGroup::PROCESS_THREAD_GROUP_SUB_THREAD);
this->safe_position = get_global_position();
return;
}
@ -67,14 +70,17 @@ void TerrainChunkMesh::_notification(int what) {
void TerrainChunkMesh::update_mesh() {
ERR_FAIL_COND_EDMSG(this->size <= 0.f, "TerrainChunkMesh::generate: size <= 0");
ERR_FAIL_COND_EDMSG(points_per_side() <= 0, "TerrainChunkMesh::generate: points per side <= 0");
this->surface->clear();
this->lock.lock();
this->surface = memnew(SurfaceTool);
this->surface->begin(Mesh::PRIMITIVE_TRIANGLES);
generate_vertices();
generate_faces();
this->surface->generate_normals();
this->surface->generate_tangents();
callable_mp(Ref<ArrayMesh>(this->mesh).ptr(), &ArrayMesh::clear_surfaces).call_deferred();
callable_mp(Ref<ArrayMesh>(this->mesh).ptr(), &ArrayMesh::add_surface_from_arrays).call_deferred(Mesh::PRIMITIVE_TRIANGLES, this->surface->commit_to_arrays(), TypedArray<Array>(), Dictionary(), 0);
this->new_mesh = memnew(ArrayMesh);
this->surface->commit(this->new_mesh);
callable_mp(this, &self_type::apply_new_mesh).call_deferred();
this->lock.unlock();
}
size_t TerrainChunkMesh::points_per_side() const {