Merge pull request #793 from MihailRis/fix-extended-blocks-culling-on-chunk-borders

fix extended blocks culling on chunk borders
This commit is contained in:
MihailRis 2026-02-18 23:02:54 +03:00 • committed by GitHub
commit 2a730ec23f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 39 additions and 13 deletions

View file

@ -251,6 +251,10 @@ void BlocksRenderer::blockAABB(
Z = orient.axes[2]; Z = orient.axes[2];
orient.transform(hitbox); orient.transform(hitbox);
} }
if (block->rt.extended) {
meshAABB.addPoint(coord + hitbox.max());
meshAABB.addPoint(coord + hitbox.min());
}
coord -= glm::vec3(0.5f) - hitbox.center(); coord -= glm::vec3(0.5f) - hitbox.center();
if (ao) { if (ao) {
@ -306,6 +310,12 @@ void BlocksRenderer::blockCustomModel(
if (culled) { if (culled) {
return; return;
} }
} else {
meshAABB.addPoint(
coord + X * glm::vec3(block.size.x) +
Y * glm::vec3(block.size.y) +
Z * glm::vec3(block.size.z)
);
} }
const auto& model = cache.getModel(block.rt.id, block.getVariantIndex(states.userbits)); const auto& model = cache.getModel(block.rt.id, block.getVariantIndex(states.userbits));
@ -687,6 +697,7 @@ SortingMeshData BlocksRenderer::renderTranslucent(
void BlocksRenderer::build( void BlocksRenderer::build(
const Chunk* chunk, const VoxelsRenderVolume& volume const Chunk* chunk, const VoxelsRenderVolume& volume
) { ) {
meshAABB = AABB(glm::vec3(CHUNK_W, CHUNK_H, CHUNK_D));
this->chunk = chunk; this->chunk = chunk;
this->voxelsBuffer = &volume; this->voxelsBuffer = &volume;
if (voxelsBuffer->pickBlockId( if (voxelsBuffer->pickBlockId(
@ -767,7 +778,8 @@ ChunkMeshData BlocksRenderer::createMesh() {
sizeof(ChunkVertex::ATTRIBUTES) / sizeof(VertexAttribute) sizeof(ChunkVertex::ATTRIBUTES) / sizeof(VertexAttribute)
) )
), ),
std::move(sortingMesh) std::move(sortingMesh),
std::move(meshAABB)
}; };
} }
@ -786,7 +798,7 @@ ChunkMesh BlocksRenderer::render(
IndexBufferData {indexBuffer.get(), indexCount}, IndexBufferData {indexBuffer.get(), indexCount},
IndexBufferData {denseIndexBuffer.get(), denseIndexCount}, IndexBufferData {denseIndexBuffer.get(), denseIndexCount},
} }
), std::move(sortingMesh), nullptr}; ), std::move(sortingMesh), nullptr, std::move(meshAABB)};
} }
size_t BlocksRenderer::getMemoryConsumption() const { size_t BlocksRenderer::getMemoryConsumption() const {

View file

@ -6,6 +6,7 @@
#include "voxels/Chunk.hpp" #include "voxels/Chunk.hpp"
#include "voxels/VoxelsVolume.hpp" #include "voxels/VoxelsVolume.hpp"
#include "maths/util.hpp" #include "maths/util.hpp"
#include "maths/aabb.hpp"
#include "commons.hpp" #include "commons.hpp"
#include "settings.hpp" #include "settings.hpp"
@ -56,6 +57,7 @@ private:
bool cancelled = false; bool cancelled = false;
bool densePass = false; bool densePass = false;
bool denseRender = false; bool denseRender = false;
AABB meshAABB {};
const Chunk* chunk = nullptr; const Chunk* chunk = nullptr;
const VoxelsRenderVolume* voxelsBuffer = nullptr; const VoxelsRenderVolume* voxelsBuffer = nullptr;

View file

@ -81,7 +81,9 @@ ChunksRenderer::ChunksRenderer(
meshes[result.key] = ChunkMesh { meshes[result.key] = ChunkMesh {
std::move(chunk), std::move(chunk),
std::move(meshData.sortingMesh), std::move(meshData.sortingMesh),
nullptr}; nullptr,
std::move(meshData.meshAABB)
};
} }
inwork.erase(result.key); inwork.erase(result.key);
}, },
@ -120,7 +122,7 @@ std::shared_ptr<VoxelsRenderVolume> ChunksRenderer::prepareVoxelsVolume(
return voxelsBuffer; return voxelsBuffer;
} }
const Mesh<ChunkVertex>* ChunksRenderer::render( const ChunkMesh* ChunksRenderer::render(
const std::shared_ptr<Chunk>& chunk, bool important, bool lowPriority const std::shared_ptr<Chunk>& chunk, bool important, bool lowPriority
) { ) {
glm::ivec2 key(chunk->x, chunk->z); glm::ivec2 key(chunk->x, chunk->z);
@ -130,7 +132,7 @@ const Mesh<ChunkVertex>* ChunksRenderer::render(
auto voxelsBuffer = prepareVoxelsVolume(*chunk); auto voxelsBuffer = prepareVoxelsVolume(*chunk);
mesh = renderer->render(chunk.get(), *voxelsBuffer); mesh = renderer->render(chunk.get(), *voxelsBuffer);
meshes[key] = std::move(mesh); meshes[key] = std::move(mesh);
return meshes[key].mesh.get(); return &meshes[key];
} }
if (inwork.find(key) != inwork.end() || if (inwork.find(key) != inwork.end() ||
((inwork.size() >= threadPool.getWorkersCount() || ((inwork.size() >= threadPool.getWorkersCount() ||
@ -158,7 +160,7 @@ void ChunksRenderer::clear() {
threadPool.clearQueue(); threadPool.clearQueue();
} }
const Mesh<ChunkVertex>* ChunksRenderer::getOrRender( const ChunkMesh* ChunksRenderer::getOrRender(
const std::shared_ptr<Chunk>& chunk, bool important, bool lowPriority const std::shared_ptr<Chunk>& chunk, bool important, bool lowPriority
) { ) {
auto found = meshes.find(glm::ivec2(chunk->x, chunk->z)); auto found = meshes.find(glm::ivec2(chunk->x, chunk->z));
@ -168,7 +170,7 @@ const Mesh<ChunkVertex>* ChunksRenderer::getOrRender(
if (chunk->flags.modified && chunk->flags.lighted) { if (chunk->flags.modified && chunk->flags.lighted) {
render(chunk, important, lowPriority); render(chunk, important, lowPriority);
} }
return found->second.mesh.get(); return &found->second;
} }
void ChunksRenderer::update() { void ChunksRenderer::update() {
@ -211,16 +213,23 @@ const Mesh<ChunkVertex>* ChunksRenderer::retrieveChunk(
chunk->updateHeights(); chunk->updateHeights();
} }
if (culling) { if (culling) {
glm::vec3 min(chunk->x * CHUNK_W, chunk->bottom, chunk->z * CHUNK_D); 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),
chunk->bottom,
chunk->z * CHUNK_D + std::min(0.0f, aabbMin.z)
);
glm::vec3 max( glm::vec3 max(
chunk->x * CHUNK_W + CHUNK_W, chunk->x * CHUNK_W + aabbMax.x,
chunk->top, chunk->top,
chunk->z * CHUNK_D + CHUNK_D chunk->z * CHUNK_D + aabbMax.z
); );
if (!frustum.isBoxVisible(min, max)) return nullptr; if (!frustum.isBoxVisible(min, max)) return nullptr;
} }
return mesh; return mesh->mesh.get();
} }
void ChunksRenderer::drawShadowsPass( void ChunksRenderer::drawShadowsPass(

View file

@ -71,13 +71,13 @@ public:
); );
virtual ~ChunksRenderer(); virtual ~ChunksRenderer();
const Mesh<ChunkVertex>* render( const ChunkMesh* render(
const std::shared_ptr<Chunk>& chunk, bool important, bool lowPriority const std::shared_ptr<Chunk>& chunk, bool important, bool lowPriority
); );
void unload(const Chunk* chunk); void unload(const Chunk* chunk);
void clear(); void clear();
const Mesh<ChunkVertex>* getOrRender( const ChunkMesh* getOrRender(
const std::shared_ptr<Chunk>& chunk, bool important, bool lowPriority const std::shared_ptr<Chunk>& chunk, bool important, bool lowPriority
); );

View file

@ -2,6 +2,7 @@
#include "constants.hpp" #include "constants.hpp"
#include "graphics/core/MeshData.hpp" #include "graphics/core/MeshData.hpp"
#include "maths/aabb.hpp"
#include "util/Buffer.hpp" #include "util/Buffer.hpp"
#include <vector> #include <vector>
@ -45,12 +46,14 @@ struct SortingMeshData {
struct ChunkMeshData { struct ChunkMeshData {
MeshData<ChunkVertex> mesh; MeshData<ChunkVertex> mesh;
SortingMeshData sortingMesh; SortingMeshData sortingMesh;
AABB meshAABB;
}; };
struct ChunkMesh { struct ChunkMesh {
std::unique_ptr<Mesh<ChunkVertex>> mesh; std::unique_ptr<Mesh<ChunkVertex>> mesh;
SortingMeshData sortingMeshData; SortingMeshData sortingMeshData;
std::unique_ptr<Mesh<ChunkVertex> > sortedMesh; std::unique_ptr<Mesh<ChunkVertex> > sortedMesh;
AABB meshAABB;
}; };
inline constexpr int VOXELS_BUFFER_PADDING = 2; inline constexpr int VOXELS_BUFFER_PADDING = 2;