2019-05-31 22:54:31 +02:00
|
|
|
#include "voxel_surface.h"
|
|
|
|
|
2019-06-01 00:11:59 +02:00
|
|
|
int VoxelSurface::get_id() const {
|
2019-08-11 22:28:26 +02:00
|
|
|
return _id;
|
2019-06-01 00:11:59 +02:00
|
|
|
}
|
|
|
|
void VoxelSurface::set_id(int value) {
|
2019-08-11 22:28:26 +02:00
|
|
|
_id = value;
|
2019-06-01 00:11:59 +02:00
|
|
|
}
|
|
|
|
|
2019-08-11 22:28:26 +02:00
|
|
|
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];
|
2019-06-01 00:11:59 +02:00
|
|
|
}
|
2019-08-11 22:28:26 +02:00
|
|
|
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;
|
2019-06-01 00:11:59 +02:00
|
|
|
}
|
|
|
|
|
2019-08-11 22:28:26 +02:00
|
|
|
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];
|
2019-06-01 00:11:59 +02:00
|
|
|
}
|
2019-08-11 22:28:26 +02:00
|
|
|
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;
|
2019-06-01 00:11:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool VoxelSurface::is_transparent() const {
|
2019-08-11 22:28:26 +02:00
|
|
|
return _is_transparent;
|
2019-06-01 00:11:59 +02:00
|
|
|
}
|
|
|
|
void VoxelSurface::set_transparent(bool transparent) {
|
2019-08-11 22:28:26 +02:00
|
|
|
_is_transparent = transparent;
|
2019-06-01 00:11:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
String VoxelSurface::get_voxel_name() const {
|
2019-08-11 22:28:26 +02:00
|
|
|
return _name;
|
2019-06-01 00:11:59 +02:00
|
|
|
}
|
2019-05-31 22:54:31 +02:00
|
|
|
void VoxelSurface::set_voxel_name(String name) {
|
2019-08-11 22:28:26 +02:00
|
|
|
_name = name;
|
2019-06-01 00:11:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Ref<VoxelmanLibrary> VoxelSurface::get_library() const {
|
2019-08-11 22:28:26 +02:00
|
|
|
return Ref<VoxelmanLibrary>(_library);
|
2019-06-01 00:11:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void VoxelSurface::set_library(Ref<VoxelmanLibrary> library) {
|
2019-08-14 00:29:28 +02:00
|
|
|
_library = (*library);
|
2019-08-11 22:28:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2019-05-31 22:54:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
VoxelSurface::VoxelSurface() {
|
2019-08-11 15:29:32 +02:00
|
|
|
_id = 0;
|
|
|
|
|
2019-08-11 22:28:26 +02:00
|
|
|
for (int i = 0; i < VOXEL_SIDES_ARRAY_SIZE; ++i) {
|
|
|
|
_atlas_positions[i] = 0;
|
|
|
|
}
|
2019-08-11 15:29:32 +02:00
|
|
|
|
|
|
|
_is_transparent = false;
|
2019-08-14 00:29:28 +02:00
|
|
|
|
|
|
|
_library = NULL;
|
2019-05-31 22:54:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
VoxelSurface::VoxelSurface(int id) {
|
2019-08-11 15:29:32 +02:00
|
|
|
_id = id;
|
2019-05-31 22:54:31 +02:00
|
|
|
|
2019-08-11 22:28:26 +02:00
|
|
|
for (int i = 0; i < VOXEL_SIDES_ARRAY_SIZE; ++i) {
|
|
|
|
_atlas_positions[i] = 0;
|
|
|
|
}
|
2019-08-11 15:29:32 +02:00
|
|
|
|
|
|
|
_is_transparent = false;
|
2019-08-14 00:29:28 +02:00
|
|
|
|
|
|
|
_library = NULL;
|
2019-05-31 22:54:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
VoxelSurface::~VoxelSurface() {
|
2019-08-14 00:29:28 +02:00
|
|
|
//_library = NULL;
|
2019-05-31 22:54:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void VoxelSurface::_bind_methods() {
|
2019-08-11 22:28:26 +02:00
|
|
|
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");
|
2019-05-31 22:54:31 +02:00
|
|
|
|
2019-08-11 15:29:32 +02:00
|
|
|
//create an array
|
2019-08-11 22:28:26 +02:00
|
|
|
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);
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
2019-05-31 22:54:31 +02:00
|
|
|
|
2019-08-11 22:28:26 +02:00
|
|
|
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");
|
2019-05-31 22:54:31 +02:00
|
|
|
|
2019-08-11 22:28:26 +02:00
|
|
|
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");
|
2019-05-31 22:54:31 +02:00
|
|
|
|
2019-08-11 22:28:26 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("transform_uv", "side", "uv"), &VoxelSurface::transform_uv);
|
2019-06-09 20:59:54 +02:00
|
|
|
|
|
|
|
BIND_ENUM_CONSTANT(VOXEL_SIDE_TOP);
|
|
|
|
BIND_ENUM_CONSTANT(VOXEL_SIDE_BOTTOM);
|
2019-08-11 22:28:26 +02:00
|
|
|
BIND_ENUM_CONSTANT(VOXEL_SIDE_SIDE);
|
2019-06-09 20:59:54 +02:00
|
|
|
BIND_ENUM_CONSTANT(VOXEL_SIDES_COUNT);
|
2019-05-31 22:54:31 +02:00
|
|
|
}
|