Apply Image Effect to Image


I’m currently working on a custom Editor Window that does a lot of image processing. I am storing an image as a Texture2D and doing stuff with it.

I would like to be able to apply a Unity Image Effect (e.g. bloom, blur, etc) to the Texture2D in code and get the result as a Texture2D. I don’t know much about how image effects work but i’m happy to learn if someone can point me in the right direction for this.

Does anyone know how to do this? Or if its even possible?

Thank you!

HI there

Yes it is possible, though not entirely simple. The main issue is that the way image effects work is:

  • you attach them to a camera
  • the camera renders whatever it renders to a temporary buffer
  • that buffer gets passed to the image effect, which runs a shader over the whole buffer to generate the final output

The difficulty is that you don’t have objects and cameras and things, so you’ll have to do a bit of work to glue everything together. Your best approach will be:

  • create a game object with a camera on in your scene
  • add any image effect components you want
  • create a render texture and point the camera at it
  • draw a ‘full screen’ quad with your texture on to the camera
  • use Graphics.CopyTexture to copy the camera’s output render texture back into your source texture

there’s a few gotyas along the way. You’ll probably want to make your camera component disabled, as this means it won’t render at all unless you explicitly call Camera.Render on it. That way, when you want to update your texture your code can simply:

  • call Graphics.DrawMesh with a material/quad to draw your texture to the camera
  • call Camera.Render to actually trigger the drawing
  • call Graphics.CopyTexture to get the results

As I say, not entirely simple, but possible! I would like to add that your idea of using the GPU to do image processing is entirely sensible, and in unity creating shaders that copy from 1 render texture to another whilst applying an effect is relatively simple. What makes this tricky is the fact that you want to use the Unity post effect objects to do it.