In order to create a ghosting effect I need to keep the previously rendered frame and blend it with the current one. I keep the previous frame by turning it into a Texture2D but the performance of ReadPixels is terrible. I’m wondering if there’s a better way to do so.
Here’s the code I have:
// Ghosting.
using UnityEngine;
using System.Collections;
[AddComponentMenu ("OCASM/Image Effects/ImageEffect002")]
public class ImageEffect_002 : MonoBehaviour {
[Range (0,0.99f)]
public float intensity;
private Material ImageEffect_002_Mat;
private Texture2D pastFrame;
void Awake(){
ImageEffect_002_Mat = new Material (Shader.Find("Hidden/OCASM/Image Effects/ImageEffect_002"));
}
void OnRenderImage(RenderTexture src, RenderTexture dst){
if (pastFrame) {
ImageEffect_002_Mat.SetFloat ("_Intensity", intensity);
ImageEffect_002_Mat.SetTexture ("_BTex", pastFrame);
Graphics.Blit (src, dst, ImageEffect_002_Mat);
}
}
void OnPostRender () {
if (!pastFrame) {
pastFrame = new Texture2D (Screen.width, Screen.height);
}
RenderTexture.active = Camera.main.targetTexture;
pastFrame.ReadPixels (new Rect(0,0,Screen.width,Screen.height),0,0);
pastFrame.Apply ();
}
}
Thank you so much for this. I’ve been searching for an effect like this everywhere and couldn’t find anything. I’ve used this code for my game and am going to study it to learn more about the shader that you’ve created, and will make sure to credit you if I use it in my game for the future! Great work!