diff --git a/voxel_buffer.h b/voxel_buffer.h index cb681a8..6af94dc 100644 --- a/voxel_buffer.h +++ b/voxel_buffer.h @@ -65,6 +65,7 @@ public: void try_set_voxel(int x, int y, int z, int value, unsigned int channel_index = 0); + // TODO Rename set_voxel_iso to set_voxel_f etc. _FORCE_INLINE_ void set_voxel_iso(real_t value, int x, int y, int z, unsigned int channel_index = 0) { set_voxel(iso_to_byte(value), x, y, z, channel_index); } _FORCE_INLINE_ real_t get_voxel_iso(int x, int y, int z, unsigned int channel_index = 0) const { return byte_to_iso(get_voxel(x, y, z, channel_index)); } diff --git a/voxel_map.cpp b/voxel_map.cpp index e53da55..b4b1483 100644 --- a/voxel_map.cpp +++ b/voxel_map.cpp @@ -38,10 +38,10 @@ int VoxelMap::get_voxel(Vector3i pos, unsigned int c) { if (block == NULL) { return _default_voxel[c]; } - return block->voxels->get_voxel(VoxelMap::to_local(pos), c); + return block->voxels->get_voxel(to_local(pos), c); } -void VoxelMap::set_voxel(int value, Vector3i pos, unsigned int c) { +VoxelBlock *VoxelMap::get_or_create_block_at_voxel_pos(Vector3i pos) { Vector3i bpos = voxel_to_block(pos); VoxelBlock *block = get_block(bpos); @@ -57,7 +57,33 @@ void VoxelMap::set_voxel(int value, Vector3i pos, unsigned int c) { set_block(bpos, block); } - block->voxels->set_voxel(value, VoxelMap::to_local(pos), c); + return block; +} + +void VoxelMap::set_voxel(int value, Vector3i pos, unsigned int c) { + + VoxelBlock *block = get_or_create_block_at_voxel_pos(pos); + block->voxels->set_voxel(value, to_local(pos), c); +} + +float VoxelMap::get_voxel_f(int x, int y, int z, unsigned int c) { + + Vector3i pos(x, y, z); + Vector3i bpos = voxel_to_block(pos); + VoxelBlock *block = get_block(bpos); + if (block == NULL) { + return _default_voxel[c]; + } + Vector3i lpos = to_local(pos); + return block->voxels->get_voxel_iso(lpos.x, lpos.y, lpos.z, c); +} + +void VoxelMap::set_voxel_f(real_t value, int x, int y, int z, unsigned int c) { + + Vector3i pos(x, y, z); + VoxelBlock *block = get_or_create_block_at_voxel_pos(pos); + Vector3i lpos = to_local(pos); + block->voxels->set_voxel_iso(value, lpos.x, lpos.y, lpos.z, c); } void VoxelMap::set_default_voxel(int value, unsigned int channel) { @@ -177,6 +203,8 @@ void VoxelMap::_bind_methods() { ClassDB::bind_method(D_METHOD("get_voxel", "x", "y", "z", "c"), &VoxelMap::_get_voxel_binding, DEFVAL(0)); ClassDB::bind_method(D_METHOD("set_voxel", "value", "x", "y", "z", "c"), &VoxelMap::_set_voxel_binding, DEFVAL(0)); + ClassDB::bind_method(D_METHOD("get_voxel_f", "x", "y", "z", "c"), &VoxelMap::_get_voxel_binding, DEFVAL(VoxelBuffer::CHANNEL_ISOLEVEL)); + ClassDB::bind_method(D_METHOD("set_voxel_f", "value", "x", "y", "z", "c"), &VoxelMap::_set_voxel_binding, DEFVAL(VoxelBuffer::CHANNEL_ISOLEVEL)); ClassDB::bind_method(D_METHOD("get_voxel_v", "pos", "c"), &VoxelMap::_get_voxel_v_binding, DEFVAL(0)); ClassDB::bind_method(D_METHOD("set_voxel_v", "value", "pos", "c"), &VoxelMap::_set_voxel_v_binding, DEFVAL(0)); ClassDB::bind_method(D_METHOD("get_default_voxel", "channel"), &VoxelMap::get_default_voxel, DEFVAL(0)); diff --git a/voxel_map.h b/voxel_map.h index f5ce7f8..ef601eb 100644 --- a/voxel_map.h +++ b/voxel_map.h @@ -45,6 +45,9 @@ public: int get_voxel(Vector3i pos, unsigned int c = 0); void set_voxel(int value, Vector3i pos, unsigned int c = 0); + float get_voxel_f(int x, int y, int z, unsigned int c = VoxelBuffer::CHANNEL_ISOLEVEL); + void set_voxel_f(real_t value, int x, int y, int z, unsigned int c = VoxelBuffer::CHANNEL_ISOLEVEL); + void set_default_voxel(int value, unsigned int channel = 0); int get_default_voxel(unsigned int channel = 0); @@ -122,6 +125,7 @@ public: private: void set_block(Vector3i bpos, VoxelBlock *block); + VoxelBlock *get_or_create_block_at_voxel_pos(Vector3i pos); void set_block_size_pow2(unsigned int p);