Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Take item and skeleton transforms into account when calculating mesh AABB in 2D #76184

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions doc/classes/RenderingServer.xml
Original file line number Diff line number Diff line change
Expand Up @@ -891,6 +891,14 @@
[b]Note:[/b] When using the OpenGL backend or when running in headless mode, this function always returns [code]null[/code].
</description>
</method>
<method name="debug_canvas_item_get_rect">
<return type="Rect2" />
<param index="0" name="item" type="RID" />
<description>
Returns the bounding rectangle for a canvas item in local space, as calculated by the renderer. This bound is used internally for culling.
[b]Warning:[/b] This function is intended for debugging in the editor, and will pass through and return a zero [Rect2] in exported projects.
</description>
</method>
<method name="decal_create">
<return type="RID" />
<description>
Expand Down
15 changes: 11 additions & 4 deletions drivers/gles3/storage/mesh_storage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -502,7 +502,7 @@ AABB MeshStorage::mesh_get_custom_aabb(RID p_mesh) const {
return mesh->custom_aabb;
}

AABB MeshStorage::mesh_get_aabb(RID p_mesh, RID p_skeleton) {
AABB MeshStorage::mesh_get_aabb(RID p_mesh, RID p_skeleton, Transform3D p_transform) {
Mesh *mesh = mesh_owner.get_or_null(p_mesh);
ERR_FAIL_COND_V(!mesh, AABB());

Expand Down Expand Up @@ -534,7 +534,7 @@ AABB MeshStorage::mesh_get_aabb(RID p_mesh, RID p_skeleton) {

if (skeleton->use_2d) {
for (int j = 0; j < bs; j++) {
if (skbones[0].size == Vector3()) {
if (skbones[j].size == Vector3(-1, -1, -1)) {
continue; //bone is unused
}

Expand All @@ -550,7 +550,14 @@ AABB MeshStorage::mesh_get_aabb(RID p_mesh, RID p_skeleton) {
mtx.basis.rows[1][1] = dataptr[5];
mtx.origin.y = dataptr[7];

AABB baabb = mtx.xform(skbones[j]);
Vector2 t_x = skeleton->base_transform_2d.columns[0];
Vector2 t_y = skeleton->base_transform_2d.columns[1];
Vector2 origin = skeleton->base_transform_2d.columns[2];
Transform3D skeleton_transform = Transform3D(Vector3(t_x.x, t_x.y, 0.0), Vector3(t_y.x, t_y.y, 0.0), Vector3(0.0, 0.0, 1.0), Vector3(origin.x, origin.y, 0.0));

Transform3D transform = skeleton_transform.affine_inverse() * p_transform;

AABB baabb = transform.affine_inverse().xform(mtx.xform(transform.xform(skbones[j])));

if (first) {
laabb = baabb;
Expand All @@ -561,7 +568,7 @@ AABB MeshStorage::mesh_get_aabb(RID p_mesh, RID p_skeleton) {
}
} else {
for (int j = 0; j < bs; j++) {
if (skbones[0].size == Vector3()) {
if (skbones[j].size == Vector3(-1, -1, -1)) {
continue; //bone is unused
}

Expand Down
2 changes: 1 addition & 1 deletion drivers/gles3/storage/mesh_storage.h
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ class MeshStorage : public RendererMeshStorage {
virtual void mesh_set_custom_aabb(RID p_mesh, const AABB &p_aabb) override;
virtual AABB mesh_get_custom_aabb(RID p_mesh) const override;

virtual AABB mesh_get_aabb(RID p_mesh, RID p_skeleton = RID()) override;
virtual AABB mesh_get_aabb(RID p_mesh, RID p_skeleton = RID(), Transform3D p_transform = Transform3D()) override;
virtual void mesh_set_shadow_mesh(RID p_mesh, RID p_shadow_mesh) override;
virtual void mesh_clear(RID p_mesh) override;

Expand Down
2 changes: 1 addition & 1 deletion servers/rendering/dummy/storage/mesh_storage.h
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ class MeshStorage : public RendererMeshStorage {
virtual void mesh_set_custom_aabb(RID p_mesh, const AABB &p_aabb) override {}
virtual AABB mesh_get_custom_aabb(RID p_mesh) const override { return AABB(); }

virtual AABB mesh_get_aabb(RID p_mesh, RID p_skeleton = RID()) override { return AABB(); }
virtual AABB mesh_get_aabb(RID p_mesh, RID p_skeleton = RID(), Transform3D p_transform = Transform3D()) override { return AABB(); }
virtual void mesh_set_shadow_mesh(RID p_mesh, RID p_shadow_mesh) override {}
virtual void mesh_clear(RID p_mesh) override;

Expand Down
6 changes: 6 additions & 0 deletions servers/rendering/renderer_canvas_cull.h
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,12 @@ class RendererCanvasCull {

void update_visibility_notifiers();

Rect2 _debug_canvas_item_get_rect(RID p_item) {
Item *canvas_item = canvas_item_owner.get_or_null(p_item);
ERR_FAIL_COND_V(!canvas_item, Rect2());
return canvas_item->get_rect();
}

bool free(RID p_rid);
RendererCanvasCull();
~RendererCanvasCull();
Expand Down
7 changes: 6 additions & 1 deletion servers/rendering/renderer_canvas_render.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,12 @@ const Rect2 &RendererCanvasRender::Item::get_rect() const {
} break;
case Item::Command::TYPE_MESH: {
const Item::CommandMesh *mesh = static_cast<const Item::CommandMesh *>(c);
AABB aabb = RSG::mesh_storage->mesh_get_aabb(mesh->mesh, skeleton);

Vector2 t_x = xform.columns[0];
Vector2 t_y = xform.columns[1];
Vector2 origin = xform.columns[2];
Transform3D transform = Transform3D(Vector3(t_x.x, t_x.y, 0.0), Vector3(t_y.x, t_y.y, 0.0), Vector3(0.0, 0.0, 1.0), Vector3(origin.x, origin.y, 0.0));
AABB aabb = RSG::mesh_storage->mesh_get_aabb(mesh->mesh, skeleton, transform);

r = Rect2(aabb.position.x, aabb.position.y, aabb.size.x, aabb.size.y);

Expand Down
15 changes: 11 additions & 4 deletions servers/rendering/renderer_rd/storage_rd/mesh_storage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -597,7 +597,7 @@ AABB MeshStorage::mesh_get_custom_aabb(RID p_mesh) const {
return mesh->custom_aabb;
}

AABB MeshStorage::mesh_get_aabb(RID p_mesh, RID p_skeleton) {
AABB MeshStorage::mesh_get_aabb(RID p_mesh, RID p_skeleton, Transform3D p_transform) {
Mesh *mesh = mesh_owner.get_or_null(p_mesh);
ERR_FAIL_COND_V(!mesh, AABB());

Expand Down Expand Up @@ -627,7 +627,7 @@ AABB MeshStorage::mesh_get_aabb(RID p_mesh, RID p_skeleton) {

if (skeleton->use_2d) {
for (int j = 0; j < bs; j++) {
if (skbones[0].size == Vector3()) {
if (skbones[j].size == Vector3(-1, -1, -1)) {
continue; //bone is unused
}

Expand All @@ -643,7 +643,14 @@ AABB MeshStorage::mesh_get_aabb(RID p_mesh, RID p_skeleton) {
mtx.basis.rows[1][1] = dataptr[5];
mtx.origin.y = dataptr[7];

AABB baabb = mtx.xform(skbones[j]);
Vector2 t_x = skeleton->base_transform_2d.columns[0];
Vector2 t_y = skeleton->base_transform_2d.columns[1];
Vector2 origin = skeleton->base_transform_2d.columns[2];
Transform3D skeleton_transform = Transform3D(Vector3(t_x.x, t_x.y, 0.0), Vector3(t_y.x, t_y.y, 0.0), Vector3(0.0, 0.0, 1.0), Vector3(origin.x, origin.y, 0.0));

Transform3D transform = skeleton_transform.affine_inverse() * p_transform;

AABB baabb = transform.affine_inverse().xform(mtx.xform(transform.xform(skbones[j])));

if (first) {
laabb = baabb;
Expand All @@ -654,7 +661,7 @@ AABB MeshStorage::mesh_get_aabb(RID p_mesh, RID p_skeleton) {
}
} else {
for (int j = 0; j < bs; j++) {
if (skbones[0].size == Vector3()) {
if (skbones[j].size == Vector3(-1, -1, -1)) {
continue; //bone is unused
}

Expand Down
2 changes: 1 addition & 1 deletion servers/rendering/renderer_rd/storage_rd/mesh_storage.h
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ class MeshStorage : public RendererMeshStorage {
virtual void mesh_set_custom_aabb(RID p_mesh, const AABB &p_aabb) override;
virtual AABB mesh_get_custom_aabb(RID p_mesh) const override;

virtual AABB mesh_get_aabb(RID p_mesh, RID p_skeleton = RID()) override;
virtual AABB mesh_get_aabb(RID p_mesh, RID p_skeleton = RID(), Transform3D p_transform = Transform3D()) override;
virtual void mesh_set_shadow_mesh(RID p_mesh, RID p_shadow_mesh) override;

virtual void mesh_clear(RID p_mesh) override;
Expand Down
2 changes: 2 additions & 0 deletions servers/rendering/rendering_server_default.h
Original file line number Diff line number Diff line change
Expand Up @@ -923,6 +923,8 @@ class RenderingServerDefault : public RenderingServer {

FUNC1(canvas_set_shadow_texture_size, int)

FUNC1R(Rect2, _debug_canvas_item_get_rect, RID)

/* GLOBAL SHADER UNIFORMS */

#undef server_name
Expand Down
2 changes: 1 addition & 1 deletion servers/rendering/storage/mesh_storage.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class RendererMeshStorage {
virtual void mesh_set_custom_aabb(RID p_mesh, const AABB &p_aabb) = 0;
virtual AABB mesh_get_custom_aabb(RID p_mesh) const = 0;

virtual AABB mesh_get_aabb(RID p_mesh, RID p_skeleton = RID()) = 0;
virtual AABB mesh_get_aabb(RID p_mesh, RID p_skeleton = RID(), Transform3D p_transform = Transform3D()) = 0;

virtual void mesh_set_shadow_mesh(RID p_mesh, RID p_shadow_mesh) = 0;

Expand Down
2 changes: 2 additions & 0 deletions servers/rendering_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2631,6 +2631,8 @@ void RenderingServer::_bind_methods() {
ClassDB::bind_method(D_METHOD("canvas_item_set_visibility_notifier", "item", "enable", "area", "enter_callable", "exit_callable"), &RenderingServer::canvas_item_set_visibility_notifier);
ClassDB::bind_method(D_METHOD("canvas_item_set_canvas_group_mode", "item", "mode", "clear_margin", "fit_empty", "fit_margin", "blur_mipmaps"), &RenderingServer::canvas_item_set_canvas_group_mode, DEFVAL(5.0), DEFVAL(false), DEFVAL(0.0), DEFVAL(false));

ClassDB::bind_method(D_METHOD("debug_canvas_item_get_rect", "item"), &RenderingServer::debug_canvas_item_get_rect);

BIND_ENUM_CONSTANT(NINE_PATCH_STRETCH);
BIND_ENUM_CONSTANT(NINE_PATCH_TILE);
BIND_ENUM_CONSTANT(NINE_PATCH_TILE_FIT);
Expand Down
9 changes: 9 additions & 0 deletions servers/rendering_server.h
Original file line number Diff line number Diff line change
Expand Up @@ -1457,6 +1457,15 @@ class RenderingServer : public Object {

virtual void canvas_set_shadow_texture_size(int p_size) = 0;

Rect2 debug_canvas_item_get_rect(RID p_item) {
#ifdef TOOLS_ENABLED
return _debug_canvas_item_get_rect(p_item);
#else
return Rect2();
#endif
}
virtual Rect2 _debug_canvas_item_get_rect(RID p_item) = 0;

/* GLOBAL SHADER UNIFORMS */

enum GlobalShaderParameterType {
Expand Down