Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 
            }
        } 
    } 
}
like image 369
ARTAGE Avatar asked Jul 23 '16 12:07

ARTAGE


1 Answers

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
        }
    }
}
like image 166
Emilio Martinez Avatar answered Nov 14 '22 23:11

Emilio Martinez