如何使用canvas_item_set_transform旋转?

发布于 2025-01-22 11:16:20 字数 1556 浏览 0 评论 0原文

我正在尝试复制Sprite节点,并通过visualserver在屏幕上显示它,到目前为止,这是我基于这个示例,

ci_rid = VisualServer.canvas_item_create()
VisualServer.canvas_item_set_parent(ci_rid, get_canvas_item())

self.remove_child(sprite_node);
sprite_node.queue_free();

var texture = load(sprite_node.texture.resource_path);
var pos_and_size=Rect2(Vector2(0,0), sprite_node.region_rect.size);

if(sprite_node.region_enabled==true):
    VisualServer.canvas_item_add_texture_rect_region(ci_rid,pos_and_size, texture,sprite_node.region_rect);
    var transform_matrix=Transform2D.IDENTITY;
    
    VisualServer.canvas_item_set_transform(ci_rid,transform_matrix.translated(sprite_node.position-sprite_node.region_rect.size / 2))

但是当我尝试在其自身的轴上复制旋转时,这样:

VisualServer.canvas_item_set_transform(ci_rid,
 transform_matrix.rotated(deg2rad(sprite_node.rotation_degrees)).translated(sprite_node.position-sprite_node.region_rect.size / 2)
) 

它没有给出所需的结果,并将整个图像移至另一个位置

原始Sprite节点:

VisualServer副本:

I'm trying to copy a Sprite node and display it on screen through a VisualServer and so far this has been my progress based on this example

ci_rid = VisualServer.canvas_item_create()
VisualServer.canvas_item_set_parent(ci_rid, get_canvas_item())

self.remove_child(sprite_node);
sprite_node.queue_free();

var texture = load(sprite_node.texture.resource_path);
var pos_and_size=Rect2(Vector2(0,0), sprite_node.region_rect.size);

if(sprite_node.region_enabled==true):
    VisualServer.canvas_item_add_texture_rect_region(ci_rid,pos_and_size, texture,sprite_node.region_rect);
    var transform_matrix=Transform2D.IDENTITY;
    
    VisualServer.canvas_item_set_transform(ci_rid,transform_matrix.translated(sprite_node.position-sprite_node.region_rect.size / 2))

but when I try to replicate the rotation on it's own axis like this:

VisualServer.canvas_item_set_transform(ci_rid,
 transform_matrix.rotated(deg2rad(sprite_node.rotation_degrees)).translated(sprite_node.position-sprite_node.region_rect.size / 2)
) 

it doesn't give the desired result and moves the entire image to another position

Original Sprite node:
enter image description here

VisualServer copy:
enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

剪不断理还乱 2025-01-29 11:16:20

您可以在删除global_transform 之前:

var sprite_transform := sprite_node.global_transform

或在将其删除后可以重建它:

var sprite_transform = global_transform * Transform2D(sprite_node.rotation, sprite_node.position).scaled(sprite_node.scale)

我在这里使用global_transform,因为节点曾经是一个孩子代码正在运行。因此,它的转换与之相对。


我真的不知道您的纹理不会遇到错误。这是我最终进行的方式:

var image:Image = sprite_node.texture.get_data()
var texture_rid := VisualServer.texture_create_from_image(image)

然后,将区域和大小像这样处理:

var texture_size:Vector2 = image.get_size()
var region:Rect2 = sprite_node.region_rect if sprite_node.region_enabled else Rect2(Vector2.ZERO, texture_size)
var pos_and_size := Rect2(region.size * -0.5 if sprite_node.centered else Vector2.ZERO, region.size)

这些是我的visualserver调用:

var ci_rid := VisualServer.canvas_item_create()
VisualServer.canvas_item_set_parent(ci_rid, get_canvas_item())
VisualServer.canvas_item_add_texture_rect_region(ci_rid, pos_and_size, texture_rid, region);
VisualServer.canvas_item_set_transform(ci_rid, sprite_transform)

You can take the global_transform before you remove it:

var sprite_transform := sprite_node.global_transform

Or you can reconstruct it after you removed it:

var sprite_transform = global_transform * Transform2D(sprite_node.rotation, sprite_node.position).scaled(sprite_node.scale)

I'm using global_transform here because the node used to be a child of where the code is running. So its transform is relative to it.


I really don't know how you are not getting errors with your texture. This is the way I ended up going about it:

var image:Image = sprite_node.texture.get_data()
var texture_rid := VisualServer.texture_create_from_image(image)

Then the region and size are handled like this:

var texture_size:Vector2 = image.get_size()
var region:Rect2 = sprite_node.region_rect if sprite_node.region_enabled else Rect2(Vector2.ZERO, texture_size)
var pos_and_size := Rect2(region.size * -0.5 if sprite_node.centered else Vector2.ZERO, region.size)

And these are my VisualServer calls:

var ci_rid := VisualServer.canvas_item_create()
VisualServer.canvas_item_set_parent(ci_rid, get_canvas_item())
VisualServer.canvas_item_add_texture_rect_region(ci_rid, pos_and_size, texture_rid, region);
VisualServer.canvas_item_set_transform(ci_rid, sprite_transform)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文