Repository navigation
Expand file tree
/
Copy pathvoxel_block.cpp
More file actions
86 lines (65 loc) · 2.18 KB
/
Copy pathvoxel_block.cpp
File metadata and controls
86 lines (65 loc) · 2.18 KB
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
#include "voxel_block.h"
// Helper
VoxelBlock *VoxelBlock::create(Vector3i bpos, Ref<VoxelBuffer> buffer, unsigned int size) {
const int bs = size;
ERR_FAIL_COND_V(buffer.is_null(), NULL);
ERR_FAIL_COND_V(buffer->get_size() != Vector3i(bs, bs, bs), NULL);
VoxelBlock *block = memnew(VoxelBlock);
block->pos = bpos;
block->_position_in_voxels = bpos * size;
block->voxels = buffer;
//block->map = ↦
return block;
}
VoxelBlock::VoxelBlock()
: voxels(NULL), _mesh_update_count(0) {
VisualServer &vs = *VisualServer::get_singleton();
if (_mesh_instance.is_valid()) {
vs.free(_mesh_instance);
_mesh_instance = RID();
}
}
void VoxelBlock::set_mesh(Ref<Mesh> mesh, Ref<World> world) {
VisualServer &vs = *VisualServer::get_singleton();
if(mesh.is_valid()) {
if(_mesh_instance.is_valid() == false) {
// Create instance if it doesn't exist
ERR_FAIL_COND(world.is_null());
_mesh_instance = vs.instance_create();
vs.instance_set_scenario(_mesh_instance, world->get_scenario());
}
vs.instance_set_base(_mesh_instance, mesh.is_valid() ? mesh->get_rid() : RID());
Transform local_transform(Basis(), _position_in_voxels.to_vec3());
vs.instance_set_transform(_mesh_instance, local_transform);
// TODO The day VoxelTerrain becomes a Spatial, this transform will need to be updatable separately
} else {
if(_mesh_instance.is_valid()) {
// Delete instance if it exists
vs.free(_mesh_instance);
_mesh_instance = RID();
}
}
_mesh = mesh;
++_mesh_update_count;
// if(_mesh_update_count > 1) {
// print_line(String("Block {0} was updated {1} times").format(varray(pos.to_vec3(), _mesh_update_count)));
// }
}
void VoxelBlock::enter_world(World *world) {
if(_mesh_instance.is_valid()) {
VisualServer &vs = *VisualServer::get_singleton();
vs.instance_set_scenario(_mesh_instance, world->get_scenario());
}
}
void VoxelBlock::exit_world() {
if(_mesh_instance.is_valid()) {
VisualServer &vs = *VisualServer::get_singleton();
vs.instance_set_scenario(_mesh_instance, RID());
}
}
void VoxelBlock::set_visible(bool visible) {
if(_mesh_instance.is_valid()) {
VisualServer &vs = *VisualServer::get_singleton();
vs.instance_set_visible(_mesh_instance, visible);
}
}