Skip to content

Commit

Permalink
improved the accuracy of the expandable shader (not 100%)
Browse files Browse the repository at this point in the history
improved sphere and cylinder shapes
added more prints to resource source generator
added dev ability to recreate geometry
  • Loading branch information
DmitriySalnikov committed Nov 20, 2023
1 parent f9b5e0c commit 5f48569
Show file tree
Hide file tree
Showing 14 changed files with 168 additions and 119 deletions.
15 changes: 10 additions & 5 deletions examples_dd3d/DebugDrawDemoScene.gd
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ func _update_keys_just_press():
var set_key = func (k: Key): return (1 if button_presses[k] == 0 else button_presses[k]) if Input.is_key_pressed(k) else 0
button_presses[KEY_LEFT] = set_key.call(KEY_LEFT)
button_presses[KEY_UP] = set_key.call(KEY_UP)
button_presses[KEY_CTRL] = set_key.call(KEY_CTRL)
button_presses[KEY_F1] = set_key.call(KEY_F1)
button_presses[KEY_1] = set_key.call(KEY_1)
button_presses[KEY_2] = set_key.call(KEY_2)
Expand Down Expand Up @@ -106,6 +107,9 @@ func _process(delta: float) -> void:
DebugDraw3D.config.use_frustum_culling = !DebugDraw3D.config.use_frustum_culling
if _is_key_just_pressed(KEY_UP):
DebugDraw3D.config.force_use_camera_from_scene = !DebugDraw3D.config.force_use_camera_from_scene
if _is_key_just_pressed(KEY_CTRL):
if !Engine.is_editor_hint():
get_viewport().msaa_3d = Viewport.MSAA_DISABLED if get_viewport().msaa_3d == Viewport.MSAA_4X else Viewport.MSAA_4X

if _is_key_just_pressed(KEY_1):
DebugDraw3D.debug_enabled = !DebugDraw3D.debug_enabled
Expand Down Expand Up @@ -305,11 +309,12 @@ func _text_tests():
if text_groups_show_hints:
DebugDraw2D.begin_text_group("controls", 1024, Color.WHITE, false)
DebugDraw2D.set_text("Shift: change render layers", DebugDraw3D.config.geometry_render_layers, 1)
DebugDraw2D.set_text("Down: freeze render", DebugDraw3D.config.freeze_3d_render, 2)
DebugDraw2D.set_text("Up: use scene camera", DebugDraw3D.config.force_use_camera_from_scene, 3)
DebugDraw2D.set_text("1,2,3: toggle debug", "%s, %s 😐, %s 😏" % [DebugDraw3D.debug_enabled, DebugDraw2D.debug_enabled, DebugDrawManager.debug_enabled], 4)
DebugDraw2D.set_text("Left: toggle frustum culling", DebugDraw3D.config.use_frustum_culling, 5)
DebugDraw2D.set_text("Right: draw bounds for culling", DebugDraw3D.config.visible_instance_bounds, 6)
DebugDraw2D.set_text("Ctrl: toggle anti-aliasing", "MSAA 4x" if get_viewport().msaa_3d == Viewport.MSAA_4X else "Disabled", 2)
DebugDraw2D.set_text("Down: freeze render", DebugDraw3D.config.freeze_3d_render, 3)
DebugDraw2D.set_text("Up: use scene camera", DebugDraw3D.config.force_use_camera_from_scene, 4)
DebugDraw2D.set_text("1,2,3: toggle debug", "%s, %s 😐, %s 😏" % [DebugDraw3D.debug_enabled, DebugDraw2D.debug_enabled, DebugDrawManager.debug_enabled], 5)
DebugDraw2D.set_text("Left: toggle frustum culling", DebugDraw3D.config.use_frustum_culling, 6)
DebugDraw2D.set_text("Right: draw bounds for culling", DebugDraw3D.config.visible_instance_bounds, 7)


func _more_tests():
Expand Down
6 changes: 3 additions & 3 deletions examples_dd3d/DebugDrawDemoScene.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -283,10 +283,10 @@ transform = Transform3D(10.7186, 0, 0, 0, 3.9777, 0, 0, 0, 7.05487, 10.6302, 1.9
[node name="LinePathAnim" type="AnimationPlayer" parent="."]
root_node = NodePath("../LinePath")
autoplay = "New Anim"
libraries = {
"": SubResource("AnimationLibrary_nj4nv")
}
autoplay = "New Anim"
[node name="LinePath" type="Node3D" parent="."]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 2.67362, -8)
Expand Down Expand Up @@ -328,6 +328,7 @@ transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, -10.6349, 0.625214, -5.81358)
[node name="Box1" type="Node3D" parent="."]
transform = Transform3D(2.90583, -0.000527017, -5.34615, 0.00469241, 3.92788, 0.0141019, 0.556318, -0.0303774, 1.91619, -14.007, 0, 2.794)
rotation_edit_mode = 2
[node name="Box2" type="Node3D" parent="."]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, -14.4103, 0, 6.71577)
Expand Down Expand Up @@ -452,11 +453,10 @@ transform = Transform3D(0.935992, 0.352021, 0, -0.352021, 0.935992, 0, 0, 0, 1,
target_position = Vector3(0, -3.464, 0)
[node name="AnimationPlayer" type="AnimationPlayer" parent="HitTest"]
callback_mode_process = 0
autoplay = "New Anim"
libraries = {
"": SubResource("AnimationLibrary_vh8ml")
}
autoplay = "New Anim"
[node name="LagTest" type="CSGBox3D" parent="."]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 10.4482, -1.49353, -0.450473)
Expand Down
16 changes: 11 additions & 5 deletions examples_dd3d/DebugDrawDemoSceneCS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ void _update_keys_just_press()
var set = (Key k) => Input.IsKeyPressed(k) ? (button_presses[k] == 0 ? 1 : button_presses[k]) : 0;
button_presses[Key.Left] = set(Key.Left);
button_presses[Key.Up] = set(Key.Up);
button_presses[Key.Ctrl] = set(Key.Ctrl);
button_presses[Key.F1] = set(Key.F1);
button_presses[Key.Key1] = set(Key.Key1);
button_presses[Key.Key2] = set(Key.Key2);
Expand Down Expand Up @@ -210,6 +211,10 @@ public override void _Process(double delta)
if (_is_key_just_pressed(Key.Up))
DebugDraw3D.Config.ForceUseCameraFromScene = !DebugDraw3D.Config.ForceUseCameraFromScene;

if (_is_key_just_pressed(Key.Ctrl))
if (!Engine.IsEditorHint())
GetViewport().Msaa3D = GetViewport().Msaa3D == Viewport.Msaa.Msaa4X ? Viewport.Msaa.Disabled : Viewport.Msaa.Msaa4X;

if (_is_key_just_pressed(Key.Key1))
DebugDraw3D.DebugEnabled = !DebugDraw3D.DebugEnabled;
if (_is_key_just_pressed(Key.Key2))
Expand Down Expand Up @@ -467,11 +472,12 @@ void _text_tests()
{
DebugDraw2D.BeginTextGroup("controls", 1024, Colors.White, false);
DebugDraw2D.SetText("Shift: change render layers", DebugDraw3D.Config.GeometryRenderLayers, 1);
DebugDraw2D.SetText("Down: freeze render", DebugDraw3D.Config.Freeze3dRender, 2);
DebugDraw2D.SetText("Up: use scene camera", DebugDraw3D.Config.ForceUseCameraFromScene, 3);
DebugDraw2D.SetText("1,2,3: toggle debug", $"{DebugDraw3D.DebugEnabled}, {DebugDraw2D.DebugEnabled} 😐, {DebugDrawManager.DebugEnabled} 😏", 4);
DebugDraw2D.SetText("Left: toggle frustum culling", DebugDraw3D.Config.UseFrustumCulling, 5);
DebugDraw2D.SetText("Right: draw bounds for culling", DebugDraw3D.Config.VisibleInstanceBounds, 6);
DebugDraw2D.SetText("Ctrl: change render layers", GetViewport().Msaa3D == Viewport.Msaa.Msaa4X ? "MSAA 4x" : "Disabled", 2);
DebugDraw2D.SetText("Down: freeze render", DebugDraw3D.Config.Freeze3dRender, 3);
DebugDraw2D.SetText("Up: use scene camera", DebugDraw3D.Config.ForceUseCameraFromScene, 4);
DebugDraw2D.SetText("1,2,3: toggle debug", $"{DebugDraw3D.DebugEnabled}, {DebugDraw2D.DebugEnabled} 😐, {DebugDrawManager.DebugEnabled} 😏", 5);
DebugDraw2D.SetText("Left: toggle frustum culling", DebugDraw3D.Config.UseFrustumCulling, 6);
DebugDraw2D.SetText("Right: draw bounds for culling", DebugDraw3D.Config.VisibleInstanceBounds, 7);
}
}

Expand Down
6 changes: 6 additions & 0 deletions lib_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,9 +150,15 @@ def generate_sources_for_resources(env, src_out):
("src/resources/billboard_unshaded.gdshader", True),
]
generate_resources_cpp_h_files(shared_files, "DD3DResources", "shared_resources.gen", src_out)
print("The generation of C++ sources with the contents of resources has been completed")
print()


def generate_resources_cpp_h_files(input_files, namespace, output_no_ext, src_out):
print(f"Generating sources '{output_no_ext}.[cpp/h]' with content from resources:")
print("\n".join([f"{i[0]} as {'text' if i[1] else 'binary'}" for i in input_files]))
print()

gen_dir = "gen/"
out_dir = src_folder + "/" + gen_dir
Path(out_dir).mkdir(parents=True, exist_ok=True)
Expand Down
55 changes: 36 additions & 19 deletions src/3d/debug_draw_3d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

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

#include <limits.h>
Expand Down Expand Up @@ -94,6 +95,10 @@ void DebugDraw3D::init(DebugDrawManager *root) {
_load_materials();

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

dgc = std::make_unique<DebugGeometryContainer>(this);
#endif
}
Expand All @@ -119,6 +124,18 @@ DebugDraw3D::~DebugDraw3D() {

void DebugDraw3D::process(double delta) {
#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);
dgc->set_world(old_world);
}
#endif

// Update 3D debug
dgc->update_geometry(delta);
#endif
Expand Down Expand Up @@ -277,7 +294,7 @@ void DebugDraw3D::draw_sphere_base(const Vector3 &position, const real_t &radius

void DebugDraw3D::draw_sphere_xf_base(const Transform3D &transform, const Color &color, const real_t &duration, const bool &hd) {
CHECK_BEFORE_CALL();
LOCK_GUARD(dgc->datalock);
LOCK_GUARD(datalock);
dgc->geometry_pool.add_or_update_instance(
hd ? InstanceType::SPHERES_HD : InstanceType::SPHERES,
duration,
Expand Down Expand Up @@ -311,7 +328,7 @@ void DebugDraw3D::draw_sphere_hd_xf(const Transform3D &transform, const Color &c

void DebugDraw3D::draw_cylinder(const Transform3D &transform, const Color &color, const real_t &duration) {
CHECK_BEFORE_CALL();
LOCK_GUARD(dgc->datalock);
LOCK_GUARD(datalock);
dgc->geometry_pool.add_or_update_instance(
InstanceType::CYLINDERS,
duration,
Expand All @@ -330,7 +347,7 @@ void DebugDraw3D::draw_box(const Vector3 &position, const Vector3 &size, const C

void DebugDraw3D::draw_box_xf(const Transform3D &transform, const Color &color, const bool &is_box_centered, const real_t &duration) {
CHECK_BEFORE_CALL();
LOCK_GUARD(dgc->datalock);
LOCK_GUARD(datalock);

/// It's possible to use less space to contain box, but this method works better in more cases
SphereBounds sb(transform.origin, MathUtils::get_max_basis_length(transform.basis) * MathUtils::CubeRadiusForSphere);
Expand Down Expand Up @@ -365,7 +382,7 @@ void DebugDraw3D::draw_aabb_ab(const Vector3 &a, const Vector3 &b, const Color &

void DebugDraw3D::draw_line_hit(const Vector3 &start, const Vector3 &end, const Vector3 &hit, const bool &is_hit, const real_t &hit_size, const Color &hit_color, const Color &after_hit_color, const real_t &duration) {
CHECK_BEFORE_CALL();
LOCK_GUARD(dgc->datalock);
LOCK_GUARD(datalock);
if (is_hit) {
dgc->geometry_pool.add_or_update_line(duration, { start, hit }, IS_DEFAULT_COLOR(hit_color) ? config->get_line_hit_color() : hit_color);
dgc->geometry_pool.add_or_update_line(duration, { hit, end }, IS_DEFAULT_COLOR(after_hit_color) ? config->get_line_after_hit_color() : after_hit_color);
Expand All @@ -383,7 +400,7 @@ void DebugDraw3D::draw_line_hit(const Vector3 &start, const Vector3 &end, const

void DebugDraw3D::draw_line_hit_offset(const Vector3 &start, const Vector3 &end, const bool &is_hit, const real_t &unit_offset_of_hit, const real_t &hit_size, const Color &hit_color, const Color &after_hit_color, const real_t &duration) {
CHECK_BEFORE_CALL();
LOCK_GUARD(dgc->datalock);
LOCK_GUARD(datalock);
if (is_hit && unit_offset_of_hit >= 0 && unit_offset_of_hit <= 1) {
draw_line_hit(start, end, ((end - start).normalized() * start.distance_to(end) * unit_offset_of_hit + start), is_hit, hit_size, hit_color, after_hit_color, duration);
} else {
Expand All @@ -395,13 +412,13 @@ void DebugDraw3D::draw_line_hit_offset(const Vector3 &start, const Vector3 &end,

void DebugDraw3D::draw_line(const Vector3 &a, const Vector3 &b, const Color &color, const real_t &duration) {
CHECK_BEFORE_CALL();
LOCK_GUARD(dgc->datalock);
LOCK_GUARD(datalock);
dgc->geometry_pool.add_or_update_line(duration, { a, b }, IS_DEFAULT_COLOR(color) ? Colors::red : color);
}

void DebugDraw3D::draw_lines(const PackedVector3Array &lines, const Color &color, const real_t &duration) {
CHECK_BEFORE_CALL();
LOCK_GUARD(dgc->datalock);
LOCK_GUARD(datalock);

if (lines.size() % 2 != 0) {
PRINT_ERROR("The size of the lines array must be even. " + String::num_int64(lines.size()) + " is not even.");
Expand All @@ -418,7 +435,7 @@ void DebugDraw3D::draw_lines(const PackedVector3Array &lines, const Color &color

void DebugDraw3D::draw_lines_c(const std::vector<Vector3> &lines, const Color &color, const real_t &duration) {
CHECK_BEFORE_CALL();
LOCK_GUARD(dgc->datalock);
LOCK_GUARD(datalock);

if (lines.size() % 2 != 0) {
PRINT_ERROR("The size of the lines array must be even. " + String::num_int64(lines.size()) + " is not even.");
Expand All @@ -430,7 +447,7 @@ void DebugDraw3D::draw_lines_c(const std::vector<Vector3> &lines, const Color &c

void DebugDraw3D::draw_ray(const Vector3 &origin, const Vector3 &direction, const real_t &length, const Color &color, const real_t &duration) {
CHECK_BEFORE_CALL();
LOCK_GUARD(dgc->datalock);
LOCK_GUARD(datalock);
dgc->geometry_pool.add_or_update_line(duration, { origin, origin + direction * length }, IS_DEFAULT_COLOR(color) ? Colors::red : color);
}

Expand All @@ -443,7 +460,7 @@ void DebugDraw3D::draw_line_path(const PackedVector3Array &path, const Color &co
return;
}

LOCK_GUARD(dgc->datalock);
LOCK_GUARD(datalock);
dgc->geometry_pool.add_or_update_line(duration, GeometryGenerator::CreateLinesFromPath(path), IS_DEFAULT_COLOR(color) ? Colors::light_green : color);
}

Expand All @@ -452,7 +469,7 @@ void DebugDraw3D::draw_line_path(const PackedVector3Array &path, const Color &co

void DebugDraw3D::create_arrow(const Vector3 &a, const Vector3 &b, const Color &color, const real_t &arrow_size, const bool &is_absolute_size, const real_t &duration) {
CHECK_BEFORE_CALL();
LOCK_GUARD(dgc->datalock);
LOCK_GUARD(datalock);
Vector3 dir = (b - a);
real_t size = (is_absolute_size ? arrow_size : dir.length() * arrow_size) * 2;
Vector3 pos = b - dir.normalized() * size;
Expand All @@ -473,7 +490,7 @@ void DebugDraw3D::create_arrow(const Vector3 &a, const Vector3 &b, const Color &

void DebugDraw3D::draw_arrow(const Transform3D &transform, const Color &color, const real_t &duration) {
CHECK_BEFORE_CALL();
LOCK_GUARD(dgc->datalock);
LOCK_GUARD(datalock);

dgc->geometry_pool.add_or_update_instance(
InstanceType::ARROWHEADS,
Expand All @@ -485,7 +502,7 @@ void DebugDraw3D::draw_arrow(const Transform3D &transform, const Color &color, c

void DebugDraw3D::draw_arrow_line(const Vector3 &a, const Vector3 &b, const Color &color, const real_t &arrow_size, const bool &is_absolute_size, const real_t &duration) {
CHECK_BEFORE_CALL();
LOCK_GUARD(dgc->datalock);
LOCK_GUARD(datalock);
dgc->geometry_pool.add_or_update_line(duration, { a, b }, IS_DEFAULT_COLOR(color) ? Colors::light_green : color);

create_arrow(a, b, color, arrow_size, is_absolute_size, duration);
Expand All @@ -498,7 +515,7 @@ void DebugDraw3D::draw_arrow_ray(const Vector3 &origin, const Vector3 &direction

void DebugDraw3D::draw_arrow_path(const PackedVector3Array &path, const Color &color, const real_t &arrow_size, const bool &is_absolute_size, const real_t &duration) {
CHECK_BEFORE_CALL();
LOCK_GUARD(dgc->datalock);
LOCK_GUARD(datalock);
dgc->geometry_pool.add_or_update_line(duration, GeometryGenerator::CreateLinesFromPath(path), IS_DEFAULT_COLOR(color) ? Colors::light_green : color);

for (int i = 0; i < path.size() - 1; i++) {
Expand All @@ -525,7 +542,7 @@ void DebugDraw3D::draw_square(const Vector3 &position, const real_t &size, const
Transform3D t(Basis(), position);
t.basis.scale(Vector3_ONE * size);

LOCK_GUARD(dgc->datalock);
LOCK_GUARD(datalock);
dgc->geometry_pool.add_or_update_instance(
InstanceType::BILLBOARD_SQUARES,
duration,
Expand All @@ -543,7 +560,7 @@ void DebugDraw3D::draw_points(const PackedVector3Array &points, const real_t &si

void DebugDraw3D::draw_position(const Transform3D &transform, const Color &color, const real_t &duration) {
CHECK_BEFORE_CALL();
LOCK_GUARD(dgc->datalock);
LOCK_GUARD(datalock);
dgc->geometry_pool.add_or_update_instance(
InstanceType::POSITIONS,
duration,
Expand All @@ -554,7 +571,7 @@ void DebugDraw3D::draw_position(const Transform3D &transform, const Color &color

void DebugDraw3D::draw_gizmo(const Transform3D &transform, const Color &color, const bool &is_centered, const real_t &duration) {
CHECK_BEFORE_CALL();
LOCK_GUARD(dgc->datalock);
LOCK_GUARD(datalock);

bool is_color_empty = IS_DEFAULT_COLOR(color);
#define COLOR(axis) is_color_empty ? Colors::axis_##axis : color
Expand Down Expand Up @@ -587,7 +604,7 @@ void DebugDraw3D::draw_grid_xf(const Transform3D &transform, const Vector2i &_su
#define MAX_SUBDIVISIONS 1024 * 1024
ERR_FAIL_COND(_subdivision.x > MAX_SUBDIVISIONS);
ERR_FAIL_COND(_subdivision.y > MAX_SUBDIVISIONS);
LOCK_GUARD(dgc->datalock);
LOCK_GUARD(datalock);

Vector2i subdivision = _subdivision.abs();
subdivision = Vector2i(Math::clamp(subdivision.x, 1, MAX_SUBDIVISIONS), Math::clamp(subdivision.y, 1, MAX_SUBDIVISIONS));
Expand Down Expand Up @@ -622,7 +639,7 @@ void DebugDraw3D::draw_camera_frustum_planes_c(const std::array<Plane, 6> &plane
CHECK_BEFORE_CALL();
auto lines = GeometryGenerator::CreateCameraFrustumLines(planes);

LOCK_GUARD(dgc->datalock);
LOCK_GUARD(datalock);
dgc->geometry_pool.add_or_update_line(duration, lines, IS_DEFAULT_COLOR(color) ? Colors::red : color);
}

Expand Down
7 changes: 7 additions & 0 deletions src/3d/debug_draw_3d.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ GODOT_WARNING_DISABLE()
GODOT_WARNING_RESTORE()

#include <memory>
#include <mutex>

using namespace godot;

Expand All @@ -23,6 +24,7 @@ class DebugDraw3D : public Object {
GDCLASS(DebugDraw3D, Object)

friend DebugDrawManager;
friend DebugGeometryContainer;

private:
static DebugDraw3D *singleton;
Expand All @@ -31,6 +33,11 @@ class DebugDraw3D : public Object {
DebugDrawManager *root_node = nullptr;

#ifndef DISABLE_DEBUG_RENDERING
#ifdef DEV_ENABLED
const char *reload_action_name = "ui_end";
#endif
std::recursive_mutex datalock;

// Meshes
std::unique_ptr<DebugGeometryContainer> dgc;

Expand Down
Loading

0 comments on commit 5f48569

Please sign in to comment.