Skip to content

Commit

Permalink
Fix instance blocks not cleared when regenerating terrain
Browse files Browse the repository at this point in the history
  • Loading branch information
Zylann committed Oct 26, 2023
1 parent ab5422e commit 22be792
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 18 deletions.
1 change: 1 addition & 0 deletions doc/source/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ Semver is not yet in place, so each version can have breaking changes, although
- `VoxelInstancer`:
- Fixed crash when hiding the node in the editor
- Fixed crash when closing the scene while an instancer node is selected
- Fixed instances were not cleared when using the "Re-generate" menu in the editor when terrain shape changed
- `VoxelInstanceLibrary`:
- Fixed `find_item_by_name` was not finding items
- Fixed newly added items in the editor rendering badly by default when the terrain doesn't have LOD. For now they always default to LOD 0 instead of LOD 2.
Expand Down
40 changes: 22 additions & 18 deletions terrain/fixed_lod/voxel_terrain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -227,24 +227,8 @@ void VoxelTerrain::set_mesh_block_size(unsigned int mesh_block_size) {

_mesh_block_size_po2 = po2;

if (_instancer != nullptr) {
VoxelInstancer &instancer = *_instancer;
_mesh_map.for_each_block([&instancer, this](VoxelMeshBlockVT &block) { //
instancer.on_mesh_block_exit(block.position, 0);
if (block.is_loaded) {
emit_mesh_block_exited(block.position);
}
});
} else {
_mesh_map.for_each_block([this](VoxelMeshBlockVT &block) { //
if (block.is_loaded) {
emit_mesh_block_exited(block.position);
}
});
}

// Unload all mesh blocks regardless of refcount
_mesh_map.clear();
clear_mesh_map();

// Make paired viewers re-view the new meshable area
for (unsigned int i = 0; i < _paired_viewers.size(); ++i) {
Expand Down Expand Up @@ -690,6 +674,26 @@ void VoxelTerrain::stop_streamer() {
_blocks_pending_load.clear();
}

void VoxelTerrain::clear_mesh_map() {
if (_instancer != nullptr) {
VoxelInstancer &instancer = *_instancer;
_mesh_map.for_each_block([&instancer, this](VoxelMeshBlockVT &block) { //
instancer.on_mesh_block_exit(block.position, 0);
if (block.is_loaded) {
emit_mesh_block_exited(block.position);
}
});
} else {
_mesh_map.for_each_block([this](VoxelMeshBlockVT &block) { //
if (block.is_loaded) {
emit_mesh_block_exited(block.position);
}
});
}

_mesh_map.clear();
}

void VoxelTerrain::reset_map() {
// Discard everything, to reload it all

Expand All @@ -698,7 +702,7 @@ void VoxelTerrain::reset_map() {
});
_data->reset_maps();

_mesh_map.clear();
clear_mesh_map();

_loading_blocks.clear();
_blocks_pending_load.clear();
Expand Down
1 change: 1 addition & 0 deletions terrain/fixed_lod/voxel_terrain.h
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ class VoxelTerrain : public VoxelNode {
void start_streamer();
void stop_streamer();
void reset_map();
void clear_mesh_map();

// void view_data_block(Vector3i bpos, uint32_t viewer_id, bool require_notification);
void view_mesh_block(Vector3i bpos, bool mesh_flag, bool collision_flag);
Expand Down

0 comments on commit 22be792

Please sign in to comment.