后處理之輝光效果

這是我在《游戲架構-核心技術與面試精粹》看的蟆技,記錄一下~

泛光和輝光不一樣么汰现?
那是肯定的

輝光(Glow):是全屏泛光的升級版本
區(qū)別就是輝光要求場景內(nèi)只有部分物體泛光,而不是全部泛光
所以需要區(qū)分出忽略那些物體


大體思路:
1.用一個額外的攝像機給,在這個攝像機下宴合,所有的非泛光物體都是黑色的,泛光物體保持原樣
2.再將這個結果渲染到一個 RT 上迹鹅,對他進行模糊
3.最后將模糊后的反光圖疊加到主攝像機的渲染圖像上

創(chuàng)建一個 LightGeometry.shader
默認的surface shader卦洽,改了 RenderType
RenderType 可以使用自定義名稱,并不會對改 shader 的使用和著色器產(chǎn)生影響
需要自己區(qū)別場景中不同渲染對象使用的 Shader 的 RenderType 的類型名稱不同

Shader "Custom/LightGeometry"
{
    Properties
    {
        _Color ("Color", Color) = (1,1,1,1)
        _MainTex ("Albedo (RGB)", 2D) = "white" {}
        _Glossiness ("Smoothness", Range(0,1)) = 0.5
        _Metallic ("Metallic", Range(0,1)) = 0.0
    }
    SubShader
    {
        Tags { "RenderType"="Glow" } //只改了這里
        LOD 200

        CGPROGRAM
        // Physically based Standard lighting model, and enable shadows on all light types
        #pragma surface surf Standard fullforwardshadows

        // Use shader model 3.0 target, to get nicer looking lighting
        #pragma target 3.0

        sampler2D _MainTex;

        struct Input
        {
            float2 uv_MainTex;
        };

        half _Glossiness;
        half _Metallic;
        fixed4 _Color;

        // Add instancing support for this shader. You need to check 'Enable Instancing' on materials that use the shader.
        // See https://docs.unity3d.com/Manual/GPUInstancing.html for more information about instancing.
        // #pragma instancing_options assumeuniformscaling
        UNITY_INSTANCING_BUFFER_START(Props)
            // put more per-instance properties here
        UNITY_INSTANCING_BUFFER_END(Props)

        void surf (Input IN, inout SurfaceOutputStandard o)
        {
            // Albedo comes from a texture tinted by color
            fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
            o.Albedo = c.rgb;
            // Metallic and smoothness come from slider variables
            o.Metallic = _Metallic;
            o.Smoothness = _Glossiness;
            o.Alpha = c.a;
        }
        ENDCG
    }
    FallBack "Diffuse"
}

在相機那邊斜棚,可以通過該函數(shù) 動態(tài)替換攝像機下物體的 shader

 GetComponent<Camera>().RenderWithShader(replaceShader, "RenderType");

當調(diào)用 RenderWithShader 是阀蒂,會檢查攝像機下的每個物體的 RenderTag
(通常選取 Tag 的條件都是 RenderType)

替換的過程:
1.遍歷所有要顯示的物體
2.如果物體的 Shader 與傳入的 replaceShader 有相同的 RenderType
3.則用 replaceShader 的響應 subshader 替換

下面的 shader该窗,增加了一個 subshader
用來將 RenderType 為 Opaque 的物體替換成為黑色,而為 Glow 的內(nèi)容保持不變
(不發(fā)光的物體要繪制呈黑色蚤霞,避免遮擋關系會出錯)

Shader "Custom/RenderGlowRT"
{
    Properties
    {
        _Color ("Color", Color) = (1,1,1,1)
        _MainTex ("Albedo (RGB)", 2D) = "white" {}
        _Glossiness ("Smoothness", Range(0,1)) = 0.5
        _Metallic ("Metallic", Range(0,1)) = 0.0
    }
    SubShader
    {
        Tags { "RenderType"="Glow" }
        LOD 200

        CGPROGRAM
        // Physically based Standard lighting model, and enable shadows on all light types
        #pragma surface surf Standard fullforwardshadows

        // Use shader model 3.0 target, to get nicer looking lighting
        #pragma target 3.0

        sampler2D _MainTex;

        struct Input
        {
            float2 uv_MainTex;
        };

        half _Glossiness;
        half _Metallic;
        fixed4 _Color;

        // Add instancing support for this shader. You need to check 'Enable Instancing' on materials that use the shader.
        // See https://docs.unity3d.com/Manual/GPUInstancing.html for more information about instancing.
        // #pragma instancing_options assumeuniformscaling
        UNITY_INSTANCING_BUFFER_START(Props)
            // put more per-instance properties here
        UNITY_INSTANCING_BUFFER_END(Props)

        void surf (Input IN, inout SurfaceOutputStandard o)
        {
            // Albedo comes from a texture tinted by color
            fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
            o.Albedo = c.rgb;
            // Metallic and smoothness come from slider variables
            o.Metallic = _Metallic;
            o.Smoothness = _Glossiness;
            o.Alpha = c.a;
        }
        ENDCG
    }

    SubShader
    {
        Tags {"RenderType" = "Opaque"}
        LOD 200

        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag

            #include "UnityCG.cginc"

            struct appdata
            {
                float4 vertex : POSITION;
                float2 uv : TEXCOORD0;
            };

            struct v2f
            {
                float2 uv : TEXCOORD0;
                float4 vertex : SV_POSITION;
            };

            sampler2D _MainTex;
            float4 _MainTex_ST;

            v2f vert(appdata v)
            {
                v2f o;
                o.vertex = UnityObjectToClipPos(v.vertex);
                o.uv = TRANSFORM_TEX(v.uv, _MainTex);
                return o;
            }

            fixed4 frag(v2f i) : SV_TARGET
            {
                fixed4 col = fixed4(0, 0, 0, 1);
                return col;
            }

            ENDCG
        }
    }

    FallBack "Diffuse"
}

再創(chuàng)建一個 CustomGlow.cs 腳本酗失,負責:
1.在主攝像機下創(chuàng)建了參數(shù)相同的子攝像機
2.并把這個攝像機的渲染目標設置為內(nèi)存中的一個 RenderTextrue
3.渲染時,通過 RenderImage 將其與主攝像機的內(nèi)容進行混合
4.使用 RenderWithShader 實現(xiàn)特效時争便,應該將相機設為disable级零,防止影響原圖的繪制

public class CustomGlow : MonoBehaviour
{
    public Shader renderGlowShader;
    public Material blurMaterial;
    public Material mixMaterial;

    private RenderTexture rt;


    private void Start()
    {
        Camera mainCam = GetComponent<Camera>();

        this.rt = new RenderTexture(Screen.width, Screen.height, (int)mainCam.depth);

        GameObject go = new GameObject("CameraRT");
        go.transform.SetParent(transform, false);

        Camera cam = go.AddComponent<Camera>();
        cam.enabled = false;
        cam.clearFlags = CameraClearFlags.SolidColor;
        cam.backgroundColor = Color.black;
        cam.orthographic = mainCam.orthographic;
        cam.orthographicSize = mainCam.orthographicSize;
        cam.nearClipPlane = mainCam.nearClipPlane;
        cam.farClipPlane = mainCam.farClipPlane;
        cam.fieldOfView = mainCam.fieldOfView;
        cam.fieldOfView = mainCam.fieldOfView;
        cam.targetTexture = rt;

        RenderPostEffect bloomTex = go.AddComponent<RenderPostEffect>();
        bloomTex.replaceShader = renderGlowShader;
        bloomTex.renderMaterial = blurMaterial;

        mixMaterial.SetTexture("_BlurTex", rt);
    }

    private void OnRenderImage(RenderTexture source, RenderTexture destination)
    {
        Graphics.Blit(source, destination, mixMaterial);
    }
}

最后創(chuàng)建一個 mixTexture.shader 文件

Shader "Custom/MixTexture"
{
    Properties
    {
        _MainTex ("Texture", 2D) = "white" {}
        _BlurTex ("Blur Textrue", 2D) = "White" {}
        _BlurFactor ("Blur Factor", Range(0, 1)) = 0.5
    }
    SubShader
    {
        Tags { "RenderType"="Opaque" }
        LOD 100

        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            // make fog work
            #pragma multi_compile_fog

            #include "UnityCG.cginc"

            struct appdata
            {
                float4 vertex : POSITION;
                float2 uv : TEXCOORD0;
            };

            struct v2f
            {
                float2 uv : TEXCOORD0;
                UNITY_FOG_COORDS(1)
                float4 vertex : SV_POSITION;
            };

            sampler2D _MainTex;
            float4 _MainTex_ST;

            sampler2D _BlurTex;
            float4 _BlurTex_ST;

            float _BlurFactor;

            v2f vert (appdata v)
            {
                v2f o;
                o.vertex = UnityObjectToClipPos(v.vertex);
                o.uv = TRANSFORM_TEX(v.uv, _MainTex);
                UNITY_TRANSFER_FOG(o,o.vertex);
                return o;
            }

            fixed4 frag (v2f i) : SV_Target
            {
                // sample the texture
                fixed4 col = tex2D(_MainTex, i.uv);
                fixed4 blur = tex2D(_BlurTex, i.uv);
                fixed4 final = col + blur * _BlurFactor;
                // apply fog
                UNITY_APPLY_FOG(i.fogCoord, final);
                return final ;
            }
            ENDCG
        }
    }
}

最后把它掛到相機上,掛到相機上:

最后編輯于
?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末滞乙,一起剝皮案震驚了整個濱河市奏纪,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌斩启,老刑警劉巖序调,帶你破解...
    沈念sama閱讀 217,657評論 6 505
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異兔簇,居然都是意外死亡发绢,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,889評論 3 394
  • 文/潘曉璐 我一進店門垄琐,熙熙樓的掌柜王于貴愁眉苦臉地迎上來边酒,“玉大人,你說我怎么就攤上這事狸窘《针” “怎么了?”我有些...
    開封第一講書人閱讀 164,057評論 0 354
  • 文/不壞的土叔 我叫張陵翻擒,是天一觀的道長氓涣。 經(jīng)常有香客問我,道長陋气,這世上最難降的妖魔是什么劳吠? 我笑而不...
    開封第一講書人閱讀 58,509評論 1 293
  • 正文 為了忘掉前任,我火速辦了婚禮巩趁,結果婚禮上痒玩,老公的妹妹穿的比我還像新娘。我一直安慰自己议慰,他們只是感情好凰荚,可當我...
    茶點故事閱讀 67,562評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著褒脯,像睡著了一般便瑟。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上番川,一...
    開封第一講書人閱讀 51,443評論 1 302
  • 那天到涂,我揣著相機與錄音脊框,去河邊找鬼。 笑死践啄,一個胖子當著我的面吹牛浇雹,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播屿讽,決...
    沈念sama閱讀 40,251評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼昭灵,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了伐谈?” 一聲冷哼從身側響起烂完,我...
    開封第一講書人閱讀 39,129評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎诵棵,沒想到半個月后抠蚣,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,561評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡履澳,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,779評論 3 335
  • 正文 我和宋清朗相戀三年嘶窄,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片距贷。...
    茶點故事閱讀 39,902評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡柄冲,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出忠蝗,到底是詐尸還是另有隱情羊初,我是刑警寧澤,帶...
    沈念sama閱讀 35,621評論 5 345
  • 正文 年R本政府宣布什湘,位于F島的核電站,受9級特大地震影響晦攒,放射性物質發(fā)生泄漏闽撤。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,220評論 3 328
  • 文/蒙蒙 一脯颜、第九天 我趴在偏房一處隱蔽的房頂上張望哟旗。 院中可真熱鬧,春花似錦栋操、人聲如沸闸餐。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,838評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽舍沙。三九已至,卻和暖如春剔宪,著一層夾襖步出監(jiān)牢的瞬間拂铡,已是汗流浹背壹无。 一陣腳步聲響...
    開封第一講書人閱讀 32,971評論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留感帅,地道東北人斗锭。 一個月前我還...
    沈念sama閱讀 48,025評論 2 370
  • 正文 我出身青樓,卻偏偏與公主長得像失球,于是被迫代替她去往敵國和親岖是。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 44,843評論 2 354