From 2ed334dade98f28798604fe0949ad0ac5ecbae5b Mon Sep 17 00:00:00 2001 From: Relintai Date: Sun, 11 Aug 2019 22:28:26 +0200 Subject: [PATCH] Now Voxel Surfaces work again. --- library/voxel_surface.cpp | 110 ++++++++++++++++++++++------------- library/voxel_surface.h | 25 ++++---- library/voxelman_library.cpp | 19 ++---- library/voxelman_library.h | 8 +-- meshers/voxel_mesher.cpp | 5 ++ 5 files changed, 94 insertions(+), 73 deletions(-) diff --git a/library/voxel_surface.cpp b/library/voxel_surface.cpp index b329a60..b8d5426 100644 --- a/library/voxel_surface.cpp +++ b/library/voxel_surface.cpp @@ -1,53 +1,77 @@ #include "voxel_surface.h" int VoxelSurface::get_id() const { - return _id; + return _id; } void VoxelSurface::set_id(int value) { - _id = value; + _id = value; } -int VoxelSurface::get_atlas_x() const { - return _atlas_x; +int VoxelSurface::get_atlas_x(const int side) const { + int indx = (side * 2); + + ERR_FAIL_INDEX_V(indx, VOXEL_SIDES_ARRAY_SIZE, 0); + + return _atlas_positions[indx]; } -void VoxelSurface::set_atlas_x(int value) { - _atlas_x = value; +void VoxelSurface::set_atlas_x(const int side, int value) { + int indx = (side * 2); + + ERR_FAIL_INDEX(indx, VOXEL_SIDES_ARRAY_SIZE); + + _atlas_positions[indx] = value; } -int VoxelSurface::get_atlas_y() const { - return _atlas_y; +int VoxelSurface::get_atlas_y(const int side) const { + int indx = (side * 2) + 1; + + ERR_FAIL_INDEX_V(indx, VOXEL_SIDES_ARRAY_SIZE, 0); + + return _atlas_positions[indx]; } -void VoxelSurface::set_atlas_y(int value) { - _atlas_y = value; +void VoxelSurface::set_atlas_y(const int side, int value) { + int indx = (side * 2) + 1; + + ERR_FAIL_INDEX(indx, VOXEL_SIDES_ARRAY_SIZE); + + _atlas_positions[indx] = value; } bool VoxelSurface::is_transparent() const { - return _is_transparent; + return _is_transparent; } void VoxelSurface::set_transparent(bool transparent) { - _is_transparent = transparent; + _is_transparent = transparent; } String VoxelSurface::get_voxel_name() const { - return _name; + return _name; } void VoxelSurface::set_voxel_name(String name) { - _name = name; + _name = name; } Ref VoxelSurface::get_library() const { - return Ref(_library); + return Ref(_library); } void VoxelSurface::set_library(Ref library) { - _library = library; + _library = library; +} + +Vector2 VoxelSurface::transform_uv(const int side, const Vector2 uv) const { + float culomn = 1.0 / static_cast(_library->get_atlas_columns()); + float row = 1.0 / static_cast(_library->get_atlas_rows()); + + return Vector2(get_atlas_x(side) * culomn + culomn * uv.x, get_atlas_y(side) * row + row * uv.y); } VoxelSurface::VoxelSurface() { _id = 0; - _atlas_x = 0; - _atlas_y = 0; + for (int i = 0; i < VOXEL_SIDES_ARRAY_SIZE; ++i) { + _atlas_positions[i] = 0; + } _is_transparent = false; } @@ -55,8 +79,9 @@ VoxelSurface::VoxelSurface() { VoxelSurface::VoxelSurface(int id) { _id = id; - _atlas_x = 0; - _atlas_y = 0; + for (int i = 0; i < VOXEL_SIDES_ARRAY_SIZE; ++i) { + _atlas_positions[i] = 0; + } _is_transparent = false; } @@ -66,32 +91,39 @@ VoxelSurface::~VoxelSurface() { } void VoxelSurface::_bind_methods() { - ClassDB::bind_method(D_METHOD("get_id"), &VoxelSurface::get_id); - ClassDB::bind_method(D_METHOD("set_id", "value"), &VoxelSurface::set_id); - ADD_PROPERTY(PropertyInfo(Variant::INT, "id"), "set_id", "get_id"); + ClassDB::bind_method(D_METHOD("get_id"), &VoxelSurface::get_id); + ClassDB::bind_method(D_METHOD("set_id", "value"), &VoxelSurface::set_id); + ADD_PROPERTY(PropertyInfo(Variant::INT, "id"), "set_id", "get_id"); //create an array - ClassDB::bind_method(D_METHOD("get_atlas_x"), &VoxelSurface::get_atlas_x); - ClassDB::bind_method(D_METHOD("set_atlas_x", "value"), &VoxelSurface::set_atlas_x); - ADD_PROPERTY(PropertyInfo(Variant::INT, "atlas_x"), "set_atlas_x", "get_atlas_x"); + ClassDB::bind_method(D_METHOD("get_atlas_x", "side"), &VoxelSurface::get_atlas_x); + ClassDB::bind_method(D_METHOD("set_atlas_x", "side", "value"), &VoxelSurface::set_atlas_x); + + ClassDB::bind_method(D_METHOD("get_atlas_y", "side"), &VoxelSurface::get_atlas_y); + ClassDB::bind_method(D_METHOD("set_atlas_y", "side", "value"), &VoxelSurface::set_atlas_y); - ClassDB::bind_method(D_METHOD("get_atlas_y"), &VoxelSurface::get_atlas_y); - ClassDB::bind_method(D_METHOD("set_atlas_y", "value"), &VoxelSurface::set_atlas_y); - ADD_PROPERTY(PropertyInfo(Variant::INT, "atlas_y"), "set_atlas_y", "get_atlas_y"); + ADD_PROPERTYI(PropertyInfo(Variant::INT, "top_atlas_x"), "set_atlas_x", "get_atlas_x", VOXEL_SIDE_TOP); + ADD_PROPERTYI(PropertyInfo(Variant::INT, "top_atlas_y"), "set_atlas_y", "get_atlas_y", VOXEL_SIDE_TOP); - ClassDB::bind_method(D_METHOD("set_transparent", "transparent"), &VoxelSurface::set_transparent, DEFVAL(false)); - ClassDB::bind_method(D_METHOD("is_transparent"), &VoxelSurface::is_transparent); - ADD_PROPERTY(PropertyInfo(Variant::BOOL, "transparent"), "set_transparent", "is_transparent"); + ADD_PROPERTYI(PropertyInfo(Variant::INT, "bottom_atlas_x"), "set_atlas_x", "get_atlas_x", VOXEL_SIDE_BOTTOM); + ADD_PROPERTYI(PropertyInfo(Variant::INT, "bottom_atlas_y"), "set_atlas_y", "get_atlas_y", VOXEL_SIDE_BOTTOM); - ClassDB::bind_method(D_METHOD("set_voxel_name", "name"), &VoxelSurface::set_voxel_name); - ClassDB::bind_method(D_METHOD("get_voxel_name"), &VoxelSurface::get_voxel_name); - ADD_PROPERTY(PropertyInfo(Variant::STRING, "voxel_name"), "set_name", "get_name"); + ADD_PROPERTYI(PropertyInfo(Variant::INT, "side_atlas_x"), "set_atlas_x", "get_atlas_x", VOXEL_SIDE_SIDE); + ADD_PROPERTYI(PropertyInfo(Variant::INT, "side_atlas_y"), "set_atlas_y", "get_atlas_y", VOXEL_SIDE_SIDE); + + + ClassDB::bind_method(D_METHOD("set_transparent", "transparent"), &VoxelSurface::set_transparent, DEFVAL(false)); + ClassDB::bind_method(D_METHOD("is_transparent"), &VoxelSurface::is_transparent); + ADD_PROPERTY(PropertyInfo(Variant::BOOL, "transparent"), "set_transparent", "is_transparent"); + + ClassDB::bind_method(D_METHOD("set_voxel_name", "name"), &VoxelSurface::set_voxel_name); + ClassDB::bind_method(D_METHOD("get_voxel_name"), &VoxelSurface::get_voxel_name); + ADD_PROPERTY(PropertyInfo(Variant::STRING, "voxel_name"), "set_name", "get_name"); + + ClassDB::bind_method(D_METHOD("transform_uv", "side", "uv"), &VoxelSurface::transform_uv); BIND_ENUM_CONSTANT(VOXEL_SIDE_TOP); BIND_ENUM_CONSTANT(VOXEL_SIDE_BOTTOM); - BIND_ENUM_CONSTANT(VOXEL_SIDE_LEFT); - BIND_ENUM_CONSTANT(VOXEL_SIDE_FRONT); - BIND_ENUM_CONSTANT(VOXEL_SIDE_RIGHT); - BIND_ENUM_CONSTANT(VOXEL_SIDE_BACK); + BIND_ENUM_CONSTANT(VOXEL_SIDE_SIDE); BIND_ENUM_CONSTANT(VOXEL_SIDES_COUNT); } diff --git a/library/voxel_surface.h b/library/voxel_surface.h index 96bde7b..21d61e8 100644 --- a/library/voxel_surface.h +++ b/library/voxel_surface.h @@ -30,24 +30,22 @@ public: */ enum VoxelSurfaceSides { - VOXEL_SIDE_TOP = 1 << 0, - VOXEL_SIDE_BOTTOM = 1 << 1, - VOXEL_SIDE_LEFT = 1 << 2, - VOXEL_SIDE_FRONT = 1 << 3, - VOXEL_SIDE_RIGHT = 1 << 4, - VOXEL_SIDE_BACK = 1 << 5, + VOXEL_SIDE_TOP = 0, + VOXEL_SIDE_BOTTOM = 1, + VOXEL_SIDE_SIDE = 2, - VOXEL_SIDES_COUNT = 6, + VOXEL_SIDES_COUNT = 3, + VOXEL_SIDES_ARRAY_SIZE = VOXEL_SIDES_COUNT * 2, }; int get_id() const; void set_id(int value); - int get_atlas_x() const; - void set_atlas_x(int value); + int get_atlas_x(const int side) const; + void set_atlas_x(const int side, int value); - int get_atlas_y() const; - void set_atlas_y(int value); + int get_atlas_y(const int side) const; + void set_atlas_y(const int side, int value); bool is_transparent() const; void set_transparent(bool transparent); @@ -57,6 +55,8 @@ public: Ref get_library() const; void set_library(Ref library); + + Vector2 transform_uv(const int side, const Vector2 uv) const; VoxelSurface(); VoxelSurface(int id); @@ -70,8 +70,7 @@ private: int _id; String _name; - int _atlas_x; - int _atlas_y; + int _atlas_positions[VOXEL_SIDES_ARRAY_SIZE]; bool _is_transparent; }; diff --git a/library/voxelman_library.cpp b/library/voxelman_library.cpp index 793ba8c..e92b247 100644 --- a/library/voxelman_library.cpp +++ b/library/voxelman_library.cpp @@ -16,7 +16,6 @@ VoxelmanLibrary::VoxelmanLibrary() { _voxel_types[i]->set_library(Ref(this)); } } - } VoxelmanLibrary::~VoxelmanLibrary() { @@ -33,7 +32,7 @@ void VoxelmanLibrary::set_voxel_surface(int index, Ref value) { ERR_FAIL_COND(value == NULL); if (value != NULL) { - + value->set_library(Ref(this)); _voxel_types[index] = Ref(value); @@ -69,16 +68,13 @@ int VoxelmanLibrary::get_voxel_count() const { return count; } -void VoxelmanLibrary::load_default() { -} - void VoxelmanLibrary::set_atlas_columns(int s) { - ERR_FAIL_COND(s <= 0); + ERR_FAIL_COND(s < 0); _atlas_columns = s; } void VoxelmanLibrary::set_atlas_rows(int s) { - ERR_FAIL_COND(s <= 0); + ERR_FAIL_COND(s < 0); _atlas_rows = s; } @@ -100,11 +96,6 @@ Ref VoxelmanLibrary::create_voxel(int id, String name) { return voxel; } -Ref VoxelmanLibrary::_get_voxel_bind(int id) { - ERR_FAIL_COND_V(id < 0 || id >= MAX_VOXEL_TYPES, Ref()); - return _voxel_types[id]; -} - void VoxelmanLibrary::_validate_property(PropertyInfo &property) const { String prop = property.name; @@ -118,12 +109,12 @@ void VoxelmanLibrary::_validate_property(PropertyInfo &property) const { void VoxelmanLibrary::_bind_methods() { ClassDB::bind_method(D_METHOD("create_voxel", "id", "name"), &VoxelmanLibrary::create_voxel); - ClassDB::bind_method(D_METHOD("get_voxel", "id"), &VoxelmanLibrary::_get_voxel_bind); + ClassDB::bind_method(D_METHOD("get_surface", "id"), &VoxelmanLibrary::get_surface); ClassDB::bind_method(D_METHOD("get_atlas_columns"), &VoxelmanLibrary::get_atlas_columns); ClassDB::bind_method(D_METHOD("set_atlas_columns", "value"), &VoxelmanLibrary::set_atlas_columns); ADD_PROPERTY(PropertyInfo(Variant::INT, "atlas_columns"), "set_atlas_columns", "get_atlas_columns"); - + ClassDB::bind_method(D_METHOD("get_atlas_rows"), &VoxelmanLibrary::get_atlas_rows); ClassDB::bind_method(D_METHOD("set_atlas_rows", "value"), &VoxelmanLibrary::set_atlas_rows); ADD_PROPERTY(PropertyInfo(Variant::INT, "atlas_rows"), "set_atlas_rows", "get_atlas_rows"); diff --git a/library/voxelman_library.h b/library/voxelman_library.h index 5576307..1b27d8d 100644 --- a/library/voxelman_library.h +++ b/library/voxelman_library.h @@ -14,7 +14,6 @@ class VoxelmanLibrary : public Resource { GDCLASS(VoxelmanLibrary, Resource) public: - VoxelmanLibrary(); ~VoxelmanLibrary(); @@ -37,8 +36,6 @@ public: int get_voxel_count() const; - void load_default(); - Ref get_voxel_surface(int index) const; void set_voxel_surface(int index, Ref value); @@ -49,15 +46,12 @@ public: void set_voxel_types_page(int value); _FORCE_INLINE_ bool has_voxel(int id) const { return _voxel_types[id].is_valid(); } - _FORCE_INLINE_ const VoxelSurface &get_voxel_const(int id) const { return **_voxel_types[id]; } - _FORCE_INLINE_ Ref get_surface(int id) { return Ref(*_voxel_types[id]); } + _FORCE_INLINE_ Ref get_surface(int id) const { return _voxel_types[id]; } protected: static void _bind_methods(); void _validate_property(PropertyInfo &property) const; - Ref _get_voxel_bind(int id); - private: enum { MAX_VOXEL_TYPES = 256, diff --git a/meshers/voxel_mesher.cpp b/meshers/voxel_mesher.cpp index 1cfdafa..4b23b47 100644 --- a/meshers/voxel_mesher.cpp +++ b/meshers/voxel_mesher.cpp @@ -40,6 +40,11 @@ void VoxelMesher::build_mesh(RID mesh) { VS::get_singleton()->mesh_clear(mesh); + if (_vertices.size() == 0) { + //Nothing to do + return; + } + _surface_tool->begin(Mesh::PRIMITIVE_TRIANGLES); _surface_tool->set_material(_library->get_material());