diff --git a/world/blocky/voxel_chunk_blocky.cpp b/world/blocky/voxel_chunk_blocky.cpp index bdf8b1b..ddda70d 100644 --- a/world/blocky/voxel_chunk_blocky.cpp +++ b/world/blocky/voxel_chunk_blocky.cpp @@ -23,6 +23,7 @@ SOFTWARE. #include "voxel_chunk_blocky.h" #include "../../meshers/blocky/voxel_mesher_blocky.h" +#include "../../meshers/blocky/voxel_mesher_liquid_blocky.h" #include "core/version.h" @@ -42,6 +43,7 @@ void VoxelChunkBlocky::_setup_channels() { void VoxelChunkBlocky::_create_meshers() { add_mesher(Ref(memnew(VoxelMesherBlocky()))); + add_liquid_mesher(Ref(memnew(VoxelMesherLiquidBlocky()))); for (int i = 0; i < _meshers.size(); ++i) { Ref mesher = _meshers.get(i); @@ -57,6 +59,21 @@ void VoxelChunkBlocky::_create_meshers() { md->set_build_flags(get_build_flags()); } } + + for (int i = 0; i < _liquid_meshers.size(); ++i) { + Ref mesher = _liquid_meshers.get(i); + + ERR_CONTINUE(!mesher.is_valid()); + + mesher->set_lod_size(get_lod_size()); + mesher->set_voxel_scale(get_voxel_scale()); + + Ref md = mesher; + + if (md.is_valid()) { + md->set_build_flags(get_build_flags()); + } + } } void VoxelChunkBlocky::_bind_methods() { diff --git a/world/default/voxel_chunk_default.cpp b/world/default/voxel_chunk_default.cpp index 500c28e..3c99ca9 100644 --- a/world/default/voxel_chunk_default.cpp +++ b/world/default/voxel_chunk_default.cpp @@ -1068,6 +1068,15 @@ void VoxelChunkDefault::_build_phase(int phase) { mesher->reset(); } + for (int i = 0; i < _liquid_meshers.size(); ++i) { + Ref mesher = _liquid_meshers.get(i); + + ERR_CONTINUE(!mesher.is_valid()); + + mesher->set_library(_library); + mesher->reset(); + } + next_phase(); return; @@ -1081,6 +1090,14 @@ void VoxelChunkDefault::_build_phase(int phase) { mesher->add_chunk(this); } + for (int i = 0; i < _liquid_meshers.size(); ++i) { + Ref mesher = _liquid_meshers.get(i); + + ERR_CONTINUE(!mesher.is_valid()); + + mesher->add_chunk(this); + } + next_phase(); return; @@ -1099,7 +1116,15 @@ void VoxelChunkDefault::_build_phase(int phase) { temp_arr_collider.append_array(mesher->build_collider()); } - if (temp_arr_collider.size() == 0) { + for (int i = 0; i < _liquid_meshers.size(); ++i) { + Ref mesher = _liquid_meshers.get(i); + + ERR_CONTINUE(!mesher.is_valid()); + + temp_arr_collider_liquid.append_array(mesher->build_collider()); + } + + if (temp_arr_collider.size() == 0 && temp_arr_collider_liquid.size() == 0) { next_phase(); return; } @@ -1109,13 +1134,25 @@ void VoxelChunkDefault::_build_phase(int phase) { return; } - if (!has_meshes(MESH_INDEX_TERRARIN, MESH_TYPE_INDEX_BODY)) { - create_colliders(MESH_INDEX_TERRARIN); + if (temp_arr_collider.size() != 0) { + if (!has_meshes(MESH_INDEX_TERRARIN, MESH_TYPE_INDEX_BODY)) { + create_colliders(MESH_INDEX_TERRARIN); + } + + PhysicsServer::get_singleton()->shape_set_data(get_mesh_rid(MESH_INDEX_TERRARIN, MESH_TYPE_INDEX_SHAPE), temp_arr_collider); + + //temp_arr_collider.resize(0); } - PhysicsServer::get_singleton()->shape_set_data(get_mesh_rid(MESH_INDEX_TERRARIN, MESH_TYPE_INDEX_SHAPE), temp_arr_collider); + if (temp_arr_collider_liquid.size() != 0) { + if (!has_meshes(MESH_INDEX_LIQUID, MESH_TYPE_INDEX_BODY)) { + create_colliders(MESH_INDEX_LIQUID); + } - //temp_arr_collider.resize(0); + PhysicsServer::get_singleton()->shape_set_data(get_mesh_rid(MESH_INDEX_LIQUID, MESH_TYPE_INDEX_SHAPE), temp_arr_collider_liquid); + + //temp_arr_collider_liquid.resize(0); + } next_phase(); @@ -1130,6 +1167,14 @@ void VoxelChunkDefault::_build_phase(int phase) { mesher->set_library(_library); } + for (int i = 0; i < _liquid_meshers.size(); ++i) { + Ref mesher = _liquid_meshers.get(i); + + ERR_CONTINUE(!mesher.is_valid()); + + mesher->set_library(_library); + } + if ((_build_flags & VoxelChunkDefault::BUILD_FLAG_USE_LIGHTING) != 0) { for (int i = 0; i < _meshers.size(); ++i) { Ref mesher = _meshers.get(i); @@ -1138,6 +1183,14 @@ void VoxelChunkDefault::_build_phase(int phase) { mesher->bake_colors(this); } + + for (int i = 0; i < _liquid_meshers.size(); ++i) { + Ref mesher = _liquid_meshers.get(i); + + ERR_CONTINUE(!mesher.is_valid()); + + mesher->bake_colors(this); + } } Ref mesher; @@ -1158,96 +1211,134 @@ void VoxelChunkDefault::_build_phase(int phase) { ERR_FAIL_COND(!mesher.is_valid()); - if (mesher->get_vertex_count() == 0) { + Ref liquid_mesher; + for (int i = 0; i < _liquid_meshers.size(); ++i) { + Ref m = _liquid_meshers.get(i); + + ERR_CONTINUE(!m.is_valid()); + + if (!liquid_mesher.is_valid()) { + liquid_mesher = m; + liquid_mesher->set_material(get_library()->get_material(0)); + continue; + } + + liquid_mesher->set_material(get_library()->get_material(0)); + liquid_mesher->add_mesher(m); + } + + if (mesher->get_vertex_count() == 0 && liquid_mesher.is_valid() && liquid_mesher->get_vertex_count() == 0) { next_phase(); return; } - RID mesh_rid = get_mesh_rid_index(MESH_INDEX_TERRARIN, MESH_TYPE_INDEX_MESH, 0); + if (mesher->get_vertex_count() != 0) { + RID mesh_rid = get_mesh_rid_index(MESH_INDEX_TERRARIN, MESH_TYPE_INDEX_MESH, 0); - Array temp_mesh_arr = mesher->build_mesh(); + Array temp_mesh_arr = mesher->build_mesh(); - if (mesh_rid == RID()) { - if ((_build_flags & BUILD_FLAG_CREATE_LODS) != 0) - create_meshes(MESH_INDEX_TERRARIN, _lod_num + 1); - else - create_meshes(MESH_INDEX_TERRARIN, 1); + if (mesh_rid == RID()) { + if ((_build_flags & BUILD_FLAG_CREATE_LODS) != 0) + create_meshes(MESH_INDEX_TERRARIN, _lod_num + 1); + else + create_meshes(MESH_INDEX_TERRARIN, 1); - mesh_rid = get_mesh_rid_index(MESH_INDEX_TERRARIN, MESH_TYPE_INDEX_MESH, 0); + mesh_rid = get_mesh_rid_index(MESH_INDEX_TERRARIN, MESH_TYPE_INDEX_MESH, 0); + } + + if (VS::get_singleton()->mesh_get_surface_count(mesh_rid) > 0) + VS::get_singleton()->mesh_remove_surface(mesh_rid, 0); + + VS::get_singleton()->mesh_add_surface_from_arrays(mesh_rid, VisualServer::PRIMITIVE_TRIANGLES, temp_mesh_arr); + + if (_library->get_material(0).is_valid()) + VS::get_singleton()->mesh_surface_set_material(mesh_rid, 0, _library->get_material(0)->get_rid()); + + if ((_build_flags & BUILD_FLAG_CREATE_LODS) != 0) { + if (_lod_num >= 1) { + //for lod 1 just remove uv2 + temp_mesh_arr[VisualServer::ARRAY_TEX_UV2] = Variant(); + + VisualServer::get_singleton()->mesh_add_surface_from_arrays(get_mesh_rid_index(MESH_INDEX_TERRARIN, MESH_TYPE_INDEX_MESH, 1), VisualServer::PRIMITIVE_TRIANGLES, temp_mesh_arr); + + if (get_library()->get_material(1).is_valid()) + VisualServer::get_singleton()->mesh_surface_set_material(get_mesh_rid_index(MESH_INDEX_TERRARIN, MESH_TYPE_INDEX_MESH, 1), 0, get_library()->get_material(1)->get_rid()); + } + + if (_lod_num >= 2) { + Array temp_mesh_arr2 = merge_mesh_array(temp_mesh_arr); + + VisualServer::get_singleton()->mesh_add_surface_from_arrays(get_mesh_rid_index(MESH_INDEX_TERRARIN, MESH_TYPE_INDEX_MESH, 2), VisualServer::PRIMITIVE_TRIANGLES, temp_mesh_arr2); + + if (get_library()->get_material(2).is_valid()) + VisualServer::get_singleton()->mesh_surface_set_material(get_mesh_rid_index(MESH_INDEX_TERRARIN, MESH_TYPE_INDEX_MESH, 2), 0, get_library()->get_material(2)->get_rid()); + } + + if (_lod_num >= 3) { + Ref mat = get_library()->get_material(0); + Ref spmat = get_library()->get_material(0); + Ref tex; + + if (mat.is_valid()) { + tex = mat->get_shader_param("texture_albedo"); + } else if (spmat.is_valid()) { + tex = spmat->get_texture(SpatialMaterial::TEXTURE_ALBEDO); + } + + if (tex.is_valid()) { + temp_mesh_arr = bake_mesh_array_uv(temp_mesh_arr, tex); + temp_mesh_arr[VisualServer::ARRAY_TEX_UV] = Variant(); + + VisualServer::get_singleton()->mesh_add_surface_from_arrays(get_mesh_rid_index(MESH_INDEX_TERRARIN, MESH_TYPE_INDEX_MESH, 3), VisualServer::PRIMITIVE_TRIANGLES, temp_mesh_arr); + + if (get_library()->get_material(3).is_valid()) + VisualServer::get_singleton()->mesh_surface_set_material(get_mesh_rid_index(MESH_INDEX_TERRARIN, MESH_TYPE_INDEX_MESH, 3), 0, get_library()->get_material(3)->get_rid()); + } + } + + /* + if (_lod_num > 4) { + Ref fqms; + fqms.instance(); + fqms->initialize(temp_mesh_arr); + + Array arr_merged_simplified; + + for (int i = 4; i < _lod_num; ++i) { + fqms->simplify_mesh(arr_merged_simplified[0].size() * 0.8, 7); + arr_merged_simplified = fqms->get_arrays(); + + if (arr_merged_simplified[0].size() == 0) + break; + + VisualServer::get_singleton()->mesh_add_surface_from_arrays(get_mesh_rid_index(MESH_INDEX_TERRARIN, MESH_TYPE_INDEX_MESH, i), VisualServer::PRIMITIVE_TRIANGLES, arr_merged_simplified); + + if (get_library()->get_material(i).is_valid()) + VisualServer::get_singleton()->mesh_surface_set_material(get_mesh_rid_index(MESH_INDEX_TERRARIN, MESH_TYPE_INDEX_MESH, i), 0, get_library()->get_material(i)->get_rid()); + } + } + */ + } } - if (VS::get_singleton()->mesh_get_surface_count(mesh_rid) > 0) - VS::get_singleton()->mesh_remove_surface(mesh_rid, 0); + if (liquid_mesher.is_valid() && liquid_mesher->get_vertex_count() != 0) { + RID mesh_rid = get_mesh_rid_index(MESH_INDEX_LIQUID, MESH_TYPE_INDEX_MESH, 0); - VS::get_singleton()->mesh_add_surface_from_arrays(mesh_rid, VisualServer::PRIMITIVE_TRIANGLES, temp_mesh_arr); + Array temp_mesh_arr = liquid_mesher->build_mesh(); - if (_library->get_material(0).is_valid()) - VS::get_singleton()->mesh_surface_set_material(mesh_rid, 0, _library->get_material(0)->get_rid()); + if (mesh_rid == RID()) { + create_meshes(MESH_INDEX_LIQUID, 1); - if ((_build_flags & BUILD_FLAG_CREATE_LODS) != 0) { - if (_lod_num >= 1) { - //for lod 1 just remove uv2 - temp_mesh_arr[VisualServer::ARRAY_TEX_UV2] = Variant(); - - VisualServer::get_singleton()->mesh_add_surface_from_arrays(get_mesh_rid_index(MESH_INDEX_TERRARIN, MESH_TYPE_INDEX_MESH, 1), VisualServer::PRIMITIVE_TRIANGLES, temp_mesh_arr); - - if (get_library()->get_material(1).is_valid()) - VisualServer::get_singleton()->mesh_surface_set_material(get_mesh_rid_index(MESH_INDEX_TERRARIN, MESH_TYPE_INDEX_MESH, 1), 0, get_library()->get_material(1)->get_rid()); + mesh_rid = get_mesh_rid_index(MESH_INDEX_LIQUID, MESH_TYPE_INDEX_MESH, 0); } - if (_lod_num >= 2) { - Array temp_mesh_arr2 = merge_mesh_array(temp_mesh_arr); + if (VS::get_singleton()->mesh_get_surface_count(mesh_rid) > 0) + VS::get_singleton()->mesh_remove_surface(mesh_rid, 0); - VisualServer::get_singleton()->mesh_add_surface_from_arrays(get_mesh_rid_index(MESH_INDEX_TERRARIN, MESH_TYPE_INDEX_MESH, 2), VisualServer::PRIMITIVE_TRIANGLES, temp_mesh_arr2); + VS::get_singleton()->mesh_add_surface_from_arrays(mesh_rid, VisualServer::PRIMITIVE_TRIANGLES, temp_mesh_arr); - if (get_library()->get_material(2).is_valid()) - VisualServer::get_singleton()->mesh_surface_set_material(get_mesh_rid_index(MESH_INDEX_TERRARIN, MESH_TYPE_INDEX_MESH, 2), 0, get_library()->get_material(2)->get_rid()); - } - - if (_lod_num >= 3) { - Ref mat = get_library()->get_material(0); - Ref spmat = get_library()->get_material(0); - Ref tex; - - if (mat.is_valid()) { - tex = mat->get_shader_param("texture_albedo"); - } else if (spmat.is_valid()) { - tex = spmat->get_texture(SpatialMaterial::TEXTURE_ALBEDO); - } - - if (tex.is_valid()) { - temp_mesh_arr = bake_mesh_array_uv(temp_mesh_arr, tex); - temp_mesh_arr[VisualServer::ARRAY_TEX_UV] = Variant(); - - VisualServer::get_singleton()->mesh_add_surface_from_arrays(get_mesh_rid_index(MESH_INDEX_TERRARIN, MESH_TYPE_INDEX_MESH, 3), VisualServer::PRIMITIVE_TRIANGLES, temp_mesh_arr); - - if (get_library()->get_material(3).is_valid()) - VisualServer::get_singleton()->mesh_surface_set_material(get_mesh_rid_index(MESH_INDEX_TERRARIN, MESH_TYPE_INDEX_MESH, 3), 0, get_library()->get_material(3)->get_rid()); - } - } - - /* - if (_lod_num > 4) { - Ref fqms; - fqms.instance(); - fqms->initialize(temp_mesh_arr); - - Array arr_merged_simplified; - - for (int i = 4; i < _lod_num; ++i) { - fqms->simplify_mesh(arr_merged_simplified[0].size() * 0.8, 7); - arr_merged_simplified = fqms->get_arrays(); - - if (arr_merged_simplified[0].size() == 0) - break; - - VisualServer::get_singleton()->mesh_add_surface_from_arrays(get_mesh_rid_index(MESH_INDEX_TERRARIN, MESH_TYPE_INDEX_MESH, i), VisualServer::PRIMITIVE_TRIANGLES, arr_merged_simplified); - - if (get_library()->get_material(i).is_valid()) - VisualServer::get_singleton()->mesh_surface_set_material(get_mesh_rid_index(MESH_INDEX_TERRARIN, MESH_TYPE_INDEX_MESH, i), 0, get_library()->get_material(i)->get_rid()); - } - } - */ + if (_library->get_liquid_material(0).is_valid()) + VS::get_singleton()->mesh_surface_set_material(mesh_rid, 0, _library->get_liquid_material(0)->get_rid()); } next_phase(); @@ -1277,12 +1368,6 @@ void VoxelChunkDefault::_build_phase(int phase) { return; } - /* - case BUILD_PHASE_LIQUID: { - next_phase(); - return; - } - */ case BUILD_PHASE_FINALIZE: { update_transforms(); @@ -1298,13 +1383,25 @@ void VoxelChunkDefault::_build_phase_process(int phase) { void VoxelChunkDefault::_build_phase_physics_process(int phase) { if (phase == BUILD_PHASE_TERRARIN_MESH_COLLIDER) { + if (temp_arr_collider.size() != 0) { + if (!has_meshes(MESH_INDEX_TERRARIN, MESH_TYPE_INDEX_BODY)) { + create_colliders(MESH_INDEX_TERRARIN); + } - if (!has_meshes(MESH_INDEX_TERRARIN, MESH_TYPE_INDEX_SHAPE)) { - create_colliders(MESH_INDEX_TERRARIN); + PhysicsServer::get_singleton()->shape_set_data(get_mesh_rid(MESH_INDEX_TERRARIN, MESH_TYPE_INDEX_SHAPE), temp_arr_collider); + + //temp_arr_collider.resize(0); } - PhysicsServer::get_singleton()->shape_set_data(get_mesh_rid(MESH_INDEX_TERRARIN, MESH_TYPE_INDEX_SHAPE), temp_arr_collider); - //temp_arr_collider.resize(0); + if (temp_arr_collider_liquid.size() != 0) { + if (!has_meshes(MESH_INDEX_LIQUID, MESH_TYPE_INDEX_BODY)) { + create_colliders(MESH_INDEX_LIQUID); + } + + PhysicsServer::get_singleton()->shape_set_data(get_mesh_rid(MESH_INDEX_LIQUID, MESH_TYPE_INDEX_SHAPE), temp_arr_collider_liquid); + + //temp_arr_collider_liquid.resize(0); + } set_active_build_phase_type(BUILD_PHASE_TYPE_NORMAL); next_phase(); diff --git a/world/default/voxel_chunk_default.h b/world/default/voxel_chunk_default.h index 49142ad..c7e53fe 100644 --- a/world/default/voxel_chunk_default.h +++ b/world/default/voxel_chunk_default.h @@ -287,6 +287,7 @@ protected: Array temp_array; PoolVector temp_arr_collider; + PoolVector temp_arr_collider_liquid; ActiveBuildPhaseType _active_build_phase_type; diff --git a/world/voxel_chunk.cpp b/world/voxel_chunk.cpp index 9838122..8a2a43f 100644 --- a/world/voxel_chunk.cpp +++ b/world/voxel_chunk.cpp @@ -239,6 +239,28 @@ int VoxelChunk::get_mesher_count() const { return _meshers.size(); } +Ref VoxelChunk::get_liquid_mesher(int index) const { + ERR_FAIL_INDEX_V(index, _liquid_meshers.size(), Ref()); + + return _liquid_meshers.get(index); +} +void VoxelChunk::set_liquid_mesher(int index, const Ref &mesher) { + ERR_FAIL_INDEX(index, _liquid_meshers.size()); + + _liquid_meshers.set(index, mesher); +} +void VoxelChunk::remove_liquid_mesher(const int index) { + ERR_FAIL_INDEX(index, _liquid_meshers.size()); + + _liquid_meshers.remove(index); +} +void VoxelChunk::add_liquid_mesher(const Ref &mesher) { + _liquid_meshers.push_back(mesher); +} +int VoxelChunk::get_liquid_mesher_count() const { + return _liquid_meshers.size(); +} + //Voxel Data void VoxelChunk::setup_channels() { ERR_FAIL_COND_MSG(!has_method("_setup_channels"), "VoxelChunk: _setup_channels() is missing! Please implement it!"); diff --git a/world/voxel_chunk.h b/world/voxel_chunk.h index 3ac7ef1..4aa2c30 100644 --- a/world/voxel_chunk.h +++ b/world/voxel_chunk.h @@ -140,6 +140,13 @@ public: void add_mesher(const Ref &mesher); int get_mesher_count() const; + //Liquid Meshers + Ref get_liquid_mesher(const int index) const; + void set_liquid_mesher(const int index, const Ref &mesher); + void remove_liquid_mesher(const int index); + void add_liquid_mesher(const Ref &mesher); + int get_liquid_mesher_count() const; + //Channels void setup_channels(); @@ -250,6 +257,7 @@ protected: Ref _library; Vector > _meshers; + Vector > _liquid_meshers; //mergeable props Vector > _props;