Added data margin start, and end property to VoxelWorld for easy access. Improved set_voxel, and get_voxel in VoxelChunk to accomodate for margins as originally intended (e.g. if start_margin is 1, indexes go from -1 to size + end margin). Fixed/updated indexing in VoxelMesherCubic.

This commit is contained in:
Relintai 2020-02-14 03:19:15 +01:00
parent b5ff42ae11
commit a457ed400c
5 changed files with 67 additions and 27 deletions

View File

@ -29,11 +29,10 @@ void VoxelMesherCubic::_add_chunk(Node *p_chunk) {
chunk->generate_ao();
int x_size = chunk->get_size_x() - 1;
int y_size = chunk->get_size_y() - 1;
int z_size = chunk->get_size_z() - 1;
int x_size = chunk->get_size_x();
int y_size = chunk->get_size_y();
int z_size = chunk->get_size_z();
float lod_size = get_lod_size();
float voxel_size = get_lod_size();
float voxel_scale = get_voxel_scale();
@ -43,11 +42,11 @@ void VoxelMesherCubic::_add_chunk(Node *p_chunk) {
float tile_uv_size = 1 / 4.0;
Color base_light(_base_light_value, _base_light_value, _base_light_value);
for (int y = 1; y < y_size - 1; y += lod_size) {
for (int z = 1; z < z_size - 1; z += lod_size) {
for (int x = 1; x < x_size - 1; x += lod_size) {
for (int y = 0; y < y_size; ++y) {
for (int z = 0; z < z_size; ++z) {
for (int x = 0; x < x_size; ++x) {
cube_points->setup(chunk, x, y, z, lod_size);
cube_points->setup(chunk, x, y, z, 1);
if (!cube_points->has_points())
continue;

View File

@ -306,24 +306,32 @@ bool VoxelChunk::validate_channel_data_position(uint32_t x, uint32_t y, uint32_t
return x < _data_size_x && y < _data_size_y && z < _data_size_z;
}
uint8_t VoxelChunk::get_voxel(int x, int y, int z, int channel_index) const {
ERR_FAIL_INDEX_V(channel_index, _channels.size(), 0);
ERR_FAIL_COND_V(!validate_channel_data_position(x, y, z), 0);
uint8_t VoxelChunk::get_voxel(int p_x, int p_y, int p_z, int p_channel_index) const {
int x = p_x + _margin_start;
int y = p_y + _margin_start;
int z = p_z + _margin_start;
uint8_t *ch = _channels.get(channel_index);
ERR_FAIL_INDEX_V(p_channel_index, _channels.size(), 0);
ERR_FAIL_COND_V_MSG(!validate_channel_data_position(x, y, z), 0, "Error, index out of range! " + String::num(x) + " " + String::num(y) + " " + String::num(z));
uint8_t *ch = _channels.get(p_channel_index);
if (!ch)
return 0;
return ch[get_data_index(x, y, z)];
}
void VoxelChunk::set_voxel(uint8_t value, int x, int y, int z, int channel_index) {
ERR_FAIL_INDEX(channel_index, _channels.size());
ERR_FAIL_COND(!validate_channel_data_position(x, y, z));
void VoxelChunk::set_voxel(uint8_t p_value, int p_x, int p_y, int p_z, int p_channel_index) {
int x = p_x + _margin_start;
int y = p_y + _margin_start;
int z = p_z + _margin_start;
uint8_t *ch = get_valid_channel(channel_index);
ERR_FAIL_INDEX(p_channel_index, _channels.size());
ERR_FAIL_COND_MSG(!validate_channel_data_position(x, y, z), "Error, index out of range! " + String::num(x) + " " + String::num(y) + " " + String::num(z));
ch[get_data_index(x, y, z)] = value;
uint8_t *ch = get_valid_channel(p_channel_index);
ch[get_data_index(x, y, z)] = p_value;
}
void VoxelChunk::set_channel_count(int count) {
@ -420,15 +428,15 @@ _FORCE_INLINE_ uint32_t VoxelChunk::get_data_size() const {
//Data Management functions
void VoxelChunk::generate_ao() {
unsigned int size_x = _data_size_x;
unsigned int size_y = _data_size_y;
unsigned int size_z = _data_size_z;
ERR_FAIL_COND(_data_size_x == 0 || _data_size_y == 0 || _data_size_z == 0);
ERR_FAIL_COND(size_x == 0 || size_y == 0 || size_z == 0);
int size_x = get_size_x() + get_margin_end();
int size_y = get_size_y() + get_margin_end();
int size_z = get_size_z() + get_margin_end();
for (unsigned int y = 1; y < size_y - 1; ++y) {
for (unsigned int z = 1; z < size_z - 1; ++z) {
for (unsigned int x = 1; x < size_x - 1; ++x) {
for (int y = get_margin_start() - 1; y < size_y - 1; ++y) {
for (int z = get_margin_start() - 1; z < size_z - 1; ++z) {
for (int x = get_margin_start() - 1; x < size_x - 1; ++x) {
int current = get_voxel(x, y, z, DEFAULT_CHANNEL_ISOLEVEL);
int sum = get_voxel(x + 1, y, z, DEFAULT_CHANNEL_ISOLEVEL);

View File

@ -196,8 +196,8 @@ public:
bool validate_channel_data_position(uint32_t x, uint32_t y, uint32_t z) const;
uint8_t get_voxel(int x, int y, int z, int channel_index) const;
void set_voxel(uint8_t value, int x, int y, int z, int channel_index);
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_channel_count(int count);
void allocate_channel(int channel_index, uint8_t default_value = 0);

View File

@ -45,6 +45,21 @@ void VoxelWorld::set_chunk_size_z(const int value) {
_chunk_size_z = value;
}
int VoxelWorld::get_data_margin_start() const {
return _data_margin_start;
}
void VoxelWorld::set_data_margin_start(const int value) {
_data_margin_start = value;
}
int VoxelWorld::get_data_margin_end() const {
return _data_margin_end;
}
void VoxelWorld::set_data_margin_end(const int value) {
_data_margin_end = value;
}
int VoxelWorld::get_current_seed() const {
return _current_seed;
}
@ -263,7 +278,7 @@ VoxelChunk *VoxelWorld::_create_chunk(int x, int y, int z, Node *p_chunk) {
chunk->set_position(x, y, z);
chunk->set_library(_library);
chunk->set_voxel_scale(_voxel_scale);
chunk->set_size(_chunk_size_x, _chunk_size_y, _chunk_size_z);
chunk->set_size(_chunk_size_x, _chunk_size_y, _chunk_size_z, _data_margin_start, _data_margin_end);
chunk->set_translation(Vector3(x * _chunk_size_x * _voxel_scale, y * _chunk_size_y * _voxel_scale, z * _chunk_size_z * _voxel_scale));
add_chunk(chunk, x, y, z);
@ -303,6 +318,8 @@ VoxelWorld::VoxelWorld() {
_chunk_size_y = 16;
_chunk_size_z = 16;
_current_seed = 0;
_data_margin_start = 0;
_data_margin_end = 0;
set_use_threads(true);
set_max_concurrent_generations(3);
@ -390,6 +407,14 @@ void VoxelWorld::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_chunk_size_z", "value"), &VoxelWorld::set_chunk_size_z);
ADD_PROPERTY(PropertyInfo(Variant::INT, "chunk_size_z"), "set_chunk_size_z", "get_chunk_size_z");
ClassDB::bind_method(D_METHOD("get_data_margin_start"), &VoxelWorld::get_data_margin_start);
ClassDB::bind_method(D_METHOD("set_data_margin_start", "value"), &VoxelWorld::set_data_margin_start);
ADD_PROPERTY(PropertyInfo(Variant::INT, "data_margin_start"), "set_data_margin_start", "get_data_margin_start");
ClassDB::bind_method(D_METHOD("get_data_margin_end"), &VoxelWorld::get_data_margin_end);
ClassDB::bind_method(D_METHOD("set_data_margin_end", "value"), &VoxelWorld::set_data_margin_end);
ADD_PROPERTY(PropertyInfo(Variant::INT, "data_margin_end"), "set_data_margin_end", "get_data_margin_end");
ClassDB::bind_method(D_METHOD("get_current_seed"), &VoxelWorld::get_current_seed);
ClassDB::bind_method(D_METHOD("set_current_seed", "value"), &VoxelWorld::set_current_seed);
ADD_PROPERTY(PropertyInfo(Variant::INT, "current_seed"), "set_current_seed", "get_current_seed");

View File

@ -48,6 +48,12 @@ public:
int get_chunk_size_z() const;
void set_chunk_size_z(const int value);
int get_data_margin_start() const;
void set_data_margin_start(const int value);
int get_data_margin_end() const;
void set_data_margin_end(const int value);
int get_current_seed() const;
void set_current_seed(const int value);
@ -150,6 +156,8 @@ private:
int _chunk_size_y;
int _chunk_size_z;
int _current_seed;
int _data_margin_start;
int _data_margin_end;
Ref<VoxelmanLibrary> _library;
Ref<VoxelmanLevelGenerator> _level_generator;