Fixed lighting artifacts, improved chunks loading speed

- No more dark lines while loading chunks
- Less lighting recalculations
- Less chunks mesh rebuilding
This commit is contained in:
MihailRis 2022-03-06 01:20:51 +03:00 committed by GitHub
parent 10125808c0
commit 30c2d9889e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 19 additions and 42 deletions

View File

@ -16,8 +16,7 @@ public:
Lightmap* lightmap;
bool modified = true;
bool ready = false;
bool accepted = false;
bool generated = false;
bool loaded = false;
int references = 1;
Chunk(int x, int y, int z);
~Chunk();

View File

@ -73,7 +73,7 @@ bool ChunksController::loadVisible(WorldFiles* worldFiles){
return false;
chunk = new Chunk(nearX+ox,nearY+oy,nearZ+oz);
if (worldFiles->getChunk(chunk->x, chunk->z, (char*)chunk->voxels))
chunk->generated = true;
chunk->loaded = true;
chunks->chunks[index] = chunk;
@ -99,7 +99,7 @@ bool ChunksController::loadVisible(WorldFiles* worldFiles){
oz += 1;
closes[(oy * 3 + oz) * 3 + ox] = other;
}
freeLoader->perform(chunk, (const Chunk**)closes);
freeLoader->perform(chunk, (Chunk**)closes);
return true;
}
@ -144,29 +144,6 @@ bool ChunksController::_buildMeshes(VoxelRenderer* renderer, int tick) {
Chunk* chunk = chunks->chunks[index];
if (chunk == nullptr){
for (int y = 0; y < h; y++){
for (int z = 1; z < d-1; z++){
for (int x = 1; x < w-1; x++){
int index = (y * d + z) * w + x;
chunk = chunks->chunks[index];
if (chunk != nullptr && chunk->ready && !chunk->accepted){
int lx = x - w / 2;
int ly = y - h / 2;
int lz = z - d / 2;
int distance = (lx * lx + ly * ly + lz * lz);
lighting->onChunkLoaded(chunk->x, chunk->y, chunk->z, false);
for (int i = 0; i < chunks->volume; i++){
Chunk* other = chunks->chunks[i];
if (other)
other->modified = true;
}
chunk->accepted = true;
std::cout << "1: built mesh for " << chunk << std::endl;
return true;
}
}
}
}
return false;
}
Mesh* mesh = chunks->meshes[index];

View File

@ -8,6 +8,8 @@
#include <iostream>
#define CLOSES_C 27
void ChunksLoader::_thread(){
Chunks chunks(3,3,3, -1,-1,-1);
Lighting lighting(&chunks);
@ -17,25 +19,22 @@ void ChunksLoader::_thread(){
continue;
}
Chunk* chunk = current;
chunk->incref();
for (size_t i = 0; i < 27; i++){
chunks._setOffset(chunk->x-1, chunk->y-1, chunk->z-1);
for (size_t i = 0; i < CLOSES_C; i++){
Chunk* other = closes[i];
if (other){
other->incref();
chunks.putChunk(other);
}
}
chunks._setOffset(chunk->x-1, chunk->y-1, chunk->z-1);
if (!chunk->generated){
if (!chunk->loaded){
WorldGenerator::generate(chunk->voxels, chunk->x, chunk->y, chunk->z);
}
chunks.putChunk(chunk);
lighting.onChunkLoaded(chunk->x, chunk->y, chunk->z, true);
chunks.clear(false);
for (int i = 0; i < 27; i++){
for (int i = 0; i < CLOSES_C; i++){
Chunk* other = closes[i];
if (other)
other->decref();
@ -43,24 +42,26 @@ void ChunksLoader::_thread(){
chunk->ready = true;
current = nullptr;
chunk->decref();
//std::cout << "LOADER: success" << std::endl;
}
}
void ChunksLoader::perform(Chunk* chunk, const Chunk** cs){
void ChunksLoader::perform(Chunk* chunk, Chunk** closes_passed){
if (isBusy()){
std::cerr << "performing while busy" << std::endl;
return;
}
chunk->incref();
if (closes == nullptr){
closes = new Chunk*[27];
closes = new Chunk*[CLOSES_C];
}
for (int i = 0; i < 27; i++){
const Chunk* other = cs[i];
for (int i = 0; i < CLOSES_C; i++){
Chunk* other = closes_passed[i];
if (other == nullptr)
closes[i] = nullptr;
else
closes[i] = (Chunk*)other;//->clone();
else {
other->incref();
closes[i] = other;
}
}
current = chunk;
}

View File

@ -26,7 +26,7 @@ public:
return current != nullptr;
}
void perform(Chunk* chunk, const Chunk** closes);
void perform(Chunk* chunk, Chunk** closes_passed);
void stop(){
working = false;