Browse Source

Merge pull request #100093 from dalexeev/fix-collision-shape-2d-3d-debug-color

Fix `CollisionShape{2D,3D}.debug_color` inconsistencies
Rémi Verschelde 8 months ago
parent
commit
46c8f8c5c5

+ 3 - 3
doc/classes/CollisionShape2D.xml

@@ -13,9 +13,9 @@
 		<link title="2D Kinematic Character Demo">https://godotengine.org/asset-library/asset/2719</link>
 		<link title="2D Kinematic Character Demo">https://godotengine.org/asset-library/asset/2719</link>
 	</tutorials>
 	</tutorials>
 	<members>
 	<members>
-		<member name="debug_color" type="Color" setter="set_debug_color" getter="get_debug_color" default="Color(0, 0, 0, 1)">
-			The collision shape debug color.
-			[b]Note:[/b] The default value is [member ProjectSettings.debug/shapes/collision/shape_color]. The [code]Color(0, 0, 0, 1)[/code] value documented here is a placeholder, and not the actual default debug color.
+		<member name="debug_color" type="Color" setter="set_debug_color" getter="get_debug_color" default="Color(0, 0, 0, 0)">
+			The collision shape color that is displayed in the editor, or in the running project if [b]Debug &gt; Visible Collision Shapes[/b] is checked at the top of the editor.
+			[b]Note:[/b] The default value is [member ProjectSettings.debug/shapes/collision/shape_color]. The [code]Color(0, 0, 0, 0)[/code] value documented here is a placeholder, and not the actual default debug color.
 		</member>
 		</member>
 		<member name="disabled" type="bool" setter="set_disabled" getter="is_disabled" default="false" keywords="enabled">
 		<member name="disabled" type="bool" setter="set_disabled" getter="is_disabled" default="false" keywords="enabled">
 			A disabled collision shape has no effect in the world. This property should be changed with [method Object.set_deferred].
 			A disabled collision shape has no effect in the world. This property should be changed with [method Object.set_deferred].

+ 2 - 1
doc/classes/CollisionShape3D.xml

@@ -30,7 +30,8 @@
 	</methods>
 	</methods>
 	<members>
 	<members>
 		<member name="debug_color" type="Color" setter="set_debug_color" getter="get_debug_color" default="Color(0, 0, 0, 0)">
 		<member name="debug_color" type="Color" setter="set_debug_color" getter="get_debug_color" default="Color(0, 0, 0, 0)">
-			The collision shape color that is displayed in the editor, or in the running project if [b]Debug &gt; Visible Collision Shapes[/b] is checked at the top of the editor. If this is reset to its default value of [code]Color(0, 0, 0, 0)[/code], the value of [member ProjectSettings.debug/shapes/collision/shape_color] will be used instead.
+			The collision shape color that is displayed in the editor, or in the running project if [b]Debug &gt; Visible Collision Shapes[/b] is checked at the top of the editor.
+			[b]Note:[/b] The default value is [member ProjectSettings.debug/shapes/collision/shape_color]. The [code]Color(0, 0, 0, 0)[/code] value documented here is a placeholder, and not the actual default debug color.
 		</member>
 		</member>
 		<member name="debug_fill" type="bool" setter="set_enable_debug_fill" getter="get_enable_debug_fill" default="true">
 		<member name="debug_fill" type="bool" setter="set_enable_debug_fill" getter="get_enable_debug_fill" default="true">
 			If [code]true[/code], when the shape is displayed, it will show a solid fill color in addition to its wireframe.
 			If [code]true[/code], when the shape is displayed, it will show a solid fill color in addition to its wireframe.

+ 22 - 8
scene/2d/physics/collision_shape_2d.cpp

@@ -49,11 +49,6 @@ void CollisionShape2D::_update_in_shape_owner(bool p_xform_only) {
 	collision_object->shape_owner_set_one_way_collision_margin(owner_id, one_way_collision_margin);
 	collision_object->shape_owner_set_one_way_collision_margin(owner_id, one_way_collision_margin);
 }
 }
 
 
-Color CollisionShape2D::_get_default_debug_color() const {
-	SceneTree *st = SceneTree::get_singleton();
-	return st ? st->get_debug_collisions_color() : Color();
-}
-
 void CollisionShape2D::_notification(int p_what) {
 void CollisionShape2D::_notification(int p_what) {
 	switch (p_what) {
 	switch (p_what) {
 		case NOTIFICATION_PARENTED: {
 		case NOTIFICATION_PARENTED: {
@@ -232,7 +227,18 @@ real_t CollisionShape2D::get_one_way_collision_margin() const {
 	return one_way_collision_margin;
 	return one_way_collision_margin;
 }
 }
 
 
+#ifdef DEBUG_ENABLED
+
+Color CollisionShape2D::_get_default_debug_color() const {
+	const SceneTree *st = SceneTree::get_singleton();
+	return st ? st->get_debug_collisions_color() : Color(0.0, 0.0, 0.0, 0.0);
+}
+
 void CollisionShape2D::set_debug_color(const Color &p_color) {
 void CollisionShape2D::set_debug_color(const Color &p_color) {
+	if (debug_color == p_color) {
+		return;
+	}
+
 	debug_color = p_color;
 	debug_color = p_color;
 	queue_redraw();
 	queue_redraw();
 }
 }
@@ -266,6 +272,8 @@ void CollisionShape2D::_validate_property(PropertyInfo &p_property) const {
 	}
 	}
 }
 }
 
 
+#endif // DEBUG_ENABLED
+
 void CollisionShape2D::_bind_methods() {
 void CollisionShape2D::_bind_methods() {
 	ClassDB::bind_method(D_METHOD("set_shape", "shape"), &CollisionShape2D::set_shape);
 	ClassDB::bind_method(D_METHOD("set_shape", "shape"), &CollisionShape2D::set_shape);
 	ClassDB::bind_method(D_METHOD("get_shape"), &CollisionShape2D::get_shape);
 	ClassDB::bind_method(D_METHOD("get_shape"), &CollisionShape2D::get_shape);
@@ -275,20 +283,26 @@ void CollisionShape2D::_bind_methods() {
 	ClassDB::bind_method(D_METHOD("is_one_way_collision_enabled"), &CollisionShape2D::is_one_way_collision_enabled);
 	ClassDB::bind_method(D_METHOD("is_one_way_collision_enabled"), &CollisionShape2D::is_one_way_collision_enabled);
 	ClassDB::bind_method(D_METHOD("set_one_way_collision_margin", "margin"), &CollisionShape2D::set_one_way_collision_margin);
 	ClassDB::bind_method(D_METHOD("set_one_way_collision_margin", "margin"), &CollisionShape2D::set_one_way_collision_margin);
 	ClassDB::bind_method(D_METHOD("get_one_way_collision_margin"), &CollisionShape2D::get_one_way_collision_margin);
 	ClassDB::bind_method(D_METHOD("get_one_way_collision_margin"), &CollisionShape2D::get_one_way_collision_margin);
-	ClassDB::bind_method(D_METHOD("set_debug_color", "color"), &CollisionShape2D::set_debug_color);
-	ClassDB::bind_method(D_METHOD("get_debug_color"), &CollisionShape2D::get_debug_color);
 
 
 	ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "shape", PROPERTY_HINT_RESOURCE_TYPE, "Shape2D"), "set_shape", "get_shape");
 	ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "shape", PROPERTY_HINT_RESOURCE_TYPE, "Shape2D"), "set_shape", "get_shape");
 	ADD_PROPERTY(PropertyInfo(Variant::BOOL, "disabled"), "set_disabled", "is_disabled");
 	ADD_PROPERTY(PropertyInfo(Variant::BOOL, "disabled"), "set_disabled", "is_disabled");
 	ADD_PROPERTY(PropertyInfo(Variant::BOOL, "one_way_collision"), "set_one_way_collision", "is_one_way_collision_enabled");
 	ADD_PROPERTY(PropertyInfo(Variant::BOOL, "one_way_collision"), "set_one_way_collision", "is_one_way_collision_enabled");
 	ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "one_way_collision_margin", PROPERTY_HINT_RANGE, "0,128,0.1,suffix:px"), "set_one_way_collision_margin", "get_one_way_collision_margin");
 	ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "one_way_collision_margin", PROPERTY_HINT_RANGE, "0,128,0.1,suffix:px"), "set_one_way_collision_margin", "get_one_way_collision_margin");
+
+#ifdef DEBUG_ENABLED
+	ClassDB::bind_method(D_METHOD("set_debug_color", "color"), &CollisionShape2D::set_debug_color);
+	ClassDB::bind_method(D_METHOD("get_debug_color"), &CollisionShape2D::get_debug_color);
+
 	ADD_PROPERTY(PropertyInfo(Variant::COLOR, "debug_color"), "set_debug_color", "get_debug_color");
 	ADD_PROPERTY(PropertyInfo(Variant::COLOR, "debug_color"), "set_debug_color", "get_debug_color");
 	// Default value depends on a project setting, override for doc generation purposes.
 	// Default value depends on a project setting, override for doc generation purposes.
-	ADD_PROPERTY_DEFAULT("debug_color", Color());
+	ADD_PROPERTY_DEFAULT("debug_color", Color(0.0, 0.0, 0.0, 0.0));
+#endif // DEBUG_ENABLED
 }
 }
 
 
 CollisionShape2D::CollisionShape2D() {
 CollisionShape2D::CollisionShape2D() {
 	set_notify_local_transform(true);
 	set_notify_local_transform(true);
 	set_hide_clip_children(true);
 	set_hide_clip_children(true);
+#ifdef DEBUG_ENABLED
 	debug_color = _get_default_debug_color();
 	debug_color = _get_default_debug_color();
+#endif // DEBUG_ENABLED
 }
 }

+ 12 - 1
scene/2d/physics/collision_shape_2d.h

@@ -45,17 +45,26 @@ class CollisionShape2D : public Node2D {
 	bool disabled = false;
 	bool disabled = false;
 	bool one_way_collision = false;
 	bool one_way_collision = false;
 	real_t one_way_collision_margin = 1.0;
 	real_t one_way_collision_margin = 1.0;
-	Color debug_color;
 
 
 	void _shape_changed();
 	void _shape_changed();
 	void _update_in_shape_owner(bool p_xform_only = false);
 	void _update_in_shape_owner(bool p_xform_only = false);
+
+	// Not wrapped in `#ifdef DEBUG_ENABLED` as it is used for rendering.
+	Color debug_color = Color(0.0, 0.0, 0.0, 0.0);
+
+#ifdef DEBUG_ENABLED
 	Color _get_default_debug_color() const;
 	Color _get_default_debug_color() const;
+#endif // DEBUG_ENABLED
 
 
 protected:
 protected:
 	void _notification(int p_what);
 	void _notification(int p_what);
+
+#ifdef DEBUG_ENABLED
 	bool _property_can_revert(const StringName &p_name) const;
 	bool _property_can_revert(const StringName &p_name) const;
 	bool _property_get_revert(const StringName &p_name, Variant &r_property) const;
 	bool _property_get_revert(const StringName &p_name, Variant &r_property) const;
 	void _validate_property(PropertyInfo &p_property) const;
 	void _validate_property(PropertyInfo &p_property) const;
+#endif // DEBUG_ENABLED
+
 	static void _bind_methods();
 	static void _bind_methods();
 
 
 public:
 public:
@@ -77,8 +86,10 @@ public:
 	void set_one_way_collision_margin(real_t p_margin);
 	void set_one_way_collision_margin(real_t p_margin);
 	real_t get_one_way_collision_margin() const;
 	real_t get_one_way_collision_margin() const;
 
 
+#ifdef DEBUG_ENABLED
 	void set_debug_color(const Color &p_color);
 	void set_debug_color(const Color &p_color);
 	Color get_debug_color() const;
 	Color get_debug_color() const;
+#endif // DEBUG_ENABLED
 
 
 	PackedStringArray get_configuration_warnings() const override;
 	PackedStringArray get_configuration_warnings() const override;
 
 

+ 39 - 27
scene/3d/physics/collision_shape_3d.cpp

@@ -81,12 +81,6 @@ void CollisionShape3D::_update_in_shape_owner(bool p_xform_only) {
 void CollisionShape3D::_notification(int p_what) {
 void CollisionShape3D::_notification(int p_what) {
 	switch (p_what) {
 	switch (p_what) {
 		case NOTIFICATION_PARENTED: {
 		case NOTIFICATION_PARENTED: {
-#ifdef DEBUG_ENABLED
-			if (debug_color == get_placeholder_default_color()) {
-				debug_color = SceneTree::get_singleton()->get_debug_collisions_color();
-			}
-#endif // DEBUG_ENABLED
-
 			collision_object = Object::cast_to<CollisionObject3D>(get_parent());
 			collision_object = Object::cast_to<CollisionObject3D>(get_parent());
 			if (collision_object) {
 			if (collision_object) {
 				owner_id = collision_object->create_shape_owner(this);
 				owner_id = collision_object->create_shape_owner(this);
@@ -189,7 +183,7 @@ void CollisionShape3D::_bind_methods() {
 
 
 	ADD_PROPERTY(PropertyInfo(Variant::COLOR, "debug_color"), "set_debug_color", "get_debug_color");
 	ADD_PROPERTY(PropertyInfo(Variant::COLOR, "debug_color"), "set_debug_color", "get_debug_color");
 	// Default value depends on a project setting, override for doc generation purposes.
 	// Default value depends on a project setting, override for doc generation purposes.
-	ADD_PROPERTY_DEFAULT("debug_color", get_placeholder_default_color());
+	ADD_PROPERTY_DEFAULT("debug_color", Color(0.0, 0.0, 0.0, 0.0));
 
 
 	ADD_PROPERTY(PropertyInfo(Variant::BOOL, "debug_fill"), "set_enable_debug_fill", "get_enable_debug_fill");
 	ADD_PROPERTY(PropertyInfo(Variant::BOOL, "debug_fill"), "set_enable_debug_fill", "get_enable_debug_fill");
 #endif // DEBUG_ENABLED
 #endif // DEBUG_ENABLED
@@ -200,27 +194,27 @@ void CollisionShape3D::set_shape(const Ref<Shape3D> &p_shape) {
 		return;
 		return;
 	}
 	}
 	if (shape.is_valid()) {
 	if (shape.is_valid()) {
-		shape->disconnect_changed(callable_mp(this, &CollisionShape3D::shape_changed));
+#ifdef DEBUG_ENABLED
+		shape->disconnect_changed(callable_mp(this, &CollisionShape3D::_shape_changed));
+#endif // DEBUG_ENABLED
 		shape->disconnect_changed(callable_mp((Node3D *)this, &Node3D::update_gizmos));
 		shape->disconnect_changed(callable_mp((Node3D *)this, &Node3D::update_gizmos));
 	}
 	}
 	shape = p_shape;
 	shape = p_shape;
 	if (shape.is_valid()) {
 	if (shape.is_valid()) {
 #ifdef DEBUG_ENABLED
 #ifdef DEBUG_ENABLED
-		if (shape->get_debug_color() != get_placeholder_default_color()) {
+		if (shape->are_debug_properties_edited()) {
 			set_debug_color(shape->get_debug_color());
 			set_debug_color(shape->get_debug_color());
 			set_debug_fill_enabled(shape->get_debug_fill());
 			set_debug_fill_enabled(shape->get_debug_fill());
-		} else if (get_debug_color() != get_placeholder_default_color()) {
-			shape->set_debug_color(debug_color);
-			shape->set_debug_fill(debug_fill);
 		} else {
 		} else {
-			set_debug_color(SceneTree::get_singleton()->get_debug_collisions_color());
-			shape->set_debug_color(SceneTree::get_singleton()->get_debug_collisions_color());
+			shape->set_debug_color(debug_color);
 			shape->set_debug_fill(debug_fill);
 			shape->set_debug_fill(debug_fill);
 		}
 		}
 #endif // DEBUG_ENABLED
 #endif // DEBUG_ENABLED
 
 
 		shape->connect_changed(callable_mp((Node3D *)this, &Node3D::update_gizmos));
 		shape->connect_changed(callable_mp((Node3D *)this, &Node3D::update_gizmos));
-		shape->connect_changed(callable_mp(this, &CollisionShape3D::shape_changed));
+#ifdef DEBUG_ENABLED
+		shape->connect_changed(callable_mp(this, &CollisionShape3D::_shape_changed));
+#endif // DEBUG_ENABLED
 	}
 	}
 	update_gizmos();
 	update_gizmos();
 	if (collision_object) {
 	if (collision_object) {
@@ -254,15 +248,21 @@ bool CollisionShape3D::is_disabled() const {
 }
 }
 
 
 #ifdef DEBUG_ENABLED
 #ifdef DEBUG_ENABLED
+
+Color CollisionShape3D::_get_default_debug_color() const {
+	const SceneTree *st = SceneTree::get_singleton();
+	return st ? st->get_debug_collisions_color() : Color(0.0, 0.0, 0.0, 0.0);
+}
+
 void CollisionShape3D::set_debug_color(const Color &p_color) {
 void CollisionShape3D::set_debug_color(const Color &p_color) {
-	if (p_color == get_placeholder_default_color()) {
-		debug_color = SceneTree::get_singleton()->get_debug_collisions_color();
-	} else if (debug_color != p_color) {
-		debug_color = p_color;
+	if (debug_color == p_color) {
+		return;
+	}
 
 
-		if (shape.is_valid()) {
-			shape->set_debug_color(p_color);
-		}
+	debug_color = p_color;
+
+	if (shape.is_valid()) {
+		shape->set_debug_color(p_color);
 	}
 	}
 }
 }
 
 
@@ -295,27 +295,39 @@ bool CollisionShape3D::_property_can_revert(const StringName &p_name) const {
 
 
 bool CollisionShape3D::_property_get_revert(const StringName &p_name, Variant &r_property) const {
 bool CollisionShape3D::_property_get_revert(const StringName &p_name, Variant &r_property) const {
 	if (p_name == "debug_color") {
 	if (p_name == "debug_color") {
-		r_property = SceneTree::get_singleton()->get_debug_collisions_color();
+		r_property = _get_default_debug_color();
 		return true;
 		return true;
 	}
 	}
 	return false;
 	return false;
 }
 }
-#endif // DEBUG_ENABLED
 
 
-void CollisionShape3D::shape_changed() {
-#ifdef DEBUG_ENABLED
+void CollisionShape3D::_validate_property(PropertyInfo &p_property) const {
+	if (p_property.name == "debug_color") {
+		if (debug_color == _get_default_debug_color()) {
+			p_property.usage = PROPERTY_USAGE_DEFAULT & ~PROPERTY_USAGE_STORAGE;
+		} else {
+			p_property.usage = PROPERTY_USAGE_DEFAULT;
+		}
+	}
+}
+
+void CollisionShape3D::_shape_changed() {
 	if (shape->get_debug_color() != debug_color) {
 	if (shape->get_debug_color() != debug_color) {
 		set_debug_color(shape->get_debug_color());
 		set_debug_color(shape->get_debug_color());
 	}
 	}
 	if (shape->get_debug_fill() != debug_fill) {
 	if (shape->get_debug_fill() != debug_fill) {
 		set_debug_fill_enabled(shape->get_debug_fill());
 		set_debug_fill_enabled(shape->get_debug_fill());
 	}
 	}
-#endif // DEBUG_ENABLED
 }
 }
 
 
+#endif // DEBUG_ENABLED
+
 CollisionShape3D::CollisionShape3D() {
 CollisionShape3D::CollisionShape3D() {
 	//indicator = RenderingServer::get_singleton()->mesh_create();
 	//indicator = RenderingServer::get_singleton()->mesh_create();
 	set_notify_local_transform(true);
 	set_notify_local_transform(true);
+#ifdef DEBUG_ENABLED
+	debug_color = _get_default_debug_color();
+#endif // DEBUG_ENABLED
 }
 }
 
 
 CollisionShape3D::~CollisionShape3D() {
 CollisionShape3D::~CollisionShape3D() {

+ 4 - 4
scene/3d/physics/collision_shape_3d.h

@@ -44,10 +44,11 @@ class CollisionShape3D : public Node3D {
 	CollisionObject3D *collision_object = nullptr;
 	CollisionObject3D *collision_object = nullptr;
 
 
 #ifdef DEBUG_ENABLED
 #ifdef DEBUG_ENABLED
-	Color debug_color = get_placeholder_default_color();
+	Color debug_color;
 	bool debug_fill = true;
 	bool debug_fill = true;
 
 
-	static const Color get_placeholder_default_color() { return Color(0.0, 0.0, 0.0, 0.0); }
+	Color _get_default_debug_color() const;
+	void _shape_changed();
 #endif // DEBUG_ENABLED
 #endif // DEBUG_ENABLED
 
 
 #ifndef DISABLE_DEPRECATED
 #ifndef DISABLE_DEPRECATED
@@ -65,10 +66,9 @@ protected:
 #ifdef DEBUG_ENABLED
 #ifdef DEBUG_ENABLED
 	bool _property_can_revert(const StringName &p_name) const;
 	bool _property_can_revert(const StringName &p_name) const;
 	bool _property_get_revert(const StringName &p_name, Variant &r_property) const;
 	bool _property_get_revert(const StringName &p_name, Variant &r_property) const;
+	void _validate_property(PropertyInfo &p_property) const;
 #endif // DEBUG_ENABLED
 #endif // DEBUG_ENABLED
 
 
-	void shape_changed();
-
 public:
 public:
 	void make_convex_from_siblings();
 	void make_convex_from_siblings();
 
 

+ 4 - 0
scene/resources/3d/shape_3d.cpp

@@ -67,12 +67,14 @@ void Shape3D::set_margin(real_t p_margin) {
 }
 }
 
 
 #ifdef DEBUG_ENABLED
 #ifdef DEBUG_ENABLED
+
 void Shape3D::set_debug_color(const Color &p_color) {
 void Shape3D::set_debug_color(const Color &p_color) {
 	if (p_color == debug_color) {
 	if (p_color == debug_color) {
 		return;
 		return;
 	}
 	}
 
 
 	debug_color = p_color;
 	debug_color = p_color;
+	debug_properties_edited = true;
 	_update_shape();
 	_update_shape();
 }
 }
 
 
@@ -86,12 +88,14 @@ void Shape3D::set_debug_fill(bool p_fill) {
 	}
 	}
 
 
 	debug_fill = p_fill;
 	debug_fill = p_fill;
+	debug_properties_edited = true;
 	_update_shape();
 	_update_shape();
 }
 }
 
 
 bool Shape3D::get_debug_fill() const {
 bool Shape3D::get_debug_fill() const {
 	return debug_fill;
 	return debug_fill;
 }
 }
+
 #endif // DEBUG_ENABLED
 #endif // DEBUG_ENABLED
 
 
 Ref<ArrayMesh> Shape3D::get_debug_mesh() {
 Ref<ArrayMesh> Shape3D::get_debug_mesh() {

+ 6 - 0
scene/resources/3d/shape_3d.h

@@ -47,8 +47,12 @@ class Shape3D : public Resource {
 	Ref<ArrayMesh> debug_mesh_cache;
 	Ref<ArrayMesh> debug_mesh_cache;
 	Ref<Material> collision_material;
 	Ref<Material> collision_material;
 
 
+	// Not wrapped in `#ifdef DEBUG_ENABLED` as it is used for rendering.
 	Color debug_color = Color(0.0, 0.0, 0.0, 0.0);
 	Color debug_color = Color(0.0, 0.0, 0.0, 0.0);
 	bool debug_fill = true;
 	bool debug_fill = true;
+#ifdef DEBUG_ENABLED
+	bool debug_properties_edited = false;
+#endif // DEBUG_ENABLED
 
 
 protected:
 protected:
 	static void _bind_methods();
 	static void _bind_methods();
@@ -83,6 +87,8 @@ public:
 
 
 	void set_debug_fill(bool p_fill);
 	void set_debug_fill(bool p_fill);
 	bool get_debug_fill() const;
 	bool get_debug_fill() const;
+
+	_FORCE_INLINE_ bool are_debug_properties_edited() const { return debug_properties_edited; }
 #endif // DEBUG_ENABLED
 #endif // DEBUG_ENABLED
 
 
 	Shape3D();
 	Shape3D();