Skip to content

Commit

Permalink
some broken code
Browse files Browse the repository at this point in the history
  • Loading branch information
DmitriySalnikov committed Sep 16, 2023
1 parent 3b719b1 commit 8cfe169
Show file tree
Hide file tree
Showing 12 changed files with 301 additions and 221 deletions.
7 changes: 6 additions & 1 deletion examples_dd3d/DebugDrawDemoScene.gd
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,15 @@ func _process(delta: float) -> void:
$LagTest.visible = false
return

## 2D

DebugDraw2D.draw_set_transform_2d(Vector2(100,1), deg_to_rad(45), Vector2(0.5, 1))
DebugDraw2D.draw_line(Vector2(20,0), Vector2(100,0), Color.BROWN, 2, true)
var trans = DebugDraw2D.draw_set_transform_2d(Vector2(100,1), deg_to_rad(45), Vector2(0.5, 1))
DebugDraw2D.draw_line(Vector2(20,2), Vector2(100,2), Color.CADET_BLUE, -1, false)
if true:
var trans2 = DebugDraw2D.draw_set_transform_2d(Vector2(1,1), deg_to_rad(180))
var trans2 = DebugDraw2D.draw_set_transform_2d(Vector2(1,1), deg_to_rad(90))
DebugDraw2D.draw_line(Vector2(20,4), Vector2(100,4), Color.DARK_BLUE, -1, false)
trans = null

$HitTest.visible = true
Expand Down
45 changes: 36 additions & 9 deletions src/2d/debug_draw_2d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include "config_2d.h"
#include "debug_draw_manager.h"
#include "geometry_container_2d.h"
#include "graphs.h"
#include "grouped_text.h"
#include "stats_2d.h"
Expand All @@ -14,13 +15,11 @@ GODOT_WARNING_RESTORE()
#include <limits.h>

#define NEED_LEAVE (!debug_enabled)

std::atomic<uint64_t> DDTransform2DGuard::create_counter = 0;

DDTransform2DGuard::DDTransform2DGuard() {
guard_id = create_counter++;
thread_id = OS::get_singleton()->get_thread_caller_id();

DebugDraw2D::get_singleton()->register_transform_guard(guard_id, thread_id, Transform2D());
ERR_FAIL_MSG(NAMEOF(DDTransform2DGuard) " is not intended to be instantiated directly");
}

DDTransform2DGuard::DDTransform2DGuard(const Transform2D &xform) {
Expand All @@ -47,6 +46,7 @@ void DebugDraw2D::_bind_methods() {
REG_PROP(empty_color, Variant::COLOR);
REG_PROP_BOOL(debug_enabled);

REG_PROP(default_transform_2d, Variant::TRANSFORM2D);
REG_PROP(config, Variant::OBJECT);

REG_PROP(custom_canvas, Variant::OBJECT);
Expand All @@ -58,7 +58,7 @@ void DebugDraw2D::_bind_methods() {
ClassDB::bind_method(D_METHOD(NAMEOF(clear_2d_objects)), &DebugDraw2D::clear_2d_objects);

const int DEFAULT_FONT_SIZE = 16;
// ClassDB::bind_method(D_METHOD(NAMEOF(draw_line), "from", "to", "color", "width", "antialiased"), &DebugDraw2D::draw_line, -1.0, false);
ClassDB::bind_method(D_METHOD(NAMEOF(draw_line), "from", "to", "color", "width", "antialiased"), &DebugDraw2D::draw_line, -1.0, false);
// ClassDB::bind_method(D_METHOD(NAMEOF(draw_dashed_line), "from", "to", "color", "width", "dash", "aligned"), &DebugDraw2D::draw_dashed_line, -1.0, 2.0, true);
// ClassDB::bind_method(D_METHOD(NAMEOF(draw_polyline), "points", "color", "width", "antialiased"), &DebugDraw2D::draw_polyline, -1.0, false);
// ClassDB::bind_method(D_METHOD(NAMEOF(draw_polyline_colors), "points", "colors", "width", "antialiased"), &DebugDraw2D::draw_polyline_colors, -1.0, false);
Expand All @@ -70,8 +70,6 @@ void DebugDraw2D::_bind_methods() {
// ClassDB::bind_method(D_METHOD(NAMEOF(draw_texture), "texture", "position", "modulate"), &DebugDraw2D::draw_texture, Color(1, 1, 1, 1));
// ClassDB::bind_method(D_METHOD(NAMEOF(draw_texture_rect), "texture", "rect", "tile", "modulate", "transpose"), &DebugDraw2D::draw_texture_rect, Color(1, 1, 1, 1), false);
// ClassDB::bind_method(D_METHOD(NAMEOF(draw_texture_rect_region), "texture", "rect", "src_rect", "modulate", "transpose", "clip_uv"), &DebugDraw2D::draw_texture_rect_region, Color(1, 1, 1, 1), false, true);
// ClassDB::bind_method(D_METHOD(NAMEOF(draw_msdf_texture_rect_region), "texture", "rect", "src_rect", "modulate", "outline", "pixel_range", "scale"), &DebugDraw2D::draw_msdf_texture_rect_region, Color(1, 1, 1, 1), 0.0, 4.0, 1.0);
// ClassDB::bind_method(D_METHOD(NAMEOF(draw_lcd_texture_rect_region), "texture", "rect", "src_rect", "modulate"), &DebugDraw2D::draw_lcd_texture_rect_region, Color(1, 1, 1, 1));
// ClassDB::bind_method(D_METHOD(NAMEOF(draw_style_box), "style_box", "rect"), &DebugDraw2D::draw_style_box);
// ClassDB::bind_method(D_METHOD(NAMEOF(draw_primitive), "points", "colors", "uvs", "texture"), &DebugDraw2D::draw_primitive, Ref<Texture2D>());
// ClassDB::bind_method(D_METHOD(NAMEOF(draw_polygon), "points", "colors", "uvs", "texture"), &DebugDraw2D::draw_polygon, PackedVector2Array(), Ref<Texture2D>());
Expand All @@ -80,8 +78,6 @@ void DebugDraw2D::_bind_methods() {
// ClassDB::bind_method(D_METHOD(NAMEOF(draw_multiline_string), "font", "pos", "text", "alignment", "width", "font_size", "max_lines", "modulate", "brk_flags", "justification_flags", "direction", "orientation"), &DebugDraw2D::draw_multiline_string, HORIZONTAL_ALIGNMENT_LEFT, -1, DEFAULT_FONT_SIZE, -1, Color(1.0, 1.0, 1.0), TextServer::BREAK_MANDATORY | TextServer::BREAK_WORD_BOUND, TextServer::JUSTIFICATION_KASHIDA | TextServer::JUSTIFICATION_WORD_BOUND, TextServer::DIRECTION_AUTO, TextServer::ORIENTATION_HORIZONTAL);
// ClassDB::bind_method(D_METHOD(NAMEOF(draw_string_outline), "font", "pos", "text", "alignment", "width", "font_size", "size", "modulate", "justification_flags", "direction", "orientation"), &DebugDraw2D::draw_string_outline, HORIZONTAL_ALIGNMENT_LEFT, -1, DEFAULT_FONT_SIZE, 1, Color(1.0, 1.0, 1.0), TextServer::JUSTIFICATION_KASHIDA | TextServer::JUSTIFICATION_WORD_BOUND, TextServer::DIRECTION_AUTO, TextServer::ORIENTATION_HORIZONTAL);
// ClassDB::bind_method(D_METHOD(NAMEOF(draw_multiline_string_outline), "font", "pos", "text", "alignment", "width", "font_size", "max_lines", "size", "modulate", "brk_flags", "justification_flags", "direction", "orientation"), &DebugDraw2D::draw_multiline_string_outline, HORIZONTAL_ALIGNMENT_LEFT, -1, DEFAULT_FONT_SIZE, -1, 1, Color(1.0, 1.0, 1.0), TextServer::BREAK_MANDATORY | TextServer::BREAK_WORD_BOUND, TextServer::JUSTIFICATION_KASHIDA | TextServer::JUSTIFICATION_WORD_BOUND, TextServer::DIRECTION_AUTO, TextServer::ORIENTATION_HORIZONTAL);
// ClassDB::bind_method(D_METHOD(NAMEOF(draw_char), "font", "pos", "char", "font_size", "modulate"), &DebugDraw2D::draw_char, DEFAULT_FONT_SIZE, Color(1.0, 1.0, 1.0));
// ClassDB::bind_method(D_METHOD(NAMEOF(draw_char_outline), "font", "pos", "char", "font_size", "size", "modulate"), &DebugDraw2D::draw_char_outline, DEFAULT_FONT_SIZE, -1, Color(1.0, 1.0, 1.0));
// ClassDB::bind_method(D_METHOD(NAMEOF(draw_mesh), "mesh", "texture", "transform", "modulate"), &DebugDraw2D::draw_mesh, Transform2D(), Color(1, 1, 1, 1));
// ClassDB::bind_method(D_METHOD(NAMEOF(draw_multimesh), "multimesh", "texture"), &DebugDraw2D::draw_multimesh);

Expand Down Expand Up @@ -183,6 +179,7 @@ void DebugDraw2D::_on_canvas_item_draw(Control *ci) {
#ifndef DISABLE_DEBUG_RENDERING
Vector2 vp_size = ci->has_meta("UseParentSize") ? Object::cast_to<Control>(ci->get_parent())->get_rect().size : ci->get_rect().size;

_draw_2d_geometry(ci);
grouped_text->draw(ci, _font, vp_size);
data_graphs->draw(ci, _font, vp_size, ci->get_process_delta_time());

Expand All @@ -192,6 +189,24 @@ void DebugDraw2D::_on_canvas_item_draw(Control *ci) {
#endif
}

void DebugDraw2D::_draw_2d_geometry(Control *ci) {
Window *root = SCENE_ROOT();
Camera2D *cam = root->get_camera_2d();

Vector2i size = root->get_size();
Transform2D xform = root->get_viewport()->get_canvas_transform();

Transform2D xform_sized = xform.scaled(size);
Rect2 camera_rect = Rect2(Point2(), size);
Rect2 camera_rect_res = xform.xform(camera_rect);

//PRINT(camera_rect_res);

if (cam) {
//camera_rect = cam->get_rec;
}
}

void DebugDraw2D::mark_canvas_dirty() {
_canvas_need_update = true;
}
Expand Down Expand Up @@ -221,6 +236,14 @@ bool DebugDraw2D::is_debug_enabled() const {
return debug_enabled;
}

void DebugDraw2D::set_default_transform_2d(const Transform2D &_xform) {
default_transform_2d = _xform;
}

Transform2D DebugDraw2D::get_default_transform_2d() const {
return default_transform_2d;
}

void DebugDraw2D::set_config(Ref<DebugDrawConfig2D> _cfg) {
if (_cfg.is_valid()) {
#ifndef DISABLE_DEBUG_RENDERING
Expand Down Expand Up @@ -344,6 +367,10 @@ void DebugDraw2D::clear_2d_objects() {
}

#pragma region 2D

void DebugDraw2D::draw_line(const Vector2 &from, const Vector2 &to, const Color &color, double width, bool antialiased) {
}

#pragma region Text

#ifndef DISABLE_DEBUG_RENDERING
Expand Down
12 changes: 8 additions & 4 deletions src/2d/debug_draw_2d.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class DebugDrawManager;
class DebugDrawConfig2D;
class DebugDrawGraph;
class DebugDrawStats2D;
class GeometryContainer2D;
class GroupedText;

class DDTransform2DGuard : public RefCounted {
Expand Down Expand Up @@ -73,12 +74,16 @@ class DebugDraw2D : public Object {

// Graphs
std::unique_ptr<DataGraphManager> data_graphs;

// Objects
std::unique_ptr<GeometryContainer2D> dgc2d;
#endif

bool is_ready = false;

void _on_canvas_marked_dirty();
void _on_canvas_item_draw(Control *ci);
void _draw_2d_geometry(Control *ci);

void process(double delta);

Expand Down Expand Up @@ -128,6 +133,9 @@ class DebugDraw2D : public Object {
void set_debug_enabled(const bool &_state);
bool is_debug_enabled() const;

void set_default_transform_2d(const Transform2D &_xform);
Transform2D get_default_transform_2d() const;

void set_config(Ref<DebugDrawConfig2D> _cfg);
Ref<DebugDrawConfig2D> get_config() const;

Expand Down Expand Up @@ -160,8 +168,6 @@ class DebugDraw2D : public Object {
void draw_texture(const Ref<Texture2D> &texture, const Vector2 &position, const Color &modulate = Color(1, 1, 1, 1));
void draw_texture_rect(const Ref<Texture2D> &texture, const Rect2 &rect, bool tile, const Color &modulate = Color(1, 1, 1, 1), bool transpose = false);
void draw_texture_rect_region(const Ref<Texture2D> &texture, const Rect2 &rect, const Rect2 &src_rect, const Color &modulate = Color(1, 1, 1, 1), bool transpose = false, bool clip_uv = true);
void draw_msdf_texture_rect_region(const Ref<Texture2D> &texture, const Rect2 &rect, const Rect2 &src_rect, const Color &modulate = Color(1, 1, 1, 1), double outline = 0.0, double pixel_range = 4.0, double scale = 1.0);
void draw_lcd_texture_rect_region(const Ref<Texture2D> &texture, const Rect2 &rect, const Rect2 &src_rect, const Color &modulate = Color(1, 1, 1, 1));
void draw_style_box(const Ref<StyleBox> &style_box, const Rect2 &rect);
void draw_primitive(const PackedVector2Array &points, const PackedColorArray &colors, const PackedVector2Array &uvs, const Ref<Texture2D> &texture = nullptr);
void draw_polygon(const PackedVector2Array &points, const PackedColorArray &colors, const PackedVector2Array &uvs = PackedVector2Array(), const Ref<Texture2D> &texture = nullptr);
Expand All @@ -170,8 +176,6 @@ class DebugDraw2D : public Object {
void draw_multiline_string(const Ref<Font> &font, const Vector2 &pos, const String &text, HorizontalAlignment alignment = (HorizontalAlignment)0, double width = -1, int32_t font_size = 16, int32_t max_lines = -1, const Color &modulate = Color(1, 1, 1, 1), BitField<TextServer::LineBreakFlag> brk_flags = (BitField<TextServer::LineBreakFlag>)3, BitField<TextServer::JustificationFlag> justification_flags = (BitField<TextServer::JustificationFlag>)3, TextServer::Direction direction = (TextServer::Direction)0, TextServer::Orientation orientation = (TextServer::Orientation)0) const;
void draw_string_outline(const Ref<Font> &font, const Vector2 &pos, const String &text, HorizontalAlignment alignment = (HorizontalAlignment)0, double width = -1, int32_t font_size = 16, int32_t size = 1, const Color &modulate = Color(1, 1, 1, 1), BitField<TextServer::JustificationFlag> justification_flags = (BitField<TextServer::JustificationFlag>)3, TextServer::Direction direction = (TextServer::Direction)0, TextServer::Orientation orientation = (TextServer::Orientation)0) const;
void draw_multiline_string_outline(const Ref<Font> &font, const Vector2 &pos, const String &text, HorizontalAlignment alignment = (HorizontalAlignment)0, double width = -1, int32_t font_size = 16, int32_t max_lines = -1, int32_t size = 1, const Color &modulate = Color(1, 1, 1, 1), BitField<TextServer::LineBreakFlag> brk_flags = (BitField<TextServer::LineBreakFlag>)3, BitField<TextServer::JustificationFlag> justification_flags = (BitField<TextServer::JustificationFlag>)3, TextServer::Direction direction = (TextServer::Direction)0, TextServer::Orientation orientation = (TextServer::Orientation)0) const;
void draw_char(const Ref<Font> &font, const Vector2 &pos, const String &_char, int32_t font_size = 16, const Color &modulate = Color(1, 1, 1, 1)) const;
void draw_char_outline(const Ref<Font> &font, const Vector2 &pos, const String &_char, int32_t font_size = 16, int32_t size = -1, const Color &modulate = Color(1, 1, 1, 1)) const;
void draw_mesh(const Ref<Mesh> &mesh, const Ref<Texture2D> &texture, const Transform2D &transform = Transform2D(), const Color &modulate = Color(1, 1, 1, 1));
void draw_multimesh(const Ref<MultiMesh> &multimesh, const Ref<Texture2D> &texture);

Expand Down
28 changes: 18 additions & 10 deletions src/2d/geometry_container_2d.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,23 @@ using namespace godot;
class DebugDrawStats3D;

enum class InstanceType2D : char {
CUBES,
CUBES_CENTERED,
ARROWHEADS,
BILLBOARD_SQUARES,
POSITIONS,
SPHERES,
SPHERES_HD,
CYLINDERS,
ALL,
Line,
DashedLine,
PolylineColors, // Used to draw Polyline, PolylineColors
Arc,
MultilineColors, // Used to draw Multiline, MultilineColors
Rect,
Circle,
Texture,
TextureRect,
TextureRectRegion,
StyleBox,
Primitive,
ColoredPolygon, // Used to draw Polygon, ColoredPolygon
MultilineStringOutline, // Used to draw String, MultilineString, StringOutline, MultilineStringOutline
Mesh,
MultiMesh,
ALL
};

class GeometryPoolDistanceCullingData2D {
Expand Down Expand Up @@ -119,7 +127,7 @@ class DelayedRenderer2D {
class DelayedRendererInstance2D : public DelayedRenderer2D<SphereBounds> {
public:
Transform3D transform;
InstanceType2D type = InstanceType2D::CUBES;
InstanceType2D type = InstanceType2D::Line;

DelayedRendererInstance2D(){};
// void update(real_t _exp_time, const InstanceType2D &_type, const Transform3D &_transform, const Color &_col, const SphereBounds &_bounds);
Expand Down
16 changes: 8 additions & 8 deletions src/3d/debug_draw_3d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ void DebugDraw3D::draw_sphere_xf_base(const Transform3D &transform, const Color
CHECK_BEFORE_CALL();
LOCK_GUARD(dgc->datalock);
dgc->geometry_pool.add_or_update_instance(
hd ? InstanceType::SPHERES_HD : InstanceType::SPHERES,
hd ? InstanceType::SphereHD : InstanceType::Sphere,
duration,
transform,
IS_DEFAULT_COLOR(color) ? Colors::chartreuse : color,
Expand Down Expand Up @@ -256,7 +256,7 @@ void DebugDraw3D::draw_cylinder(const Transform3D &transform, const Color &color
CHECK_BEFORE_CALL();
LOCK_GUARD(dgc->datalock);
dgc->geometry_pool.add_or_update_instance(
InstanceType::CYLINDERS,
InstanceType::Cylinder,
duration,
transform,
IS_DEFAULT_COLOR(color) ? Colors::forest_green : color,
Expand All @@ -282,7 +282,7 @@ void DebugDraw3D::draw_box_xf(const Transform3D &transform, const Color &color,
sb.position = transform.origin + transform.basis.get_scale() * 0.5f;
}
dgc->geometry_pool.add_or_update_instance(
is_box_centered ? InstanceType::CUBES_CENTERED : InstanceType::CUBES,
is_box_centered ? InstanceType::CubeCentered : InstanceType::Cube,
duration,
transform,
IS_DEFAULT_COLOR(color) ? Colors::forest_green : color,
Expand Down Expand Up @@ -314,7 +314,7 @@ void DebugDraw3D::draw_line_hit(const Vector3 &start, const Vector3 &end, const
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);

dgc->geometry_pool.add_or_update_instance(
InstanceType::BILLBOARD_SQUARES,
InstanceType::BillboardSquare,
duration,
Transform3D(Basis().scaled(Vector3_ONE * hit_size), hit),
IS_DEFAULT_COLOR(hit_color) ? config->get_line_hit_color() : hit_color,
Expand Down Expand Up @@ -407,7 +407,7 @@ void DebugDraw3D::create_arrow(const Vector3 &a, const Vector3 &b, const Color &
t.origin = pos;

dgc->geometry_pool.add_or_update_instance(
InstanceType::ARROWHEADS,
InstanceType::ArrowHead,
duration,
t,
IS_DEFAULT_COLOR(color) ? Colors::light_green : color,
Expand All @@ -419,7 +419,7 @@ void DebugDraw3D::draw_arrow(const Transform3D &transform, const Color &color, c
LOCK_GUARD(dgc->datalock);

dgc->geometry_pool.add_or_update_instance(
InstanceType::ARROWHEADS,
InstanceType::ArrowHead,
duration,
transform,
IS_DEFAULT_COLOR(color) ? Colors::light_green : color,
Expand Down Expand Up @@ -470,7 +470,7 @@ void DebugDraw3D::draw_square(const Vector3 &position, const real_t &size, const

LOCK_GUARD(dgc->datalock);
dgc->geometry_pool.add_or_update_instance(
InstanceType::BILLBOARD_SQUARES,
InstanceType::BillboardSquare,
duration,
t,
IS_DEFAULT_COLOR(color) ? Colors::red : color,
Expand All @@ -488,7 +488,7 @@ void DebugDraw3D::draw_position(const Transform3D &transform, const Color &color
CHECK_BEFORE_CALL();
LOCK_GUARD(dgc->datalock);
dgc->geometry_pool.add_or_update_instance(
InstanceType::POSITIONS,
InstanceType::Position,
duration,
transform,
IS_DEFAULT_COLOR(color) ? Colors::crimson : color,
Expand Down
Loading

0 comments on commit 8cfe169

Please sign in to comment.