Added margin to VoxelBuffer.

This commit is contained in:
Relintai 2019-11-17 02:17:40 +01:00
parent b6f49e3f89
commit 5e5d95aac5
2 changed files with 37 additions and 7 deletions

View File

@ -22,20 +22,24 @@
#include <core/math/math_funcs.h> #include <core/math/math_funcs.h>
#include <string.h> #include <string.h>
const char *VoxelBuffer::CHANNEL_ID_HINT_STRING = "Type,Sdf,Data2,Data3,Data4,Data5,Data6,Data7"; const char *VoxelBuffer::CHANNEL_ID_HINT_STRING = "Type,Isolevel,Light Color R,Light Color G,Light Color B,AO,Random AO,Liquid Types,Liquid Fill,Liquid Flow";
VoxelBuffer::VoxelBuffer() { VoxelBuffer::VoxelBuffer() {
_margin_start = 0;
_margin_end = 0;
} }
VoxelBuffer::~VoxelBuffer() { VoxelBuffer::~VoxelBuffer() {
clear(); clear();
} }
void VoxelBuffer::create(int sx, int sy, int sz) { void VoxelBuffer::create(int sx, int sy, int sz, int margin_start, int margin_end) {
if (sx <= 0 || sy <= 0 || sz <= 0) { if (sx <= 0 || sy <= 0 || sz <= 0) {
return; return;
} }
Vector3i new_size(sx, sy, sz);
Vector3i new_size(sx + margin_start + margin_end, sy + margin_start + margin_end, sz + margin_start + margin_end);
if (new_size != _size) { if (new_size != _size) {
for (unsigned int i = 0; i < MAX_CHANNELS; ++i) { for (unsigned int i = 0; i < MAX_CHANNELS; ++i) {
Channel &channel = _channels[i]; Channel &channel = _channels[i];
@ -46,8 +50,12 @@ void VoxelBuffer::create(int sx, int sy, int sz) {
create_channel(i, new_size, channel.defval); create_channel(i, new_size, channel.defval);
} }
} }
_size = new_size; _size = new_size;
} }
_margin_start = margin_start;
_margin_end = margin_end;
} }
void VoxelBuffer::clear() { void VoxelBuffer::clear() {
@ -78,6 +86,10 @@ int VoxelBuffer::get_voxel(int x, int y, int z, unsigned int channel_index) cons
const Channel &channel = _channels[channel_index]; const Channel &channel = _channels[channel_index];
x += _margin_start + _margin_end;
y += _margin_start + _margin_end;
z += _margin_start + _margin_end;
if (validate_pos(x, y, z) && channel.data) { if (validate_pos(x, y, z) && channel.data) {
return channel.data[index(x, y, z)]; return channel.data[index(x, y, z)];
} else { } else {
@ -87,6 +99,11 @@ int VoxelBuffer::get_voxel(int x, int y, int z, unsigned int channel_index) cons
void VoxelBuffer::set_voxel(int value, int x, int y, int z, unsigned int channel_index) { void VoxelBuffer::set_voxel(int value, int x, int y, int z, unsigned int channel_index) {
ERR_FAIL_INDEX(channel_index, MAX_CHANNELS); ERR_FAIL_INDEX(channel_index, MAX_CHANNELS);
x += _margin_start + _margin_end;
y += _margin_start + _margin_end;
z += _margin_start + _margin_end;
ERR_FAIL_COND(!validate_pos(x, y, z)); ERR_FAIL_COND(!validate_pos(x, y, z));
Channel &channel = _channels[channel_index]; Channel &channel = _channels[channel_index];
@ -105,6 +122,11 @@ void VoxelBuffer::set_voxel(int value, int x, int y, int z, unsigned int channel
// This version does not cause errors if out of bounds. Use only if it's okay to be outside. // This version does not cause errors if out of bounds. Use only if it's okay to be outside.
void VoxelBuffer::try_set_voxel(int x, int y, int z, int value, unsigned int channel_index) { void VoxelBuffer::try_set_voxel(int x, int y, int z, int value, unsigned int channel_index) {
ERR_FAIL_INDEX(channel_index, MAX_CHANNELS); ERR_FAIL_INDEX(channel_index, MAX_CHANNELS);
x += _margin_start + _margin_end;
y += _margin_start + _margin_end;
z += _margin_start + _margin_end;
if (!validate_pos(x, y, z)) { if (!validate_pos(x, y, z)) {
return; return;
} }
@ -399,9 +421,12 @@ void VoxelBuffer::delete_channel(int i) {
void VoxelBuffer::_bind_methods() { void VoxelBuffer::_bind_methods() {
ClassDB::bind_method(D_METHOD("create", "sx", "sy", "sz"), &VoxelBuffer::create); ClassDB::bind_method(D_METHOD("create", "sx", "sy", "sz", "margin_start", "margin_end"), &VoxelBuffer::create, DEFVAL(0), DEFVAL(0));
ClassDB::bind_method(D_METHOD("clear"), &VoxelBuffer::clear); ClassDB::bind_method(D_METHOD("clear"), &VoxelBuffer::clear);
ClassDB::bind_method(D_METHOD("get_margin_start"), &VoxelBuffer::get_margin_start);
ClassDB::bind_method(D_METHOD("get_margin_end"), &VoxelBuffer::get_margin_end);
ClassDB::bind_method(D_METHOD("get_size"), &VoxelBuffer::_get_size_binding); ClassDB::bind_method(D_METHOD("get_size"), &VoxelBuffer::_get_size_binding);
ClassDB::bind_method(D_METHOD("get_size_x"), &VoxelBuffer::get_size_x); ClassDB::bind_method(D_METHOD("get_size_x"), &VoxelBuffer::get_size_x);
ClassDB::bind_method(D_METHOD("get_size_y"), &VoxelBuffer::get_size_y); ClassDB::bind_method(D_METHOD("get_size_y"), &VoxelBuffer::get_size_y);

View File

@ -72,10 +72,10 @@ public:
return static_cast<float>(b - 128) / 128.f; return static_cast<float>(b - 128) / 128.f;
} }
VoxelBuffer(); _FORCE_INLINE_ int get_margin_start() const { return _margin_start; }
~VoxelBuffer(); _FORCE_INLINE_ int get_margin_end() const { return _margin_end; }
void create(int sx, int sy, int sz); void create(int sx, int sy, int sz, int margin_start = 0, int margin_end = 0);
void clear(); void clear();
void clear_channel(unsigned int channel_index, int clear_value = 0); void clear_channel(unsigned int channel_index, int clear_value = 0);
_FORCE_INLINE_ void clear_channel_f(unsigned int channel_index, float clear_value = 0) { clear_channel(channel_index, iso_to_byte(clear_value)); } _FORCE_INLINE_ void clear_channel_f(unsigned int channel_index, float clear_value = 0) { clear_channel(channel_index, iso_to_byte(clear_value)); }
@ -130,6 +130,9 @@ public:
void add_light(int local_x, int local_y, int local_z, int size, Color color); void add_light(int local_x, int local_y, int local_z, int size, Color color);
void clear_lights(); void clear_lights();
VoxelBuffer();
~VoxelBuffer();
private: private:
void create_channel_noinit(int i, Vector3i size); void create_channel_noinit(int i, Vector3i size);
void create_channel(int i, Vector3i size, uint8_t defval); void create_channel(int i, Vector3i size, uint8_t defval);
@ -170,6 +173,8 @@ private:
// How many voxels are there in the three directions. All populated channels have the same size. // How many voxels are there in the three directions. All populated channels have the same size.
Vector3i _size; Vector3i _size;
uint32_t _margin_start;
uint32_t _margin_end;
}; };
VARIANT_ENUM_CAST(VoxelBuffer::ChannelId) VARIANT_ENUM_CAST(VoxelBuffer::ChannelId)