Occlusion: Begin cleanup

This commit is contained in:
SmallJoker 2019-08-17 14:21:22 +02:00
parent ba8fb774c1
commit 7d016b4efd
3 changed files with 51 additions and 45 deletions

View File

@ -144,7 +144,7 @@ void ClientMap::updateDrawList()
// No occlusion culling when free_move is on and camera is // No occlusion culling when free_move is on and camera is
// inside ground // inside ground
bool occlusion_culling_enabled = true; bool occlusion_culling_enabled = true;
if (g_settings->getBool("free_move")) { if (g_settings->getBool("free_move") && g_settings->getBool("noclip")) {
MapNode n = getNode(cam_pos_nodes); MapNode n = getNode(cam_pos_nodes);
if (n.getContent() == CONTENT_IGNORE || if (n.getContent() == CONTENT_IGNORE ||
m_nodedef->get(n).solidness == 2) m_nodedef->get(n).solidness == 2)

View File

@ -1032,24 +1032,23 @@ void Map::removeNodeTimer(v3s16 p)
block->m_node_timers.remove(p_rel); block->m_node_timers.remove(p_rel);
} }
bool Map::isOccluded(v3s16 p0, v3s16 p1, float step, float stepfac, bool Map::isOccluded(v3s16 pos_origin, v3s16 pos_blockcenter, float step,
float start_off, float end_off, u32 needed_count) float stepfac, float offset, float end_offset, u32 needed_count)
{ {
float d0 = (float)BS * p0.getDistanceFrom(p1); float distance = BS * pos_origin.getDistanceFrom(pos_blockcenter);
v3s16 u0 = p1 - p0; v3f direction = intToFloat(pos_blockcenter - pos_origin, BS);
v3f uf = v3f(u0.X, u0.Y, u0.Z) * BS; direction.normalize();
uf.normalize(); v3f pos_origin_f = intToFloat(pos_origin, BS);
v3f p0f = v3f(p0.X, p0.Y, p0.Z) * BS;
u32 count = 0; u32 count = 0;
for(float s=start_off; s<d0+end_off; s+=step){ for (; offset < distance + end_offset; offset += step){
v3f pf = p0f + uf * s; v3f pf = pos_origin_f + direction * offset;
v3s16 p = floatToInt(pf, BS); v3s16 pos_node = floatToInt(pf, BS);
MapNode n = getNode(p); MapNode node = getNode(pos_node);
const ContentFeatures &f = m_nodedef->get(n); if (m_nodedef->get(node).drawtype == NDT_NORMAL) {
if(f.drawtype == NDT_NORMAL){
// not transparent, see ContentFeature::updateTextures // not transparent, see ContentFeature::updateTextures
count++; count++;
if(count >= needed_count) if (count >= needed_count)
return true; return true;
} }
step *= stepfac; step *= stepfac;
@ -1057,44 +1056,51 @@ bool Map::isOccluded(v3s16 p0, v3s16 p1, float step, float stepfac,
return false; return false;
} }
bool Map::isBlockOccluded(MapBlock *block, v3s16 cam_pos_nodes) { bool Map::isBlockOccluded(MapBlock *block, v3s16 cam_pos_nodes)
v3s16 cpn = block->getPos() * MAP_BLOCKSIZE; {
cpn += v3s16(MAP_BLOCKSIZE / 2, MAP_BLOCKSIZE / 2, MAP_BLOCKSIZE / 2); static const s16 bs2 = MAP_BLOCKSIZE / 2 + 1;
static const v3s16 dir8[8] = {
v3s16( 1, 1, 1) * bs2,
v3s16( 1, 1, -1) * bs2,
v3s16( 1, -1, 1) * bs2,
v3s16( 1, -1, -1) * bs2,
v3s16(-1, 1, 1) * bs2,
v3s16(-1, 1, -1) * bs2,
v3s16(-1, -1, 1) * bs2,
v3s16(-1, -1, -1) * bs2,
};
v3s16 pos_blockcenter = block->getPos() * MAP_BLOCKSIZE;
pos_blockcenter += v3s16(MAP_BLOCKSIZE) / 2;
float step = BS * 1; float step = BS * 1;
// Multiply step by each iteraction by 'stepfac' to reduce checks in distance
float stepfac = 1.1; float stepfac = 1.1;
float startoff = BS * 1;
float start_offset = BS * 1;
// The occlusion search of 'isOccluded()' must stop short of the target // The occlusion search of 'isOccluded()' must stop short of the target
// point by distance 'endoff' (end offset) to not enter the target mapblock. // point by distance 'end_offset' (end offset) to not enter the target mapblock.
// For the 8 mapblock corners 'endoff' must therefore be the maximum diagonal // For the 8 mapblock corners 'end_offset' must therefore be the maximum diagonal
// of a mapblock, because we must consider all view angles. // of a mapblock, because we must consider all view angles.
// sqrt(1^2 + 1^2 + 1^2) = 1.732 // sqrt(1^2 + 1^2 + 1^2) = 1.732
float endoff = -BS * MAP_BLOCKSIZE * 1.732050807569; float end_offset = -BS * MAP_BLOCKSIZE * 1.7321;
s16 bs2 = MAP_BLOCKSIZE / 2 + 1;
// to reduce the likelihood of falsely occluded blocks // to reduce the likelihood of falsely occluded blocks
// require at least two solid blocks // require at least two solid blocks
// this is a HACK, we should think of a more precise algorithm // this is a HACK, we should think of a more precise algorithm
u32 needed_count = 2; u32 needed_count = 2;
return ( // For the central point of the mapblock 'end_offset' can be halved
// For the central point of the mapblock 'endoff' can be halved if (!isOccluded(cam_pos_nodes, pos_blockcenter,
isOccluded(cam_pos_nodes, cpn, step, stepfac, start_offset, end_offset / 2.0f, needed_count))
step, stepfac, startoff, endoff / 2.0f, needed_count) && return false;
isOccluded(cam_pos_nodes, cpn + v3s16(bs2,bs2,bs2),
step, stepfac, startoff, endoff, needed_count) && for (const v3s16 &dir : dir8) {
isOccluded(cam_pos_nodes, cpn + v3s16(bs2,bs2,-bs2), if (!isOccluded(cam_pos_nodes, pos_blockcenter + dir,
step, stepfac, startoff, endoff, needed_count) && step, stepfac, start_offset, end_offset, needed_count))
isOccluded(cam_pos_nodes, cpn + v3s16(bs2,-bs2,bs2), return false;
step, stepfac, startoff, endoff, needed_count) && }
isOccluded(cam_pos_nodes, cpn + v3s16(bs2,-bs2,-bs2), return true;
step, stepfac, startoff, endoff, needed_count) &&
isOccluded(cam_pos_nodes, cpn + v3s16(-bs2,bs2,bs2),
step, stepfac, startoff, endoff, needed_count) &&
isOccluded(cam_pos_nodes, cpn + v3s16(-bs2,bs2,-bs2),
step, stepfac, startoff, endoff, needed_count) &&
isOccluded(cam_pos_nodes, cpn + v3s16(-bs2,-bs2,bs2),
step, stepfac, startoff, endoff, needed_count) &&
isOccluded(cam_pos_nodes, cpn + v3s16(-bs2,-bs2,-bs2),
step, stepfac, startoff, endoff, needed_count));
} }
/* /*

View File

@ -310,8 +310,8 @@ protected:
// This stores the properties of the nodes on the map. // This stores the properties of the nodes on the map.
const NodeDefManager *m_nodedef; const NodeDefManager *m_nodedef;
bool isOccluded(v3s16 p0, v3s16 p1, float step, float stepfac, bool isOccluded(v3s16 pos_origin, v3s16 pos_blockcenter, float step,
float start_off, float end_off, u32 needed_count); float stepfac, float start_offset, float end_offset, u32 needed_count);
private: private:
f32 m_transforming_liquid_loop_count_multiplier = 1.0f; f32 m_transforming_liquid_loop_count_multiplier = 1.0f;