Feedback Loops

SabataSabata Posts: 2Member

I would like to use shaders for simple simulations like these.

The basic idea is to manipulate a texture with a shader and then feed it back into the shader
for the next frame. So the shaders effect can accumulate over time.

So my question is, is there a way to access the output of a shader and feed it back in as a uniform sampler2D?
Ideally without copying or leaving the GPU memory.

Answers

  • SabataSabata Posts: 2Member
    edited November 2018 Answer ✓

    Got it working using a TextureRect inside a Viewport inside a TextureRect. The inner TextureRect applies a shader to its texture which the Viewport captures. Then the Viewports texture becomes the new texture of the inner TextureRect.

    The outer TextureRect is used to display the Viewports content in the main viewport. It has the following
    GDScript attached:

    extends TextureRect
    
    func _ready():
        $iterate_viewport/iterate_texture.texture
        $iterate_viewport/iterate_texture.texture = $iterate_viewport.get_texture()
        texture = $iterate_viewport.get_texture()
    
    func _process(delta):
        $iterate_viewport/iterate_texture.material.set_shader_param("draw_pos", get_local_mouse_position())
    
    
Sign In or Register to comment.