Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Combine multiple frames image effect

Discussion in 'Image Effects' started by RogueCode, Mar 10, 2016.

  1. RogueCode

    RogueCode

    Joined:
    Apr 3, 2013
    Posts:
    230
    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.
    Code (CSharp):
    1.  
    2.   var buffer = RenderTexture.GetTemporary(source.width, source.height, 0);
    3.   // Write this frame to buffer
    4.   Graphics.Blit(source, buffer);
    5.  
    6.   // Write each previous frame
    7.   for (var f = _previousFrames.Count - 1; f > 0; f--)
    8.   {
    9.   var newBuffer = RenderTexture.GetTemporary(source.width, source.height, 0);
    10.   material.SetTexture("_MainTex2", _previousFrames[f]);
    11.   Graphics.Blit(buffer, newBuffer, material);
    12.   RenderTexture.ReleaseTemporary(buffer);
    13.   buffer = newBuffer;
    14.   }
    15.  
    16.   Graphics.Blit(buffer, destination);
    17.   RenderTexture.ReleaseTemporary(buffer);
    18.  
    19.  
    Thanks!
     
    Last edited: Mar 10, 2016
  2. fffMalzbier

    fffMalzbier

    Joined:
    Jun 14, 2011
    Posts:
    3,276
    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.
     
    RogueCode likes this.