Fix infinite viewing_range (#13225)

Use a simplified version of the old loops culler for infinite viewing range.
This commit is contained in:
lhofhansl 2023-03-12 09:37:47 -07:00 committed by GitHub
parent b1ed0ef721
commit 3e148e2810
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 239 additions and 177 deletions

@ -262,20 +262,12 @@ void ClientMap::updateDrawList()
} }
m_keeplist.clear(); m_keeplist.clear();
v3s16 cam_pos_nodes = floatToInt(m_camera_position, BS); const v3s16 cam_pos_nodes = floatToInt(m_camera_position, BS);
v3s16 p_blocks_min;
v3s16 p_blocks_max;
getBlocksInViewRange(cam_pos_nodes, &p_blocks_min, &p_blocks_max);
// Number of blocks occlusion culled // Number of blocks occlusion culled
u32 blocks_occlusion_culled = 0; u32 blocks_occlusion_culled = 0;
// Number of blocks frustum culled // Number of blocks frustum culled
u32 blocks_frustum_culled = 0; u32 blocks_frustum_culled = 0;
// Blocks visited by the algorithm
u32 blocks_visited = 0;
// Block sides that were not traversed
u32 sides_skipped = 0;
MeshGrid mesh_grid = m_client->getMeshGrid(); MeshGrid mesh_grid = m_client->getMeshGrid();
@ -289,7 +281,7 @@ void ClientMap::updateDrawList()
occlusion_culling_enabled = false; occlusion_culling_enabled = false;
} }
v3s16 camera_block = getContainerPos(cam_pos_nodes, MAP_BLOCKSIZE); const v3s16 camera_block = getContainerPos(cam_pos_nodes, MAP_BLOCKSIZE);
m_drawlist = std::map<v3s16, MapBlock*, MapBlockComparer>(MapBlockComparer(camera_block)); m_drawlist = std::map<v3s16, MapBlock*, MapBlockComparer>(MapBlockComparer(camera_block));
auto is_frustum_culled = m_client->getCamera()->getFrustumCuller(); auto is_frustum_culled = m_client->getCamera()->getFrustumCuller();
@ -299,6 +291,79 @@ void ClientMap::updateDrawList()
// if (occlusion_culling_enabled && m_control.show_wireframe) // if (occlusion_culling_enabled && m_control.show_wireframe)
// occlusion_culling_enabled = porting::getTimeS() & 1; // occlusion_culling_enabled = porting::getTimeS() & 1;
// Set of mesh holding blocks
std::set<v3s16> shortlist;
/*
When range_all is enabled, enumerate all blocks visible in the
frustum and display them.
*/
if (m_control.range_all) {
MapBlockVect sectorblocks;
for (auto &sector_it : m_sectors) {
MapSector *sector = sector_it.second;
if (!sector)
continue;
sectorblocks.clear();
sector->getBlocks(sectorblocks);
// Loop through blocks in sector
for (MapBlock *block : sectorblocks) {
MapBlockMesh *mesh = block->mesh;
// Calculate the coordinates for range and frustum culling
v3f mesh_sphere_center;
f32 mesh_sphere_radius;
v3s16 block_pos_nodes = block->getPos() * MAP_BLOCKSIZE;
if (mesh) {
mesh_sphere_center = intToFloat(block_pos_nodes, BS)
+ mesh->getBoundingSphereCenter();
mesh_sphere_radius = mesh->getBoundingRadius();
} else {
mesh_sphere_center = intToFloat(block_pos_nodes, BS)
+ v3f((MAP_BLOCKSIZE * 0.5f - 0.5f) * BS);
mesh_sphere_radius = 0.0f;
}
// Frustum culling
// Only do coarse culling here, to account for fast camera movement.
// This is needed because this function is not called every frame.
float frustum_cull_extra_radius = 300.0f;
if (is_frustum_culled(mesh_sphere_center,
mesh_sphere_radius + frustum_cull_extra_radius)) {
blocks_frustum_culled++;
continue;
}
if (mesh_grid.cell_size > 1) {
// Block meshes are stored in the corner block of a chunk
// (where all coordinate are divisible by the chunk size)
// Add them to the de-dup set.
shortlist.emplace(mesh_grid.getMeshPos(block->getPos()));
// All other blocks we can grab and add to the keeplist right away.
m_keeplist.push_back(block);
block->refGrab();
} else if (mesh) {
// without mesh chunking we can add the block to the drawlist
block->refGrab();
m_drawlist.emplace(block->getPos(), block);
}
}
}
} else {
// Blocks visited by the algorithm
u32 blocks_visited = 0;
// Block sides that were not traversed
u32 sides_skipped = 0;
v3s16 p_blocks_min;
v3s16 p_blocks_max;
getBlocksInViewRange(cam_pos_nodes, &p_blocks_min, &p_blocks_max);
std::queue<v3s16> blocks_to_consider; std::queue<v3s16> blocks_to_consider;
v3s16 camera_mesh = mesh_grid.getMeshPos(camera_block); v3s16 camera_mesh = mesh_grid.getMeshPos(camera_block);
@ -312,10 +377,8 @@ void ClientMap::updateDrawList()
blocks_to_consider.push(camera_mesh); blocks_to_consider.push(camera_mesh);
meshes_seen.getChunk(camera_cell).getBits(camera_cell) = 0x07; // mark all sides as visible meshes_seen.getChunk(camera_cell).getBits(camera_cell) = 0x07; // mark all sides as visible
std::set<v3s16> shortlist;
// Recursively walk the space and pick mapblocks for drawing // Recursively walk the space and pick mapblocks for drawing
while (blocks_to_consider.size() > 0) { while (!blocks_to_consider.empty()) {
v3s16 block_coord = blocks_to_consider.front(); v3s16 block_coord = blocks_to_consider.front();
blocks_to_consider.pop(); blocks_to_consider.pop();
@ -347,8 +410,7 @@ void ClientMap::updateDrawList()
mesh_sphere_center = intToFloat(block_pos_nodes, BS) mesh_sphere_center = intToFloat(block_pos_nodes, BS)
+ mesh->getBoundingSphereCenter(); + mesh->getBoundingSphereCenter();
mesh_sphere_radius = mesh->getBoundingRadius(); mesh_sphere_radius = mesh->getBoundingRadius();
} } else {
else {
mesh_sphere_center = intToFloat(block_pos_nodes, BS) + v3f((mesh_grid.cell_size * MAP_BLOCKSIZE * 0.5f - 0.5f) * BS); mesh_sphere_center = intToFloat(block_pos_nodes, BS) + v3f((mesh_grid.cell_size * MAP_BLOCKSIZE * 0.5f - 0.5f) * BS);
mesh_sphere_radius = 0.87f * mesh_grid.cell_size * MAP_BLOCKSIZE * BS; mesh_sphere_radius = 0.87f * mesh_grid.cell_size * MAP_BLOCKSIZE * BS;
} }
@ -394,8 +456,7 @@ void ClientMap::updateDrawList()
m_keeplist.push_back(block); m_keeplist.push_back(block);
block->refGrab(); block->refGrab();
} }
} } else if (mesh) {
else if (mesh) {
// without mesh chunking we can add the block to the drawlist // without mesh chunking we can add the block to the drawlist
block->refGrab(); block->refGrab();
m_drawlist.emplace(block_coord, block); m_drawlist.emplace(block_coord, block);
@ -500,12 +561,14 @@ void ClientMap::updateDrawList()
traverse_far_side(+mesh_grid.cell_size); traverse_far_side(+mesh_grid.cell_size);
} }
} }
g_profiler->avg("MapBlocks sides skipped [#]", sides_skipped);
g_profiler->avg("MapBlocks examined [#]", blocks_visited);
}
g_profiler->avg("MapBlocks shortlist [#]", shortlist.size()); g_profiler->avg("MapBlocks shortlist [#]", shortlist.size());
assert(m_drawlist.empty() || shortlist.empty()); assert(m_drawlist.empty() || shortlist.empty());
for (auto pos : shortlist) { for (auto pos : shortlist) {
MapBlock * block = getBlockNoCreateNoEx(pos); MapBlock *block = getBlockNoCreateNoEx(pos);
if (block) { if (block) {
block->refGrab(); block->refGrab();
m_drawlist.emplace(pos, block); m_drawlist.emplace(pos, block);
@ -514,8 +577,6 @@ void ClientMap::updateDrawList()
g_profiler->avg("MapBlocks occlusion culled [#]", blocks_occlusion_culled); g_profiler->avg("MapBlocks occlusion culled [#]", blocks_occlusion_culled);
g_profiler->avg("MapBlocks frustum culled [#]", blocks_frustum_culled); g_profiler->avg("MapBlocks frustum culled [#]", blocks_frustum_culled);
g_profiler->avg("MapBlocks sides skipped [#]", sides_skipped);
g_profiler->avg("MapBlocks examined [#]", blocks_visited);
g_profiler->avg("MapBlocks drawn [#]", m_drawlist.size()); g_profiler->avg("MapBlocks drawn [#]", m_drawlist.size());
} }

@ -124,6 +124,7 @@ void MapSector::deleteBlock(MapBlock *block)
void MapSector::getBlocks(MapBlockVect &dest) void MapSector::getBlocks(MapBlockVect &dest)
{ {
dest.reserve(dest.size() + m_blocks.size());
for (auto &block : m_blocks) { for (auto &block : m_blocks) {
dest.push_back(block.second); dest.push_back(block.second);
} }