From 5e5d95aac5ed79a06586aaddb2c74cae03ff94ff Mon Sep 17 00:00:00 2001 From: Relintai Date: Sun, 17 Nov 2019 02:17:40 +0100 Subject: [PATCH] Added margin to VoxelBuffer. --- world/voxel_buffer.cpp | 33 +++++++++++++++++++++++++++++---- world/voxel_buffer.h | 11 ++++++++--- 2 files changed, 37 insertions(+), 7 deletions(-) diff --git a/world/voxel_buffer.cpp b/world/voxel_buffer.cpp index 3325c12..b18383f 100644 --- a/world/voxel_buffer.cpp +++ b/world/voxel_buffer.cpp @@ -22,20 +22,24 @@ #include #include -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() { + _margin_start = 0; + _margin_end = 0; } VoxelBuffer::~VoxelBuffer() { 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) { 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) { for (unsigned int i = 0; i < MAX_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); } } + _size = new_size; } + + _margin_start = margin_start; + _margin_end = margin_end; } 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]; + x += _margin_start + _margin_end; + y += _margin_start + _margin_end; + z += _margin_start + _margin_end; + if (validate_pos(x, y, z) && channel.data) { return channel.data[index(x, y, z)]; } 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) { 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)); 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. void VoxelBuffer::try_set_voxel(int x, int y, int z, int value, unsigned int channel_index) { 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)) { return; } @@ -399,9 +421,12 @@ void VoxelBuffer::delete_channel(int i) { 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("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_x"), &VoxelBuffer::get_size_x); ClassDB::bind_method(D_METHOD("get_size_y"), &VoxelBuffer::get_size_y); diff --git a/world/voxel_buffer.h b/world/voxel_buffer.h index 9089829..2111145 100644 --- a/world/voxel_buffer.h +++ b/world/voxel_buffer.h @@ -72,10 +72,10 @@ public: return static_cast(b - 128) / 128.f; } - VoxelBuffer(); - ~VoxelBuffer(); + _FORCE_INLINE_ int get_margin_start() const { return _margin_start; } + _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_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)); } @@ -130,6 +130,9 @@ public: void add_light(int local_x, int local_y, int local_z, int size, Color color); void clear_lights(); + VoxelBuffer(); + ~VoxelBuffer(); + private: void create_channel_noinit(int i, Vector3i size); 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. Vector3i _size; + uint32_t _margin_start; + uint32_t _margin_end; }; VARIANT_ENUM_CAST(VoxelBuffer::ChannelId)