diff --git a/scene/3d/collision_shape.cpp b/scene/3d/collision_shape.cpp index ac33e2b7148a..6bb2b547c76a 100644 --- a/scene/3d/collision_shape.cpp +++ b/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 &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 &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 s = get_shape(); - if (s.is_null()) return; Ref 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"); + } +} diff --git a/scene/3d/collision_shape.h b/scene/3d/collision_shape.h index 0c8e383a7fad..98427b85904a 100644 --- a/scene/3d/collision_shape.h +++ b/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); diff --git a/scene/resources/box_shape.cpp b/scene/resources/box_shape.cpp index d93754076cf9..d819e9f776b7 100644 --- a/scene/resources/box_shape.cpp +++ b/scene/resources/box_shape.cpp @@ -51,6 +51,7 @@ Vector 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) { diff --git a/scene/resources/capsule_shape.cpp b/scene/resources/capsule_shape.cpp index 3f7bf1e0ecd1..669b261bfec9 100644 --- a/scene/resources/capsule_shape.cpp +++ b/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) { diff --git a/scene/resources/concave_polygon_shape.cpp b/scene/resources/concave_polygon_shape.cpp index b192d088d847..b4cc38c8c00b 100644 --- a/scene/resources/concave_polygon_shape.cpp +++ b/scene/resources/concave_polygon_shape.cpp @@ -65,6 +65,7 @@ Vector ConcavePolygonShape::_gen_debug_mesh_lines() { } void ConcavePolygonShape::_update_shape() { + Shape::_update_shape(); } void ConcavePolygonShape::set_faces(const PoolVector &p_faces) { diff --git a/scene/resources/convex_polygon_shape.cpp b/scene/resources/convex_polygon_shape.cpp index 5845e4be50e5..499688a18583 100644 --- a/scene/resources/convex_polygon_shape.cpp +++ b/scene/resources/convex_polygon_shape.cpp @@ -58,7 +58,7 @@ Vector 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 &p_points) { diff --git a/scene/resources/cylinder_shape.cpp b/scene/resources/cylinder_shape.cpp index 4fd829b34966..f60f7ab3766d 100644 --- a/scene/resources/cylinder_shape.cpp +++ b/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) { diff --git a/scene/resources/plane_shape.cpp b/scene/resources/plane_shape.cpp index 419ff8f97255..08f6ccd76430 100644 --- a/scene/resources/plane_shape.cpp +++ b/scene/resources/plane_shape.cpp @@ -64,6 +64,7 @@ Vector 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) { diff --git a/scene/resources/ray_shape.cpp b/scene/resources/ray_shape.cpp index b7925d8a5877..0acfffdc0678 100644 --- a/scene/resources/ray_shape.cpp +++ b/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) { diff --git a/scene/resources/shape.cpp b/scene/resources/shape.cpp index 825b7f4c8be9..6ba46f066cdc 100644 --- a/scene/resources/shape.cpp +++ b/scene/resources/shape.cpp @@ -96,6 +96,11 @@ Ref 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); diff --git a/scene/resources/shape.h b/scene/resources/shape.h index de99a967a847..ba763eaab143 100644 --- a/scene/resources/shape.h +++ b/scene/resources/shape.h @@ -51,6 +51,8 @@ protected: Shape(RID p_shape); virtual Vector _gen_debug_mesh_lines() = 0; // { return Vector(); } + virtual void _update_shape(); + public: virtual RID get_rid() const { return shape; } diff --git a/scene/resources/sphere_shape.cpp b/scene/resources/sphere_shape.cpp index 492cf3959d52..af89413cede0 100644 --- a/scene/resources/sphere_shape.cpp +++ b/scene/resources/sphere_shape.cpp @@ -58,6 +58,7 @@ Vector 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) {