godot_voxel/storage/voxel_data_block.h
2022-01-31 21:29:08 +00:00

106 lines
2.7 KiB
C++

#ifndef VOXEL_DATA_BLOCK_H
#define VOXEL_DATA_BLOCK_H
#include "../storage/voxel_buffer_internal.h"
#include "../util/macros.h"
#include "../util/ref_count.h"
#include <memory>
namespace zylann::voxel {
// Stores loaded voxel data for a chunk of the volume. Mesh and colliders are stored separately.
class VoxelDataBlock {
public:
const Vector3i position;
const unsigned int lod_index = 0;
RefCount viewers;
static VoxelDataBlock *create(
Vector3i bpos, std::shared_ptr<VoxelBufferInternal> &buffer, unsigned int size, unsigned int p_lod_index) {
const int bs = size;
ERR_FAIL_COND_V(buffer == nullptr, nullptr);
ERR_FAIL_COND_V(buffer->get_size() != Vector3i(bs, bs, bs), nullptr);
return memnew(VoxelDataBlock(bpos, buffer, p_lod_index));
}
VoxelBufferInternal &get_voxels() {
#ifdef DEBUG_ENABLED
CRASH_COND(_voxels == nullptr);
#endif
return *_voxels;
}
const VoxelBufferInternal &get_voxels_const() const {
#ifdef DEBUG_ENABLED
CRASH_COND(_voxels == nullptr);
#endif
return *_voxels;
}
std::shared_ptr<VoxelBufferInternal> get_voxels_shared() const {
#ifdef DEBUG_ENABLED
CRASH_COND(_voxels == nullptr);
#endif
return _voxels;
}
void set_voxels(std::shared_ptr<VoxelBufferInternal> &buffer) {
ERR_FAIL_COND(buffer == nullptr);
_voxels = buffer;
}
void set_modified(bool modified) {
#ifdef TOOLS_ENABLED
if (_modified == false && modified) {
PRINT_VERBOSE(String("Marking block {0} as modified").format(varray(position)));
}
#endif
_modified = modified;
}
inline bool is_modified() const {
return _modified;
}
void set_needs_lodding(bool need_lodding) {
_needs_lodding = need_lodding;
}
inline bool get_needs_lodding() const {
return _needs_lodding;
}
inline void set_edited(bool edited) {
_edited = true;
}
inline bool is_edited() const {
return _edited;
}
private:
VoxelDataBlock(Vector3i bpos, std::shared_ptr<VoxelBufferInternal> &buffer, unsigned int p_lod_index) :
position(bpos), lod_index(p_lod_index), _voxels(buffer) {}
std::shared_ptr<VoxelBufferInternal> _voxels;
// The block was edited, which requires its LOD counterparts to be recomputed
bool _needs_lodding = false;
// Indicates if this block is different from the time it was loaded (should be saved)
bool _modified = false;
// Tells if the block has ever been edited.
// If `false`, the same data can be obtained by running the generator.
// Once it becomes `true`, it usually never comes back to `false` unless reverted.
bool _edited = false;
// Tells if it's worth requesting a more precise version of the data.
// Will be `true` if it's not worth it.
//bool _max_lod_hint = false;
};
} // namespace zylann::voxel
#endif // VOXEL_DATA_BLOCK_H