如何使用canvas_item_set_transform旋转?
我正在尝试复制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)
)
它没有给出所需的结果,并将整个图像移至另一个位置
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以在删除
global_transform
之前:或在将其删除后可以重建它:
我在这里使用
global_transform
,因为节点曾经是一个孩子代码正在运行。因此,它的转换与之相对。我真的不知道您的纹理不会遇到错误。这是我最终进行的方式:
然后,将区域和大小像这样处理:
这些是我的
visualserver
调用:You can take the
global_transform
before you remove it:Or you can reconstruct it after you removed it:
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:
Then the region and size are handled like this:
And these are my
VisualServer
calls: