fix: rebuilding grid fully stops threads first

This commit is contained in:
Sara Gerretsen 2026-02-25 23:05:03 +01:00
parent 1e44fcd09f
commit 0d982a751c
2 changed files with 24 additions and 12 deletions

View file

@ -92,13 +92,20 @@ void Terrain::generate_meshes_thread(void *terrain) {
}
void Terrain::construct_chunk_grid() {
this->workload_lock.lock();
this->threads_stop = true;
this->workload_lock.unlock();
for (Thread &thread : this->threads) {
thread.wait_to_finish();
}
this->workload_lock.lock();
for (TerrainChunkMesh *mesh : this->meshes) {
mesh->queue_free();
}
this->meshes.clear();
size_t const chunks_per_side{ this->side_length / this->chunk_size };
Vector3 const origin{ (float)this->chunk_size / 2.f, 0.f, (float)this->chunk_size / 2.f };
this->workload_lock.lock();
this->meshes.clear();
this->workload.clear();
for (size_t y{ 0 }; y < chunks_per_side; ++y) {
for (size_t x{ 0 }; x < chunks_per_side; ++x) {
TerrainChunkMesh *chunk{ memnew(TerrainChunkMesh) };
@ -112,7 +119,12 @@ void Terrain::construct_chunk_grid() {
this->workload.push_back(chunk);
}
}
this->threads_stop = false;
this->dirty_meshes.clear();
this->workload_lock.unlock();
for (Thread &thread : this->threads) {
thread.start(&self_type::generate_meshes_thread, this);
}
}
float Terrain::height_at(Vector2 world_coordinate) {

View file

@ -13,15 +13,15 @@ background_mode = 2
sky = SubResource("Sky_w3uoq")
[sub_resource type="Curve" id="Curve_kbmr5"]
_limits = [0.0, 1.0, 0.0, 500.0]
_data = [Vector2(0, 1), 0.0, 0.0, 0, 0, Vector2(76.609314, 0.6878389), -0.006221681, -0.006221681, 0, 0, Vector2(238.22075, 0.107827306), -0.0011518027, -0.0011518027, 0, 0, Vector2(500, 0), 0.0, 0.0, 0, 0]
_limits = [0.0, 1.0, 0.0, 323.34308]
_data = [Vector2(0, 1), 0.0, 0.0, 0, 0, Vector2(134.4993, 0.6274206), -0.006221681, -0.006221681, 0, 0, Vector2(237.39348, 0.0977577), -0.0030463666, -0.0030463666, 0, 0, Vector2(323.34308, 0), 0.0, 0.0, 0, 0]
point_count = 4
[sub_resource type="Curve" id="Curve_w3uoq"]
[sub_resource type="Curve" id="Curve_chm2y"]
_limits = [0.0, 1.0, 0.0, 200.0]
_data = [Vector2(0, 1), 0.0, -0.0043322877, 0, 0, Vector2(92.849304, 0.5972115), -0.0058290004, -0.0058290004, 0, 0, Vector2(199.46088, 0), 0.00017739221, -0.05797184, 0, 0]
_limits = [0.0, 1.0, 0.0, 500.0]
_data = [Vector2(0, 1), 0.0, -0.0016919965, 0, 0, Vector2(228.71338, 0.44817185), -0.0011880936, -0.0011880936, 0, 0, Vector2(500, 0), 7.00571e-05, -0.05797184, 0, 0]
point_count = 3
[sub_resource type="Curve" id="Curve_o3i6r"]
@ -31,7 +31,7 @@ point_count = 1
[sub_resource type="Curve" id="Curve_nonsf"]
_limits = [0.0, 1.0, 0.0, 500.0]
_data = [Vector2(0, 1), 0.0, 0.0, 0, 0, Vector2(101.02984, 0.7462511), -0.0056187166, -0.0056187166, 0, 0, Vector2(500, 0), 0.0, 0.0, 0, 0]
_data = [Vector2(0, 1), 0.0, 0.0, 0, 0, Vector2(287.8075, 0.3595702), -0.0056187166, -0.0056187166, 0, 0, Vector2(500, 0), 0.0, 0.0, 0, 0]
point_count = 3
[sub_resource type="Curve" id="Curve_4kj3c"]
@ -48,24 +48,24 @@ environment = SubResource("Environment_o3i6r")
[node name="Terrain" type="Terrain" parent="." unique_id=1169843565]
side_length = 1000
chunk_size = 200
chunk_size = 100
thread_count = 2
[node name="TerrainModifierDistance" type="TerrainModifierDistance" parent="Terrain" unique_id=1885116624]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 544.8632, 213.3872, 613.2415)
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 115.563965, 82.77078, 637.2768)
blend_distance = 4.0
distance_weight_curve = SubResource("Curve_kbmr5")
distance_height_curve = SubResource("Curve_w3uoq")
[node name="TerrainModifierDistance3" type="TerrainModifierDistance" parent="Terrain" unique_id=1846439541]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 534.7505, 330.91986, 385.44415)
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 392.32507, 273.6683, 272.20636)
blend_distance = 4.0
distance_weight_curve = SubResource("Curve_chm2y")
distance_height_curve = SubResource("Curve_o3i6r")
[node name="TerrainModifierDistance2" type="TerrainModifierDistance" parent="Terrain" unique_id=2110821264]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 788.26746, -122.60801, 675.6073)
blend_mode = 1
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 788.26746, -90.20175, 675.6073)
blend_mode = 2
distance_weight_curve = SubResource("Curve_nonsf")
distance_height_curve = SubResource("Curve_4kj3c")