+1 vote

I am trying to render something into a off screen viewport and take a screenshot of it by doing something like this:

func _ready() -> void:
    var viewport = Viewport.new()
    viewport.size = Vector2(1000,1000)
    viewport.usage = Viewport.USAGE_2D
    viewport.render_target_update_mode = Viewport.UPDATE_ONCE

    #add some content
    var poly = Polygon2D.new()
    poly.polygon = [Vector2(20,20), Vector2(100,20), Vector2(60,60)]
    poly.color = Color(1,1,1,1)

    add_child(viewport) #removing this line will result in empty screenshot
    yield(VisualServer, "frame_post_draw")

    var img = viewport.get_texture().get_data()

But when I don't do add_child(viewport), screenshot is empty. I guess render is not initiated unless added to scene tree. Is there a way to render the viewport without adding it as a child?

in Engine by (44 points)

1 Answer

0 votes

I don't think you can render a viewport to a texture without adding it to the scene tree. However, I think the viewport might be able to render its contents even if you place it outside the camera's bounds. (If objects disappear when you do this, you may have to extend their visibility/culling margins.)

by (12,835 points)
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.