Added option in project settings to draw Shape2D outlines

Disabling collision outlines can be useful for performance when the game
is running and many collision shapes are displayed.
This commit is contained in:
PouleyKetchoupp 2021-02-25 06:56:47 -07:00
parent 2ffecb76ed
commit c4b116cff7
8 changed files with 54 additions and 25 deletions

View file

@ -460,6 +460,9 @@
<member name="debug/shapes/collision/contact_color" type="Color" setter="" getter="" default="Color( 1, 0.2, 0.1, 0.8 )"> <member name="debug/shapes/collision/contact_color" type="Color" setter="" getter="" default="Color( 1, 0.2, 0.1, 0.8 )">
Color of the contact points between collision shapes, visible when "Visible Collision Shapes" is enabled in the Debug menu. Color of the contact points between collision shapes, visible when "Visible Collision Shapes" is enabled in the Debug menu.
</member> </member>
<member name="debug/shapes/collision/draw_2d_outlines" type="bool" setter="" getter="" default="true">
Sets whether 2D physics will display collision outlines in game when "Visible Collision Shapes" is enabled in the Debug menu.
</member>
<member name="debug/shapes/collision/max_contacts_displayed" type="int" setter="" getter="" default="10000"> <member name="debug/shapes/collision/max_contacts_displayed" type="int" setter="" getter="" default="10000">
Maximum number of contact points between collision shapes to display when "Visible Collision Shapes" is enabled in the Debug menu. Maximum number of contact points between collision shapes to display when "Visible Collision Shapes" is enabled in the Debug menu.
</member> </member>

View file

@ -1350,6 +1350,8 @@ SceneTree::SceneTree() {
collision_debug_contacts = GLOBAL_DEF("debug/shapes/collision/max_contacts_displayed", 10000); collision_debug_contacts = GLOBAL_DEF("debug/shapes/collision/max_contacts_displayed", 10000);
ProjectSettings::get_singleton()->set_custom_property_info("debug/shapes/collision/max_contacts_displayed", PropertyInfo(Variant::INT, "debug/shapes/collision/max_contacts_displayed", PROPERTY_HINT_RANGE, "0,20000,1")); // No negative ProjectSettings::get_singleton()->set_custom_property_info("debug/shapes/collision/max_contacts_displayed", PropertyInfo(Variant::INT, "debug/shapes/collision/max_contacts_displayed", PROPERTY_HINT_RANGE, "0,20000,1")); // No negative
GLOBAL_DEF("debug/shapes/collision/draw_2d_outlines", true);
// Create with mainloop. // Create with mainloop.
root = memnew(Window); root = memnew(Window);

View file

@ -85,9 +85,11 @@ void CapsuleShape2D::draw(const RID &p_to_rid, const Color &p_color) {
Vector<Color> col; Vector<Color> col;
col.push_back(p_color); col.push_back(p_color);
RenderingServer::get_singleton()->canvas_item_add_polygon(p_to_rid, points, col); RenderingServer::get_singleton()->canvas_item_add_polygon(p_to_rid, points, col);
RenderingServer::get_singleton()->canvas_item_add_polyline(p_to_rid, points, col); if (is_collision_outline_enabled()) {
// Draw the last segment as it's not drawn by `canvas_item_add_polyline()`. RenderingServer::get_singleton()->canvas_item_add_polyline(p_to_rid, points, col);
RenderingServer::get_singleton()->canvas_item_add_line(p_to_rid, points[points.size() - 1], points[0], p_color); // Draw the last segment as it's not drawn by `canvas_item_add_polyline()`.
RenderingServer::get_singleton()->canvas_item_add_line(p_to_rid, points[points.size() - 1], points[0], p_color);
}
} }
Rect2 CapsuleShape2D::get_rect() const { Rect2 CapsuleShape2D::get_rect() const {

View file

@ -79,9 +79,11 @@ void CircleShape2D::draw(const RID &p_to_rid, const Color &p_color) {
Vector<Color> col; Vector<Color> col;
col.push_back(p_color); col.push_back(p_color);
RenderingServer::get_singleton()->canvas_item_add_polygon(p_to_rid, points, col); RenderingServer::get_singleton()->canvas_item_add_polygon(p_to_rid, points, col);
RenderingServer::get_singleton()->canvas_item_add_polyline(p_to_rid, points, col); if (is_collision_outline_enabled()) {
// Draw the last segment as it's not drawn by `canvas_item_add_polyline()`. RenderingServer::get_singleton()->canvas_item_add_polyline(p_to_rid, points, col);
RenderingServer::get_singleton()->canvas_item_add_line(p_to_rid, points[points.size() - 1], points[0], p_color); // Draw the last segment as it's not drawn by `canvas_item_add_polyline()`.
RenderingServer::get_singleton()->canvas_item_add_line(p_to_rid, points[points.size() - 1], points[0], p_color);
}
} }
CircleShape2D::CircleShape2D() : CircleShape2D::CircleShape2D() :

View file

@ -75,9 +75,11 @@ void ConvexPolygonShape2D::draw(const RID &p_to_rid, const Color &p_color) {
Vector<Color> col; Vector<Color> col;
col.push_back(p_color); col.push_back(p_color);
RenderingServer::get_singleton()->canvas_item_add_polygon(p_to_rid, points, col); RenderingServer::get_singleton()->canvas_item_add_polygon(p_to_rid, points, col);
RenderingServer::get_singleton()->canvas_item_add_polyline(p_to_rid, points, col); if (is_collision_outline_enabled()) {
// Draw the last segment as it's not drawn by `canvas_item_add_polyline()`. RenderingServer::get_singleton()->canvas_item_add_polyline(p_to_rid, points, col);
RenderingServer::get_singleton()->canvas_item_add_line(p_to_rid, points[points.size() - 1], points[0], p_color); // Draw the last segment as it's not drawn by `canvas_item_add_polyline()`.
RenderingServer::get_singleton()->canvas_item_add_line(p_to_rid, points[points.size() - 1], points[0], p_color);
}
} }
Rect2 ConvexPolygonShape2D::get_rect() const { Rect2 ConvexPolygonShape2D::get_rect() const {

View file

@ -47,23 +47,25 @@ Vector2 RectangleShape2D::get_size() const {
} }
void RectangleShape2D::draw(const RID &p_to_rid, const Color &p_color) { void RectangleShape2D::draw(const RID &p_to_rid, const Color &p_color) {
// Draw an outlined rectangle to make individual shapes easier to distinguish.
Vector<Vector2> stroke_points;
stroke_points.resize(5);
stroke_points.write[0] = -size * 0.5;
stroke_points.write[1] = Vector2(size.x, -size.y) * 0.5;
stroke_points.write[2] = size * 0.5;
stroke_points.write[3] = Vector2(-size.x, size.y) * 0.5;
stroke_points.write[4] = -size * 0.5;
Vector<Color> stroke_colors;
stroke_colors.resize(5);
for (int i = 0; i < 5; i++) {
stroke_colors.write[i] = (p_color);
}
RenderingServer::get_singleton()->canvas_item_add_rect(p_to_rid, Rect2(-size * 0.5, size), p_color); RenderingServer::get_singleton()->canvas_item_add_rect(p_to_rid, Rect2(-size * 0.5, size), p_color);
RenderingServer::get_singleton()->canvas_item_add_polyline(p_to_rid, stroke_points, stroke_colors); if (is_collision_outline_enabled()) {
// Draw an outlined rectangle to make individual shapes easier to distinguish.
Vector<Vector2> stroke_points;
stroke_points.resize(5);
stroke_points.write[0] = -size * 0.5;
stroke_points.write[1] = Vector2(size.x, -size.y) * 0.5;
stroke_points.write[2] = size * 0.5;
stroke_points.write[3] = Vector2(-size.x, size.y) * 0.5;
stroke_points.write[4] = -size * 0.5;
Vector<Color> stroke_colors;
stroke_colors.resize(5);
for (int i = 0; i < 5; i++) {
stroke_colors.write[i] = (p_color);
}
RenderingServer::get_singleton()->canvas_item_add_polyline(p_to_rid, stroke_points, stroke_colors);
}
} }
Rect2 RectangleShape2D::get_rect() const { Rect2 RectangleShape2D::get_rect() const {

View file

@ -29,7 +29,11 @@
/*************************************************************************/ /*************************************************************************/
#include "shape_2d.h" #include "shape_2d.h"
#include "core/config/engine.h"
#include "core/config/project_settings.h"
#include "servers/physics_server_2d.h" #include "servers/physics_server_2d.h"
RID Shape2D::get_rid() const { RID Shape2D::get_rid() const {
return shape; return shape;
} }
@ -105,6 +109,15 @@ void Shape2D::_bind_methods() {
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "custom_solver_bias", PROPERTY_HINT_RANGE, "0,1,0.001"), "set_custom_solver_bias", "get_custom_solver_bias"); ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "custom_solver_bias", PROPERTY_HINT_RANGE, "0,1,0.001"), "set_custom_solver_bias", "get_custom_solver_bias");
} }
bool Shape2D::is_collision_outline_enabled() {
#ifdef TOOLS_ENABLED
if (Engine::get_singleton()->is_editor_hint()) {
return true;
}
#endif
return GLOBAL_DEF("debug/shapes/collision/draw_2d_outlines", true);
}
Shape2D::Shape2D(const RID &p_rid) { Shape2D::Shape2D(const RID &p_rid) {
shape = p_rid; shape = p_rid;
} }

View file

@ -61,6 +61,9 @@ public:
/// Returns the radius of a circle that fully enclose this shape /// Returns the radius of a circle that fully enclose this shape
virtual real_t get_enclosing_radius() const = 0; virtual real_t get_enclosing_radius() const = 0;
virtual RID get_rid() const override; virtual RID get_rid() const override;
static bool is_collision_outline_enabled();
Shape2D(); Shape2D();
~Shape2D(); ~Shape2D();
}; };