Search Unity

Simple bloom post process shader

Discussion in 'Shaders' started by aubergine, Sep 5, 2011.

  1. aubergine

    aubergine

    Joined:
    Sep 12, 2009
    Posts:
    2,878
    Hello people,

    Here is a simple bloom effect, sort of faking it as cheap as possible. I would appreciate if anyone with ios or android capabilities to test it and tell me if it works on mobile platforms.

    By the way, you can get 34 more post process effects for your games in the AssetStore following this Forum Post.

    This is the effect file itself, save it as PP_BloomSimple.cs file.
    Code (csharp):
    1. using UnityEngine;
    2.  
    3. [ExecuteInEditMode]
    4. [AddComponentMenu("Image Effects/Aubergine/BloomSimple")]
    5. public class PP_BloomSimple : MonoBehaviour {
    6.     //Color
    7.     public float strength = 0.5f;
    8.    
    9.     public Shader shader;
    10.     private Material m_Material;
    11.     //Properties
    12.     protected Material material {
    13.         get {
    14.             if (m_Material == null) {
    15.                 m_Material = new Material(shader);
    16.                 m_Material.hideFlags = HideFlags.HideAndDontSave;
    17.             }
    18.             return m_Material;
    19.         }
    20.     }
    21.     //Methods
    22.     protected void Start () {
    23.         // Disable if we don't support image effects
    24.         if (!SystemInfo.supportsImageEffects) {
    25.             enabled = false;
    26.             return;
    27.         }
    28.         // Disable the image effect if the shader can't run on the users graphics card
    29.         if (!shader || !shader.isSupported) enabled = false;
    30.     }
    31.     protected void OnDisable () {
    32.         if( m_Material ) {
    33.             DestroyImmediate(m_Material);
    34.         }
    35.     }
    36.  
    37.     void Awake () {
    38.         material.SetFloat("_Strength", strength);
    39.     }
    40.     void OnEnable () {
    41.         shader = Shader.Find("Hidden/Aubergine/BloomSimple");
    42.     }
    43.     // Called by camera to apply image effect
    44.     void OnRenderImage (RenderTexture source, RenderTexture destination) {
    45.         material.SetFloat("_Strength", strength);
    46.         Graphics.Blit (source, destination, material);
    47.     }
    48. }
    This is the shader file.
    Code (csharp):
    1. Shader "Hidden/Aubergine/BloomSimple" {
    2.     Properties {
    3.         _MainTex ("Base (RGB)", 2D) = "white" {}
    4.         _Strength ("Bloom Strength", Float) = 0.5
    5.     }
    6.     SubShader {
    7.         Pass {
    8.             ZTest Always Cull Off ZWrite Off Lighting Off Fog { Mode off }
    9.             CGPROGRAM
    10.                 #pragma vertex vert_img
    11.                 #pragma fragment frag
    12.                 #pragma fragmentoption ARB_precision_hint_fastest
    13.                 #include "UnityCG.cginc"
    14.  
    15.                 sampler2D _MainTex;
    16.                 float _Strength;
    17.  
    18.                 float4 frag (v2f_img i) : COLOR {
    19.                     float4 col = tex2D(_MainTex, i.uv);
    20.                     float4 bloom = col;
    21.                     col.rgb = pow(bloom.rgb, _Strength);
    22.                     col.rgb *= bloom;
    23.                     col.rgb += bloom;
    24.                     return col;
    25.                 }
    26.             ENDCG
    27.         }
    28.     }
    29.     Fallback off
    30. }
     
  2. ole

    ole

    Unity programmer

    Joined:
    Sep 30, 2010
    Posts:
    56
    hello, afaik, pow() per pixel quickly destroys performance on iOS. a lookup texture encoding the strength could probably help. "fixed4" instead of "float4" for all colors (which results to lowp vec4 in glsl) will speed up things as well.
     
  3. aubergine

    aubergine

    Joined:
    Sep 12, 2009
    Posts:
    2,878
    Thanks for the tips :)