Skip to content

Commit

Permalink
Display VoxelTerrain bounds in editor viewport
Browse files Browse the repository at this point in the history
  • Loading branch information
Zylann committed Oct 5, 2023
1 parent c2535c1 commit 9101753
Show file tree
Hide file tree
Showing 3 changed files with 142 additions and 3 deletions.
13 changes: 13 additions & 0 deletions editor/terrain/voxel_terrain_editor_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,10 @@ void VoxelTerrainEditorPlugin::set_node(VoxelNode *node) {
if (vlt != nullptr) {
vlt->debug_set_draw_enabled(false);
}
VoxelTerrain *vt = Object::cast_to<VoxelTerrain>(_node);
if (vt != nullptr) {
vt->debug_set_draw_enabled(false);
}
}

_node = node;
Expand All @@ -195,6 +199,7 @@ void VoxelTerrainEditorPlugin::set_node(VoxelNode *node) {
_node->connect("tree_exited", ZN_GODOT_CALLABLE_MP(this, VoxelTerrainEditorPlugin, _on_terrain_tree_exited));

VoxelLodTerrain *vlt = Object::cast_to<VoxelLodTerrain>(_node);
VoxelTerrain *vt = Object::cast_to<VoxelTerrain>(_node);

generate_menu_items(_menu_button, vlt != nullptr);

Expand All @@ -205,6 +210,10 @@ void VoxelTerrainEditorPlugin::set_node(VoxelNode *node) {
vlt->debug_set_draw_flag(VoxelLodTerrain::DEBUG_DRAW_OCTREE_BOUNDS, _show_octree_bounds);
vlt->debug_set_draw_flag(VoxelLodTerrain::DEBUG_DRAW_MESH_UPDATES, _show_mesh_updates);
vlt->debug_set_draw_flag(VoxelLodTerrain::DEBUG_DRAW_MODIFIER_BOUNDS, _show_modifier_bounds);

} else if (vt != nullptr) {
vt->debug_set_draw_enabled(true);
vt->debug_set_draw_flag(VoxelTerrain::DEBUG_DRAW_VOLUME_BOUNDS, true);
}
}
}
Expand All @@ -219,6 +228,10 @@ void VoxelTerrainEditorPlugin::_zn_make_visible(bool visible) {
if (vlt != nullptr) {
vlt->debug_set_draw_enabled(visible);
}
VoxelTerrain *vt = Object::cast_to<VoxelTerrain>(_node);
if (vt != nullptr) {
vt->debug_set_draw_enabled(visible);
}
}

// TODO There are deselection problems I cannot fix cleanly!
Expand Down
103 changes: 100 additions & 3 deletions terrain/fixed_lod/voxel_terrain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -799,16 +799,30 @@ void VoxelTerrain::_notification(int p_what) {
case NOTIFICATION_EXIT_TREE:
break;

case NOTIFICATION_ENTER_WORLD:
_mesh_map.for_each_block(SetWorldAction(*get_world_3d()));
break;
case NOTIFICATION_ENTER_WORLD: {
World3D *world = *get_world_3d();
_mesh_map.for_each_block(SetWorldAction(world));
#ifdef TOOLS_ENABLED
if (debug_is_draw_enabled()) {
_debug_renderer.set_world(is_visible_in_tree() ? world : nullptr);
}
#endif
} break;

case NOTIFICATION_EXIT_WORLD:
_mesh_map.for_each_block(SetWorldAction(nullptr));
#ifdef TOOLS_ENABLED
_debug_renderer.set_world(nullptr);
#endif
break;

case NOTIFICATION_VISIBILITY_CHANGED:
_mesh_map.for_each_block(SetParentVisibilityAction(is_visible()));
#ifdef TOOLS_ENABLED
if (debug_is_draw_enabled()) {
_debug_renderer.set_world(is_visible_in_tree() ? *get_world_3d() : nullptr);
}
#endif
break;

case NOTIFICATION_TRANSFORM_CHANGED: {
Expand Down Expand Up @@ -1036,6 +1050,12 @@ void VoxelTerrain::process() {
process_viewers();
// process_received_data_blocks();
process_meshing();

#ifdef TOOLS_ENABLED
if (debug_is_draw_enabled() && is_visible_in_tree()) {
process_debug_draw();
}
#endif
}

void VoxelTerrain::process_viewers() {
Expand Down Expand Up @@ -1819,6 +1839,83 @@ void VoxelTerrain::get_configuration_warnings(PackedStringArray &warnings) const

#endif

// DEBUG LAND

void VoxelTerrain::debug_set_draw_enabled(bool enabled) {
#ifdef TOOLS_ENABLED
_debug_draw_enabled = enabled;
if (_debug_draw_enabled) {
if (is_inside_tree()) {
_debug_renderer.set_world(is_visible_in_tree() ? *get_world_3d() : nullptr);
}
} else {
_debug_renderer.clear();
// _debug_mesh_update_items.clear();
// _debug_edit_items.clear();
}
#endif
}

bool VoxelTerrain::debug_is_draw_enabled() const {
#ifdef TOOLS_ENABLED
return _debug_draw_enabled;
#else
return false;
#endif
}

void VoxelTerrain::debug_set_draw_flag(DebugDrawFlag flag_index, bool enabled) {
#ifdef TOOLS_ENABLED
ERR_FAIL_INDEX(flag_index, DEBUG_DRAW_FLAGS_COUNT);
if (enabled) {
_debug_draw_flags |= (1 << flag_index);
} else {
_debug_draw_flags &= ~(1 << flag_index);
}
#endif
}

bool VoxelTerrain::debug_get_draw_flag(DebugDrawFlag flag_index) const {
#ifdef TOOLS_ENABLED
ERR_FAIL_INDEX_V(flag_index, DEBUG_DRAW_FLAGS_COUNT, false);
return (_debug_draw_flags & (1 << flag_index)) != 0;
#else
return false;
#endif
}

#ifdef TOOLS_ENABLED

void VoxelTerrain::process_debug_draw() {
ZN_PROFILE_SCOPE();

DebugRenderer &dr = _debug_renderer;
dr.begin();

const Transform3D parent_transform = get_global_transform();
const int mesh_block_size = get_mesh_block_size();

// Volume bounds
if (debug_get_draw_flag(DEBUG_DRAW_VOLUME_BOUNDS)) {
const Box3i bounds_in_voxels = get_bounds();
const float bounds_in_voxels_len = Vector3(bounds_in_voxels.size).length();

if (bounds_in_voxels_len < 10000) {
const Vector3 margin = Vector3(1, 1, 1) * bounds_in_voxels_len * 0.0025f;
const Vector3 size = bounds_in_voxels.size;
const Transform3D local_transform(
Basis().scaled(size + margin * 2.f), Vector3(bounds_in_voxels.pos) - margin);
dr.draw_box(parent_transform * local_transform, DebugColors::ID_VOXEL_BOUNDS);
}
}

dr.end();
}

#endif

// BINDING LAND

Vector3i VoxelTerrain::_b_voxel_to_data_block(Vector3 pos) const {
return _data->voxel_to_block(math::floor_to_int(pos));
}
Expand Down
29 changes: 29 additions & 0 deletions terrain/fixed_lod/voxel_terrain.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
#include "voxel_mesh_block_vt.h"
#include "voxel_terrain_multiplayer_synchronizer.h"

#ifdef TOOLS_ENABLED
#include "../../editor/voxel_debug.h"
#endif

namespace zylann {

class AsyncDependencyTracker;
Expand Down Expand Up @@ -139,6 +143,20 @@ class VoxelTerrain : public VoxelNode {
// Vector3i position;
// };

// Debug

enum DebugDrawFlag {
DEBUG_DRAW_VOLUME_BOUNDS = 0,

DEBUG_DRAW_FLAGS_COUNT = 1
};

void debug_set_draw_enabled(bool enabled);
bool debug_is_draw_enabled() const;

void debug_set_draw_flag(DebugDrawFlag flag_index, bool enabled);
bool debug_get_draw_flag(DebugDrawFlag flag_index) const;

// Internal

void set_instancer(VoxelInstancer *instancer);
Expand Down Expand Up @@ -215,6 +233,10 @@ class VoxelTerrain : public VoxelNode {

bool is_area_meshed(const Box3i &box_in_voxels) const;

#ifdef TOOLS_ENABLED
void process_debug_draw();
#endif

#ifdef ZN_GODOT
// Called each time a data block enters a viewer's area.
// This can be either when the block exists and the viewer gets close enough, or when it gets loaded.
Expand Down Expand Up @@ -321,6 +343,13 @@ class VoxelTerrain : public VoxelNode {
VoxelTerrainMultiplayerSynchronizer *_multiplayer_synchronizer = nullptr;

Stats _stats;

#ifdef TOOLS_ENABLED
bool _debug_draw_enabled = false;
uint8_t _debug_draw_flags = 0;

DebugRenderer _debug_renderer;
#endif
};

} // namespace voxel
Expand Down

0 comments on commit 9101753

Please sign in to comment.