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; Lightmap* lightmap;
bool modified = true; bool modified = true;
bool ready = false; bool ready = false;
bool accepted = false; bool loaded = false;
bool generated = false;
int references = 1; int references = 1;
Chunk(int x, int y, int z); Chunk(int x, int y, int z);
~Chunk(); ~Chunk();

View File

@ -73,7 +73,7 @@ bool ChunksController::loadVisible(WorldFiles* worldFiles){
return false; return false;
chunk = new Chunk(nearX+ox,nearY+oy,nearZ+oz); chunk = new Chunk(nearX+ox,nearY+oy,nearZ+oz);
if (worldFiles->getChunk(chunk->x, chunk->z, (char*)chunk->voxels)) if (worldFiles->getChunk(chunk->x, chunk->z, (char*)chunk->voxels))
chunk->generated = true; chunk->loaded = true;
chunks->chunks[index] = chunk; chunks->chunks[index] = chunk;
@ -99,7 +99,7 @@ bool ChunksController::loadVisible(WorldFiles* worldFiles){
oz += 1; oz += 1;
closes[(oy * 3 + oz) * 3 + ox] = other; closes[(oy * 3 + oz) * 3 + ox] = other;
} }
freeLoader->perform(chunk, (const Chunk**)closes); freeLoader->perform(chunk, (Chunk**)closes);
return true; return true;
} }
@ -144,29 +144,6 @@ bool ChunksController::_buildMeshes(VoxelRenderer* renderer, int tick) {
Chunk* chunk = chunks->chunks[index]; Chunk* chunk = chunks->chunks[index];
if (chunk == nullptr){ 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; return false;
} }
Mesh* mesh = chunks->meshes[index]; Mesh* mesh = chunks->meshes[index];

View File

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

View File

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