Image Effects
Image effects are shaders that are applied to a camera after the initial camera pass has completed, such as bloom, color shift, blur, and so forth.
Notice that effect shaders do not require lighting, and therefore they are almost always legacy shaders.
Image effects are applied by adding a script to a Camera object:
using UnityEngine;
using System.Collections;
namespace Shaders.Effects {
[RequireComponent(typeof(Camera))]
public class PostProcessingEffect : MonoBehaviour {
[Tooltip("The material to apply the post processing effect with")]
public Material material;
void OnRenderImage (RenderTexture source, RenderTexture destination) {
Graphics.Blit(source, destination, material);
}
}
}
Every pass makes the rendering slower as additional effects must be applied, but you can easily apply multiple consecutive effects in this manner:
using UnityEngine;
using System.Collections;
namespace Shaders.Effects {
public enum PostProcessingEffectAddDebug {
NONE,
DEBUG_PHASE_ONE,
DEBUG_PHASE_TWO
}
[RequireComponent(typeof(Camera))]
public class PostProcessingEffectAdd : MonoBehaviour {
[Tooltip("The material to apply the first post processing effect with")]
public Material material1;
[Tooltip("The material to apply the second post processing effect with")]
public Material material2;
public PostProcessingEffectAddDebug debug = PostProcessingEffectAddDebug.NONE;
private Material addFilter;
void Start() {
addFilter = new Material (Shader.Find("Shaders/Filters/LinearAdd"));
}
void OnRenderImage (RenderTexture source, RenderTexture destination) {
// If we only want the first shader?
if (debug == PostProcessingEffectAddDebug.DEBUG_PHASE_ONE) {
Graphics.Blit(source, destination, material1);
}
// See after second shader is applied
else if (debug == PostProcessingEffectAddDebug.DEBUG_PHASE_TWO) {
// Apply first shader
RenderTexture tmp = RenderTexture.GetTemporary(source.width / 2, source.height / 2);
Graphics.Blit(source, tmp, material1);
// Apply second shader
RenderTexture tmp2 = RenderTexture.GetTemporary(source.width / 2, source.height / 2);
Graphics.Blit(tmp, destination, material2);
Graphics.Blit(tmp, tmp2, material2);
Graphics.Blit(tmp2, tmp, material2);
Graphics.Blit(tmp, destination, material2);
RenderTexture.ReleaseTemporary(tmp);
RenderTexture.ReleaseTemporary(tmp2);
}
// Entire pipeline
else {
// Apply first shader
RenderTexture tmp = RenderTexture.GetTemporary(source.width, source.height);
Graphics.Blit(source, tmp, material1);
// Apply second shader
RenderTexture tmp2 = RenderTexture.GetTemporary(source.width, source.height);
Graphics.Blit(tmp, tmp2, material2);
Graphics.Blit(tmp2, tmp, material2);
Graphics.Blit(tmp, tmp2, material2);
// Release...
RenderTexture.ReleaseTemporary(tmp);
// Combine results using 'Addative' filter
addFilter.SetTexture("_AddTex", tmp2);
Graphics.Blit(source, destination, addFilter);
RenderTexture.ReleaseTemporary(tmp2);
}
}
}
}
Notice that these effects are applied on Camera
objects; applying them to any other object type has no effect.