2016-05-01 06:00:02 -07:00
|
|
|
#ifndef VOXEL_BUFFER_H
|
|
|
|
#define VOXEL_BUFFER_H
|
|
|
|
|
2019-09-03 14:54:40 -07:00
|
|
|
#include "math/rect3i.h"
|
2018-09-19 12:25:04 -07:00
|
|
|
#include <core/reference.h>
|
|
|
|
#include <core/vector.h>
|
2016-05-01 06:00:02 -07:00
|
|
|
|
2019-09-05 11:43:25 -07:00
|
|
|
class VoxelTool;
|
|
|
|
|
2016-05-01 06:00:02 -07:00
|
|
|
// Dense voxels data storage.
|
|
|
|
// Organized in 8-bit channels like images, all optional.
|
|
|
|
// Note: for float storage (marching cubes for example), you can map [0..256] to [0..1] and save 3 bytes per cell
|
|
|
|
|
|
|
|
class VoxelBuffer : public Reference {
|
2017-03-24 17:23:36 -07:00
|
|
|
GDCLASS(VoxelBuffer, Reference)
|
2016-05-01 06:00:02 -07:00
|
|
|
|
2016-05-05 12:08:52 -07:00
|
|
|
public:
|
2019-04-21 11:31:35 -07:00
|
|
|
enum ChannelId {
|
|
|
|
CHANNEL_TYPE = 0,
|
|
|
|
CHANNEL_ISOLEVEL,
|
|
|
|
CHANNEL_DATA2,
|
|
|
|
CHANNEL_DATA3,
|
2019-04-22 13:17:19 -07:00
|
|
|
CHANNEL_DATA4,
|
|
|
|
CHANNEL_DATA5,
|
|
|
|
CHANNEL_DATA6,
|
|
|
|
CHANNEL_DATA7,
|
2019-04-21 11:31:35 -07:00
|
|
|
// Arbitrary value, 8 should be enough. Tweak for your needs.
|
|
|
|
MAX_CHANNELS
|
|
|
|
};
|
2016-05-01 06:00:02 -07:00
|
|
|
|
2019-05-29 16:46:08 -07:00
|
|
|
// TODO use C++17 inline to initialize right here...
|
|
|
|
static const char *CHANNEL_ID_HINT_STRING;
|
|
|
|
|
2019-05-27 16:40:09 -07:00
|
|
|
enum Compression {
|
|
|
|
COMPRESSION_NONE = 0,
|
|
|
|
COMPRESSION_UNIFORM,
|
|
|
|
//COMPRESSION_RLE,
|
|
|
|
COMPRESSION_COUNT
|
|
|
|
};
|
|
|
|
|
2019-04-22 13:17:19 -07:00
|
|
|
// TODO Quantification options
|
|
|
|
// enum ChannelFormat {
|
2019-04-27 17:34:00 -07:00
|
|
|
// FORMAT_I8_Q256U, // 0..255 integer
|
2019-04-22 13:17:19 -07:00
|
|
|
// FORMAT_F8_Q1S, // -1..1 float stored in 8 bits
|
|
|
|
// FORMAT_F16_Q128S // -128..128 float stored in 16 bits
|
|
|
|
// };
|
|
|
|
|
2017-04-06 14:44:11 -07:00
|
|
|
// Converts -1..1 float into 0..255 integer
|
2019-05-27 16:40:09 -07:00
|
|
|
static inline int
|
|
|
|
iso_to_byte(real_t iso) {
|
2017-04-06 14:44:11 -07:00
|
|
|
int v = static_cast<int>(128.f * iso + 128.f);
|
2017-08-12 16:19:39 -07:00
|
|
|
if (v > 255)
|
2017-04-06 14:44:11 -07:00
|
|
|
return 255;
|
2017-08-12 16:19:39 -07:00
|
|
|
else if (v < 0)
|
2017-04-06 14:44:11 -07:00
|
|
|
return 0;
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Converts 0..255 integer into -1..1 float
|
|
|
|
static inline real_t byte_to_iso(int b) {
|
|
|
|
return static_cast<float>(b - 128) / 128.f;
|
|
|
|
}
|
|
|
|
|
2016-12-31 19:40:16 -08:00
|
|
|
VoxelBuffer();
|
|
|
|
~VoxelBuffer();
|
2016-05-01 06:00:02 -07:00
|
|
|
|
2016-12-31 19:40:16 -08:00
|
|
|
void create(int sx, int sy, int sz);
|
2019-09-06 15:24:56 -07:00
|
|
|
void create(Vector3i size);
|
2016-12-31 19:40:16 -08:00
|
|
|
void clear();
|
2017-08-12 16:19:39 -07:00
|
|
|
void clear_channel(unsigned int channel_index, int clear_value = 0);
|
2019-05-05 10:27:09 -07:00
|
|
|
_FORCE_INLINE_ void clear_channel_f(unsigned int channel_index, float clear_value = 0) { clear_channel(channel_index, iso_to_byte(clear_value)); }
|
2016-05-01 06:00:02 -07:00
|
|
|
|
2019-04-21 11:31:35 -07:00
|
|
|
_FORCE_INLINE_ const Vector3i &get_size() const { return _size; }
|
2016-05-01 06:00:02 -07:00
|
|
|
|
2017-01-04 17:39:40 -08:00
|
|
|
void set_default_values(uint8_t values[MAX_CHANNELS]);
|
|
|
|
|
2017-08-12 16:19:39 -07:00
|
|
|
int get_voxel(int x, int y, int z, unsigned int channel_index = 0) const;
|
|
|
|
void set_voxel(int value, int x, int y, int z, unsigned int channel_index = 0);
|
2017-08-19 12:10:31 -07:00
|
|
|
|
2018-10-04 15:13:03 -07:00
|
|
|
void try_set_voxel(int x, int y, int z, int value, unsigned int channel_index = 0);
|
|
|
|
|
2019-04-28 08:28:49 -07:00
|
|
|
_FORCE_INLINE_ void set_voxel_f(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_f(int x, int y, int z, unsigned int channel_index = 0) const { return byte_to_iso(get_voxel(x, y, z, channel_index)); }
|
2017-08-19 12:10:31 -07:00
|
|
|
|
2016-12-31 19:40:16 -08:00
|
|
|
_FORCE_INLINE_ int get_voxel(const Vector3i pos, unsigned int channel_index = 0) const { return get_voxel(pos.x, pos.y, pos.z, channel_index); }
|
|
|
|
_FORCE_INLINE_ void set_voxel(int value, const Vector3i pos, unsigned int channel_index = 0) { set_voxel(value, pos.x, pos.y, pos.z, channel_index); }
|
2016-05-01 06:00:02 -07:00
|
|
|
|
2016-12-31 19:40:16 -08:00
|
|
|
void fill(int defval, unsigned int channel_index = 0);
|
2019-04-28 08:28:49 -07:00
|
|
|
_FORCE_INLINE_ void fill_f(float value, unsigned int channel = 0) { fill(iso_to_byte(value), channel); }
|
2016-12-31 19:40:16 -08:00
|
|
|
void fill_area(int defval, Vector3i min, Vector3i max, unsigned int channel_index = 0);
|
2016-05-01 06:00:02 -07:00
|
|
|
|
2019-04-27 16:27:17 -07:00
|
|
|
bool is_uniform(unsigned int channel_index) const;
|
2016-05-01 06:00:02 -07:00
|
|
|
|
2019-05-25 07:51:59 -07:00
|
|
|
void compress_uniform_channels();
|
2019-05-27 16:40:09 -07:00
|
|
|
void decompress_channel(unsigned int channel_index);
|
|
|
|
Compression get_channel_compression(unsigned int channel_index) const;
|
|
|
|
|
|
|
|
void grab_channel_data(uint8_t *in_buffer, unsigned int channel_index, Compression compression);
|
2016-05-01 06:00:02 -07:00
|
|
|
|
2017-08-12 16:19:39 -07:00
|
|
|
void copy_from(const VoxelBuffer &other, unsigned int channel_index = 0);
|
|
|
|
void copy_from(const VoxelBuffer &other, Vector3i src_min, Vector3i src_max, Vector3i dst_min, unsigned int channel_index = 0);
|
2016-05-01 06:00:02 -07:00
|
|
|
|
2019-05-27 16:40:09 -07:00
|
|
|
Ref<VoxelBuffer> duplicate() const;
|
|
|
|
|
2016-12-31 19:40:16 -08:00
|
|
|
_FORCE_INLINE_ bool validate_pos(unsigned int x, unsigned int y, unsigned int z) const {
|
2019-08-14 17:21:45 -07:00
|
|
|
return x < (unsigned)_size.x && y < (unsigned)_size.y && z < (unsigned)_size.z;
|
2016-12-31 19:40:16 -08:00
|
|
|
}
|
2016-05-01 06:00:02 -07:00
|
|
|
|
2016-12-31 19:40:16 -08:00
|
|
|
_FORCE_INLINE_ unsigned int index(unsigned int x, unsigned int y, unsigned int z) const {
|
2019-04-25 19:35:00 -07:00
|
|
|
return y + _size.y * (x + _size.x * z);
|
2016-12-31 19:40:16 -08:00
|
|
|
}
|
2016-05-02 15:09:07 -07:00
|
|
|
|
2019-04-21 11:31:35 -07:00
|
|
|
// _FORCE_INLINE_ unsigned int row_index(unsigned int x, unsigned int y, unsigned int z) const {
|
2019-04-25 19:35:00 -07:00
|
|
|
// return _size.y * (x + _size.x * z);
|
2019-04-21 11:31:35 -07:00
|
|
|
// }
|
2016-05-05 12:08:52 -07:00
|
|
|
|
2017-04-06 14:44:11 -07:00
|
|
|
_FORCE_INLINE_ unsigned int get_volume() const {
|
2016-12-31 19:40:16 -08:00
|
|
|
return _size.x * _size.y * _size.z;
|
|
|
|
}
|
2016-05-02 15:09:07 -07:00
|
|
|
|
2018-09-29 17:55:09 -07:00
|
|
|
uint8_t *get_channel_raw(unsigned int channel_index) const;
|
|
|
|
|
2019-09-02 13:14:30 -07:00
|
|
|
void downscale_to(VoxelBuffer &dst, Vector3i src_min, Vector3i src_max, Vector3i dst_min) const;
|
2019-09-03 14:54:40 -07:00
|
|
|
Ref<VoxelTool> get_voxel_tool() const;
|
|
|
|
|
2019-09-06 15:24:56 -07:00
|
|
|
bool equals(const VoxelBuffer *p_other) const;
|
|
|
|
|
2019-09-03 14:54:40 -07:00
|
|
|
// TODO Make this work, would be awesome for perf
|
|
|
|
//
|
|
|
|
// template <typename F>
|
|
|
|
// void read_write_action(Rect3i box, Vector3i offset, unsigned int channel_index, F f) {
|
|
|
|
// ERR_FAIL_INDEX(channel_index, MAX_CHANNELS);
|
|
|
|
// box.clip(Rect3i(Vector3i(), _size));
|
|
|
|
// Vector3i min_pos = box.pos;
|
|
|
|
// Vector3i max_pos = box.pos + box.size;
|
|
|
|
// Vector3i pos;
|
|
|
|
// for (pos.z = min_pos.z; pos.z < max_pos.z; ++pos.z) {
|
|
|
|
// for (pos.x = min_pos.x; pos.x < max_pos.x; ++pos.x) {
|
|
|
|
// for (pos.y = min_pos.y; pos.y < max_pos.y; ++pos.y) {
|
|
|
|
// int v0 = get_voxel(pos, channel_index);
|
|
|
|
// int v1 = f(pos + offset, v0);
|
|
|
|
// if (v0 != v1) {
|
|
|
|
// set_voxel(v1, pos, channel_index);
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// }
|
2019-09-02 13:14:30 -07:00
|
|
|
|
2019-09-06 15:24:56 -07:00
|
|
|
#ifdef TOOLS_ENABLED
|
|
|
|
|
|
|
|
// Debugging
|
|
|
|
Ref<Image> debug_print_sdf_to_image_top_down();
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2016-05-01 06:00:02 -07:00
|
|
|
private:
|
2016-12-31 19:40:16 -08:00
|
|
|
void create_channel_noinit(int i, Vector3i size);
|
2019-04-22 12:15:19 -07:00
|
|
|
void create_channel(int i, Vector3i size, uint8_t defval);
|
2017-01-04 17:39:40 -08:00
|
|
|
void delete_channel(int i);
|
2016-05-01 06:00:02 -07:00
|
|
|
|
2016-05-07 17:12:47 -07:00
|
|
|
protected:
|
2016-12-31 19:40:16 -08:00
|
|
|
static void _bind_methods();
|
|
|
|
|
|
|
|
_FORCE_INLINE_ int get_size_x() const { return _size.x; }
|
|
|
|
_FORCE_INLINE_ int get_size_y() const { return _size.y; }
|
|
|
|
_FORCE_INLINE_ int get_size_z() const { return _size.z; }
|
2019-09-06 15:24:56 -07:00
|
|
|
|
|
|
|
// Bindings
|
|
|
|
_FORCE_INLINE_ Vector3 _b_get_size() const { return _size.to_vec3(); }
|
|
|
|
void _b_create(int x, int y, int z) { create(x, y, z); }
|
|
|
|
_FORCE_INLINE_ int _b_get_voxel(int x, int y, int z, unsigned int channel) const { return get_voxel(x, y, z, channel); }
|
|
|
|
_FORCE_INLINE_ void _b_set_voxel(int value, int x, int y, int z, unsigned int channel) { set_voxel(value, x, y, z, channel); }
|
|
|
|
void _b_copy_from(Ref<VoxelBuffer> other, unsigned int channel);
|
|
|
|
void _b_copy_from_area(Ref<VoxelBuffer> other, Vector3 src_min, Vector3 src_max, Vector3 dst_min, unsigned int channel);
|
|
|
|
_FORCE_INLINE_ void _b_fill_area(int defval, Vector3 min, Vector3 max, unsigned int channel_index) { fill_area(defval, Vector3i(min), Vector3i(max), channel_index); }
|
|
|
|
_FORCE_INLINE_ void _b_set_voxel_f(real_t value, int x, int y, int z, unsigned int channel) { set_voxel_f(value, x, y, z, channel); }
|
|
|
|
void _b_set_voxel_v(int value, Vector3 pos, unsigned int channel_index = 0) { set_voxel(value, pos.x, pos.y, pos.z, channel_index); }
|
2016-05-05 12:08:52 -07:00
|
|
|
|
2017-01-01 17:19:02 -08:00
|
|
|
private:
|
|
|
|
struct Channel {
|
|
|
|
// Allocated when the channel is populated.
|
|
|
|
// Flat array, in order [z][x][y] because it allows faster vertical-wise access (the engine is Y-up).
|
2017-08-12 16:19:39 -07:00
|
|
|
uint8_t *data;
|
2017-01-01 17:19:02 -08:00
|
|
|
|
|
|
|
// Default value when data is null
|
|
|
|
uint8_t defval;
|
|
|
|
|
2019-04-20 09:32:10 -07:00
|
|
|
Channel() :
|
|
|
|
data(NULL),
|
|
|
|
defval(0) {}
|
2017-01-01 17:19:02 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
// Each channel can store arbitary data.
|
|
|
|
// For example, you can decide to store colors (R, G, B, A), gameplay types (type, state, light) or both.
|
|
|
|
Channel _channels[MAX_CHANNELS];
|
|
|
|
|
|
|
|
// How many voxels are there in the three directions. All populated channels have the same size.
|
|
|
|
Vector3i _size;
|
2016-05-01 06:00:02 -07:00
|
|
|
};
|
|
|
|
|
2019-04-21 11:31:35 -07:00
|
|
|
VARIANT_ENUM_CAST(VoxelBuffer::ChannelId)
|
|
|
|
|
2016-05-01 06:00:02 -07:00
|
|
|
#endif // VOXEL_BUFFER_H
|