Adding bloom (glow) effect to shader (Unity game engine)

28,372

A shader program is just code used to draw triangles on the screen. Bloom effects are a completely different beast, and are screen-space calculations that are done after the geometry is drawn. You won't get a bloom effect just by modifying the object's shader.

Simply speaking, with a shader you can never draw "outside the lines", and here you need to alter pixels that are outside the object's reach. I'm sorry, but it's just not within the shader's capabilities.

Still, you can make it work by implementing Image Effect scripts, like unity's built-in bloom effect. Once the script is added to the camera, and the camera's HDR setting is activated, you can use a special shader that will result in a glow, but not before all that.

Once you have set up the effect correctly (and enable the HDR option on the camera), you can now use any shader that returns values greater than 1 in the pixel shader to generate a glow effect around the object. The shader you posted is a legacy shader program. Here's the updated code, with a Glow multiplier included:

Shader "Glow" {
    Properties {
        _MainTex ("Texture", 2D) = "white" {}
        _Color ("Color", Color) = (1,1,1,1)
        _Glow ("Intensity", Range(0, 3)) = 1
    }
    SubShader {
        Tags { "Queue" = "Transparent" "IgnoreProjector" = "True" "RenderType" = "Transparent" }
        LOD 100
        Cull Off
        ZWrite On
        Blend SrcAlpha OneMinusSrcAlpha
        Pass {
            CGPROGRAM
                #pragma vertex vert
                #pragma fragment frag
                sampler2D _MainTex;
                half4 _MainTex_ST;
                fixed4 _Color;
                half _Glow;
                struct vertIn {
                    float4 pos : POSITION;
                    half2 tex : TEXCOORD0;
                };
                struct v2f {
                    float4 pos : SV_POSITION;
                    half2 tex : TEXCOORD0;
                };
                v2f vert (vertIn v) {
                    v2f o;
                    o.pos = mul(UNITY_MATRIX_MVP, v.pos);
                    o.tex = v.tex * _MainTex_ST.xy + _MainTex_ST.zw;
                    return o;
                }
                fixed4 frag (v2f f) : SV_Target {
                    fixed4 col = tex2D(_MainTex, f.tex);
                    col *= _Color;
                    col *= _Glow;
                    return col;
                }
            ENDCG
        }
    }
}
Share:
28,372

Related videos on Youtube

Author by

ARTAGE

ARTAGE

Updated on November 13, 2020

Comments

  • ARTAGE almost 3 years

    I am creating iOS app in Unity game engine.

    I am trying to rewrite my shader so that the material, witch using it, gave Bloom (Glow) effect (like a Halo component).

    An example of how it should look:

    Bloom (Glow) effect

    enter image description here

    I really searched answer in the internet but did not find anything for the worker or of suitable solution to my problem.

    My shader's code:

    Shader "Unlit"
    { 
        Properties
        {
            _MainTex("Base (RGB) Trans (A)", 2D) = "white" {}
            _Color("Main Color", Color) = (1, 1, 1, 1) 
        }
        SubShader
        {
            Tags{ "Queue" = "Transparent" "IgnoreProjector" = "True" "RenderType" = "Transparent" }
            LOD 100 
            Cull off
            ZWrite on 
            Blend SrcAlpha OneMinusSrcAlpha
            Pass 
            { 
                Lighting Off
                SetTexture[_MainTex]
                {
                    constantColor[_Color]
                    Combine texture * constant, texture * constant 
                }
            } 
        } 
    }
    
    • Absinthe
      Absinthe over 7 years
      Emission, and thus bloom amount comes fromthe value of the final pixel color value which should usually be in the HDR range. Try adding an emission property e.g. _Emission("Emmisive Strength", Range(0.01,3.0)) = 1.0. Then multiply your output by this property.
  • ARTAGE over 7 years
    Unity selective per-object bloom - m.youtube.com/watch?v=XJ0tMHDrzDo. How I can create something like this with my shader?
  • Emilio Martinez over 7 years
    By using a script on the camera like they use on the video. Look at the camera's inspector at 2:16
  • ARTAGE over 7 years
    So can you show me how it should be done in my shader witch I add to question?
  • Emilio Martinez over 7 years
    I simply added a multiplier to the color output. This will make the glow effect work if a Bloom script is used. Still, it's a bit strange that it's a transparent shader with ZWrite On, you might get unexpected results
  • ARTAGE over 7 years
    Can I use bloom(glow) effect for only one object (like on video)?
  • Emilio Martinez over 7 years
    Yes, you can. Here's how it works: the effect affects the camera, and whatever is drawn using this shader will be bloomed. If you only want one object with bloom, then only use this shader in one object
  • ARTAGE over 7 years
    You decide my problem. Thanks you!

Related