Help with screen-reading shader

Godot Version

4.3

Question

I would like a canvas item shader to make a sprite copy the screen’s texture at its position shifted by some vec2 offset.
This is what I came up with:

shader_type canvas_item;

uniform sampler2D SCREEN_TEXTURE : hint_screen_texture;

uniform vec2 offset = vec2(0.1, 0.1);

void fragment() {
	COLOR = texture(SCREEN_TEXTURE, SCREEN_UV+offset);
}

And the screen texture actually gets shifted!
But…
When I zoom the offset changes
And that makes sense because
compared to the image, the offset looks greater if the image appears small on the screen and vice versa the movement appears smaller if the image appears big on the screen
The problem is…
I absolutely got no idea how to fix this
Can anybody help me?
Thanks in advance!

Solved!
Solution:

shader_type canvas_item;

uniform sampler2D SCREEN_TEXTURE : hint_screen_texture;

uniform vec2 offset = vec2(0.1, 0.1);

varying mat4 CM;

void vertex() {
	CM = CANVAS_MATRIX;
}

void fragment() {
	vec2 scl = vec2( length(CM[0]) , length(CM[1]));
	COLOR = texture(SCREEN_TEXTURE, SCREEN_UV+offset*scl);
}

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.