Unity-背景模糊處理-毛玻璃效果

前言


拖更了好久hhhh.游戲上線忙活,期間搞了一些爬蟲相關(guān)的東西和服務(wù)器相關(guān)的東西,之后也會(huì)分享一些爬蟲相關(guān)的文章,開源部分代碼。有時(shí)間就來(lái)補(bǔ)一篇實(shí)用的文章


Unity的屏幕模糊處理

先說說常見3種游戲中模糊處理的方法:

  • 在攝像機(jī)上掛一個(gè)monoBehavior腳本,在onRenderImage方法中使用對(duì)應(yīng)的shader進(jìn)行模糊處理. 主要是針對(duì)整個(gè)屏幕處理。
  • 通過UnityShader中的GrabPass,然后進(jìn)行模糊處理.
  • 針對(duì)想要的攝像機(jī)進(jìn)行截屏,而后進(jìn)行模糊處理.
    本文以第3種方式實(shí)現(xiàn)對(duì)于背景的截圖.原因如下: 線上機(jī)型眾多,并不是所有機(jī)型都支持GrabPass.

實(shí)現(xiàn)

模糊效果的實(shí)現(xiàn)分成c#和shader兩部分. 代碼部分為偽代碼.


c#部分實(shí)現(xiàn)

控制模糊效果的參數(shù)

[Range(1, 4)]
public int iteration = 1;   // 迭代次數(shù)
[Range(1,8)]
public int downSample = 2;  // 降采樣比例
[Range(0.2f, 3.0f)]
public int blurSpread = 2f;  // 模糊范圍

屏幕截圖方法

// 進(jìn)行降采用處理
// Screen.width為屏幕寬度, Screen.height為屏幕高度
// 根據(jù)實(shí)際項(xiàng)目調(diào)整
int rtW = Screen.width / downSample;
int rtH  = Screen.height / downSample;

// 獲得指定攝像機(jī)渲染的屏幕像素
if (texture == null) {
    texture = RenderTexture.GetTemporary(rtW, rtH, 24);
}
RenderTexture source = RenderTexture.GetTemporary(rtW, rtH, 24);
source.filterMode = FilterMode.Bilinear;
targetCamera.targetTexture = source;  // targetCamera 是需要使用的攝像機(jī)
RenderTexture.active = source;
targetCamera.Render();
RenderTexture buffer0 = RenderTexture.GetTemporary(rtW, rtH, 24);
buffer0.filterMode = FilterMode.Bilinear;
Graphics.Blit(source, buffer0);
// 進(jìn)行模糊迭代,這里注意是i是從1起
for (int i = 1; i < iterations; ++i) {
    material.SetFloat("_BlurSize", 1.0f + i * blurSpread);
    RenderTexture buffer1 = RenderTexture.GetTemporary(rtW, rtH, 24);
    Graphics.Blit(buffer0, buffer1, material, 0);  // materail是使用對(duì)應(yīng)blurshader的材質(zhì)
    RenderTexture.ReleaseTemporary(buffer0);  // 申請(qǐng)了就要釋放
    buffer0 = buffer1;
}
Graphics.Blit(buffer0, texture);

RenderTexture.ReleaseTemporary(buffer0);
RenderTexture.ReleaseTemporary(source);
targetCamera.targetTexture = originTexture;
RenderTexture.active = null;
material.SetTexture("_MainTex", tex);

至此完成了c#部分的代碼片段。

  • 以上代碼為偽代碼,直接使用需要根據(jù)項(xiàng)目不同未對(duì)應(yīng)變量設(shè)置正確的值
  • 以上代碼中核心方法為Graphics.Blit(),使用該方法進(jìn)行調(diào)用對(duì)應(yīng)pass進(jìn)行模糊.
  • RenderTexture申請(qǐng)和釋放要注意不要泄漏
  • dowmSample 可以提高效率,降低采樣成本,
  • iterations 值越大,效果越好,消耗越高
  • blurSpread 影響模糊效果

shader部分


采用高斯模糊,具體什么是高斯模糊我這里就不解釋了哈.
這里我只用單pass實(shí)現(xiàn)模糊,因?yàn)閷?duì)于效果要求不高.如果有需要可以實(shí)現(xiàn)縱向和橫向的pass

Shader "GassianBlur"
{
    Properties
    {
        _MainTex("Base(RGB)", 2D) = "white" {}
        _BlurSize("Blure Size", Float) = 1.0
    }
    SubShader
    {
        Tags {"Queue" = "Transparent"}
        CGINCLUDE
        #include "UnityCG.cginc"
        #include "UnityUI.cginc"
        sampler2D _MainTex;
        half4 _MainTex_TexelSize;
        float _BlurSize;
        struct v2f
        {
            float uv[5]: TEXCOORD0;
            float pos : SV_POSITION;
        };
        v2f vertBlur(appdata_img v) 
        {
             v2f o;
             o.pos = UnityObjectToClipPos(v.vertex);
             half2 uv = v.texcoord;
             o.uv[0] = uv;
             o.uv[1] = uv + float2(_MainTex_TexelSize.x *1.0,  _MainTex_TexelSize.y) * _BlurSize;
             o.uv[2] = uv - float2(_MainTex_TexelSize.x *1.0,  _MainTex_TexelSize.y) * _BlurSize;
             o.uv[3] = uv + float2(_MainTex_TexelSize.x *2.0,  _MainTex_TexelSize.y * 2.0) * _BlurSize;
             o.uv[4] = uv - float2(_MainTex_TexelSize.x *2.0,  _MainTex_TexelSize.y * 2.0) * _BlurSize;
             return o;
        }
        fixed4 fragBlur(v2f i) : SV_Target 
        {
            float weight[3] = {0.4026, 0.2442, 0.0545);
            fixed3 sum = tex2D(_MainTex, i.uv[0]).rgb * weight[0];
            for (int it = 1; it < 3; it++)
            {
                sum += tex2D(_MainTex, i.uv[it * 2 - 1]).rgb * weight[0];
                sum += tex2D(_MainTex, i.uv[it * 2]).rgb * weight[0];
             }
             return fixed4(sum, 1.0)
        }
        ENDCG
        ZTEST ALWAYS Cull Off ZWrite Off
        Pass
        {
             CGPROGRAM
             #pragma vertex vertBlur
             #pragma fragment fragBlur
             ENDCG
        }
    }
    Fallback Off
}

結(jié)語(yǔ)


以上代碼若要在實(shí)際中使用,請(qǐng)根據(jù)情況進(jìn)行調(diào)整。這里不太方便放成果圖,就不貼。該思路經(jīng)過了線上游戲的驗(yàn)證。
若本文出現(xiàn)錯(cuò)漏,或者有更好的方法實(shí)現(xiàn)模糊效果歡迎留評(píng).
ps: 簡(jiǎn)書的代碼片段自動(dòng)換行怎么辦呀,這縮進(jìn)要自己輸入'空格'.....

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容