forked from Zylann/godot_heightmap_module
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathheight_map.h
111 lines (82 loc) · 2.89 KB
/
height_map.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#ifndef HEIGHT_MAP_H
#define HEIGHT_MAP_H
#include "height_map_chunk.h"
#include "height_map_data.h"
#include "height_map_mesher.h"
#include "quad_tree_lod.h"
#include <scene/3d/spatial.h>
// Heightmap-based 3D terrain
class HeightMap : public Spatial {
GDCLASS(HeightMap, Spatial)
public:
//static const int CHUNK_SIZE = 16;
// Workaround because GCC doesn't links the line above properly
enum { CHUNK_SIZE = 16 };
static const char *SHADER_PARAM_HEIGHT_TEXTURE;
static const char *SHADER_PARAM_NORMAL_TEXTURE;
static const char *SHADER_PARAM_COLOR_TEXTURE;
static const char *SHADER_PARAM_RESOLUTION;
static const char *SHADER_PARAM_INVERSE_TRANSFORM;
HeightMap();
~HeightMap();
void set_data(Ref<HeightMapData> data);
Ref<HeightMapData> get_data() { return _data; }
void set_custom_shader(Ref<Shader> p_shader);
inline Ref<Shader> get_custom_shader() const { return _custom_shader; }
void set_collision_enabled(bool enabled);
inline bool is_collision_enabled() const { return _collision_enabled; }
void set_lod_scale(float lod_scale);
float get_lod_scale() const;
void set_area_dirty(Point2i origin_in_cells, Point2i size_in_cells);
bool cell_raycast(Vector3 origin_world, Vector3 dir_world, Point2i &out_cell_pos);
static void init_default_resources();
static void free_default_resources();
Vector3 _manual_viewer_pos;
protected:
void _notification(int p_what);
private:
void _process();
void update_material();
HeightMapChunk *_make_chunk_cb(Point2i cpos, int lod);
void _recycle_chunk_cb(HeightMapChunk *chunk);
void add_chunk_update(HeightMapChunk &chunk, Point2i pos, int lod);
void update_chunk(HeightMapChunk &chunk, int lod);
Point2i local_pos_to_cell(Vector3 local_pos) const;
void _on_data_resolution_changed();
void _on_data_region_changed(int min_x, int min_y, int max_x, int max_y, int channel);
void clear_all_chunks();
HeightMapChunk *get_chunk_at(Point2i pos, int lod) const;
static void _bind_methods();
static HeightMapChunk *s_make_chunk_cb(void *context, Point2i origin, int lod);
static void s_recycle_chunk_cb(void *context, HeightMapChunk *chunk, Point2i origin, int lod);
template <typename Action_T>
void for_all_chunks(Action_T action) {
for(int lod = 0; lod < _chunks.size(); ++lod) {
int area = _chunks[lod].area();
for(int i = 0; i < area; ++i) {
HeightMapChunk *chunk = _chunks[lod][i];
if(chunk)
action(*chunk);
}
}
}
private:
Ref<Shader> _custom_shader;
Ref<ShaderMaterial> _material;
bool _collision_enabled;
Ref<HeightMapData> _data;
HeightMapMesher _mesher;
QuadTreeLod<HeightMapChunk *> _lodder;
struct PendingChunkUpdate {
Point2i pos;
int lod;
PendingChunkUpdate() : lod(0) {}
};
Vector<PendingChunkUpdate> _pending_chunk_updates;
// [lod][pos]
// This container owns chunks, so will be used to free them
Vector< Grid2D< HeightMapChunk* > > _chunks;
// Stats
int _updated_chunks;
};
#endif // HEIGHT_MAP_H