2016-05-10 01:59:54 +02:00
|
|
|
#include "voxel_terrain.h"
|
|
|
|
#include <scene/3d/mesh_instance.h>
|
|
|
|
#include <os/os.h>
|
2017-03-26 20:07:01 +02:00
|
|
|
#include "voxel_raycast.h"
|
2016-05-10 01:59:54 +02:00
|
|
|
|
2017-03-26 18:09:37 +02:00
|
|
|
VoxelTerrain::VoxelTerrain(): Node(), _min_y(-4), _max_y(4), _generate_collisions(true) {
|
2016-05-10 01:59:54 +02:00
|
|
|
|
2017-01-01 04:40:16 +01:00
|
|
|
_map = Ref<VoxelMap>(memnew(VoxelMap));
|
|
|
|
_mesher = Ref<VoxelMesher>(memnew(VoxelMesher));
|
2016-05-10 01:59:54 +02:00
|
|
|
}
|
|
|
|
|
2017-03-28 00:56:01 +02:00
|
|
|
Vector3i g_viewer_block_pos; // TODO UGLY! Lambdas or pointers needed...
|
|
|
|
|
|
|
|
// Sorts distance to viewer
|
|
|
|
struct BlockUpdateComparator {
|
2016-05-10 01:59:54 +02:00
|
|
|
inline bool operator()(const Vector3i & a, const Vector3i & b) const {
|
2017-03-28 00:56:01 +02:00
|
|
|
return a.distance_sq(g_viewer_block_pos) > b.distance_sq(g_viewer_block_pos);
|
2017-01-01 04:40:16 +01:00
|
|
|
}
|
2016-05-10 01:59:54 +02:00
|
|
|
};
|
|
|
|
|
2017-01-02 02:15:57 +01:00
|
|
|
void VoxelTerrain::set_provider(Ref<VoxelProvider> provider) {
|
|
|
|
_provider = provider;
|
|
|
|
}
|
|
|
|
|
|
|
|
Ref<VoxelProvider> VoxelTerrain::get_provider() {
|
|
|
|
return _provider;
|
|
|
|
}
|
|
|
|
|
2017-03-26 20:07:01 +02:00
|
|
|
Ref<VoxelLibrary> VoxelTerrain::get_voxel_library() {
|
|
|
|
return _mesher->get_library();
|
|
|
|
}
|
|
|
|
|
2017-03-26 18:09:37 +02:00
|
|
|
void VoxelTerrain::set_generate_collisions(bool enabled) {
|
|
|
|
_generate_collisions = enabled;
|
|
|
|
}
|
|
|
|
|
2017-03-28 00:56:01 +02:00
|
|
|
void VoxelTerrain::set_viewer_path(NodePath path) {
|
|
|
|
if(!path.is_empty())
|
|
|
|
ERR_FAIL_COND(get_viewer(path) == NULL);
|
|
|
|
_viewer_path = path;
|
|
|
|
}
|
|
|
|
|
|
|
|
NodePath VoxelTerrain::get_viewer_path() {
|
|
|
|
return _viewer_path;
|
|
|
|
}
|
2016-05-10 01:59:54 +02:00
|
|
|
|
2017-03-28 00:56:01 +02:00
|
|
|
Spatial * VoxelTerrain::get_viewer(NodePath path) {
|
|
|
|
if(path.is_empty())
|
|
|
|
return NULL;
|
|
|
|
Node * node = get_node(path);
|
|
|
|
if(node == NULL)
|
|
|
|
return NULL;
|
|
|
|
return node->cast_to<Spatial>();
|
|
|
|
}
|
|
|
|
|
|
|
|
//void VoxelTerrain::clear_update_queue() {
|
|
|
|
// _block_update_queue.clear();
|
|
|
|
// _dirty_blocks.clear();
|
|
|
|
//}
|
2016-05-10 01:59:54 +02:00
|
|
|
|
2017-03-28 00:56:01 +02:00
|
|
|
void VoxelTerrain::make_block_dirty(Vector3i bpos) {
|
|
|
|
if(_dirty_blocks.has(bpos) == false) {
|
|
|
|
_block_update_queue.push_back(bpos);
|
|
|
|
_dirty_blocks[bpos] = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void VoxelTerrain::make_blocks_dirty(Vector3i min, Vector3i size) {
|
|
|
|
Vector3i max = min + size;
|
2017-01-01 04:40:16 +01:00
|
|
|
Vector3i pos;
|
2017-03-28 00:56:01 +02:00
|
|
|
for(pos.z = min.z; pos.z < max.z; ++pos.z) {
|
|
|
|
for(pos.y = min.y; pos.y < max.y; ++pos.y) {
|
|
|
|
for(pos.x = min.x; pos.x < max.x; ++pos.x) {
|
|
|
|
make_block_dirty(pos);
|
2017-01-01 04:40:16 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-05-10 01:59:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int VoxelTerrain::get_block_update_count() {
|
2017-01-01 04:40:16 +01:00
|
|
|
return _block_update_queue.size();
|
2016-05-10 01:59:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void VoxelTerrain::_notification(int p_what) {
|
|
|
|
|
2017-01-01 04:40:16 +01:00
|
|
|
switch (p_what) {
|
2016-05-10 01:59:54 +02:00
|
|
|
|
2017-01-01 04:40:16 +01:00
|
|
|
case NOTIFICATION_ENTER_TREE:
|
|
|
|
set_process(true);
|
|
|
|
break;
|
2016-05-10 01:59:54 +02:00
|
|
|
|
2017-01-01 04:40:16 +01:00
|
|
|
case NOTIFICATION_PROCESS:
|
|
|
|
_process();
|
|
|
|
break;
|
2016-05-10 01:59:54 +02:00
|
|
|
|
2017-01-01 04:40:16 +01:00
|
|
|
case NOTIFICATION_EXIT_TREE:
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2016-05-10 01:59:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void VoxelTerrain::_process() {
|
2017-01-01 04:40:16 +01:00
|
|
|
update_blocks();
|
2016-05-10 01:59:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void VoxelTerrain::update_blocks() {
|
2017-01-01 04:40:16 +01:00
|
|
|
OS & os = *OS::get_singleton();
|
|
|
|
|
|
|
|
uint32_t time_before = os.get_ticks_msec();
|
|
|
|
uint32_t max_time = 1000 / 60;
|
|
|
|
|
2017-03-28 00:56:01 +02:00
|
|
|
// Get viewer location
|
|
|
|
Spatial * viewer = get_viewer(_viewer_path);
|
|
|
|
if(viewer)
|
|
|
|
g_viewer_block_pos = VoxelMap::voxel_to_block(viewer->get_translation());
|
|
|
|
else
|
|
|
|
g_viewer_block_pos = Vector3i();
|
|
|
|
|
|
|
|
// Sort updates so nearest blocks are done first
|
|
|
|
_block_update_queue.sort_custom<BlockUpdateComparator>();
|
|
|
|
|
|
|
|
// Update a bunch of blocks until none are left or too much time elapsed
|
2017-01-01 04:40:16 +01:00
|
|
|
while (!_block_update_queue.empty() && (os.get_ticks_msec() - time_before) < max_time) {
|
2017-03-28 00:56:01 +02:00
|
|
|
|
2017-01-05 02:39:40 +01:00
|
|
|
//printf("Remaining: %i\n", _block_update_queue.size());
|
2017-01-01 04:40:16 +01:00
|
|
|
|
|
|
|
// TODO Move this to a thread
|
|
|
|
// TODO Have VoxelTerrainGenerator in C++
|
|
|
|
|
|
|
|
// Get request
|
|
|
|
Vector3i block_pos = _block_update_queue[_block_update_queue.size() - 1];
|
|
|
|
|
|
|
|
if (!_map->has_block(block_pos)) {
|
|
|
|
// Create buffer
|
2017-01-02 02:15:57 +01:00
|
|
|
if(!_provider.is_null()) {
|
2017-01-05 02:39:40 +01:00
|
|
|
Ref<VoxelBuffer> buffer_ref = Ref<VoxelBuffer>(memnew(VoxelBuffer));
|
|
|
|
const Vector3i block_size(VoxelBlock::SIZE, VoxelBlock::SIZE, VoxelBlock::SIZE);
|
|
|
|
buffer_ref->create(block_size.x, block_size.y, block_size.z);
|
|
|
|
|
|
|
|
// Query voxel provider
|
2017-01-02 02:15:57 +01:00
|
|
|
_provider->emerge_block(buffer_ref, block_pos);
|
2017-01-05 02:39:40 +01:00
|
|
|
|
|
|
|
// Check script return
|
|
|
|
ERR_FAIL_COND(buffer_ref->get_size() != block_size);
|
|
|
|
|
|
|
|
// Store buffer
|
|
|
|
_map->set_block_buffer(block_pos, buffer_ref);
|
2017-01-02 02:15:57 +01:00
|
|
|
}
|
2017-01-05 02:39:40 +01:00
|
|
|
}
|
2017-01-01 04:40:16 +01:00
|
|
|
|
2017-01-05 02:39:40 +01:00
|
|
|
// Update meshes
|
|
|
|
Vector3i ndir;
|
|
|
|
for (ndir.z = -1; ndir.z < 2; ++ndir.z) {
|
|
|
|
for (ndir.x = -1; ndir.x < 2; ++ndir.x) {
|
|
|
|
for (ndir.y = -1; ndir.y < 2; ++ndir.y) {
|
|
|
|
Vector3i npos = block_pos + ndir;
|
|
|
|
// TODO What if the map is really composed of empty blocks?
|
|
|
|
if (_map->is_block_surrounded(npos)) {
|
|
|
|
update_block_mesh(npos);
|
2017-01-01 04:40:16 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-01-05 02:39:40 +01:00
|
|
|
//update_block_mesh(block_pos);
|
2017-01-01 04:40:16 +01:00
|
|
|
|
|
|
|
// Pop request
|
|
|
|
_block_update_queue.resize(_block_update_queue.size() - 1);
|
2017-03-28 00:56:01 +02:00
|
|
|
_dirty_blocks.erase(block_pos);
|
2017-01-01 04:40:16 +01:00
|
|
|
}
|
2016-05-10 01:59:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void VoxelTerrain::update_block_mesh(Vector3i block_pos) {
|
2017-01-03 02:50:19 +01:00
|
|
|
VoxelBlock * block = _map->get_block(block_pos);
|
|
|
|
if (block == NULL) {
|
2017-01-01 04:40:16 +01:00
|
|
|
return;
|
|
|
|
}
|
2017-01-03 02:50:19 +01:00
|
|
|
if (block->voxels->is_uniform(0) && block->voxels->get_voxel(0, 0, 0, 0) == 0) {
|
2017-01-01 04:40:16 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create buffer padded with neighbor voxels
|
|
|
|
VoxelBuffer nbuffer;
|
|
|
|
nbuffer.create(VoxelBlock::SIZE + 2, VoxelBlock::SIZE + 2, VoxelBlock::SIZE + 2);
|
|
|
|
_map->get_buffer_copy(VoxelMap::block_to_voxel(block_pos) - Vector3i(1, 1, 1), nbuffer);
|
|
|
|
|
2017-03-26 18:09:37 +02:00
|
|
|
Vector3 block_node_pos = VoxelMap::block_to_voxel(block_pos).to_vec3();
|
|
|
|
|
2017-01-01 04:40:16 +01:00
|
|
|
// Build mesh (that part is the most CPU-intensive)
|
|
|
|
Ref<Mesh> mesh = _mesher->build(nbuffer);
|
|
|
|
|
2017-03-28 00:56:01 +02:00
|
|
|
// TODO Don't use nodes! Use servers directly, it's faster
|
|
|
|
|
2017-01-03 02:50:19 +01:00
|
|
|
MeshInstance * mesh_instance = block->get_mesh_instance(*this);
|
2017-01-01 04:40:16 +01:00
|
|
|
if (mesh_instance == NULL) {
|
|
|
|
// Create and spawn mesh
|
|
|
|
mesh_instance = memnew(MeshInstance);
|
|
|
|
mesh_instance->set_mesh(mesh);
|
2017-03-26 18:09:37 +02:00
|
|
|
mesh_instance->set_translation(block_node_pos);
|
2017-01-01 04:40:16 +01:00
|
|
|
add_child(mesh_instance);
|
2017-01-03 02:50:19 +01:00
|
|
|
block->mesh_instance_path = mesh_instance->get_path();
|
2017-01-01 04:40:16 +01:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
// Update mesh
|
|
|
|
mesh_instance->set_mesh(mesh);
|
2017-03-26 18:09:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if(get_tree()->is_editor_hint() == false && _generate_collisions) {
|
|
|
|
|
|
|
|
// Generate collisions
|
|
|
|
// TODO Need to select only specific surfaces because some may not have collisions
|
|
|
|
Ref<Shape> shape = mesh->create_trimesh_shape();
|
|
|
|
|
|
|
|
StaticBody * body = block->get_physics_body(*this);
|
|
|
|
if(body == NULL) {
|
|
|
|
// Create body
|
|
|
|
body = memnew(StaticBody);
|
|
|
|
body->set_translation(block_node_pos);
|
|
|
|
body->add_shape(shape);
|
|
|
|
add_child(body);
|
|
|
|
block->body_path = body->get_path();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// Update body
|
|
|
|
body->set_shape(0, shape);
|
|
|
|
}
|
2017-01-01 04:40:16 +01:00
|
|
|
}
|
2016-05-10 01:59:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//void VoxelTerrain::block_removed(VoxelBlock & block) {
|
|
|
|
// MeshInstance * mesh_instance = block.get_mesh_instance(*this);
|
|
|
|
// if (mesh_instance) {
|
|
|
|
// mesh_instance->queue_delete();
|
|
|
|
// }
|
|
|
|
//}
|
|
|
|
|
2017-03-26 20:07:01 +02:00
|
|
|
static bool _raycast_binding_predicate(Vector3i pos, void *context) {
|
|
|
|
|
|
|
|
ERR_FAIL_COND_V(context == NULL, false);
|
|
|
|
VoxelTerrain * terrain = (VoxelTerrain*)context;
|
|
|
|
|
|
|
|
Ref<VoxelLibrary> lib_ref = terrain->get_voxel_library();
|
|
|
|
if(lib_ref.is_null())
|
|
|
|
return false;
|
|
|
|
const VoxelLibrary & lib = **lib_ref;
|
|
|
|
|
|
|
|
Ref<VoxelMap> map = terrain->get_map();
|
|
|
|
// TODO In the future we may want to query more channels
|
|
|
|
int v = map->get_voxel(pos, 0);
|
|
|
|
if(lib.has_voxel(v) == false)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
const Voxel & voxel = lib.get_voxel_const(v);
|
|
|
|
return !voxel.is_transparent();
|
|
|
|
}
|
|
|
|
|
|
|
|
Variant VoxelTerrain::_raycast_binding(Vector3 origin, Vector3 direction, real_t max_distance) {
|
|
|
|
|
|
|
|
// TODO Transform input if the terrain is rotated (in the future it can be made a Spatial node)
|
|
|
|
|
|
|
|
Vector3i hit_pos;
|
|
|
|
Vector3i prev_pos;
|
|
|
|
|
|
|
|
if(voxel_raycast(origin, direction, _raycast_binding_predicate, this, max_distance, hit_pos, prev_pos)) {
|
|
|
|
|
|
|
|
Dictionary hit = Dictionary();
|
|
|
|
hit["position"] = hit_pos.to_vec3();
|
|
|
|
hit["prev_position"] = prev_pos.to_vec3();
|
|
|
|
return hit;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return Variant(); // Null dictionary, no alloc
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-28 00:56:01 +02:00
|
|
|
//void VoxelTerrain::set_voxel_immediate(Vector3 pos, int value, int c) {
|
|
|
|
// Vector3i vpos = pos;
|
|
|
|
|
|
|
|
// _map->set_voxel(value, vpos, c);
|
|
|
|
|
|
|
|
|
|
|
|
//}
|
|
|
|
|
|
|
|
//int VoxelTerrain::get_voxel(Vector3 pos, int c) {
|
|
|
|
// return _map->get_voxel(pos, c);
|
|
|
|
//}
|
|
|
|
|
2017-03-26 20:07:01 +02:00
|
|
|
|
2016-05-10 01:59:54 +02:00
|
|
|
void VoxelTerrain::_bind_methods() {
|
|
|
|
|
2017-03-25 01:23:36 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("set_provider", "provider:VoxelProvider"), &VoxelTerrain::set_provider);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_provider:VoxelProvider"), &VoxelTerrain::get_provider);
|
2017-01-02 02:15:57 +01:00
|
|
|
|
2017-03-25 01:23:36 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("get_block_update_count"), &VoxelTerrain::get_block_update_count);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_mesher:VoxelMesher"), &VoxelTerrain::get_mesher);
|
2016-05-10 01:59:54 +02:00
|
|
|
|
2017-03-26 18:09:37 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("get_generate_collisions"), &VoxelTerrain::get_generate_collisions);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_generate_collisions", "enabled"), &VoxelTerrain::set_generate_collisions);
|
|
|
|
|
2017-03-28 00:56:01 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("get_viewer"), &VoxelTerrain::get_viewer_path);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_viewer", "path"), &VoxelTerrain::set_viewer_path);
|
|
|
|
|
2017-03-25 01:23:36 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("get_map:VoxelMap"), &VoxelTerrain::get_map);
|
2017-01-05 02:39:40 +01:00
|
|
|
|
2017-01-01 04:40:16 +01:00
|
|
|
// TODO Make those two static in VoxelMap?
|
2017-03-25 01:23:36 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("voxel_to_block", "voxel_pos"), &VoxelTerrain::_voxel_to_block_binding);
|
|
|
|
ClassDB::bind_method(D_METHOD("block_to_voxel", "block_pos"), &VoxelTerrain::_block_to_voxel_binding);
|
2016-05-10 01:59:54 +02:00
|
|
|
|
2017-03-28 00:56:01 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("make_block_dirty", "pos"), &VoxelTerrain::_make_block_dirty_binding);
|
|
|
|
ClassDB::bind_method(D_METHOD("make_blocks_dirty", "min", "size"), &VoxelTerrain::_make_blocks_dirty_binding);
|
2016-05-10 01:59:54 +02:00
|
|
|
|
2017-03-26 20:07:01 +02:00
|
|
|
ClassDB::bind_method(D_METHOD("raycast:Dictionary", "origin", "direction", "max_distance"), &VoxelTerrain::_raycast_binding, DEFVAL(100));
|
|
|
|
|
2016-05-10 01:59:54 +02:00
|
|
|
}
|
|
|
|
|