From 79b6c2dc60a581f4e79fd68dc9ed55e9a9715c25 Mon Sep 17 00:00:00 2001 From: Sara Date: Mon, 8 Dec 2025 20:42:25 +0100 Subject: [PATCH] feat: added position offset to faraway chunks --- modules/terrain_editor/terrain_chunk.cpp | 44 +++++++++++++----------- 1 file changed, 24 insertions(+), 20 deletions(-) diff --git a/modules/terrain_editor/terrain_chunk.cpp b/modules/terrain_editor/terrain_chunk.cpp index 39cb14b8..427d217e 100644 --- a/modules/terrain_editor/terrain_chunk.cpp +++ b/modules/terrain_editor/terrain_chunk.cpp @@ -55,25 +55,28 @@ void TerrainChunk::lod_generated(size_t lod) { } void TerrainChunk::process_lod() { - size_t result{ (size_t)this->meshes.size() }; - if (is_ready() && this->meshes.size() > 0) { - Vector3 position{ get_global_position() }; - position.y = 0; - Vector3 camera{ get_viewport()->get_camera_3d()->get_global_position() }; - camera.y = 0; - float distance{ (position - camera).length() - this->size }; - distance = distance > 0.f ? distance : 0.f; - size_t lod{ size_t(Math::floor(distance / ((this->max_lod_distance * this->size) / this->meshes.size()))) }; - result = lod < this->meshes.size() ? lod : (this->meshes.size() - 1); - if (this->meshes[result].flag == MESH_DIRTY) { - generate_lod(result); - } - while (this->meshes[result].flag != MESH_LOADED && result < (this->meshes.size() - 1)) { - result++; - } - if (this->meshes[result].mesh != this->get_mesh()) { - this->set_mesh(this->meshes[result].mesh); - } + if (!is_ready() || this->meshes.size() == 0) { + return; + } + Vector3 position{ get_global_position() }; + position.y = 0; + Vector3 camera{ get_viewport()->get_camera_3d()->get_global_position() }; + camera.y = 0; + float distance{ (position - camera).length() }; + distance = distance > this->size ? distance - this->size : 0.f; + size_t lod{ size_t(Math::floor(distance / ((this->max_lod_distance * this->size) / this->meshes.size()))) }; + lod = lod < this->meshes.size() ? lod : (this->meshes.size() - 1); + Vector3 pos{ get_position() }; + pos.y = (this->lod_count - lod) * 0.5f; + set_position(pos); + if (this->meshes[lod].flag == MESH_DIRTY) { + generate_lod(lod); + } + while (this->meshes[lod].flag != MESH_LOADED && lod < (this->meshes.size() - 1)) { + lod++; + } + if (this->meshes[lod].mesh != this->get_mesh()) { + this->set_mesh(this->meshes[lod].mesh); } } @@ -85,7 +88,8 @@ void TerrainChunk::_notification(int what) { default: return; case NOTIFICATION_READY: - set_process_thread_group(ProcessThreadGroup::PROCESS_THREAD_GROUP_SUB_THREAD); + set_process_mode(ProcessMode::PROCESS_MODE_ALWAYS); + set_process_thread_group(ProcessThreadGroup::PROCESS_THREAD_GROUP_MAIN_THREAD); set_process(true); ready(); return;