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():
|
2017-10-07 15:08:34 +02:00
|
|
|
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.
|
2017-10-07 15:08:34 +02:00
|
|
|
yield(get_tree(), "idle_frame")
|
|
|
|
yield(get_tree(), "idle_frame")
|
2017-10-30 12:38:39 +01:00
|
|
|
|
2020-01-31 21:21:23 +01:00
|
|
|
# Retrieve the captured image.
|
2017-10-07 15:08:34 +02:00
|
|
|
var img = get_viewport().get_texture().get_data()
|
2019-07-20 12:36:37 +02:00
|
|
|
|
2020-01-31 21:21:23 +01:00
|
|
|
# Flip it on the y-axis (because it's flipped).
|
2017-10-07 15:08:34 +02:00
|
|
|
img.flip_y()
|
2017-10-30 12:38:39 +01:00
|
|
|
|
2020-01-31 21:21:23 +01:00
|
|
|
# Create a texture for it.
|
2017-10-07 15:08:34 +02:00
|
|
|
var tex = ImageTexture.new()
|
|
|
|
tex.create_from_image(img)
|
2017-10-30 12:38:39 +01:00
|
|
|
|
2020-01-31 21:21:23 +01:00
|
|
|
# Set the texture to the captured image node.
|
|
|
|
captured_image.set_texture(tex)
|