Added a few properties to chunk, so they can be saved into PackedScenes if needed.

This commit is contained in:
Relintai 2020-02-26 01:08:18 +01:00
parent 5b32345181
commit 3e98b8e01c
2 changed files with 171 additions and 7 deletions

View File

@ -98,6 +98,16 @@ _FORCE_INLINE_ int VoxelChunk::get_size_z() {
return _size_z; return _size_z;
} }
_FORCE_INLINE_ void VoxelChunk::set_size_x(int value) {
_size_x = value;
}
_FORCE_INLINE_ void VoxelChunk::set_size_y(int value) {
_size_y = value;
}
_FORCE_INLINE_ void VoxelChunk::set_size_z(int value) {
_size_z = value;
}
_FORCE_INLINE_ Vector3 VoxelChunk::get_size() const { _FORCE_INLINE_ Vector3 VoxelChunk::get_size() const {
return Vector3(_size_x, _size_y, _size_z); return Vector3(_size_x, _size_y, _size_z);
} }
@ -112,6 +122,16 @@ _FORCE_INLINE_ int VoxelChunk::get_data_size_z() {
return _data_size_z; return _data_size_z;
} }
_FORCE_INLINE_ void VoxelChunk::set_data_size_x(int value) {
_data_size_x = value;
}
_FORCE_INLINE_ void VoxelChunk::set_data_size_y(int value) {
_data_size_y = value;
}
_FORCE_INLINE_ void VoxelChunk::set_data_size_z(int value) {
_data_size_z = value;
}
void VoxelChunk::set_position(int x, int y, int z) { void VoxelChunk::set_position(int x, int y, int z) {
_position_x = x; _position_x = x;
_position_y = y; _position_y = y;
@ -125,6 +145,13 @@ _FORCE_INLINE_ int VoxelChunk::get_margin_end() const {
return _margin_end; return _margin_end;
} }
_FORCE_INLINE_ void VoxelChunk::set_margin_start(int value) {
_margin_start = value;
}
_FORCE_INLINE_ void VoxelChunk::set_margin_end(int value) {
_margin_end = value;
}
Ref<VoxelmanLibrary> VoxelChunk::get_library() { Ref<VoxelmanLibrary> VoxelChunk::get_library() {
return _library; return _library;
} }
@ -334,6 +361,10 @@ void VoxelChunk::set_voxel(uint8_t p_value, int p_x, int p_y, int p_z, int p_cha
ch[get_data_index(x, y, z)] = p_value; ch[get_data_index(x, y, z)] = p_value;
} }
int VoxelChunk::get_channel_count() {
return _channels.size();
}
void VoxelChunk::set_channel_count(int count) { void VoxelChunk::set_channel_count(int count) {
if (count == _channels.size()) if (count == _channels.size())
return; return;
@ -418,6 +449,49 @@ uint8_t *VoxelChunk::get_valid_channel(int channel_index, uint8_t default_value)
return ch; return ch;
} }
PoolByteArray VoxelChunk::get_channel_array(int channel_index) const {
PoolByteArray arr;
uint32_t size = _data_size_x * _data_size_y * _data_size_z;
if (channel_index >= _channels.size())
return arr;
uint8_t *ch = _channels.get(channel_index);
if (ch == NULL)
return arr;
arr.resize(size);
for (uint32_t i = 0; i < size; ++i) {
arr.set(i, ch[i]);
}
return arr;
}
void VoxelChunk::set_channel_array(int channel_index, const PoolByteArray &array) {
if (array.size() == 0)
return;
if (_channels.size() <= channel_index)
set_channel_count(channel_index + 1);
uint8_t *ch = _channels.get(channel_index);
if (ch == NULL) {
if (_channels[channel_index] != NULL)
return;
ch = memnew_arr(uint8_t, array.size());
_channels.set(channel_index, ch);
}
for (int i = 0; i < array.size(); ++i) {
ch[i] = array[i];
}
}
_FORCE_INLINE_ uint32_t VoxelChunk::get_data_index(uint32_t x, uint32_t y, uint32_t z) const { _FORCE_INLINE_ uint32_t VoxelChunk::get_data_index(uint32_t x, uint32_t y, uint32_t z) const {
return y + _data_size_y * (x + _data_size_x * z); return y + _data_size_y * (x + _data_size_x * z);
} }
@ -1271,6 +1345,7 @@ VoxelChunk::VoxelChunk() {
_dirty = false; _dirty = false;
_state = VOXEL_CHUNK_STATE_OK; _state = VOXEL_CHUNK_STATE_OK;
_enabled = true;
_build_mesh = true; _build_mesh = true;
_create_collider = true; _create_collider = true;
_bake_lights = true; _bake_lights = true;
@ -1283,6 +1358,18 @@ VoxelChunk::VoxelChunk() {
_debug_drawer = NULL; _debug_drawer = NULL;
_voxel_world = NULL; _voxel_world = NULL;
_position_x = 0;
_position_y = 0;
_position_z = 0;
_size_x = 0;
_size_y = 0;
_size_z = 0;
_data_size_x = 0;
_data_size_y = 0;
_data_size_z = 0;
_margin_start = 0; _margin_start = 0;
_margin_end = 0; _margin_end = 0;
@ -1349,6 +1436,50 @@ void VoxelChunk::wait_and_finish_thread() {
} }
} }
bool VoxelChunk::_set(const StringName &p_name, const Variant &p_value) {
String name = p_name;
if (name.begins_with("channels/")) {
int index = name.get_slicec('/', 1).to_int();
if (_channels.size() <= index) {
set_channel_count(index);
}
PoolByteArray arr = p_value;
if (arr.size() == 0)
return true;
set_channel_array(index, arr);
}
return true;
}
bool VoxelChunk::_get(const StringName &p_name, Variant &r_ret) const {
String name = p_name;
if (name.begins_with("channels/")) {
int index = name.get_slicec('/', 1).to_int();
r_ret = get_channel_array(index);
return true;
}
return false;
}
void VoxelChunk::_get_property_list(List<PropertyInfo> *p_list) const {
for (int i = 0; i < _channels.size(); ++i) {
p_list->push_back(PropertyInfo(Variant::POOL_BYTE_ARRAY, "channels/" + String::num(i)));
//p_list->push_back(PropertyInfo(Variant::POOL_BYTE_ARRAY, "channels/" + String::num(i), PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE | PROPERTY_USAGE_INTERNAL));
}
}
void VoxelChunk::_bind_methods() { void VoxelChunk::_bind_methods() {
ADD_SIGNAL(MethodInfo("mesh_generation_finished", PropertyInfo(Variant::OBJECT, "chunk", PROPERTY_HINT_RESOURCE_TYPE, "VoxelChunk"))); ADD_SIGNAL(MethodInfo("mesh_generation_finished", PropertyInfo(Variant::OBJECT, "chunk", PROPERTY_HINT_RESOURCE_TYPE, "VoxelChunk")));
@ -1386,29 +1517,39 @@ void VoxelChunk::_bind_methods() {
ADD_PROPERTY(PropertyInfo(Variant::INT, "position_z"), "set_position_z", "get_position_z"); ADD_PROPERTY(PropertyInfo(Variant::INT, "position_z"), "set_position_z", "get_position_z");
ClassDB::bind_method(D_METHOD("get_size_x"), &VoxelChunk::get_size_x); ClassDB::bind_method(D_METHOD("get_size_x"), &VoxelChunk::get_size_x);
ADD_PROPERTY(PropertyInfo(Variant::INT, "size_x"), "", "get_size_x"); ClassDB::bind_method(D_METHOD("set_size_x"), &VoxelChunk::set_size_x);
ADD_PROPERTY(PropertyInfo(Variant::INT, "size_x"), "set_size_x", "get_size_x");
ClassDB::bind_method(D_METHOD("get_size_y"), &VoxelChunk::get_size_y); ClassDB::bind_method(D_METHOD("get_size_y"), &VoxelChunk::get_size_y);
ADD_PROPERTY(PropertyInfo(Variant::INT, "size_y"), "", "get_size_y"); ClassDB::bind_method(D_METHOD("set_size_y"), &VoxelChunk::set_size_y);
ADD_PROPERTY(PropertyInfo(Variant::INT, "size_y"), "set_size_y", "get_size_y");
ClassDB::bind_method(D_METHOD("get_size_z"), &VoxelChunk::get_size_z); ClassDB::bind_method(D_METHOD("get_size_z"), &VoxelChunk::get_size_z);
ADD_PROPERTY(PropertyInfo(Variant::INT, "size_z"), "", "get_size_z"); ClassDB::bind_method(D_METHOD("set_size_z"), &VoxelChunk::set_size_z);
ADD_PROPERTY(PropertyInfo(Variant::INT, "size_z"), "set_size_z", "get_size_z");
ClassDB::bind_method(D_METHOD("get_data_size_x"), &VoxelChunk::get_data_size_x); ClassDB::bind_method(D_METHOD("get_data_size_x"), &VoxelChunk::get_data_size_x);
ADD_PROPERTY(PropertyInfo(Variant::INT, "data_size_x"), "", "get_data_size_x"); ClassDB::bind_method(D_METHOD("set_data_size_x"), &VoxelChunk::set_data_size_x);
ADD_PROPERTY(PropertyInfo(Variant::INT, "data_size_x"), "set_data_size_x", "get_data_size_x");
ClassDB::bind_method(D_METHOD("get_data_size_y"), &VoxelChunk::get_data_size_y); ClassDB::bind_method(D_METHOD("get_data_size_y"), &VoxelChunk::get_data_size_y);
ADD_PROPERTY(PropertyInfo(Variant::INT, "data_size_y"), "", "get_data_size_y"); ClassDB::bind_method(D_METHOD("set_data_size_y"), &VoxelChunk::set_data_size_y);
ADD_PROPERTY(PropertyInfo(Variant::INT, "data_size_y"), "set_data_size_y", "get_data_size_y");
ClassDB::bind_method(D_METHOD("get_data_size_z"), &VoxelChunk::get_data_size_z); ClassDB::bind_method(D_METHOD("get_data_size_z"), &VoxelChunk::get_data_size_z);
ADD_PROPERTY(PropertyInfo(Variant::INT, "data_size_z"), "", "get_data_size_z"); ClassDB::bind_method(D_METHOD("set_data_size_z"), &VoxelChunk::set_data_size_z);
ADD_PROPERTY(PropertyInfo(Variant::INT, "data_size_z"), "set_data_size_z", "get_data_size_z");
ClassDB::bind_method(D_METHOD("get_position"), &VoxelChunk::get_position); ClassDB::bind_method(D_METHOD("get_position"), &VoxelChunk::get_position);
ClassDB::bind_method(D_METHOD("get_size"), &VoxelChunk::get_size);
ClassDB::bind_method(D_METHOD("set_position", "x", "y", "z"), &VoxelChunk::set_position); ClassDB::bind_method(D_METHOD("set_position", "x", "y", "z"), &VoxelChunk::set_position);
ClassDB::bind_method(D_METHOD("get_margin_start"), &VoxelChunk::get_margin_start); ClassDB::bind_method(D_METHOD("get_margin_start"), &VoxelChunk::get_margin_start);
ClassDB::bind_method(D_METHOD("set_margin_start"), &VoxelChunk::set_margin_start);
ADD_PROPERTY(PropertyInfo(Variant::INT, "margin_start"), "set_margin_start", "get_margin_start");
ClassDB::bind_method(D_METHOD("get_margin_end"), &VoxelChunk::get_margin_end); ClassDB::bind_method(D_METHOD("get_margin_end"), &VoxelChunk::get_margin_end);
ClassDB::bind_method(D_METHOD("set_margin_end"), &VoxelChunk::set_margin_end);
ADD_PROPERTY(PropertyInfo(Variant::INT, "margin_end"), "set_margin_end", "get_margin_end");
ClassDB::bind_method(D_METHOD("get_library"), &VoxelChunk::get_library); ClassDB::bind_method(D_METHOD("get_library"), &VoxelChunk::get_library);
ClassDB::bind_method(D_METHOD("set_library", "value"), &VoxelChunk::set_library); ClassDB::bind_method(D_METHOD("set_library", "value"), &VoxelChunk::set_library);
@ -1481,11 +1622,17 @@ void VoxelChunk::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_voxel", "x", "y", "z", "channel_index"), &VoxelChunk::get_voxel); ClassDB::bind_method(D_METHOD("get_voxel", "x", "y", "z", "channel_index"), &VoxelChunk::get_voxel);
ClassDB::bind_method(D_METHOD("set_voxel", "value", "x", "y", "z", "channel_index"), &VoxelChunk::set_voxel); ClassDB::bind_method(D_METHOD("set_voxel", "value", "x", "y", "z", "channel_index"), &VoxelChunk::set_voxel);
ClassDB::bind_method(D_METHOD("get_channel_count"), &VoxelChunk::get_channel_count);
ClassDB::bind_method(D_METHOD("set_channel_count", "count"), &VoxelChunk::set_channel_count); ClassDB::bind_method(D_METHOD("set_channel_count", "count"), &VoxelChunk::set_channel_count);
ADD_PROPERTY(PropertyInfo(Variant::INT, "channel_count"), "set_channel_count", "get_channel_count");
ClassDB::bind_method(D_METHOD("allocate_channel", "channel_index", "default_value"), &VoxelChunk::allocate_channel); ClassDB::bind_method(D_METHOD("allocate_channel", "channel_index", "default_value"), &VoxelChunk::allocate_channel);
ClassDB::bind_method(D_METHOD("fill_channel", "value", "channel_index"), &VoxelChunk::fill_channel); ClassDB::bind_method(D_METHOD("fill_channel", "value", "channel_index"), &VoxelChunk::fill_channel);
ClassDB::bind_method(D_METHOD("dealloc_channel", "channel_index"), &VoxelChunk::dealloc_channel); ClassDB::bind_method(D_METHOD("dealloc_channel", "channel_index"), &VoxelChunk::dealloc_channel);
ClassDB::bind_method(D_METHOD("get_channel_array", "channel_index"), &VoxelChunk::get_channel_array);
ClassDB::bind_method(D_METHOD("set_channel_array", "channel_index", "array"), &VoxelChunk::set_channel_array);
ClassDB::bind_method(D_METHOD("get_data_index", "x", "y", "z"), &VoxelChunk::get_data_index); ClassDB::bind_method(D_METHOD("get_data_index", "x", "y", "z"), &VoxelChunk::get_data_index);
ClassDB::bind_method(D_METHOD("get_data_size"), &VoxelChunk::get_data_size); ClassDB::bind_method(D_METHOD("get_data_size"), &VoxelChunk::get_data_size);

View File

@ -32,6 +32,7 @@ SOFTWARE.
#include "core/ustring.h" #include "core/ustring.h"
#include "core/array.h" #include "core/array.h"
#include "core/pool_vector.h"
#include "scene/3d/collision_shape.h" #include "scene/3d/collision_shape.h"
#include "scene/3d/mesh_instance.h" #include "scene/3d/mesh_instance.h"
#include "scene/3d/physics_body.h" #include "scene/3d/physics_body.h"
@ -129,10 +130,16 @@ public:
int get_size_x(); int get_size_x();
int get_size_y(); int get_size_y();
int get_size_z(); int get_size_z();
void set_size_x(int value);
void set_size_y(int value);
void set_size_z(int value);
int get_data_size_x(); int get_data_size_x();
int get_data_size_y(); int get_data_size_y();
int get_data_size_z(); int get_data_size_z();
void set_data_size_x(int value);
void set_data_size_y(int value);
void set_data_size_z(int value);
Vector3 get_position() const; Vector3 get_position() const;
Vector3 get_size() const; Vector3 get_size() const;
@ -140,6 +147,8 @@ public:
int get_margin_start() const; int get_margin_start() const;
int get_margin_end() const; int get_margin_end() const;
void set_margin_start(int value);
void set_margin_end(int value);
Ref<VoxelmanLibrary> get_library(); Ref<VoxelmanLibrary> get_library();
void set_library(Ref<VoxelmanLibrary> value); void set_library(Ref<VoxelmanLibrary> value);
@ -199,7 +208,9 @@ public:
uint8_t get_voxel(int p_x, int p_y, int p_z, int p_channel_index) const; uint8_t get_voxel(int p_x, int p_y, int p_z, int p_channel_index) const;
void set_voxel(uint8_t p_value, int p_x, int p_y, int p_z, int p_channel_index); void set_voxel(uint8_t p_value, int p_x, int p_y, int p_z, int p_channel_index);
int get_channel_count();
void set_channel_count(int count); void set_channel_count(int count);
void allocate_channel(int channel_index, uint8_t default_value = 0); void allocate_channel(int channel_index, uint8_t default_value = 0);
void fill_channel(uint8_t value, int channel_index); void fill_channel(uint8_t value, int channel_index);
void dealloc_channel(int channel_index); void dealloc_channel(int channel_index);
@ -207,6 +218,9 @@ public:
uint8_t *get_channel(int channel_index); uint8_t *get_channel(int channel_index);
uint8_t *get_valid_channel(int channel_index, uint8_t default_value = 0); uint8_t *get_valid_channel(int channel_index, uint8_t default_value = 0);
PoolByteArray get_channel_array(int channel_index) const;
void set_channel_array(int channel_index, const PoolByteArray &array);
uint32_t get_data_index(uint32_t x, uint32_t y, uint32_t z) const; uint32_t get_data_index(uint32_t x, uint32_t y, uint32_t z) const;
uint32_t get_data_size() const; uint32_t get_data_size() const;
@ -297,6 +311,9 @@ protected:
void wait_and_finish_thread(); void wait_and_finish_thread();
void _notification(int p_what); void _notification(int p_what);
bool _set(const StringName &p_name, const Variant &p_value);
bool _get(const StringName &p_name, Variant &r_ret) const;
void _get_property_list(List<PropertyInfo> *p_list) const;
static void _bind_methods(); static void _bind_methods();
bool _is_generating; bool _is_generating;