diff --git a/README.md b/README.md index 84721c2..bb38e32 100644 --- a/README.md +++ b/README.md @@ -123,11 +123,16 @@ Since properly initializing a chunk usually takes quite a few steps that you pro parameter was added. This means you can just call the super `_create_chunk` methods, and you won't need to worry about your chunk getting overridden. Like: +Note that `_create_chunk` is also responsible for initializing chunks if you have them stored inside a scene. +This is done by `setup_chunk(shunk)` in `VoxelWorld`. + ``` func _create_chunk(x : int, y : int, z : int, chunk : VoxelChunk) -> VoxelChunk: if !chunk: chunk = MyChunk.new() + # We need to check whether or not we need to initialize jobs + if chunk.get_job_count() == 0: # Setup a blocky (minecratf like) mesher job var tj : VoxelTerrarinJob = VoxelTerrarinJob.new() diff --git a/world/blocky/voxel_world_blocky.cpp b/world/blocky/voxel_world_blocky.cpp index 2f70a9f..bee81da 100644 --- a/world/blocky/voxel_world_blocky.cpp +++ b/world/blocky/voxel_world_blocky.cpp @@ -34,7 +34,9 @@ Ref VoxelWorldBlocky::_create_chunk(int x, int y, int z, Ref(memnew(VoxelChunkBlocky)); + } + if (chunk->get_job_count() == 0) { Ref tj; tj.instance(); diff --git a/world/cubic/voxel_world_cubic.cpp b/world/cubic/voxel_world_cubic.cpp index 05d9eb7..2256573 100644 --- a/world/cubic/voxel_world_cubic.cpp +++ b/world/cubic/voxel_world_cubic.cpp @@ -33,7 +33,9 @@ Ref VoxelWorldCubic::_create_chunk(int x, int y, int z, Ref(memnew(VoxelChunkCubic)); + } + if (chunk->get_job_count() == 0) { Ref tj; tj.instance(); diff --git a/world/default/voxel_world_default.cpp b/world/default/voxel_world_default.cpp index ac882df..5252a99 100644 --- a/world/default/voxel_world_default.cpp +++ b/world/default/voxel_world_default.cpp @@ -175,7 +175,9 @@ void VoxelWorldDefault::_update_lods() { Ref VoxelWorldDefault::_create_chunk(int x, int y, int z, Ref chunk) { if (!chunk.is_valid()) { chunk = Ref(memnew(VoxelChunkDefault)); + } + if (chunk->get_job_count() == 0) { Ref tj; tj.instance(); diff --git a/world/marching_cubes/voxel_world_marching_cubes.cpp b/world/marching_cubes/voxel_world_marching_cubes.cpp index 577ec78..bfe3f65 100644 --- a/world/marching_cubes/voxel_world_marching_cubes.cpp +++ b/world/marching_cubes/voxel_world_marching_cubes.cpp @@ -32,7 +32,9 @@ SOFTWARE. Ref VoxelWorldMarchingCubes::_create_chunk(int x, int y, int z, Ref chunk) { if (!chunk.is_valid()) { chunk = Ref(memnew(VoxelChunkMarchingCubes)); + } + if (chunk->get_job_count() == 0) { Ref tj; tj.instance(); diff --git a/world/voxel_world.cpp b/world/voxel_world.cpp index e0b67c6..f9463fd 100644 --- a/world/voxel_world.cpp +++ b/world/voxel_world.cpp @@ -262,14 +262,17 @@ void VoxelWorld::add_chunk(Ref chunk, const int x, const int y, cons IntPos pos(x, y, z); - ERR_FAIL_COND(_chunks.has(pos)); + //ERR_FAIL_COND(_chunks.has(pos)); chunk->set_voxel_world(this); chunk->set_position(x, y, z); chunk->world_transform_changed(); - _chunks.set(pos, chunk); - _chunks_vector.push_back(chunk); + if (!_chunks.has(pos)) + _chunks.set(pos, chunk); + + if (_chunks_vector.find(chunk) == -1) + _chunks_vector.push_back(chunk); if (is_inside_tree()) chunk->enter_tree(); @@ -397,15 +400,24 @@ Ref VoxelWorld::get_or_create_chunk(int x, int y, int z) { Ref VoxelWorld::create_chunk(const int x, const int y, const int z) { Ref c = call("_create_chunk", x, y, z, Ref()); + add_to_generation_queue(c); + return c; } + +void VoxelWorld::setup_chunk(Ref chunk) { + ERR_FAIL_COND(!chunk.is_valid()); + + call("_create_chunk", chunk->get_position_x(), chunk->get_position_y(), chunk->get_position_z(), chunk); +} + Ref VoxelWorld::_create_chunk(const int x, const int y, const int z, Ref chunk) { if (!chunk.is_valid()) { chunk.instance(); - - //no meshers here } + //no meshers here + ERR_FAIL_COND_V(!chunk.is_valid(), NULL); chunk->set_name("Chunk[" + String::num(x) + "," + String::num(y) + "," + String::num(z) + "]"); @@ -424,8 +436,6 @@ Ref VoxelWorld::_create_chunk(const int x, const int y, const int z, add_chunk(chunk, x, y, z); - add_to_generation_queue(chunk); - return chunk; } @@ -907,14 +917,9 @@ void VoxelWorld::_notification(int p_what) { Ref chunk = _chunks_vector[i]; if (chunk.is_valid()) { - IntPos pos(chunk->get_position_x(), chunk->get_position_y(), chunk->get_position_z()); + setup_chunk(chunk); - chunk->set_voxel_world(this); - chunk->world_transform_changed(); - - _chunks.set(pos, chunk); - - chunk->enter_tree(); + chunk->build(); } } } break; @@ -1136,6 +1141,7 @@ void VoxelWorld::_bind_methods() { ClassDB::bind_method(D_METHOD("get_or_create_chunk", "x", "y", "z"), &VoxelWorld::get_or_create_chunk); ClassDB::bind_method(D_METHOD("create_chunk", "x", "y", "z"), &VoxelWorld::create_chunk); ClassDB::bind_method(D_METHOD("_create_chunk", "x", "y", "z", "chunk"), &VoxelWorld::_create_chunk); + ClassDB::bind_method(D_METHOD("setup_chunk", "chunk"), &VoxelWorld::setup_chunk); ClassDB::bind_method(D_METHOD("_generate_chunk", "chunk"), &VoxelWorld::_generate_chunk); diff --git a/world/voxel_world.h b/world/voxel_world.h index 0bd2de6..754b321 100644 --- a/world/voxel_world.h +++ b/world/voxel_world.h @@ -149,6 +149,7 @@ public: Ref get_or_create_chunk(const int x, const int y, const int z); Ref create_chunk(const int x, const int y, const int z); + void setup_chunk(Ref chunk); void generate_chunk(Ref chunk);