Skip to content

Commit

Permalink
Re-namespaced VoxelDebug
Browse files Browse the repository at this point in the history
  • Loading branch information
Zylann committed Jan 4, 2022
1 parent abcb307 commit 1484063
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 33 deletions.
2 changes: 1 addition & 1 deletion editor/graph/voxel_graph_editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -811,7 +811,7 @@ void VoxelGraphEditor::update_range_analysis_gizmo() {
const AABB aabb = _range_analysis_dialog->get_aabb();
_debug_renderer.begin();
_debug_renderer.draw_box(parent_transform * Transform3D(Basis().scaled(aabb.size), aabb.position),
VoxelDebug::ID_VOXEL_GRAPH_DEBUG_BOUNDS);
DebugColors::ID_VOXEL_GRAPH_DEBUG_BOUNDS);
_debug_renderer.end();
}

Expand Down
2 changes: 1 addition & 1 deletion editor/graph/voxel_graph_editor.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ class VoxelGraphEditor : public Control {
bool _nothing_selected_check_scheduled = false;
float _time_before_preview_update = 0.f;
Node3D *_voxel_node = nullptr;
VoxelDebug::DebugRenderer _debug_renderer;
zylann::DebugRenderer _debug_renderer;
};

#endif // VOXEL_GRAPH_EDITOR_H
30 changes: 14 additions & 16 deletions editor/voxel_debug.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@

#include <scene/resources/mesh.h>

using namespace zylann;
namespace zylann {

namespace VoxelDebug {

FixedArray<Ref<Mesh>, ID_COUNT> g_wirecubes;
FixedArray<Ref<Mesh>, DebugColors::ID_COUNT> g_wirecubes;
bool g_finalized = false;

template <typename T>
Expand All @@ -18,23 +16,23 @@ void raw_copy_to(Vector<T> &dst, const T *src, unsigned int count) {
memcpy(dst.ptrw(), src, count * sizeof(T));
}

static Color get_color(ColorID id) {
static Color get_color(DebugColors::ColorID id) {
switch (id) {
case ID_VOXEL_BOUNDS:
case DebugColors::ID_VOXEL_BOUNDS:
return Color(1, 1, 1);
case ID_OCTREE_BOUNDS:
case DebugColors::ID_OCTREE_BOUNDS:
return Color(0.5, 0.5, 0.5);
case ID_VOXEL_GRAPH_DEBUG_BOUNDS:
case DebugColors::ID_VOXEL_GRAPH_DEBUG_BOUNDS:
return Color(1.0, 1.0, 0.0);
case ID_WHITE:
case DebugColors::ID_WHITE:
return Color(1, 1, 1);
default:
CRASH_NOW_MSG("Unexpected index");
}
return Color();
}

Ref<Mesh> get_wirecube(ColorID id) {
Ref<Mesh> get_debug_wirecube(DebugColors::ColorID id) {
CRASH_COND(g_finalized);

Ref<Mesh> &wirecube = g_wirecubes[id];
Expand Down Expand Up @@ -99,7 +97,7 @@ Ref<Mesh> get_wirecube(ColorID id) {
return wirecube;
}

void free_resources() {
void free_debug_resources() {
for (unsigned int i = 0; i < g_wirecubes.size(); ++i) {
g_wirecubes[i].unref();
}
Expand Down Expand Up @@ -147,7 +145,7 @@ class DebugRendererItem {
Transform3D _transform;
bool _visible = true;
Ref<Mesh> _mesh;
DirectMeshInstance _mesh_instance;
zylann::DirectMeshInstance _mesh_instance;
};

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -180,7 +178,7 @@ void DebugRenderer::begin() {
_mm_renderer.begin();
}

void DebugRenderer::draw_box(const Transform3D &t, ColorID color) {
void DebugRenderer::draw_box(const Transform3D &t, DebugColors::ColorID color) {
// Pick an existing item, or create one
DebugRendererItem *item;
if (_current >= _items.size()) {
Expand All @@ -191,7 +189,7 @@ void DebugRenderer::draw_box(const Transform3D &t, ColorID color) {
item = _items[_current];
}

item->set_mesh(get_wirecube(color));
item->set_mesh(get_debug_wirecube(color));
item->set_transform(t);
item->set_visible(true);

Expand Down Expand Up @@ -222,7 +220,7 @@ DebugMultiMeshRenderer::DebugMultiMeshRenderer() {
// It should be off anyways, but it's rather concerning.
_multimesh_instance.set_cast_shadows_setting(RenderingServer::SHADOW_CASTING_SETTING_OFF);
_multimesh.instantiate();
Ref<Mesh> wirecube = get_wirecube(ID_WHITE);
Ref<Mesh> wirecube = get_debug_wirecube(DebugColors::ID_WHITE);
_multimesh->set_mesh(wirecube);
_multimesh->set_transform_format(MultiMesh::TRANSFORM_3D);
// TODO Optimize: Godot needs to bring back 8-bit color attributes on multimesh, 32-bit colors are too much
Expand Down Expand Up @@ -281,4 +279,4 @@ void DebugMultiMeshRenderer::clear() {
_multimesh->set_instance_count(0);
}

} // namespace VoxelDebug
} // namespace zylann
21 changes: 12 additions & 9 deletions editor/voxel_debug.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@
#include <vector>

class Mesh;
class World3D;

namespace zylann {

class DirectMeshInstance;
}
class World3D;

namespace VoxelDebug {
namespace DebugColors {

enum ColorID { //
ID_VOXEL_BOUNDS = 0,
Expand All @@ -21,8 +22,10 @@ enum ColorID { //
ID_COUNT
};

Ref<Mesh> get_wirecube(ColorID id);
void free_resources();
} //namespace DebugColors

Ref<Mesh> get_debug_wirecube(DebugColors::ColorID id);
void free_debug_resources();

class DebugMultiMeshRenderer {
public:
Expand All @@ -31,7 +34,7 @@ class DebugMultiMeshRenderer {

void set_world(World3D *world);
void begin();
void draw_box(const Transform3D &t, zylann::Color8 color);
void draw_box(const Transform3D &t, Color8 color);
void end();
void clear();

Expand All @@ -58,11 +61,11 @@ class DebugRenderer {
// Call this before issuing drawing commands
void begin();

void draw_box(const Transform3D &t, ColorID color);
void draw_box(const Transform3D &t, DebugColors::ColorID color);

// Draws a box wireframe using MultiMesh, allowing to draw much more without slowing down.
// The box's origin is its lower corner. Size is defined by the transform's basis.
void draw_box_mm(const Transform3D &t, zylann::Color8 color);
void draw_box_mm(const Transform3D &t, Color8 color);

// Call this after issuing all drawing commands
void end();
Expand All @@ -78,6 +81,6 @@ class DebugRenderer {
DebugMultiMeshRenderer _mm_renderer;
};

} // namespace VoxelDebug
} //namespace zylann

#endif // VOXEL_DEBUG_H
2 changes: 1 addition & 1 deletion register_types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ void unregister_voxel_types() {
// TODO No remove?

#ifdef TOOLS_ENABLED
VoxelDebug::free_resources();
zylann::free_debug_resources();

// TODO Seriously, no remove?
//EditorPlugins::remove_by_type<VoxelGraphEditorPlugin>();
Expand Down
2 changes: 1 addition & 1 deletion terrain/instancing/voxel_instancer.h
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ class VoxelInstancer : public Node3D, public VoxelInstanceLibrary::IListener {
VoxelLodTerrain *_parent;

#ifdef TOOLS_ENABLED
VoxelDebug::DebugRenderer _debug_renderer;
zylann::DebugRenderer _debug_renderer;
bool _gizmos_enabled = false;
#endif
};
Expand Down
6 changes: 3 additions & 3 deletions terrain/voxel_lod_terrain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2781,7 +2781,7 @@ Array VoxelLodTerrain::debug_get_octrees_detailed() const {
void VoxelLodTerrain::update_gizmos() {
VOXEL_PROFILE_SCOPE();

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

const Transform3D parent_transform = get_global_transform();
Expand All @@ -2792,7 +2792,7 @@ void VoxelLodTerrain::update_gizmos() {
const Basis local_octree_basis = Basis().scaled(Vector3(octree_size, octree_size, octree_size));
for (Map<Vector3i, OctreeItem>::Element *e = _lod_octrees.front(); e; e = e->next()) {
const Transform3D local_transform(local_octree_basis, e->key() * octree_size);
dr.draw_box(parent_transform * local_transform, VoxelDebug::ID_OCTREE_BOUNDS);
dr.draw_box(parent_transform * local_transform, DebugColors::ID_OCTREE_BOUNDS);
}
}

Expand All @@ -2804,7 +2804,7 @@ void VoxelLodTerrain::update_gizmos() {
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, VoxelDebug::ID_VOXEL_BOUNDS);
dr.draw_box(parent_transform * local_transform, DebugColors::ID_VOXEL_BOUNDS);
}
}

Expand Down
2 changes: 1 addition & 1 deletion terrain/voxel_lod_terrain.h
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,7 @@ class VoxelLodTerrain : public VoxelNode {
bool _show_octree_node_gizmos = false;
bool _show_edited_blocks = false;
unsigned int _edited_blocks_gizmos_lod_index = 0;
VoxelDebug::DebugRenderer _debug_renderer;
zylann::DebugRenderer _debug_renderer;
#endif

Stats _stats;
Expand Down

0 comments on commit 1484063

Please sign in to comment.