为着色器添加bloom(glow)效果(Unity游戏引擎)

时间:2016-07-23 12:43:32

标签: ios unity3d opengl-es shader unity5

我正在Unity game engine创建 iOS应用

我正在尝试重写我的 着色器 ,以便使用它的 材料 给了 绽放(辉光)效果 (如Halo component)。

一个示例 ,它应该如何显示:

Bloom (Glow) effect

enter image description here

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

我的着色器的 代码

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

1 个答案:

答案 0 :(得分:6)

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

简单地说,使用着色器,您无法在线条之外绘制"在这里,您需要更改对象范围之外的像素。对不起,但它不在着色器的功能范围内。

但是,您可以通过实施Image Effect脚本来实现它,例如团结built-in bloom effect。将脚本添加到相机并激活相机的HDR设置后,您可以使用一个特殊的着色器,这将导致发光,但不会在所有之前。

正确设置效果后(并在相机上启用HDR选项),您现在可以使用在像素着色器中返回大于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
        }
    }
}
相关问题