Why is hint_screen_texture giving an empty texture?

Godot Version

4.4.1

Question

How do I use hint_screen_texture to get the previously rendered texture. When I try to use it it seems to always be empty.

I have the following shader code:

shader_type canvas_item;

uniform sampler2D screen_texture : hint_screen_texture;

void fragment() {
vec3 oldColor = texture(screen_texture,SCREEN_UV).rgb;
COLOR = vec4(oldColor+vec3(0.1,0.1,0.1),1);
}

I’m trying to make a shader that slowly fades to white (this isn’t the actual goal, just a simple example to reproduce the issue I’m seeing). This shader is attached (with a ShaderMaterial) to a Polygon2D which is the only child of the main node2D.

The output does not fade to white, but stays the same constant gray color forever.

I think with canvasItem you need to do textureLod

shader_type canvas_item;

uniform sampler2D screen_texture : hint_screen_texture, repeat_disable, filter_nearest;

void fragment() {
    COLOR = textureLod(screen_texture, SCREEN_UV, 0.0);
}

of course we need to rule out other problems too. that code looks like it could cause problems.

for fading to white, we use mix, not addition.


COLOR = mix(oldColor, vec3(1), fade_amount);

I made those changes but I’m still seeing the same thing. No matter how long this runs it always stays the same dark gray. I thought this was supposed to fade to white, getting 10% closer to all white every step.

shader_type canvas_item;

uniform sampler2D screen_texture : hint_screen_texture,repeat_disable,filter_nearest;
void fragment() {
vec3 oldColor = textureLod(screen_texture,SCREEN_UV,0.0).rgb;
COLOR = vec4(mix(oldColor,vec3(1.0),0.1),1);
}

And these are the properties on the Image (Polygon2d) node where the shader is attached

I don’t think hint_screen_texture actually returns the last frame, but the current frame up to the object with the shader.

From the tutorial Screen-reading shaders:

Godot Shading language has a special texture to access the already rendered contents of the screen. It is used by specifying a hint when declaring a sampler2D uniform: hint_screen_texture.