2019-06-07 01:33:41 +02:00
|
|
|
#ifndef VOXEL_CHUNK_H
|
|
|
|
#define VOXEL_CHUNK_H
|
|
|
|
|
2019-11-06 03:37:22 +01:00
|
|
|
#include "scene/3d/spatial.h"
|
|
|
|
|
2019-06-07 01:33:41 +02:00
|
|
|
#include "core/engine.h"
|
2019-11-19 20:39:45 +01:00
|
|
|
#include "core/os/thread.h"
|
|
|
|
#include "core/os/thread_safe.h"
|
2020-01-09 04:29:05 +01:00
|
|
|
#include "core/ustring.h"
|
2019-11-19 20:39:45 +01:00
|
|
|
|
2019-07-18 18:56:42 +02:00
|
|
|
#include "core/array.h"
|
2019-06-07 01:33:41 +02:00
|
|
|
#include "scene/3d/collision_shape.h"
|
2020-01-09 04:29:05 +01:00
|
|
|
#include "scene/3d/mesh_instance.h"
|
2019-06-07 01:33:41 +02:00
|
|
|
#include "scene/3d/physics_body.h"
|
2019-07-18 18:56:42 +02:00
|
|
|
#include "scene/3d/spatial.h"
|
2020-01-09 04:29:05 +01:00
|
|
|
#include "scene/resources/concave_polygon_shape.h"
|
|
|
|
#include "scene/resources/packed_scene.h"
|
2019-06-07 01:33:41 +02:00
|
|
|
|
2019-07-18 02:05:50 +02:00
|
|
|
#include "voxel_world.h"
|
|
|
|
|
2019-06-07 01:33:41 +02:00
|
|
|
#include "../data/voxel_light.h"
|
|
|
|
|
2019-07-17 17:01:12 +02:00
|
|
|
#include "../meshers/cubic_mesher/voxel_mesher_cubic.h"
|
2019-07-18 18:56:42 +02:00
|
|
|
#include "../meshers/voxel_mesher.h"
|
2019-06-07 01:33:41 +02:00
|
|
|
|
|
|
|
#include "../library/voxel_surface.h"
|
|
|
|
#include "../library/voxelman_library.h"
|
|
|
|
|
2019-12-24 03:22:17 +01:00
|
|
|
#include "../../mesh_data_resource/mesh_data_resource.h"
|
2019-11-07 21:32:31 +01:00
|
|
|
#include "../props/prop_data.h"
|
|
|
|
#include "../props/prop_data_entry.h"
|
|
|
|
#include "../props/prop_data_light.h"
|
2020-01-09 04:29:05 +01:00
|
|
|
#include "../props/prop_data_mesh.h"
|
|
|
|
#include "../props/prop_data_scene.h"
|
2019-10-10 23:51:05 +02:00
|
|
|
#include "voxel_chunk_prop_data.h"
|
2019-06-07 01:33:41 +02:00
|
|
|
|
2019-08-12 20:40:05 +02:00
|
|
|
class VoxelWorld;
|
|
|
|
|
2019-11-06 03:37:22 +01:00
|
|
|
class VoxelChunk : public Spatial {
|
|
|
|
GDCLASS(VoxelChunk, Spatial);
|
2019-06-07 01:33:41 +02:00
|
|
|
|
2019-11-19 20:39:45 +01:00
|
|
|
_THREAD_SAFE_CLASS_
|
|
|
|
|
2019-06-07 01:33:41 +02:00
|
|
|
public:
|
2019-11-10 03:10:42 +01:00
|
|
|
enum {
|
|
|
|
VOXEL_CHUNK_STATE_OK = 0,
|
|
|
|
VOXEL_CHUNK_STATE_GENERATION_QUEUED = 1,
|
|
|
|
VOXEL_CHUNK_STATE_GENERATION = 2,
|
|
|
|
VOXEL_CHUNK_STATE_MESH_GENERATION_QUEUED = 3,
|
|
|
|
VOXEL_CHUNK_STATE_MESH_GENERATION = 4,
|
|
|
|
VOXEL_CHUNK_STATE_MAX = 5,
|
|
|
|
};
|
|
|
|
|
|
|
|
enum {
|
|
|
|
BUILD_PHASE_DONE = 0,
|
|
|
|
BUILD_PHASE_SETUP = 1,
|
|
|
|
BUILD_PHASE_TERRARIN_MESH_SETUP = 2,
|
|
|
|
BUILD_PHASE_TERRARIN_MESH_COLLIDER = 3,
|
|
|
|
BUILD_PHASE_LIGHTS = 4,
|
|
|
|
BUILD_PHASE_TERRARIN_MESH = 5,
|
|
|
|
BUILD_PHASE_PROP_MESH = 6,
|
|
|
|
BUILD_PHASE_PROP_COLLIDER = 7,
|
|
|
|
BUILD_PHASE_LIQUID = 8,
|
|
|
|
BUILD_PHASE_CLUTTER = 9,
|
2019-11-19 00:54:28 +01:00
|
|
|
BUILD_PHASE_FINALIZE = 10,
|
|
|
|
BUILD_PHASE_MAX = 11
|
2019-11-10 03:10:42 +01:00
|
|
|
};
|
|
|
|
|
2019-11-18 20:16:29 +01:00
|
|
|
enum DefaultChannels {
|
|
|
|
DEFAULT_CHANNEL_TYPE = 0,
|
|
|
|
DEFAULT_CHANNEL_ISOLEVEL,
|
|
|
|
DEFAULT_CHANNEL_LIGHT_COLOR_R,
|
|
|
|
DEFAULT_CHANNEL_LIGHT_COLOR_G,
|
|
|
|
DEFAULT_CHANNEL_LIGHT_COLOR_B,
|
2020-01-09 04:29:05 +01:00
|
|
|
DEFAULT_CHANNEL_AO,
|
2019-11-18 20:16:29 +01:00
|
|
|
DEFAULT_CHANNEL_RANDOM_AO,
|
|
|
|
DEFAULT_CHANNEL_LIQUID_TYPES,
|
|
|
|
DEFAULT_CHANNEL_LIQUID_FILL,
|
|
|
|
DEFAULT_CHANNEL_LIQUID_FLOW,
|
|
|
|
MAX_DEFAULT_CHANNELS
|
2019-11-18 19:22:11 +01:00
|
|
|
};
|
|
|
|
|
2019-11-10 03:10:42 +01:00
|
|
|
public:
|
2019-11-19 14:42:21 +01:00
|
|
|
bool get_is_generating() const;
|
|
|
|
void set_is_generating(bool value);
|
|
|
|
|
2019-11-19 20:39:45 +01:00
|
|
|
bool get_is_build_threaded() const;
|
|
|
|
void set_is_build_threaded(bool value);
|
|
|
|
|
2019-11-10 03:10:42 +01:00
|
|
|
bool get_dirty() const;
|
|
|
|
void set_dirty(bool value);
|
|
|
|
|
|
|
|
int get_state() const;
|
|
|
|
void set_state(int value);
|
|
|
|
|
2019-11-18 20:16:29 +01:00
|
|
|
int get_position_x();
|
2019-11-18 22:22:41 +01:00
|
|
|
void set_position_x(int value);
|
2019-11-18 20:16:29 +01:00
|
|
|
int get_position_y();
|
2019-11-18 22:22:41 +01:00
|
|
|
void set_position_y(int value);
|
2019-11-18 20:16:29 +01:00
|
|
|
int get_position_z();
|
2019-11-18 22:22:41 +01:00
|
|
|
void set_position_z(int value);
|
2019-07-17 02:28:16 +02:00
|
|
|
|
2019-11-18 20:16:29 +01:00
|
|
|
int get_size_x();
|
|
|
|
int get_size_y();
|
|
|
|
int get_size_z();
|
2019-07-17 02:28:16 +02:00
|
|
|
|
2019-11-18 20:16:29 +01:00
|
|
|
int get_data_size_x();
|
|
|
|
int get_data_size_y();
|
|
|
|
int get_data_size_z();
|
2019-11-18 19:22:11 +01:00
|
|
|
|
2019-11-18 20:16:29 +01:00
|
|
|
Vector3 get_position() const;
|
|
|
|
Vector3 get_size() const;
|
2019-11-18 22:22:41 +01:00
|
|
|
void set_position(int x, int y, int z);
|
2019-11-18 19:22:11 +01:00
|
|
|
|
2019-11-18 19:43:15 +01:00
|
|
|
int get_margin_start() const;
|
|
|
|
int get_margin_end() const;
|
2019-11-18 19:22:11 +01:00
|
|
|
|
2019-06-07 01:33:41 +02:00
|
|
|
Ref<VoxelmanLibrary> get_library();
|
|
|
|
void set_library(Ref<VoxelmanLibrary> value);
|
|
|
|
|
2019-07-17 17:01:12 +02:00
|
|
|
int get_lod_size() const;
|
|
|
|
void set_lod_size(int lod_size);
|
|
|
|
|
2019-07-17 02:28:16 +02:00
|
|
|
float get_voxel_scale() const;
|
2019-06-07 01:33:41 +02:00
|
|
|
void set_voxel_scale(float value);
|
|
|
|
|
2019-10-10 23:51:05 +02:00
|
|
|
int get_current_build_phase();
|
|
|
|
void set_current_build_phase(int value);
|
|
|
|
|
2020-01-13 00:39:55 +01:00
|
|
|
int get_max_build_phase();
|
|
|
|
void set_max_build_phase(int value);
|
|
|
|
|
|
|
|
Ref<VoxelMesher> get_mesher(int index) const;
|
|
|
|
void set_mesher(int index, Ref<VoxelMesher> mesher);
|
|
|
|
void remove_mesher(int index);
|
|
|
|
void add_mesher(Ref<VoxelMesher> mesher);
|
|
|
|
int get_mesher_count();
|
2019-06-07 01:33:41 +02:00
|
|
|
|
2019-07-18 02:05:50 +02:00
|
|
|
VoxelWorld *get_voxel_world() const;
|
|
|
|
void set_voxel_world(VoxelWorld *world);
|
|
|
|
void set_voxel_world_bind(Node *world);
|
|
|
|
|
2019-07-17 02:28:16 +02:00
|
|
|
bool get_create_collider() const;
|
2019-06-07 01:33:41 +02:00
|
|
|
void set_create_collider(bool value);
|
|
|
|
|
2019-07-17 02:28:16 +02:00
|
|
|
bool get_bake_lights() const;
|
2019-06-07 01:33:41 +02:00
|
|
|
void set_bake_lights(bool value);
|
|
|
|
|
2019-10-10 23:51:05 +02:00
|
|
|
RID get_mesh_rid();
|
|
|
|
RID get_mesh_instance_rid();
|
|
|
|
RID get_shape_rid();
|
|
|
|
RID get_body_rid();
|
|
|
|
|
|
|
|
RID get_prop_mesh_rid();
|
|
|
|
RID get_prop_mesh_instance_rid();
|
|
|
|
RID get_prop_shape_rid();
|
|
|
|
RID get_prop_body_rid();
|
|
|
|
|
2019-11-10 13:42:59 +01:00
|
|
|
RID get_liquid_mesh_rid();
|
|
|
|
RID get_liquid_mesh_instance_rid();
|
|
|
|
|
|
|
|
RID get_clutter_mesh_rid();
|
|
|
|
RID get_clutter_mesh_instance_rid();
|
|
|
|
|
2019-11-18 23:50:06 +01:00
|
|
|
//Voxel Data
|
|
|
|
void setup_channels();
|
|
|
|
void _setup_channels();
|
2019-11-18 19:43:15 +01:00
|
|
|
|
2019-11-18 23:50:06 +01:00
|
|
|
void set_size(int size_x, int size_y, int size_z, int margin_start = 0, int margin_end = 0);
|
|
|
|
|
|
|
|
bool validate_channel_data_position(uint32_t x, uint32_t y, uint32_t z) const;
|
2019-11-18 21:53:33 +01:00
|
|
|
|
|
|
|
uint8_t get_voxel(int x, int y, int z, int channel_index) const;
|
2019-11-18 19:43:15 +01:00
|
|
|
void set_voxel(uint8_t value, int x, int y, int z, int channel_index);
|
|
|
|
|
|
|
|
void set_channel_count(int count);
|
2019-11-18 23:50:06 +01:00
|
|
|
void allocate_channel(int channel_index, uint8_t default_value = 0);
|
2019-11-18 21:53:33 +01:00
|
|
|
void fill_channel(uint8_t value, int channel_index);
|
2019-11-18 19:43:15 +01:00
|
|
|
void dealloc_channel(int channel_index);
|
|
|
|
|
|
|
|
uint8_t *get_channel(int channel_index);
|
2019-11-18 23:50:06 +01:00
|
|
|
uint8_t *get_valid_channel(int channel_index, uint8_t default_value = 0);
|
|
|
|
|
|
|
|
uint32_t get_data_index(uint32_t x, uint32_t y, uint32_t z) const;
|
|
|
|
uint32_t get_data_size() const;
|
2020-01-09 04:29:05 +01:00
|
|
|
|
2019-11-18 19:43:15 +01:00
|
|
|
//Data Management functions
|
2019-11-18 19:22:11 +01:00
|
|
|
void generate_ao();
|
|
|
|
|
2020-01-09 04:29:05 +01:00
|
|
|
void add_light(int local_x, int local_y, int local_z, int size, Color color);
|
2019-11-18 21:53:33 +01:00
|
|
|
void clear_baked_lights();
|
2019-11-18 19:22:11 +01:00
|
|
|
|
2019-10-10 23:51:05 +02:00
|
|
|
//Meshing
|
2020-01-13 00:39:55 +01:00
|
|
|
void create_meshers();
|
|
|
|
void _create_meshers();
|
2019-06-08 19:47:17 +02:00
|
|
|
|
2019-06-07 01:33:41 +02:00
|
|
|
void finalize_mesh();
|
|
|
|
|
|
|
|
void build();
|
2019-11-19 20:39:45 +01:00
|
|
|
static void _build_phase_threaded(void *_userdata);
|
|
|
|
void build_phase();
|
2019-10-10 23:51:05 +02:00
|
|
|
void _build_phase(int phase);
|
|
|
|
void next_phase();
|
2020-01-09 04:29:05 +01:00
|
|
|
|
2019-07-19 23:54:56 +02:00
|
|
|
void clear();
|
2019-06-07 01:33:41 +02:00
|
|
|
|
2019-10-10 23:51:05 +02:00
|
|
|
//Colliders
|
2019-07-18 01:43:58 +02:00
|
|
|
void create_colliders();
|
|
|
|
void build_collider();
|
|
|
|
void remove_colliders();
|
2019-06-07 01:33:41 +02:00
|
|
|
|
2019-10-10 23:51:05 +02:00
|
|
|
//lights
|
2019-07-17 02:28:16 +02:00
|
|
|
void add_lights(Array lights);
|
|
|
|
void add_voxel_light(Ref<VoxelLight> light);
|
2019-07-19 23:54:56 +02:00
|
|
|
void create_voxel_light(const Color color, const int size, const int x, const int y, const int z);
|
2019-07-17 02:28:16 +02:00
|
|
|
void remove_voxel_light(Ref<VoxelLight> light);
|
|
|
|
void clear_voxel_lights();
|
|
|
|
|
2019-07-19 23:54:56 +02:00
|
|
|
void add_lights_into(Array target);
|
2020-01-09 04:29:05 +01:00
|
|
|
void add_unique_lights_into(Array target);
|
|
|
|
Array get_lights();
|
2019-07-17 02:28:16 +02:00
|
|
|
|
2020-01-09 04:29:05 +01:00
|
|
|
void bake_lights();
|
2019-07-17 02:28:16 +02:00
|
|
|
void bake_light(Ref<VoxelLight> light);
|
2020-01-09 04:29:05 +01:00
|
|
|
|
|
|
|
void add_prop_light(Ref<VoxelLight> light);
|
2019-10-10 23:51:05 +02:00
|
|
|
|
|
|
|
//props
|
|
|
|
void add_prop(Ref<VoxelChunkPropData> prop);
|
|
|
|
Ref<VoxelChunkPropData> get_prop(int index);
|
|
|
|
int get_prop_count();
|
|
|
|
void remove_prop(int index);
|
2019-07-18 18:56:42 +02:00
|
|
|
void clear_props();
|
2019-07-20 14:48:56 +02:00
|
|
|
|
2020-01-09 04:29:05 +01:00
|
|
|
void process_props();
|
|
|
|
|
2019-07-19 23:54:56 +02:00
|
|
|
void build_prop_meshes();
|
2019-07-18 18:56:42 +02:00
|
|
|
void build_prop_collider();
|
2020-01-09 04:29:05 +01:00
|
|
|
void free_spawn_props();
|
2019-07-19 20:54:09 +02:00
|
|
|
|
2019-10-10 23:51:05 +02:00
|
|
|
//Meshes
|
2020-01-09 04:29:05 +01:00
|
|
|
void allocate_main_mesh();
|
2019-07-19 20:54:09 +02:00
|
|
|
void free_main_mesh();
|
2020-01-09 04:29:05 +01:00
|
|
|
|
2019-07-19 20:54:09 +02:00
|
|
|
void allocate_prop_mesh();
|
|
|
|
void free_prop_mesh();
|
2020-01-09 04:29:05 +01:00
|
|
|
|
|
|
|
void allocate_prop_colliders();
|
2019-07-19 20:54:09 +02:00
|
|
|
void free_prop_colliders();
|
2019-10-10 23:51:05 +02:00
|
|
|
|
2019-11-10 13:42:59 +01:00
|
|
|
void allocate_liquid_mesh();
|
|
|
|
void free_liquid_mesh();
|
|
|
|
|
|
|
|
void allocate_clutter_mesh();
|
|
|
|
void free_clutter_mesh();
|
|
|
|
|
2019-10-10 23:51:05 +02:00
|
|
|
//Debug
|
2019-07-18 01:43:58 +02:00
|
|
|
void create_debug_immediate_geometry();
|
|
|
|
void free_debug_immediate_geometry();
|
2019-06-07 01:33:41 +02:00
|
|
|
|
|
|
|
void draw_cross_voxels(Vector3 pos);
|
2019-07-19 23:54:56 +02:00
|
|
|
void draw_cross_voxels_fill(Vector3 pos, float fill);
|
2019-07-24 02:14:02 +02:00
|
|
|
void draw_debug_voxels(int max, Color color = Color(1, 1, 1));
|
2019-07-19 23:54:56 +02:00
|
|
|
void draw_debug_voxel_lights();
|
2019-06-07 01:33:41 +02:00
|
|
|
|
2019-10-10 23:51:05 +02:00
|
|
|
//free
|
|
|
|
void free_chunk();
|
|
|
|
|
2019-07-18 01:43:58 +02:00
|
|
|
VoxelChunk();
|
|
|
|
~VoxelChunk();
|
|
|
|
|
2019-06-07 01:33:41 +02:00
|
|
|
protected:
|
2019-11-19 20:39:45 +01:00
|
|
|
void _notification(int p_what);
|
2019-06-07 01:33:41 +02:00
|
|
|
static void _bind_methods();
|
2019-11-10 03:10:42 +01:00
|
|
|
|
2019-11-19 14:42:21 +01:00
|
|
|
bool _is_generating;
|
2019-11-19 20:39:45 +01:00
|
|
|
bool _is_build_threaded;
|
|
|
|
bool _abort_build;
|
2019-11-10 03:10:42 +01:00
|
|
|
bool _dirty;
|
|
|
|
int _state;
|
|
|
|
|
2019-10-10 23:51:05 +02:00
|
|
|
int _current_build_phase;
|
2020-01-13 00:39:55 +01:00
|
|
|
int _max_build_phases;
|
2019-06-07 01:33:41 +02:00
|
|
|
bool _enabled;
|
|
|
|
|
2019-07-18 02:05:50 +02:00
|
|
|
VoxelWorld *_voxel_world;
|
|
|
|
|
2019-11-18 20:16:29 +01:00
|
|
|
int _position_x;
|
|
|
|
int _position_y;
|
|
|
|
int _position_z;
|
2019-11-18 19:22:11 +01:00
|
|
|
|
2019-11-18 20:16:29 +01:00
|
|
|
uint32_t _size_x;
|
|
|
|
uint32_t _size_y;
|
|
|
|
uint32_t _size_z;
|
2019-11-18 19:22:11 +01:00
|
|
|
|
2019-11-18 20:16:29 +01:00
|
|
|
uint32_t _data_size_x;
|
|
|
|
uint32_t _data_size_y;
|
|
|
|
uint32_t _data_size_z;
|
2019-11-18 19:22:11 +01:00
|
|
|
|
|
|
|
uint32_t _margin_start;
|
|
|
|
uint32_t _margin_end;
|
2019-07-17 02:28:16 +02:00
|
|
|
|
2019-11-18 19:43:15 +01:00
|
|
|
Vector<uint8_t *> _channels;
|
|
|
|
|
2019-06-07 01:33:41 +02:00
|
|
|
Vector<Ref<VoxelLight> > _voxel_lights;
|
2019-07-17 17:01:12 +02:00
|
|
|
|
|
|
|
int _lod_size;
|
2019-06-07 01:33:41 +02:00
|
|
|
float _voxel_scale;
|
|
|
|
|
|
|
|
NodePath _library_path;
|
|
|
|
Ref<VoxelmanLibrary> _library;
|
2020-01-13 00:39:55 +01:00
|
|
|
Vector<Ref<VoxelMesher> > _meshers;
|
2019-06-07 01:33:41 +02:00
|
|
|
|
2019-07-18 18:56:42 +02:00
|
|
|
//voxel mesh
|
2019-07-18 01:43:58 +02:00
|
|
|
RID _mesh_rid;
|
|
|
|
RID _mesh_instance_rid;
|
|
|
|
|
|
|
|
RID _shape_rid;
|
|
|
|
RID _body_rid;
|
2019-06-07 01:33:41 +02:00
|
|
|
|
2019-07-18 18:56:42 +02:00
|
|
|
//mergeable props
|
2019-10-10 23:51:05 +02:00
|
|
|
Vector<Ref<VoxelChunkPropData> > _props;
|
2019-07-18 18:56:42 +02:00
|
|
|
|
|
|
|
RID _prop_mesh_rid;
|
|
|
|
RID _prop_mesh_instance_rid;
|
|
|
|
|
|
|
|
RID _prop_shape_rid;
|
|
|
|
RID _prop_body_rid;
|
|
|
|
|
2019-11-10 13:42:59 +01:00
|
|
|
//liquids
|
|
|
|
RID _liquid_mesh_rid;
|
|
|
|
RID _liquid_mesh_instance_rid;
|
|
|
|
|
|
|
|
//clutter
|
|
|
|
RID _clutter_mesh_rid;
|
|
|
|
RID _clutter_mesh_instance_rid;
|
|
|
|
|
2019-07-18 18:56:42 +02:00
|
|
|
//spawned props
|
|
|
|
Vector<Node *> _spawned_props;
|
|
|
|
|
|
|
|
//debug
|
2019-06-07 01:33:41 +02:00
|
|
|
ImmediateGeometry *_debug_drawer;
|
|
|
|
|
|
|
|
bool _build_mesh;
|
|
|
|
bool _create_collider;
|
|
|
|
|
|
|
|
bool _bake_lights;
|
2019-11-19 20:39:45 +01:00
|
|
|
|
|
|
|
Thread *_build_thread;
|
2019-06-07 01:33:41 +02:00
|
|
|
};
|
|
|
|
|
2019-11-18 20:16:29 +01:00
|
|
|
VARIANT_ENUM_CAST(VoxelChunk::DefaultChannels);
|
|
|
|
|
2019-06-07 01:33:41 +02:00
|
|
|
#endif
|