feat: added position offset to faraway chunks

This commit is contained in:
Sara Gerretsen 2025-12-08 20:42:25 +01:00
parent 2a3eeef522
commit 79b6c2dc60

View file

@ -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;