ScreenTexture in Viewport with transparency

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By powertomato

I’m trying to read the alpha value of a viewport with “Transparent BG” enabled using the following shader:

shader_type canvas_item;

uniform sampler2D SCREEN_TEXTURE : hint_screen_texture, filter_linear_mipmap;

void fragment() {
	vec4 col = texture(SCREEN_TEXTURE, SCREEN_UV);
	if (col.a < 0.5) {
		COLOR = vec4(1.,0.,0.,1.);
	}
}

It is applied on a ShaderMaterial from a Sprite2D inside the viewport. The alpha value is a constant 1.0. Am I correct to suspect SCREEN_TEXTURE is still the screen texture of the screen, rather than the viewport?
Is it possible to read the contents of the viewport itself?

Edit:
Through experimentation I found out that the SCREEN_TEXTURE is not referring to the entire screen, but the viewport. However for some reason the alpha channel is always 1.0, even for the transparent parts.

My test involves two rectangles, a green and a white one.
If I change the code slightly to use the green channel (i.e. col.g < 0.5) this is the result I get:
enter image description here
The non-overlapping part is red as expected

The original shader always reads alpha values of 1.0. So it appears like this:
enter image description here