浏览代码

Merge pull request #27219 from raphael10241024/debug_fix

Fix collider debug shape didn't changes with collider size changes
Rémi Verschelde 6 年之前
父节点
当前提交
33b69340ad

+ 21 - 7
scene/3d/collision_shape.cpp

@@ -91,7 +91,7 @@ void CollisionShape::_notification(int p_what) {
 				_update_in_shape_owner();
 			}
 			if (get_tree()->is_debugging_collisions_hint()) {
-				_create_debug_shape();
+				_update_debug_shape();
 			}
 		} break;
 		case NOTIFICATION_LOCAL_TRANSFORM_CHANGED: {
@@ -142,17 +142,24 @@ void CollisionShape::_bind_methods() {
 	ClassDB::bind_method(D_METHOD("make_convex_from_brothers"), &CollisionShape::make_convex_from_brothers);
 	ClassDB::set_method_flags("CollisionShape", "make_convex_from_brothers", METHOD_FLAGS_DEFAULT | METHOD_FLAG_EDITOR);
 
+	ClassDB::bind_method(D_METHOD("_shape_changed"), &CollisionShape::_shape_changed);
+	ClassDB::bind_method(D_METHOD("_update_debug_shape"), &CollisionShape::_update_debug_shape);
+
 	ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "shape", PROPERTY_HINT_RESOURCE_TYPE, "Shape"), "set_shape", "get_shape");
 	ADD_PROPERTY(PropertyInfo(Variant::BOOL, "disabled"), "set_disabled", "is_disabled");
 }
 
 void CollisionShape::set_shape(const Ref<Shape> &p_shape) {
 
-	if (!shape.is_null())
+	if (!shape.is_null()) {
 		shape->unregister_owner(this);
+		shape->disconnect("changed", this, "_shape_changed");
+	}
 	shape = p_shape;
-	if (!shape.is_null())
+	if (!shape.is_null()) {
 		shape->register_owner(this);
+		shape->connect("changed", this, "_shape_changed");
+	}
 	update_gizmo();
 	if (parent) {
 		parent->shape_owner_clear_shapes(owner_id);
@@ -161,6 +168,8 @@ void CollisionShape::set_shape(const Ref<Shape> &p_shape) {
 		}
 	}
 
+	if (is_inside_tree())
+		_shape_changed();
 	update_configuration_warning();
 }
 
@@ -199,7 +208,8 @@ CollisionShape::~CollisionShape() {
 	//VisualServer::get_singleton()->free(indicator);
 }
 
-void CollisionShape::_create_debug_shape() {
+void CollisionShape::_update_debug_shape() {
+	debug_shape_dirty = false;
 
 	if (debug_shape) {
 		debug_shape->queue_delete();
@@ -207,15 +217,19 @@ void CollisionShape::_create_debug_shape() {
 	}
 
 	Ref<Shape> s = get_shape();
-
 	if (s.is_null())
 		return;
 
 	Ref<Mesh> mesh = s->get_debug_mesh();
-
 	MeshInstance *mi = memnew(MeshInstance);
 	mi->set_mesh(mesh);
-
 	add_child(mi);
 	debug_shape = mi;
 }
+
+void CollisionShape::_shape_changed() {
+	if (get_tree()->is_debugging_collisions_hint() && !debug_shape_dirty) {
+		debug_shape_dirty = true;
+		call_deferred("_update_debug_shape");
+	}
+}

+ 3 - 1
scene/3d/collision_shape.h

@@ -45,12 +45,14 @@ class CollisionShape : public Spatial {
 	CollisionObject *parent;
 
 	Node *debug_shape;
+	bool debug_shape_dirty;
 
 	void resource_changed(RES res);
 	bool disabled;
 
 protected:
-	void _create_debug_shape();
+	void _update_debug_shape();
+	void _shape_changed();
 
 	void _update_in_shape_owner(bool p_xform_only = false);
 

+ 1 - 0
scene/resources/box_shape.cpp

@@ -51,6 +51,7 @@ Vector<Vector3> BoxShape::_gen_debug_mesh_lines() {
 void BoxShape::_update_shape() {
 
 	PhysicsServer::get_singleton()->shape_set_data(get_shape(), extents);
+	Shape::_update_shape();
 }
 
 void BoxShape::set_extents(const Vector3 &p_extents) {

+ 1 - 0
scene/resources/capsule_shape.cpp

@@ -75,6 +75,7 @@ void CapsuleShape::_update_shape() {
 	d["radius"] = radius;
 	d["height"] = height;
 	PhysicsServer::get_singleton()->shape_set_data(get_shape(), d);
+	Shape::_update_shape();
 }
 
 void CapsuleShape::set_radius(float p_radius) {

+ 1 - 0
scene/resources/concave_polygon_shape.cpp

@@ -65,6 +65,7 @@ Vector<Vector3> ConcavePolygonShape::_gen_debug_mesh_lines() {
 }
 
 void ConcavePolygonShape::_update_shape() {
+	Shape::_update_shape();
 }
 
 void ConcavePolygonShape::set_faces(const PoolVector<Vector3> &p_faces) {

+ 1 - 1
scene/resources/convex_polygon_shape.cpp

@@ -58,7 +58,7 @@ Vector<Vector3> ConvexPolygonShape::_gen_debug_mesh_lines() {
 void ConvexPolygonShape::_update_shape() {
 
 	PhysicsServer::get_singleton()->shape_set_data(get_shape(), points);
-	emit_changed();
+	Shape::_update_shape();
 }
 
 void ConvexPolygonShape::set_points(const PoolVector<Vector3> &p_points) {

+ 1 - 0
scene/resources/cylinder_shape.cpp

@@ -68,6 +68,7 @@ void CylinderShape::_update_shape() {
 	d["radius"] = radius;
 	d["height"] = height;
 	PhysicsServer::get_singleton()->shape_set_data(get_shape(), d);
+	Shape::_update_shape();
 }
 
 void CylinderShape::set_radius(float p_radius) {

+ 1 - 0
scene/resources/plane_shape.cpp

@@ -64,6 +64,7 @@ Vector<Vector3> PlaneShape::_gen_debug_mesh_lines() {
 void PlaneShape::_update_shape() {
 
 	PhysicsServer::get_singleton()->shape_set_data(get_shape(), plane);
+	Shape::_update_shape();
 }
 
 void PlaneShape::set_plane(Plane p_plane) {

+ 1 - 1
scene/resources/ray_shape.cpp

@@ -47,7 +47,7 @@ void RayShape::_update_shape() {
 	d["length"] = length;
 	d["slips_on_slope"] = slips_on_slope;
 	PhysicsServer::get_singleton()->shape_set_data(get_shape(), d);
-	emit_changed();
+	Shape::_update_shape();
 }
 
 void RayShape::set_length(float p_length) {

+ 5 - 0
scene/resources/shape.cpp

@@ -96,6 +96,11 @@ Ref<ArrayMesh> Shape::get_debug_mesh() {
 	return debug_mesh_cache;
 }
 
+void Shape::_update_shape() {
+	emit_changed();
+	debug_mesh_cache.unref();
+}
+
 void Shape::_bind_methods() {
 
 	ClassDB::bind_method(D_METHOD("set_margin", "margin"), &Shape::set_margin);

+ 2 - 0
scene/resources/shape.h

@@ -51,6 +51,8 @@ protected:
 	Shape(RID p_shape);
 
 	virtual Vector<Vector3> _gen_debug_mesh_lines() = 0; // { return Vector<Vector3>(); }
+	virtual void _update_shape();
+
 public:
 	virtual RID get_rid() const { return shape; }
 

+ 1 - 0
scene/resources/sphere_shape.cpp

@@ -58,6 +58,7 @@ Vector<Vector3> SphereShape::_gen_debug_mesh_lines() {
 void SphereShape::_update_shape() {
 
 	PhysicsServer::get_singleton()->shape_set_data(get_shape(), radius);
+	Shape::_update_shape();
 }
 
 void SphereShape::set_radius(float p_radius) {