Skip to content

Commit

Permalink
Keep mapblocks in memory if they're in range (#10714)
Browse files Browse the repository at this point in the history
Some other minor parts of clientmap.cpp have been cleaned up along the way
  • Loading branch information
hecktest committed Feb 26, 2021
1 parent 3edb1dd commit 225e690
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 27 deletions.
45 changes: 24 additions & 21 deletions src/client/clientmap.cpp
Expand Up @@ -165,6 +165,9 @@ void ClientMap::updateDrawList()
v3s16 p_blocks_max;
getBlocksInViewRange(cam_pos_nodes, &p_blocks_min, &p_blocks_max);

// Read the vision range, unless unlimited range is enabled.
float range = m_control.range_all ? 1e7 : m_control.wanted_range;

// Number of blocks currently loaded by the client
u32 blocks_loaded = 0;
// Number of blocks with mesh in rendering range
Expand All @@ -182,6 +185,7 @@ void ClientMap::updateDrawList()
occlusion_culling_enabled = false;
}


// Uncomment to debug occluded blocks in the wireframe mode
// TODO: Include this as a flag for an extended debugging setting
//if (occlusion_culling_enabled && m_control.show_wireframe)
Expand Down Expand Up @@ -218,32 +222,34 @@ void ClientMap::updateDrawList()
continue;
}

float range = 100000 * BS;
if (!m_control.range_all)
range = m_control.wanted_range * BS;
v3s16 block_coord = block->getPos();
v3s16 block_position = block->getPosRelative() + MAP_BLOCKSIZE / 2;

float d = 0.0;
if (!isBlockInSight(block->getPos(), camera_position,
camera_direction, camera_fov, range, &d))
continue;
// First, perform a simple distance check, with a padding of one extra block.
if (!m_control.range_all &&
block_position.getDistanceFrom(cam_pos_nodes) > range + MAP_BLOCKSIZE)
continue; // Out of range, skip.

// Keep the block alive as long as it is in range.
block->resetUsageTimer();
blocks_in_range_with_mesh++;

/*
Occlusion culling
*/
// Frustum culling
float d = 0.0;
if (!isBlockInSight(block_coord, camera_position,
camera_direction, camera_fov, range * BS, &d))
continue;

// Occlusion culling
if ((!m_control.range_all && d > m_control.wanted_range * BS) ||
(occlusion_culling_enabled && isBlockOccluded(block, cam_pos_nodes))) {
blocks_occlusion_culled++;
continue;
}

// This block is in range. Reset usage timer.
block->resetUsageTimer();

// Add to set
block->refGrab();
m_drawlist[block->getPos()] = block;
m_drawlist[block_coord] = block;

sector_blocks_drawn++;
} // foreach sectorblocks
Expand Down Expand Up @@ -282,8 +288,6 @@ void ClientMap::renderMap(video::IVideoDriver* driver, s32 pass)
const u32 daynight_ratio = m_client->getEnv().getDayNightRatio();

const v3f camera_position = m_camera_position;
const v3f camera_direction = m_camera_direction;
const f32 camera_fov = m_camera_fov;

/*
Get all blocks and draw all visible ones
Expand All @@ -310,11 +314,10 @@ void ClientMap::renderMap(video::IVideoDriver* driver, s32 pass)
if (!block->mesh)
continue;

float d = 0.0;
if (!isBlockInSight(block->getPos(), camera_position,
camera_direction, camera_fov, 100000 * BS, &d))
continue;

v3f block_pos_r = intToFloat(block->getPosRelative() + MAP_BLOCKSIZE / 2, BS);
float d = camera_position.getDistanceFrom(block_pos_r);
d = MYMAX(0,d - BLOCK_MAX_RADIUS);

// Mesh animation
if (pass == scene::ESNRP_SOLID) {
//MutexAutoLock lock(block->mesh_mutex);
Expand Down
8 changes: 2 additions & 6 deletions src/util/numeric.cpp
Expand Up @@ -106,10 +106,6 @@ u64 murmur_hash_64_ua(const void *key, int len, unsigned int seed)
bool isBlockInSight(v3s16 blockpos_b, v3f camera_pos, v3f camera_dir,
f32 camera_fov, f32 range, f32 *distance_ptr)
{
// Maximum radius of a block. The magic number is
// sqrt(3.0) / 2.0 in literal form.
static constexpr const f32 block_max_radius = 0.866025403784f * MAP_BLOCKSIZE * BS;

v3s16 blockpos_nodes = blockpos_b * MAP_BLOCKSIZE;

// Block center position
Expand All @@ -123,7 +119,7 @@ bool isBlockInSight(v3s16 blockpos_b, v3f camera_pos, v3f camera_dir,
v3f blockpos_relative = blockpos - camera_pos;

// Total distance
f32 d = MYMAX(0, blockpos_relative.getLength() - block_max_radius);
f32 d = MYMAX(0, blockpos_relative.getLength() - BLOCK_MAX_RADIUS);

if (distance_ptr)
*distance_ptr = d;
Expand All @@ -141,7 +137,7 @@ bool isBlockInSight(v3s16 blockpos_b, v3f camera_pos, v3f camera_dir,
// such that a block that has any portion visible with the
// current camera position will have the center visible at the
// adjusted postion
f32 adjdist = block_max_radius / cos((M_PI - camera_fov) / 2);
f32 adjdist = BLOCK_MAX_RADIUS / cos((M_PI - camera_fov) / 2);

// Block position relative to adjusted camera
v3f blockpos_adj = blockpos - (camera_pos - camera_dir * adjdist);
Expand Down
4 changes: 4 additions & 0 deletions src/util/numeric.h
Expand Up @@ -20,6 +20,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#pragma once

#include "basic_macros.h"
#include "constants.h"
#include "irrlichttypes.h"
#include "irr_v2d.h"
#include "irr_v3d.h"
Expand All @@ -36,6 +37,9 @@ with this program; if not, write to the Free Software Foundation, Inc.,
y = temp; \
} while (0)

// Maximum radius of a block. The magic number is
// sqrt(3.0) / 2.0 in literal form.
static constexpr const f32 BLOCK_MAX_RADIUS = 0.866025403784f * MAP_BLOCKSIZE * BS;

inline s16 getContainerPos(s16 p, s16 d)
{
Expand Down

0 comments on commit 225e690

Please sign in to comment.