Now Voxel Surfaces work again.

This commit is contained in:
Relintai 2019-08-11 22:28:26 +02:00
parent 9ae53b157c
commit 2ed334dade
5 changed files with 94 additions and 73 deletions

View File

@ -1,53 +1,77 @@
#include "voxel_surface.h" #include "voxel_surface.h"
int VoxelSurface::get_id() const { int VoxelSurface::get_id() const {
return _id; return _id;
} }
void VoxelSurface::set_id(int value) { void VoxelSurface::set_id(int value) {
_id = value; _id = value;
} }
int VoxelSurface::get_atlas_x() const { int VoxelSurface::get_atlas_x(const int side) const {
return _atlas_x; 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) { void VoxelSurface::set_atlas_x(const int side, int value) {
_atlas_x = value; int indx = (side * 2);
ERR_FAIL_INDEX(indx, VOXEL_SIDES_ARRAY_SIZE);
_atlas_positions[indx] = value;
} }
int VoxelSurface::get_atlas_y() const { int VoxelSurface::get_atlas_y(const int side) const {
return _atlas_y; 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) { void VoxelSurface::set_atlas_y(const int side, int value) {
_atlas_y = value; int indx = (side * 2) + 1;
ERR_FAIL_INDEX(indx, VOXEL_SIDES_ARRAY_SIZE);
_atlas_positions[indx] = value;
} }
bool VoxelSurface::is_transparent() const { bool VoxelSurface::is_transparent() const {
return _is_transparent; return _is_transparent;
} }
void VoxelSurface::set_transparent(bool transparent) { void VoxelSurface::set_transparent(bool transparent) {
_is_transparent = transparent; _is_transparent = transparent;
} }
String VoxelSurface::get_voxel_name() const { String VoxelSurface::get_voxel_name() const {
return _name; return _name;
} }
void VoxelSurface::set_voxel_name(String name) { void VoxelSurface::set_voxel_name(String name) {
_name = name; _name = name;
} }
Ref<VoxelmanLibrary> VoxelSurface::get_library() const { Ref<VoxelmanLibrary> VoxelSurface::get_library() const {
return Ref<VoxelmanLibrary>(_library); return Ref<VoxelmanLibrary>(_library);
} }
void VoxelSurface::set_library(Ref<VoxelmanLibrary> library) { void VoxelSurface::set_library(Ref<VoxelmanLibrary> library) {
_library = library; _library = library;
}
Vector2 VoxelSurface::transform_uv(const int side, const Vector2 uv) const {
float culomn = 1.0 / static_cast<float>(_library->get_atlas_columns());
float row = 1.0 / static_cast<float>(_library->get_atlas_rows());
return Vector2(get_atlas_x(side) * culomn + culomn * uv.x, get_atlas_y(side) * row + row * uv.y);
} }
VoxelSurface::VoxelSurface() { VoxelSurface::VoxelSurface() {
_id = 0; _id = 0;
_atlas_x = 0; for (int i = 0; i < VOXEL_SIDES_ARRAY_SIZE; ++i) {
_atlas_y = 0; _atlas_positions[i] = 0;
}
_is_transparent = false; _is_transparent = false;
} }
@ -55,8 +79,9 @@ VoxelSurface::VoxelSurface() {
VoxelSurface::VoxelSurface(int id) { VoxelSurface::VoxelSurface(int id) {
_id = id; _id = id;
_atlas_x = 0; for (int i = 0; i < VOXEL_SIDES_ARRAY_SIZE; ++i) {
_atlas_y = 0; _atlas_positions[i] = 0;
}
_is_transparent = false; _is_transparent = false;
} }
@ -66,32 +91,39 @@ VoxelSurface::~VoxelSurface() {
} }
void VoxelSurface::_bind_methods() { void VoxelSurface::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_id"), &VoxelSurface::get_id); ClassDB::bind_method(D_METHOD("get_id"), &VoxelSurface::get_id);
ClassDB::bind_method(D_METHOD("set_id", "value"), &VoxelSurface::set_id); ClassDB::bind_method(D_METHOD("set_id", "value"), &VoxelSurface::set_id);
ADD_PROPERTY(PropertyInfo(Variant::INT, "id"), "set_id", "get_id"); ADD_PROPERTY(PropertyInfo(Variant::INT, "id"), "set_id", "get_id");
//create an array //create an array
ClassDB::bind_method(D_METHOD("get_atlas_x"), &VoxelSurface::get_atlas_x); ClassDB::bind_method(D_METHOD("get_atlas_x", "side"), &VoxelSurface::get_atlas_x);
ClassDB::bind_method(D_METHOD("set_atlas_x", "value"), &VoxelSurface::set_atlas_x); ClassDB::bind_method(D_METHOD("set_atlas_x", "side", "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_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); ADD_PROPERTYI(PropertyInfo(Variant::INT, "top_atlas_x"), "set_atlas_x", "get_atlas_x", VOXEL_SIDE_TOP);
ClassDB::bind_method(D_METHOD("set_atlas_y", "value"), &VoxelSurface::set_atlas_y); ADD_PROPERTYI(PropertyInfo(Variant::INT, "top_atlas_y"), "set_atlas_y", "get_atlas_y", VOXEL_SIDE_TOP);
ADD_PROPERTY(PropertyInfo(Variant::INT, "atlas_y"), "set_atlas_y", "get_atlas_y");
ClassDB::bind_method(D_METHOD("set_transparent", "transparent"), &VoxelSurface::set_transparent, DEFVAL(false)); ADD_PROPERTYI(PropertyInfo(Variant::INT, "bottom_atlas_x"), "set_atlas_x", "get_atlas_x", VOXEL_SIDE_BOTTOM);
ClassDB::bind_method(D_METHOD("is_transparent"), &VoxelSurface::is_transparent); ADD_PROPERTYI(PropertyInfo(Variant::INT, "bottom_atlas_y"), "set_atlas_y", "get_atlas_y", VOXEL_SIDE_BOTTOM);
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "transparent"), "set_transparent", "is_transparent");
ClassDB::bind_method(D_METHOD("set_voxel_name", "name"), &VoxelSurface::set_voxel_name); ADD_PROPERTYI(PropertyInfo(Variant::INT, "side_atlas_x"), "set_atlas_x", "get_atlas_x", VOXEL_SIDE_SIDE);
ClassDB::bind_method(D_METHOD("get_voxel_name"), &VoxelSurface::get_voxel_name); ADD_PROPERTYI(PropertyInfo(Variant::INT, "side_atlas_y"), "set_atlas_y", "get_atlas_y", VOXEL_SIDE_SIDE);
ADD_PROPERTY(PropertyInfo(Variant::STRING, "voxel_name"), "set_name", "get_name");
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_TOP);
BIND_ENUM_CONSTANT(VOXEL_SIDE_BOTTOM); BIND_ENUM_CONSTANT(VOXEL_SIDE_BOTTOM);
BIND_ENUM_CONSTANT(VOXEL_SIDE_LEFT); BIND_ENUM_CONSTANT(VOXEL_SIDE_SIDE);
BIND_ENUM_CONSTANT(VOXEL_SIDE_FRONT);
BIND_ENUM_CONSTANT(VOXEL_SIDE_RIGHT);
BIND_ENUM_CONSTANT(VOXEL_SIDE_BACK);
BIND_ENUM_CONSTANT(VOXEL_SIDES_COUNT); BIND_ENUM_CONSTANT(VOXEL_SIDES_COUNT);
} }

View File

@ -30,24 +30,22 @@ public:
*/ */
enum VoxelSurfaceSides { enum VoxelSurfaceSides {
VOXEL_SIDE_TOP = 1 << 0, VOXEL_SIDE_TOP = 0,
VOXEL_SIDE_BOTTOM = 1 << 1, VOXEL_SIDE_BOTTOM = 1,
VOXEL_SIDE_LEFT = 1 << 2, VOXEL_SIDE_SIDE = 2,
VOXEL_SIDE_FRONT = 1 << 3,
VOXEL_SIDE_RIGHT = 1 << 4,
VOXEL_SIDE_BACK = 1 << 5,
VOXEL_SIDES_COUNT = 6, VOXEL_SIDES_COUNT = 3,
VOXEL_SIDES_ARRAY_SIZE = VOXEL_SIDES_COUNT * 2,
}; };
int get_id() const; int get_id() const;
void set_id(int value); void set_id(int value);
int get_atlas_x() const; int get_atlas_x(const int side) const;
void set_atlas_x(int value); void set_atlas_x(const int side, int value);
int get_atlas_y() const; int get_atlas_y(const int side) const;
void set_atlas_y(int value); void set_atlas_y(const int side, int value);
bool is_transparent() const; bool is_transparent() const;
void set_transparent(bool transparent); void set_transparent(bool transparent);
@ -57,6 +55,8 @@ public:
Ref<VoxelmanLibrary> get_library() const; Ref<VoxelmanLibrary> get_library() const;
void set_library(Ref<VoxelmanLibrary> library); void set_library(Ref<VoxelmanLibrary> library);
Vector2 transform_uv(const int side, const Vector2 uv) const;
VoxelSurface(); VoxelSurface();
VoxelSurface(int id); VoxelSurface(int id);
@ -70,8 +70,7 @@ private:
int _id; int _id;
String _name; String _name;
int _atlas_x; int _atlas_positions[VOXEL_SIDES_ARRAY_SIZE];
int _atlas_y;
bool _is_transparent; bool _is_transparent;
}; };

View File

@ -16,7 +16,6 @@ VoxelmanLibrary::VoxelmanLibrary() {
_voxel_types[i]->set_library(Ref<VoxelmanLibrary>(this)); _voxel_types[i]->set_library(Ref<VoxelmanLibrary>(this));
} }
} }
} }
VoxelmanLibrary::~VoxelmanLibrary() { VoxelmanLibrary::~VoxelmanLibrary() {
@ -33,7 +32,7 @@ void VoxelmanLibrary::set_voxel_surface(int index, Ref<VoxelSurface> value) {
ERR_FAIL_COND(value == NULL); ERR_FAIL_COND(value == NULL);
if (value != NULL) { if (value != NULL) {
value->set_library(Ref<VoxelmanLibrary>(this)); value->set_library(Ref<VoxelmanLibrary>(this));
_voxel_types[index] = Ref<VoxelSurface>(value); _voxel_types[index] = Ref<VoxelSurface>(value);
@ -69,16 +68,13 @@ int VoxelmanLibrary::get_voxel_count() const {
return count; return count;
} }
void VoxelmanLibrary::load_default() {
}
void VoxelmanLibrary::set_atlas_columns(int s) { void VoxelmanLibrary::set_atlas_columns(int s) {
ERR_FAIL_COND(s <= 0); ERR_FAIL_COND(s < 0);
_atlas_columns = s; _atlas_columns = s;
} }
void VoxelmanLibrary::set_atlas_rows(int s) { void VoxelmanLibrary::set_atlas_rows(int s) {
ERR_FAIL_COND(s <= 0); ERR_FAIL_COND(s < 0);
_atlas_rows = s; _atlas_rows = s;
} }
@ -100,11 +96,6 @@ Ref<VoxelSurface> VoxelmanLibrary::create_voxel(int id, String name) {
return voxel; return voxel;
} }
Ref<VoxelSurface> VoxelmanLibrary::_get_voxel_bind(int id) {
ERR_FAIL_COND_V(id < 0 || id >= MAX_VOXEL_TYPES, Ref<VoxelSurface>());
return _voxel_types[id];
}
void VoxelmanLibrary::_validate_property(PropertyInfo &property) const { void VoxelmanLibrary::_validate_property(PropertyInfo &property) const {
String prop = property.name; String prop = property.name;
@ -118,12 +109,12 @@ void VoxelmanLibrary::_validate_property(PropertyInfo &property) const {
void VoxelmanLibrary::_bind_methods() { void VoxelmanLibrary::_bind_methods() {
ClassDB::bind_method(D_METHOD("create_voxel", "id", "name"), &VoxelmanLibrary::create_voxel); 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("get_atlas_columns"), &VoxelmanLibrary::get_atlas_columns);
ClassDB::bind_method(D_METHOD("set_atlas_columns", "value"), &VoxelmanLibrary::set_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"); 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("get_atlas_rows"), &VoxelmanLibrary::get_atlas_rows);
ClassDB::bind_method(D_METHOD("set_atlas_rows", "value"), &VoxelmanLibrary::set_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"); ADD_PROPERTY(PropertyInfo(Variant::INT, "atlas_rows"), "set_atlas_rows", "get_atlas_rows");

View File

@ -14,7 +14,6 @@ class VoxelmanLibrary : public Resource {
GDCLASS(VoxelmanLibrary, Resource) GDCLASS(VoxelmanLibrary, Resource)
public: public:
VoxelmanLibrary(); VoxelmanLibrary();
~VoxelmanLibrary(); ~VoxelmanLibrary();
@ -37,8 +36,6 @@ public:
int get_voxel_count() const; int get_voxel_count() const;
void load_default();
Ref<VoxelSurface> get_voxel_surface(int index) const; Ref<VoxelSurface> get_voxel_surface(int index) const;
void set_voxel_surface(int index, Ref<VoxelSurface> value); void set_voxel_surface(int index, Ref<VoxelSurface> value);
@ -49,15 +46,12 @@ public:
void set_voxel_types_page(int value); void set_voxel_types_page(int value);
_FORCE_INLINE_ bool has_voxel(int id) const { return _voxel_types[id].is_valid(); } _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<VoxelSurface> get_surface(int id) const { return _voxel_types[id]; }
_FORCE_INLINE_ Ref<VoxelSurface> get_surface(int id) { return Ref<VoxelSurface>(*_voxel_types[id]); }
protected: protected:
static void _bind_methods(); static void _bind_methods();
void _validate_property(PropertyInfo &property) const; void _validate_property(PropertyInfo &property) const;
Ref<VoxelSurface> _get_voxel_bind(int id);
private: private:
enum { enum {
MAX_VOXEL_TYPES = 256, MAX_VOXEL_TYPES = 256,

View File

@ -40,6 +40,11 @@ void VoxelMesher::build_mesh(RID mesh) {
VS::get_singleton()->mesh_clear(mesh); VS::get_singleton()->mesh_clear(mesh);
if (_vertices.size() == 0) {
//Nothing to do
return;
}
_surface_tool->begin(Mesh::PRIMITIVE_TRIANGLES); _surface_tool->begin(Mesh::PRIMITIVE_TRIANGLES);
_surface_tool->set_material(_library->get_material()); _surface_tool->set_material(_library->get_material());