Optimize raycast performance ()

by skipping nodes not on the ray with selection boxes smaller than 1x1x1 early on
This commit is contained in:
sfence
2024-12-14 17:01:06 +01:00
committed by GitHub
parent ba63c1505a
commit f7a695c212
4 changed files with 25 additions and 0 deletions

@ -9948,6 +9948,8 @@ Used by `core.register_node`.
selection_box = { selection_box = {
-- see [Node boxes] for possibilities -- see [Node boxes] for possibilities
-- Selection boxes that oversize node size can cause
-- significant performance drop of Raycasts.
}, },
-- Custom selection box definition. Multiple boxes can be defined. -- Custom selection box definition. Multiple boxes can be defined.
-- If "nodebox" drawtype is used and selection_box is nil, then node_box -- If "nodebox" drawtype is used and selection_box is nil, then node_box

@ -163,6 +163,8 @@ void Environment::continueRaycast(RaycastState *state, PointedThing *result_p)
break; // About to go out of bounds break; // About to go out of bounds
} }
const v3s16 pos_on_ray = state->m_iterator.m_current_node_pos;
// For each untested node // For each untested node
for (s16 z = new_nodes.MinEdge.Z; z <= new_nodes.MaxEdge.Z; z++) for (s16 z = new_nodes.MinEdge.Z; z <= new_nodes.MaxEdge.Z; z++)
for (s16 y = new_nodes.MinEdge.Y; y <= new_nodes.MaxEdge.Y; y++) for (s16 y = new_nodes.MinEdge.Y; y <= new_nodes.MaxEdge.Y; y++)
@ -175,6 +177,10 @@ void Environment::continueRaycast(RaycastState *state, PointedThing *result_p)
if (!is_valid_position) if (!is_valid_position)
continue; continue;
// Optimization: Skip non-oversized selection boxes for other positions.
if ((pos_on_ray != np) && !nodedef->get(n).has_big_selection_box)
continue;
PointabilityType pointable = isPointableNode(n, nodedef, PointabilityType pointable = isPointableNode(n, nodedef,
state->m_liquids_pointable, state->m_liquids_pointable,
state->m_pointabilities); state->m_pointabilities);

@ -1257,6 +1257,15 @@ inline void NodeDefManager::fixSelectionBoxIntUnion()
m_selection_box_union.MaxEdge.Z / BS - 0.5f); m_selection_box_union.MaxEdge.Z / BS - 0.5f);
} }
inline void NodeDefManager::calcBigSelectionBox(content_t id, const ContentFeatures &def)
{
aabb3f box_union;
getNodeBoxUnion(def.selection_box, def, &box_union);
m_content_features[id].has_big_selection_box =
(box_union.MinEdge.X < -BS/2) || (box_union.MaxEdge.X > BS/2) ||
(box_union.MinEdge.Y < -BS/2) || (box_union.MaxEdge.Y > BS/2) ||
(box_union.MinEdge.Z < -BS/2) || (box_union.MaxEdge.Z > BS/2);
}
void NodeDefManager::eraseIdFromGroups(content_t id) void NodeDefManager::eraseIdFromGroups(content_t id)
{ {
@ -1312,6 +1321,7 @@ content_t NodeDefManager::set(const std::string &name, const ContentFeatures &de
getNodeBoxUnion(def.selection_box, def, &m_selection_box_union); getNodeBoxUnion(def.selection_box, def, &m_selection_box_union);
fixSelectionBoxIntUnion(); fixSelectionBoxIntUnion();
calcBigSelectionBox(id, def);
// Add this content to the list of all groups it belongs to // Add this content to the list of all groups it belongs to
for (const auto &group : def.groups) { for (const auto &group : def.groups) {
const std::string &group_name = group.first; const std::string &group_name = group.first;
@ -1525,6 +1535,8 @@ void NodeDefManager::deSerialize(std::istream &is, u16 protocol_version)
getNodeBoxUnion(f.selection_box, f, &m_selection_box_union); getNodeBoxUnion(f.selection_box, f, &m_selection_box_union);
fixSelectionBoxIntUnion(); fixSelectionBoxIntUnion();
calcBigSelectionBox(i, f);
} }
// Since liquid_alternative_flowing_id and liquid_alternative_source_id // Since liquid_alternative_flowing_id and liquid_alternative_source_id

@ -425,6 +425,8 @@ struct ContentFeatures
NodeBox node_box; NodeBox node_box;
NodeBox selection_box; NodeBox selection_box;
NodeBox collision_box; NodeBox collision_box;
//! Whether any selection box extent is > BS/2.
bool has_big_selection_box;
// --- SOUND PROPERTIES --- // --- SOUND PROPERTIES ---
@ -774,6 +776,9 @@ private:
*/ */
void fixSelectionBoxIntUnion(); void fixSelectionBoxIntUnion();
//! Calculates ContentFeatures::&has_big_selection_box
void calcBigSelectionBox(content_t id, const ContentFeatures &def);
//! Features indexed by ID. //! Features indexed by ID.
std::vector<ContentFeatures> m_content_features; std::vector<ContentFeatures> m_content_features;