+5 votes

Hello.
I've a canvas on which i draw a lot of stuff through draw* functions.
This canvas can be very big so it won't fit viewport. The question is, is there a way to draw/save entire canvas to image or i have to do it other way?
I care about the final image quality.

Thanks for attention.

Godot version 3.5.1 stable
in Engine by (20 points)

1 Answer

+1 vote
Best answer

I think the way to go about this is using the custom_viewport property of CanvasLayer
I got saving of the image working with this giant label that is bigger than the main Viewport

Several paragraphs of "Lorem ipsum"

For this node setup, the node Content is a PanelContainer and therefore the size of the everything being draw on this CanvasLayer. It doesn't need to be a PanelContainer just something that contains everything being drawn.

CanvasLayer with a PanelContainer has label as its child. A TextureRect is also the child of the CanvasLayer

The CanvasLayer has a script attached to it with the following code:

extends CanvasLayer

func _ready() -> void:
    var viewport := Viewport.new()
    viewport.disable_3d = true
    viewport.size = $Content.rect_size
    viewport.render_target_update_mode = Viewport.UPDATE_ONCE
    custom_viewport = viewport
    get_parent().call_deferred("add_child", viewport)
    $"../TextureRect".texture = viewport.get_texture()

The Viewport can be manually updated again (therefore updating the image) with the following line code:
custom_viewport.render_target_update_mode = Viewport.UPDATE_ONCE

If you just want to save the texture and then free everything you can call .duplicate() on the texture (this must be done after at least one frame is drawn or else it'll be fully black) and then .queue_free() on the Viewport and your CanvasLayer

Hope this helps :)

by (46 points)
selected by

Ah, big thanks
I've already done this with viewport but more bruteforce way, hope your code 'll help do it better way

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.