Combine multiple frames image effect

I’m trying to create a fairly simple Image Effect that combines previous frames together to get a sort of “ghosting”. It is similar to motion blur, but instead of adding to a constant image, it would keep a list of the last x frames and combine them.

The issue I’m finding is that my resulting image each frame seems to be just the current frame and the last frame it draws in the loop.

Am I doing something stupid?

EDIT: OK, this works, but it seems inefficient because I need to create a new RenderTexture every frame.

  var buffer = RenderTexture.GetTemporary(source.width, source.height, 0);
  // Write this frame to buffer
  Graphics.Blit(source, buffer);

  // Write each previous frame
  for (var f = _previousFrames.Count - 1; f > 0; f--)
  {
  var newBuffer = RenderTexture.GetTemporary(source.width, source.height, 0);
  material.SetTexture("_MainTex2", _previousFrames[f]);
  Graphics.Blit(buffer, newBuffer, material);
  RenderTexture.ReleaseTemporary(buffer);
  buffer = newBuffer;
  }

  Graphics.Blit(buffer, destination);
  RenderTexture.ReleaseTemporary(buffer);

Thanks!

With a temporary rendertexture Unity will reuse the the same memory space that a previews temporary rendertexture did use if the parameters are the same. So its not in-efficient at all. Unity does it to int there image effects.
Sure you can Allocate a rendertexture your self but that would mean that the memory space can not be reused by other scripts that would generate a temporary rendertexture.

1 Like