首先是 ViewportTexture,不能手动构造再进行赋值( new ViewportTexture; ViewportTexture.ViewportPath = SubViewport.GetPath(); )。需要使用 SubViewport.GetTexture() 获取有效的 ViewportTexture。
然后从 ViewportTexture 抓取 image 时需要等待一帧的绘制完毕,GetTexture 方法有提示:
***
Returns the viewport's texture. Note: When trying to store the current texture (e.g. in a file), it might be completely black or outdated if used too early, especially when used in e.g. _Ready(). To make sure the texture you get is correct, you can await FramePostDraw signal.
func _ready():
await RenderingServer.frame_post_draw
$Viewport.get_texture().get_image().save_png("user://Screenshot.png")
***
示例中 await 就是事件的变形。
而实际使用时,ViewportTexture 好像还需要添加到场景树中才行,实际应用时就是要把 ViewportTexture 赋值给 Sprite3.Texture,然后再到 RenderingServer.FramePostDraw 里通过 ViewportTexture.GetImage() 获取 Image。
暂无关于此日志的评论。