EsotericSoftware/spine-runtimes

[godot] getting bounding box of slot

proggen-com opened this issue · 0 comments

In spine-godot it's not possible to get the bounding box of a slot. The SpineMesh2D doesn't expose anything that can be used to calculate the bounds. Internally it does calculate the bounds, so with this little patch it's possible to get them in the GDScript:

diff --git a/spine-godot/spine_godot/SpineSprite.cpp b/spine-godot/spine_godot/SpineSprite.cpp
index 7be358db7..45ad2a16f 100644
--- a/spine-godot/spine_godot/SpineSprite.cpp
+++ b/spine-godot/spine_godot/SpineSprite.cpp
@@ -123,6 +123,7 @@ void SpineMesh2D::_notification(int what) {
 }

 void SpineMesh2D::_bind_methods() {
+       ClassDB::bind_method(D_METHOD("get_mesh_rid"), &SpineMesh2D::get_mesh_rid);
 }

 void SpineMesh2D::update_mesh(const Vector<Point2> &vertices,
diff --git a/spine-godot/spine_godot/SpineSprite.h b/spine-godot/spine_godot/SpineSprite.h
index 06ca2f7b0..33eac6e76 100644
--- a/spine-godot/spine_godot/SpineSprite.h
+++ b/spine-godot/spine_godot/SpineSprite.h
@@ -95,6 +95,9 @@ public:
                }
        }
 #endif
+       RID get_mesh_rid() {
+               return mesh;
+       }

        void update_mesh(const Vector<Point2> &vertices,
                                         const Vector<Point2> &uvs,

It exposes the internal mesh RID to GDScript, then with the RenderingServer you can fetch the bounds:

var mesh = $SpineSprite.get_child(index)
var internal_mesh_rid = mesh.get_mesh_rid()
var mesh_aabb = RenderingServer.mesh_get_custom_aabb(internal_mesh_rid)

Drawback is that this bounding box is calculated after a render, so to use it you probably need to wait for something like:

await get_tree().process_frame

Perhaps there is a cleaner way to implement this, or expose a get_bounds() to the SpineSlot ?