Table of Contents

How to use post effects

Built-in Render Pipeline

Post effects for the Built-in Render Pipeline are those components that implement the IPostProcess interface.

Adding an effect directly to a camera

  1. Add the post-process component directly to the camera you want it to affect.

Using PostProcessRunner

  1. Create an empty GameObject to hold your post effects (for example, name it PostEffects). Add the effect components to this object or to its children.
  2. Add a PostProcessRunner component to your camera.
  3. Assign the PostEffects object to the EffectsRoot field of the PostProcessRunner.

This workflow is useful when you have multiple cameras or when you want to enable and disable many effects while experimenting.

Using your own post-process component

You can also use the shader inside a custom post-process component. Here is an example:

using UnityEngine;

namespace Gamelogic.Fx.Samples.BuiltIn
{
    /// <summary>
    /// This is an example of how to write a post process component to use one opf the post-process shaders meant for the
    /// Built-in Render Pipeline. 
    /// </summary>
    [ExecuteInEditMode]
    [RequireComponent(typeof(Camera))]
    public class MyAdjustGamma : MonoBehaviour
    {
        // Expose properties in the inspector you want the user to control 
        [Range(0.1f, 5f)]
        [SerializeField] private float gamma = 2.2f;
    
        [SerializeField] private Shader shader;

        private Material material;

        private void OnEnable()
        {
            // Find the shader to use. 
            if (shader == null)
            {
                shader = Shader.Find("Gamelogic/Fx/BuiltIn/AdjustGamma");
            }

            // Create a material for the shader.  
            if (shader != null && material == null)
            {
                material = new Material(shader)
                {
                    hideFlags = HideFlags.DontSave
                };
            }
        }

        // The work happens here. If this component is attached to a camera, this method is called automatically by Unity.
        private void OnRenderImage(RenderTexture src, RenderTexture dest)
        {
            // If the material is not ready, just blit the source to destination.
            if (material == null)
            {
                Graphics.Blit(src, dest);
                return;
            }

            // SetTextureTiling the properties on the material and do the blit.
            material.SetFloat("_Gamma", gamma);
            Graphics.Blit(src, dest, material);
        }

        private void OnDisable()
        {
            // Cleanup the material.
            if (material != null)
            {
                DestroyImmediate(material);
            }
        }
    }
}