godot_voxel/streams/instance_data.h
Marc Gilleron 1f128a7803 Fix some issues with persistent instance positions
- Scene instances were not made relative to their block
- Saving a block while mesh size is 16 and loading it back while mesh size
  is increased to 32 should now work. Before, positions were saved
  relative to render block instead of data block, which forced to
  keep mesh and data sizes the same
- Fixed incorrect octant_index in save_block, Z shift was 1 instead of 2
2022-07-24 01:00:02 +01:00

64 lines
1.8 KiB
C++

#ifndef VOXEL_INSTANCE_DATA_H
#define VOXEL_INSTANCE_DATA_H
#include "../util/span.h"
#include <core/math/transform_3d.h>
namespace zylann::voxel {
// Stores data to pass around until it either gets saved or turned into actual instances
struct InstanceBlockData {
struct InstanceData {
// Transform of the instance, relative to the origin of the data block.
Transform3D transform;
};
enum VoxelInstanceFormat {
// Position is lossy-compressed based on the size of the block
// - uint16_t x;
// - uint16_t y;
// - uint16_t z;
//
// Scale is uniform and is lossy-compressed to 256 values
// - uint8_t scale;
//
// Rotation is a compressed quaternion with naive quantization of its members to 256 values
// - uint8_t x;
// - uint8_t y;
// - uint8_t z;
// - uint8_t w;
FORMAT_SIMPLE_11B_V1 = 0
};
static const int POSITION_RESOLUTION = 65536;
// Because position is quantized we need its range, but it cannot be zero so it may be clamped to this.
static const float POSITION_RANGE_MINIMUM;
static const int SIMPLE_11B_V1_SCALE_RESOLUTION = 256;
static const int SIMPLE_11B_V1_QUAT_RESOLUTION = 256;
// Because scale is quantized we need its range, but it cannot be zero so it may be clamped to this.
static const float SIMPLE_11B_V1_SCALE_RANGE_MINIMUM;
struct LayerData {
uint16_t id;
float scale_min;
float scale_max;
std::vector<InstanceData> instances;
};
float position_range;
std::vector<LayerData> layers;
void copy_to(InstanceBlockData &dst) const {
// It's all POD so it should work for now
dst = *this;
}
};
bool serialize_instance_block_data(const InstanceBlockData &src, std::vector<uint8_t> &dst);
bool deserialize_instance_block_data(InstanceBlockData &dst, Span<const uint8_t> src);
} // namespace zylann::voxel
#endif // VOXEL_INSTANCE_DATA_H