2017-08-20 15:48:55 +02:00
|
|
|
#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;
|
2017-08-20 18:37:08 +02:00
|
|
|
block->_position_in_voxels = bpos * size;
|
2017-08-20 15:48:55 +02:00
|
|
|
|
|
|
|
block->voxels = buffer;
|
|
|
|
//block->map = ↦
|
|
|
|
return block;
|
|
|
|
}
|
|
|
|
|
|
|
|
VoxelBlock::VoxelBlock()
|
|
|
|
: voxels(NULL) {
|
2017-08-20 18:37:08 +02:00
|
|
|
|
|
|
|
VisualServer &vs = *VisualServer::get_singleton();
|
|
|
|
|
|
|
|
if (_mesh_instance.is_valid()) {
|
|
|
|
vs.free(_mesh_instance);
|
|
|
|
_mesh_instance = RID();
|
|
|
|
}
|
2017-08-20 15:48:55 +02:00
|
|
|
}
|
|
|
|
|
2017-08-20 18:37:08 +02:00
|
|
|
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;
|
|
|
|
}
|