screen_capture.gd 628 B

123456789101112131415161718192021222324252627
  1. extends Control
  2. func _ready():
  3. get_node("Button").connect("pressed", self, "_on_button_pressed");
  4. func _on_button_pressed():
  5. get_viewport().set_clear_mode(Viewport.CLEAR_MODE_ONLY_NEXT_FRAME)
  6. # Let two frames pass to make sure the screen was captured
  7. yield(get_tree(), "idle_frame")
  8. yield(get_tree(), "idle_frame")
  9. # Retrieve the captured image
  10. var img = get_viewport().get_texture().get_data()
  11. # Flip it on the y-axis (because it's flipped)
  12. img.flip_y()
  13. # Create a texture for it
  14. var tex = ImageTexture.new()
  15. tex.create_from_image(img)
  16. # Set it to the capture node
  17. get_node("capture").set_texture(tex)