Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Apply post processing image effects to specific camera layer

Discussion in 'Shaders' started by jc_lvngstn, Jun 30, 2012.

  1. jc_lvngstn


    Jul 19, 2006
    Is it possible to create an post processing shader that only applies its effects to the camera it is assigned to, and not cameras that render before?

    I want to be able to switch objects to a different layer (a "glow" layer), and have a camera that only renders those objects. That "glow" camera has the glow post processing effect on it.
    Unfortunately...the main camera that renders the rest of my scene gets the glow applied also. Pretty much rendering the entire process useless.
    It would be very helpful if, when assigning a post processing effect to a camera, you could select whether or not it effects what is drawn by previous cameras.
  2. BIG-BUG


    Mar 29, 2009
    Image effects apply always to the current content on screen, which includes any pixels rendered with another camera before.
    However, some effects like "glow" are using the alpha channel as mask. So all you have to do is clear the alpha channel, either by not writing it in the first place or by clearing it afterwards.

    Clearing the alpha could be achieved by using the following image effect (on your first camera):
    Code (csharp):
    2. using UnityEngine;
    3. using System.Collections;
    5. [ExecuteInEditMode]
    6. public class ImageEffectClearAlpha : MonoBehaviour {
    8.     //Shader clears alpha channel only
    9.     private static string clearAlphaMatString =
    10. @"Shader ""ClearAlpha"" {
    11.     SubShader {
    12.         ColorMask A
    13.         ZTest Always Cull Off ZWrite Off Fog { Mode Off }
    14.        Pass { Color (0,0,0,0) }
    15.     }
    16.     Fallback off
    17. }";
    19.     static Material m_Material = null;
    20.     protected static Material material {
    21.         get {
    22.             if (m_Material == null) {
    23.                 m_Material = new Material( clearAlphaMatString );
    24.                 m_Material.hideFlags = HideFlags.HideAndDontSave;
    25.                 m_Material.shader.hideFlags = HideFlags.HideAndDontSave;
    26.             }
    27.             return m_Material;
    28.         }
    29.     }
    32.     // Called by camera to apply image effect
    33.     void OnRenderImage (RenderTexture source, RenderTexture destination) {
    34.         Graphics.Blit (source, destination, material);
    35.     }
    36. }
  3. jc_lvngstn


    Jul 19, 2006
    Many thanks, BIG BUG. I'm still experimenting with this, but it works very well!