2019-05-03 16:02:10 -07:00
|
|
|
#ifndef VOXEL_LOD_TERRAIN_HPP
|
|
|
|
#define VOXEL_LOD_TERRAIN_HPP
|
|
|
|
|
|
|
|
#include "lod_octree.h"
|
2019-05-25 07:07:16 -07:00
|
|
|
#include "voxel_data_loader.h"
|
2019-05-03 16:02:10 -07:00
|
|
|
#include "voxel_mesh_updater.h"
|
|
|
|
#include <core/set.h>
|
|
|
|
#include <scene/3d/spatial.h>
|
|
|
|
|
|
|
|
class VoxelMap;
|
2019-09-05 11:43:25 -07:00
|
|
|
class VoxelTool;
|
|
|
|
class VoxelStream;
|
2019-09-06 15:24:56 -07:00
|
|
|
class VoxelBlock;
|
2019-05-03 16:02:10 -07:00
|
|
|
|
|
|
|
// Paged terrain made of voxel blocks of variable level of detail.
|
|
|
|
// Designed for highest view distances, preferably using smooth voxels.
|
|
|
|
// Voxels are polygonized around the viewer by distance in a very large sphere, usually extending beyond far clip.
|
2019-05-25 08:07:38 -07:00
|
|
|
// Data is streamed using a VoxelStream, which must support LOD.
|
2019-05-03 16:02:10 -07:00
|
|
|
class VoxelLodTerrain : public Spatial {
|
|
|
|
GDCLASS(VoxelLodTerrain, Spatial)
|
|
|
|
public:
|
|
|
|
VoxelLodTerrain();
|
|
|
|
~VoxelLodTerrain();
|
|
|
|
|
|
|
|
Ref<Material> get_material() const;
|
|
|
|
void set_material(Ref<Material> p_material);
|
|
|
|
|
2019-05-25 08:07:38 -07:00
|
|
|
Ref<VoxelStream> get_stream() const;
|
|
|
|
void set_stream(Ref<VoxelStream> p_stream);
|
2019-05-03 16:02:10 -07:00
|
|
|
|
|
|
|
int get_view_distance() const;
|
2019-09-03 14:56:57 -07:00
|
|
|
void set_view_distance(int p_distance_in_voxels);
|
2019-05-03 16:02:10 -07:00
|
|
|
|
|
|
|
void set_lod_split_scale(float p_lod_split_scale);
|
|
|
|
float get_lod_split_scale() const;
|
|
|
|
|
2019-08-23 17:44:27 -07:00
|
|
|
void set_lod_count(int p_lod_count);
|
|
|
|
int get_lod_count() const;
|
2019-05-03 16:02:10 -07:00
|
|
|
|
2019-08-25 05:04:49 -07:00
|
|
|
void set_generate_collisions(bool enabled);
|
|
|
|
bool get_generate_collisions() const { return _generate_collisions; }
|
|
|
|
|
|
|
|
// Sets up to which amount of LODs collision will generate. -1 means all of them.
|
|
|
|
void set_collision_lod_count(int lod_count);
|
|
|
|
int get_collision_lod_count() const;
|
|
|
|
|
2019-05-03 16:02:10 -07:00
|
|
|
void set_viewer_path(NodePath path);
|
|
|
|
NodePath get_viewer_path() const;
|
|
|
|
|
2019-05-07 16:57:34 -07:00
|
|
|
int get_block_region_extent() const;
|
2019-08-31 17:47:45 -07:00
|
|
|
Vector3 voxel_to_block_position(Vector3 vpos, int lod_index) const;
|
2019-05-03 16:02:10 -07:00
|
|
|
|
2019-08-23 17:44:27 -07:00
|
|
|
unsigned int get_block_size_pow2() const;
|
|
|
|
void set_block_size_po2(unsigned int p_block_size_po2);
|
|
|
|
|
2019-09-03 14:54:40 -07:00
|
|
|
// These must be called after an edit
|
|
|
|
void post_edit_area(Rect3i p_box);
|
|
|
|
void post_edit_block_lod0(Vector3i bpos);
|
|
|
|
|
|
|
|
Ref<VoxelTool> get_voxel_tool();
|
|
|
|
|
2019-05-04 17:09:12 -07:00
|
|
|
struct Stats {
|
|
|
|
VoxelMeshUpdater::Stats updater;
|
2019-05-27 17:10:50 -07:00
|
|
|
VoxelDataLoader::Stats stream;
|
2019-08-25 10:47:43 -07:00
|
|
|
int blocked_lods = 0;
|
|
|
|
int updated_blocks = 0;
|
|
|
|
int dropped_block_loads = 0;
|
|
|
|
int dropped_block_meshs = 0;
|
|
|
|
uint64_t time_detect_required_blocks = 0;
|
2019-05-04 17:09:12 -07:00
|
|
|
uint64_t time_request_blocks_to_load = 0;
|
|
|
|
uint64_t time_process_load_responses = 0;
|
|
|
|
uint64_t time_request_blocks_to_update = 0;
|
|
|
|
uint64_t time_process_update_responses = 0;
|
|
|
|
};
|
|
|
|
|
2019-08-25 09:40:19 -07:00
|
|
|
Dictionary get_statistics() const;
|
2020-01-15 13:04:23 -08:00
|
|
|
|
2020-01-05 15:27:43 -08:00
|
|
|
Array debug_raycast_block(Vector3 world_origin, Vector3 world_direction) const;
|
2020-01-15 13:04:23 -08:00
|
|
|
Array debug_get_last_unexpected_block_drops() const;
|
|
|
|
Dictionary debug_get_block_info(Vector3 fbpos, int lod_index) const;
|
2020-01-22 16:37:13 -08:00
|
|
|
Array debug_get_octrees() const;
|
2019-05-04 17:09:12 -07:00
|
|
|
|
2019-05-03 16:02:10 -07:00
|
|
|
protected:
|
|
|
|
static void _bind_methods();
|
|
|
|
|
|
|
|
void _notification(int p_what);
|
|
|
|
void _process();
|
|
|
|
|
|
|
|
private:
|
2019-06-17 22:24:56 -07:00
|
|
|
unsigned int get_block_size() const;
|
2019-05-03 16:02:10 -07:00
|
|
|
Spatial *get_viewer() const;
|
2019-08-31 17:47:45 -07:00
|
|
|
void immerge_block(Vector3i block_pos, int lod_index);
|
2019-08-23 17:44:27 -07:00
|
|
|
|
|
|
|
void start_updater();
|
|
|
|
void stop_updater();
|
|
|
|
void start_streamer();
|
|
|
|
void stop_streamer();
|
|
|
|
void reset_maps();
|
|
|
|
|
2019-09-07 13:19:12 -07:00
|
|
|
void get_viewer_pos_and_direction(Vector3 &out_viewer_pos, Vector3 &out_direction) const;
|
2019-08-31 17:47:45 -07:00
|
|
|
void try_schedule_loading_with_neighbors(const Vector3i &p_bpos, int lod_index);
|
|
|
|
bool check_block_loaded_and_updated(const Vector3i &p_bpos, int lod_index);
|
2020-01-23 15:20:23 -08:00
|
|
|
bool check_block_mesh_updated(VoxelBlock *block);
|
2019-08-23 17:44:27 -07:00
|
|
|
void _set_lod_count(int p_lod_count);
|
|
|
|
void _set_block_size_po2(int p_block_size_po2);
|
|
|
|
|
|
|
|
void _on_stream_params_changed();
|
2019-05-03 16:02:10 -07:00
|
|
|
|
2019-09-03 14:54:40 -07:00
|
|
|
void flush_pending_lod_edits();
|
2019-09-07 13:19:12 -07:00
|
|
|
void save_all_modified_blocks(bool with_copy);
|
|
|
|
void send_block_data_requests();
|
2019-09-03 14:54:40 -07:00
|
|
|
|
2019-12-31 08:48:46 -08:00
|
|
|
void add_transition_update(VoxelBlock *block);
|
|
|
|
void add_transition_updates_around(Vector3i block_pos, int lod_index);
|
|
|
|
void process_transition_updates();
|
2020-01-06 16:22:58 -08:00
|
|
|
uint8_t get_transition_mask(Vector3i block_pos, int lod_index) const;
|
2019-12-31 08:48:46 -08:00
|
|
|
|
2019-08-29 14:55:02 -07:00
|
|
|
struct OctreeItem {
|
2020-01-22 16:37:13 -08:00
|
|
|
LodOctree octree;
|
2019-08-29 14:55:02 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
// This terrain type is a sparse grid of octrees.
|
|
|
|
// Indexed by a grid coordinate whose step is the size of the highest-LOD block
|
2019-05-03 16:02:10 -07:00
|
|
|
// This octree doesn't hold any data... hence bool.
|
2019-08-29 14:55:02 -07:00
|
|
|
Map<Vector3i, OctreeItem> _lod_octrees;
|
2019-09-01 16:44:28 -07:00
|
|
|
Rect3i _last_octree_region_box;
|
2019-05-03 16:02:10 -07:00
|
|
|
|
|
|
|
NodePath _viewer_path;
|
|
|
|
|
2019-05-25 08:07:38 -07:00
|
|
|
Ref<VoxelStream> _stream;
|
|
|
|
VoxelDataLoader *_stream_thread = nullptr;
|
2019-05-03 16:02:10 -07:00
|
|
|
VoxelMeshUpdater *_block_updater = nullptr;
|
|
|
|
std::vector<VoxelMeshUpdater::OutputBlock> _blocks_pending_main_thread_update;
|
2019-09-07 13:19:12 -07:00
|
|
|
std::vector<VoxelDataLoader::InputBlock> _blocks_to_save;
|
2019-05-03 16:02:10 -07:00
|
|
|
|
2019-12-31 08:48:46 -08:00
|
|
|
// Only populated and then cleared inside _process, so lifetime of pointers should be valid
|
|
|
|
std::vector<VoxelBlock *> _blocks_pending_transition_update;
|
|
|
|
|
2019-05-03 16:02:10 -07:00
|
|
|
Ref<Material> _material;
|
2020-01-02 12:29:40 -08:00
|
|
|
std::vector<Ref<ShaderMaterial> > _shader_material_pool;
|
2019-05-03 16:02:10 -07:00
|
|
|
|
2019-08-25 05:04:49 -07:00
|
|
|
bool _generate_collisions = true;
|
|
|
|
int _collision_lod_count = -1;
|
|
|
|
|
2019-05-03 16:02:10 -07:00
|
|
|
// Each LOD works in a set of coordinates spanning 2x more voxels the higher their index is
|
|
|
|
struct Lod {
|
|
|
|
Ref<VoxelMap> map;
|
2019-05-09 11:11:17 -07:00
|
|
|
Set<Vector3i> loading_blocks;
|
2019-05-03 16:02:10 -07:00
|
|
|
std::vector<Vector3i> blocks_pending_update;
|
|
|
|
|
2019-09-03 14:54:40 -07:00
|
|
|
// Blocks that were edited and need their LOD counterparts to be updated
|
|
|
|
std::vector<Vector3i> blocks_pending_lodding;
|
|
|
|
|
2019-05-03 16:02:10 -07:00
|
|
|
// These are relative to this LOD, in block coordinates
|
|
|
|
Vector3i last_viewer_block_pos;
|
|
|
|
int last_view_distance_blocks = 0;
|
|
|
|
|
|
|
|
// Members for memory caching
|
|
|
|
std::vector<Vector3i> blocks_to_load;
|
2019-05-20 12:49:46 -07:00
|
|
|
|
|
|
|
#ifdef TOOLS_ENABLED
|
|
|
|
// TODO Debug, may be removed in the future
|
2020-01-15 13:04:23 -08:00
|
|
|
std::vector<Vector3i> debug_unexpected_block_drops;
|
2019-05-20 12:49:46 -07:00
|
|
|
#endif
|
2019-05-03 16:02:10 -07:00
|
|
|
};
|
|
|
|
|
2020-01-15 13:12:10 -08:00
|
|
|
FixedArray<Lod, VoxelConstants::MAX_LOD> _lods;
|
2019-08-29 14:55:02 -07:00
|
|
|
int _lod_count = 0;
|
|
|
|
float _lod_split_scale = 0.f;
|
|
|
|
unsigned int _view_distance_voxels = 512;
|
2019-05-04 17:09:12 -07:00
|
|
|
|
|
|
|
Stats _stats;
|
2019-05-03 16:02:10 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif // VOXEL_LOD_TERRAIN_HPP
|