From eb1d55f05b3d8245b0b851b1aeee1b6b6740e279 Mon Sep 17 00:00:00 2001 From: Relintai Date: Sat, 3 Oct 2020 17:18:43 +0200 Subject: [PATCH] More work on the logic, and api fixes. --- world/default/voxel_chunk_default.cpp | 14 ++------ world/default/voxel_chunk_default.h | 5 +-- world/voxel_chunk.cpp | 47 +++++++++++++++++++++------ world/voxel_chunk.h | 12 ++++--- world/voxel_world.cpp | 2 ++ 5 files changed, 50 insertions(+), 30 deletions(-) diff --git a/world/default/voxel_chunk_default.cpp b/world/default/voxel_chunk_default.cpp index e837610..c462dd8 100644 --- a/world/default/voxel_chunk_default.cpp +++ b/world/default/voxel_chunk_default.cpp @@ -833,16 +833,7 @@ void VoxelChunkDefault::free_chunk() { free_rids(); } -void VoxelChunkDefault::build() { - if (get_is_generating()) { - _queued_generation = true; - return; - } - - next_job(); -} - -void VoxelChunkDefault::finalize_build() { +void VoxelChunkDefault::_finalize_build() { ERR_FAIL_COND(!_library.is_valid()); #if TOOLS_ENABLED @@ -858,7 +849,6 @@ void VoxelChunkDefault::finalize_build() { VoxelChunkDefault::VoxelChunkDefault() { _abort_build = false; - _queued_generation = false; _enabled = true; @@ -978,7 +968,7 @@ void VoxelChunkDefault::_bind_methods() { ClassDB::bind_method(D_METHOD("_world_light_added", "light"), &VoxelChunkDefault::_world_light_added); ClassDB::bind_method(D_METHOD("_world_light_removed", "light"), &VoxelChunkDefault::_world_light_removed); - //ClassDB::bind_method(D_METHOD("get_job"), &VoxelChunkDefault::get_job); + ClassDB::bind_method(D_METHOD("_finalize_build"), &VoxelChunkDefault::_finalize_build); BIND_ENUM_CONSTANT(DEFAULT_CHANNEL_TYPE); BIND_ENUM_CONSTANT(DEFAULT_CHANNEL_ISOLEVEL); diff --git a/world/default/voxel_chunk_default.h b/world/default/voxel_chunk_default.h index 6ae1a35..0b96ee0 100644 --- a/world/default/voxel_chunk_default.h +++ b/world/default/voxel_chunk_default.h @@ -174,8 +174,7 @@ public: bool get_build_step_in_progress() const; void set_build_step_in_progress(const bool value); - void build(); - void finalize_build(); + void _finalize_build(); VoxelChunkDefault(); ~VoxelChunkDefault(); @@ -200,8 +199,6 @@ protected: int _build_flags; - bool _queued_generation; - bool _enabled; bool _lights_dirty; diff --git a/world/voxel_chunk.cpp b/world/voxel_chunk.cpp index 54f581d..c1f1ff5 100644 --- a/world/voxel_chunk.cpp +++ b/world/voxel_chunk.cpp @@ -216,12 +216,12 @@ void VoxelChunk::set_voxel_world_bind(Node *world) { _voxel_world = Object::cast_to(world); } -Ref VoxelChunk::get_job(int index) const { - ERR_FAIL_INDEX_V(index, _jobs.size(), Ref()); +Ref VoxelChunk::get_job(int index) const { + ERR_FAIL_INDEX_V(index, _jobs.size(), Ref()); return _jobs.get(index); } -void VoxelChunk::set_job(int index, const Ref &job) { +void VoxelChunk::set_job(int index, const Ref &job) { ERR_FAIL_INDEX(index, _jobs.size()); _jobs.set(index, job); @@ -231,7 +231,7 @@ void VoxelChunk::remove_job(const int index) { _jobs.remove(index); } -void VoxelChunk::add_job(const Ref &job) { +void VoxelChunk::add_job(const Ref &job) { _jobs.push_back(job); } int VoxelChunk::get_job_count() const { @@ -257,11 +257,12 @@ void VoxelChunk::next_job() { next_job(); } - if (j->get_build_phase_type() == VoxelJob::BUILD_PHASE_TYPE_NORMAL) { - j->set_complete(false); + j->set_complete(false); + if (j->get_build_phase_type() == VoxelJob::BUILD_PHASE_TYPE_NORMAL) { #if THREAD_POOL_PRESENT - ThreadPool::get_singleton()->add_job(j); + //ThreadPool::get_singleton()->add_job(j); + j->execute(); #else j->execute(); #endif @@ -566,13 +567,23 @@ void VoxelChunk::create_meshers() { call("_create_meshers"); } -void VoxelChunk::build(const bool immediate) { +void VoxelChunk::build() { ERR_FAIL_COND(!INSTANCE_VALIDATE(get_voxel_world())); ERR_FAIL_COND(!get_voxel_world()->is_inside_tree()); ERR_FAIL_COND(!is_in_tree()); - ERR_FAIL_COND_MSG(!has_method("_build"), "VoxelChunk: _build(immediate : bool) is missing! Please implement it!"); - call("_build", immediate); + call("_build"); +} + +void VoxelChunk::_build() { + if (get_is_generating()) { + _queued_generation = true; + return; + } + + _is_generating = true; + + next_job(); } void VoxelChunk::clear() { @@ -581,6 +592,12 @@ void VoxelChunk::clear() { call("_clear"); } +void VoxelChunk::finalize_build() { + if (has_method("_finalize_build")) { + call("_finalize_build"); + } +} + void VoxelChunk::bake_lights() { if (has_method("_bake_lights")) call("_bake_lights"); @@ -952,6 +969,8 @@ VoxelChunk::VoxelChunk() { _margin_end = 0; _current_job = 0; + + _queued_generation = false; } VoxelChunk::~VoxelChunk() { @@ -1115,6 +1134,8 @@ void VoxelChunk::_bind_methods() { BIND_VMETHOD(MethodInfo("_generation_process", PropertyInfo(Variant::REAL, "delta"))); BIND_VMETHOD(MethodInfo("_generation_physics_process", PropertyInfo(Variant::REAL, "delta"))); + BIND_VMETHOD(MethodInfo("_finalize_build")); + ClassDB::bind_method(D_METHOD("enter_tree"), &VoxelChunk::enter_tree); ClassDB::bind_method(D_METHOD("exit_tree"), &VoxelChunk::exit_tree); ClassDB::bind_method(D_METHOD("process", "delta"), &VoxelChunk::process); @@ -1127,6 +1148,8 @@ void VoxelChunk::_bind_methods() { ClassDB::bind_method(D_METHOD("generation_process", "delta"), &VoxelChunk::generation_process); ClassDB::bind_method(D_METHOD("generation_physics_process", "delta"), &VoxelChunk::generation_physics_process); + ClassDB::bind_method(D_METHOD("finalize_build"), &VoxelChunk::finalize_build); + ClassDB::bind_method(D_METHOD("get_process"), &VoxelChunk::get_process); ClassDB::bind_method(D_METHOD("set_process", "value"), &VoxelChunk::set_process); @@ -1317,6 +1340,10 @@ void VoxelChunk::_bind_methods() { ClassDB::bind_method(D_METHOD("create_meshers"), &VoxelChunk::create_meshers); + BIND_VMETHOD(MethodInfo("_build")); + ClassDB::bind_method(D_METHOD("build"), &VoxelChunk::build); + ClassDB::bind_method(D_METHOD("_build"), &VoxelChunk::_build); + ClassDB::bind_method(D_METHOD("get_global_transform"), &VoxelChunk::get_global_transform); ClassDB::bind_method(D_METHOD("to_local", "global"), &VoxelChunk::to_local); ClassDB::bind_method(D_METHOD("to_global", "local"), &VoxelChunk::to_global); diff --git a/world/voxel_chunk.h b/world/voxel_chunk.h index 00564dd..4918533 100644 --- a/world/voxel_chunk.h +++ b/world/voxel_chunk.h @@ -141,10 +141,10 @@ public: void set_voxel_world_bind(Node *world); //Jobs - Ref get_job(const int index) const; - void set_job(const int index, const Ref &job); + Ref get_job(const int index) const; + void set_job(const int index, const Ref &job); void remove_job(const int index); - void add_job(const Ref &job); + void add_job(const Ref &job); int get_job_count() const; int get_current_job_index(); @@ -185,8 +185,11 @@ public: //Meshing void create_meshers(); - void build(const bool immediate = false); + void build(); void clear(); + void finalize_build(); + + void _build(); //light Baking void bake_lights(); @@ -364,6 +367,7 @@ protected: Transform _transform; bool _abort_build; + bool _queued_generation; }; #endif diff --git a/world/voxel_world.cpp b/world/voxel_world.cpp index 558c991..c9a27a1 100644 --- a/world/voxel_world.cpp +++ b/world/voxel_world.cpp @@ -420,6 +420,8 @@ Ref VoxelWorld::_create_chunk(const int x, const int y, const int z, chunk->set_size(_chunk_size_x, _chunk_size_y, _chunk_size_z, _data_margin_start, _data_margin_end); //chunk->set_translation(Vector3(x * _chunk_size_x * _voxel_scale, y * _chunk_size_y * _voxel_scale, z * _chunk_size_z * _voxel_scale)); + chunk->create_meshers(); + add_chunk(chunk, x, y, z); add_to_generation_queue(chunk);