#include "voxel_terrain.h" #include "../edition/voxel_tool_terrain.h" #include "../server/voxel_server.h" #include "../util/macros.h" #include "../util/profiling.h" #include "../util/profiling_clock.h" #include "../util/utility.h" #include "../voxel_constants.h" #include "../voxel_string_names.h" #include "voxel_block.h" #include "voxel_map.h" #include #include #include VoxelTerrain::VoxelTerrain() { // Note: don't do anything heavy in the constructor. // Godot may create and destroy dozens of instances of all node types on startup, // due to how ClassDB gets its default values. set_notify_transform(true); // TODO Should it actually be finite for better discovery? // Infinite by default _bounds_in_voxels = Rect3i::from_center_extents(Vector3i(0), Vector3i(VoxelConstants::MAX_VOLUME_EXTENT)); _volume_id = VoxelServer::get_singleton()->add_volume(&_reception_buffers, VoxelServer::VOLUME_SPARSE_GRID); // For ease of use in editor Ref default_mesher; default_mesher.instance(); _mesher = default_mesher; } VoxelTerrain::~VoxelTerrain() { PRINT_VERBOSE("Destroying VoxelTerrain"); VoxelServer::get_singleton()->remove_volume(_volume_id); } // TODO See if there is a way to specify materials in voxels directly? bool VoxelTerrain::_set(const StringName &p_name, const Variant &p_value) { if (p_name.operator String().begins_with("material/")) { unsigned int idx = p_name.operator String().get_slicec('/', 1).to_int(); ERR_FAIL_COND_V(idx >= VoxelMesherBlocky::MAX_MATERIALS || idx < 0, false); set_material(idx, p_value); return true; } return false; } bool VoxelTerrain::_get(const StringName &p_name, Variant &r_ret) const { if (p_name.operator String().begins_with("material/")) { unsigned int idx = p_name.operator String().get_slicec('/', 1).to_int(); ERR_FAIL_COND_V(idx >= VoxelMesherBlocky::MAX_MATERIALS || idx < 0, false); r_ret = get_material(idx); return true; } return false; } void VoxelTerrain::_get_property_list(List *p_list) const { for (unsigned int i = 0; i < VoxelMesherBlocky::MAX_MATERIALS; ++i) { p_list->push_back(PropertyInfo( Variant::OBJECT, "material/" + itos(i), PROPERTY_HINT_RESOURCE_TYPE, "ShaderMaterial,SpatialMaterial")); } } void VoxelTerrain::set_stream(Ref p_stream) { if (p_stream == _stream) { return; } _stream = p_stream; #ifdef TOOLS_ENABLED if (_stream.is_valid()) { if (Engine::get_singleton()->is_editor_hint()) { Ref