Skip to content

Commit

Permalink
added a volumetric arrowhead
Browse files Browse the repository at this point in the history
added the ability to change the aliases of singletons
fixed removing singletons too early
added DebugDraw3D::regenerate_geometry_meshes()
unified UP vector
  • Loading branch information
DmitriySalnikov committed Dec 3, 2023
1 parent a9dc569 commit 03d4062
Show file tree
Hide file tree
Showing 12 changed files with 256 additions and 97 deletions.
15 changes: 9 additions & 6 deletions examples_dd3d/DebugDrawDemoScene.gd
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ extends Node3D
@export var test_graphs := false
@export var more_test_cases := true
@export var draw_array_of_boxes := false
@export_range(0, 2, 0.001) var debug_thickness := 0.075
@export_range(0, 1024) var start_culling_distance := 0.0
@export_range(0, 5, 0.001) var debug_thickness := 0.05
@export_range(0, 1024) var start_culling_distance := 55.0

@export_group("Text groups", "text_groups")
@export var text_groups_show_hints := true
Expand All @@ -17,16 +17,16 @@ extends Node3D
@export var text_groups_position := DebugDrawConfig2D.POSITION_LEFT_TOP
@export var text_groups_offset := Vector2i(8, 8)
@export var text_groups_padding := Vector2i(3, 1)
@export_range(1, 100) var text_groups_default_font_size := 12
@export_range(1, 100) var text_groups_title_font_size := 14
@export_range(1, 100) var text_groups_text_font_size := 12
@export_range(1, 100) var text_groups_default_font_size := 15
@export_range(1, 100) var text_groups_title_font_size := 20
@export_range(1, 100) var text_groups_text_font_size := 17

@export_group("Graphs", "graph")
@export var graph_offset := Vector2i(8, 8)
@export var graph_size := Vector2i(200, 80)
@export_range(1, 100) var graph_title_font_size := 14
@export_range(1, 100) var graph_text_font_size := 12
@export_range(0, 64) var graph_text_precision := 2
@export_range(0, 64) var graph_text_precision := 1
@export_range(1, 32) var graph_line_width := 1.0
@export_range(1, 512) var graph_buffer_size := 128
@export var graph_frame_time_mode := true
Expand Down Expand Up @@ -68,6 +68,9 @@ func _update_keys_just_press():


func _process(delta: float) -> void:
if Input.is_action_just_pressed("ui_end"):
DebugDraw3D.regenerate_geometry_meshes()

DebugDraw3D.scoped_config().set_thickness(debug_thickness)
if false: #test
var s11 = DebugDraw3D.new_scoped_config()
Expand Down
13 changes: 3 additions & 10 deletions examples_dd3d/DebugDrawDemoScene.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -220,14 +220,7 @@ func _on_Button_pressed() -> void:
process_priority = 1
script = ExtResource("1")
custom_font = ExtResource("2_aedbq")
draw_array_of_boxes = true
start_culling_distance = 55.0
text_groups_show_stats = true
text_groups_position = 2
text_groups_default_font_size = 15
text_groups_title_font_size = 20
text_groups_text_font_size = 17
graph_text_precision = 1
[node name="Camera" type="Camera3D" parent="."]
transform = Transform3D(1, 0, 0, 0, 0.953191, 0.30237, 0, -0.30237, 0.953191, -3.988, 13.525, 37.21)
Expand Down Expand Up @@ -388,13 +381,13 @@ transform = Transform3D(0.834492, 0, -0.551019, 0, 1, 0, 0.55102, 0, 0.834493, -
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, -3.18606, -0.331599, 6.66091)
[node name="Arrow" type="Node3D" parent="Misc"]
transform = Transform3D(0.802141, -0.286294, -0.524028, -0.539546, 0.0285125, -0.841473, 0.25585, 0.957718, -0.131597, -4.38512, -1.18432, 6.66091)
transform = Transform3D(0.802141, -0.286294, -0.524028, -0.539546, 0.0285125, -0.841473, 0.25585, 0.957718, -0.131597, -4.06502, -0.670307, 6.7413)
[node name="Position" type="Node3D" parent="Misc"]
transform = Transform3D(1.51514, 0.589536, 1.00858, -1.34875, 0.662262, 1.133, 0, -0.462445, 2.90833, -2.73567, -0.331599, 2.69873)
[node name="GizmoNormal" type="Node3D" parent="Misc"]
transform = Transform3D(1.98138, 0, -0.70285, 0, 0.999999, 0, 0.434655, 0, 3.20395, -2.69921, -0.331599, 4.79165)
transform = Transform3D(1.95938, 0, -0.848962, 0, 0.999999, 0, 0.525014, 0, 3.16837, -2.69921, -0.306246, 4.79165)
[node name="GizmoTransform" type="Node3D" parent="Misc"]
transform = Transform3D(0.879881, 0.248446, -0.405072, -0.346604, 0.918688, -0.189411, 0.325077, 0.307059, 0.894449, -4.428, -0.458, 4.259)
Expand Down Expand Up @@ -431,7 +424,7 @@ transform = Transform3D(1, 6.61592e-09, 2.23038e-08, 9.40939e-07, 1, 0, -2.76085
transform = Transform3D(1, 6.61592e-09, 2.23038e-08, 6.87561e-07, 1, 0, -2.87275e-08, -1.49012e-08, 1, -0.1559, -0.407045, 0.0523388)
[node name="4" type="Node3D" parent="Lines"]
transform = Transform3D(1, 6.61592e-09, 2.23038e-08, 3.42235e-07, 1, 0, -4.57815e-08, -1.49012e-08, 1, 1.18591, 1.8987, 0.301906)
transform = Transform3D(1, 6.61592e-09, 2.23038e-08, 4.9239e-07, 1, 0, -3.40677e-08, -1.49012e-08, 1, 1.18591, 1.8987, 0.301906)
[node name="5" type="Node3D" parent="Lines"]
transform = Transform3D(-0.998871, -0.0207882, -0.0355643, 0.0855375, -0.5714, -2.68836, 0.0136011, -0.249864, 0.572532, 1.43126, 0.26242, 1.92347)
Expand Down
45 changes: 23 additions & 22 deletions src/3d/debug_draw_3d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

GODOT_WARNING_DISABLE()
#include <godot_cpp/classes/camera3d.hpp>
#include <godot_cpp/classes/input.hpp>
#include <godot_cpp/classes/os.hpp>
GODOT_WARNING_RESTORE()

Expand All @@ -36,6 +35,7 @@ void DebugDraw3D::_bind_methods() {
#pragma endregion

#pragma region Draw Functions
ClassDB::bind_method(D_METHOD(NAMEOF(regenerate_geometry_meshes)), &DebugDraw3D::regenerate_geometry_meshes);
ClassDB::bind_method(D_METHOD(NAMEOF(clear_all)), &DebugDraw3D::clear_all);

ClassDB::bind_method(D_METHOD(NAMEOF(draw_sphere), "position", "radius", "color", "duration"), &DebugDraw3D::draw_sphere, 0.5f, Colors::empty_color, 0);
Expand Down Expand Up @@ -99,9 +99,9 @@ void DebugDraw3D::init(DebugDrawManager *root) {
root_node = root;
set_config(nullptr);

root_settings_section = String(Utils::root_settings_section) + "3D/";
root_settings_section = String(Utils::root_settings_section) + "3d/";
DEFINE_SETTING_AND_GET(bool add_bevel, root_settings_section + s_add_bevel_to_volumetric, true, Variant::BOOL);
DEFINE_SETTING_AND_GET(real_t def_thickness, root_settings_section + s_default_thickness, 0.075f, Variant::FLOAT);
DEFINE_SETTING_AND_GET(real_t def_thickness, root_settings_section + s_default_thickness, 0.05f, Variant::FLOAT);

stats_3d.instantiate();
default_scoped_config.instantiate();
Expand All @@ -111,8 +111,6 @@ void DebugDraw3D::init(DebugDrawManager *root) {
_load_materials();

#ifndef DISABLE_DEBUG_RENDERING
DEV_PRINT_STD("To regenerate the 3D geometry press the \"%s\" action\n", reload_action_name);

dgc = std::make_unique<DebugGeometryContainer>(this, add_bevel);
#endif
}
Expand All @@ -128,17 +126,6 @@ void DebugDraw3D::process(double delta) {
ZoneScoped;
FrameMarkStart("3D Setup");
#ifndef DISABLE_DEBUG_RENDERING
#ifdef DEV_ENABLED
if (Input::get_singleton()->is_action_just_pressed(reload_action_name)) {
LOCK_GUARD(datalock);
Node *old_world = dgc->get_world();
dgc.reset();

_load_materials();
dgc = std::make_unique<DebugGeometryContainer>(this, PS()->get_setting(root_settings_section + s_add_bevel_to_volumetric));
dgc->set_world(old_world);
}
#endif

// Update 3D debug
dgc->update_geometry(delta);
Expand Down Expand Up @@ -445,6 +432,17 @@ Ref<DebugDrawStats3D> DebugDraw3D::get_render_stats() {
return stats_3d;
}

void DebugDraw3D::regenerate_geometry_meshes() {
#ifndef DISABLE_DEBUG_RENDERING
LOCK_GUARD(datalock);
Node *old_world = dgc->get_world();
dgc.reset();

dgc = std::make_unique<DebugGeometryContainer>(this, PS()->get_setting(root_settings_section + s_add_bevel_to_volumetric));
dgc->set_world(old_world);
#endif
}

void DebugDraw3D::clear_all() {
ZoneScoped;
#ifndef DISABLE_DEBUG_RENDERING
Expand All @@ -465,6 +463,10 @@ void DebugDraw3D::clear_all() {

#ifndef DISABLE_DEBUG_RENDERING

Vector3 DebugDraw3D::get_up_vector(const Vector3 &dir) {
return (Math::is_equal_approx(dir.x, 0) && Math::is_equal_approx(dir.z, 0)) || Math::is_equal_approx(dir.y, 0) ? Vector3_UP_OF : Vector3_UP;
}

void DebugDraw3D::add_or_update_line_with_thickness(real_t _exp_time, const std::vector<Vector3> &_lines, const Color &_col, const std::function<void(DelayedRendererLine *)> _custom_upd) {
ZoneScoped;

Expand All @@ -481,11 +483,10 @@ void DebugDraw3D::add_or_update_line_with_thickness(real_t _exp_time, const std:
Vector3 diff = _lines[i + 1] - a;
real_t len = diff.length();
Vector3 center = diff.normalized() * len * .5f;
Vector3 up = center.x == 0 && center.z == 0 ? Vector3_UP_OF : Vector3_UP;
dgc->geometry_pool.add_or_update_instance(
InstanceType::LINE_VOLUMETRIC,
_exp_time,
Transform3D(Basis().looking_at(center, up).scaled(Vector3(len, len, len)), a), // slow
Transform3D(Basis().looking_at(center, get_up_vector(center)).scaled(Vector3(len, len, len)), a), // slow
_col,
_scoped_config_to_custom(scfg.ptr()),
SphereBounds(a + center, len * .5f));
Expand Down Expand Up @@ -568,12 +569,12 @@ void DebugDraw3D::draw_cylinder_ab(const Vector3 &a, const Vector3 &b, const rea
ZoneScoped;
CHECK_BEFORE_CALL();

// TODO SLOOOOOW
// TODO sloooow
Vector3 diff = b - a;
real_t len = diff.length();
real_t diameter = radius * 2;
Vector3 center = diff.normalized() * len * .5f;
Vector3 up = (Math::is_equal_approx(center.x, 0) && Math::is_equal_approx(center.z, 0)) || Math::is_equal_approx(center.y, 0) ? Vector3_UP_OF : Vector3_UP;
Vector3 up = get_up_vector(center);
Vector3 center_rot = center.rotated(center.cross(up), Math::deg_to_rad(90.f)); // Convert +Y of cylinder to -Z
Transform3D t = Transform3D(Basis().looking_at(center_rot, up).scaled_local(Vector3(diameter, len, diameter)), a + center);

Expand Down Expand Up @@ -739,8 +740,8 @@ void DebugDraw3D::create_arrow(const Vector3 &a, const Vector3 &b, const Color &
Vector3 dir = (b - a);
real_t size = (is_absolute_size ? arrow_size : dir.length() * arrow_size) * 2;

const Vector3 UP = Vector3(0.0000000001f, 1, 0);
Transform3D t = Transform3D(Basis().looking_at(dir, UP).scaled(Vector3(size, size, size)), b);
Vector3 up = get_up_vector(dir);
Transform3D t = Transform3D(Basis().looking_at(dir, up).scaled(Vector3(size, size, size)), b);

Ref<DDScopedConfig3D> scfg = scoped_config_for_current_thread();

Expand Down
6 changes: 3 additions & 3 deletions src/3d/debug_draw_3d.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,6 @@ class DebugDraw3D : public Object, public IScopedStorage<DDScopedConfig3D> {
#endif

#ifndef DISABLE_DEBUG_RENDERING
#ifdef DEV_ENABLED
const char *reload_action_name = "ui_end";
#endif

// Meshes
std::unique_ptr<DebugGeometryContainer> dgc;
Expand All @@ -100,6 +97,7 @@ class DebugDraw3D : public Object, public IScopedStorage<DDScopedConfig3D> {
InstanceType _scoped_config_type_convert(ConvertableInstanceType type, DDScopedConfig3D *cfg);
GeometryType _scoped_config_get_geometry_type(DDScopedConfig3D *cfg);

_FORCE_INLINE_ Vector3 get_up_vector(const Vector3 &dir);
void add_or_update_line_with_thickness(real_t _exp_time, const std::vector<Vector3> &_lines, const Color &_col, const std::function<void(DelayedRendererLine *)> _custom_upd = nullptr);

#endif
Expand Down Expand Up @@ -173,6 +171,8 @@ class DebugDraw3D : public Object, public IScopedStorage<DDScopedConfig3D> {
{}
#endif

void regenerate_geometry_meshes();

/// Clear all 3D objects
void clear_all();

Expand Down
3 changes: 1 addition & 2 deletions src/3d/debug_geometry_container.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,7 @@ DebugGeometryContainer::DebugGeometryContainer(class DebugDraw3D *root, const bo
auto array_mesh_cube_centered_volumetric = GeometryGenerator::ConvertWireframeToVolumetric(array_mesh_cube_center, add_bevel);
CreateMMI(InstanceType::CUBE_CENTERED_VOLUMETRIC, UsingShaderType::Expandable, NAMEOF(mmi_cubes_centered_volumetric), array_mesh_cube_centered_volumetric);

auto array_mesh_arrow_head_simple = GeometryGenerator::CreateMeshNative(Mesh::PrimitiveType::PRIMITIVE_LINES, GeometryGenerator::ArrowheadVertices, GeometryGenerator::ArrowheadIndicesSimplified);
auto array_mesh_arrow_head_volumetric = GeometryGenerator::ConvertWireframeToVolumetric(array_mesh_arrow_head_simple, add_bevel, true);
auto array_mesh_arrow_head_volumetric = GeometryGenerator::CreateVolumetricArrowHead(.25f, 1.f, 1.f, add_bevel);
CreateMMI(InstanceType::ARROWHEAD_VOLUMETRIC, UsingShaderType::Expandable, NAMEOF(mmi_arrowheads_volumetric), array_mesh_arrow_head_volumetric);

auto array_mesh_pos_volumetric = GeometryGenerator::ConvertWireframeToVolumetric(array_mesh_pos, add_bevel);
Expand Down
Loading

0 comments on commit 03d4062

Please sign in to comment.