Added VoxelmanLibraryMerger. (A copy of the simple library at the moment.)

This commit is contained in:
Relintai 2019-11-09 18:36:17 +01:00
parent f931f3f1bf
commit cb91888f2a
6 changed files with 272 additions and 0 deletions

2
SCsub
View File

@ -6,9 +6,11 @@ env.add_source_files(env.modules_sources,"collections/vector3i.cpp")
env.add_source_files(env.modules_sources,"library/voxelman_library.cpp") env.add_source_files(env.modules_sources,"library/voxelman_library.cpp")
env.add_source_files(env.modules_sources,"library/voxelman_library_simple.cpp") env.add_source_files(env.modules_sources,"library/voxelman_library_simple.cpp")
env.add_source_files(env.modules_sources,"library/voxelman_library_merger.cpp")
env.add_source_files(env.modules_sources,"library/voxel_surface.cpp") env.add_source_files(env.modules_sources,"library/voxel_surface.cpp")
env.add_source_files(env.modules_sources,"library/voxel_surface_simple.cpp") env.add_source_files(env.modules_sources,"library/voxel_surface_simple.cpp")
env.add_source_files(env.modules_sources,"library/voxel_surface_merger.cpp")
env.add_source_files(env.modules_sources,"data/voxel_light.cpp") env.add_source_files(env.modules_sources,"data/voxel_light.cpp")

View File

@ -0,0 +1,72 @@
#include "voxel_surface_merger.h"
#include "voxelman_library_merger.h"
int VoxelSurfaceMerger::get_atlas_x(const VoxelSurfaceSides side) const {
int indx = (side * 2);
return _atlas_positions[indx];
}
void VoxelSurfaceMerger::set_atlas_x(const VoxelSurfaceSides side, int value) {
int indx = (side * 2);
_atlas_positions[indx] = value;
}
int VoxelSurfaceMerger::get_atlas_y(const VoxelSurfaceSides side) const {
int indx = (side * 2) + 1;
return _atlas_positions[indx];
}
void VoxelSurfaceMerger::set_atlas_y(const VoxelSurfaceSides side, int value) {
int indx = (side * 2) + 1;
_atlas_positions[indx] = value;
}
void VoxelSurfaceMerger::refresh_rects() {
VoxelmanLibraryMerger *lib = Object::cast_to<VoxelmanLibraryMerger>(_library);
ERR_FAIL_COND(!ObjectDB::instance_validate(lib));
for (int i = 0; i < VOXEL_SIDES_COUNT; ++i) {
float culomn = 1.0 / static_cast<float>(lib->get_atlas_columns());
float row = 1.0 / static_cast<float>(lib->get_atlas_rows());
Rect2 r;
r.position.x = _atlas_positions[i * 2] * culomn;
r.position.y = _atlas_positions[i * 2 + 1] * row;
r.size.x = culomn;
r.size.y = row;
_rects[i] = r;
}
}
VoxelSurfaceMerger::VoxelSurfaceMerger() {
for (int i = 0; i < VOXEL_SIDES_ARRAY_SIZE; ++i) {
_atlas_positions[i] = 0;
}
}
VoxelSurfaceMerger::~VoxelSurfaceMerger() {
}
void VoxelSurfaceMerger::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_atlas_x", "side"), &VoxelSurfaceMerger::get_atlas_x);
ClassDB::bind_method(D_METHOD("set_atlas_x", "side", "value"), &VoxelSurfaceMerger::set_atlas_x);
ClassDB::bind_method(D_METHOD("get_atlas_y", "side"), &VoxelSurfaceMerger::get_atlas_y);
ClassDB::bind_method(D_METHOD("set_atlas_y", "side", "value"), &VoxelSurfaceMerger::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);
}

View File

@ -0,0 +1,28 @@
#ifndef VOXEL_SURFACE_MERGER_H
#define VOXEL_SURFACE_MERGER_H
#include "voxel_surface.h"
class VoxelSurfaceMerger : public VoxelSurface {
GDCLASS(VoxelSurfaceMerger, VoxelSurface)
public:
int get_atlas_x(const VoxelSurfaceSides side) const;
void set_atlas_x(const VoxelSurfaceSides side, int value);
int get_atlas_y(const VoxelSurfaceSides side) const;
void set_atlas_y(const VoxelSurfaceSides side, int value);
void refresh_rects();
VoxelSurfaceMerger();
~VoxelSurfaceMerger();
protected:
static void _bind_methods();
private:
int _atlas_positions[VOXEL_SIDES_ARRAY_SIZE];
};
#endif

View File

@ -0,0 +1,117 @@
#include "voxelman_library_merger.h"
int VoxelmanLibraryMerger::get_atlas_columns() const {
return _atlas_columns;
}
void VoxelmanLibraryMerger::set_atlas_columns(int s) {
ERR_FAIL_COND(s < 0);
_atlas_columns = s;
}
int VoxelmanLibraryMerger::get_atlas_rows() const {
return _atlas_rows;
}
void VoxelmanLibraryMerger::set_atlas_rows(int s) {
ERR_FAIL_COND(s < 0);
_atlas_rows = s;
}
Ref<VoxelSurface> VoxelmanLibraryMerger::get_voxel_surface(int index) const {
ERR_FAIL_INDEX_V(index, _voxel_surfaces.size(), Ref<VoxelSurface>(NULL));
return _voxel_surfaces[index];
}
void VoxelmanLibraryMerger::set_voxel_surface(int index, Ref<VoxelSurface> value) {
ERR_FAIL_COND(index < 0);
if (_voxel_surfaces.size() < index) {
_voxel_surfaces.resize(index + 1);
}
if (_voxel_surfaces[index].is_valid()) {
_voxel_surfaces.get(index)->set_library(Ref<VoxelmanLibraryMerger>(NULL));
}
if (value.is_valid()) {
value->set_library(Ref<VoxelmanLibraryMerger>(this));
_voxel_surfaces.set(index, value);
}
}
void VoxelmanLibraryMerger::remove_surface(int index) {
_voxel_surfaces.remove(index);
}
int VoxelmanLibraryMerger::get_num_surfaces() {
return _voxel_surfaces.size();
}
Vector<Variant> VoxelmanLibraryMerger::get_voxel_surfaces() {
Vector<Variant> r;
for (int i = 0; i < _voxel_surfaces.size(); i++) {
r.push_back(_voxel_surfaces[i].get_ref_ptr());
}
return r;
}
void VoxelmanLibraryMerger::set_voxel_surfaces(const Vector<Variant> &surfaces) {
_voxel_surfaces.clear();
for (int i = 0; i < surfaces.size(); i++) {
Ref<VoxelSurfaceMerger> surface = Ref<VoxelSurfaceMerger>(surfaces[i]);
if (surface.is_valid()) {
surface->set_library(this);
}
_voxel_surfaces.push_back(surface);
}
}
void VoxelmanLibraryMerger::refresh_rects() {
for (int i = 0; i < _voxel_surfaces.size(); i++) {
Ref<VoxelSurfaceMerger> surface = Ref<VoxelSurfaceMerger>(_voxel_surfaces[i]);
if (surface.is_valid()) {
surface->refresh_rects();
}
}
}
VoxelmanLibraryMerger::VoxelmanLibraryMerger() {
_atlas_rows = 8;
_atlas_columns = 8;
}
VoxelmanLibraryMerger::~VoxelmanLibraryMerger() {
for (int i = 0; i < _voxel_surfaces.size(); ++i) {
Ref<VoxelSurface> surface = _voxel_surfaces[i];
if (surface.is_valid()) {
surface->set_library(Ref<VoxelmanLibraryMerger>());
surface.unref();
}
}
_voxel_surfaces.clear();
}
void VoxelmanLibraryMerger::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_atlas_columns"), &VoxelmanLibraryMerger::get_atlas_columns);
ClassDB::bind_method(D_METHOD("set_atlas_columns", "value"), &VoxelmanLibraryMerger::set_atlas_columns);
ADD_PROPERTY(PropertyInfo(Variant::INT, "atlas_columns"), "set_atlas_columns", "get_atlas_columns");
ClassDB::bind_method(D_METHOD("get_atlas_rows"), &VoxelmanLibraryMerger::get_atlas_rows);
ClassDB::bind_method(D_METHOD("set_atlas_rows", "value"), &VoxelmanLibraryMerger::set_atlas_rows);
ADD_PROPERTY(PropertyInfo(Variant::INT, "atlas_rows"), "set_atlas_rows", "get_atlas_rows");
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");
}

View File

@ -0,0 +1,49 @@
#ifndef VOXELMAN_LIBRARY_MERGER_H
#define VOXELMAN_LIBRARY_MERGER_H
#include "voxelman_library.h"
#include "core/resource.h"
#include "scene/resources/material.h"
#include "../data/voxel_light.h"
#include "voxel_surface_merger.h"
class VoxelSurfaceSimple;
class VoxelMesher;
class VoxelmanLibraryMerger : public VoxelmanLibrary {
GDCLASS(VoxelmanLibraryMerger, VoxelmanLibrary)
public:
int get_atlas_columns() const;
void set_atlas_columns(int s);
int get_atlas_rows() const;
void set_atlas_rows(int s);
Ref<VoxelSurface> get_voxel_surface(int index) const;
void set_voxel_surface(int index, Ref<VoxelSurface> value);
void remove_surface(int index);
int get_num_surfaces();
Vector<Variant> get_voxel_surfaces();
void set_voxel_surfaces(const Vector<Variant> &surfaces);
void refresh_rects();
VoxelmanLibraryMerger();
~VoxelmanLibraryMerger();
protected:
static void _bind_methods();
private:
Vector<Ref<VoxelSurfaceMerger> > _voxel_surfaces;
//atlas
int _atlas_columns;
int _atlas_rows;
};
#endif // VOXEL_LIBRARY_H

View File

@ -2,9 +2,11 @@
#include "library/voxel_surface.h" #include "library/voxel_surface.h"
#include "library/voxel_surface_simple.h" #include "library/voxel_surface_simple.h"
#include "library/voxel_surface_merger.h"
#include "library/voxelman_library.h" #include "library/voxelman_library.h"
#include "library/voxelman_library_simple.h" #include "library/voxelman_library_simple.h"
#include "library/voxelman_library_merger.h"
#include "data/voxel_light.h" #include "data/voxel_light.h"
#include "meshers/voxel_mesher.h" #include "meshers/voxel_mesher.h"
@ -56,9 +58,11 @@ void register_voxelman_types() {
ClassDB::register_class<VoxelSurface>(); ClassDB::register_class<VoxelSurface>();
ClassDB::register_class<VoxelSurfaceSimple>(); ClassDB::register_class<VoxelSurfaceSimple>();
ClassDB::register_class<VoxelSurfaceMerger>();
ClassDB::register_class<VoxelmanLibrary>(); ClassDB::register_class<VoxelmanLibrary>();
ClassDB::register_class<VoxelmanLibrarySimple>(); ClassDB::register_class<VoxelmanLibrarySimple>();
ClassDB::register_class<VoxelmanLibraryMerger>();
ClassDB::register_class<VoxelLight>(); ClassDB::register_class<VoxelLight>();