godot-demo-projects/viewport/screen_capture/screen_capture.gd

23 lines
604 B
GDScript3
Raw Normal View History

2020-01-31 21:21:23 +01:00
extends Node
2017-08-19 19:10:25 +02:00
2020-01-31 21:21:23 +01:00
onready var captured_image = $CapturedImage
2017-08-19 19:10:25 +02:00
2020-01-31 21:21:23 +01:00
func _on_CaptureButton_pressed():
get_viewport().set_clear_mode(Viewport.CLEAR_MODE_ONLY_NEXT_FRAME)
2020-01-31 21:21:23 +01:00
# Let two frames pass to make sure the screen was captured.
yield(get_tree(), "idle_frame")
yield(get_tree(), "idle_frame")
2020-01-31 21:21:23 +01:00
# Retrieve the captured image.
var img = get_viewport().get_texture().get_data()
2020-01-31 21:21:23 +01:00
# Flip it on the y-axis (because it's flipped).
img.flip_y()
2020-01-31 21:21:23 +01:00
# Create a texture for it.
var tex = ImageTexture.new()
tex.create_from_image(img)
2020-01-31 21:21:23 +01:00
# Set the texture to the captured image node.
captured_image.set_texture(tex)