From 21d135fb0ba937ec0e03b5b88b92501d2c5bacc0 Mon Sep 17 00:00:00 2001 From: Relintai Date: Sun, 28 Jun 2020 20:04:40 +0200 Subject: [PATCH] Now chunk can only have one prop mesher. --- world/blocky/voxel_chunk_blocky.cpp | 1 + world/cubic/voxel_chunk_cubic.cpp | 1 + world/default/voxel_chunk_default.cpp | 53 +++++++------------ .../voxel_chunk_marching_cubes.cpp | 1 + world/voxel_chunk.cpp | 31 +++-------- world/voxel_chunk.h | 9 ++-- 6 files changed, 31 insertions(+), 65 deletions(-) diff --git a/world/blocky/voxel_chunk_blocky.cpp b/world/blocky/voxel_chunk_blocky.cpp index 214abbe..e0e16ee 100644 --- a/world/blocky/voxel_chunk_blocky.cpp +++ b/world/blocky/voxel_chunk_blocky.cpp @@ -38,6 +38,7 @@ void VoxelChunkBlocky::_setup_channels() { } void VoxelChunkBlocky::_create_meshers() { + set_prop_mesher(Ref(memnew(VoxelMesherBlocky))); add_mesher(Ref(memnew(VoxelMesherBlocky()))); add_liquid_mesher(Ref(memnew(VoxelMesherLiquidBlocky()))); diff --git a/world/cubic/voxel_chunk_cubic.cpp b/world/cubic/voxel_chunk_cubic.cpp index d5953b5..698f737 100644 --- a/world/cubic/voxel_chunk_cubic.cpp +++ b/world/cubic/voxel_chunk_cubic.cpp @@ -37,6 +37,7 @@ void VoxelChunkCubic::_setup_channels() { } void VoxelChunkCubic::_create_meshers() { + set_prop_mesher(Ref(memnew(VoxelMesherCubic))); add_mesher(Ref(memnew(VoxelMesherCubic()))); //add_liquid_mesher(Ref(memnew(VoxelMesherLiquiCubic()))); diff --git a/world/default/voxel_chunk_default.cpp b/world/default/voxel_chunk_default.cpp index d1cf372..4bf7143 100644 --- a/world/default/voxel_chunk_default.cpp +++ b/world/default/voxel_chunk_default.cpp @@ -1146,13 +1146,9 @@ void VoxelChunkDefault::_build_phase(int phase) { mesher->reset(); } - for (int i = 0; i < _prop_meshers.size(); ++i) { - Ref mesher = _prop_meshers.get(i); - - ERR_CONTINUE(!mesher.is_valid()); - - mesher->reset(); - mesher->set_library(_library); + if (get_prop_mesher().is_valid()) { + get_prop_mesher()->reset(); + get_prop_mesher()->set_library(_library); } next_phase(); @@ -1469,50 +1465,33 @@ void VoxelChunkDefault::_build_phase(int phase) { } #ifdef MESH_DATA_RESOURCE_PRESENT case BUILD_PHASE_MESH_DATA_RESOURCES: { + if (!get_prop_mesher().is_valid()) { + next_phase(); + return; + } + if (get_mesh_data_resource_count() == 0) { next_phase(); return; } - for (int i = 0; i < _meshers.size(); ++i) { - Ref m = _meshers.get(i); - - ERR_CONTINUE(!m.is_valid()); - - m->add_mesh_data_resource_transform(get_mesh_data_resource(i), get_mesh_data_resource_transform(i), get_mesh_data_resource_uv_rect(i)); + for (int i = 0; i < get_mesh_data_resource_count(); ++i) { + get_prop_mesher()->add_mesh_data_resource_transform(get_mesh_data_resource(i), get_mesh_data_resource_transform(i), get_mesh_data_resource_uv_rect(i)); } - Ref mesher; - for (int i = 0; i < _meshers.size(); ++i) { - Ref m = _meshers.get(i); - - ERR_CONTINUE(!m.is_valid()); - - if (!mesher.is_valid()) { - mesher = m; - // mesher->set_material(get_library()->get_material(0)); - continue; - } - - // mesher->set_material(get_library()->get_material(0)); - mesher->add_mesher(m); - } - - ERR_FAIL_COND(!mesher.is_valid()); - - if (mesher->get_vertex_count() == 0) { + if (get_prop_mesher()->get_vertex_count() == 0) { next_phase(); return; } if ((_build_flags & VoxelChunkDefault::BUILD_FLAG_USE_LIGHTING) != 0) { - mesher->bake_colors(this); + get_prop_mesher()->bake_colors(this); } - if (mesher->get_vertex_count() != 0) { + if (get_prop_mesher()->get_vertex_count() != 0) { RID mesh_rid = get_mesh_rid_index(MESH_INDEX_PROP, MESH_TYPE_INDEX_MESH, 0); - Array temp_mesh_arr = mesher->build_mesh(); + Array temp_mesh_arr = get_prop_mesher()->build_mesh(); if (mesh_rid == RID()) { if ((_build_flags & BUILD_FLAG_CREATE_LODS) != 0) @@ -1681,6 +1660,10 @@ void VoxelChunkDefault::_build_phase_physics_process(int phase) { } void VoxelChunkDefault::_create_meshers() { + if (!_prop_mesher.is_valid()) { + _prop_mesher = Ref(memnew(VoxelMesherDefault)); + } + for (int i = 0; i < _meshers.size(); ++i) { Ref mesher = _meshers.get(i); diff --git a/world/marching_cubes/voxel_chunk_marching_cubes.cpp b/world/marching_cubes/voxel_chunk_marching_cubes.cpp index 29dc035..c9fb44c 100644 --- a/world/marching_cubes/voxel_chunk_marching_cubes.cpp +++ b/world/marching_cubes/voxel_chunk_marching_cubes.cpp @@ -37,6 +37,7 @@ void VoxelChunkMarchingCubes::_setup_channels() { } void VoxelChunkMarchingCubes::_create_meshers() { + set_prop_mesher(Ref(memnew(VoxelMesherMarchingCubes))); add_mesher(Ref(memnew(VoxelMesherMarchingCubes()))); //add_liquid_mesher(Ref(memnew(VoxelMesherLiquidMarchingCubes()))); diff --git a/world/voxel_chunk.cpp b/world/voxel_chunk.cpp index 8dfb877..f79d600 100644 --- a/world/voxel_chunk.cpp +++ b/world/voxel_chunk.cpp @@ -251,26 +251,11 @@ int VoxelChunk::get_liquid_mesher_count() const { return _liquid_meshers.size(); } -Ref VoxelChunk::get_prop_mesher(int index) const { - ERR_FAIL_INDEX_V(index, _prop_meshers.size(), Ref()); - - return _prop_meshers.get(index); +Ref VoxelChunk::get_prop_mesher() const { + return _prop_mesher; } -void VoxelChunk::set_prop_mesher(int index, const Ref &mesher) { - ERR_FAIL_INDEX(index, _prop_meshers.size()); - - _prop_meshers.set(index, mesher); -} -void VoxelChunk::remove_prop_mesher(const int index) { - ERR_FAIL_INDEX(index, _prop_meshers.size()); - - _prop_meshers.remove(index); -} -void VoxelChunk::add_prop_mesher(const Ref &mesher) { - _prop_meshers.push_back(mesher); -} -int VoxelChunk::get_prop_mesher_count() const { - return _prop_meshers.size(); +void VoxelChunk::set_prop_mesher(const Ref &mesher) { + _prop_mesher = mesher; } //Voxel Data @@ -1100,11 +1085,9 @@ void VoxelChunk::_bind_methods() { ClassDB::bind_method(D_METHOD("add_liquid_mesher", "mesher"), &VoxelChunk::add_liquid_mesher); ClassDB::bind_method(D_METHOD("get_liquid_mesher_count"), &VoxelChunk::get_liquid_mesher_count); - ClassDB::bind_method(D_METHOD("get_prop_mesher", "index"), &VoxelChunk::get_prop_mesher); - ClassDB::bind_method(D_METHOD("set_prop_mesher", "index", "mesher"), &VoxelChunk::set_prop_mesher); - ClassDB::bind_method(D_METHOD("remove_prop_mesher", "index"), &VoxelChunk::remove_prop_mesher); - ClassDB::bind_method(D_METHOD("add_prop_mesher", "mesher"), &VoxelChunk::add_prop_mesher); - ClassDB::bind_method(D_METHOD("get_prop_mesher_count"), &VoxelChunk::get_prop_mesher_count); + ClassDB::bind_method(D_METHOD("get_prop_mesher"), &VoxelChunk::get_prop_mesher); + ClassDB::bind_method(D_METHOD("set_prop_mesher", "mesher"), &VoxelChunk::set_prop_mesher); + ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "prop_mesher", PROPERTY_HINT_RESOURCE_TYPE, "VoxelMesher", 0), "set_prop_mesher", "get_prop_mesher"); ClassDB::bind_method(D_METHOD("get_voxel_world"), &VoxelChunk::get_voxel_world); ClassDB::bind_method(D_METHOD("set_voxel_world", "world"), &VoxelChunk::set_voxel_world_bind); diff --git a/world/voxel_chunk.h b/world/voxel_chunk.h index 49e10da..b9af458 100644 --- a/world/voxel_chunk.h +++ b/world/voxel_chunk.h @@ -146,11 +146,8 @@ public: int get_liquid_mesher_count() const; //Prop Meshers - Ref get_prop_mesher(const int index) const; - void set_prop_mesher(const int index, const Ref &mesher); - void remove_prop_mesher(const int index); - void add_prop_mesher(const Ref &mesher); - int get_prop_mesher_count() const; + Ref get_prop_mesher() const; + void set_prop_mesher(const Ref &mesher); //Channels void setup_channels(); @@ -299,7 +296,7 @@ protected: Ref _library; Vector > _meshers; Vector > _liquid_meshers; - Vector > _prop_meshers; + Ref _prop_mesher; //mergeable props Vector > _props;