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() { void TerrainChunk::process_lod() {
size_t result{ (size_t)this->meshes.size() }; if (!is_ready() || this->meshes.size() == 0) {
if (is_ready() && this->meshes.size() > 0) { return;
Vector3 position{ get_global_position() }; }
position.y = 0; Vector3 position{ get_global_position() };
Vector3 camera{ get_viewport()->get_camera_3d()->get_global_position() }; position.y = 0;
camera.y = 0; Vector3 camera{ get_viewport()->get_camera_3d()->get_global_position() };
float distance{ (position - camera).length() - this->size }; camera.y = 0;
distance = distance > 0.f ? distance : 0.f; float distance{ (position - camera).length() };
size_t lod{ size_t(Math::floor(distance / ((this->max_lod_distance * this->size) / this->meshes.size()))) }; distance = distance > this->size ? distance - this->size : 0.f;
result = lod < this->meshes.size() ? lod : (this->meshes.size() - 1); size_t lod{ size_t(Math::floor(distance / ((this->max_lod_distance * this->size) / this->meshes.size()))) };
if (this->meshes[result].flag == MESH_DIRTY) { lod = lod < this->meshes.size() ? lod : (this->meshes.size() - 1);
generate_lod(result); Vector3 pos{ get_position() };
} pos.y = (this->lod_count - lod) * 0.5f;
while (this->meshes[result].flag != MESH_LOADED && result < (this->meshes.size() - 1)) { set_position(pos);
result++; if (this->meshes[lod].flag == MESH_DIRTY) {
} generate_lod(lod);
if (this->meshes[result].mesh != this->get_mesh()) { }
this->set_mesh(this->meshes[result].mesh); 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: default:
return; return;
case NOTIFICATION_READY: 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); set_process(true);
ready(); ready();
return; return;