#pragma once #include "core/os/mutex.h" #include "core/os/thread.h" #include "core/templates/vector.h" #include "macros.h" #include "scene/main/node.h" #include "scene/main/timer.h" class TerrainChunkMesh; class TerrainModifier; class Terrain : public Node { GDCLASS(Terrain, Node); static void _bind_methods(); void ready(); void process(); void child_order_changed(); void child_entered(Node *node); void child_exiting(Node *node); void on_terrain_changed(); protected: void _notification(int what); static void generate_meshes_thread(void *terrain); public: void construct_chunk_grid(); void generate_meshes(); float height_at(Vector2 world_coordinate); void mesh_task_complete(); private: Timer *timer{ nullptr }; size_t side_length{ 200 }; size_t chunk_size{ 50 }; size_t detail{ 1 }; bool dirty{ false }; size_t pending_tasks{ 0 }; public: void set_side_length(size_t length); size_t get_side_length() const; void set_chunk_size(size_t size); size_t get_chunk_size() const; void set_detail(size_t detail); size_t get_detail() const; void set_thread_count(size_t num); size_t get_thread_count() const; private: Vector workload{}; Mutex workload_lock; bool threads_stop{ false }; Vector meshes{}; Vector modifiers{}; LocalVector threads{}; };