From 481299269712a53eb3fd64ba74300367a7f61244 Mon Sep 17 00:00:00 2001 From: lawnjelly Date: Mon, 31 Jul 2023 12:51:59 +0100 Subject: [PATCH] Add debug_canvas_item_get_local_bound() function to VisualServer Useful for debugging hierarchical culling. --- doc/classes/VisualServer.xml | 8 ++++++++ servers/visual/visual_server_canvas.cpp | 6 ++++++ servers/visual/visual_server_canvas.h | 1 + servers/visual/visual_server_raster.h | 1 + servers/visual/visual_server_wrap_mt.h | 1 + servers/visual_server.cpp | 1 + servers/visual_server.h | 9 +++++---- 7 files changed, 23 insertions(+), 4 deletions(-) diff --git a/doc/classes/VisualServer.xml b/doc/classes/VisualServer.xml index cd0460b974ae..82fd0f809941 100644 --- a/doc/classes/VisualServer.xml +++ b/doc/classes/VisualServer.xml @@ -728,6 +728,14 @@ Modulates all colors in the given canvas. + + + + + Returns the bounding rectangle for a canvas item and its descendants in local space, as calculated by the renderer. This bound is used internally for culling. + [b]Warning:[/b] This function is intended for debugging in the editor, and will pass through and return a zero [Rect2] in exported projects. + + diff --git a/servers/visual/visual_server_canvas.cpp b/servers/visual/visual_server_canvas.cpp index 2d4a414a8d66..e4780ede4170 100644 --- a/servers/visual/visual_server_canvas.cpp +++ b/servers/visual/visual_server_canvas.cpp @@ -1409,6 +1409,12 @@ Rect2 VisualServerCanvas::_debug_canvas_item_get_rect(RID p_item) { return canvas_item->get_rect(); } +Rect2 VisualServerCanvas::_debug_canvas_item_get_local_bound(RID p_item) { + Item *canvas_item = canvas_item_owner.getornull(p_item); + ERR_FAIL_COND_V(!canvas_item, Rect2()); + return canvas_item->local_bound; +} + void VisualServerCanvas::canvas_item_set_skeleton_relative_xform(RID p_item, Transform2D p_relative_xform) { Item *canvas_item = canvas_item_owner.getornull(p_item); ERR_FAIL_COND(!canvas_item); diff --git a/servers/visual/visual_server_canvas.h b/servers/visual/visual_server_canvas.h index e59849b9e128..5a231cb8d4b9 100644 --- a/servers/visual/visual_server_canvas.h +++ b/servers/visual/visual_server_canvas.h @@ -258,6 +258,7 @@ class VisualServerCanvas { void canvas_item_attach_skeleton(RID p_item, RID p_skeleton); void canvas_item_set_skeleton_relative_xform(RID p_item, Transform2D p_relative_xform); Rect2 _debug_canvas_item_get_rect(RID p_item); + Rect2 _debug_canvas_item_get_local_bound(RID p_item); void _canvas_item_skeleton_moved(RID p_item); RID canvas_light_create(); diff --git a/servers/visual/visual_server_raster.h b/servers/visual/visual_server_raster.h index a0a1b1db49ae..8df124a2ee08 100644 --- a/servers/visual/visual_server_raster.h +++ b/servers/visual/visual_server_raster.h @@ -722,6 +722,7 @@ class VisualServerRaster : public VisualServer { BIND2(canvas_item_attach_skeleton, RID, RID) BIND2(canvas_item_set_skeleton_relative_xform, RID, Transform2D) BIND1R(Rect2, _debug_canvas_item_get_rect, RID) + BIND1R(Rect2, _debug_canvas_item_get_local_bound, RID) BIND1(canvas_item_clear, RID) BIND2(canvas_item_set_draw_index, RID, int) diff --git a/servers/visual/visual_server_wrap_mt.h b/servers/visual/visual_server_wrap_mt.h index 5230a26ee10e..759bbedc0685 100644 --- a/servers/visual/visual_server_wrap_mt.h +++ b/servers/visual/visual_server_wrap_mt.h @@ -623,6 +623,7 @@ class VisualServerWrapMT : public VisualServer { FUNC2(canvas_item_attach_skeleton, RID, RID) FUNC2(canvas_item_set_skeleton_relative_xform, RID, Transform2D) FUNC1R(Rect2, _debug_canvas_item_get_rect, RID) + FUNC1R(Rect2, _debug_canvas_item_get_local_bound, RID) FUNC1(canvas_item_clear, RID) FUNC2(canvas_item_set_draw_index, RID, int) diff --git a/servers/visual_server.cpp b/servers/visual_server.cpp index 7a2e00cb9643..f7af68c0c629 100644 --- a/servers/visual_server.cpp +++ b/servers/visual_server.cpp @@ -2203,6 +2203,7 @@ void VisualServer::_bind_methods() { ClassDB::bind_method(D_METHOD("canvas_item_set_material", "item", "material"), &VisualServer::canvas_item_set_material); ClassDB::bind_method(D_METHOD("canvas_item_set_use_parent_material", "item", "enabled"), &VisualServer::canvas_item_set_use_parent_material); ClassDB::bind_method(D_METHOD("debug_canvas_item_get_rect", "item"), &VisualServer::debug_canvas_item_get_rect); + ClassDB::bind_method(D_METHOD("debug_canvas_item_get_local_bound", "item"), &VisualServer::debug_canvas_item_get_local_bound); ClassDB::bind_method(D_METHOD("canvas_light_create"), &VisualServer::canvas_light_create); ClassDB::bind_method(D_METHOD("canvas_light_attach_to_canvas", "light", "canvas"), &VisualServer::canvas_light_attach_to_canvas); ClassDB::bind_method(D_METHOD("canvas_light_set_enabled", "light", "enabled"), &VisualServer::canvas_light_set_enabled); diff --git a/servers/visual_server.h b/servers/visual_server.h index 81df90ae7f09..a5c635f0c592 100644 --- a/servers/visual_server.h +++ b/servers/visual_server.h @@ -1056,14 +1056,15 @@ class VisualServer : public Object { virtual void canvas_item_attach_skeleton(RID p_item, RID p_skeleton) = 0; virtual void canvas_item_set_skeleton_relative_xform(RID p_item, Transform2D p_relative_xform) = 0; - Rect2 debug_canvas_item_get_rect(RID p_item) { #ifdef TOOLS_ENABLED - return _debug_canvas_item_get_rect(p_item); + Rect2 debug_canvas_item_get_rect(RID p_item) { return _debug_canvas_item_get_rect(p_item); } + Rect2 debug_canvas_item_get_local_bound(RID p_item) { return _debug_canvas_item_get_local_bound(p_item); } #else - return Rect2(); + Rect2 debug_canvas_item_get_rect(RID p_item) { return Rect2(); } + Rect2 debug_canvas_item_get_local_bound(RID p_item) { return Rect2(); } #endif - } virtual Rect2 _debug_canvas_item_get_rect(RID p_item) = 0; + virtual Rect2 _debug_canvas_item_get_local_bound(RID p_item) = 0; virtual void canvas_item_clear(RID p_item) = 0; virtual void canvas_item_set_draw_index(RID p_item, int p_index) = 0;