0 votes

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)
    viewport.add_child(poly)

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

    print(viewport.get_render_info())
    var img = viewport.get_texture().get_data()
    img.flip_y()
    img.save_png("screenshot.png")

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?

asked Mar 11 in Engine by RedPilled (34 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.)

answered Mar 12 by Calinou (6,336 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 How to use this Q&A? before posting your first questions.