Efficient way to sample screen for afterimage shader?

Godot Version

4.2.2

Question

Hello, I’m going for a tracer effect kind of like this but more blurred:

Right now I have it working but it’s not very performant. My way of sampling the screen is with the following operations

var curr_image = $SubViewport.get_texture().get_image()
var texture = ImageTexture.create_from_image(curr_image)
material.set_shader_parameter("curr_texture", texture)

SubViewport is the view of the player, and I sample every few frames.
The shader averages the pixels together of 4 images it stores in it’s parameters to create an after image effect.

Is there anyway to make this more efficient? Maybe completely through shader code?

Thank you!

Is this what you are looking for?

Thanks for the reply

but not quite, in my use case I need to store the current view for a few frames. So I can average them over time to create an afterimage.

With pure shader code I couldn’t find a way to store data in memory for operations a few frames later. But if there was a way it would definitely be more efficient than gdscript.