forked from Zylann/godot_heightmap_module
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathheight_map_chunk.cpp
121 lines (96 loc) · 4.18 KB
/
height_map_chunk.cpp
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
112
113
114
115
116
117
118
119
120
121
#include "height_map_chunk.h"
HeightMapChunk::HeightMapChunk(Spatial *p_parent, Point2i p_cell_pos, Ref<Material> p_material, Ref<Material> decel_material) {
cell_origin = p_cell_pos;
VisualServer &vs = *VisualServer::get_singleton();
_mesh_instance = vs.instance_create();
parent_transform_changed(p_parent->get_global_transform());
if (p_material.is_valid()) {
vs.instance_geometry_set_material_override(_mesh_instance, p_material->get_rid());
}
Ref<World> world = p_parent->get_world();
if (world.is_valid()) {
vs.instance_set_scenario(_mesh_instance, world->get_scenario());
}
// TODO Is this needed?
vs.instance_set_visible(_mesh_instance, true);
_visible = true;
_active = true;
_dirty = true;
_pending_update = false;
_decel_mesh_instance = vs.instance_create();
parent_transform_changed(p_parent->get_global_transform());
if (decel_material.is_valid()) {
vs.instance_geometry_set_material_override(_decel_mesh_instance, decel_material->get_rid());
}
if (world.is_valid()) {
vs.instance_set_scenario(_decel_mesh_instance, world->get_scenario());
}
vs.instance_set_visible(_decel_mesh_instance, true);
}
HeightMapChunk::~HeightMapChunk() {
VisualServer &vs = *VisualServer::get_singleton();
if (_mesh_instance.is_valid()) {
vs.free(_mesh_instance);
_mesh_instance = RID();
}
if (_decel_mesh_instance.is_valid()) {
vs.free(_decel_mesh_instance);
_decel_mesh_instance = RID();
}
// if(collider)
// collider->queue_delete();
}
void HeightMapChunk::enter_world(World &world) {
ERR_FAIL_COND(_mesh_instance.is_valid() == false);
VisualServer &vs = *VisualServer::get_singleton();
vs.instance_set_scenario(_mesh_instance, world.get_scenario());
ERR_FAIL_COND(_decel_mesh_instance.is_valid() == false);
vs.instance_set_scenario(_decel_mesh_instance, world.get_scenario());
}
void HeightMapChunk::exit_world() {
ERR_FAIL_COND(_mesh_instance.is_valid() == false);
VisualServer &vs = *VisualServer::get_singleton();
vs.instance_set_scenario(_mesh_instance, RID());
ERR_FAIL_COND(_decel_mesh_instance.is_valid() == false);
vs.instance_set_scenario(_decel_mesh_instance, RID());
}
void HeightMapChunk::parent_transform_changed(const Transform &parent_transform) {
ERR_FAIL_COND(_mesh_instance.is_valid() == false);
VisualServer &vs = *VisualServer::get_singleton();
Transform local_transform(Basis(), Vector3(cell_origin.x, 0, cell_origin.y));
Transform world_transform = parent_transform * local_transform;
vs.instance_set_transform(_mesh_instance, world_transform);
vs.instance_set_transform(_decel_mesh_instance, world_transform);
}
void HeightMapChunk::set_mesh(Ref<Mesh> mesh) {
ERR_FAIL_COND(_mesh_instance.is_valid() == false);
VisualServer &vs = *VisualServer::get_singleton();
vs.instance_set_base(_mesh_instance, mesh.is_valid() ? mesh->get_rid() : RID());
_mesh = mesh;
}
void HeightMapChunk::set_decel_mesh(Ref<Mesh> mesh) {
ERR_FAIL_COND(_decel_mesh_instance.is_valid() == false);
VisualServer &vs = *VisualServer::get_singleton();
vs.instance_set_base(_decel_mesh_instance, mesh.is_valid() ? mesh->get_rid() : RID());
_decel_mesh = mesh;
}
void HeightMapChunk::set_material(Ref<Material> material) {
ERR_FAIL_COND(_mesh_instance.is_valid() == false);
VisualServer &vs = *VisualServer::get_singleton();
vs.instance_geometry_set_material_override(_mesh_instance, material.is_valid() ? material->get_rid() : RID());
}
void HeightMapChunk::set_decel_material(Ref<Material> material) {
ERR_FAIL_COND(_decel_mesh_instance.is_valid() == false);
VisualServer &vs = *VisualServer::get_singleton();
vs.instance_geometry_set_material_override(_decel_mesh_instance, material.is_valid() ? material->get_rid() : RID());
}
void HeightMapChunk::set_visible(bool visible) {
ERR_FAIL_COND(_mesh_instance.is_valid() == false);
VisualServer &vs = *VisualServer::get_singleton();
vs.instance_set_visible(_mesh_instance, visible);
}
void HeightMapChunk::set_decel_visible(bool decel_visible) {
ERR_FAIL_COND(_decel_mesh_instance.is_valid() == false);
VisualServer &vs = *VisualServer::get_singleton();
vs.instance_set_visible(_decel_mesh_instance, decel_visible);
}