2020-01-31 19:52:37 +01:00
|
|
|
/*
|
|
|
|
Copyright (c) 2019-2020 Péter Magyar
|
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in all
|
|
|
|
copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
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
|
|
|
}
|
2019-11-09 17:26:16 +01:00
|
|
|
void VoxelSurface::set_id(const int value) {
|
2019-08-11 22:28:26 +02:00
|
|
|
_id = value;
|
2019-06-01 00:11:59 +02:00
|
|
|
}
|
|
|
|
|
2020-02-13 14:16:57 +01:00
|
|
|
int VoxelSurface::get_mesher_index() const {
|
|
|
|
return _mesher_index;
|
|
|
|
}
|
|
|
|
void VoxelSurface::set_mesher_index(const int value) {
|
|
|
|
_mesher_index = 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
|
|
|
}
|
2019-11-09 17:26:16 +01:00
|
|
|
void VoxelSurface::set_transparent(const bool transparent) {
|
2019-08-11 22:28:26 +02:00
|
|
|
_is_transparent = transparent;
|
2019-06-01 00:11:59 +02:00
|
|
|
}
|
|
|
|
|
2019-11-09 17:26:16 +01:00
|
|
|
Rect2 VoxelSurface::get_rect(const VoxelSurfaceSides side) const {
|
|
|
|
return _rects[side];
|
2019-06-01 00:11:59 +02:00
|
|
|
}
|
2020-04-16 17:54:32 +02:00
|
|
|
void VoxelSurface::set_rect(const VoxelSurfaceSides side, const Rect2 &rect) {
|
2019-11-09 17:26:16 +01:00
|
|
|
_rects[side] = rect;
|
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-05-31 22:54:31 +02:00
|
|
|
}
|
|
|
|
|
2020-04-16 17:54:32 +02:00
|
|
|
_FORCE_INLINE_ Vector2 VoxelSurface::transform_uv(const VoxelSurfaceSides p_side, const Vector2 &p_uv) const {
|
2019-11-09 17:26:16 +01:00
|
|
|
Vector2 uv = p_uv;
|
2019-08-11 15:29:32 +02:00
|
|
|
|
2019-11-09 17:26:16 +01:00
|
|
|
Rect2 r = _rects[p_side];
|
2019-08-11 15:29:32 +02:00
|
|
|
|
2019-11-09 17:26:16 +01:00
|
|
|
uv.x *= r.size.x;
|
|
|
|
uv.y *= r.size.y;
|
|
|
|
uv.x += r.position.x;
|
|
|
|
uv.y += r.position.y;
|
2019-08-14 00:29:28 +02:00
|
|
|
|
2019-11-09 17:26:16 +01:00
|
|
|
return uv;
|
2019-05-31 22:54:31 +02:00
|
|
|
}
|
|
|
|
|
2020-04-16 17:54:32 +02:00
|
|
|
_FORCE_INLINE_ Vector2 VoxelSurface::transform_uv_scaled(const VoxelSurfaceSides p_side, const Vector2 &p_uv, const int p_current_x, const int p_current_y, const int p_max) const {
|
2020-01-14 22:26:40 +01:00
|
|
|
Vector2 uv = p_uv;
|
|
|
|
|
|
|
|
Rect2 r = _rects[p_side];
|
|
|
|
|
|
|
|
float sizex = r.size.x / static_cast<float>(p_max);
|
|
|
|
float sizey = r.size.x / static_cast<float>(p_max);
|
|
|
|
|
|
|
|
uv.x *= sizex;
|
|
|
|
uv.y *= sizey;
|
|
|
|
|
|
|
|
uv.x += r.position.x + sizex * p_current_x;
|
|
|
|
uv.y += r.position.y + sizey * p_current_y;
|
|
|
|
|
|
|
|
return uv;
|
|
|
|
}
|
|
|
|
|
2019-11-09 17:26:16 +01:00
|
|
|
void VoxelSurface::refresh_rects() {
|
|
|
|
}
|
2019-08-11 15:29:32 +02:00
|
|
|
|
2019-11-09 17:26:16 +01:00
|
|
|
VoxelSurface::VoxelSurface() {
|
|
|
|
_id = 0;
|
2020-02-13 14:16:57 +01:00
|
|
|
_mesher_index = 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-11-09 17:26:16 +01: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
|
|
|
|
2020-02-13 14:16:57 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("get_mesher_index"), &VoxelSurface::get_mesher_index);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_mesher_index", "value"), &VoxelSurface::set_mesher_index);
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::INT, "mesher_index"), "set_mesher_index", "get_mesher_index");
|
|
|
|
|
2019-11-09 17:26:16 +01:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::STRING, "voxel_name"), "set_name", "get_name");
|
2019-05-31 22:54:31 +02:00
|
|
|
|
2019-11-09 17:26:16 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("set_transparent", "transparent"), &VoxelSurface::set_transparent);
|
2019-08-11 22:28:26 +02:00
|
|
|
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-11-09 17:26:16 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("get_rect", "side"), &VoxelSurface::get_rect);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_rect", "side", "rect"), &VoxelSurface::set_rect);
|
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);
|
2020-01-14 22:26:40 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("transform_uv_scaled", "side", "uv", "p_current_x", "p_current_y", "max"), &VoxelSurface::transform_uv_scaled);
|
2019-06-09 20:59:54 +02:00
|
|
|
|
2019-11-09 17:26:16 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("refresh_rects"), &VoxelSurface::refresh_rects);
|
|
|
|
|
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-11-09 17:26:16 +01:00
|
|
|
|
|
|
|
BIND_CONSTANT(VOXEL_SIDES_COUNT);
|
2019-05-31 22:54:31 +02:00
|
|
|
}
|