From 156ba027507c57ed372206006ebaaaddf692273e Mon Sep 17 00:00:00 2001 From: MihailRis Date: Thu, 12 Nov 2026 23:43:00 +0300 Subject: [PATCH] optimize ChunksRenderer by reducing meshes map reads --- src/frontend/screens/LevelScreen.cpp | 6 ++-- src/graphics/render/ChunksRenderer.cpp | 40 +++++++++++++++++--------- 2 files changed, 29 insertions(+), 17 deletions(-) diff --git a/src/frontend/screens/LevelScreen.cpp b/src/frontend/screens/LevelScreen.cpp index 2f29e8b7d..ea02fe025 100644 --- a/src/frontend/screens/LevelScreen.cpp +++ b/src/frontend/screens/LevelScreen.cpp @@ -24,7 +24,6 @@ #include "logic/PlayerController.hpp" #include "logic/scripting/scripting.hpp" #include "logic/scripting/scripting_hud.hpp" -#include "maths/voxmaths.hpp" #include "objects/Players.hpp" #include "objects/Entities.hpp" #include "physics/Hitbox.hpp" @@ -257,8 +256,7 @@ void LevelScreen::update(float delta) { updateHotkeys(); } updateAudio(); - - auto menu = gui.getMenu(); + bool inputLocked = gui.getActiveFrame() || hud->isInventoryOpen() || gui.isFocusCaught(); bool paused = hud->isPause(); @@ -284,7 +282,7 @@ void LevelScreen::update(float delta) { renderer->update(camera, delta * !hud->isPause()); } -void LevelScreen::draw(float delta) { +void LevelScreen::draw(float) { Camera& camera = *playerController->getPlayer().currentCamera; const DrawContext ctx(nullptr, engine.getWindow(), batch.get()); diff --git a/src/graphics/render/ChunksRenderer.cpp b/src/graphics/render/ChunksRenderer.cpp index 6cccbe3fa..3f8fad4e3 100644 --- a/src/graphics/render/ChunksRenderer.cpp +++ b/src/graphics/render/ChunksRenderer.cpp @@ -315,6 +315,19 @@ void ChunksRenderer::drawChunks( if (chunk == nullptr) { continue; } + int cx = chunk->x; + int cz = chunk->z; + if (culling) { + glm::vec3 min(cx * CHUNK_W, chunk->bottom, cz * CHUNK_D); + glm::vec3 max( + cx * CHUNK_W + CHUNK_W, chunk->top, cz * CHUNK_D + CHUNK_D + ); + + if (!frustum.isBoxVisible(min, max)) { + continue; + } + } + auto found = meshes.find(glm::ivec2(chunk->x, chunk->z)); if (found == meshes.end()) { continue; @@ -323,19 +336,19 @@ void ChunksRenderer::drawChunks( if (mesh.mesh == nullptr) { continue; } + if (culling) { const auto& meshAABB = mesh.meshAABB; auto aabbMin = meshAABB.min(); auto aabbMax = meshAABB.max(); + glm::vec3 min( - chunk->x * CHUNK_W + std::min(0.0f, aabbMin.x), + cx * CHUNK_W + std::min(0.0f, aabbMin.x), chunk->bottom, - chunk->z * CHUNK_D + std::min(0.0f, aabbMin.z) + cz * CHUNK_D + std::min(0.0f, aabbMin.z) ); glm::vec3 max( - chunk->x * CHUNK_W + aabbMax.x, - chunk->top, - chunk->z * CHUNK_D + aabbMax.z + cx * CHUNK_W + aabbMax.x, chunk->top, cz * CHUNK_D + aabbMax.z ); if (!frustum.isBoxVisible(min, max)) { @@ -388,22 +401,23 @@ void ChunksRenderer::drawSortedMeshes(const Camera& camera, Shader& shader) { if (chunk == nullptr || !chunk->flags.lighted) { continue; } - const auto& found = meshes.find(glm::ivec2(chunk->x, chunk->z)); - if (found == meshes.end() || found->second.sortingMeshData.entries.empty()) { - continue; - } + int cx = chunk->x; + int cz = chunk->z; if (culling) { - glm::vec3 min(chunk->x * CHUNK_W, chunk->bottom, chunk->z * CHUNK_D); + glm::vec3 min(cx * CHUNK_W, chunk->bottom, cz * CHUNK_D); glm::vec3 max( - chunk->x * CHUNK_W + CHUNK_W, - chunk->top, - chunk->z * CHUNK_D + CHUNK_D + cx * CHUNK_W + CHUNK_W, chunk->top, cz * CHUNK_D + CHUNK_D ); if (!frustum.isBoxVisible(min, max)) continue; } + const auto& found = meshes.find(glm::ivec2(cx, cz)); + if (found == meshes.end() || found->second.sortingMeshData.entries.empty()) { + continue; + } + auto& chunkEntries = found->second.sortingMeshData.entries; if (chunkEntries.size() == 1) {