diff --git a/library/voxelman_library.cpp b/library/voxelman_library.cpp index fd62e53..6e47984 100644 --- a/library/voxelman_library.cpp +++ b/library/voxelman_library.cpp @@ -8,22 +8,36 @@ VoxelmanLibrary::~VoxelmanLibrary() { _prop_material.unref(); } +//Surfaces Ref VoxelmanLibrary::get_voxel_surface(int index) const { return Ref(); } - void VoxelmanLibrary::set_voxel_surface(int index, Ref value) { } - void VoxelmanLibrary::remove_surface(int index) { } - int VoxelmanLibrary::get_num_surfaces() { return 0; } +void VoxelmanLibrary::clear_surfaces() { +} +//Liquids +Ref VoxelmanLibrary::get_liquid_voxel_surface(int index) const { + return Ref(); +} +void VoxelmanLibrary::set_liquid_voxel_surface(int index, Ref value) { +} +void VoxelmanLibrary::remove_liquid_surface(int index) { +} +int VoxelmanLibrary::get_liquid_num_surfaces() { + return 0; +} +void VoxelmanLibrary::clear_liquid_surfaces() { +} + +//Rects void VoxelmanLibrary::refresh_rects() { - } void VoxelmanLibrary::_bind_methods() { @@ -39,6 +53,13 @@ void VoxelmanLibrary::_bind_methods() { ClassDB::bind_method(D_METHOD("set_voxel_surface", "index", "surface"), &VoxelmanLibrary::set_voxel_surface); ClassDB::bind_method(D_METHOD("remove_surface", "index"), &VoxelmanLibrary::remove_surface); ClassDB::bind_method(D_METHOD("get_num_surfaces"), &VoxelmanLibrary::get_num_surfaces); + ClassDB::bind_method(D_METHOD("clear_surfaces"), &VoxelmanLibrary::clear_surfaces); + + ClassDB::bind_method(D_METHOD("get_liquid_voxel_surface", "index"), &VoxelmanLibrary::get_liquid_voxel_surface); + ClassDB::bind_method(D_METHOD("set_liquid_voxel_surface", "index", "surface"), &VoxelmanLibrary::set_liquid_voxel_surface); + ClassDB::bind_method(D_METHOD("remove_liquid_surface", "index"), &VoxelmanLibrary::remove_liquid_surface); + ClassDB::bind_method(D_METHOD("get_liquid_num_surfaces"), &VoxelmanLibrary::get_liquid_num_surfaces); + ClassDB::bind_method(D_METHOD("clear_liquid_surfaces"), &VoxelmanLibrary::clear_liquid_surfaces); ClassDB::bind_method(D_METHOD("refresh_rects"), &VoxelmanLibrary::refresh_rects); } diff --git a/library/voxelman_library.h b/library/voxelman_library.h index c55f51b..89b1195 100644 --- a/library/voxelman_library.h +++ b/library/voxelman_library.h @@ -24,6 +24,13 @@ public: virtual void set_voxel_surface(int index, Ref value); virtual void remove_surface(int index); virtual int get_num_surfaces(); + virtual void clear_surfaces(); + + virtual Ref get_liquid_voxel_surface(int index) const; + virtual void set_liquid_voxel_surface(int index, Ref value); + virtual void remove_liquid_surface(int index); + virtual int get_liquid_num_surfaces(); + virtual void clear_liquid_surfaces(); virtual void refresh_rects(); diff --git a/library/voxelman_library_merger.cpp b/library/voxelman_library_merger.cpp index d19cb2d..94c1268 100644 --- a/library/voxelman_library_merger.cpp +++ b/library/voxelman_library_merger.cpp @@ -35,6 +35,7 @@ void VoxelmanLibraryMerger::set_margin(const int margin) { _packer->set_margin(margin); } +//Surfaces Ref VoxelmanLibraryMerger::get_voxel_surface(int index) const { ERR_FAIL_INDEX_V(index, _voxel_surfaces.size(), Ref(NULL)); @@ -67,6 +68,30 @@ int VoxelmanLibraryMerger::get_num_surfaces() { return _voxel_surfaces.size(); } +void VoxelmanLibraryMerger::clear_surfaces() { + _packer->clear(); + + for (int i = 0; i < _voxel_surfaces.size(); i++) { + Ref surface = _voxel_surfaces[i]; + + if (surface.is_valid()) { + surface->set_library(NULL); + } + } + + _voxel_surfaces.clear(); + + for (int i = 0; i < _liquid_surfaces.size(); i++) { + Ref surface = _liquid_surfaces[i]; + + if (surface.is_valid()) { + surface->set_library(NULL); + } + } + + _liquid_surfaces.clear(); +} + Vector VoxelmanLibraryMerger::get_voxel_surfaces() { Vector r; for (int i = 0; i < _voxel_surfaces.size(); i++) { @@ -89,6 +114,76 @@ void VoxelmanLibraryMerger::set_voxel_surfaces(const Vector &surfaces) } } +//Liquids +Ref VoxelmanLibraryMerger::get_liquid_voxel_surface(int index) const { + ERR_FAIL_INDEX_V(index, _liquid_surfaces.size(), Ref(NULL)); + + return _liquid_surfaces[index]; +} + +void VoxelmanLibraryMerger::set_liquid_voxel_surface(int index, Ref value) { + ERR_FAIL_COND(index < 0); + + if (_liquid_surfaces.size() < index) { + _liquid_surfaces.resize(index + 1); + } + + if (_liquid_surfaces[index].is_valid()) { + _liquid_surfaces.get(index)->set_library(Ref(NULL)); + } + + if (value.is_valid()) { + value->set_library(Ref(this)); + + _liquid_surfaces.set(index, value); + } +} + +void VoxelmanLibraryMerger::remove_liquid_surface(int index) { + _liquid_surfaces.remove(index); +} + +int VoxelmanLibraryMerger::get_liquid_num_surfaces() { + return _liquid_surfaces.size(); +} + +void VoxelmanLibraryMerger::clear_liquid_surfaces() { + _packer->clear(); + + for (int i = 0; i < _liquid_surfaces.size(); i++) { + Ref surface = _liquid_surfaces[i]; + + if (surface.is_valid()) { + surface->set_library(NULL); + } + } + + _liquid_surfaces.clear(); +} + +Vector VoxelmanLibraryMerger::get_liquid_voxel_surfaces() { + Vector r; + for (int i = 0; i < _liquid_surfaces.size(); i++) { + r.push_back(_liquid_surfaces[i].get_ref_ptr()); + } + return r; +} + +void VoxelmanLibraryMerger::set_liquid_voxel_surfaces(const Vector &surfaces) { + _liquid_surfaces.clear(); + + for (int i = 0; i < surfaces.size(); i++) { + Ref surface = Ref(surfaces[i]); + + if (surface.is_valid()) { + surface->set_library(this); + } + + _liquid_surfaces.push_back(surface); + } +} + + void VoxelmanLibraryMerger::refresh_rects() { bool texture_added = false; for (int i = 0; i < _voxel_surfaces.size(); i++) { @@ -111,6 +206,26 @@ void VoxelmanLibraryMerger::refresh_rects() { } } + for (int i = 0; i < _liquid_surfaces.size(); i++) { + Ref surface = Ref(_liquid_surfaces[i]); + + if (surface.is_valid()) { + for (int j = 0; j < VoxelSurface::VOXEL_SIDES_COUNT; ++j) { + Ref tex = surface->get_texture(static_cast(j)); + + if (!tex.is_valid()) + continue; + + if (!_packer->contains_texture(tex)) { + texture_added = true; + surface->set_region(static_cast(j), _packer->add_texture(tex)); + } else { + surface->set_region(static_cast(j), _packer->get_texture(tex)); + } + } + } + } + if (texture_added) { _packer->merge(); @@ -132,6 +247,14 @@ void VoxelmanLibraryMerger::refresh_rects() { surface->refresh_rects(); } } + + for (int i = 0; i < _liquid_surfaces.size(); i++) { + Ref surface = _liquid_surfaces[i]; + + if (surface.is_valid()) { + surface->refresh_rects(); + } + } } VoxelmanLibraryMerger::VoxelmanLibraryMerger() { @@ -148,12 +271,21 @@ VoxelmanLibraryMerger::~VoxelmanLibraryMerger() { if (surface.is_valid()) { surface->set_library(Ref()); - surface.unref(); } } _voxel_surfaces.clear(); + for (int i = 0; i < _liquid_surfaces.size(); ++i) { + Ref surface = _liquid_surfaces[i]; + + if (surface.is_valid()) { + surface->set_library(Ref()); + } + } + + _liquid_surfaces.clear(); + _packer.unref(); } @@ -180,6 +312,9 @@ void VoxelmanLibraryMerger::_bind_methods() { ClassDB::bind_method(D_METHOD("get_voxel_surfaces"), &VoxelmanLibraryMerger::get_voxel_surfaces); ClassDB::bind_method(D_METHOD("set_voxel_surfaces"), &VoxelmanLibraryMerger::set_voxel_surfaces); - ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "voxel_surfaces", PROPERTY_HINT_NONE, "17/17:VoxelSurfaceMerger", PROPERTY_USAGE_DEFAULT, "VoxelSurfaceMerger"), "set_voxel_surfaces", "get_voxel_surfaces"); + + ClassDB::bind_method(D_METHOD("get_liquid_voxel_surfaces"), &VoxelmanLibraryMerger::get_liquid_voxel_surfaces); + ClassDB::bind_method(D_METHOD("set_liquid_voxel_surfaces"), &VoxelmanLibraryMerger::set_liquid_voxel_surfaces); + ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "liquid_voxel_surfaces", PROPERTY_HINT_NONE, "17/17:VoxelSurfaceMerger", PROPERTY_USAGE_DEFAULT, "VoxelSurfaceMerger"), "set_liquid_voxel_surfaces", "get_liquid_voxel_surfaces"); } diff --git a/library/voxelman_library_merger.h b/library/voxelman_library_merger.h index 4a13bee..9b74730 100644 --- a/library/voxelman_library_merger.h +++ b/library/voxelman_library_merger.h @@ -35,10 +35,20 @@ public: void set_voxel_surface(int index, Ref value); void remove_surface(int index); int get_num_surfaces(); + void clear_surfaces(); Vector get_voxel_surfaces(); void set_voxel_surfaces(const Vector &surfaces); + Ref get_liquid_voxel_surface(int index) const; + void set_liquid_voxel_surface(int index, Ref value); + void remove_liquid_surface(int index); + int get_liquid_num_surfaces(); + void clear_liquid_surfaces(); + + Vector get_liquid_voxel_surfaces(); + void set_liquid_voxel_surfaces(const Vector &surfaces); + void refresh_rects(); VoxelmanLibraryMerger(); @@ -49,6 +59,7 @@ protected: private: Vector > _voxel_surfaces; + Vector > _liquid_surfaces; Ref _packer; }; diff --git a/library/voxelman_library_simple.cpp b/library/voxelman_library_simple.cpp index 7e4f002..81d9a7f 100644 --- a/library/voxelman_library_simple.cpp +++ b/library/voxelman_library_simple.cpp @@ -18,6 +18,7 @@ void VoxelmanLibrarySimple::set_atlas_rows(int s) { _atlas_rows = s; } +//Surfaces Ref VoxelmanLibrarySimple::get_voxel_surface(int index) const { ERR_FAIL_INDEX_V(index, _voxel_surfaces.size(), Ref(NULL)); @@ -50,6 +51,10 @@ int VoxelmanLibrarySimple::get_num_surfaces() { return _voxel_surfaces.size(); } +void VoxelmanLibrarySimple::clear_surfaces() { + _voxel_surfaces.clear(); +} + Vector VoxelmanLibrarySimple::get_voxel_surfaces() { Vector r; for (int i = 0; i < _voxel_surfaces.size(); i++) { @@ -73,6 +78,66 @@ void VoxelmanLibrarySimple::set_voxel_surfaces(const Vector &surfaces) } } +//Liquids +Ref VoxelmanLibrarySimple::get_liquid_voxel_surface(int index) const { + ERR_FAIL_INDEX_V(index, _liquid_surfaces.size(), Ref(NULL)); + + return _liquid_surfaces[index]; +} + +void VoxelmanLibrarySimple::set_liquid_voxel_surface(int index, Ref value) { + ERR_FAIL_COND(index < 0); + + if (_liquid_surfaces.size() < index) { + _liquid_surfaces.resize(index + 1); + } + + if (_liquid_surfaces[index].is_valid()) { + _liquid_surfaces.get(index)->set_library(Ref(NULL)); + } + + if (value.is_valid()) { + value->set_library(Ref(this)); + + _liquid_surfaces.set(index, value); + } +} + +void VoxelmanLibrarySimple::remove_liquid_surface(int index) { + _liquid_surfaces.remove(index); +} + +int VoxelmanLibrarySimple::get_liquid_num_surfaces() { + return _liquid_surfaces.size(); +} + +void VoxelmanLibrarySimple::clear_liquid_surfaces() { + _liquid_surfaces.clear(); +} + +Vector VoxelmanLibrarySimple::get_liquid_voxel_surfaces() { + Vector r; + for (int i = 0; i < _liquid_surfaces.size(); i++) { + r.push_back(_liquid_surfaces[i].get_ref_ptr()); + } + return r; +} + +void VoxelmanLibrarySimple::set_liquid_voxel_surfaces(const Vector &surfaces) { + _liquid_surfaces.clear(); + + for (int i = 0; i < surfaces.size(); i++) { + Ref surface = Ref(surfaces[i]); + + if (surface.is_valid()) { + surface->set_library(this); + surface->refresh_rects(); + } + + _liquid_surfaces.push_back(surface); + } +} + void VoxelmanLibrarySimple::refresh_rects() { for (int i = 0; i < _voxel_surfaces.size(); i++) { Ref surface = Ref(_voxel_surfaces[i]); @@ -81,6 +146,14 @@ void VoxelmanLibrarySimple::refresh_rects() { surface->refresh_rects(); } } + + for (int i = 0; i < _liquid_surfaces.size(); i++) { + Ref surface = Ref(_liquid_surfaces[i]); + + if (surface.is_valid()) { + surface->refresh_rects(); + } + } } VoxelmanLibrarySimple::VoxelmanLibrarySimple() { @@ -94,11 +167,20 @@ VoxelmanLibrarySimple::~VoxelmanLibrarySimple() { if (surface.is_valid()) { surface->set_library(Ref()); - surface.unref(); } } _voxel_surfaces.clear(); + + for (int i = 0; i < _liquid_surfaces.size(); ++i) { + Ref surface = _liquid_surfaces[i]; + + if (surface.is_valid()) { + surface->set_library(Ref()); + } + } + + _liquid_surfaces.clear(); } @@ -115,4 +197,8 @@ void VoxelmanLibrarySimple::_bind_methods() { ClassDB::bind_method(D_METHOD("set_voxel_surfaces"), &VoxelmanLibrarySimple::set_voxel_surfaces); ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "voxel_surfaces", PROPERTY_HINT_NONE, "17/17:VoxelSurfaceSimple", PROPERTY_USAGE_DEFAULT, "VoxelSurfaceSimple"), "set_voxel_surfaces", "get_voxel_surfaces"); + + ClassDB::bind_method(D_METHOD("get_liquid_voxel_surfaces"), &VoxelmanLibrarySimple::get_liquid_voxel_surfaces); + ClassDB::bind_method(D_METHOD("set_liquid_voxel_surfaces"), &VoxelmanLibrarySimple::set_liquid_voxel_surfaces); + ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "liquid_voxel_surfaces", PROPERTY_HINT_NONE, "17/17:VoxelSurfaceMerger", PROPERTY_USAGE_DEFAULT, "VoxelSurfaceMerger"), "set_liquid_voxel_surfaces", "get_liquid_voxel_surfaces"); } diff --git a/library/voxelman_library_simple.h b/library/voxelman_library_simple.h index ab07daf..363db8b 100644 --- a/library/voxelman_library_simple.h +++ b/library/voxelman_library_simple.h @@ -26,10 +26,20 @@ public: void set_voxel_surface(int index, Ref value); void remove_surface(int index); int get_num_surfaces(); + void clear_surfaces(); Vector get_voxel_surfaces(); void set_voxel_surfaces(const Vector &surfaces); + Ref get_liquid_voxel_surface(int index) const; + void set_liquid_voxel_surface(int index, Ref value); + void remove_liquid_surface(int index); + int get_liquid_num_surfaces(); + void clear_liquid_surfaces(); + + Vector get_liquid_voxel_surfaces(); + void set_liquid_voxel_surfaces(const Vector &surfaces); + void refresh_rects(); VoxelmanLibrarySimple(); @@ -40,6 +50,7 @@ protected: private: Vector > _voxel_surfaces; + Vector > _liquid_surfaces; //atlas int _atlas_columns; diff --git a/world/voxel_buffer.cpp b/world/voxel_buffer.cpp index e32f8cf..3325c12 100644 --- a/world/voxel_buffer.cpp +++ b/world/voxel_buffer.cpp @@ -434,8 +434,9 @@ void VoxelBuffer::_bind_methods() { BIND_ENUM_CONSTANT(CHANNEL_LIGHT_COLOR_B); BIND_ENUM_CONSTANT(CHANNEL_AO); BIND_ENUM_CONSTANT(CHANNEL_RANDOM_AO); - BIND_ENUM_CONSTANT(CHANNEL_DATA1); - BIND_ENUM_CONSTANT(CHANNEL_DATA2); + BIND_ENUM_CONSTANT(CHANNEL_LIQUID_TYPES); + BIND_ENUM_CONSTANT(CHANNEL_LIQUID_FILL); + BIND_ENUM_CONSTANT(CHANNEL_LIQUID_FLOW); BIND_ENUM_CONSTANT(MAX_CHANNELS); } diff --git a/world/voxel_buffer.h b/world/voxel_buffer.h index 819b9a1..9089829 100644 --- a/world/voxel_buffer.h +++ b/world/voxel_buffer.h @@ -40,8 +40,9 @@ public: CHANNEL_LIGHT_COLOR_B, CHANNEL_AO, CHANNEL_RANDOM_AO, - CHANNEL_DATA1, - CHANNEL_DATA2, + CHANNEL_LIQUID_TYPES, + CHANNEL_LIQUID_FILL, + CHANNEL_LIQUID_FLOW, // Arbitrary value, 8 should be enough. Tweak for your needs. MAX_CHANNELS };