为shader添加bloom(发光)效果(Unity游戏引擎)

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

我正在 iOS 应用 Unity game engine.

我正在尝试重写我的 shader 以便 material 、 使用它,产生了 Bloom(发光)效果(就像 Halo component).

示例 它应该如何显示:

我真的在互联网上搜索了答案,但没有找到任何适合工人或适合我问题的解决方案。

我的着色器的代码:

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 
            }
        } 
    } 
}

着色器程序只是用于在屏幕上绘制三角形的代码。绽放效果是完全不同的野兽,and are screen-space calculations that are done after the geometry is drawn。仅通过修改对象的着色器不会获得光晕效果。

简单地说,使用着色器您永远无法绘制 "outside the lines",在这里您需要更改对象无法触及的像素。抱歉,这不在着色器的能力范围内。

不过,您可以通过实施 Image Effect scripts, like unity's built-in bloom effect 使其工作。将脚本添加到相机并激活相机的 HDR 设置后,您可以使用特殊的着色器来产生光晕,但在此之前不会。

一旦您正确设置了效果(并在相机上启用 HDR 选项),您现在可以在像素着色器中使用任何 returns 值大于 1 的着色器来生成周围的发光效果物体。您发布的着色器是旧版着色器程序。这是更新后的代码,其中包含一个 Glow 倍增器:

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
        }
    }
}